Skip to content

Commit 2646761

Browse files
committed
:root -> scoped
1 parent 3689db7 commit 2646761

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

lib/style-rewriter.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
var postcss = require('postcss')
22
var nested = require('postcss-nested')
3+
var selectorParser = require('postcss-selector-parser')
34
var hash = require('hash-sum')
45

5-
var currentClass
6-
var rootRE = /:root\b/g
76
var liftRE = /^(html|head|body)\b/
7+
var scopeRE = /:scope\b/
88
var processRoot = postcss.plugin('process-root', function () {
99
return function (root) {
1010
var lifted = 0
@@ -13,13 +13,23 @@ var processRoot = postcss.plugin('process-root', function () {
1313
}
1414
root.each(function (node) {
1515
node.each(function (node) {
16-
var sel = node.selector
17-
if (liftRE.test(sel)) {
18-
lift(node)
19-
} else if (rootRE.test(sel)) {
20-
// replace :root selector
21-
node.selector = sel.replace(rootRE, currentClass)
22-
lift(node)
16+
var kept = []
17+
selectorParser(function (selectors) {
18+
selectors.each(function (selector) {
19+
var sel = selector.toString()
20+
if (liftRE.test(sel)) {
21+
lift(node.clone({
22+
selector: sel
23+
}))
24+
} else {
25+
kept.push(sel)
26+
}
27+
})
28+
}).process(node.selector)
29+
if (!kept.length) {
30+
node.removeSelf()
31+
} else {
32+
node.selector = kept.join(',').replace(scopeRE, '&')
2333
}
2434
})
2535
})
@@ -29,7 +39,7 @@ var processRoot = postcss.plugin('process-root', function () {
2939
module.exports = function (css) {
3040
this.cacheable()
3141
var cb = this.async()
32-
var cls = currentClass = '.v-' + hash(this.resourcePath)
42+
var cls = '.v-' + hash(this.resourcePath)
3343
css = cls + '{' + css + '}'
3444
postcss([processRoot, nested])
3545
.process(css)

test/fixtures/scoped-css.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<style scoped>
2-
body {
2+
body, h2 {
33
color: yellow;
44
}
5-
:root {
5+
:scope {
66
color: red;
77
}
8-
div:root.test {
8+
div:scope.test {
99
color: blue;
1010
}
1111
h1 {

test/test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ describe('vue-loader', function () {
103103
// lift selectors that start with body
104104
expect(style).to.contain('body {\n color: yellow;\n}')
105105
expect(style).not.to.contain(cls + ' body')
106-
// lift and replace :root inside compound selectors
106+
expect(style).to.contain(cls + ' h2 {\n color: yellow;\n}')
107+
// lift and replace :scope inside compound selectors
107108
expect(style).to.contain('\ndiv' + cls + '.test {\n color: blue;\n}')
108-
// lift :root
109+
// lift :scope
109110
expect(style).to.contain('\n' + cls + ' {\n color: red;\n}')
110111
// nest normal selectors
111112
expect(style).to.contain('\n' + cls + ' h1 {\n color: green;\n}')

0 commit comments

Comments
 (0)