|
1 | 1 |
|
| 2 | +/** |
| 3 | + * Module dependencies. |
| 4 | + */ |
| 5 | + |
| 6 | +var Compressed = require('./lib/compress'); |
| 7 | +var Identity = require('./lib/identity'); |
| 8 | + |
2 | 9 | /**
|
3 | 10 | * Stringfy the given AST `node`.
|
4 | 11 | *
|
|
9 | 16 | */
|
10 | 17 |
|
11 | 18 | module.exports = function(node, options){
|
12 |
| - return new Compiler(options).compile(node); |
13 |
| -}; |
14 |
| - |
15 |
| -/** |
16 |
| - * Initialize a new `Compiler`. |
17 |
| - */ |
18 |
| - |
19 |
| -function Compiler(options) { |
20 |
| - options = options || {}; |
21 |
| - this.compress = options.compress; |
22 |
| - this.indentation = options.indent; |
23 |
| -} |
24 |
| - |
25 |
| -/** |
26 |
| - * Compile `node`. |
27 |
| - */ |
28 |
| - |
29 |
| -Compiler.prototype.compile = function(node){ |
30 |
| - return node.stylesheet |
31 |
| - .rules.map(this.visit, this) |
32 |
| - .join(this.compress ? '' : '\n\n'); |
33 |
| -}; |
34 |
| - |
35 |
| -/** |
36 |
| - * Visit `node`. |
37 |
| - */ |
38 |
| - |
39 |
| -Compiler.prototype.visit = function(node){ |
40 |
| - return this[node.type](node); |
41 |
| -}; |
42 |
| - |
43 |
| -/** |
44 |
| - * Visit comment node. |
45 |
| - */ |
46 |
| - |
47 |
| -Compiler.prototype.comment = function(node){ |
48 |
| - if (this.compress) return ''; |
49 |
| - return this.indent() + '/*' + node.comment + '*/'; |
50 |
| -}; |
51 |
| - |
52 |
| -/** |
53 |
| - * Visit import node. |
54 |
| - */ |
55 |
| - |
56 |
| -Compiler.prototype.import = function(node){ |
57 |
| - return '@import ' + node.import + ';'; |
58 |
| -}; |
59 |
| - |
60 |
| -/** |
61 |
| - * Visit media node. |
62 |
| - */ |
63 |
| - |
64 |
| -Compiler.prototype.media = function(node){ |
65 |
| - if (this.compress) { |
66 |
| - return '@media ' |
67 |
| - + node.media |
68 |
| - + '{' |
69 |
| - + node.rules.map(this.visit, this).join('') |
70 |
| - + '}'; |
71 |
| - } |
72 |
| - |
73 |
| - return '@media ' |
74 |
| - + node.media |
75 |
| - + ' {\n' |
76 |
| - + this.indent(1) |
77 |
| - + node.rules.map(this.visit, this).join('\n\n') |
78 |
| - + this.indent(-1) |
79 |
| - + '\n}'; |
80 |
| -}; |
81 |
| - |
82 |
| -/** |
83 |
| - * Visit document node. |
84 |
| - */ |
85 |
| - |
86 |
| -Compiler.prototype.document = function(node){ |
87 |
| - var doc = '@' + (node.vendor || '') + 'document ' + node.document; |
88 |
| - |
89 |
| - if (this.compress) { |
90 |
| - return doc |
91 |
| - + '{' |
92 |
| - + node.rules.map(this.visit, this).join('') |
93 |
| - + '}'; |
94 |
| - } |
95 |
| - |
96 |
| - return doc + ' ' |
97 |
| - + ' {\n' |
98 |
| - + this.indent(1) |
99 |
| - + node.rules.map(this.visit, this).join('\n\n') |
100 |
| - + this.indent(-1) |
101 |
| - + '\n}'; |
102 |
| -}; |
103 |
| - |
104 |
| -/** |
105 |
| - * Visit charset node. |
106 |
| - */ |
107 |
| - |
108 |
| -Compiler.prototype.charset = function(node){ |
109 |
| - if (this.compress) { |
110 |
| - return '@charset ' + node.charset + ';'; |
| 19 | + if (options.compress) { |
| 20 | + return new Compressed(options).compile(node); |
111 | 21 | }
|
112 | 22 |
|
113 |
| - return '@charset ' + node.charset + ';\n'; |
| 23 | + return new Identity(options).compile(node); |
114 | 24 | };
|
115 | 25 |
|
116 |
| -/** |
117 |
| - * Visit supports node. |
118 |
| - */ |
119 |
| - |
120 |
| -Compiler.prototype.supports = function(node){ |
121 |
| - return '@supports ' |
122 |
| - + node.supports |
123 |
| - + ' {\n' |
124 |
| - + this.indent(1) |
125 |
| - + node.rules.map(this.visit, this).join('\n\n') |
126 |
| - + this.indent(-1) |
127 |
| - + '\n}'; |
128 |
| -}; |
129 |
| - |
130 |
| -/** |
131 |
| - * Visit keyframes node. |
132 |
| - */ |
133 |
| - |
134 |
| -Compiler.prototype.keyframes = function(node){ |
135 |
| - if (this.compress) { |
136 |
| - return '@' |
137 |
| - + (node.vendor || '') |
138 |
| - + 'keyframes ' |
139 |
| - + node.name |
140 |
| - + '{' |
141 |
| - + node.keyframes.map(this.visit, this).join('') |
142 |
| - + '}'; |
143 |
| - } |
144 |
| - |
145 |
| - return '@' |
146 |
| - + (node.vendor || '') |
147 |
| - + 'keyframes ' |
148 |
| - + node.name |
149 |
| - + ' {\n' |
150 |
| - + this.indent(1) |
151 |
| - + node.keyframes.map(this.visit, this).join('\n') |
152 |
| - + this.indent(-1) |
153 |
| - + '}'; |
154 |
| -}; |
155 |
| - |
156 |
| -/** |
157 |
| - * Visit keyframe node. |
158 |
| - */ |
159 |
| - |
160 |
| -Compiler.prototype.keyframe = function(node){ |
161 |
| - var decls = node.declarations; |
162 |
| - |
163 |
| - if (this.compress) { |
164 |
| - return node.values.join(',') |
165 |
| - + '{' |
166 |
| - + decls.map(this.visit, this).join('') |
167 |
| - + '}'; |
168 |
| - } |
169 |
| - |
170 |
| - return this.indent() |
171 |
| - + node.values.join(', ') |
172 |
| - + ' {\n' |
173 |
| - + this.indent(1) |
174 |
| - + decls.map(this.visit, this).join('\n') |
175 |
| - + this.indent(-1) |
176 |
| - + '\n' + this.indent() + '}\n'; |
177 |
| -}; |
178 |
| - |
179 |
| -/** |
180 |
| - * Visit page node. |
181 |
| - */ |
182 |
| - |
183 |
| -Compiler.prototype.page = function(node){ |
184 |
| - var sel = node.selectors.length |
185 |
| - ? node.selectors.join(', ') + ' ' |
186 |
| - : ''; |
187 |
| - |
188 |
| - return '@page ' + sel |
189 |
| - + '{\n' |
190 |
| - + this.indent(1) |
191 |
| - + node.declarations.map(this.visit, this).join('\n') |
192 |
| - + this.indent(-1) |
193 |
| - + '\n}'; |
194 |
| -}; |
195 |
| - |
196 |
| -/** |
197 |
| - * Visit rule node. |
198 |
| - */ |
199 |
| - |
200 |
| -Compiler.prototype.rule = function(node){ |
201 |
| - var indent = this.indent(); |
202 |
| - var decls = node.declarations; |
203 |
| - |
204 |
| - if (this.compress) { |
205 |
| - if (!decls.length) return ''; |
206 |
| - |
207 |
| - return node.selectors.join(',') |
208 |
| - + '{' |
209 |
| - + decls.map(this.visit, this).join('') |
210 |
| - + '}'; |
211 |
| - } |
212 |
| - |
213 |
| - return node.selectors.map(function(s){ return indent + s }).join(',\n') |
214 |
| - + ' {\n' |
215 |
| - + this.indent(1) |
216 |
| - + decls.map(this.visit, this).join('\n') |
217 |
| - + this.indent(-1) |
218 |
| - + '\n' + this.indent() + '}'; |
219 |
| -}; |
220 |
| - |
221 |
| -/** |
222 |
| - * Visit declaration node. |
223 |
| - */ |
224 |
| - |
225 |
| -Compiler.prototype.declaration = function(node){ |
226 |
| - if (this.compress) { |
227 |
| - return node.property + ':' + node.value + ';'; |
228 |
| - } |
229 |
| - |
230 |
| - return this.indent() + node.property + ': ' + node.value + ';'; |
231 |
| -}; |
232 |
| - |
233 |
| -/** |
234 |
| - * Increase, decrease or return current indentation. |
235 |
| - */ |
236 |
| - |
237 |
| -Compiler.prototype.indent = function(level) { |
238 |
| - this.level = this.level || 1; |
239 |
| - |
240 |
| - if (null != level) { |
241 |
| - this.level += level; |
242 |
| - return ''; |
243 |
| - } |
244 |
| - |
245 |
| - return Array(this.level).join(this.indentation || ' '); |
246 |
| -}; |
0 commit comments