Skip to content

Commit 1e2c7d6

Browse files
jrusso1020claude
andauthored
fix(core): preserve nested-rule selectors in composition CSS scoping (#2721) (#2733)
Chrome 112+ / Firefox 117+ / Safari 16.5+ support native CSS Nesting. A nested rule like '.title { … }' inside '[data-composition-id="intro"] { … }' resolves at match time to '<parent> .title' via the implicit '&' prefix. 'scopeCssToComposition' walks every rule via 'root.walkRules' and re- scopes selectors, but it did so for nested rules too — producing '[…scope…] .title' inside '[…scope…] { … }', which nesting then prepends AGAIN to '[…scope…] […scope…] .title'. Since the composition root only appears once in the DOM, the doubly-scoped selector never matches — the nested rule appears 'just ignored' as the reporter described (#2721). Reproduced on 0.7.66 with the reporter's exact composition. Fix: add 'isNestedInsideAnotherRule' predicate — mirrors the existing 'isInsideGlobalAtRule' — and skip nested rules in the walkRules callback. Top-level rules still get scoped; their nested descendants inherit scope naturally via CSS Nesting at match time. Added two focused tests: - 'preserves nested-rule selectors so CSS Nesting inheritance works (#2721)' — asserts nested '.title' and 'h2' selectors stay verbatim while top-level rules keep scoping. - 'preserves deeply-nested CSS Nesting rules (#2721)' — same rule at depth 3. All 37 existing scopeCssToComposition tests still pass. Fixes #2721. Co-authored-by: Claude Opus 4.7 <[email protected]>
1 parent a637f39 commit 1e2c7d6

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

‎packages/core/src/compiler/compositionScoping.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,51 @@ window.__afterTimeline = window.__timelines.scene;
800800
expect(result).toContain('[id="intro"]');
801801
});
802802

803+
it("preserves nested-rule selectors so CSS Nesting inheritance works (#2721)", () => {
804+
// Chrome 112+ / Firefox 117+ / Safari 16.5+ support native CSS Nesting.
805+
// A nested rule like `.title { … }` inside `[data-composition-id="intro"] { … }`
806+
// resolves at match time to `<parent> .title` via the implicit `&` prefix.
807+
// The scoper must NOT re-apply the composition scope to the nested selector —
808+
// that produces `<scope> <scope> .title`, which matches nothing because the
809+
// composition root only appears once in the DOM.
810+
const scoped = scopeCssToComposition(
811+
`
812+
[data-composition-id="intro"] h1 { color: red; }
813+
[data-composition-id="intro"] {
814+
.title { color: brown; }
815+
h2 { color: blue; }
816+
}
817+
`,
818+
"intro",
819+
);
820+
// Top-level rules still get scoped.
821+
expect(scoped).toContain('[data-composition-id="intro"] h1');
822+
// Nested rules keep their author-original selectors verbatim so CSS Nesting
823+
// can prepend the parent's `&` at match time.
824+
expect(scoped).toMatch(/\.title\s*\{/);
825+
expect(scoped).toMatch(/h2\s*\{/);
826+
// The pre-fix bug re-scoped nested rules to `[…] .title`, which never matched.
827+
expect(scoped).not.toContain('[data-composition-id="intro"] .title');
828+
expect(scoped).not.toContain('[data-composition-id="intro"] h2');
829+
});
830+
831+
it("preserves deeply-nested CSS Nesting rules (#2721)", () => {
832+
const scoped = scopeCssToComposition(
833+
`
834+
[data-composition-id="intro"] {
835+
.card {
836+
.header { font-weight: bold; }
837+
}
838+
}
839+
`,
840+
"intro",
841+
);
842+
expect(scoped).toMatch(/\.card\s*\{/);
843+
expect(scoped).toMatch(/\.header\s*\{/);
844+
expect(scoped).not.toContain('[data-composition-id="intro"] .card');
845+
expect(scoped).not.toContain('[data-composition-id="intro"] .header');
846+
});
847+
803848
it("wraps scripts with authored root id normalization for #id GSAP selectors", () => {
804849
const { document } = parseHTML(`
805850
<div data-composition-id="intro">

‎packages/core/src/compiler/compositionScoping.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,24 @@ function isInsideGlobalAtRule(rule: Rule): boolean {
200200
return false;
201201
}
202202

203+
/**
204+
* A Rule nested inside another Rule (CSS Nesting Module Level 1) already
205+
* inherits scope from its parent's `&` prefix at match time — re-applying
206+
* the composition scope to the nested selector produces
207+
* `<scope> <scope> .child`, which matches nothing when the composition
208+
* root only appears once in the DOM. Only top-level rules get scoped;
209+
* their nested descendants inherit the scope naturally via CSS nesting.
210+
* See #2721 for the reproducer that motivated this.
211+
*/
212+
function isNestedInsideAnotherRule(rule: Rule): boolean {
213+
let current: Node["parent"] = rule.parent;
214+
while (current) {
215+
if (current.type === "rule") return true;
216+
current = current.parent;
217+
}
218+
return false;
219+
}
220+
203221
export function scopeCssToComposition(
204222
css: string,
205223
compositionId: string,
@@ -216,6 +234,7 @@ export function scopeCssToComposition(
216234

217235
root.walkRules((rule) => {
218236
if (isInsideGlobalAtRule(rule)) return;
237+
if (isNestedInsideAnotherRule(rule)) return;
219238
rule.selectors = rule.selectors.map((selector) =>
220239
scopeSelector(
221240
selector,

0 commit comments

Comments
 (0)