Skip to content

Commit

Permalink
feat(scoped-css): support leading >>> or /deep/ in selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Aug 16, 2018
1 parent 80edaf4 commit 1a3b5bb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/stylePlugins/scoped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ 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 @@ -37,6 +39,7 @@ 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') {
Expand All @@ -51,6 +54,12 @@ export default postcss.plugin('add-id', (options: any) => (root: Root) => {
attribute: id
})
)
} else if (hasDeep) {
selector.prepend(
selectorParser.attribute({
attribute: id
})
)
}
})
}).processSync(node.selector)
Expand Down
10 changes: 10 additions & 0 deletions test/stylePluginScoped.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,13 @@ test('spaces after selector', () => {

expect(code).toMatch(`.foo[test], .bar[test] { color: red;`)
})

test('leading deep selector', () => {
const { code } = compileStyle({
source: `>>> .foo { color: red; }`,
filename: 'test.css',
id: 'test'
})

expect(code).toMatch(`[test] .foo { color: red;`)
})

0 comments on commit 1a3b5bb

Please sign in to comment.