import { describe, expect, it, vi } from "vitest"; import { Parser, Tokenizer } from "./index.js"; import type { Handler } from "./Parser.js"; const arrayIndex = /^\d+$/; // Count indexed reads, including the moves made by shift/unshift/splice. function countArrayReads(parser: Parser, key: string, run: () => void): number { const arrays = parser as unknown as Record; let reads = 0; arrays[key] = new Proxy(arrays[key], { get(target, property, receiver) { if (typeof property === "string" && arrayIndex.test(property)) { reads++; } return Reflect.get(target, property, receiver); }, }); run(); return reads; } // Record open and close tag events in the order they are emitted. function tagEvents(html: string): string[] { const events: string[] = []; new Parser({ onopentagname(name) { events.push(`<${name}>`); }, onclosetag(name) { events.push(``); }, }).end(html); return events; } describe("API", () => { it("should work without callbacks", () => { const cbs: Partial = { onerror: vi.fn() }; const p = new Parser(cbs, { xmlMode: true, lowerCaseAttributeNames: true, }); p.end("boohay"); p.write("foo"); // Check for an error p.end(); p.write("foo"); expect(cbs.onerror).toHaveBeenLastCalledWith( new Error(".write() after done!"), ); p.end(); expect(cbs.onerror).toHaveBeenLastCalledWith( new Error(".end() after done!"), ); // Should ignore the error if there is no callback delete cbs.onerror; p.write("foo"); p.reset(); // Remove method cbs.onopentag = vi.fn(); p.write(""); // Pause/resume const onText = vi.fn(); cbs.ontext = onText; p.pause(); p.write("foo"); expect(onText).not.toHaveBeenCalled(); p.resume(); expect(onText).toHaveBeenLastCalledWith("foo"); p.pause(); expect(onText).toHaveBeenCalledTimes(1); p.resume(); expect(onText).toHaveBeenCalledTimes(1); p.pause(); p.end("bar"); expect(onText).toHaveBeenCalledTimes(1); p.resume(); expect(onText).toHaveBeenCalledTimes(2); expect(onText).toHaveBeenLastCalledWith("bar"); }); it("should back out of numeric entities (#125)", () => { const onend = vi.fn(); let text = ""; const p = new Parser({ ontext(data) { text += data; }, onend, }); p.end("id=770&#anchor"); expect(onend).toHaveBeenCalledTimes(1); expect(text).toBe("id=770&#anchor"); p.reset(); text = ""; p.end("0&#xn"); expect(onend).toHaveBeenCalledTimes(2); expect(text).toBe("0&#xn"); }); it("should not have the start index be greater than the end index", () => { const onopentag = vi.fn(); const onclosetag = vi.fn(); const p = new Parser({ onopentag(tag) { expect(p.startIndex).toBeLessThanOrEqual(p.endIndex); onopentag(tag, p.startIndex, p.endIndex); }, onclosetag(tag) { expect(p.startIndex).toBeLessThanOrEqual(p.endIndex); onclosetag(tag, p.endIndex); }, }); p.write("

"); expect(onopentag).toHaveBeenLastCalledWith("p", 0, 2); expect(onclosetag).not.toHaveBeenCalled(); p.write("Foo"); p.write("


"); expect(onopentag).toHaveBeenLastCalledWith("hr", 6, 9); expect(onclosetag).toHaveBeenCalledTimes(2); expect(onclosetag).toHaveBeenNthCalledWith(1, "p", 9); expect(onclosetag).toHaveBeenNthCalledWith(2, "hr", 9); }); it("should preserve declaration names outside of HTML mode", () => { const onprocessinginstruction = vi.fn(); new Parser( { onprocessinginstruction, }, { xmlMode: true }, ).end(""); expect(onprocessinginstruction).toHaveBeenCalledWith( "!DOCTYPEhtml", "!DOCTYPEhtml", ); }); it("should preserve declaration casing when lowerCaseTags is disabled", () => { const onprocessinginstruction = vi.fn(); new Parser( { onprocessinginstruction, }, { lowerCaseTags: false }, ).end(""); expect(onprocessinginstruction).toHaveBeenCalledWith( "!DOCTYPE", "!DOCTYPEhtml", ); }); it("should update the position when a single tag is spread across multiple chunks", () => { let isCalled = false; const p = new Parser({ onopentag() { isCalled = true; expect(p.startIndex).toBe(0); expect(p.endIndex).toBe(12); }, }); p.write("
"); expect(isCalled).toBe(true); }); it("should have the correct position for implied opening tags", () => { let isCalled = false; const p = new Parser({ onopentag() { isCalled = true; expect(p.startIndex).toBe(0); expect(p.endIndex).toBe(3); }, }); p.write("

