Skip to content

fix(compiler-sfc): add scoping tag to trailing universal selector #12918

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion packages/compiler-sfc/__tests__/compileStyle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,31 @@ describe('SFC style preprocessors', () => {
}"
`)
expect(compileScoped(`.foo * { color: red; }`)).toMatchInlineSnapshot(`
".foo[data-v-test] * { color: red;
".foo[data-v-test] [data-v-test] { color: red;
}"
`)
expect(compileScoped(`.foo :active { color: red; }`))
.toMatchInlineSnapshot(`
".foo[data-v-test] :active { color: red;
}"
`)
expect(compileScoped(`.foo *:active { color: red; }`))
.toMatchInlineSnapshot(`
".foo[data-v-test] [data-v-test]:active { color: red;
}"
`)
expect(compileScoped(`.foo * .bar { color: red; }`)).toMatchInlineSnapshot(`
".foo * .bar[data-v-test] { color: red;
}"
`)
expect(compileScoped(`:last-child * { color: red; }`))
.toMatchInlineSnapshot(`
"[data-v-test]:last-child [data-v-test] { color: red;
}"
`)
expect(compileScoped(`:last-child *:active { color: red; }`))
.toMatchInlineSnapshot(`
"[data-v-test]:last-child [data-v-test]:active { color: red;
}"
`)
})
Expand Down
25 changes: 22 additions & 3 deletions packages/compiler-sfc/src/style/pluginScoped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ function rewriteSelector(
slotted = false,
) {
let node: selectorParser.Node | null = null
let starNode: selectorParser.Node | null = null
let shouldInject = !deep
// find the last child node to insert attribute selector
selector.each(n => {
Expand Down Expand Up @@ -216,17 +217,21 @@ function rewriteSelector(
return false
}
}
// .foo * -> .foo[xxxxxxx] *
if (node) return
// v- store this
// .foo * -> .foo[xxxxxxx] *[xxxxxxx]
starNode = n
}

if (
(n.type !== 'pseudo' && n.type !== 'combinator') ||
(n.type !== 'pseudo' &&
n.type !== 'combinator' &&
n.type !== 'universal') ||
(n.type === 'pseudo' &&
(n.value === ':is' || n.value === ':where') &&
!node)
) {
node = n
starNode = null
}
})

Expand Down Expand Up @@ -274,6 +279,20 @@ function rewriteSelector(
quoteMark: `"`,
}),
)
// Used for trailing universal selectors (#12906)
// `.foo * {}` -> `.foo[xxxxxxx] [xxxxxxx] {}`
if (starNode) {
selector.insertBefore(
starNode,
selectorParser.attribute({
attribute: idToAdd,
value: idToAdd,
raws: {},
quoteMark: `"`,
}),
)
selector.removeChild(starNode)
}
}
}

Expand Down