Skip to content

Commit

Permalink
fix(compiler-sfc): fix universal selector scope (vuejs#10551)
Browse files Browse the repository at this point in the history
  • Loading branch information
Doctor-wu authored and lynxlangya committed May 30, 2024
1 parent d0eb5c2 commit f3497a6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
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

0 comments on commit f3497a6

Please sign in to comment.