Skip to content

Commit 7d46195

Browse files
committed
add keyframe support
1 parent 8724bdf commit 7d46195

File tree

1 file changed

+79
-8
lines changed

1 file changed

+79
-8
lines changed

index.js

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,24 @@
1111
module.exports = function(node, options){
1212
options = options || {};
1313
return options.compress
14-
? node.stylesheet.rules.map(rule(options)).join('')
15-
: node.stylesheet.rules.map(rule(options)).join('\n\n');
14+
? node.stylesheet.rules.map(visit(options)).join('')
15+
: node.stylesheet.rules.map(visit(options)).join('\n\n');
1616
};
1717

18+
/**
19+
* Visit rule nodes.
20+
*/
21+
22+
function visit(options) {
23+
var _rule = rule(options);
24+
var _keyframes = keyframes(options);
25+
return function(node){
26+
if (node.keyframes) return _keyframes(node);
27+
if (node.import) return atimport(node);
28+
return _rule(options);
29+
}
30+
}
31+
1832
/**
1933
* Compile import.
2034
*/
@@ -23,25 +37,72 @@ function atimport(rule) {
2337
return '@import ' + rule.import + ';';
2438
}
2539

40+
/**
41+
* Compile keyframes.
42+
*/
43+
44+
function keyframes(options) {
45+
if (options.compress) {
46+
return function(keyframes){
47+
return '@'
48+
+ (keyframes.vendor || '')
49+
+ 'keyframes '
50+
+ keyframes.name
51+
+ '{'
52+
+ keyframes.keyframes.map(keyframe(options)).join('')
53+
+ '}';
54+
}
55+
}
56+
57+
return function(keyframes){
58+
return '@'
59+
+ (keyframes.vendor || '')
60+
+ 'keyframes '
61+
+ keyframes.name
62+
+ ' {\n'
63+
+ keyframes.keyframes.map(keyframe(options)).join('\n')
64+
+ '}';
65+
}
66+
}
67+
68+
/**
69+
* Compile keyframe.
70+
*/
71+
72+
function keyframe(options) {
73+
if (options.compress) {
74+
return function(keyframe){
75+
return keyframe.values.join(',')
76+
+ '{'
77+
+ keyframe.declarations.map(declaration(options)).join(';')
78+
+ '}'
79+
}
80+
}
81+
82+
return function(keyframe){
83+
return ' '
84+
+ keyframe.values.join(', ')
85+
+ ' {\n'
86+
+ keyframe.declarations.map(indent(declaration(options))).join(';')
87+
+ '\n }\n'
88+
}
89+
}
90+
2691
/**
2792
* Compile rule.
2893
*/
2994

3095
function rule(options) {
3196
if (options.compress) {
32-
return function(rule) {
33-
if (rule.import) return atimport(rule);
34-
97+
return function(rule){
3598
return rule.selector
3699
+ '{'
37100
+ rule.declarations.map(declaration(options)).join(';')
38101
+ '}';
39102
}
40103
}
41104

42-
return function(rule) {
43-
if (rule.import) return atimport(rule);
44-
105+
return function(rule){
45106
return rule.selector
46107
+ ' {\n'
47108
+ rule.declarations.map(declaration(options)).join('\n')
@@ -63,4 +124,14 @@ function declaration(options) {
63124
return function(decl){
64125
return ' ' + decl.property + ': ' + decl.value + ';';
65126
}
127+
}
128+
129+
/**
130+
* Indent.
131+
*/
132+
133+
function indent(fn) {
134+
return function(val){
135+
return ' ' + fn(val);
136+
}
66137
}

0 commit comments

Comments
 (0)