|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const { |
| 4 | + RESPELLINGS, |
| 5 | + attributeSites, |
| 6 | + oneLine, |
| 7 | + respell, |
| 8 | + sweepDocument, |
| 9 | + tokenStream |
| 10 | +} = require("../tooling/check-html-invariants"); |
| 11 | + |
| 12 | +// One attribute per value shape the respellings have to decide about: a |
| 13 | +// delimiter inside the value, a reference, a space, `>`, and an astral character. |
| 14 | +const SHAPES = |
| 15 | + '<input a="1" b=\'q"t\' c=bare d e="a&b" f="a>b" g="x y" h="😀">'; |
| 16 | + |
| 17 | +describe("check-html-invariants", () => { |
| 18 | + describe("attributeSites", () => { |
| 19 | + it("reports every attribute with the tag holding it", () => { |
| 20 | + const sites = attributeSites('<p id=a class="b c">t</p>'); |
| 21 | + expect(sites.map((site) => [site.name, site.value])).toEqual([ |
| 22 | + ["id", "a"], |
| 23 | + ["class", "b c"] |
| 24 | + ]); |
| 25 | + expect(sites.map((site) => [site.tagStart, site.tagEnd])).toEqual([ |
| 26 | + [0, 20], |
| 27 | + [0, 20] |
| 28 | + ]); |
| 29 | + }); |
| 30 | + |
| 31 | + it("reads a valueless attribute as an empty value", () => { |
| 32 | + expect(attributeSites("<input checked>")[0]).toMatchObject({ |
| 33 | + name: "checked", |
| 34 | + value: "" |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + it("leaves a close tag's attributes alone, as the tree builder does", () => { |
| 39 | + expect(attributeSites("<p>t</p id=a>")).toEqual([]); |
| 40 | + }); |
| 41 | + |
| 42 | + it("decodes a value through its references", () => { |
| 43 | + expect( |
| 44 | + attributeSites('<a t="a&b<c">').map((s) => s.value) |
| 45 | + ).toEqual(["a&b<c"]); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + describe("tokenStream", () => { |
| 50 | + it("reads three spellings of one attribute identically", () => { |
| 51 | + const quoted = tokenStream('<form method="get">'); |
| 52 | + expect(tokenStream("<form method=get>")).toBe(quoted); |
| 53 | + expect(tokenStream('<form method="get">')).toBe(quoted); |
| 54 | + expect(tokenStream("<FORM METHOD='get'>")).toBe(quoted); |
| 55 | + }); |
| 56 | + |
| 57 | + it("separates two documents that say different things", () => { |
| 58 | + expect(tokenStream("<form method=get>")).not.toBe( |
| 59 | + tokenStream("<form method=post>") |
| 60 | + ); |
| 61 | + expect(tokenStream("<input checked>")).not.toBe( |
| 62 | + tokenStream("<input checked=checked>") |
| 63 | + ); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe("respell", () => { |
| 68 | + for (const respelling of RESPELLINGS) { |
| 69 | + it(`keeps what the document says through ${respelling.name}`, () => { |
| 70 | + const respelled = respell(SHAPES, attributeSites(SHAPES), respelling); |
| 71 | + expect(respelled).not.toBe(SHAPES); |
| 72 | + expect(tokenStream(respelled)).toBe(tokenStream(SHAPES)); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + it("declines a value no delimiter can hold", () => { |
| 77 | + const respelling = |
| 78 | + /** @type {import("../tooling/check-html-invariants").Respelling} */ ( |
| 79 | + RESPELLINGS.find((one) => one.name === "unquote") |
| 80 | + ); |
| 81 | + const respelled = respell(SHAPES, attributeSites(SHAPES), respelling); |
| 82 | + expect(respelled).toContain('g="x y"'); |
| 83 | + expect(respelled).toContain('f="a>b"'); |
| 84 | + expect(respelled).toContain("c=bare"); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe("oneLine", () => { |
| 89 | + // Built rather than written: a control character in this file makes git |
| 90 | + // read it as binary and stop showing its diff. |
| 91 | + const control = (/** @type {number} */ code) => String.fromCharCode(code); |
| 92 | + |
| 93 | + it("shows a control character the whitespace collapse would hide", () => { |
| 94 | + expect(oneLine(`a${control(0x0b)}b`, 40)).toBe("a\\x0bb"); |
| 95 | + expect(oneLine(`a${control(0)}b`, 40)).toBe("a\\x00b"); |
| 96 | + }); |
| 97 | + |
| 98 | + it("collapses the whitespace a tag is written with", () => { |
| 99 | + expect(oneLine("<p\n\tid=a\t>", 40)).toBe("<p id=a >"); |
| 100 | + }); |
| 101 | + |
| 102 | + it("cuts what is past the limit", () => { |
| 103 | + expect(oneLine("abcdef", 4)).toBe("abc…"); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe("sweepDocument", () => { |
| 108 | + it("reports nothing about a printer that is stable and spelling-blind", () => { |
| 109 | + // Canonicalizing, so every spelling of the tag reaches one output. An |
| 110 | + // identity printer is not it: echoing a respelling is depending on it. |
| 111 | + const minify = (/** @type {string} */ html) => |
| 112 | + html.replace(/^<(\w+)[\S\s]*>$/, "<$1>"); |
| 113 | + expect(sweepDocument(minify, SHAPES)).toEqual([]); |
| 114 | + }); |
| 115 | + |
| 116 | + it("reports a printer whose second pass moves", () => { |
| 117 | + // One space per pass, so no pass is ever the last. |
| 118 | + const minify = (/** @type {string} */ html) => html.replace(" ", ""); |
| 119 | + const reports = sweepDocument(minify, SHAPES); |
| 120 | + expect(reports.map((one) => one.relation)).toContain("idempotence"); |
| 121 | + }); |
| 122 | + |
| 123 | + it("reports a printer that folds one spelling and not another", () => { |
| 124 | + // Reaches the quoted spelling only, which is the shape of the defect |
| 125 | + // this sweep exists for. |
| 126 | + const minify = (/** @type {string} */ html) => |
| 127 | + html.replace(/[=]"GET"/g, '="get"'); |
| 128 | + const reports = sweepDocument(minify, '<form method="GET">'); |
| 129 | + expect(reports.map((one) => one.relation)).toContain( |
| 130 | + "respelling unquote" |
| 131 | + ); |
| 132 | + }); |
| 133 | + |
| 134 | + it("reports a printer that throws on a respelling", () => { |
| 135 | + const minify = (/** @type {string} */ html) => { |
| 136 | + if (html.includes("&#x")) throw new Error("nope"); |
| 137 | + return html; |
| 138 | + }; |
| 139 | + const reports = sweepDocument(minify, '<a t="ab">'); |
| 140 | + expect(reports).toContainEqual( |
| 141 | + expect.objectContaining({ |
| 142 | + relation: "respelling references", |
| 143 | + what: "threw: nope" |
| 144 | + }) |
| 145 | + ); |
| 146 | + }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments