Skip to content

suggestion: add html() and css() tagged template functions #6726

Open
@iuioiua

Description

@iuioiua

@std/http/file-server already uses a html() function that simply returns the passed in arguments as strings but enabled formatting thanks to improvements to deno fmt. I suggest we factor that out somewhere in this codebase, as well as a css(). Maybe @std/http/syntax or @std/html/syntax.

Tests

import { assertEquals } from "@std/assert";
import { html } from "@std/html/syntax";

Deno.test("html returns raw interpolated string", () => {
  const name = "world";
  const result = html`
    <p>Hello, ${name}!</p>
  `;
  assertEquals(
    result,
    `
    <p>Hello, world!</p>
  `,
  );
});

Deno.test("html handles multiple interpolations", () => {
  const a = "foo";
  const b = "bar";
  const result = html`
    <p>A: ${a}, B: ${b}</p>
  `;
  assertEquals(
    result,
    `
    <p>A: foo, B: bar</p>
  `,
  );
});

Deno.test("html handles no interpolations", () => {
  const result = html`
    <p>Just a string</p>
  `;
  assertEquals(
    result,
    `
    <p>Just a string</p>
  `,
  );
});

Deno.test("html handles special characters", () => {
  const special = "<>&\"'";
  const result = html`
    <p>Special: ${special}</p>
  `;
  assertEquals(
    result,
    `
    <p>Special: <>&\"'</p>
  `,
  );
});

Deno.test("html preserves raw backslashes and escapes", () => {
  const path = "\\foo\\bar";
  const result = html`
    <p>Path: ${path}</p>
  `;
  assertEquals(
    result,
    `
    <p>Path: \\foo\\bar</p>
  `,
  );
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions