Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions packages/lint/src/rules/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,30 @@ describe("core rules", () => {
).toBeUndefined();
});

it.each([
["adjacent", `<span>A</span><span>B</span>`],
["spaces", `<span>A</span ><span>B</span>`],
["newline", `<span>A</span\n ><span>B</span>`],
])(
"does not flag valid sibling spans when the closing tag uses %s whitespace",
async (_label, body) => {
const result = await lintHyperframeHtml(compositionWithBodyPrefix(body));
expect(
result.findings.find((f) => f.code === "unclosed_tag_swallowed_element"),
).toBeUndefined();
},
);

it.each([`<span class="first" <span>B</span>`, `<span data-label=first <strong>B</strong>`])(
"still flags a malformed span start tag that swallows its next element",
async (body) => {
const result = await lintHyperframeHtml(compositionWithBodyPrefix(body));
const finding = result.findings.find((f) => f.code === "unclosed_tag_swallowed_element");
expect(finding?.severity).toBe("error");
expect(finding?.snippet).toContain("<span");
},
);

it("does not flag a legitimate attribute value containing a raw <", async () => {
const html = compositionWithBodyPrefix(`<div data-expr="x < y">hi</div>`);
const result = await lintHyperframeHtml(html);
Expand Down
74 changes: 73 additions & 1 deletion packages/lint/src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,81 @@
import { describe, it, expect } from "vitest";
import { stripCssComments, stripJsComments, stripJsStringLiterals } from "./utils.js";
import {
parseHtmlStructure,
stripCssComments,
stripJsComments,
stripJsStringLiterals,
} from "./utils.js";

const scan = (src: string) => stripJsStringLiterals(stripJsComments(src));
const findsRaf = (src: string) => /requestAnimationFrame\s*\(/.test(scan(src));

describe("parseHtmlStructure source ranges", () => {
it("does not include ignored markup inside a preceding malformed closing tag", () => {
const source = "<p>x</p ignored<noise><span>y</span>";
const tags = parseHtmlStructure(source).tags;
expect(tags[1]).toMatchObject({ name: "span", raw: "<span>", attrs: "", index: 22 });
});

it("keeps a less-than inside a malformed tag name in the original source range", () => {
const tags = parseHtmlStructure("<div<foo>body</div<foo>").tags;
expect(tags.map(({ name, raw, attrs, index }) => ({ name, raw, attrs, index }))).toEqual([
{ name: "div<foo", raw: "<div<foo>", attrs: "", index: 0 },
]);
});

it("uses the original name boundary when Unicode lowercasing changes its length", () => {
const source = '<Aİİİ data-check="<" data-flag=x>body</Aİİİ>';
const tags = parseHtmlStructure(source).tags;
expect(tags.map(({ name, raw, attrs, index }) => ({ name, raw, attrs, index }))).toEqual([
{
name: "ai̇i̇i̇",
raw: '<Aİİİ data-check="<" data-flag=x>',
attrs: ' data-check="<" data-flag=x',
index: 0,
},
]);
});

it("keeps malformed names and quoted less-than values after a multiline close", () => {
const source = '<p>x</p\n ><div<foo data-expr="a < b">y</div<foo>';
const tags = parseHtmlStructure(source).tags;
expect(tags[1]).toMatchObject({
name: "div<foo",
raw: '<div<foo data-expr="a < b">',
attrs: ' data-expr="a < b"',
index: 11,
});
});

it("does not reuse implied-open origins for the following explicit tag", () => {
const source = '</p></br><Aİ data-expr="x < y">text</Aİ>';
const tags = parseHtmlStructure(source).tags;
expect(tags.map(({ name, raw, attrs, index }) => ({ name, raw, attrs, index }))).toEqual([
{ name: "p", raw: "</p>", attrs: "p", index: 0 },
{ name: "br", raw: "</br>", attrs: "r", index: 4 },
{ name: "ai̇", raw: '<Aİ data-expr="x < y">', attrs: ' data-expr="x < y"', index: 9 },
]);
});

it("does not use apparent tags in comments or raw-text scripts as an opening origin", () => {
const source = '<!-- <fake> --><script>const text = "<fake>";</script\n ><div<foo>x</div<foo>';
const tags = parseHtmlStructure(source).tags;
expect(tags.map(({ name, raw, attrs }) => ({ name, raw, attrs }))).toEqual([
{ name: "script", raw: "<script>", attrs: "" },
{ name: "div<foo", raw: "<div<foo>", attrs: "" },
]);
});

it("starts each open tag at its own < after a multiline closing tag", () => {
const source = `<span data-expr="x < y">A</span\n ><span>B</span>`;
const tags = parseHtmlStructure(source).tags;
expect(tags.map(({ raw, index }) => ({ raw, index }))).toEqual([
{ raw: `<span data-expr="x < y">`, index: 0 },
{ raw: `<span>`, index: source.indexOf("<span>") },
]);
});
});

describe("stripJsStringLiterals", () => {
it("blanks a call the composition only renders as text", () => {
expect(findsRaf('const CODE = "requestAnimationFrame(step);";')).toBe(false);
Expand Down
31 changes: 28 additions & 3 deletions packages/lint/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,37 @@ export function parseHtmlStructure(source: string): {
contentStart: number;
index: number;
}> = [];
let explicitOpenTag: { index: number; nameEnd: number } | null = null;
const parser: Parser = new Parser(
{
onopentag(name) {
const index = parser.startIndex;
onopentagname(name) {
// startIndex can still point into the preceding close. Bound this scan by
// HTML name delimiters, not '<' (which can occur in a malformed name).
// Keep the raw name end too: Unicode lowercasing can change UTF-16 length.
let tokenStart = parser.endIndex - 1;
while (
tokenStart >= parser.startIndex &&
!/[\t\n\f\r />]/.test(source.charAt(tokenStart))
) {
tokenStart -= 1;
}
const index = source.indexOf("<", tokenStart + 1);
explicitOpenTag =
index >= 0 &&
index < parser.endIndex &&
source.slice(index + 1, parser.endIndex).toLowerCase() === name
? { index, nameEnd: parser.endIndex }
: null;
},
onopentag(name, _attrs, isImplied) {
const origin = !isImplied ? explicitOpenTag : null;
const index = origin?.index ?? parser.startIndex;
explicitOpenTag = null;
const raw = source.slice(index, parser.endIndex + 1);
const attrs = raw.slice(name.length + 1, -1).replace(/\s*\/$/, "");
const rawAttrs = origin
? source.slice(origin.nameEnd, parser.endIndex)
: raw.slice(name.length + 1, -1);
const attrs = rawAttrs.replace(/\s*\/$/, "");
const tag = { raw, name, attrs, index };
tags.push(tag);
const sameNameStack = openTagsByName.get(name) ?? [];
Expand Down
Loading