Skip to content

Commit 54a6afa

Browse files
authoredApr 15, 2024··
fix(compiler-sfc): fix universal selector scope (#10551)
close #10548
1 parent d58d133 commit 54a6afa

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
 

‎packages/compiler-sfc/__tests__/compileStyle.spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -429,4 +429,23 @@ describe('SFC style preprocessors', () => {
429429

430430
expect(res.errors.length).toBe(0)
431431
})
432+
433+
test('should mount scope on correct selector when have universal selector', () => {
434+
expect(compileScoped(`* { color: red; }`)).toMatchInlineSnapshot(`
435+
"[data-v-test] { color: red;
436+
}"
437+
`)
438+
expect(compileScoped('* .foo { color: red; }')).toMatchInlineSnapshot(`
439+
".foo[data-v-test] { color: red;
440+
}"
441+
`)
442+
expect(compileScoped(`*.foo { color: red; }`)).toMatchInlineSnapshot(`
443+
".foo[data-v-test] { color: red;
444+
}"
445+
`)
446+
expect(compileScoped(`.foo * { color: red; }`)).toMatchInlineSnapshot(`
447+
".foo[data-v-test] * { color: red;
448+
}"
449+
`)
450+
})
432451
})

‎packages/compiler-sfc/src/style/pluginScoped.ts

+26
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,32 @@ function rewriteSelector(
170170
}
171171
}
172172

173+
if (n.type === 'universal') {
174+
const prev = selector.at(selector.index(n) - 1)
175+
const next = selector.at(selector.index(n) + 1)
176+
// * ... {}
177+
if (!prev) {
178+
// * .foo {} -> .foo[xxxxxxx] {}
179+
if (next) {
180+
if (next.type === 'combinator' && next.value === ' ') {
181+
selector.removeChild(next)
182+
}
183+
selector.removeChild(n)
184+
return
185+
} else {
186+
// * {} -> [xxxxxxx] {}
187+
node = selectorParser.combinator({
188+
value: '',
189+
})
190+
selector.insertBefore(n, node)
191+
selector.removeChild(n)
192+
return false
193+
}
194+
}
195+
// .foo * -> .foo[xxxxxxx] *
196+
if (node) return
197+
}
198+
173199
if (
174200
(n.type !== 'pseudo' && n.type !== 'combinator') ||
175201
(n.type === 'pseudo' &&

0 commit comments

Comments
 (0)
Please sign in to comment.