Skip to content

Commit f3cc41f

Browse files
committed
feat(compiler-sfc): allow using :deep, :global & :slotted for short in <style scoped>
1 parent bd5c3b9 commit f3cc41f

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

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

+14
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ describe('SFC scoped CSS', () => {
6060
})
6161

6262
test('::v-deep', () => {
63+
expect(compileScoped(`:deep(.foo) { color: red; }`)).toMatchInlineSnapshot(`
64+
"[test] .foo { color: red;
65+
}"
66+
`)
6367
expect(compileScoped(`::v-deep(.foo) { color: red; }`))
6468
.toMatchInlineSnapshot(`
6569
"[test] .foo { color: red;
@@ -78,6 +82,11 @@ describe('SFC scoped CSS', () => {
7882
})
7983

8084
test('::v-slotted', () => {
85+
expect(compileScoped(`:slotted(.foo) { color: red; }`))
86+
.toMatchInlineSnapshot(`
87+
".foo[test-s] { color: red;
88+
}"
89+
`)
8190
expect(compileScoped(`::v-slotted(.foo) { color: red; }`))
8291
.toMatchInlineSnapshot(`
8392
".foo[test-s] { color: red;
@@ -96,6 +105,11 @@ describe('SFC scoped CSS', () => {
96105
})
97106

98107
test('::v-global', () => {
108+
expect(compileScoped(`:global(.foo) { color: red; }`))
109+
.toMatchInlineSnapshot(`
110+
".foo { color: red;
111+
}"
112+
`)
99113
expect(compileScoped(`::v-global(.foo) { color: red; }`))
100114
.toMatchInlineSnapshot(`
101115
".foo { color: red;

packages/compiler-sfc/src/stylePluginScoped.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ export default postcss.plugin('vue-scoped', (options: any) => (root: Root) => {
4444
}
4545

4646
if (n.type === 'pseudo') {
47+
const { value } = n
4748
// deep: inject [id] attribute at the node before the ::v-deep
4849
// combinator.
49-
if (n.value === '::v-deep') {
50+
if (value === ':deep' || value === '::v-deep') {
5051
if (n.nodes.length) {
5152
// .foo ::v-deep(.bar) -> .foo[xxxxxxx] .bar
5253
// replace the current node with ::v-deep's inner selector
@@ -81,7 +82,7 @@ export default postcss.plugin('vue-scoped', (options: any) => (root: Root) => {
8182
// slot: use selector inside `::v-slotted` and inject [id + '-s']
8283
// instead.
8384
// ::v-slotted(.foo) -> .foo[xxxxxxx-s]
84-
if (n.value === '::v-slotted') {
85+
if (value === ':slotted' || value === '::v-slotted') {
8586
rewriteSelector(n.nodes[0] as Selector, true /* slotted */)
8687
selector.insertAfter(n, n.nodes[0])
8788
selector.removeChild(n)
@@ -93,7 +94,7 @@ export default postcss.plugin('vue-scoped', (options: any) => (root: Root) => {
9394

9495
// global: replace with inner selector and do not inject [id].
9596
// ::v-global(.foo) -> .foo
96-
if (n.value === '::v-global') {
97+
if (value === ':global' || n.value === '::v-global') {
9798
selectors.insertAfter(selector, n.nodes[0])
9899
selectors.removeChild(selector)
99100
return false

0 commit comments

Comments
 (0)