Skip to content

Commit 6f0f944

Browse files
committed
fix(compile-sfc): auto add nesting combinators for ::v-deep in scoped CSS
1 parent 466b30f commit 6f0f944

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ color: red
140140
}
141141
}"
142142
`)
143+
expect(compileScoped(`.foo { :deep(.bar) { color: red; }}`))
144+
.toMatchInlineSnapshot(`
145+
".foo {
146+
&[data-v-test] .bar { color: red;
147+
}}"
148+
`)
149+
expect(compileScoped(`.foo { & :deep(.bar) { color: red; }}`))
150+
.toMatchInlineSnapshot(`
151+
".foo {
152+
&[data-v-test] .bar { color: red;
153+
}}"
154+
`)
143155
})
144156

145157
test('::v-slotted', () => {

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,29 @@ function rewriteSelector(
133133
selector.insertAfter(last, ss)
134134
last = ss
135135
})
136+
137+
// if css nesting is used, we need to insert a nesting combinator
138+
// before the ::v-deep node
139+
// .foo { ::v-deep(.bar) } -> .foo { &[xxxxxxx] .bar }
140+
const isNestedRule = rule.parent && rule.parent.type === 'rule'
141+
if (isNestedRule && n.parent) {
142+
let hasNestingCombinator = false
143+
let index = n.parent.index(n) - 1
144+
while (index >= 0) {
145+
const prev = n.parent.at(index)
146+
if (!prev) break
147+
if (prev.type === 'nesting') {
148+
hasNestingCombinator = true
149+
break
150+
}
151+
index--
152+
}
153+
if (!hasNestingCombinator) {
154+
node = selectorParser.nesting()
155+
selector.insertBefore(n, node)
156+
}
157+
}
158+
136159
// insert a space combinator before if it doesn't already have one
137160
const prev = selector.at(selector.index(n) - 1)
138161
if (!prev || !isSpaceCombinator(prev)) {

0 commit comments

Comments
 (0)