// HTML reclassifier tests — the full language() entry point that embeds // JavaScript and CSS via the reclassifier pipeline. import { describe, it, expect } from "vitest"; import { tokenize as make_language } from "./index.js"; const language = make_language(); function tokens_of(src: string) { const result = language(src); const out = []; for (let i = 0; i < result.tokens.length / 3; i++) { out.push({ type: result.token_types[result.tokens[i * 3]], value: src.slice(result.tokens[i * 3 + 1], result.tokens[i * 3 + 2]), start: result.tokens[i * 3 + 1], end: result.tokens[i * 3 + 2], }); } return out; } function type_of(tokens: { type: string; value: string }[], value: string) { return tokens.find((t) => t.value === value)?.type; } describe("HTML language — script embedding", () => { it("tokenizes script body as JavaScript", () => { const src = ""; const tokens = tokens_of(src); // The script body should be JS tokens, not raw_script. expect(tokens.some((t) => t.type === "raw_script")).toBe(false); expect(type_of(tokens, "const")).toBe("keyword"); // JS reclassifiers run inside the embed too: `const x` promotes the // binding to `constant` via promote_js_const_bindings. expect(type_of(tokens, "x")).toBe("constant"); expect(type_of(tokens, "5")).toBe("number"); }); it("applies JS reclassifiers (function-variable) inside the script", () => { const src = ""; const tokens = tokens_of(src); // `foo` should be detected as a function by JS's function-variable // rule, which ran inside the sub-language before splicing. expect(type_of(tokens, "foo")).toBe("function"); }); it("remaps positions from content-local to input-global", () => { const src = ""; const tokens = tokens_of(src); const const_tok = tokens.find((t) => t.value === "const"); // `const` in the source text is at offset 8 (after `"; const tokens = tokens_of(src); // First few tokens are HTML open tag boundary/name. expect(tokens[0]).toMatchObject({ type: "punctuation", value: "<" }); expect(tokens[1]).toMatchObject({ type: "tag_name", value: "script" }); expect(tokens[2]).toMatchObject({ type: "punctuation", value: ">" }); // Last three tokens are the closing tag, split into // ``. const last = tokens.length - 1; expect(tokens[last - 2]).toMatchObject({ type: "punctuation", value: "", }); }); it("handles multiple script blocks independently", () => { const src = ""; const tokens = tokens_of(src); // Both a and b should be JS identifiers. expect(type_of(tokens, "a")).toBe("identifier"); expect(type_of(tokens, "b")).toBe("identifier"); // No raw_script leaked through. expect(tokens.some((t) => t.type === "raw_script")).toBe(false); }); }); describe("HTML language — style embedding", () => { it("tokenizes style body as CSS", () => { const src = ""; const tokens = tokens_of(src); expect(tokens.some((t) => t.type === "raw_style")).toBe(false); // `.btn` is a CSS class selector. expect(type_of(tokens, ".btn")).toBe("selector_class"); // `color` is a CSS property. expect(type_of(tokens, "color")).toBe("property"); }); it("remaps CSS positions to input-global", () => { const src = ""; const tokens = tokens_of(src); const prop = tokens.find((t) => t.value === "margin"); // `margin` starts at offset 11 in the source. expect(prop?.start).toBe(11); expect(prop?.end).toBe(17); }); }); describe("HTML language — combined document", () => { it("composes HTML + CSS + JS in a single pass", () => { const src = `

Hello

`; const tokens = tokens_of(src); // No raw_* tokens leaked through. expect(tokens.some((t) => t.type === "raw_script")).toBe(false); expect(tokens.some((t) => t.type === "raw_style")).toBe(false); // CSS tokens are present. expect(tokens.some((t) => t.type === "property" && t.value === "color")).toBe(true); // JS tokens are present and JS reclassifiers ran. expect(type_of(tokens, "greet")).toBe("function"); // HTML tokens are also present. expect(tokens.some((t) => t.type === "tag_name" && t.value === "p")).toBe(true); expect(tokens.some((t) => t.type === "attr_name" && t.value === "class")).toBe(true); }); it("token offsets form a non-overlapping, monotonically increasing sequence", () => { const src = `

text

`; const result = language(src); let prev_end = 0; for (let i = 0; i < result.tokens.length / 3; i++) { const start = result.tokens[i * 3 + 1]; const end = result.tokens[i * 3 + 2]; expect(start).toBeGreaterThanOrEqual(prev_end); expect(end).toBeGreaterThan(start); prev_end = end; } }); it("falling back to raw grammar still works for consumers who want plain HTML", async () => { // This verifies the separate exports — grammar (raw) and reclassifiers // (list) — are accessible and usable. const { grammar, reclassifiers } = await import("./index.js"); const { tokenize } = await import("@twinkleplop/core"); const src = "

plain

"; // Raw tokenize — no embedding applied. const raw = tokenize(src, grammar); expect(raw.tokens.length).toBeGreaterThan(0); expect(Array.isArray(reclassifiers)).toBe(true); expect(reclassifiers.length).toBeGreaterThan(0); }); });