Skip to content

Commit c3e2c55

Browse files
fix(compiler-sfc): fix :where and :is selector in scoped mode with multiple selectors (#9735)
close #9707
1 parent bf7269a commit c3e2c55

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

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

+17
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,23 @@ describe('SFC scoped CSS', () => {
144144
`)
145145
})
146146

147+
test(':is() and :where() with multiple selectors', () => {
148+
expect(compileScoped(`:is(.foo) { color: red; }`)).toMatchInlineSnapshot(`
149+
":is(.foo[data-v-test]) { color: red;
150+
}"
151+
`)
152+
expect(compileScoped(`:where(.foo, .bar) { color: red; }`))
153+
.toMatchInlineSnapshot(`
154+
":where(.foo[data-v-test], .bar[data-v-test]) { color: red;
155+
}"
156+
`)
157+
expect(compileScoped(`:is(.foo, .bar) div { color: red; }`))
158+
.toMatchInlineSnapshot(`
159+
":is(.foo, .bar) div[data-v-test] { color: red;
160+
}"
161+
`)
162+
})
163+
147164
test('media query', () => {
148165
expect(compileScoped(`@media print { .foo { color: red }}`))
149166
.toMatchInlineSnapshot(`

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,23 @@ function rewriteSelector(
170170
}
171171
}
172172

173-
if (n.type !== 'pseudo' && n.type !== 'combinator') {
173+
if (
174+
(n.type !== 'pseudo' && n.type !== 'combinator') ||
175+
(n.type === 'pseudo' && (n.value === ':is' || n.value === ':where'))
176+
) {
174177
node = n
175178
}
179+
})
176180

177-
if (n.type === 'pseudo' && (n.value === ':is' || n.value === ':where')) {
178-
rewriteSelector(id, n.nodes[0], selectorRoot, slotted)
181+
if (node) {
182+
const { type, value } = node as selectorParser.Node
183+
if (type === 'pseudo' && (value === ':is' || value === ':where')) {
184+
;(node as selectorParser.Pseudo).nodes.forEach(value =>
185+
rewriteSelector(id, value, selectorRoot, slotted)
186+
)
179187
shouldInject = false
180188
}
181-
})
189+
}
182190

183191
if (node) {
184192
;(node as selectorParser.Node).spaces.after = ''

0 commit comments

Comments
 (0)