"); expect(isCalled).toBe(true); }); it("should parse <__proto__> (#387)", () => { const p = new Parser(null); // Should not throw p.write("<__proto__>"); }); it("should implicitly close table sections when another section opens", () => { const onopentagname = vi.fn(); const onclosetag = vi.fn(); // must auto-close new Parser({ onopentagname, onclosetag }).end( "
F
B
", ); expect(onclosetag).toHaveBeenCalledWith("tfoot", true); const tfootClose = onclosetag.mock.calls.findIndex( ([name]) => name === "tfoot", ); const tbodyOpen = onopentagname.mock.calls.findIndex( ([name]) => name === "tbody", ); expect(tfootClose).toBeLessThan(tbodyOpen); }); it("should close an open

when a block element that implies closing starts", () => { const onopentag = vi.fn(); const onclosetag = vi.fn(); new Parser({ onopentag, onclosetag }).end( "

text

inner
", ); expect(onclosetag).toHaveBeenCalledWith("p", true); const pClose = onclosetag.mock.calls.findIndex( ([name]) => name === "p", ); const hgroupOpen = onopentag.mock.calls.findIndex( ([name]) => name === "hgroup", ); expect(pClose).toBeLessThan(hgroupOpen); }); it("should close an open

when

starts", () => { expect(tagEvents("

text

inner")).toEqual([ "

", "

", "", "", ]); }); it("should close an open

together with the previous list item", () => { expect( tagEvents("

  • a

  • b

c

d
"), ).toEqual([ "
    ", "
  • ", "

    ", "

    ", "
  • ", "
  • ", "
  • ", "
", "
", "
", "

", "

", "
", "
", "
", "
", ]); }); it("should implicitly close when opens", () => { const onopentagname = vi.fn(); const onclosetag = vi.fn(); new Parser({ onopentagname, onclosetag }).end( "
B
H
", ); expect(onclosetag).toHaveBeenCalledWith("tbody", true); const tbodyClose = onclosetag.mock.calls.findIndex( ([name]) => name === "tbody", ); const theadOpen = onopentagname.mock.calls.findIndex( ([name]) => name === "thead", ); expect(tbodyClose).toBeLessThan(theadOpen); }); it("should implicitly close when opens", () => { const onclosetag = vi.fn(); const onopentagname = vi.fn(); new Parser({ onclosetag, onopentagname }).end( "
AB
", ); // must auto-close , making them siblings per the HTML spec expect(onclosetag).toHaveBeenCalledWith("td", true); const tdClose = onclosetag.mock.calls.findIndex( ([name]) => name === "td", ); const thOpen = onopentagname.mock.calls.findIndex( ([name]) => name === "th", ); expect(tdClose).toBeLessThan(thOpen); }); it.each(["div", "svg", "a"])( "should do linear stack work for nested %s elements", (name) => { const depth = 500; const parser = new Parser({}, { xmlMode: name === "a" }); const input = `<${name}>`.repeat(depth) + ``.repeat(depth); const reads = countArrayReads(parser, "stack", () => parser.end(input), ); expect(reads).toBeLessThan(depth * 10); }, ); it("should do linear work on the foreign context stack", () => { const depth = 500; const parser = new Parser({}); const reads = countArrayReads(parser, "foreignContext", () => parser.end("".repeat(depth) + "".repeat(depth)), ); expect(reads).toBeLessThan(depth * 10); }); it("should stop closing elements when reset at the end of the input", () => { const closed: string[] = []; const parser = new Parser({ onclosetag(name) { closed.push(name); if (name === "b") parser.reset(); }, }); parser.end("
"); expect(closed).toStrictEqual(["c", "b"]); }); it.each(["abc", "