Skip to content

Commit 2c14438

Browse files
fix(lint): flag hidden-style opacity guards (#3694)
1 parent 4958a9f commit 2c14438

2 files changed

Lines changed: 147 additions & 7 deletions

File tree

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,78 @@ describe("core rules", () => {
870870
});
871871
});
872872

873+
describe("runtime_hidden_style_opacity", () => {
874+
const comp = (css: string, extraMarkup = "") => `
875+
<html><head><style>${css}</style></head><body>
876+
<div id="root" data-composition-id="main" data-width="1920" data-height="1080">
877+
<video id="footage" src="clip.mp4" data-start="0" data-duration="5" muted playsinline></video>
878+
${extraMarkup}
879+
</div>
880+
<script>window.__timelines = { main: gsap.timeline({ paused: true }) };</script>
881+
</body></html>`;
882+
883+
it("errors when a broad hidden-style selector forces replacement-frame opacity to zero", async () => {
884+
const result = await lintHyperframeHtml(
885+
comp(`[style*="visibility: hidden"] { opacity: 0 !important; }`),
886+
);
887+
const finding = result.findings.find((item) => item.code === "runtime_hidden_style_opacity");
888+
889+
expect(finding?.severity).toBe("error");
890+
expect(finding?.selector).toBe(`[style*="visibility: hidden"]`);
891+
expect(finding?.message).toContain("replacement frame");
892+
expect(finding?.fixHint).toContain("data-composition-src");
893+
});
894+
895+
it("errors when composition scoping still leaves the hidden-style selector on video", async () => {
896+
const result = await lintHyperframeHtml(
897+
comp(`#root > video[style*="visibility: hidden"] { opacity: 0; }`),
898+
);
899+
900+
expect(
901+
result.findings.find((item) => item.code === "runtime_hidden_style_opacity")?.selector,
902+
).toBe(`#root > video[style*="visibility: hidden"]`);
903+
});
904+
905+
it("errors when the root stylesheet can affect video mounted from a sub-composition", async () => {
906+
const result = await lintHyperframeHtml(`
907+
<html><head><style>[style*="visibility: hidden"] { opacity: 0; }</style></head><body>
908+
<div id="root" data-composition-id="main" data-width="1920" data-height="1080">
909+
<div data-composition-id="scene" data-composition-src="scene.html"></div>
910+
</div>
911+
<script>window.__timelines = { main: gsap.timeline({ paused: true }) };</script>
912+
</body></html>`);
913+
914+
expect(
915+
result.findings.find((item) => item.code === "runtime_hidden_style_opacity")?.severity,
916+
).toBe("error");
917+
});
918+
919+
it("allows hidden-style opacity guards scoped to sub-composition hosts", async () => {
920+
const result = await lintHyperframeHtml(
921+
comp(
922+
`[data-composition-src][style*="visibility: hidden"],
923+
[data-composition-file][style*="visibility: hidden"] { opacity: 0 !important; }`,
924+
`<div data-composition-id="scene-a" data-composition-src="scene-a.html"></div>
925+
<div data-composition-id="scene-b" data-composition-file="scene-b.html"></div>`,
926+
),
927+
);
928+
929+
expect(
930+
result.findings.find((item) => item.code === "runtime_hidden_style_opacity"),
931+
).toBeUndefined();
932+
});
933+
934+
it("allows broad hidden-style selectors that do not change opacity", async () => {
935+
const result = await lintHyperframeHtml(
936+
comp(`[style*="visibility: hidden"] { pointer-events: none; }`),
937+
);
938+
939+
expect(
940+
result.findings.find((item) => item.code === "runtime_hidden_style_opacity"),
941+
).toBeUndefined();
942+
});
943+
});
944+
873945
describe("css_parse_error — malformed CSS is reported instead of silently swallowed", () => {
874946
it("reports a css_parse_error finding for unparseable CSS", async () => {
875947
const html = `<html><body>

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

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,53 @@ function resolvedRuleSelectors(rule: postcss.Rule): string[] {
9797
);
9898
}
9999

100+
function selectorAliasesRuntimeHiddenStyle(selector: string): boolean {
101+
let unsafe = false;
102+
try {
103+
selectorParser((root) => {
104+
root.each((selectorNode) => {
105+
const subject: selectorParser.Node[] = [];
106+
selectorNode.each((node) => {
107+
if (node.type === "combinator") subject.length = 0;
108+
else subject.push(node);
109+
});
110+
111+
const hostScoped = subject.some(
112+
(node) =>
113+
node.type === "attribute" &&
114+
["data-composition-src", "data-composition-file"].includes(
115+
node.attribute.toLowerCase(),
116+
),
117+
);
118+
if (hostScoped) return;
119+
120+
if (
121+
subject.some((node) => {
122+
if (node.type !== "attribute" || node.attribute.toLowerCase() !== "style") return false;
123+
if (node.operator !== "*=" || !node.value) return false;
124+
const needle = node.insensitive ? node.value.toLowerCase() : node.value;
125+
if (!needle.includes("visibility") && !needle.includes("hidden")) return false;
126+
return "visibility: hidden !important;".includes(needle);
127+
})
128+
) {
129+
unsafe = true;
130+
}
131+
});
132+
}).processSync(selector);
133+
} catch {
134+
return false;
135+
}
136+
return unsafe;
137+
}
138+
139+
function ruleForcesOpacityZero(rule: postcss.Rule): boolean {
140+
let forcesOpacityZero = false;
141+
rule.walkDecls(/^opacity$/i, (declaration) => {
142+
if (Number(declaration.value.trim()) === 0) forcesOpacityZero = true;
143+
});
144+
return forcesOpacityZero;
145+
}
146+
100147
function isStudioTimelineElement(tag: { raw: string; name: string }): boolean {
101148
if (["script", "style", "link", "meta", "template", "noscript"].includes(tag.name)) {
102149
return false;
@@ -333,10 +380,11 @@ export const coreRules: Array<(ctx: LintContext) => HyperframeLintFinding[]> = [
333380
return findings;
334381
},
335382

336-
// repeated_id_descendant_selector
383+
// CSS selector safety
337384
({ styles }) => {
338385
const findings: HyperframeLintFinding[] = [];
339-
const reported = new Set<string>();
386+
const reportedRepeatedIds = new Set<string>();
387+
const reportedHiddenStyleSelectors = new Set<string>();
340388
for (const style of styles) {
341389
let root: postcss.Root;
342390
try {
@@ -350,16 +398,36 @@ export const coreRules: Array<(ctx: LintContext) => HyperframeLintFinding[]> = [
350398
continue;
351399
}
352400
root.walkRules((rule) => {
401+
const forcesOpacityZero = ruleForcesOpacityZero(rule);
353402
for (const selector of resolvedRuleSelectors(rule)) {
354403
const repeatedId = repeatedDescendantId(selector);
355-
if (!repeatedId || reported.has(repeatedId)) continue;
356-
reported.add(repeatedId);
404+
if (repeatedId && !reportedRepeatedIds.has(repeatedId)) {
405+
reportedRepeatedIds.add(repeatedId);
406+
findings.push({
407+
code: "repeated_id_descendant_selector",
408+
severity: "error",
409+
message: `Selector "${selector}" requires #${repeatedId} to be nested inside another #${repeatedId}. IDs must be unique, so this selector cannot match a valid composition.`,
410+
selector,
411+
fixHint: `Remove the duplicate ancestor: change \`#${repeatedId} #${repeatedId}\` to \`#${repeatedId}\`.`,
412+
});
413+
}
414+
415+
if (
416+
!forcesOpacityZero ||
417+
reportedHiddenStyleSelectors.has(selector) ||
418+
!selectorAliasesRuntimeHiddenStyle(selector)
419+
) {
420+
continue;
421+
}
422+
reportedHiddenStyleSelectors.add(selector);
357423
findings.push({
358-
code: "repeated_id_descendant_selector",
424+
code: "runtime_hidden_style_opacity",
359425
severity: "error",
360-
message: `Selector "${selector}" requires #${repeatedId} to be nested inside another #${repeatedId}. IDs must be unique, so this selector cannot match a valid composition.`,
426+
message: `Selector "${selector}" observes HyperFrames' runtime-owned hidden style and forces opacity to zero. The renderer hides each native video before copying its computed opacity to the visible replacement frame, so this rule makes both transparent.`,
361427
selector,
362-
fixHint: `Remove the duplicate ancestor: change \`#${repeatedId} #${repeatedId}\` to \`#${repeatedId}\`.`,
428+
fixHint:
429+
'Restrict the guard to sub-composition hosts, for example `[data-composition-src][style*="visibility: hidden"]` and `[data-composition-file][style*="visibility: hidden"]`. Do not derive arbitrary element or media opacity from runtime-owned inline visibility.',
430+
snippet: truncateSnippet(rule.toString()),
363431
});
364432
}
365433
});

0 commit comments

Comments
 (0)