import { describe, expect, it } from "vitest"; import { XML_NODE_KIND } from "@/core/xml/parse/parse.constants.ts"; import type { XmlNode } from "@/core/xml/parse/parse.types.ts"; import { XmlParser } from "@/core/xml/parse/xml-parser.ts"; function nodesOf(source: string): readonly XmlNode[] { const result = new XmlParser().parse(source); if (!result.ok) { throw new Error(result.error.message); } return result.nodes; } function failure(source: string): { line: number; column: number; message: string } { const result = new XmlParser().parse(source); if (result.ok) { throw new Error("parsed"); } return result.error; } // C-001: a guess would put nodes in the wrong place, so every error names its line and column and the words are kept. describe("XmlParser errors", () => { it("names the closing tag that does not match, with the line of the open tag", () => { expect(failure("\n Hi")).toEqual({ line: 2, column: 11, message: "Invalid XML at line 2, column 11: Expected for the tag at line 2, found .", }); }); it("names the tag left open", () => { expect(failure("\n ")).toEqual({ line: 2, column: 2, message: "Invalid XML at line 2, column 2: is not closed.", }); }); it("refuses a closing tag that closes nothing", () => { expect(failure("\n")).toMatchObject({ line: 2, column: 1, message: expect.stringContaining(" closes nothing."), }); }); it("finds the line in a long document, from the end too", () => { const lines = Array.from({ length: 5000 }, (_value, index) => `${index}`); expect(failure(`${lines.join("\n")}\n`)).toMatchObject({ line: 5001, column: 4 }); }); it("names an attribute given twice at its second name", () => { expect(failure('')).toEqual({ line: 2, column: 4, message: "Invalid XML at line 2, column 4: Attribute b is given twice.", }); }); it("refuses a bare & and says how to write it, at the first character of the text", () => { expect(failure("\n a & b")).toEqual({ line: 2, column: 3, message: 'Invalid XML at line 2, column 3: "& b…" is no entity: write & as &.', }); }); it("refuses an entity it does not know, in a value too", () => { expect(failure("&bogus;").message).toContain("write & as &"); expect(failure('').message).toContain("write & as &"); }); it("refuses a < in a value, and a value without quotes", () => { expect(failure('')).toMatchObject({ line: 1, column: 4, message: expect.stringContaining("write <"), }); expect(failure("")).toMatchObject({ column: 6, message: expect.stringContaining('The value of b needs quotes: b="…".'), }); expect(failure("").message).toContain("needs quotes"); }); it("refuses a value never closed and a tag never ended", () => { expect(failure(' has no closing >."); }); it("refuses a < in text and a declaration", () => { expect(failure("a < b")).toMatchObject({ column: 6, message: expect.stringContaining("Expected a tag name after <."), }); expect(failure("").message).toContain("Expected a tag name after <."); }); it("refuses a comment and a CDATA never closed", () => { expect(failure("."), }); expect(failure("").message).toContain("."); }); }); // C-002, C-005, C-006: what is accepted. describe("XmlParser reading", () => { it("accepts $, - and : in names and both quotes, and several top-level elements", () => { const [first, second] = nodesOf(``); expect(first).toMatchObject({ kind: XML_NODE_KIND.element, type: "Frame", props: { $css: "a: b", "data-theme": "dark", "$on-click": "go" }, line: 1, column: 1, }); expect(second).toMatchObject({ type: "Text", column: 54 }); }); it("skips comments and processing instructions and keeps CDATA verbatim as its own piece", () => { const [element] = nodesOf("x & ]]>y"); expect(element).toMatchObject({ children: [ { kind: XML_NODE_KIND.text, value: "x" }, { kind: XML_NODE_KIND.text, value: " & " }, { kind: XML_NODE_KIND.text, value: "y" }, ], }); }); // C-003, D-17 it("decodes HTML named entities, numeric ones and the six XML ones", () => { const [element] = nodesOf('— AB <&>"' '); expect(element).toMatchObject({ props: { t: "©" }, children: [{ value: "— AB <&>\"'\u00a0" }] }); expect(failure("�").message).toContain("no entity"); expect(failure("�").message).toContain("no entity"); }); // C-004, B-059 it("keeps edge spaces written as references, line and paragraph separators and no-break spaces", () => { const [element] = nodesOf(" a\u2028b\u2029c\u00a0 "); expect(element).toMatchObject({ children: [{ value: " a\u2028b\u2029c\u00a0 " }] }); }); it("drops indentation and joins lines by one space, and keeps a lone space between two spans", () => { expect(nodesOf("\n one\n two\n")[0]).toMatchObject({ children: [{ value: "one two" }] }); expect(nodesOf(" ")[0]).toMatchObject({ children: [{}, { value: " " }, {}] }); expect(nodesOf(" \n \n")).toMatchObject([{ children: [{ value: " " }] }, { children: [] }]); }); it("refuses elements nested deeper than 256 levels at the tag that goes too deep, and never overflows the stack", () => { const nested = (depth: number) => `${"".repeat(depth)}${"".repeat(depth)}`; expect(nodesOf(nested(256))).toHaveLength(1); expect(failure(nested(257))).toEqual({ line: 1, column: 769, message: "Invalid XML at line 1, column 769: Elements nest deeper than 256 levels.", }); expect(failure(nested(100_000)).message).toContain("nest deeper than 256 levels"); }); it("wants a space between attributes", () => { expect(failure('')).toEqual({ line: 1, column: 13, message: "Invalid XML at line 1, column 13: Put a space between attributes.", }); expect(nodesOf('')).toHaveLength(1); expect(nodesOf('')).toHaveLength(1); expect(nodesOf('')).toHaveLength(1); }); it("refuses a processing instruction that is never closed", () => { expect(failure(".", }); }); it("refuses a processing instruction open at the very end of the input", () => { expect(failure(".", }); }); // D-67 it("parses 20x the lines in far less than 400x the time, so the line search is not linear", () => { const document = (count: number) => { const lines = Array.from({ length: count }, (_value, index) => ` line ${index}`); return `\n${lines.join("\n")}\n`; }; // The fastest of several runs is the least disturbed by a busy parallel suite. const fastest = (source: string) => Math.min( ...Array.from({ length: 5 }, () => { const started = performance.now(); nodesOf(source); return performance.now() - started; }), ); nodesOf(document(2000)); // warm-up const small = fastest(document(1000)); const large = fastest(document(20_000)); // Linear is about 20x (measured 18x), a linear line search makes it about 400x; 100x sits between. expect(large / Math.max(small, 0.5)).toBeLessThan(100); }); });