Skip to content

Commit c35f5d7

Browse files
committed
fix some specificity stuff
1 parent e624b71 commit c35f5d7

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

packages/svelte/src/compiler/phases/2-analyze/css/css-analyze.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ const analysis_visitors = {
7373
...context.state,
7474
rule: node
7575
});
76+
77+
node.metadata.has_local_selectors = node.prelude.children.some((selector) => {
78+
return selector.children.some(
79+
({ metadata }) => !metadata.is_global && !metadata.is_host && !metadata.is_root
80+
);
81+
});
7682
}
7783
};
7884

packages/svelte/src/compiler/phases/3-transform/css/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,20 @@ const visitors = {
172172

173173
// if this selector list belongs to a rule, require a specificity bump for the
174174
// first scoped selector but only if we're at the top level
175-
// TODO this isn't quite right, it would break with `:global(x) {...}`
176-
const parent = path.at(-1);
175+
let parent = path.at(-1);
177176
if (parent?.type === 'Rule') {
178-
specificity = { bumped: !!parent.metadata.parent_rule };
177+
specificity = { bumped: false };
178+
179+
/** @type {import('#compiler').Css.Rule | null} */
180+
let rule = parent.metadata.parent_rule;
181+
182+
while (rule) {
183+
if (rule.metadata.has_local_selectors) {
184+
specificity = { bumped: true };
185+
break;
186+
}
187+
rule = rule.metadata.parent_rule;
188+
}
179189
}
180190

181191
next({ ...state, specificity });

packages/svelte/src/compiler/types/css.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface Rule extends BaseNode {
2727
block: Block;
2828
metadata: {
2929
parent_rule: null | Rule;
30+
has_local_selectors: boolean;
3031
};
3132
}
3233

0 commit comments

Comments
 (0)