Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-sfc): fix universal selector scope #10551

Merged
merged 8 commits into from
Apr 15, 2024
19 changes: 19 additions & 0 deletions packages/compiler-sfc/__tests__/compileStyle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,4 +429,23 @@ describe('SFC style preprocessors', () => {

expect(res.errors.length).toBe(0)
})

test('should mount scope on correct selector when have universal selector', () => {
expect(compileScoped(`* { color: red; }`)).toMatchInlineSnapshot(`
"[data-v-test] { color: red;
}"
`)
expect(compileScoped('* .foo { color: red; }')).toMatchInlineSnapshot(`
".foo[data-v-test] { color: red;
}"
`)
expect(compileScoped(`*.foo { color: red; }`)).toMatchInlineSnapshot(`
".foo[data-v-test] { color: red;
}"
`)
expect(compileScoped(`.foo * { color: red; }`)).toMatchInlineSnapshot(`
".foo[data-v-test] * { color: red;
}"
`)
})
})
26 changes: 26 additions & 0 deletions packages/compiler-sfc/src/style/pluginScoped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,32 @@ function rewriteSelector(
}
}

if (n.type === 'universal') {
const prev = selector.at(selector.index(n) - 1)
const next = selector.at(selector.index(n) + 1)
// * ... {}
if (!prev) {
// * .foo {} -> .foo[xxxxxxx] {}
if (next) {
if (next.type === 'combinator' && next.value === ' ') {
selector.removeChild(next)
}
selector.removeChild(n)
return
} else {
// * {} -> [xxxxxxx] {}
node = selectorParser.combinator({
value: '',
})
selector.insertBefore(n, node)
selector.removeChild(n)
return false
}
}
// .foo * -> .foo[xxxxxxx] *
if (node) return
}

if (
(n.type !== 'pseudo' && n.type !== 'combinator') ||
(n.type === 'pseudo' &&
Expand Down
Loading