Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 34f2260

Browse files
sunnylostznck
authored andcommitted
support /deep/ and >>> (#120)
* support /deep/ and >>> * fix eslint warning add test for deep and >>> * simplify code
1 parent 4f59b8c commit 34f2260

File tree

5 files changed

+94
-1
lines changed

5 files changed

+94
-1
lines changed

src/style/css.js

+37-1
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,53 @@ import camelcase from 'camelcase'
66
import genScopeID from '../gen-scope-id'
77
import debug from '../debug'
88

9+
/**
10+
* filter invalid tag, e.g. percentage, keyword(from, to)...
11+
* @param tag
12+
* @returns {boolean}
13+
*/
14+
function isInvalidTag (tag) {
15+
if (
16+
tag === 'from' ||
17+
tag === 'to' ||
18+
/^\d/.test(tag)
19+
) {
20+
return true
21+
}
22+
}
23+
924
const addScopeID = postcss.plugin('add-scope-id', options => {
1025
const selectorTransformer = selectorParser(selectors => {
1126
selectors.each(selector => {
1227
let target = null
1328
selector.each(n => {
29+
if (n.type === 'combinator' && n.value === '>>>') {
30+
n.value = ' '
31+
n.spaces.before = n.spaces.after = ''
32+
return false
33+
}
34+
35+
if (n.type === 'tag') {
36+
if (n.value === '/deep/') {
37+
const next = n.next()
38+
39+
if (next.type === 'combinator' && next.value === ' ') {
40+
next.remove()
41+
}
42+
43+
n.remove()
44+
return false
45+
} else if (isInvalidTag(n.value)) {
46+
return
47+
}
48+
}
49+
1450
if (n.type !== 'pseudo' && n.type !== 'combinator') {
1551
target = n
1652
}
1753
})
1854

19-
selector.insertAfter(target, selectorParser.attribute({
55+
target && selector.insertAfter(target, selectorParser.attribute({
2056
attribute: options.scopeID
2157
}))
2258
})
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
.test[data-v-00b08a60] a {
2+
color: red;
3+
}
4+
5+
.test[data-v-00b08a60] .text {
6+
background-color: red;
7+
}
8+
9+
@keyframes test {
10+
0% {
11+
color: red;
12+
}
13+
14+
50% {
15+
color: green;
16+
}
17+
18+
100% {
19+
color: yellow;
20+
}
21+
}
22+
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var scopedCssWithDeepTag = { template: "<div class=\"test\">Foo</div>",_scopeId: 'data-v-00b08a60',};
2+
3+
export default scopedCssWithDeepTag;
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<template>
2+
<div class="test">Foo</div>
3+
</template>
4+
5+
<script lang="babel">
6+
export default {}
7+
</script>
8+
9+
<style lang="css" scoped>
10+
.test /deep/ a {
11+
color: red;
12+
}
13+
14+
.test >>> .text {
15+
background-color: red;
16+
}
17+
18+
@keyframes test {
19+
0% {
20+
color: red;
21+
}
22+
23+
50% {
24+
color: green;
25+
}
26+
27+
100% {
28+
color: yellow;
29+
}
30+
}
31+
</style>

test/test.js

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function test(name) {
5757
'pug',
5858
'scoped-css',
5959
'scoped-css-with-no-auto-style',
60+
'scoped-css-with-deep-tag',
6061
'scss',
6162
'sass',
6263
'pug',

0 commit comments

Comments
 (0)