Skip to content

Commit 7201818

Browse files
committed
fix(lint): stop CSS comments in <style> from manufacturing phantom root tags
extractOpenTags scans raw source text with a flat regex that has no concept of <style>/<script> block boundaries, so a CSS comment like `/* <g> wrapper */` inside a <style> block reads as a real open tag. findRootTag consumes that flat tag list and only skips tags literally named script/style/meta/link/title, so the phantom <g> tag (not in that skip list) wins the "first non-ignored body tag" search and gets returned as the composition root instead of the real one that follows. This manufactured root_missing_composition_id and root_missing_dimensions (the phantom tag has neither) plus head_leaked_text (the leaked-text scan slices up to the phantom tag's position, landing inside the <style> block before its real closing tag, so the raw CSS text reads as leaked markup) on an otherwise valid sub-composition — reported with an exact bisected repro: a <template>-wrapped SVG sub-composition whose <style> block comments reference an inner <g> element. Fix: compute <style>/<script> content spans up front (reusing the existing extractBlocks + STYLE_BLOCK_PATTERN/SCRIPT_BLOCK_PATTERN) and skip any TAG_PATTERN match that falls inside one, before it ever reaches findRootTag or any other extractOpenTags consumer. Same shape as the prior fix for a leading <svg> defs block being mistaken for the root (8ee4b7d) — this closes a sibling gap in the same function. Test: new regression case with a <style> block containing a `/* <g> */` comment ahead of an <svg data-composition-id> root, asserting none of the three findings fire. Full lint package suite (318 tests) passes.
1 parent dd774b3 commit 7201818

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

‎packages/lint/src/rules/core.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,29 @@ describe("core rules", () => {
143143
expect(result.findings.find((f) => f.code === "root_missing_dimensions")).toBeUndefined();
144144
});
145145

146+
it("does not mistake a <tag>-shaped CSS comment inside <style> for the composition root", async () => {
147+
// Regression: a CSS comment referencing an SVG tag name (e.g. `/* <g> wrapper */`)
148+
// inside a <style> block reads as a real open tag to the flat TAG_PATTERN scan,
149+
// manufacturing a phantom root before the real composition root and firing
150+
// root_missing_composition_id/root_missing_dimensions/head_leaked_text on an
151+
// otherwise valid sub-composition.
152+
const html = `
153+
<html><body>
154+
<style>
155+
/* <g> wrapper for icon groups */
156+
.icon { fill: currentColor; }
157+
</style>
158+
<svg id="root" data-composition-id="c1" data-width="1920" data-height="1080">
159+
<g class="icon"></g>
160+
</svg>
161+
<script>window.__timelines = window.__timelines || {};</script>
162+
</body></html>`;
163+
const result = await lintHyperframeHtml(html);
164+
expect(result.findings.find((f) => f.code === "root_missing_composition_id")).toBeUndefined();
165+
expect(result.findings.find((f) => f.code === "root_missing_dimensions")).toBeUndefined();
166+
expect(result.findings.find((f) => f.code === "head_leaked_text")).toBeUndefined();
167+
});
168+
146169
it("reports error when timeline registry is missing", async () => {
147170
const html = `
148171
<html><body>

‎packages/lint/src/utils.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,34 @@ const TIMELINE_REGISTRY_OBJECT_BODY_PATTERN = /window\.__timelines\s*=\s*\{([\s\
5151
const TIMELINE_REGISTRY_OBJECT_ENTRY_PATTERN =
5252
/(?:["']([^"']+)["']|([A-Za-z_$][\w$]*))\s*:\s*[A-Za-z_$][\w$]*/g;
5353

54+
// <style>/<script> content is CSS/JS text, not markup — a CSS comment like
55+
// `/* <g> wrapper */` or a JS comment mentioning a tag name otherwise reads as
56+
// a real open tag to TAG_PATTERN's flat regex scan, throwing off every
57+
// consumer that relies on tag order/position (findRootTag in particular:
58+
// a phantom tag from inside a <style> block can outrank the real
59+
// composition root). Compute the content spans up front and skip any match
60+
// that falls inside one, the same way findRootTag already special-cases
61+
// <style>/<script> tags themselves.
62+
function findStyleAndScriptContentRanges(source: string): Array<[number, number]> {
63+
const ranges: Array<[number, number]> = [];
64+
for (const pattern of [STYLE_BLOCK_PATTERN, SCRIPT_BLOCK_PATTERN]) {
65+
for (const block of extractBlocks(source, pattern)) {
66+
const contentStart = block.index + block.raw.indexOf(block.content);
67+
ranges.push([contentStart, contentStart + block.content.length]);
68+
}
69+
}
70+
return ranges;
71+
}
72+
5473
export function extractOpenTags(source: string): OpenTag[] {
5574
const tags: OpenTag[] = [];
75+
const skipRanges = findStyleAndScriptContentRanges(source);
5676
let match: RegExpExecArray | null;
5777
const pattern = new RegExp(TAG_PATTERN.source, TAG_PATTERN.flags);
5878
while ((match = pattern.exec(source)) !== null) {
5979
const raw = match[0];
6080
if (raw.startsWith("</") || raw.startsWith("<!")) continue;
81+
if (skipRanges.some(([start, end]) => match!.index >= start && match!.index < end)) continue;
6182
tags.push({
6283
raw,
6384
name: (match[1] || "").toLowerCase(),

0 commit comments

Comments
 (0)