Skip to content

Commit d6cfbbf

Browse files
haoqunjiangyyx990803
authored andcommitted
fix: support standalone pseudo element selectors (#33)
closes vuejs/vue-cli#2743
1 parent f8cb88b commit d6cfbbf

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

lib/stylePlugins/scoped.ts

+14-15
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ export default postcss.plugin('add-id', (options: any) => (root: Root) => {
2323
node.selector = selectorParser((selectors: any) => {
2424
selectors.each((selector: any) => {
2525
let node: any = null
26-
let hasDeep: boolean = false
26+
2727
selector.each((n: any) => {
2828
// ">>>" combinator
2929
if (n.type === 'combinator' && n.value === '>>>') {
3030
n.value = ' '
3131
n.spaces.before = n.spaces.after = ''
32-
hasDeep = true
3332
return false
3433
}
3534
// /deep/ alias for >>>, since >>> doesn't work in SASS
@@ -39,28 +38,28 @@ export default postcss.plugin('add-id', (options: any) => (root: Root) => {
3938
prev.remove()
4039
}
4140
n.remove()
42-
hasDeep = true
4341
return false
4442
}
4543
if (n.type !== 'pseudo' && n.type !== 'combinator') {
4644
node = n
4745
}
4846
})
47+
4948
if (node) {
5049
node.spaces.after = ''
51-
selector.insertAfter(
52-
node,
53-
selectorParser.attribute({
54-
attribute: id
55-
})
56-
)
57-
} else if (hasDeep) {
58-
selector.prepend(
59-
selectorParser.attribute({
60-
attribute: id
61-
})
62-
)
50+
} else {
51+
// For deep selectors & standalone pseudo selectors,
52+
// the attribute selectors are prepended rather than appended.
53+
// So all leading spaces must be eliminated to avoid problems.
54+
selector.first.spaces.before = ''
6355
}
56+
57+
selector.insertAfter(
58+
node,
59+
selectorParser.attribute({
60+
attribute: id
61+
})
62+
)
6463
})
6564
}).processSync(node.selector)
6665
})

test/stylePluginScoped.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,24 @@ h1 {
102102
// >>> combinator
103103
expect(style).toContain(`.foo p[v-scope-xxx] .bar {\n color: red;\n}`)
104104
})
105+
106+
test('pseudo element', () => {
107+
const { code } = compileStyle({
108+
source: '::selection { display: none; }',
109+
filename: 'test.css',
110+
id: 'test'
111+
})
112+
113+
expect(code).toContain('[test]::selection {')
114+
})
115+
116+
test('spaces before pseudo element', () => {
117+
const { code } = compileStyle({
118+
source: '.abc, ::selection { color: red; }',
119+
filename: 'test.css',
120+
id: 'test'
121+
})
122+
123+
expect(code).toContain('.abc[test],')
124+
expect(code).toContain('[test]::selection {')
125+
})

0 commit comments

Comments
 (0)