|
1 | 1 | import { describe, it, expect } from "vitest"; |
2 | | -import { stripCssComments, stripJsComments, stripJsStringLiterals } from "./utils.js"; |
| 2 | +import { |
| 3 | + parseHtmlStructure, |
| 4 | + stripCssComments, |
| 5 | + stripJsComments, |
| 6 | + stripJsStringLiterals, |
| 7 | +} from "./utils.js"; |
3 | 8 |
|
4 | 9 | const scan = (src: string) => stripJsStringLiterals(stripJsComments(src)); |
5 | 10 | const findsRaf = (src: string) => /requestAnimationFrame\s*\(/.test(scan(src)); |
6 | 11 |
|
| 12 | +describe("parseHtmlStructure source ranges", () => { |
| 13 | + it("does not include ignored markup inside a preceding malformed closing tag", () => { |
| 14 | + const source = "<p>x</p ignored<noise><span>y</span>"; |
| 15 | + const tags = parseHtmlStructure(source).tags; |
| 16 | + expect(tags[1]).toMatchObject({ name: "span", raw: "<span>", attrs: "", index: 22 }); |
| 17 | + }); |
| 18 | + |
| 19 | + it("keeps a less-than inside a malformed tag name in the original source range", () => { |
| 20 | + const tags = parseHtmlStructure("<div<foo>body</div<foo>").tags; |
| 21 | + expect(tags.map(({ name, raw, attrs, index }) => ({ name, raw, attrs, index }))).toEqual([ |
| 22 | + { name: "div<foo", raw: "<div<foo>", attrs: "", index: 0 }, |
| 23 | + ]); |
| 24 | + }); |
| 25 | + |
| 26 | + it("uses the original name boundary when Unicode lowercasing changes its length", () => { |
| 27 | + const source = '<Aİİİ data-check="<" data-flag=x>body</Aİİİ>'; |
| 28 | + const tags = parseHtmlStructure(source).tags; |
| 29 | + expect(tags.map(({ name, raw, attrs, index }) => ({ name, raw, attrs, index }))).toEqual([ |
| 30 | + { |
| 31 | + name: "ai̇i̇i̇", |
| 32 | + raw: '<Aİİİ data-check="<" data-flag=x>', |
| 33 | + attrs: ' data-check="<" data-flag=x', |
| 34 | + index: 0, |
| 35 | + }, |
| 36 | + ]); |
| 37 | + }); |
| 38 | + |
| 39 | + it("keeps malformed names and quoted less-than values after a multiline close", () => { |
| 40 | + const source = '<p>x</p\n ><div<foo data-expr="a < b">y</div<foo>'; |
| 41 | + const tags = parseHtmlStructure(source).tags; |
| 42 | + expect(tags[1]).toMatchObject({ |
| 43 | + name: "div<foo", |
| 44 | + raw: '<div<foo data-expr="a < b">', |
| 45 | + attrs: ' data-expr="a < b"', |
| 46 | + index: 11, |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it("does not reuse implied-open origins for the following explicit tag", () => { |
| 51 | + const source = '</p></br><Aİ data-expr="x < y">text</Aİ>'; |
| 52 | + const tags = parseHtmlStructure(source).tags; |
| 53 | + expect(tags.map(({ name, raw, attrs, index }) => ({ name, raw, attrs, index }))).toEqual([ |
| 54 | + { name: "p", raw: "</p>", attrs: "p", index: 0 }, |
| 55 | + { name: "br", raw: "</br>", attrs: "r", index: 4 }, |
| 56 | + { name: "ai̇", raw: '<Aİ data-expr="x < y">', attrs: ' data-expr="x < y"', index: 9 }, |
| 57 | + ]); |
| 58 | + }); |
| 59 | + |
| 60 | + it("does not use apparent tags in comments or raw-text scripts as an opening origin", () => { |
| 61 | + const source = '<!-- <fake> --><script>const text = "<fake>";</script\n ><div<foo>x</div<foo>'; |
| 62 | + const tags = parseHtmlStructure(source).tags; |
| 63 | + expect(tags.map(({ name, raw, attrs }) => ({ name, raw, attrs }))).toEqual([ |
| 64 | + { name: "script", raw: "<script>", attrs: "" }, |
| 65 | + { name: "div<foo", raw: "<div<foo>", attrs: "" }, |
| 66 | + ]); |
| 67 | + }); |
| 68 | + |
| 69 | + it("starts each open tag at its own < after a multiline closing tag", () => { |
| 70 | + const source = `<span data-expr="x < y">A</span\n ><span>B</span>`; |
| 71 | + const tags = parseHtmlStructure(source).tags; |
| 72 | + expect(tags.map(({ raw, index }) => ({ raw, index }))).toEqual([ |
| 73 | + { raw: `<span data-expr="x < y">`, index: 0 }, |
| 74 | + { raw: `<span>`, index: source.indexOf("<span>") }, |
| 75 | + ]); |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
7 | 79 | describe("stripJsStringLiterals", () => { |
8 | 80 | it("blanks a call the composition only renders as text", () => { |
9 | 81 | expect(findsRaf('const CODE = "requestAnimationFrame(step);";')).toBe(false); |
|
0 commit comments