Skip to content

Commit 5f81b26

Browse files
committed
fix(compiler-sfc): fix universal selector scope
close vuejs#10548
1 parent ab59bed commit 5f81b26

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,4 +390,15 @@ describe('SFC style preprocessors', () => {
390390

391391
expect(res.errors.length).toBe(0)
392392
})
393+
394+
test('should mount scope on correct selector when have universal selector', () => {
395+
expect(compileScoped(`* { color: red; }`)).toMatchInlineSnapshot(`
396+
"[data-v-test] { color: red;
397+
}"
398+
`)
399+
expect(compileScoped(`.foo * { color: red; }`)).toMatchInlineSnapshot(`
400+
".foo[data-v-test] * { color: red;
401+
}"
402+
`)
403+
})
393404
})

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,23 @@ 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+
if (!prev && !next) {
177+
node = selectorParser.combinator({
178+
value: '',
179+
})
180+
selector.insertBefore(n, node)
181+
selector.removeChild(n)
182+
}
183+
}
184+
173185
if (
174186
(n.type !== 'pseudo' && n.type !== 'combinator') ||
175187
(n.type === 'pseudo' && (n.value === ':is' || n.value === ':where'))
176188
) {
189+
if (n.type === 'universal' && node) return
177190
node = n
178191
}
179192
})

0 commit comments

Comments
 (0)