Skip to content

Commit 8ee4b7d

Browse files
fix(lint): stop a leading <svg> defs block from being mistaken for the composition root (heygen-com#1867)
Two independent post-release feedback reports of the same mechanism: a leading <svg> block (icon/gradient/filter <defs>, referenced by url(#id) elsewhere in the document) placed before the real [data-composition-id] root manufactures root_missing_composition_id + root_missing_dimensions on an otherwise-correct composition. Moving the <svg> after the root cleared both findings for each reporter. findRootTag returned the first body child that wasn't script/style/meta/ link/title, unconditionally — <svg> was never in that skip list, so a leading defs-only <svg> got treated as the root. Fix: skip a leading <svg> when it carries none of the composition markers itself (data-composition-id/data-width/data-height), so an intentionally SVG-rooted composition is still eligible as the root. The first attempt at this only skipped the <svg> open tag, which surfaced a second bug: extractOpenTags is a flat, nesting-unaware scan, so the very next tag it returns after skipping <svg> is the svg's own nested child (<defs>, <filter>, ...), not the sibling after </svg>. Track the svg's closing tag position and skip every tag before it, not just the <svg> tag itself. Tests: skips a leading svg defs block (no false root findings); still treats an <svg> as the root when data-composition-id/data-width/ data-height are declared directly on it. Full lint suite (308 tests) passes.
1 parent d2f1adc commit 8ee4b7d

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,37 @@ describe("core rules", () => {
112112
expect(result.findings.find((f) => f.code === "root_missing_dimensions")).toBeUndefined();
113113
});
114114

115+
it("skips a leading <svg> defs block when detecting the composition root", async () => {
116+
// Regression: two independent reports of a leading <svg><defs><filter>...
117+
// block (icon/gradient/filter plumbing referenced via url(#id) elsewhere)
118+
// getting mistaken for the composition root, since findRootTag returned
119+
// the first non-script/style/meta/link/title body child unconditionally.
120+
// The <svg> here carries no composition markers, so it must be skipped in
121+
// favor of the real root that follows it.
122+
const html = `
123+
<html><body>
124+
<svg width="0" height="0" style="position:absolute">
125+
<defs><filter id="glow"><feGaussianBlur stdDeviation="4" /></filter></defs>
126+
</svg>
127+
<div id="root" data-composition-id="c1" data-width="1920" data-height="1080"></div>
128+
<script>window.__timelines = window.__timelines || {};</script>
129+
</body></html>`;
130+
const result = await lintHyperframeHtml(html);
131+
expect(result.findings.find((f) => f.code === "root_missing_composition_id")).toBeUndefined();
132+
expect(result.findings.find((f) => f.code === "root_missing_dimensions")).toBeUndefined();
133+
});
134+
135+
it("still treats an <svg> as the root when it carries composition markers itself", async () => {
136+
const html = `
137+
<html><body>
138+
<svg id="root" data-composition-id="c1" data-width="1920" data-height="1080"></svg>
139+
<script>window.__timelines = window.__timelines || {};</script>
140+
</body></html>`;
141+
const result = await lintHyperframeHtml(html);
142+
expect(result.findings.find((f) => f.code === "root_missing_composition_id")).toBeUndefined();
143+
expect(result.findings.find((f) => f.code === "root_missing_dimensions")).toBeUndefined();
144+
});
145+
115146
it("reports error when timeline registry is missing", async () => {
116147
const html = `
117148
<html><body>

‎packages/lint/src/utils.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export function findHtmlTag(source: string): OpenTag | null {
100100
};
101101
}
102102

103+
// fallow-ignore-next-line complexity
103104
export function findRootTag(source: string): OpenTag | null {
104105
const bodyOpenMatch = /<body\b([^>]*)>/i.exec(source);
105106
const bodyCloseMatch = /<\/body>/i.exec(source);
@@ -123,8 +124,34 @@ export function findRootTag(source: string): OpenTag | null {
123124
: source.length;
124125
const bodyContent = bodyOpenMatch ? source.slice(bodyStart, bodyEnd) : source;
125126
const bodyTags = extractOpenTags(bodyContent);
127+
// Set when a leading <svg> defs block is skipped (see below) — extractOpenTags
128+
// is a flat, nesting-unaware scan, so without this the very next tag it
129+
// returns is the svg's own nested child (<defs>, <filter>, ...), not the
130+
// sibling that follows the closed </svg>.
131+
let skipBefore = -1;
126132
for (const tag of bodyTags) {
133+
if (tag.index < skipBefore) continue;
127134
if (["script", "style", "meta", "link", "title"].includes(tag.name)) continue;
135+
// A leading <svg> block (icon/gradient/filter <defs>, referenced by url(#id)
136+
// from elsewhere in the document) is shared visual plumbing, not the
137+
// composition root — two independent reports of this being mistaken for
138+
// the root, manufacturing root_missing_composition_id/root_missing_dimensions
139+
// on an otherwise-correct composition. Only skip it when it carries none of
140+
// the composition markers itself, so an intentionally SVG-rooted composition
141+
// (data-composition-id/data-width/data-height directly on the <svg>) is
142+
// still eligible as the root.
143+
if (
144+
tag.name === "svg" &&
145+
!readAttr(tag.raw, "data-composition-id") &&
146+
!readAttr(tag.raw, "data-width") &&
147+
!readAttr(tag.raw, "data-height")
148+
) {
149+
const closeMatch = /<\/svg\s*>/i.exec(bodyContent.slice(tag.index));
150+
// No closing tag found (malformed HTML) — skip everything rather than
151+
// risk returning one of the svg's own children as the root.
152+
skipBefore = closeMatch ? tag.index + closeMatch.index + closeMatch[0].length : Infinity;
153+
continue;
154+
}
128155
return { ...tag, index: tag.index + bodyStart };
129156
}
130157
return null;

0 commit comments

Comments
 (0)