import { templateIdentity } from "./strings"; export function escapeHtml(unsafe: string): string { return `${unsafe}` .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, """) .replace(/'/g, "'"); } import.meta.vitest?.test("escapeHtml", ({ expect }) => { // Test with empty string expect(escapeHtml("")).toBe(""); // Test with string without special characters expect(escapeHtml("hello world")).toBe("hello world"); // Test with special characters expect(escapeHtml("
")).toBe("<div>"); expect(escapeHtml("a & b")).toBe("a & b"); expect(escapeHtml('a "quoted" string')).toBe("a "quoted" string"); expect(escapeHtml("it's a test")).toBe("it's a test"); // Test with multiple special characters expect(escapeHtml("It's a link")).toBe( "<a href="test">It's a link</a>" ); }); export function html(strings: TemplateStringsArray, ...values: any[]): string { return templateIdentity(strings, ...values.map(v => escapeHtml(`${v}`))); } import.meta.vitest?.test("html", ({ expect }) => { // Test with no interpolation expect(html`simple string`).toBe("simple string"); // Test with string interpolation expect(html`Hello, ${"world"}!`).toBe("Hello, world!"); // Test with number interpolation expect(html`Count: ${42}`).toBe("Count: 42"); // Test with HTML special characters in interpolated values expect(html`
${"