Skip to content

Commit 8119379

Browse files
committed
tada
1 parent 840974b commit 8119379

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,18 @@ import { walk } from 'zimmerframe';
33
import { is_keyframes_node, regex_css_name_boundary, remove_css_prefix } from '../../css.js';
44
import { merge_with_preprocessor_map } from '../../../utils/mapped_code.js';
55

6-
/** @typedef {{ code: MagicString, dev: boolean, hash: string, selector: string, keyframes: string[] }} State */
6+
/**
7+
* @typedef {{
8+
* code: MagicString;
9+
* dev: boolean;
10+
* hash: string;
11+
* selector: string;
12+
* keyframes: string[];
13+
* specificity: {
14+
* bumped: boolean
15+
* }
16+
* }} State
17+
*/
718

819
/**
920
*
@@ -20,7 +31,10 @@ export function render_stylesheet(source, analysis, options) {
2031
dev: options.dev,
2132
hash: analysis.css.hash,
2233
selector: `.${analysis.css.hash}`,
23-
keyframes: analysis.css.keyframes
34+
keyframes: analysis.css.keyframes,
35+
specificity: {
36+
bumped: false
37+
}
2438
};
2539

2640
const ast = /** @type {import('#compiler').Css.StyleSheet} */ (analysis.css.ast);
@@ -124,7 +138,7 @@ const visitors = {
124138

125139
next();
126140
},
127-
SelectorList(node, { state, next }) {
141+
SelectorList(node, { state, next, path }) {
128142
const used = node.children.filter((s) => s.metadata.used);
129143

130144
if (used.length < node.children.length) {
@@ -159,7 +173,11 @@ const visitors = {
159173
}
160174
}
161175

162-
next();
176+
const parent = /** @type {import('#compiler').Css.Node} */ (path.at(-1));
177+
next({
178+
...state,
179+
specificity: parent.type === 'Rule' ? { bumped: false } : state.specificity
180+
});
163181
},
164182
ComplexSelector(node, context) {
165183
/** @param {import('#compiler').Css.SimpleSelector} selector */
@@ -169,8 +187,6 @@ const visitors = {
169187
.remove(selector.end - 1, selector.end);
170188
}
171189

172-
let first = true;
173-
174190
for (const relative_selector of node.children) {
175191
if (relative_selector.metadata.is_global) {
176192
remove_global_pseudo_class(relative_selector.selectors[0]);
@@ -192,9 +208,9 @@ const visitors = {
192208
// encapsulated selector gets a +0-1-0 specificity bump. thereafter,
193209
// we use a `:where` selector, which does not affect specificity
194210
let modifier = context.state.selector;
195-
if (!first) modifier = `:where(${modifier})`;
211+
if (context.state.specificity.bumped) modifier = `:where(${modifier})`;
196212

197-
first = false;
213+
context.state.specificity.bumped = true;
198214

199215
// TODO err... can this happen?
200216
for (const selector of relative_selector.selectors) {
@@ -225,7 +241,6 @@ const visitors = {
225241

226242
break;
227243
}
228-
first = false;
229244
}
230245
}
231246

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

2-
x.svelte-xyz :is(y:where(.svelte-xyz), /* (unused) z */) {
2+
x.svelte-xyz :is(y:where(.svelte-xyz) /* (unused) z*/) {
33
color: purple;
44
}

0 commit comments

Comments
 (0)