Skip to content

Write HTML. Render video. Built for agents.

fix(lint): read attribute values with the HTML parser (#5209)

Abdullah M. Nazifb4c847aca2c1
diff --git a/packages/lint/src/rules/core.test.ts b/packages/lint/src/rules/core.test.ts
index 81c37f13ec..347d4ba642 100644
--- a/packages/lint/src/rules/core.test.ts
+++ b/packages/lint/src/rules/core.test.ts
@@ -38,6 +38,30 @@ function portraitCompositionWithScaffold(bodyCss: string, viewportContent: strin
}
describe("core rules", () => {
+ it("does not invent a composition host from an attribute mentioned in a title", async () => {
+ const html = `<html><body>
+ <div id="root" data-composition-id="main" data-no-timeline data-width="640" data-height="360">
+ <span id="example" title='data-composition-src="ghost.html"'>Example</span>
+ </div>
+ </body></html>`;
+ const result = await lintHyperframeHtml(html);
+ expect(result.findings.map((finding) => finding.code)).not.toContain(
+ "host_missing_composition_id",
+ );
+ });
+
+ it("does not invent timing from attributes mentioned in a title", async () => {
+ const html = `<html><body>
+ <div id="root" data-composition-id="main" data-no-timeline data-width="640" data-height="360">
+ <span id="example" title='data-start="0" data-duration="1"'>Example</span>
+ </div>
+ </body></html>`;
+ const result = await lintHyperframeHtml(html);
+ expect(result.findings.map((finding) => finding.code)).not.toContain(
+ "timed_element_missing_clip_class",
+ );
+ });
+
it("does not lint scripts embedded inside an iframe srcdoc attribute", async () => {
const html = `
<html><body>
diff --git a/packages/lint/src/rules/media.test.ts b/packages/lint/src/rules/media.test.ts
index 6e761b0721..bc9bf8b07a 100644
--- a/packages/lint/src/rules/media.test.ts
+++ b/packages/lint/src/rules/media.test.ts
@@ -2,6 +2,16 @@ import { describe, it, expect } from "vitest";
import { lintHyperframeHtml } from "../hyperframeLinter.js";
describe("media rules", () => {
+ it("reports a missing real media id even when the title mentions one", async () => {
+ const html = `<html><body>
+ <div id="root" data-composition-id="main" data-width="640" data-height="360">
+ <video title='id="ghost"' src="clip.mp4" data-start="0" data-duration="1" muted></video>
+ </div>
+ </body></html>`;
+ const result = await lintHyperframeHtml(html);
+ expect(result.findings.map((finding) => finding.code)).toContain("media_missing_id");
+ });
+
it.each([
'title="an unmuted muted crossorigin clip"',
"title='an unmuted muted crossorigin clip'",
diff --git a/packages/lint/src/utils.test.ts b/packages/lint/src/utils.test.ts
index cc78ecfdbf..f32ec2bcc6 100644
--- a/packages/lint/src/utils.test.ts
+++ b/packages/lint/src/utils.test.ts
@@ -2,6 +2,8 @@ import { describe, it, expect } from "vitest";
import {
isAudibleVideoTag,
parseHtmlStructure,
+ readAttr,
+ readDecodedAttr,
stripCssComments,
stripJsComments,
stripJsStringLiterals,
@@ -10,6 +12,42 @@ import {
const scan = (src: string) => stripJsStringLiterals(stripJsComments(src));
const findsRaf = (src: string) => /requestAnimationFrame\s*\(/.test(scan(src));
+describe("HTML attribute values", () => {
+ it.each([
+ { source: `<div title='id="ghost"' id="real">`, name: "id", expected: "real" },
+ {
+ source: `<div title='data-composition-src="ghost.html"'>`,
+ name: "data-composition-src",
+ expected: null,
+ },
+ { source: `<img title='src="ghost.png"' src="real.png">`, name: "src", expected: "real.png" },
+ { source: `<div title='class="ghost"' class="clip">`, name: "class", expected: "clip" },
+ { source: `<div id="scene's-title">`, name: "id", expected: "scene's-title" },
+ { source: `<div title='scene "title"'>`, name: "title", expected: 'scene "title"' },
+ { source: "<div DATA-START = 0>", name: "data-start", expected: "0" },
+ { source: '<div id="" id="ghost">', name: "id", expected: null },
+ { source: '<video data-hf-id="ghost">', name: "id", expected: null },
+ { source: '<video src="clip.mp4?id=7">', name: "id", expected: null },
+ ])("reads only the real $name from $source", ({ source, name, expected }) => {
+ expect(readAttr(source, name)).toBe(expected);
+ });
+
+ it("preserves raw entity text while the decoded reader resolves character references", () => {
+ const tag = '<img src="image.png?x=1&amp;y=2" title="Say &quot;hello&quot;">';
+ expect(readAttr(tag, "src")).toBe("image.png?x=1&amp;y=2");
+ expect(readDecodedAttr(tag, "src")).toBe("image.png?x=1&y=2");
+ expect(readAttr(tag, "title")).toBe("Say &quot;hello&quot;");
+ expect(readDecodedAttr(tag, "title")).toBe('Say "hello"');
+ });
+
+ it("preserves the empty-value contract for raw and decoded reads", () => {
+ expect(readAttr('<video muted="">', "muted")).toBeNull();
+ expect(readDecodedAttr('<video muted="">', "muted")).toBe("");
+ expect(readAttr("<video muted>", "muted")).toBeNull();
+ expect(readDecodedAttr("<video muted>", "muted")).toBe("");
+ });
+});
+
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>";
diff --git a/packages/lint/src/utils.ts b/packages/lint/src/utils.ts
index 1896a93d0f..b27fccba1a 100644
--- a/packages/lint/src/utils.ts
+++ b/packages/lint/src/utils.ts
@@ -223,14 +223,7 @@ export function hasUnquotedLessThan(attrs: string): boolean {
}
export function readAttr(tagSource: string, attr: string): string | null {
- if (!tagSource) return null;
- const escaped = attr.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
- // `(?<![\w-])` not `\b`: a plain `\b` boundary treats the hyphen in a longer
- // attribute as a word break, so reading "id" would wrongly match the trailing
- // `id="…"` inside `data-hf-id="…"` (and "width" inside `data-width`, etc.).
- // The lookbehind requires the match to start a fresh attribute name.
- const match = tagSource.match(new RegExp(`(?<![\\w-])${escaped}\\s*=\\s*["']([^"']+)["']`, "i"));
- return match?.[1] || null;
+ return readAttributeValue(tagSource, attr, false) || null;
}
export function hasAttrName(tagSource: string, attr: string): boolean {
@@ -263,15 +256,23 @@ export function mediaWindowsOverlap(
/** Read an HTML attribute using browser-equivalent character-reference decoding. */
export function readDecodedAttr(tagSource: string, attr: string): string | null {
+ return readAttributeValue(tagSource, attr, true);
+}
+
+function readAttributeValue(
+ tagSource: string,
+ attr: string,
+ decodeEntities: boolean,
+): string | null {
if (!tagSource) return null;
let value: string | null = null;
const parser = new Parser(
{
- onattribute(name, decodedValue) {
- if (value === null && name.toLowerCase() === attr.toLowerCase()) value = decodedValue;
+ onattribute(name, attributeValue) {
+ if (value === null && name.toLowerCase() === attr.toLowerCase()) value = attributeValue;
},
},
- { decodeEntities: true, lowerCaseAttributeNames: false, lowerCaseTags: true },
+ { decodeEntities, lowerCaseAttributeNames: false, lowerCaseTags: true },
);
parser.end(tagSource);
return value;