Skip to content

fix: support standalone pseudo element selectors #33

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 1 commit into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 14 additions & 15 deletions lib/stylePlugins/scoped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ export default postcss.plugin('add-id', (options: any) => (root: Root) => {
node.selector = selectorParser((selectors: any) => {
selectors.each((selector: any) => {
let node: any = null
let hasDeep: boolean = false

selector.each((n: any) => {
// ">>>" combinator
if (n.type === 'combinator' && n.value === '>>>') {
n.value = ' '
n.spaces.before = n.spaces.after = ''
hasDeep = true
return false
}
// /deep/ alias for >>>, since >>> doesn't work in SASS
Expand All @@ -39,28 +38,28 @@ export default postcss.plugin('add-id', (options: any) => (root: Root) => {
prev.remove()
}
n.remove()
hasDeep = true
return false
}
if (n.type !== 'pseudo' && n.type !== 'combinator') {
node = n
}
})

if (node) {
node.spaces.after = ''
selector.insertAfter(
node,
selectorParser.attribute({
attribute: id
})
)
} else if (hasDeep) {
selector.prepend(
selectorParser.attribute({
attribute: id
})
)
} else {
// For deep selectors & standalone pseudo selectors,
// the attribute selectors are prepended rather than appended.
// So all leading spaces must be eliminated to avoid problems.
selector.first.spaces.before = ''
}

selector.insertAfter(
node,
selectorParser.attribute({
attribute: id
})
)
})
}).processSync(node.selector)
})
Expand Down
21 changes: 21 additions & 0 deletions test/stylePluginScoped.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,24 @@ h1 {
// >>> combinator
expect(style).toContain(`.foo p[v-scope-xxx] .bar {\n color: red;\n}`)
})

test('pseudo element', () => {
const { code } = compileStyle({
source: '::selection { display: none; }',
filename: 'test.css',
id: 'test'
})

expect(code).toContain('[test]::selection {')
})

test('spaces before pseudo element', () => {
const { code } = compileStyle({
source: '.abc, ::selection { color: red; }',
filename: 'test.css',
id: 'test'
})

expect(code).toContain('.abc[test],')
expect(code).toContain('[test]::selection {')
})