Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 37e0741

Browse filesBrowse files
committed
make keyframes scoped (properly)
1 parent 08491c9 commit 37e0741
Copy full SHA for 37e0741

File tree

3 files changed

+58
-5
lines changed
Filter options

3 files changed

+58
-5
lines changed

‎lib/style-compiler/plugins/scope-id.js

Copy file name to clipboardExpand all lines: lib/style-compiler/plugins/scope-id.js
+40-2Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@ var selectorParser = require('postcss-selector-parser')
33

44
module.exports = postcss.plugin('add-id', function (opts) {
55
return function (root) {
6+
var keyframes = Object.create(null)
7+
68
root.each(function rewriteSelector (node) {
79
if (!node.selector) {
810
// handle media queries
9-
if (node.type === 'atrule' && node.name === 'media') {
10-
node.each(rewriteSelector)
11+
if (node.type === 'atrule') {
12+
if (node.name === 'media') {
13+
node.each(rewriteSelector)
14+
} else if (node.name === 'keyframes') {
15+
// register keyframes
16+
keyframes[node.params] = node.params = node.params + '-' + opts.id
17+
}
1118
}
1219
return
1320
}
@@ -23,5 +30,36 @@ module.exports = postcss.plugin('add-id', function (opts) {
2330
})
2431
}).process(node.selector).result
2532
})
33+
34+
// If keyframes are found in this <style>, find and rewrite animation names
35+
// in declarations
36+
//
37+
// TODO: the keyframes tracking needs to be across multiple <style> tags
38+
// that belongs to the same component...
39+
if (Object.keys(keyframes).length) {
40+
root.walkDecls(decl => {
41+
// individual animation-name declaration
42+
if (/-?animation-name$/.test(decl.prop)) {
43+
decl.value = decl.value.split(',')
44+
.map(v => keyframes[v.trim()])
45+
.filter(v => v)
46+
.join(',')
47+
}
48+
// shorthand
49+
if (/-?animation$/.test(decl.prop)) {
50+
decl.value = decl.value.split(',')
51+
.map(v => {
52+
var vals = v.split(/\s+/)
53+
var name = vals[0]
54+
if (keyframes[name]) {
55+
return [keyframes[name]].concat(vals.slice(1)).join(' ')
56+
} else {
57+
return v
58+
}
59+
})
60+
.join(',')
61+
}
62+
})
63+
}
2664
}
2765
})

‎test/fixtures/scoped-css.vue

Copy file name to clipboardExpand all lines: test/fixtures/scoped-css.vue
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
88
h1 {
99
color: green;
1010
}
11+
.anim {
12+
animation: color 5s infinite, other 5s;
13+
}
14+
.anim-2 {
15+
animation-name: color;
16+
animation-duration: 5s;
17+
}
18+
@keyframes color {
19+
from { color: red; }
20+
to { color: green; }
21+
}
1122
</style>
1223

1324
<template>

‎test/test.js

Copy file name to clipboardExpand all lines: test/test.js
+7-3Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,13 @@ describe('vue-loader', function () {
193193

194194
var style = window.document.querySelector('style').textContent
195195
style = normalizeNewline(style)
196-
expect(style).to.contain('.test[' + id + '] {\n color: yellow;\n}')
197-
expect(style).to.contain('.test[' + id + ']:after {\n content: \'bye!\';\n}')
198-
expect(style).to.contain('h1[' + id + '] {\n color: green;\n}')
196+
expect(style).to.contain(`.test[${id}] {\n color: yellow;\n}`)
197+
expect(style).to.contain(`.test[${id}]:after {\n content: \'bye!\';\n}`)
198+
expect(style).to.contain(`h1[${id}] {\n color: green;\n}`)
199+
// scoped keyframes
200+
expect(style).to.contain(`.anim[${id}] {\n animation: color-${id} 5s infinite, other 5s;`)
201+
expect(style).to.contain(`.anim-2[${id}] {\n animation-name: color-${id}`)
202+
expect(style).to.contain(`@keyframes color-${id} {`)
199203
done()
200204
})
201205
})

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.