index.js
3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* Stringfy the given AST `node`.
*
* @param {Object} node
* @param {Object} options
* @return {String}
* @api public
*/
module.exports = function(node, options){
return new Compiler(options).compile(node);
};
/**
* Initialize a new `Compiler`.
*/
function Compiler(options) {
options = options || {};
this.compress = options.compress;
this.indentation = options.indent;
}
/**
* Compile `node`.
*/
Compiler.prototype.compile = function(node){
return node.stylesheet.rules.map(this.visit, this)
.join(this.compress ? '' : '\n\n');
};
/**
* Visit `node`.
*/
Compiler.prototype.visit = function(node){
if (node.charset) return this.charset(node);
if (node.keyframes) return this.keyframes(node);
if (node.media) return this.media(node);
if (node.import) return this.import(node);
return this.rule(node);
};
/**
* Visit import node.
*/
Compiler.prototype.import = function(node){
return '@import ' + node.import + ';';
};
/**
* Visit media node.
*/
Compiler.prototype.media = function(node){
if (this.compress) {
return '@media '
+ node.media
+ '{'
+ node.rules.map(this.visit, this).join('')
+ '}';
}
return '@media '
+ node.media
+ ' {\n'
+ this.indent(1)
+ node.rules.map(this.visit, this).join('\n\n')
+ this.indent(-1)
+ '\n}';
};
/**
* Visit charset node.
*/
Compiler.prototype.charset = function(node){
if (this.compress) {
return '@charset ' + node.charset + ';';
}
return '@charset ' + node.charset + ';\n';
};
/**
* Visit keyframes node.
*/
Compiler.prototype.keyframes = function(node){
if (this.compress) {
return '@'
+ (node.vendor || '')
+ 'keyframes '
+ node.name
+ '{'
+ node.keyframes.map(this.keyframe, this).join('')
+ '}';
}
return '@'
+ (node.vendor || '')
+ 'keyframes '
+ node.name
+ ' {\n'
+ this.indent(1)
+ node.keyframes.map(this.keyframe, this).join('\n')
+ this.indent(-1)
+ '}';
};
/**
* Visit keyframe node.
*/
Compiler.prototype.keyframe = function(node){
if (this.compress) {
return node.values.join(',')
+ '{'
+ node.declarations.map(this.declaration, this).join(';')
+ '}';
}
return this.indent()
+ node.values.join(', ')
+ ' {\n'
+ this.indent(1)
+ node.declarations.map(this.declaration, this).join(';\n')
+ this.indent(-1)
+ '\n' + this.indent() + '}\n';
};
/**
* Visit rule node.
*/
Compiler.prototype.rule = function(node){
var indent = this.indent();
if (this.compress) {
return node.selectors.join(',')
+ '{'
+ node.declarations.map(this.declaration, this).join(';')
+ '}';
}
return node.selectors.map(function(s){ return indent + s }).join(',\n')
+ ' {\n'
+ this.indent(1)
+ node.declarations.map(this.declaration, this).join(';\n')
+ this.indent(-1)
+ '\n' + this.indent() + '}';
};
/**
* Visit declaration node.
*/
Compiler.prototype.declaration = function(node){
if (this.compress) {
return node.property + ':' + node.value;
}
return this.indent() + node.property + ': ' + node.value;
};
/**
* Increase, decrease or return current indentation.
*/
Compiler.prototype.indent = function(level) {
this.level = this.level || 1;
if (null != level) {
this.level += level;
return '';
}
return Array(this.level).join(this.indentation || ' ');
};