Skip to content

Commit 8bbc858

Browse files
committed
lift rules that start with body
1 parent 95da837 commit 8bbc858

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

lib/style-rewriter.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@ var hash = require('hash-sum')
44

55
var currentClass
66
var rootRE = /:root\b/g
7+
var liftRE = /^(html|head|body)\b/
78
var processRoot = postcss.plugin('process-root', function () {
89
return function (root) {
10+
var lifted = 0
11+
function lift (node) {
12+
node.moveBefore(root.nodes[lifted++])
13+
}
914
root.each(function (node) {
1015
node.each(function (node) {
11-
if (rootRE.test(node.selector)) {
16+
var sel = node.selector
17+
if (liftRE.test(sel)) {
18+
lift(node)
19+
} else if (rootRE.test(sel)) {
1220
// replace :root selector
13-
node.selector = node.selector.replace(rootRE, currentClass)
14-
// move the node to the outer scope to avoid nesting
15-
node.moveBefore(root.nodes[0])
21+
node.selector = sel.replace(rootRE, currentClass)
22+
lift(node)
1623
}
1724
})
1825
})

test/fixtures/local-css.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<style local>
2+
body {
3+
color: yellow;
4+
}
25
:root {
36
color: red;
47
}

test/test.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,15 @@ describe('vue-loader', function () {
100100
'<p class="abc def ' + cls.slice(1) + '">hi</p>'
101101
)
102102
var style = window.document.querySelector('style').textContent
103-
expect(style).to.contain('div' + cls + '.test {\n color: blue;\n}')
104-
expect(style).to.contain(cls + ' {\n color: red;\n}')
105-
expect(style).to.contain(cls + ' h1 {\n color: green;\n}')
103+
// lift selectors that start with body
104+
expect(style).to.contain('body {\n color: yellow;\n}')
105+
expect(style).not.to.contain(cls + ' body')
106+
// lift and replace :root inside compound selectors
107+
expect(style).to.contain('\ndiv' + cls + '.test {\n color: blue;\n}')
108+
// lift :root
109+
expect(style).to.contain('\n' + cls + ' {\n color: red;\n}')
110+
// nest normal selectors
111+
expect(style).to.contain('\n' + cls + ' h1 {\n color: green;\n}')
106112
done()
107113
})
108114
})

0 commit comments

Comments
 (0)