Skip to content

Commit e003815

Browse files
feat(lint): flag CSS rules that select on the inline style attribute (#6261)
* feat(lint): flag CSS rules that select on the inline style attribute * docs(lint): keep the new and corrected rule entries to short lines
1 parent 6f4d46a commit e003815

3 files changed

Lines changed: 92 additions & 3 deletions

File tree

‎docs/reference/lint-rules.mdx‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,15 @@ Source: `packages/lint/src/rules/core.ts`.
258258

259259
**Fix:** Rename the id to start with a letter (recommended), or build selectors with `CSS.escape(id)` at runtime.
260260

261+
### `inline_style_attribute_selector`
262+
263+
**Error.** A selector reads the inline `style` attribute (`[style]`, `[style*=...]` and the other forms).
264+
GSAP writes inline styles every frame, and each write then restyles far more than the animated element.
265+
The preview slows to a crawl.
266+
267+
**Fix:** Let the runtime hide hidden clips; it already hides their contents.
268+
For a state of your own, toggle a class or data attribute from your script and select on that.
269+
261270
### `invalid_inline_script_syntax`
262271

263272
**Error.** An inline `<script>` is closed with malformed syntax.
@@ -304,7 +313,8 @@ Source: `packages/lint/src/rules/core.ts`.
304313

305314
**Error.** A selector observes the runtime-owned hidden style (used to hide native video during frame copying) and forces `opacity` to zero, which breaks the video replacement pipeline.
306315

307-
**Fix:** Restrict the guard to sub-composition hosts (for example `[data-composition-src][style*="visibility: hidden"]`) and do not derive element or media opacity from the runtime's hidden style.
316+
**Fix:** Remove the rule; the runtime already hides a hidden clip and its contents.
317+
To fade or hide something yourself, animate it on the timeline, or toggle a class or data attribute and select on that.
308318

309319
### `scoped_css_missing_wrapper`
310320

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

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,8 @@ describe("core rules", () => {
14191419
expect(finding?.severity).toBe("error");
14201420
expect(finding?.selector).toBe(`[style*="visibility: hidden"]`);
14211421
expect(finding?.message).toContain("replacement frame");
1422-
expect(finding?.fixHint).toContain("data-composition-src");
1422+
expect(finding?.fixHint).not.toContain("[style");
1423+
expect(finding?.fixHint).toContain("class or data attribute");
14231424
});
14241425

14251426
it("errors when composition scoping still leaves the hidden-style selector on video", async () => {
@@ -1472,6 +1473,51 @@ describe("core rules", () => {
14721473
});
14731474
});
14741475

1476+
describe("inline_style_attribute_selector", () => {
1477+
const findingsFor = async (css: string) =>
1478+
(
1479+
await lintHyperframeHtml(`
1480+
<html><head><style>${css}</style></head><body>
1481+
<div id="root" data-composition-id="main" data-width="1920" data-height="1080"></div>
1482+
<script>window.__timelines = { main: gsap.timeline({ paused: true }) };</script>
1483+
</body></html>`)
1484+
).findings.filter((item) => item.code === "inline_style_attribute_selector");
1485+
1486+
it.each([
1487+
`[style] { color: red; }`,
1488+
`.scene[style*="visibility: hidden"] * { visibility: hidden !important; }`,
1489+
`div[style^="opacity"] { color: red; }`,
1490+
`div[style$="none;"] { color: red; }`,
1491+
`div[style~="x"] { color: red; }`,
1492+
`div[style|="x"] { color: red; }`,
1493+
`[STYLE*="hidden" i] { color: red; }`,
1494+
`.card:not([style*="opacity: 0"]) { color: red; }`,
1495+
])("errors on a rule that selects on the inline style attribute: %s", async (css) => {
1496+
const [finding] = await findingsFor(css);
1497+
expect(finding?.severity).toBe("error");
1498+
expect(finding?.message).toContain("every frame");
1499+
expect(finding?.fixHint).toContain("class or data attribute");
1500+
expect(finding?.fixHint).not.toContain("[style");
1501+
});
1502+
1503+
it.each([
1504+
`.scene.is-hidden * { visibility: hidden; }`,
1505+
`[data-state="hidden"] { opacity: 0; }`,
1506+
`[data-style="bold"] { font-weight: 700; }`,
1507+
`[styles] { color: red; }`,
1508+
`[data-hf-style] { color: red; }`,
1509+
])("allows a selector that does not read the inline style: %s", async (css) => {
1510+
expect(await findingsFor(css)).toEqual([]);
1511+
});
1512+
1513+
it("reports a selector once and each selector of a list on its own", async () => {
1514+
const findings = await findingsFor(
1515+
`[style*="a"], .x[style] { color: red; } [style*="a"] { color: blue; }`,
1516+
);
1517+
expect(findings.map((item) => item.selector)).toEqual([`[style*="a"]`, `.x[style]`]);
1518+
});
1519+
});
1520+
14751521
describe("id_override_reduced_specificity", () => {
14761522
const comp = (css: string) => `
14771523
<html><head><style>${css}</style></head><body>

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,20 @@ function selectorAliasesRuntimeHiddenStyle(selector: string): boolean {
196196
return unsafe;
197197
}
198198

199+
function selectsOnInlineStyle(selector: string): boolean {
200+
let found = false;
201+
try {
202+
selectorParser((root) => {
203+
root.walkAttributes((node) => {
204+
if (node.attribute.toLowerCase() === "style") found = true;
205+
});
206+
}).processSync(selector);
207+
} catch {
208+
return false;
209+
}
210+
return found;
211+
}
212+
199213
const POSITION_PROPERTIES = new Set(["left", "top", "right", "bottom", "inset"]);
200214

201215
function weakIdOf(node: selectorParser.Node): string | null {
@@ -713,6 +727,7 @@ export const coreRules: Array<(ctx: LintContext) => HyperframeLintFinding[]> = [
713727
const findings: HyperframeLintFinding[] = [];
714728
const reportedRepeatedIds = new Set<string>();
715729
const reportedHiddenStyleSelectors = new Set<string>();
730+
const reportedInlineStyleSelectors = new Set<string>();
716731
const reportedReducedIdSelectors = new Set<string>();
717732
for (const style of styles) {
718733
let root: postcss.Root;
@@ -748,6 +763,23 @@ export const coreRules: Array<(ctx: LintContext) => HyperframeLintFinding[]> = [
748763
});
749764
}
750765

766+
if (!reportedInlineStyleSelectors.has(selector) && selectsOnInlineStyle(selector)) {
767+
reportedInlineStyleSelectors.add(selector);
768+
findings.push({
769+
code: "inline_style_attribute_selector",
770+
severity: "error",
771+
message:
772+
`Selector "${selector}" reads the inline \`style\` attribute. GSAP writes inline styles every ` +
773+
"frame, and each write then restyles far more than the animated element, " +
774+
"so the preview slows to a crawl.",
775+
selector,
776+
fixHint:
777+
"Let the runtime hide hidden clips; it already hides their contents. For a state of your own, " +
778+
"toggle a class or data attribute from your script and select on that.",
779+
snippet: truncateSnippet(rule.toString()),
780+
});
781+
}
782+
751783
if (
752784
!forcesOpacityZero ||
753785
reportedHiddenStyleSelectors.has(selector) ||
@@ -762,7 +794,8 @@ export const coreRules: Array<(ctx: LintContext) => HyperframeLintFinding[]> = [
762794
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.`,
763795
selector,
764796
fixHint:
765-
'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.',
797+
"Remove this rule; the runtime already hides a hidden clip and its contents. To fade or hide something " +
798+
"yourself, animate it on the timeline, or toggle a class or data attribute and select on that.",
766799
snippet: truncateSnippet(rule.toString()),
767800
});
768801
}

0 commit comments

Comments
 (0)