1 | // |
---|
2 | // LESS - Leaner CSS v1.3.2 |
---|
3 | // http://lesscss.org |
---|
4 | // |
---|
5 | // Copyright (c) 2009-2011, Alexis Sellier |
---|
6 | // Licensed under the Apache 2.0 License. |
---|
7 | // |
---|
8 | (function (window, undefined) { |
---|
9 | // |
---|
10 | // Stub out `require` in the browser |
---|
11 | // |
---|
12 | function require(arg) { |
---|
13 | return window.less[arg.split('/')[1]]; |
---|
14 | }; |
---|
15 | |
---|
16 | |
---|
17 | // ecma-5.js |
---|
18 | // |
---|
19 | // -- kriskowal Kris Kowal Copyright (C) 2009-2010 MIT License |
---|
20 | // -- tlrobinson Tom Robinson |
---|
21 | // dantman Daniel Friesen |
---|
22 | |
---|
23 | // |
---|
24 | // Array |
---|
25 | // |
---|
26 | if (!Array.isArray) { |
---|
27 | Array.isArray = function(obj) { |
---|
28 | return Object.prototype.toString.call(obj) === "[object Array]" || |
---|
29 | (obj instanceof Array); |
---|
30 | }; |
---|
31 | } |
---|
32 | if (!Array.prototype.forEach) { |
---|
33 | Array.prototype.forEach = function(block, thisObject) { |
---|
34 | var len = this.length >>> 0; |
---|
35 | for (var i = 0; i < len; i++) { |
---|
36 | if (i in this) { |
---|
37 | block.call(thisObject, this[i], i, this); |
---|
38 | } |
---|
39 | } |
---|
40 | }; |
---|
41 | } |
---|
42 | if (!Array.prototype.map) { |
---|
43 | Array.prototype.map = function(fun /*, thisp*/) { |
---|
44 | var len = this.length >>> 0; |
---|
45 | var res = new Array(len); |
---|
46 | var thisp = arguments[1]; |
---|
47 | |
---|
48 | for (var i = 0; i < len; i++) { |
---|
49 | if (i in this) { |
---|
50 | res[i] = fun.call(thisp, this[i], i, this); |
---|
51 | } |
---|
52 | } |
---|
53 | return res; |
---|
54 | }; |
---|
55 | } |
---|
56 | if (!Array.prototype.filter) { |
---|
57 | Array.prototype.filter = function (block /*, thisp */) { |
---|
58 | var values = []; |
---|
59 | var thisp = arguments[1]; |
---|
60 | for (var i = 0; i < this.length; i++) { |
---|
61 | if (block.call(thisp, this[i])) { |
---|
62 | values.push(this[i]); |
---|
63 | } |
---|
64 | } |
---|
65 | return values; |
---|
66 | }; |
---|
67 | } |
---|
68 | if (!Array.prototype.reduce) { |
---|
69 | Array.prototype.reduce = function(fun /*, initial*/) { |
---|
70 | var len = this.length >>> 0; |
---|
71 | var i = 0; |
---|
72 | |
---|
73 | // no value to return if no initial value and an empty array |
---|
74 | if (len === 0 && arguments.length === 1) throw new TypeError(); |
---|
75 | |
---|
76 | if (arguments.length >= 2) { |
---|
77 | var rv = arguments[1]; |
---|
78 | } else { |
---|
79 | do { |
---|
80 | if (i in this) { |
---|
81 | rv = this[i++]; |
---|
82 | break; |
---|
83 | } |
---|
84 | // if array contains no values, no initial value to return |
---|
85 | if (++i >= len) throw new TypeError(); |
---|
86 | } while (true); |
---|
87 | } |
---|
88 | for (; i < len; i++) { |
---|
89 | if (i in this) { |
---|
90 | rv = fun.call(null, rv, this[i], i, this); |
---|
91 | } |
---|
92 | } |
---|
93 | return rv; |
---|
94 | }; |
---|
95 | } |
---|
96 | if (!Array.prototype.indexOf) { |
---|
97 | Array.prototype.indexOf = function (value /*, fromIndex */ ) { |
---|
98 | var length = this.length; |
---|
99 | var i = arguments[1] || 0; |
---|
100 | |
---|
101 | if (!length) return -1; |
---|
102 | if (i >= length) return -1; |
---|
103 | if (i < 0) i += length; |
---|
104 | |
---|
105 | for (; i < length; i++) { |
---|
106 | if (!Object.prototype.hasOwnProperty.call(this, i)) { continue } |
---|
107 | if (value === this[i]) return i; |
---|
108 | } |
---|
109 | return -1; |
---|
110 | }; |
---|
111 | } |
---|
112 | |
---|
113 | // |
---|
114 | // Object |
---|
115 | // |
---|
116 | if (!Object.keys) { |
---|
117 | Object.keys = function (object) { |
---|
118 | var keys = []; |
---|
119 | for (var name in object) { |
---|
120 | if (Object.prototype.hasOwnProperty.call(object, name)) { |
---|
121 | keys.push(name); |
---|
122 | } |
---|
123 | } |
---|
124 | return keys; |
---|
125 | }; |
---|
126 | } |
---|
127 | |
---|
128 | // |
---|
129 | // String |
---|
130 | // |
---|
131 | if (!String.prototype.trim) { |
---|
132 | String.prototype.trim = function () { |
---|
133 | return String(this).replace(/^\s\s*/, '').replace(/\s\s*$/, ''); |
---|
134 | }; |
---|
135 | } |
---|
136 | var less, tree, charset; |
---|
137 | |
---|
138 | if (typeof environment === "object" && ({}).toString.call(environment) === "[object Environment]") { |
---|
139 | // Rhino |
---|
140 | // Details on how to detect Rhino: https://github.com/ringo/ringojs/issues/88 |
---|
141 | if (typeof(window) === 'undefined') { less = {} } |
---|
142 | else { less = window.less = {} } |
---|
143 | tree = less.tree = {}; |
---|
144 | less.mode = 'rhino'; |
---|
145 | } else if (typeof(window) === 'undefined') { |
---|
146 | // Node.js |
---|
147 | less = exports, |
---|
148 | tree = require('./tree'); |
---|
149 | less.mode = 'node'; |
---|
150 | } else { |
---|
151 | // Browser |
---|
152 | if (typeof(window.less) === 'undefined') { window.less = {} } |
---|
153 | less = window.less, |
---|
154 | tree = window.less.tree = {}; |
---|
155 | less.mode = 'browser'; |
---|
156 | } |
---|
157 | // |
---|
158 | // less.js - parser |
---|
159 | // |
---|
160 | // A relatively straight-forward predictive parser. |
---|
161 | // There is no tokenization/lexing stage, the input is parsed |
---|
162 | // in one sweep. |
---|
163 | // |
---|
164 | // To make the parser fast enough to run in the browser, several |
---|
165 | // optimization had to be made: |
---|
166 | // |
---|
167 | // - Matching and slicing on a huge input is often cause of slowdowns. |
---|
168 | // The solution is to chunkify the input into smaller strings. |
---|
169 | // The chunks are stored in the `chunks` var, |
---|
170 | // `j` holds the current chunk index, and `current` holds |
---|
171 | // the index of the current chunk in relation to `input`. |
---|
172 | // This gives us an almost 4x speed-up. |
---|
173 | // |
---|
174 | // - In many cases, we don't need to match individual tokens; |
---|
175 | // for example, if a value doesn't hold any variables, operations |
---|
176 | // or dynamic references, the parser can effectively 'skip' it, |
---|
177 | // treating it as a literal. |
---|
178 | // An example would be '1px solid #000' - which evaluates to itself, |
---|
179 | // we don't need to know what the individual components are. |
---|
180 | // The drawback, of course is that you don't get the benefits of |
---|
181 | // syntax-checking on the CSS. This gives us a 50% speed-up in the parser, |
---|
182 | // and a smaller speed-up in the code-gen. |
---|
183 | // |
---|
184 | // |
---|
185 | // Token matching is done with the `$` function, which either takes |
---|
186 | // a terminal string or regexp, or a non-terminal function to call. |
---|
187 | // It also takes care of moving all the indices forwards. |
---|
188 | // |
---|
189 | // |
---|
190 | less.Parser = function Parser(env) { |
---|
191 | var input, // LeSS input string |
---|
192 | i, // current index in `input` |
---|
193 | j, // current chunk |
---|
194 | temp, // temporarily holds a chunk's state, for backtracking |
---|
195 | memo, // temporarily holds `i`, when backtracking |
---|
196 | furthest, // furthest index the parser has gone to |
---|
197 | chunks, // chunkified input |
---|
198 | current, // index of current chunk, in `input` |
---|
199 | parser; |
---|
200 | |
---|
201 | var that = this; |
---|
202 | |
---|
203 | // Top parser on an import tree must be sure there is one "env" |
---|
204 | // which will then be passed arround by reference. |
---|
205 | var env = env || { }; |
---|
206 | // env.contents and files must be passed arround with top env |
---|
207 | if (!env.contents) { env.contents = {}; } |
---|
208 | env.rootpath = env.rootpath || ''; // env.rootpath must be initialized to '' if not provided |
---|
209 | if (!env.files) { env.files = {}; } |
---|
210 | |
---|
211 | // This function is called after all files |
---|
212 | // have been imported through `@import`. |
---|
213 | var finish = function () {}; |
---|
214 | |
---|
215 | var imports = this.imports = { |
---|
216 | paths: env.paths || [], // Search paths, when importing |
---|
217 | queue: [], // Files which haven't been imported yet |
---|
218 | files: env.files, // Holds the imported parse trees |
---|
219 | contents: env.contents, // Holds the imported file contents |
---|
220 | mime: env.mime, // MIME type of .less files |
---|
221 | error: null, // Error in parsing/evaluating an import |
---|
222 | push: function (path, callback) { |
---|
223 | var that = this; |
---|
224 | this.queue.push(path); |
---|
225 | |
---|
226 | // |
---|
227 | // Import a file asynchronously |
---|
228 | // |
---|
229 | less.Parser.importer(path, this.paths, function (e, root, fullPath) { |
---|
230 | that.queue.splice(that.queue.indexOf(path), 1); // Remove the path from the queue |
---|
231 | |
---|
232 | var imported = fullPath in that.files; |
---|
233 | |
---|
234 | that.files[fullPath] = root; // Store the root |
---|
235 | |
---|
236 | if (e && !that.error) { that.error = e } |
---|
237 | |
---|
238 | callback(e, root, imported); |
---|
239 | |
---|
240 | if (that.queue.length === 0) { finish(that.error) } // Call `finish` if we're done importing |
---|
241 | }, env); |
---|
242 | } |
---|
243 | }; |
---|
244 | |
---|
245 | function save() { temp = chunks[j], memo = i, current = i } |
---|
246 | function restore() { chunks[j] = temp, i = memo, current = i } |
---|
247 | |
---|
248 | function sync() { |
---|
249 | if (i > current) { |
---|
250 | chunks[j] = chunks[j].slice(i - current); |
---|
251 | current = i; |
---|
252 | } |
---|
253 | } |
---|
254 | function isWhitespace(c) { |
---|
255 | // Could change to \s? |
---|
256 | var code = c.charCodeAt(0); |
---|
257 | return code === 32 || code === 10 || code === 9; |
---|
258 | } |
---|
259 | // |
---|
260 | // Parse from a token, regexp or string, and move forward if match |
---|
261 | // |
---|
262 | function $(tok) { |
---|
263 | var match, args, length, index, k; |
---|
264 | |
---|
265 | // |
---|
266 | // Non-terminal |
---|
267 | // |
---|
268 | if (tok instanceof Function) { |
---|
269 | return tok.call(parser.parsers); |
---|
270 | // |
---|
271 | // Terminal |
---|
272 | // |
---|
273 | // Either match a single character in the input, |
---|
274 | // or match a regexp in the current chunk (chunk[j]). |
---|
275 | // |
---|
276 | } else if (typeof(tok) === 'string') { |
---|
277 | match = input.charAt(i) === tok ? tok : null; |
---|
278 | length = 1; |
---|
279 | sync (); |
---|
280 | } else { |
---|
281 | sync (); |
---|
282 | |
---|
283 | if (match = tok.exec(chunks[j])) { |
---|
284 | length = match[0].length; |
---|
285 | } else { |
---|
286 | return null; |
---|
287 | } |
---|
288 | } |
---|
289 | |
---|
290 | // The match is confirmed, add the match length to `i`, |
---|
291 | // and consume any extra white-space characters (' ' || '\n') |
---|
292 | // which come after that. The reason for this is that LeSS's |
---|
293 | // grammar is mostly white-space insensitive. |
---|
294 | // |
---|
295 | if (match) { |
---|
296 | skipWhitespace(length); |
---|
297 | |
---|
298 | if(typeof(match) === 'string') { |
---|
299 | return match; |
---|
300 | } else { |
---|
301 | return match.length === 1 ? match[0] : match; |
---|
302 | } |
---|
303 | } |
---|
304 | } |
---|
305 | |
---|
306 | function skipWhitespace(length) { |
---|
307 | var oldi = i, oldj = j, |
---|
308 | endIndex = i + chunks[j].length, |
---|
309 | mem = i += length; |
---|
310 | |
---|
311 | while (i < endIndex) { |
---|
312 | if (! isWhitespace(input.charAt(i))) { break } |
---|
313 | i++; |
---|
314 | } |
---|
315 | chunks[j] = chunks[j].slice(length + (i - mem)); |
---|
316 | current = i; |
---|
317 | |
---|
318 | if (chunks[j].length === 0 && j < chunks.length - 1) { j++ } |
---|
319 | |
---|
320 | return oldi !== i || oldj !== j; |
---|
321 | } |
---|
322 | |
---|
323 | function expect(arg, msg) { |
---|
324 | var result = $(arg); |
---|
325 | if (! result) { |
---|
326 | error(msg || (typeof(arg) === 'string' ? "expected '" + arg + "' got '" + input.charAt(i) + "'" |
---|
327 | : "unexpected token")); |
---|
328 | } else { |
---|
329 | return result; |
---|
330 | } |
---|
331 | } |
---|
332 | |
---|
333 | function error(msg, type) { |
---|
334 | var e = new Error(msg); |
---|
335 | e.index = i; |
---|
336 | e.type = type || 'Syntax'; |
---|
337 | throw e; |
---|
338 | } |
---|
339 | |
---|
340 | // Same as $(), but don't change the state of the parser, |
---|
341 | // just return the match. |
---|
342 | function peek(tok) { |
---|
343 | if (typeof(tok) === 'string') { |
---|
344 | return input.charAt(i) === tok; |
---|
345 | } else { |
---|
346 | if (tok.test(chunks[j])) { |
---|
347 | return true; |
---|
348 | } else { |
---|
349 | return false; |
---|
350 | } |
---|
351 | } |
---|
352 | } |
---|
353 | |
---|
354 | function getInput(e, env) { |
---|
355 | if (e.filename && env.filename && (e.filename !== env.filename)) { |
---|
356 | return parser.imports.contents[e.filename]; |
---|
357 | } else { |
---|
358 | return input; |
---|
359 | } |
---|
360 | } |
---|
361 | |
---|
362 | function getLocation(index, input) { |
---|
363 | for (var n = index, column = -1; |
---|
364 | n >= 0 && input.charAt(n) !== '\n'; |
---|
365 | n--) { column++ } |
---|
366 | |
---|
367 | return { line: typeof(index) === 'number' ? (input.slice(0, index).match(/\n/g) || "").length : null, |
---|
368 | column: column }; |
---|
369 | } |
---|
370 | |
---|
371 | function getFileName(e) { |
---|
372 | if(less.mode === 'browser' || less.mode === 'rhino') |
---|
373 | return e.filename; |
---|
374 | else |
---|
375 | return require('path').resolve(e.filename); |
---|
376 | } |
---|
377 | |
---|
378 | function getDebugInfo(index, inputStream, e) { |
---|
379 | return { |
---|
380 | lineNumber: getLocation(index, inputStream).line + 1, |
---|
381 | fileName: getFileName(e) |
---|
382 | }; |
---|
383 | } |
---|
384 | |
---|
385 | function LessError(e, env) { |
---|
386 | var input = getInput(e, env), |
---|
387 | loc = getLocation(e.index, input), |
---|
388 | line = loc.line, |
---|
389 | col = loc.column, |
---|
390 | lines = input.split('\n'); |
---|
391 | |
---|
392 | this.type = e.type || 'Syntax'; |
---|
393 | this.message = e.message; |
---|
394 | this.filename = e.filename || env.filename; |
---|
395 | this.index = e.index; |
---|
396 | this.line = typeof(line) === 'number' ? line + 1 : null; |
---|
397 | this.callLine = e.call && (getLocation(e.call, input).line + 1); |
---|
398 | this.callExtract = lines[getLocation(e.call, input).line]; |
---|
399 | this.stack = e.stack; |
---|
400 | this.column = col; |
---|
401 | this.extract = [ |
---|
402 | lines[line - 1], |
---|
403 | lines[line], |
---|
404 | lines[line + 1] |
---|
405 | ]; |
---|
406 | } |
---|
407 | |
---|
408 | this.env = env = env || {}; |
---|
409 | |
---|
410 | // The optimization level dictates the thoroughness of the parser, |
---|
411 | // the lower the number, the less nodes it will create in the tree. |
---|
412 | // This could matter for debugging, or if you want to access |
---|
413 | // the individual nodes in the tree. |
---|
414 | this.optimization = ('optimization' in this.env) ? this.env.optimization : 1; |
---|
415 | |
---|
416 | this.env.filename = this.env.filename || null; |
---|
417 | |
---|
418 | // |
---|
419 | // The Parser |
---|
420 | // |
---|
421 | return parser = { |
---|
422 | |
---|
423 | imports: imports, |
---|
424 | // |
---|
425 | // Parse an input string into an abstract syntax tree, |
---|
426 | // call `callback` when done. |
---|
427 | // |
---|
428 | parse: function (str, callback) { |
---|
429 | var root, start, end, zone, line, lines, buff = [], c, error = null; |
---|
430 | |
---|
431 | i = j = current = furthest = 0; |
---|
432 | input = str.replace(/\r\n/g, '\n'); |
---|
433 | |
---|
434 | // Remove potential UTF Byte Order Mark |
---|
435 | input = input.replace(/^\uFEFF/, ''); |
---|
436 | |
---|
437 | // Split the input into chunks. |
---|
438 | chunks = (function (chunks) { |
---|
439 | var j = 0, |
---|
440 | skip = /(?:@\{[\w-]+\}|[^"'`\{\}\/\(\)\\])+/g, |
---|
441 | comment = /\/\*(?:[^*]|\*+[^\/*])*\*+\/|\/\/.*/g, |
---|
442 | string = /"((?:[^"\\\r\n]|\\.)*)"|'((?:[^'\\\r\n]|\\.)*)'|`((?:[^`]|\\.)*)`/g, |
---|
443 | level = 0, |
---|
444 | match, |
---|
445 | chunk = chunks[0], |
---|
446 | inParam; |
---|
447 | |
---|
448 | for (var i = 0, c, cc; i < input.length;) { |
---|
449 | skip.lastIndex = i; |
---|
450 | if (match = skip.exec(input)) { |
---|
451 | if (match.index === i) { |
---|
452 | i += match[0].length; |
---|
453 | chunk.push(match[0]); |
---|
454 | } |
---|
455 | } |
---|
456 | c = input.charAt(i); |
---|
457 | comment.lastIndex = string.lastIndex = i; |
---|
458 | |
---|
459 | if (match = string.exec(input)) { |
---|
460 | if (match.index === i) { |
---|
461 | i += match[0].length; |
---|
462 | chunk.push(match[0]); |
---|
463 | continue; |
---|
464 | } |
---|
465 | } |
---|
466 | |
---|
467 | if (!inParam && c === '/') { |
---|
468 | cc = input.charAt(i + 1); |
---|
469 | if (cc === '/' || cc === '*') { |
---|
470 | if (match = comment.exec(input)) { |
---|
471 | if (match.index === i) { |
---|
472 | i += match[0].length; |
---|
473 | chunk.push(match[0]); |
---|
474 | continue; |
---|
475 | } |
---|
476 | } |
---|
477 | } |
---|
478 | } |
---|
479 | |
---|
480 | switch (c) { |
---|
481 | case '{': if (! inParam) { level ++; chunk.push(c); break } |
---|
482 | case '}': if (! inParam) { level --; chunk.push(c); chunks[++j] = chunk = []; break } |
---|
483 | case '(': if (! inParam) { inParam = true; chunk.push(c); break } |
---|
484 | case ')': if ( inParam) { inParam = false; chunk.push(c); break } |
---|
485 | default: chunk.push(c); |
---|
486 | } |
---|
487 | |
---|
488 | i++; |
---|
489 | } |
---|
490 | if (level != 0) { |
---|
491 | error = new(LessError)({ |
---|
492 | index: i-1, |
---|
493 | type: 'Parse', |
---|
494 | message: (level > 0) ? "missing closing `}`" : "missing opening `{`", |
---|
495 | filename: env.filename |
---|
496 | }, env); |
---|
497 | } |
---|
498 | |
---|
499 | return chunks.map(function (c) { return c.join('') });; |
---|
500 | })([[]]); |
---|
501 | |
---|
502 | if (error) { |
---|
503 | return callback(error, env); |
---|
504 | } |
---|
505 | |
---|
506 | // Start with the primary rule. |
---|
507 | // The whole syntax tree is held under a Ruleset node, |
---|
508 | // with the `root` property set to true, so no `{}` are |
---|
509 | // output. The callback is called when the input is parsed. |
---|
510 | try { |
---|
511 | root = new(tree.Ruleset)([], $(this.parsers.primary)); |
---|
512 | root.root = true; |
---|
513 | } catch (e) { |
---|
514 | return callback(new(LessError)(e, env)); |
---|
515 | } |
---|
516 | |
---|
517 | root.toCSS = (function (evaluate) { |
---|
518 | var line, lines, column; |
---|
519 | |
---|
520 | return function (options, variables) { |
---|
521 | var frames = [], importError; |
---|
522 | |
---|
523 | options = options || {}; |
---|
524 | // |
---|
525 | // Allows setting variables with a hash, so: |
---|
526 | // |
---|
527 | // `{ color: new(tree.Color)('#f01') }` will become: |
---|
528 | // |
---|
529 | // new(tree.Rule)('@color', |
---|
530 | // new(tree.Value)([ |
---|
531 | // new(tree.Expression)([ |
---|
532 | // new(tree.Color)('#f01') |
---|
533 | // ]) |
---|
534 | // ]) |
---|
535 | // ) |
---|
536 | // |
---|
537 | if (typeof(variables) === 'object' && !Array.isArray(variables)) { |
---|
538 | variables = Object.keys(variables).map(function (k) { |
---|
539 | var value = variables[k]; |
---|
540 | |
---|
541 | if (! (value instanceof tree.Value)) { |
---|
542 | if (! (value instanceof tree.Expression)) { |
---|
543 | value = new(tree.Expression)([value]); |
---|
544 | } |
---|
545 | value = new(tree.Value)([value]); |
---|
546 | } |
---|
547 | return new(tree.Rule)('@' + k, value, false, 0); |
---|
548 | }); |
---|
549 | frames = [new(tree.Ruleset)(null, variables)]; |
---|
550 | } |
---|
551 | |
---|
552 | try { |
---|
553 | var css = evaluate.call(this, { frames: frames }) |
---|
554 | .toCSS([], { compress: options.compress || false, dumpLineNumbers: env.dumpLineNumbers }); |
---|
555 | } catch (e) { |
---|
556 | throw new(LessError)(e, env); |
---|
557 | } |
---|
558 | |
---|
559 | if ((importError = parser.imports.error)) { // Check if there was an error during importing |
---|
560 | if (importError instanceof LessError) throw importError; |
---|
561 | else throw new(LessError)(importError, env); |
---|
562 | } |
---|
563 | |
---|
564 | if (options.yuicompress && less.mode === 'node') { |
---|
565 | return require('ycssmin').cssmin(css); |
---|
566 | } else if (options.compress) { |
---|
567 | return css.replace(/(\s)+/g, "$1"); |
---|
568 | } else { |
---|
569 | return css; |
---|
570 | } |
---|
571 | }; |
---|
572 | })(root.eval); |
---|
573 | |
---|
574 | // If `i` is smaller than the `input.length - 1`, |
---|
575 | // it means the parser wasn't able to parse the whole |
---|
576 | // string, so we've got a parsing error. |
---|
577 | // |
---|
578 | // We try to extract a \n delimited string, |
---|
579 | // showing the line where the parse error occured. |
---|
580 | // We split it up into two parts (the part which parsed, |
---|
581 | // and the part which didn't), so we can color them differently. |
---|
582 | if (i < input.length - 1) { |
---|
583 | i = furthest; |
---|
584 | lines = input.split('\n'); |
---|
585 | line = (input.slice(0, i).match(/\n/g) || "").length + 1; |
---|
586 | |
---|
587 | for (var n = i, column = -1; n >= 0 && input.charAt(n) !== '\n'; n--) { column++ } |
---|
588 | |
---|
589 | error = { |
---|
590 | type: "Parse", |
---|
591 | message: "Syntax Error on line " + line, |
---|
592 | index: i, |
---|
593 | filename: env.filename, |
---|
594 | line: line, |
---|
595 | column: column, |
---|
596 | extract: [ |
---|
597 | lines[line - 2], |
---|
598 | lines[line - 1], |
---|
599 | lines[line] |
---|
600 | ] |
---|
601 | }; |
---|
602 | } |
---|
603 | |
---|
604 | if (this.imports.queue.length > 0) { |
---|
605 | finish = function (e) { |
---|
606 | e = error || e; |
---|
607 | if (e) callback(e); |
---|
608 | else callback(null, root); |
---|
609 | }; |
---|
610 | } else { |
---|
611 | callback(error, root); |
---|
612 | } |
---|
613 | }, |
---|
614 | |
---|
615 | // |
---|
616 | // Here in, the parsing rules/functions |
---|
617 | // |
---|
618 | // The basic structure of the syntax tree generated is as follows: |
---|
619 | // |
---|
620 | // Ruleset -> Rule -> Value -> Expression -> Entity |
---|
621 | // |
---|
622 | // Here's some LESS code: |
---|
623 | // |
---|
624 | // .class { |
---|
625 | // color: #fff; |
---|
626 | // border: 1px solid #000; |
---|
627 | // width: @w + 4px; |
---|
628 | // > .child {...} |
---|
629 | // } |
---|
630 | // |
---|
631 | // And here's what the parse tree might look like: |
---|
632 | // |
---|
633 | // Ruleset (Selector '.class', [ |
---|
634 | // Rule ("color", Value ([Expression [Color #fff]])) |
---|
635 | // Rule ("border", Value ([Expression [Dimension 1px][Keyword "solid"][Color #000]])) |
---|
636 | // Rule ("width", Value ([Expression [Operation "+" [Variable "@w"][Dimension 4px]]])) |
---|
637 | // Ruleset (Selector [Element '>', '.child'], [...]) |
---|
638 | // ]) |
---|
639 | // |
---|
640 | // In general, most rules will try to parse a token with the `$()` function, and if the return |
---|
641 | // value is truly, will return a new node, of the relevant type. Sometimes, we need to check |
---|
642 | // first, before parsing, that's when we use `peek()`. |
---|
643 | // |
---|
644 | parsers: { |
---|
645 | // |
---|
646 | // The `primary` rule is the *entry* and *exit* point of the parser. |
---|
647 | // The rules here can appear at any level of the parse tree. |
---|
648 | // |
---|
649 | // The recursive nature of the grammar is an interplay between the `block` |
---|
650 | // rule, which represents `{ ... }`, the `ruleset` rule, and this `primary` rule, |
---|
651 | // as represented by this simplified grammar: |
---|
652 | // |
---|
653 | // primary â (ruleset | rule)+ |
---|
654 | // ruleset â selector+ block |
---|
655 | // block â '{' primary '}' |
---|
656 | // |
---|
657 | // Only at one point is the primary rule not called from the |
---|
658 | // block rule: at the root level. |
---|
659 | // |
---|
660 | primary: function () { |
---|
661 | var node, root = []; |
---|
662 | |
---|
663 | while ((node = $(this.mixin.definition) || $(this.rule) || $(this.ruleset) || |
---|
664 | $(this.mixin.call) || $(this.comment) || $(this.directive)) |
---|
665 | || $(/^[\s\n]+/) || $(/^;+/)) { |
---|
666 | node && root.push(node); |
---|
667 | } |
---|
668 | return root; |
---|
669 | }, |
---|
670 | |
---|
671 | // We create a Comment node for CSS comments `/* */`, |
---|
672 | // but keep the LeSS comments `//` silent, by just skipping |
---|
673 | // over them. |
---|
674 | comment: function () { |
---|
675 | var comment; |
---|
676 | |
---|
677 | if (input.charAt(i) !== '/') return; |
---|
678 | |
---|
679 | if (input.charAt(i + 1) === '/') { |
---|
680 | return new(tree.Comment)($(/^\/\/.*/), true); |
---|
681 | } else if (comment = $(/^\/\*(?:[^*]|\*+[^\/*])*\*+\/\n?/)) { |
---|
682 | return new(tree.Comment)(comment); |
---|
683 | } |
---|
684 | }, |
---|
685 | |
---|
686 | // |
---|
687 | // Entities are tokens which can be found inside an Expression |
---|
688 | // |
---|
689 | entities: { |
---|
690 | // |
---|
691 | // A string, which supports escaping " and ' |
---|
692 | // |
---|
693 | // "milky way" 'he\'s the one!' |
---|
694 | // |
---|
695 | quoted: function () { |
---|
696 | var str, j = i, e; |
---|
697 | |
---|
698 | if (input.charAt(j) === '~') { j++, e = true } // Escaped strings |
---|
699 | if (input.charAt(j) !== '"' && input.charAt(j) !== "'") return; |
---|
700 | |
---|
701 | e && $('~'); |
---|
702 | |
---|
703 | if (str = $(/^"((?:[^"\\\r\n]|\\.)*)"|'((?:[^'\\\r\n]|\\.)*)'/)) { |
---|
704 | return new(tree.Quoted)(str[0], str[1] || str[2], e); |
---|
705 | } |
---|
706 | }, |
---|
707 | |
---|
708 | // |
---|
709 | // A catch-all word, such as: |
---|
710 | // |
---|
711 | // black border-collapse |
---|
712 | // |
---|
713 | keyword: function () { |
---|
714 | var k; |
---|
715 | |
---|
716 | if (k = $(/^[_A-Za-z-][_A-Za-z0-9-]*/)) { |
---|
717 | if (tree.colors.hasOwnProperty(k)) { |
---|
718 | // detect named color |
---|
719 | return new(tree.Color)(tree.colors[k].slice(1)); |
---|
720 | } else { |
---|
721 | return new(tree.Keyword)(k); |
---|
722 | } |
---|
723 | } |
---|
724 | }, |
---|
725 | |
---|
726 | // |
---|
727 | // A function call |
---|
728 | // |
---|
729 | // rgb(255, 0, 255) |
---|
730 | // |
---|
731 | // We also try to catch IE's `alpha()`, but let the `alpha` parser |
---|
732 | // deal with the details. |
---|
733 | // |
---|
734 | // The arguments are parsed with the `entities.arguments` parser. |
---|
735 | // |
---|
736 | call: function () { |
---|
737 | var name, nameLC, args, alpha_ret, index = i; |
---|
738 | |
---|
739 | if (! (name = /^([\w-]+|%|progid:[\w\.]+)\(/.exec(chunks[j]))) return; |
---|
740 | |
---|
741 | name = name[1]; |
---|
742 | nameLC = name.toLowerCase(); |
---|
743 | |
---|
744 | if (nameLC === 'url') { return null } |
---|
745 | else { i += name.length } |
---|
746 | |
---|
747 | if (nameLC === 'alpha') { |
---|
748 | alpha_ret = $(this.alpha); |
---|
749 | if(typeof alpha_ret !== 'undefined') { |
---|
750 | return alpha_ret; |
---|
751 | } |
---|
752 | } |
---|
753 | |
---|
754 | $('('); // Parse the '(' and consume whitespace. |
---|
755 | |
---|
756 | args = $(this.entities.arguments); |
---|
757 | |
---|
758 | if (! $(')')) return; |
---|
759 | |
---|
760 | if (name) { return new(tree.Call)(name, args, index, env.filename) } |
---|
761 | }, |
---|
762 | arguments: function () { |
---|
763 | var args = [], arg; |
---|
764 | |
---|
765 | while (arg = $(this.entities.assignment) || $(this.expression)) { |
---|
766 | args.push(arg); |
---|
767 | if (! $(',')) { break } |
---|
768 | } |
---|
769 | return args; |
---|
770 | }, |
---|
771 | literal: function () { |
---|
772 | return $(this.entities.ratio) || |
---|
773 | $(this.entities.dimension) || |
---|
774 | $(this.entities.color) || |
---|
775 | $(this.entities.quoted) || |
---|
776 | $(this.entities.unicodeDescriptor); |
---|
777 | }, |
---|
778 | |
---|
779 | // Assignments are argument entities for calls. |
---|
780 | // They are present in ie filter properties as shown below. |
---|
781 | // |
---|
782 | // filter: progid:DXImageTransform.Microsoft.Alpha( *opacity=50* ) |
---|
783 | // |
---|
784 | |
---|
785 | assignment: function () { |
---|
786 | var key, value; |
---|
787 | if ((key = $(/^\w+(?=\s?=)/i)) && $('=') && (value = $(this.entity))) { |
---|
788 | return new(tree.Assignment)(key, value); |
---|
789 | } |
---|
790 | }, |
---|
791 | |
---|
792 | // |
---|
793 | // Parse url() tokens |
---|
794 | // |
---|
795 | // We use a specific rule for urls, because they don't really behave like |
---|
796 | // standard function calls. The difference is that the argument doesn't have |
---|
797 | // to be enclosed within a string, so it can't be parsed as an Expression. |
---|
798 | // |
---|
799 | url: function () { |
---|
800 | var value; |
---|
801 | |
---|
802 | if (input.charAt(i) !== 'u' || !$(/^url\(/)) return; |
---|
803 | value = $(this.entities.quoted) || $(this.entities.variable) || |
---|
804 | $(/^(?:(?:\\[\(\)'"])|[^\(\)'"])+/) || ""; |
---|
805 | |
---|
806 | expect(')'); |
---|
807 | |
---|
808 | return new(tree.URL)((value.value != null || value instanceof tree.Variable) |
---|
809 | ? value : new(tree.Anonymous)(value), env.rootpath); |
---|
810 | }, |
---|
811 | |
---|
812 | // |
---|
813 | // A Variable entity, such as `@fink`, in |
---|
814 | // |
---|
815 | // width: @fink + 2px |
---|
816 | // |
---|
817 | // We use a different parser for variable definitions, |
---|
818 | // see `parsers.variable`. |
---|
819 | // |
---|
820 | variable: function () { |
---|
821 | var name, index = i; |
---|
822 | |
---|
823 | if (input.charAt(i) === '@' && (name = $(/^@@?[\w-]+/))) { |
---|
824 | return new(tree.Variable)(name, index, env.filename); |
---|
825 | } |
---|
826 | }, |
---|
827 | |
---|
828 | // A variable entity useing the protective {} e.g. @{var} |
---|
829 | variableCurly: function () { |
---|
830 | var name, curly, index = i; |
---|
831 | |
---|
832 | if (input.charAt(i) === '@' && (curly = $(/^@\{([\w-]+)\}/))) { |
---|
833 | return new(tree.Variable)("@" + curly[1], index, env.filename); |
---|
834 | } |
---|
835 | }, |
---|
836 | |
---|
837 | // |
---|
838 | // A Hexadecimal color |
---|
839 | // |
---|
840 | // #4F3C2F |
---|
841 | // |
---|
842 | // `rgb` and `hsl` colors are parsed through the `entities.call` parser. |
---|
843 | // |
---|
844 | color: function () { |
---|
845 | var rgb; |
---|
846 | |
---|
847 | if (input.charAt(i) === '#' && (rgb = $(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})/))) { |
---|
848 | return new(tree.Color)(rgb[1]); |
---|
849 | } |
---|
850 | }, |
---|
851 | |
---|
852 | // |
---|
853 | // A Dimension, that is, a number and a unit |
---|
854 | // |
---|
855 | // 0.5em 95% |
---|
856 | // |
---|
857 | dimension: function () { |
---|
858 | var value, c = input.charCodeAt(i); |
---|
859 | //Is the first char of the dimension 0-9, '.', '+' or '-' |
---|
860 | if ((c > 57 || c < 43) || c === 47 || c == 44) return; |
---|
861 | |
---|
862 | if (value = $(/^([+-]?\d*\.?\d+)(px|%|em|pc|ex|in|deg|s|ms|pt|cm|mm|rad|grad|turn|dpi|dpcm|dppx|rem|vw|vh|vmin|vm|ch)?/)) { |
---|
863 | return new(tree.Dimension)(value[1], value[2]); |
---|
864 | } |
---|
865 | }, |
---|
866 | |
---|
867 | // |
---|
868 | // A Ratio |
---|
869 | // |
---|
870 | // 16/9 |
---|
871 | // |
---|
872 | ratio: function () { |
---|
873 | var value, c = input.charCodeAt(i); |
---|
874 | if (c > 57 || c < 48) return; |
---|
875 | |
---|
876 | if (value = $(/^(\d+\/\d+)/)) { |
---|
877 | return new(tree.Ratio)(value[1]); |
---|
878 | } |
---|
879 | }, |
---|
880 | |
---|
881 | // |
---|
882 | // A unicode descriptor, as is used in unicode-range |
---|
883 | // |
---|
884 | // U+0?? or U+00A1-00A9 |
---|
885 | // |
---|
886 | unicodeDescriptor: function () { |
---|
887 | var ud; |
---|
888 | |
---|
889 | if (ud = $(/^U\+[0-9a-fA-F?]+(\-[0-9a-fA-F?]+)?/)) { |
---|
890 | return new(tree.UnicodeDescriptor)(ud[0]); |
---|
891 | } |
---|
892 | }, |
---|
893 | |
---|
894 | // |
---|
895 | // JavaScript code to be evaluated |
---|
896 | // |
---|
897 | // `window.location.href` |
---|
898 | // |
---|
899 | javascript: function () { |
---|
900 | var str, j = i, e; |
---|
901 | |
---|
902 | if (input.charAt(j) === '~') { j++, e = true } // Escaped strings |
---|
903 | if (input.charAt(j) !== '`') { return } |
---|
904 | |
---|
905 | e && $('~'); |
---|
906 | |
---|
907 | if (str = $(/^`([^`]*)`/)) { |
---|
908 | return new(tree.JavaScript)(str[1], i, e); |
---|
909 | } |
---|
910 | } |
---|
911 | }, |
---|
912 | |
---|
913 | // |
---|
914 | // The variable part of a variable definition. Used in the `rule` parser |
---|
915 | // |
---|
916 | // @fink: |
---|
917 | // |
---|
918 | variable: function () { |
---|
919 | var name; |
---|
920 | |
---|
921 | if (input.charAt(i) === '@' && (name = $(/^(@[\w-]+)\s*:/))) { return name[1] } |
---|
922 | }, |
---|
923 | |
---|
924 | // |
---|
925 | // A font size/line-height shorthand |
---|
926 | // |
---|
927 | // small/12px |
---|
928 | // |
---|
929 | // We need to peek first, or we'll match on keywords and dimensions |
---|
930 | // |
---|
931 | shorthand: function () { |
---|
932 | var a, b; |
---|
933 | |
---|
934 | if (! peek(/^[@\w.%-]+\/[@\w.-]+/)) return; |
---|
935 | |
---|
936 | save(); |
---|
937 | |
---|
938 | if ((a = $(this.entity)) && $('/') && (b = $(this.entity))) { |
---|
939 | return new(tree.Shorthand)(a, b); |
---|
940 | } |
---|
941 | |
---|
942 | restore(); |
---|
943 | }, |
---|
944 | |
---|
945 | // |
---|
946 | // Mixins |
---|
947 | // |
---|
948 | mixin: { |
---|
949 | // |
---|
950 | // A Mixin call, with an optional argument list |
---|
951 | // |
---|
952 | // #mixins > .square(#fff); |
---|
953 | // .rounded(4px, black); |
---|
954 | // .button; |
---|
955 | // |
---|
956 | // The `while` loop is there because mixins can be |
---|
957 | // namespaced, but we only support the child and descendant |
---|
958 | // selector for now. |
---|
959 | // |
---|
960 | call: function () { |
---|
961 | var elements = [], e, c, argsSemiColon = [], argsComma = [], args, delim, arg, nameLoop, expressions, isSemiColonSeperated, expressionContainsNamed, index = i, s = input.charAt(i), name, value, important = false; |
---|
962 | |
---|
963 | if (s !== '.' && s !== '#') { return } |
---|
964 | |
---|
965 | save(); // stop us absorbing part of an invalid selector |
---|
966 | |
---|
967 | while (e = $(/^[#.](?:[\w-]|\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+/)) { |
---|
968 | elements.push(new(tree.Element)(c, e, i)); |
---|
969 | c = $('>'); |
---|
970 | } |
---|
971 | if ($('(')) { |
---|
972 | expressions = []; |
---|
973 | while (arg = $(this.expression)) { |
---|
974 | nameLoop = null; |
---|
975 | value = arg; |
---|
976 | |
---|
977 | // Variable |
---|
978 | if (arg.value.length == 1) { |
---|
979 | var val = arg.value[0]; |
---|
980 | if (val instanceof tree.Variable) { |
---|
981 | if ($(':')) { |
---|
982 | if (expressions.length > 0) { |
---|
983 | if (isSemiColonSeperated) { |
---|
984 | error("Cannot mix ; and , as delimiter types"); |
---|
985 | } |
---|
986 | expressionContainsNamed = true; |
---|
987 | } |
---|
988 | value = expect(this.expression); |
---|
989 | nameLoop = (name = val.name); |
---|
990 | } |
---|
991 | } |
---|
992 | } |
---|
993 | |
---|
994 | expressions.push(value); |
---|
995 | |
---|
996 | argsComma.push({ name: nameLoop, value: value }); |
---|
997 | |
---|
998 | if ($(',')) { |
---|
999 | continue; |
---|
1000 | } |
---|
1001 | |
---|
1002 | if ($(';') || isSemiColonSeperated) { |
---|
1003 | |
---|
1004 | if (expressionContainsNamed) { |
---|
1005 | error("Cannot mix ; and , as delimiter types"); |
---|
1006 | } |
---|
1007 | |
---|
1008 | isSemiColonSeperated = true; |
---|
1009 | |
---|
1010 | if (expressions.length > 1) { |
---|
1011 | value = new(tree.Value)(expressions); |
---|
1012 | } |
---|
1013 | argsSemiColon.push({ name: name, value: value }); |
---|
1014 | |
---|
1015 | name = null; |
---|
1016 | expressions = []; |
---|
1017 | expressionContainsNamed = false; |
---|
1018 | } |
---|
1019 | } |
---|
1020 | |
---|
1021 | expect(')'); |
---|
1022 | } |
---|
1023 | |
---|
1024 | args = isSemiColonSeperated ? argsSemiColon : argsComma; |
---|
1025 | |
---|
1026 | if ($(this.important)) { |
---|
1027 | important = true; |
---|
1028 | } |
---|
1029 | |
---|
1030 | if (elements.length > 0 && ($(';') || peek('}'))) { |
---|
1031 | return new(tree.mixin.Call)(elements, args, index, env.filename, important); |
---|
1032 | } |
---|
1033 | |
---|
1034 | restore(); |
---|
1035 | }, |
---|
1036 | |
---|
1037 | // |
---|
1038 | // A Mixin definition, with a list of parameters |
---|
1039 | // |
---|
1040 | // .rounded (@radius: 2px, @color) { |
---|
1041 | // ... |
---|
1042 | // } |
---|
1043 | // |
---|
1044 | // Until we have a finer grained state-machine, we have to |
---|
1045 | // do a look-ahead, to make sure we don't have a mixin call. |
---|
1046 | // See the `rule` function for more information. |
---|
1047 | // |
---|
1048 | // We start by matching `.rounded (`, and then proceed on to |
---|
1049 | // the argument list, which has optional default values. |
---|
1050 | // We store the parameters in `params`, with a `value` key, |
---|
1051 | // if there is a value, such as in the case of `@radius`. |
---|
1052 | // |
---|
1053 | // Once we've got our params list, and a closing `)`, we parse |
---|
1054 | // the `{...}` block. |
---|
1055 | // |
---|
1056 | definition: function () { |
---|
1057 | var name, params = [], match, ruleset, param, value, cond, variadic = false; |
---|
1058 | if ((input.charAt(i) !== '.' && input.charAt(i) !== '#') || |
---|
1059 | peek(/^[^{]*\}/)) return; |
---|
1060 | |
---|
1061 | save(); |
---|
1062 | |
---|
1063 | if (match = $(/^([#.](?:[\w-]|\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+)\s*\(/)) { |
---|
1064 | name = match[1]; |
---|
1065 | |
---|
1066 | do { |
---|
1067 | $(this.comment); |
---|
1068 | if (input.charAt(i) === '.' && $(/^\.{3}/)) { |
---|
1069 | variadic = true; |
---|
1070 | params.push({ variadic: true }); |
---|
1071 | break; |
---|
1072 | } else if (param = $(this.entities.variable) || $(this.entities.literal) |
---|
1073 | || $(this.entities.keyword)) { |
---|
1074 | // Variable |
---|
1075 | if (param instanceof tree.Variable) { |
---|
1076 | if ($(':')) { |
---|
1077 | value = expect(this.expression, 'expected expression'); |
---|
1078 | params.push({ name: param.name, value: value }); |
---|
1079 | } else if ($(/^\.{3}/)) { |
---|
1080 | params.push({ name: param.name, variadic: true }); |
---|
1081 | variadic = true; |
---|
1082 | break; |
---|
1083 | } else { |
---|
1084 | params.push({ name: param.name }); |
---|
1085 | } |
---|
1086 | } else { |
---|
1087 | params.push({ value: param }); |
---|
1088 | } |
---|
1089 | } else { |
---|
1090 | break; |
---|
1091 | } |
---|
1092 | } while ($(',') || $(';')) |
---|
1093 | |
---|
1094 | // .mixincall("@{a}"); |
---|
1095 | // looks a bit like a mixin definition.. so we have to be nice and restore |
---|
1096 | if (!$(')')) { |
---|
1097 | furthest = i; |
---|
1098 | restore(); |
---|
1099 | } |
---|
1100 | |
---|
1101 | $(this.comment); |
---|
1102 | |
---|
1103 | if ($(/^when/)) { // Guard |
---|
1104 | cond = expect(this.conditions, 'expected condition'); |
---|
1105 | } |
---|
1106 | |
---|
1107 | ruleset = $(this.block); |
---|
1108 | |
---|
1109 | if (ruleset) { |
---|
1110 | return new(tree.mixin.Definition)(name, params, ruleset, cond, variadic); |
---|
1111 | } else { |
---|
1112 | restore(); |
---|
1113 | } |
---|
1114 | } |
---|
1115 | } |
---|
1116 | }, |
---|
1117 | |
---|
1118 | // |
---|
1119 | // Entities are the smallest recognized token, |
---|
1120 | // and can be found inside a rule's value. |
---|
1121 | // |
---|
1122 | entity: function () { |
---|
1123 | return $(this.entities.literal) || $(this.entities.variable) || $(this.entities.url) || |
---|
1124 | $(this.entities.call) || $(this.entities.keyword) ||$(this.entities.javascript) || |
---|
1125 | $(this.comment); |
---|
1126 | }, |
---|
1127 | |
---|
1128 | // |
---|
1129 | // A Rule terminator. Note that we use `peek()` to check for '}', |
---|
1130 | // because the `block` rule will be expecting it, but we still need to make sure |
---|
1131 | // it's there, if ';' was ommitted. |
---|
1132 | // |
---|
1133 | end: function () { |
---|
1134 | return $(';') || peek('}'); |
---|
1135 | }, |
---|
1136 | |
---|
1137 | // |
---|
1138 | // IE's alpha function |
---|
1139 | // |
---|
1140 | // alpha(opacity=88) |
---|
1141 | // |
---|
1142 | alpha: function () { |
---|
1143 | var value; |
---|
1144 | |
---|
1145 | if (! $(/^\(opacity=/i)) return; |
---|
1146 | if (value = $(/^\d+/) || $(this.entities.variable)) { |
---|
1147 | expect(')'); |
---|
1148 | return new(tree.Alpha)(value); |
---|
1149 | } |
---|
1150 | }, |
---|
1151 | |
---|
1152 | // |
---|
1153 | // A Selector Element |
---|
1154 | // |
---|
1155 | // div |
---|
1156 | // + h1 |
---|
1157 | // #socks |
---|
1158 | // input[type="text"] |
---|
1159 | // |
---|
1160 | // Elements are the building blocks for Selectors, |
---|
1161 | // they are made out of a `Combinator` (see combinator rule), |
---|
1162 | // and an element name, such as a tag a class, or `*`. |
---|
1163 | // |
---|
1164 | element: function () { |
---|
1165 | var e, t, c, v; |
---|
1166 | |
---|
1167 | c = $(this.combinator); |
---|
1168 | |
---|
1169 | e = $(/^(?:\d+\.\d+|\d+)%/) || $(/^(?:[.#]?|:*)(?:[\w-]|[^\x00-\x9f]|\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+/) || |
---|
1170 | $('*') || $('&') || $(this.attribute) || $(/^\([^()@]+\)/) || $(/^[\.#](?=@)/) || $(this.entities.variableCurly); |
---|
1171 | |
---|
1172 | if (! e) { |
---|
1173 | if ($('(')) { |
---|
1174 | if ((v = ($(this.entities.variableCurly) || |
---|
1175 | $(this.entities.variable) || |
---|
1176 | $(this.selector))) && |
---|
1177 | $(')')) { |
---|
1178 | e = new(tree.Paren)(v); |
---|
1179 | } |
---|
1180 | } |
---|
1181 | } |
---|
1182 | |
---|
1183 | if (e) { return new(tree.Element)(c, e, i) } |
---|
1184 | }, |
---|
1185 | |
---|
1186 | // |
---|
1187 | // Combinators combine elements together, in a Selector. |
---|
1188 | // |
---|
1189 | // Because our parser isn't white-space sensitive, special care |
---|
1190 | // has to be taken, when parsing the descendant combinator, ` `, |
---|
1191 | // as it's an empty space. We have to check the previous character |
---|
1192 | // in the input, to see if it's a ` ` character. More info on how |
---|
1193 | // we deal with this in *combinator.js*. |
---|
1194 | // |
---|
1195 | combinator: function () { |
---|
1196 | var match, c = input.charAt(i); |
---|
1197 | |
---|
1198 | if (c === '>' || c === '+' || c === '~' || c === '|') { |
---|
1199 | i++; |
---|
1200 | while (input.charAt(i).match(/\s/)) { i++ } |
---|
1201 | return new(tree.Combinator)(c); |
---|
1202 | } else if (input.charAt(i - 1).match(/\s/)) { |
---|
1203 | return new(tree.Combinator)(" "); |
---|
1204 | } else { |
---|
1205 | return new(tree.Combinator)(null); |
---|
1206 | } |
---|
1207 | }, |
---|
1208 | |
---|
1209 | // |
---|
1210 | // A CSS Selector |
---|
1211 | // |
---|
1212 | // .class > div + h1 |
---|
1213 | // li a:hover |
---|
1214 | // |
---|
1215 | // Selectors are made out of one or more Elements, see above. |
---|
1216 | // |
---|
1217 | selector: function () { |
---|
1218 | var sel, e, elements = [], c, match; |
---|
1219 | |
---|
1220 | // depreciated, will be removed soon |
---|
1221 | if ($('(')) { |
---|
1222 | sel = $(this.entity); |
---|
1223 | expect(')'); |
---|
1224 | return new(tree.Selector)([new(tree.Element)('', sel, i)]); |
---|
1225 | } |
---|
1226 | |
---|
1227 | while (e = $(this.element)) { |
---|
1228 | c = input.charAt(i); |
---|
1229 | elements.push(e) |
---|
1230 | if (c === '{' || c === '}' || c === ';' || c === ',' || c === ')') { break } |
---|
1231 | } |
---|
1232 | |
---|
1233 | if (elements.length > 0) { return new(tree.Selector)(elements) } |
---|
1234 | }, |
---|
1235 | attribute: function () { |
---|
1236 | var attr = '', key, val, op; |
---|
1237 | |
---|
1238 | if (! $('[')) return; |
---|
1239 | |
---|
1240 | if (key = $(/^(?:[_A-Za-z0-9-]|\\.)+/) || $(this.entities.quoted)) { |
---|
1241 | if ((op = $(/^[|~*$^]?=/)) && |
---|
1242 | (val = $(this.entities.quoted) || $(/^[\w-]+/))) { |
---|
1243 | attr = [key, op, val.toCSS ? val.toCSS() : val].join(''); |
---|
1244 | } else { attr = key } |
---|
1245 | } |
---|
1246 | |
---|
1247 | if (! $(']')) return; |
---|
1248 | |
---|
1249 | if (attr) { return "[" + attr + "]" } |
---|
1250 | }, |
---|
1251 | |
---|
1252 | // |
---|
1253 | // The `block` rule is used by `ruleset` and `mixin.definition`. |
---|
1254 | // It's a wrapper around the `primary` rule, with added `{}`. |
---|
1255 | // |
---|
1256 | block: function () { |
---|
1257 | var content; |
---|
1258 | if ($('{') && (content = $(this.primary)) && $('}')) { |
---|
1259 | return content; |
---|
1260 | } |
---|
1261 | }, |
---|
1262 | |
---|
1263 | // |
---|
1264 | // div, .class, body > p {...} |
---|
1265 | // |
---|
1266 | ruleset: function () { |
---|
1267 | var selectors = [], s, rules, match, debugInfo; |
---|
1268 | save(); |
---|
1269 | |
---|
1270 | if (env.dumpLineNumbers) |
---|
1271 | debugInfo = getDebugInfo(i, input, env); |
---|
1272 | |
---|
1273 | while (s = $(this.selector)) { |
---|
1274 | selectors.push(s); |
---|
1275 | $(this.comment); |
---|
1276 | if (! $(',')) { break } |
---|
1277 | $(this.comment); |
---|
1278 | } |
---|
1279 | |
---|
1280 | if (selectors.length > 0 && (rules = $(this.block))) { |
---|
1281 | var ruleset = new(tree.Ruleset)(selectors, rules, env.strictImports); |
---|
1282 | if (env.dumpLineNumbers) |
---|
1283 | ruleset.debugInfo = debugInfo; |
---|
1284 | return ruleset; |
---|
1285 | } else { |
---|
1286 | // Backtrack |
---|
1287 | furthest = i; |
---|
1288 | restore(); |
---|
1289 | } |
---|
1290 | }, |
---|
1291 | rule: function () { |
---|
1292 | var name, value, c = input.charAt(i), important, match; |
---|
1293 | save(); |
---|
1294 | |
---|
1295 | if (c === '.' || c === '#' || c === '&') { return } |
---|
1296 | |
---|
1297 | if (name = $(this.variable) || $(this.property)) { |
---|
1298 | if ((name.charAt(0) != '@') && (match = /^([^@+\/'"*`(;{}-]*);/.exec(chunks[j]))) { |
---|
1299 | i += match[0].length - 1; |
---|
1300 | value = new(tree.Anonymous)(match[1]); |
---|
1301 | } else if (name === "font") { |
---|
1302 | value = $(this.font); |
---|
1303 | } else { |
---|
1304 | value = $(this.value); |
---|
1305 | } |
---|
1306 | important = $(this.important); |
---|
1307 | |
---|
1308 | if (value && $(this.end)) { |
---|
1309 | return new(tree.Rule)(name, value, important, memo); |
---|
1310 | } else { |
---|
1311 | furthest = i; |
---|
1312 | restore(); |
---|
1313 | } |
---|
1314 | } |
---|
1315 | }, |
---|
1316 | |
---|
1317 | // |
---|
1318 | // An @import directive |
---|
1319 | // |
---|
1320 | // @import "lib"; |
---|
1321 | // |
---|
1322 | // Depending on our environemnt, importing is done differently: |
---|
1323 | // In the browser, it's an XHR request, in Node, it would be a |
---|
1324 | // file-system operation. The function used for importing is |
---|
1325 | // stored in `import`, which we pass to the Import constructor. |
---|
1326 | // |
---|
1327 | "import": function () { |
---|
1328 | var path, features, index = i; |
---|
1329 | |
---|
1330 | save(); |
---|
1331 | |
---|
1332 | var dir = $(/^@import(?:-(once))?\s+/); |
---|
1333 | |
---|
1334 | if (dir && (path = $(this.entities.quoted) || $(this.entities.url))) { |
---|
1335 | features = $(this.mediaFeatures); |
---|
1336 | if ($(';')) { |
---|
1337 | return new(tree.Import)(path, imports, features, (dir[1] === 'once'), index, env.rootpath); |
---|
1338 | } |
---|
1339 | } |
---|
1340 | |
---|
1341 | restore(); |
---|
1342 | }, |
---|
1343 | |
---|
1344 | mediaFeature: function () { |
---|
1345 | var e, p, nodes = []; |
---|
1346 | |
---|
1347 | do { |
---|
1348 | if (e = $(this.entities.keyword)) { |
---|
1349 | nodes.push(e); |
---|
1350 | } else if ($('(')) { |
---|
1351 | p = $(this.property); |
---|
1352 | e = $(this.entity); |
---|
1353 | if ($(')')) { |
---|
1354 | if (p && e) { |
---|
1355 | nodes.push(new(tree.Paren)(new(tree.Rule)(p, e, null, i, true))); |
---|
1356 | } else if (e) { |
---|
1357 | nodes.push(new(tree.Paren)(e)); |
---|
1358 | } else { |
---|
1359 | return null; |
---|
1360 | } |
---|
1361 | } else { return null } |
---|
1362 | } |
---|
1363 | } while (e); |
---|
1364 | |
---|
1365 | if (nodes.length > 0) { |
---|
1366 | return new(tree.Expression)(nodes); |
---|
1367 | } |
---|
1368 | }, |
---|
1369 | |
---|
1370 | mediaFeatures: function () { |
---|
1371 | var e, features = []; |
---|
1372 | |
---|
1373 | do { |
---|
1374 | if (e = $(this.mediaFeature)) { |
---|
1375 | features.push(e); |
---|
1376 | if (! $(',')) { break } |
---|
1377 | } else if (e = $(this.entities.variable)) { |
---|
1378 | features.push(e); |
---|
1379 | if (! $(',')) { break } |
---|
1380 | } |
---|
1381 | } while (e); |
---|
1382 | |
---|
1383 | return features.length > 0 ? features : null; |
---|
1384 | }, |
---|
1385 | |
---|
1386 | media: function () { |
---|
1387 | var features, rules, media, debugInfo; |
---|
1388 | |
---|
1389 | if (env.dumpLineNumbers) |
---|
1390 | debugInfo = getDebugInfo(i, input, env); |
---|
1391 | |
---|
1392 | if ($(/^@media/)) { |
---|
1393 | features = $(this.mediaFeatures); |
---|
1394 | |
---|
1395 | if (rules = $(this.block)) { |
---|
1396 | media = new(tree.Media)(rules, features); |
---|
1397 | if(env.dumpLineNumbers) |
---|
1398 | media.debugInfo = debugInfo; |
---|
1399 | return media; |
---|
1400 | } |
---|
1401 | } |
---|
1402 | }, |
---|
1403 | |
---|
1404 | // |
---|
1405 | // A CSS Directive |
---|
1406 | // |
---|
1407 | // @charset "utf-8"; |
---|
1408 | // |
---|
1409 | directive: function () { |
---|
1410 | var name, value, rules, identifier, e, nodes, nonVendorSpecificName, |
---|
1411 | hasBlock, hasIdentifier, hasExpression; |
---|
1412 | |
---|
1413 | if (input.charAt(i) !== '@') return; |
---|
1414 | |
---|
1415 | if (value = $(this['import']) || $(this.media)) { |
---|
1416 | return value; |
---|
1417 | } |
---|
1418 | |
---|
1419 | save(); |
---|
1420 | |
---|
1421 | name = $(/^@[a-z-]+/); |
---|
1422 | |
---|
1423 | if (!name) return; |
---|
1424 | |
---|
1425 | nonVendorSpecificName = name; |
---|
1426 | if (name.charAt(1) == '-' && name.indexOf('-', 2) > 0) { |
---|
1427 | nonVendorSpecificName = "@" + name.slice(name.indexOf('-', 2) + 1); |
---|
1428 | } |
---|
1429 | |
---|
1430 | switch(nonVendorSpecificName) { |
---|
1431 | case "@font-face": |
---|
1432 | hasBlock = true; |
---|
1433 | break; |
---|
1434 | case "@viewport": |
---|
1435 | case "@top-left": |
---|
1436 | case "@top-left-corner": |
---|
1437 | case "@top-center": |
---|
1438 | case "@top-right": |
---|
1439 | case "@top-right-corner": |
---|
1440 | case "@bottom-left": |
---|
1441 | case "@bottom-left-corner": |
---|
1442 | case "@bottom-center": |
---|
1443 | case "@bottom-right": |
---|
1444 | case "@bottom-right-corner": |
---|
1445 | case "@left-top": |
---|
1446 | case "@left-middle": |
---|
1447 | case "@left-bottom": |
---|
1448 | case "@right-top": |
---|
1449 | case "@right-middle": |
---|
1450 | case "@right-bottom": |
---|
1451 | hasBlock = true; |
---|
1452 | break; |
---|
1453 | case "@page": |
---|
1454 | case "@document": |
---|
1455 | case "@supports": |
---|
1456 | case "@keyframes": |
---|
1457 | hasBlock = true; |
---|
1458 | hasIdentifier = true; |
---|
1459 | break; |
---|
1460 | case "@namespace": |
---|
1461 | hasExpression = true; |
---|
1462 | break; |
---|
1463 | } |
---|
1464 | |
---|
1465 | if (hasIdentifier) { |
---|
1466 | name += " " + ($(/^[^{]+/) || '').trim(); |
---|
1467 | } |
---|
1468 | |
---|
1469 | if (hasBlock) |
---|
1470 | { |
---|
1471 | if (rules = $(this.block)) { |
---|
1472 | return new(tree.Directive)(name, rules); |
---|
1473 | } |
---|
1474 | } else { |
---|
1475 | if ((value = hasExpression ? $(this.expression) : $(this.entity)) && $(';')) { |
---|
1476 | var directive = new(tree.Directive)(name, value); |
---|
1477 | if (env.dumpLineNumbers) { |
---|
1478 | directive.debugInfo = getDebugInfo(i, input, env); |
---|
1479 | } |
---|
1480 | return directive; |
---|
1481 | } |
---|
1482 | } |
---|
1483 | |
---|
1484 | restore(); |
---|
1485 | }, |
---|
1486 | font: function () { |
---|
1487 | var value = [], expression = [], weight, shorthand, font, e; |
---|
1488 | |
---|
1489 | while (e = $(this.shorthand) || $(this.entity)) { |
---|
1490 | expression.push(e); |
---|
1491 | } |
---|
1492 | value.push(new(tree.Expression)(expression)); |
---|
1493 | |
---|
1494 | if ($(',')) { |
---|
1495 | while (e = $(this.expression)) { |
---|
1496 | value.push(e); |
---|
1497 | if (! $(',')) { break } |
---|
1498 | } |
---|
1499 | } |
---|
1500 | return new(tree.Value)(value); |
---|
1501 | }, |
---|
1502 | |
---|
1503 | // |
---|
1504 | // A Value is a comma-delimited list of Expressions |
---|
1505 | // |
---|
1506 | // font-family: Baskerville, Georgia, serif; |
---|
1507 | // |
---|
1508 | // In a Rule, a Value represents everything after the `:`, |
---|
1509 | // and before the `;`. |
---|
1510 | // |
---|
1511 | value: function () { |
---|
1512 | var e, expressions = [], important; |
---|
1513 | |
---|
1514 | while (e = $(this.expression)) { |
---|
1515 | expressions.push(e); |
---|
1516 | if (! $(',')) { break } |
---|
1517 | } |
---|
1518 | |
---|
1519 | if (expressions.length > 0) { |
---|
1520 | return new(tree.Value)(expressions); |
---|
1521 | } |
---|
1522 | }, |
---|
1523 | important: function () { |
---|
1524 | if (input.charAt(i) === '!') { |
---|
1525 | return $(/^! *important/); |
---|
1526 | } |
---|
1527 | }, |
---|
1528 | sub: function () { |
---|
1529 | var e; |
---|
1530 | |
---|
1531 | if ($('(') && (e = $(this.expression)) && $(')')) { |
---|
1532 | return e; |
---|
1533 | } |
---|
1534 | }, |
---|
1535 | multiplication: function () { |
---|
1536 | var m, a, op, operation; |
---|
1537 | if (m = $(this.operand)) { |
---|
1538 | while (!peek(/^\/[*\/]/) && (op = ($('/') || $('*'))) && (a = $(this.operand))) { |
---|
1539 | operation = new(tree.Operation)(op, [operation || m, a]); |
---|
1540 | } |
---|
1541 | return operation || m; |
---|
1542 | } |
---|
1543 | }, |
---|
1544 | addition: function () { |
---|
1545 | var m, a, op, operation; |
---|
1546 | if (m = $(this.multiplication)) { |
---|
1547 | while ((op = $(/^[-+]\s+/) || (!isWhitespace(input.charAt(i - 1)) && ($('+') || $('-')))) && |
---|
1548 | (a = $(this.multiplication))) { |
---|
1549 | operation = new(tree.Operation)(op, [operation || m, a]); |
---|
1550 | } |
---|
1551 | return operation || m; |
---|
1552 | } |
---|
1553 | }, |
---|
1554 | conditions: function () { |
---|
1555 | var a, b, index = i, condition; |
---|
1556 | |
---|
1557 | if (a = $(this.condition)) { |
---|
1558 | while ($(',') && (b = $(this.condition))) { |
---|
1559 | condition = new(tree.Condition)('or', condition || a, b, index); |
---|
1560 | } |
---|
1561 | return condition || a; |
---|
1562 | } |
---|
1563 | }, |
---|
1564 | condition: function () { |
---|
1565 | var a, b, c, op, index = i, negate = false; |
---|
1566 | |
---|
1567 | if ($(/^not/)) { negate = true } |
---|
1568 | expect('('); |
---|
1569 | if (a = $(this.addition) || $(this.entities.keyword) || $(this.entities.quoted)) { |
---|
1570 | if (op = $(/^(?:>=|=<|[<=>])/)) { |
---|
1571 | if (b = $(this.addition) || $(this.entities.keyword) || $(this.entities.quoted)) { |
---|
1572 | c = new(tree.Condition)(op, a, b, index, negate); |
---|
1573 | } else { |
---|
1574 | error('expected expression'); |
---|
1575 | } |
---|
1576 | } else { |
---|
1577 | c = new(tree.Condition)('=', a, new(tree.Keyword)('true'), index, negate); |
---|
1578 | } |
---|
1579 | expect(')'); |
---|
1580 | return $(/^and/) ? new(tree.Condition)('and', c, $(this.condition)) : c; |
---|
1581 | } |
---|
1582 | }, |
---|
1583 | |
---|
1584 | // |
---|
1585 | // An operand is anything that can be part of an operation, |
---|
1586 | // such as a Color, or a Variable |
---|
1587 | // |
---|
1588 | operand: function () { |
---|
1589 | var negate, p = input.charAt(i + 1); |
---|
1590 | |
---|
1591 | if (input.charAt(i) === '-' && (p === '@' || p === '(')) { negate = $('-') } |
---|
1592 | var o = $(this.sub) || $(this.entities.dimension) || |
---|
1593 | $(this.entities.color) || $(this.entities.variable) || |
---|
1594 | $(this.entities.call); |
---|
1595 | return negate ? new(tree.Operation)('*', [new(tree.Dimension)(-1), o]) |
---|
1596 | : o; |
---|
1597 | }, |
---|
1598 | |
---|
1599 | // |
---|
1600 | // Expressions either represent mathematical operations, |
---|
1601 | // or white-space delimited Entities. |
---|
1602 | // |
---|
1603 | // 1px solid black |
---|
1604 | // @var * 2 |
---|
1605 | // |
---|
1606 | expression: function () { |
---|
1607 | var e, delim, entities = [], d; |
---|
1608 | |
---|
1609 | while (e = $(this.addition) || $(this.entity)) { |
---|
1610 | entities.push(e); |
---|
1611 | } |
---|
1612 | if (entities.length > 0) { |
---|
1613 | return new(tree.Expression)(entities); |
---|
1614 | } |
---|
1615 | }, |
---|
1616 | property: function () { |
---|
1617 | var name; |
---|
1618 | |
---|
1619 | if (name = $(/^(\*?-?[_a-z0-9-]+)\s*:/)) { |
---|
1620 | return name[1]; |
---|
1621 | } |
---|
1622 | } |
---|
1623 | } |
---|
1624 | }; |
---|
1625 | }; |
---|
1626 | |
---|
1627 | if (less.mode === 'browser' || less.mode === 'rhino') { |
---|
1628 | // |
---|
1629 | // Used by `@import` directives |
---|
1630 | // |
---|
1631 | less.Parser.importer = function (path, paths, callback, env) { |
---|
1632 | if (!/^([a-z-]+:)?\//.test(path) && paths.length > 0) { |
---|
1633 | path = paths[0] + path; |
---|
1634 | } |
---|
1635 | // We pass `true` as 3rd argument, to force the reload of the import. |
---|
1636 | // This is so we can get the syntax tree as opposed to just the CSS output, |
---|
1637 | // as we need this to evaluate the current stylesheet. |
---|
1638 | loadStyleSheet({ |
---|
1639 | href: path, |
---|
1640 | title: path, |
---|
1641 | type: env.mime, |
---|
1642 | contents: env.contents, |
---|
1643 | files: env.files, |
---|
1644 | rootpath: env.rootpath, |
---|
1645 | entryPath: env.entryPath, |
---|
1646 | relativeUrls: env.relativeUrls }, |
---|
1647 | function (e, root, data, sheet, _, path) { |
---|
1648 | if (e && typeof(env.errback) === "function") { |
---|
1649 | env.errback.call(null, path, paths, callback, env); |
---|
1650 | } else { |
---|
1651 | callback.call(null, e, root, path); |
---|
1652 | } |
---|
1653 | }, true); |
---|
1654 | }; |
---|
1655 | } |
---|
1656 | |
---|
1657 | (function (tree) { |
---|
1658 | |
---|
1659 | tree.functions = { |
---|
1660 | rgb: function (r, g, b) { |
---|
1661 | return this.rgba(r, g, b, 1.0); |
---|
1662 | }, |
---|
1663 | rgba: function (r, g, b, a) { |
---|
1664 | var rgb = [r, g, b].map(function (c) { return scaled(c, 256); }); |
---|
1665 | a = number(a); |
---|
1666 | return new(tree.Color)(rgb, a); |
---|
1667 | }, |
---|
1668 | hsl: function (h, s, l) { |
---|
1669 | return this.hsla(h, s, l, 1.0); |
---|
1670 | }, |
---|
1671 | hsla: function (h, s, l, a) { |
---|
1672 | h = (number(h) % 360) / 360; |
---|
1673 | s = number(s); l = number(l); a = number(a); |
---|
1674 | |
---|
1675 | var m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s; |
---|
1676 | var m1 = l * 2 - m2; |
---|
1677 | |
---|
1678 | return this.rgba(hue(h + 1/3) * 255, |
---|
1679 | hue(h) * 255, |
---|
1680 | hue(h - 1/3) * 255, |
---|
1681 | a); |
---|
1682 | |
---|
1683 | function hue(h) { |
---|
1684 | h = h < 0 ? h + 1 : (h > 1 ? h - 1 : h); |
---|
1685 | if (h * 6 < 1) return m1 + (m2 - m1) * h * 6; |
---|
1686 | else if (h * 2 < 1) return m2; |
---|
1687 | else if (h * 3 < 2) return m1 + (m2 - m1) * (2/3 - h) * 6; |
---|
1688 | else return m1; |
---|
1689 | } |
---|
1690 | }, |
---|
1691 | |
---|
1692 | hsv: function(h, s, v) { |
---|
1693 | return this.hsva(h, s, v, 1.0); |
---|
1694 | }, |
---|
1695 | |
---|
1696 | hsva: function(h, s, v, a) { |
---|
1697 | h = ((number(h) % 360) / 360) * 360; |
---|
1698 | s = number(s); v = number(v); a = number(a); |
---|
1699 | |
---|
1700 | var i, f; |
---|
1701 | i = Math.floor((h / 60) % 6); |
---|
1702 | f = (h / 60) - i; |
---|
1703 | |
---|
1704 | var vs = [v, |
---|
1705 | v * (1 - s), |
---|
1706 | v * (1 - f * s), |
---|
1707 | v * (1 - (1 - f) * s)]; |
---|
1708 | var perm = [[0, 3, 1], |
---|
1709 | [2, 0, 1], |
---|
1710 | [1, 0, 3], |
---|
1711 | [1, 2, 0], |
---|
1712 | [3, 1, 0], |
---|
1713 | [0, 1, 2]]; |
---|
1714 | |
---|
1715 | return this.rgba(vs[perm[i][0]] * 255, |
---|
1716 | vs[perm[i][1]] * 255, |
---|
1717 | vs[perm[i][2]] * 255, |
---|
1718 | a); |
---|
1719 | }, |
---|
1720 | |
---|
1721 | hue: function (color) { |
---|
1722 | return new(tree.Dimension)(Math.round(color.toHSL().h)); |
---|
1723 | }, |
---|
1724 | saturation: function (color) { |
---|
1725 | return new(tree.Dimension)(Math.round(color.toHSL().s * 100), '%'); |
---|
1726 | }, |
---|
1727 | lightness: function (color) { |
---|
1728 | return new(tree.Dimension)(Math.round(color.toHSL().l * 100), '%'); |
---|
1729 | }, |
---|
1730 | red: function (color) { |
---|
1731 | return new(tree.Dimension)(color.rgb[0]); |
---|
1732 | }, |
---|
1733 | green: function (color) { |
---|
1734 | return new(tree.Dimension)(color.rgb[1]); |
---|
1735 | }, |
---|
1736 | blue: function (color) { |
---|
1737 | return new(tree.Dimension)(color.rgb[2]); |
---|
1738 | }, |
---|
1739 | alpha: function (color) { |
---|
1740 | return new(tree.Dimension)(color.toHSL().a); |
---|
1741 | }, |
---|
1742 | luma: function (color) { |
---|
1743 | return new(tree.Dimension)(Math.round((0.2126 * (color.rgb[0]/255) + |
---|
1744 | 0.7152 * (color.rgb[1]/255) + |
---|
1745 | 0.0722 * (color.rgb[2]/255)) * |
---|
1746 | color.alpha * 100), '%'); |
---|
1747 | }, |
---|
1748 | saturate: function (color, amount) { |
---|
1749 | var hsl = color.toHSL(); |
---|
1750 | |
---|
1751 | hsl.s += amount.value / 100; |
---|
1752 | hsl.s = clamp(hsl.s); |
---|
1753 | return hsla(hsl); |
---|
1754 | }, |
---|
1755 | desaturate: function (color, amount) { |
---|
1756 | var hsl = color.toHSL(); |
---|
1757 | |
---|
1758 | hsl.s -= amount.value / 100; |
---|
1759 | hsl.s = clamp(hsl.s); |
---|
1760 | return hsla(hsl); |
---|
1761 | }, |
---|
1762 | lighten: function (color, amount) { |
---|
1763 | var hsl = color.toHSL(); |
---|
1764 | |
---|
1765 | hsl.l += amount.value / 100; |
---|
1766 | hsl.l = clamp(hsl.l); |
---|
1767 | return hsla(hsl); |
---|
1768 | }, |
---|
1769 | darken: function (color, amount) { |
---|
1770 | var hsl = color.toHSL(); |
---|
1771 | |
---|
1772 | hsl.l -= amount.value / 100; |
---|
1773 | hsl.l = clamp(hsl.l); |
---|
1774 | return hsla(hsl); |
---|
1775 | }, |
---|
1776 | fadein: function (color, amount) { |
---|
1777 | var hsl = color.toHSL(); |
---|
1778 | |
---|
1779 | hsl.a += amount.value / 100; |
---|
1780 | hsl.a = clamp(hsl.a); |
---|
1781 | return hsla(hsl); |
---|
1782 | }, |
---|
1783 | fadeout: function (color, amount) { |
---|
1784 | var hsl = color.toHSL(); |
---|
1785 | |
---|
1786 | hsl.a -= amount.value / 100; |
---|
1787 | hsl.a = clamp(hsl.a); |
---|
1788 | return hsla(hsl); |
---|
1789 | }, |
---|
1790 | fade: function (color, amount) { |
---|
1791 | var hsl = color.toHSL(); |
---|
1792 | |
---|
1793 | hsl.a = amount.value / 100; |
---|
1794 | hsl.a = clamp(hsl.a); |
---|
1795 | return hsla(hsl); |
---|
1796 | }, |
---|
1797 | spin: function (color, amount) { |
---|
1798 | var hsl = color.toHSL(); |
---|
1799 | var hue = (hsl.h + amount.value) % 360; |
---|
1800 | |
---|
1801 | hsl.h = hue < 0 ? 360 + hue : hue; |
---|
1802 | |
---|
1803 | return hsla(hsl); |
---|
1804 | }, |
---|
1805 | // |
---|
1806 | // Copyright (c) 2006-2009 Hampton Catlin, Nathan Weizenbaum, and Chris Eppstein |
---|
1807 | // http://sass-lang.com |
---|
1808 | // |
---|
1809 | mix: function (color1, color2, weight) { |
---|
1810 | if (!weight) { |
---|
1811 | weight = new(tree.Dimension)(50); |
---|
1812 | } |
---|
1813 | var p = weight.value / 100.0; |
---|
1814 | var w = p * 2 - 1; |
---|
1815 | var a = color1.toHSL().a - color2.toHSL().a; |
---|
1816 | |
---|
1817 | var w1 = (((w * a == -1) ? w : (w + a) / (1 + w * a)) + 1) / 2.0; |
---|
1818 | var w2 = 1 - w1; |
---|
1819 | |
---|
1820 | var rgb = [color1.rgb[0] * w1 + color2.rgb[0] * w2, |
---|
1821 | color1.rgb[1] * w1 + color2.rgb[1] * w2, |
---|
1822 | color1.rgb[2] * w1 + color2.rgb[2] * w2]; |
---|
1823 | |
---|
1824 | var alpha = color1.alpha * p + color2.alpha * (1 - p); |
---|
1825 | |
---|
1826 | return new(tree.Color)(rgb, alpha); |
---|
1827 | }, |
---|
1828 | greyscale: function (color) { |
---|
1829 | return this.desaturate(color, new(tree.Dimension)(100)); |
---|
1830 | }, |
---|
1831 | contrast: function (color, dark, light, threshold) { |
---|
1832 | if (typeof light === 'undefined') { |
---|
1833 | light = this.rgba(255, 255, 255, 1.0); |
---|
1834 | } |
---|
1835 | if (typeof dark === 'undefined') { |
---|
1836 | dark = this.rgba(0, 0, 0, 1.0); |
---|
1837 | } |
---|
1838 | if (typeof threshold === 'undefined') { |
---|
1839 | threshold = 0.43; |
---|
1840 | } else { |
---|
1841 | threshold = threshold.value; |
---|
1842 | } |
---|
1843 | if (((0.2126 * (color.rgb[0]/255) + 0.7152 * (color.rgb[1]/255) + 0.0722 * (color.rgb[2]/255)) * color.alpha) < threshold) { |
---|
1844 | return light; |
---|
1845 | } else { |
---|
1846 | return dark; |
---|
1847 | } |
---|
1848 | }, |
---|
1849 | e: function (str) { |
---|
1850 | return new(tree.Anonymous)(str instanceof tree.JavaScript ? str.evaluated : str); |
---|
1851 | }, |
---|
1852 | escape: function (str) { |
---|
1853 | return new(tree.Anonymous)(encodeURI(str.value).replace(/=/g, "%3D").replace(/:/g, "%3A").replace(/#/g, "%23").replace(/;/g, "%3B").replace(/\(/g, "%28").replace(/\)/g, "%29")); |
---|
1854 | }, |
---|
1855 | '%': function (quoted /* arg, arg, ...*/) { |
---|
1856 | var args = Array.prototype.slice.call(arguments, 1), |
---|
1857 | str = quoted.value; |
---|
1858 | |
---|
1859 | for (var i = 0; i < args.length; i++) { |
---|
1860 | str = str.replace(/%[sda]/i, function(token) { |
---|
1861 | var value = token.match(/s/i) ? args[i].value : args[i].toCSS(); |
---|
1862 | return token.match(/[A-Z]$/) ? encodeURIComponent(value) : value; |
---|
1863 | }); |
---|
1864 | } |
---|
1865 | str = str.replace(/%%/g, '%'); |
---|
1866 | return new(tree.Quoted)('"' + str + '"', str); |
---|
1867 | }, |
---|
1868 | unit: function (val, unit) { |
---|
1869 | return new(tree.Dimension)(val.value, unit ? unit.toCSS() : ""); |
---|
1870 | }, |
---|
1871 | round: function (n, f) { |
---|
1872 | var fraction = typeof(f) === "undefined" ? 0 : f.value; |
---|
1873 | return this._math(function(num) { return num.toFixed(fraction); }, n); |
---|
1874 | }, |
---|
1875 | ceil: function (n) { |
---|
1876 | return this._math(Math.ceil, n); |
---|
1877 | }, |
---|
1878 | floor: function (n) { |
---|
1879 | return this._math(Math.floor, n); |
---|
1880 | }, |
---|
1881 | _math: function (fn, n) { |
---|
1882 | if (n instanceof tree.Dimension) { |
---|
1883 | return new(tree.Dimension)(fn(parseFloat(n.value)), n.unit); |
---|
1884 | } else if (typeof(n) === 'number') { |
---|
1885 | return fn(n); |
---|
1886 | } else { |
---|
1887 | throw { type: "Argument", message: "argument must be a number" }; |
---|
1888 | } |
---|
1889 | }, |
---|
1890 | argb: function (color) { |
---|
1891 | return new(tree.Anonymous)(color.toARGB()); |
---|
1892 | |
---|
1893 | }, |
---|
1894 | percentage: function (n) { |
---|
1895 | return new(tree.Dimension)(n.value * 100, '%'); |
---|
1896 | }, |
---|
1897 | color: function (n) { |
---|
1898 | if (n instanceof tree.Quoted) { |
---|
1899 | return new(tree.Color)(n.value.slice(1)); |
---|
1900 | } else { |
---|
1901 | throw { type: "Argument", message: "argument must be a string" }; |
---|
1902 | } |
---|
1903 | }, |
---|
1904 | iscolor: function (n) { |
---|
1905 | return this._isa(n, tree.Color); |
---|
1906 | }, |
---|
1907 | isnumber: function (n) { |
---|
1908 | return this._isa(n, tree.Dimension); |
---|
1909 | }, |
---|
1910 | isstring: function (n) { |
---|
1911 | return this._isa(n, tree.Quoted); |
---|
1912 | }, |
---|
1913 | iskeyword: function (n) { |
---|
1914 | return this._isa(n, tree.Keyword); |
---|
1915 | }, |
---|
1916 | isurl: function (n) { |
---|
1917 | return this._isa(n, tree.URL); |
---|
1918 | }, |
---|
1919 | ispixel: function (n) { |
---|
1920 | return (n instanceof tree.Dimension) && n.unit === 'px' ? tree.True : tree.False; |
---|
1921 | }, |
---|
1922 | ispercentage: function (n) { |
---|
1923 | return (n instanceof tree.Dimension) && n.unit === '%' ? tree.True : tree.False; |
---|
1924 | }, |
---|
1925 | isem: function (n) { |
---|
1926 | return (n instanceof tree.Dimension) && n.unit === 'em' ? tree.True : tree.False; |
---|
1927 | }, |
---|
1928 | _isa: function (n, Type) { |
---|
1929 | return (n instanceof Type) ? tree.True : tree.False; |
---|
1930 | }, |
---|
1931 | |
---|
1932 | /* Blending modes */ |
---|
1933 | |
---|
1934 | multiply: function(color1, color2) { |
---|
1935 | var r = color1.rgb[0] * color2.rgb[0] / 255; |
---|
1936 | var g = color1.rgb[1] * color2.rgb[1] / 255; |
---|
1937 | var b = color1.rgb[2] * color2.rgb[2] / 255; |
---|
1938 | return this.rgb(r, g, b); |
---|
1939 | }, |
---|
1940 | screen: function(color1, color2) { |
---|
1941 | var r = 255 - (255 - color1.rgb[0]) * (255 - color2.rgb[0]) / 255; |
---|
1942 | var g = 255 - (255 - color1.rgb[1]) * (255 - color2.rgb[1]) / 255; |
---|
1943 | var b = 255 - (255 - color1.rgb[2]) * (255 - color2.rgb[2]) / 255; |
---|
1944 | return this.rgb(r, g, b); |
---|
1945 | }, |
---|
1946 | overlay: function(color1, color2) { |
---|
1947 | var r = color1.rgb[0] < 128 ? 2 * color1.rgb[0] * color2.rgb[0] / 255 : 255 - 2 * (255 - color1.rgb[0]) * (255 - color2.rgb[0]) / 255; |
---|
1948 | var g = color1.rgb[1] < 128 ? 2 * color1.rgb[1] * color2.rgb[1] / 255 : 255 - 2 * (255 - color1.rgb[1]) * (255 - color2.rgb[1]) / 255; |
---|
1949 | var b = color1.rgb[2] < 128 ? 2 * color1.rgb[2] * color2.rgb[2] / 255 : 255 - 2 * (255 - color1.rgb[2]) * (255 - color2.rgb[2]) / 255; |
---|
1950 | return this.rgb(r, g, b); |
---|
1951 | }, |
---|
1952 | softlight: function(color1, color2) { |
---|
1953 | var t = color2.rgb[0] * color1.rgb[0] / 255; |
---|
1954 | var r = t + color1.rgb[0] * (255 - (255 - color1.rgb[0]) * (255 - color2.rgb[0]) / 255 - t) / 255; |
---|
1955 | t = color2.rgb[1] * color1.rgb[1] / 255; |
---|
1956 | var g = t + color1.rgb[1] * (255 - (255 - color1.rgb[1]) * (255 - color2.rgb[1]) / 255 - t) / 255; |
---|
1957 | t = color2.rgb[2] * color1.rgb[2] / 255; |
---|
1958 | var b = t + color1.rgb[2] * (255 - (255 - color1.rgb[2]) * (255 - color2.rgb[2]) / 255 - t) / 255; |
---|
1959 | return this.rgb(r, g, b); |
---|
1960 | }, |
---|
1961 | hardlight: function(color1, color2) { |
---|
1962 | var r = color2.rgb[0] < 128 ? 2 * color2.rgb[0] * color1.rgb[0] / 255 : 255 - 2 * (255 - color2.rgb[0]) * (255 - color1.rgb[0]) / 255; |
---|
1963 | var g = color2.rgb[1] < 128 ? 2 * color2.rgb[1] * color1.rgb[1] / 255 : 255 - 2 * (255 - color2.rgb[1]) * (255 - color1.rgb[1]) / 255; |
---|
1964 | var b = color2.rgb[2] < 128 ? 2 * color2.rgb[2] * color1.rgb[2] / 255 : 255 - 2 * (255 - color2.rgb[2]) * (255 - color1.rgb[2]) / 255; |
---|
1965 | return this.rgb(r, g, b); |
---|
1966 | }, |
---|
1967 | difference: function(color1, color2) { |
---|
1968 | var r = Math.abs(color1.rgb[0] - color2.rgb[0]); |
---|
1969 | var g = Math.abs(color1.rgb[1] - color2.rgb[1]); |
---|
1970 | var b = Math.abs(color1.rgb[2] - color2.rgb[2]); |
---|
1971 | return this.rgb(r, g, b); |
---|
1972 | }, |
---|
1973 | exclusion: function(color1, color2) { |
---|
1974 | var r = color1.rgb[0] + color2.rgb[0] * (255 - color1.rgb[0] - color1.rgb[0]) / 255; |
---|
1975 | var g = color1.rgb[1] + color2.rgb[1] * (255 - color1.rgb[1] - color1.rgb[1]) / 255; |
---|
1976 | var b = color1.rgb[2] + color2.rgb[2] * (255 - color1.rgb[2] - color1.rgb[2]) / 255; |
---|
1977 | return this.rgb(r, g, b); |
---|
1978 | }, |
---|
1979 | average: function(color1, color2) { |
---|
1980 | var r = (color1.rgb[0] + color2.rgb[0]) / 2; |
---|
1981 | var g = (color1.rgb[1] + color2.rgb[1]) / 2; |
---|
1982 | var b = (color1.rgb[2] + color2.rgb[2]) / 2; |
---|
1983 | return this.rgb(r, g, b); |
---|
1984 | }, |
---|
1985 | negation: function(color1, color2) { |
---|
1986 | var r = 255 - Math.abs(255 - color2.rgb[0] - color1.rgb[0]); |
---|
1987 | var g = 255 - Math.abs(255 - color2.rgb[1] - color1.rgb[1]); |
---|
1988 | var b = 255 - Math.abs(255 - color2.rgb[2] - color1.rgb[2]); |
---|
1989 | return this.rgb(r, g, b); |
---|
1990 | }, |
---|
1991 | tint: function(color, amount) { |
---|
1992 | return this.mix(this.rgb(255,255,255), color, amount); |
---|
1993 | }, |
---|
1994 | shade: function(color, amount) { |
---|
1995 | return this.mix(this.rgb(0, 0, 0), color, amount); |
---|
1996 | } |
---|
1997 | }; |
---|
1998 | |
---|
1999 | function hsla(color) { |
---|
2000 | return tree.functions.hsla(color.h, color.s, color.l, color.a); |
---|
2001 | } |
---|
2002 | |
---|
2003 | function scaled(n, size) { |
---|
2004 | if (n instanceof tree.Dimension && n.unit == '%') { |
---|
2005 | return parseFloat(n.value * size / 100); |
---|
2006 | } else { |
---|
2007 | return number(n); |
---|
2008 | } |
---|
2009 | } |
---|
2010 | |
---|
2011 | function number(n) { |
---|
2012 | if (n instanceof tree.Dimension) { |
---|
2013 | return parseFloat(n.unit == '%' ? n.value / 100 : n.value); |
---|
2014 | } else if (typeof(n) === 'number') { |
---|
2015 | return n; |
---|
2016 | } else { |
---|
2017 | throw { |
---|
2018 | error: "RuntimeError", |
---|
2019 | message: "color functions take numbers as parameters" |
---|
2020 | }; |
---|
2021 | } |
---|
2022 | } |
---|
2023 | |
---|
2024 | function clamp(val) { |
---|
2025 | return Math.min(1, Math.max(0, val)); |
---|
2026 | } |
---|
2027 | |
---|
2028 | })(require('./tree')); |
---|
2029 | (function (tree) { |
---|
2030 | tree.colors = { |
---|
2031 | 'aliceblue':'#f0f8ff', |
---|
2032 | 'antiquewhite':'#faebd7', |
---|
2033 | 'aqua':'#00ffff', |
---|
2034 | 'aquamarine':'#7fffd4', |
---|
2035 | 'azure':'#f0ffff', |
---|
2036 | 'beige':'#f5f5dc', |
---|
2037 | 'bisque':'#ffe4c4', |
---|
2038 | 'black':'#000000', |
---|
2039 | 'blanchedalmond':'#ffebcd', |
---|
2040 | 'blue':'#0000ff', |
---|
2041 | 'blueviolet':'#8a2be2', |
---|
2042 | 'brown':'#a52a2a', |
---|
2043 | 'burlywood':'#deb887', |
---|
2044 | 'cadetblue':'#5f9ea0', |
---|
2045 | 'chartreuse':'#7fff00', |
---|
2046 | 'chocolate':'#d2691e', |
---|
2047 | 'coral':'#ff7f50', |
---|
2048 | 'cornflowerblue':'#6495ed', |
---|
2049 | 'cornsilk':'#fff8dc', |
---|
2050 | 'crimson':'#dc143c', |
---|
2051 | 'cyan':'#00ffff', |
---|
2052 | 'darkblue':'#00008b', |
---|
2053 | 'darkcyan':'#008b8b', |
---|
2054 | 'darkgoldenrod':'#b8860b', |
---|
2055 | 'darkgray':'#a9a9a9', |
---|
2056 | 'darkgrey':'#a9a9a9', |
---|
2057 | 'darkgreen':'#006400', |
---|
2058 | 'darkkhaki':'#bdb76b', |
---|
2059 | 'darkmagenta':'#8b008b', |
---|
2060 | 'darkolivegreen':'#556b2f', |
---|
2061 | 'darkorange':'#ff8c00', |
---|
2062 | 'darkorchid':'#9932cc', |
---|
2063 | 'darkred':'#8b0000', |
---|
2064 | 'darksalmon':'#e9967a', |
---|
2065 | 'darkseagreen':'#8fbc8f', |
---|
2066 | 'darkslateblue':'#483d8b', |
---|
2067 | 'darkslategray':'#2f4f4f', |
---|
2068 | 'darkslategrey':'#2f4f4f', |
---|
2069 | 'darkturquoise':'#00ced1', |
---|
2070 | 'darkviolet':'#9400d3', |
---|
2071 | 'deeppink':'#ff1493', |
---|
2072 | 'deepskyblue':'#00bfff', |
---|
2073 | 'dimgray':'#696969', |
---|
2074 | 'dimgrey':'#696969', |
---|
2075 | 'dodgerblue':'#1e90ff', |
---|
2076 | 'firebrick':'#b22222', |
---|
2077 | 'floralwhite':'#fffaf0', |
---|
2078 | 'forestgreen':'#228b22', |
---|
2079 | 'fuchsia':'#ff00ff', |
---|
2080 | 'gainsboro':'#dcdcdc', |
---|
2081 | 'ghostwhite':'#f8f8ff', |
---|
2082 | 'gold':'#ffd700', |
---|
2083 | 'goldenrod':'#daa520', |
---|
2084 | 'gray':'#808080', |
---|
2085 | 'grey':'#808080', |
---|
2086 | 'green':'#008000', |
---|
2087 | 'greenyellow':'#adff2f', |
---|
2088 | 'honeydew':'#f0fff0', |
---|
2089 | 'hotpink':'#ff69b4', |
---|
2090 | 'indianred':'#cd5c5c', |
---|
2091 | 'indigo':'#4b0082', |
---|
2092 | 'ivory':'#fffff0', |
---|
2093 | 'khaki':'#f0e68c', |
---|
2094 | 'lavender':'#e6e6fa', |
---|
2095 | 'lavenderblush':'#fff0f5', |
---|
2096 | 'lawngreen':'#7cfc00', |
---|
2097 | 'lemonchiffon':'#fffacd', |
---|
2098 | 'lightblue':'#add8e6', |
---|
2099 | 'lightcoral':'#f08080', |
---|
2100 | 'lightcyan':'#e0ffff', |
---|
2101 | 'lightgoldenrodyellow':'#fafad2', |
---|
2102 | 'lightgray':'#d3d3d3', |
---|
2103 | 'lightgrey':'#d3d3d3', |
---|
2104 | 'lightgreen':'#90ee90', |
---|
2105 | 'lightpink':'#ffb6c1', |
---|
2106 | 'lightsalmon':'#ffa07a', |
---|
2107 | 'lightseagreen':'#20b2aa', |
---|
2108 | 'lightskyblue':'#87cefa', |
---|
2109 | 'lightslategray':'#778899', |
---|
2110 | 'lightslategrey':'#778899', |
---|
2111 | 'lightsteelblue':'#b0c4de', |
---|
2112 | 'lightyellow':'#ffffe0', |
---|
2113 | 'lime':'#00ff00', |
---|
2114 | 'limegreen':'#32cd32', |
---|
2115 | 'linen':'#faf0e6', |
---|
2116 | 'magenta':'#ff00ff', |
---|
2117 | 'maroon':'#800000', |
---|
2118 | 'mediumaquamarine':'#66cdaa', |
---|
2119 | 'mediumblue':'#0000cd', |
---|
2120 | 'mediumorchid':'#ba55d3', |
---|
2121 | 'mediumpurple':'#9370d8', |
---|
2122 | 'mediumseagreen':'#3cb371', |
---|
2123 | 'mediumslateblue':'#7b68ee', |
---|
2124 | 'mediumspringgreen':'#00fa9a', |
---|
2125 | 'mediumturquoise':'#48d1cc', |
---|
2126 | 'mediumvioletred':'#c71585', |
---|
2127 | 'midnightblue':'#191970', |
---|
2128 | 'mintcream':'#f5fffa', |
---|
2129 | 'mistyrose':'#ffe4e1', |
---|
2130 | 'moccasin':'#ffe4b5', |
---|
2131 | 'navajowhite':'#ffdead', |
---|
2132 | 'navy':'#000080', |
---|
2133 | 'oldlace':'#fdf5e6', |
---|
2134 | 'olive':'#808000', |
---|
2135 | 'olivedrab':'#6b8e23', |
---|
2136 | 'orange':'#ffa500', |
---|
2137 | 'orangered':'#ff4500', |
---|
2138 | 'orchid':'#da70d6', |
---|
2139 | 'palegoldenrod':'#eee8aa', |
---|
2140 | 'palegreen':'#98fb98', |
---|
2141 | 'paleturquoise':'#afeeee', |
---|
2142 | 'palevioletred':'#d87093', |
---|
2143 | 'papayawhip':'#ffefd5', |
---|
2144 | 'peachpuff':'#ffdab9', |
---|
2145 | 'peru':'#cd853f', |
---|
2146 | 'pink':'#ffc0cb', |
---|
2147 | 'plum':'#dda0dd', |
---|
2148 | 'powderblue':'#b0e0e6', |
---|
2149 | 'purple':'#800080', |
---|
2150 | 'red':'#ff0000', |
---|
2151 | 'rosybrown':'#bc8f8f', |
---|
2152 | 'royalblue':'#4169e1', |
---|
2153 | 'saddlebrown':'#8b4513', |
---|
2154 | 'salmon':'#fa8072', |
---|
2155 | 'sandybrown':'#f4a460', |
---|
2156 | 'seagreen':'#2e8b57', |
---|
2157 | 'seashell':'#fff5ee', |
---|
2158 | 'sienna':'#a0522d', |
---|
2159 | 'silver':'#c0c0c0', |
---|
2160 | 'skyblue':'#87ceeb', |
---|
2161 | 'slateblue':'#6a5acd', |
---|
2162 | 'slategray':'#708090', |
---|
2163 | 'slategrey':'#708090', |
---|
2164 | 'snow':'#fffafa', |
---|
2165 | 'springgreen':'#00ff7f', |
---|
2166 | 'steelblue':'#4682b4', |
---|
2167 | 'tan':'#d2b48c', |
---|
2168 | 'teal':'#008080', |
---|
2169 | 'thistle':'#d8bfd8', |
---|
2170 | 'tomato':'#ff6347', |
---|
2171 | // 'transparent':'rgba(0,0,0,0)', |
---|
2172 | 'turquoise':'#40e0d0', |
---|
2173 | 'violet':'#ee82ee', |
---|
2174 | 'wheat':'#f5deb3', |
---|
2175 | 'white':'#ffffff', |
---|
2176 | 'whitesmoke':'#f5f5f5', |
---|
2177 | 'yellow':'#ffff00', |
---|
2178 | 'yellowgreen':'#9acd32' |
---|
2179 | }; |
---|
2180 | })(require('./tree')); |
---|
2181 | (function (tree) { |
---|
2182 | |
---|
2183 | tree.Alpha = function (val) { |
---|
2184 | this.value = val; |
---|
2185 | }; |
---|
2186 | tree.Alpha.prototype = { |
---|
2187 | toCSS: function () { |
---|
2188 | return "alpha(opacity=" + |
---|
2189 | (this.value.toCSS ? this.value.toCSS() : this.value) + ")"; |
---|
2190 | }, |
---|
2191 | eval: function (env) { |
---|
2192 | if (this.value.eval) { this.value = this.value.eval(env) } |
---|
2193 | return this; |
---|
2194 | } |
---|
2195 | }; |
---|
2196 | |
---|
2197 | })(require('../tree')); |
---|
2198 | (function (tree) { |
---|
2199 | |
---|
2200 | tree.Anonymous = function (string) { |
---|
2201 | this.value = string.value || string; |
---|
2202 | }; |
---|
2203 | tree.Anonymous.prototype = { |
---|
2204 | toCSS: function () { |
---|
2205 | return this.value; |
---|
2206 | }, |
---|
2207 | eval: function () { return this }, |
---|
2208 | compare: function (x) { |
---|
2209 | if (!x.toCSS) { |
---|
2210 | return -1; |
---|
2211 | } |
---|
2212 | |
---|
2213 | var left = this.toCSS(), |
---|
2214 | right = x.toCSS(); |
---|
2215 | |
---|
2216 | if (left === right) { |
---|
2217 | return 0; |
---|
2218 | } |
---|
2219 | |
---|
2220 | return left < right ? -1 : 1; |
---|
2221 | } |
---|
2222 | }; |
---|
2223 | |
---|
2224 | })(require('../tree')); |
---|
2225 | (function (tree) { |
---|
2226 | |
---|
2227 | tree.Assignment = function (key, val) { |
---|
2228 | this.key = key; |
---|
2229 | this.value = val; |
---|
2230 | }; |
---|
2231 | tree.Assignment.prototype = { |
---|
2232 | toCSS: function () { |
---|
2233 | return this.key + '=' + (this.value.toCSS ? this.value.toCSS() : this.value); |
---|
2234 | }, |
---|
2235 | eval: function (env) { |
---|
2236 | if (this.value.eval) { |
---|
2237 | return new(tree.Assignment)(this.key, this.value.eval(env)); |
---|
2238 | } |
---|
2239 | return this; |
---|
2240 | } |
---|
2241 | }; |
---|
2242 | |
---|
2243 | })(require('../tree'));(function (tree) { |
---|
2244 | |
---|
2245 | // |
---|
2246 | // A function call node. |
---|
2247 | // |
---|
2248 | tree.Call = function (name, args, index, filename) { |
---|
2249 | this.name = name; |
---|
2250 | this.args = args; |
---|
2251 | this.index = index; |
---|
2252 | this.filename = filename; |
---|
2253 | }; |
---|
2254 | tree.Call.prototype = { |
---|
2255 | // |
---|
2256 | // When evaluating a function call, |
---|
2257 | // we either find the function in `tree.functions` [1], |
---|
2258 | // in which case we call it, passing the evaluated arguments, |
---|
2259 | // or we simply print it out as it appeared originally [2]. |
---|
2260 | // |
---|
2261 | // The *functions.js* file contains the built-in functions. |
---|
2262 | // |
---|
2263 | // The reason why we evaluate the arguments, is in the case where |
---|
2264 | // we try to pass a variable to a function, like: `saturate(@color)`. |
---|
2265 | // The function should receive the value, not the variable. |
---|
2266 | // |
---|
2267 | eval: function (env) { |
---|
2268 | var args = this.args.map(function (a) { return a.eval(env) }); |
---|
2269 | |
---|
2270 | if (this.name in tree.functions) { // 1. |
---|
2271 | try { |
---|
2272 | return tree.functions[this.name].apply(tree.functions, args); |
---|
2273 | } catch (e) { |
---|
2274 | throw { type: e.type || "Runtime", |
---|
2275 | message: "error evaluating function `" + this.name + "`" + |
---|
2276 | (e.message ? ': ' + e.message : ''), |
---|
2277 | index: this.index, filename: this.filename }; |
---|
2278 | } |
---|
2279 | } else { // 2. |
---|
2280 | return new(tree.Anonymous)(this.name + |
---|
2281 | "(" + args.map(function (a) { return a.toCSS(env) }).join(', ') + ")"); |
---|
2282 | } |
---|
2283 | }, |
---|
2284 | |
---|
2285 | toCSS: function (env) { |
---|
2286 | return this.eval(env).toCSS(); |
---|
2287 | } |
---|
2288 | }; |
---|
2289 | |
---|
2290 | })(require('../tree')); |
---|
2291 | (function (tree) { |
---|
2292 | // |
---|
2293 | // RGB Colors - #ff0014, #eee |
---|
2294 | // |
---|
2295 | tree.Color = function (rgb, a) { |
---|
2296 | // |
---|
2297 | // The end goal here, is to parse the arguments |
---|
2298 | // into an integer triplet, such as `128, 255, 0` |
---|
2299 | // |
---|
2300 | // This facilitates operations and conversions. |
---|
2301 | // |
---|
2302 | if (Array.isArray(rgb)) { |
---|
2303 | this.rgb = rgb; |
---|
2304 | } else if (rgb.length == 6) { |
---|
2305 | this.rgb = rgb.match(/.{2}/g).map(function (c) { |
---|
2306 | return parseInt(c, 16); |
---|
2307 | }); |
---|
2308 | } else { |
---|
2309 | this.rgb = rgb.split('').map(function (c) { |
---|
2310 | return parseInt(c + c, 16); |
---|
2311 | }); |
---|
2312 | } |
---|
2313 | this.alpha = typeof(a) === 'number' ? a : 1; |
---|
2314 | }; |
---|
2315 | tree.Color.prototype = { |
---|
2316 | eval: function () { return this }, |
---|
2317 | |
---|
2318 | // |
---|
2319 | // If we have some transparency, the only way to represent it |
---|
2320 | // is via `rgba`. Otherwise, we use the hex representation, |
---|
2321 | // which has better compatibility with older browsers. |
---|
2322 | // Values are capped between `0` and `255`, rounded and zero-padded. |
---|
2323 | // |
---|
2324 | toCSS: function () { |
---|
2325 | if (this.alpha < 1.0) { |
---|
2326 | return "rgba(" + this.rgb.map(function (c) { |
---|
2327 | return Math.round(c); |
---|
2328 | }).concat(this.alpha).join(', ') + ")"; |
---|
2329 | } else { |
---|
2330 | return '#' + this.rgb.map(function (i) { |
---|
2331 | i = Math.round(i); |
---|
2332 | i = (i > 255 ? 255 : (i < 0 ? 0 : i)).toString(16); |
---|
2333 | return i.length === 1 ? '0' + i : i; |
---|
2334 | }).join(''); |
---|
2335 | } |
---|
2336 | }, |
---|
2337 | |
---|
2338 | // |
---|
2339 | // Operations have to be done per-channel, if not, |
---|
2340 | // channels will spill onto each other. Once we have |
---|
2341 | // our result, in the form of an integer triplet, |
---|
2342 | // we create a new Color node to hold the result. |
---|
2343 | // |
---|
2344 | operate: function (op, other) { |
---|
2345 | var result = []; |
---|
2346 | |
---|
2347 | if (! (other instanceof tree.Color)) { |
---|
2348 | other = other.toColor(); |
---|
2349 | } |
---|
2350 | |
---|
2351 | for (var c = 0; c < 3; c++) { |
---|
2352 | result[c] = tree.operate(op, this.rgb[c], other.rgb[c]); |
---|
2353 | } |
---|
2354 | return new(tree.Color)(result, this.alpha + other.alpha); |
---|
2355 | }, |
---|
2356 | |
---|
2357 | toHSL: function () { |
---|
2358 | var r = this.rgb[0] / 255, |
---|
2359 | g = this.rgb[1] / 255, |
---|
2360 | b = this.rgb[2] / 255, |
---|
2361 | a = this.alpha; |
---|
2362 | |
---|
2363 | var max = Math.max(r, g, b), min = Math.min(r, g, b); |
---|
2364 | var h, s, l = (max + min) / 2, d = max - min; |
---|
2365 | |
---|
2366 | if (max === min) { |
---|
2367 | h = s = 0; |
---|
2368 | } else { |
---|
2369 | s = l > 0.5 ? d / (2 - max - min) : d / (max + min); |
---|
2370 | |
---|
2371 | switch (max) { |
---|
2372 | case r: h = (g - b) / d + (g < b ? 6 : 0); break; |
---|
2373 | case g: h = (b - r) / d + 2; break; |
---|
2374 | case b: h = (r - g) / d + 4; break; |
---|
2375 | } |
---|
2376 | h /= 6; |
---|
2377 | } |
---|
2378 | return { h: h * 360, s: s, l: l, a: a }; |
---|
2379 | }, |
---|
2380 | toARGB: function () { |
---|
2381 | var argb = [Math.round(this.alpha * 255)].concat(this.rgb); |
---|
2382 | return '#' + argb.map(function (i) { |
---|
2383 | i = Math.round(i); |
---|
2384 | i = (i > 255 ? 255 : (i < 0 ? 0 : i)).toString(16); |
---|
2385 | return i.length === 1 ? '0' + i : i; |
---|
2386 | }).join(''); |
---|
2387 | }, |
---|
2388 | compare: function (x) { |
---|
2389 | if (!x.rgb) { |
---|
2390 | return -1; |
---|
2391 | } |
---|
2392 | |
---|
2393 | return (x.rgb[0] === this.rgb[0] && |
---|
2394 | x.rgb[1] === this.rgb[1] && |
---|
2395 | x.rgb[2] === this.rgb[2] && |
---|
2396 | x.alpha === this.alpha) ? 0 : -1; |
---|
2397 | } |
---|
2398 | }; |
---|
2399 | |
---|
2400 | |
---|
2401 | })(require('../tree')); |
---|
2402 | (function (tree) { |
---|
2403 | |
---|
2404 | tree.Comment = function (value, silent) { |
---|
2405 | this.value = value; |
---|
2406 | this.silent = !!silent; |
---|
2407 | }; |
---|
2408 | tree.Comment.prototype = { |
---|
2409 | toCSS: function (env) { |
---|
2410 | return env.compress ? '' : this.value; |
---|
2411 | }, |
---|
2412 | eval: function () { return this } |
---|
2413 | }; |
---|
2414 | |
---|
2415 | })(require('../tree')); |
---|
2416 | (function (tree) { |
---|
2417 | |
---|
2418 | tree.Condition = function (op, l, r, i, negate) { |
---|
2419 | this.op = op.trim(); |
---|
2420 | this.lvalue = l; |
---|
2421 | this.rvalue = r; |
---|
2422 | this.index = i; |
---|
2423 | this.negate = negate; |
---|
2424 | }; |
---|
2425 | tree.Condition.prototype.eval = function (env) { |
---|
2426 | var a = this.lvalue.eval(env), |
---|
2427 | b = this.rvalue.eval(env); |
---|
2428 | |
---|
2429 | var i = this.index, result; |
---|
2430 | |
---|
2431 | var result = (function (op) { |
---|
2432 | switch (op) { |
---|
2433 | case 'and': |
---|
2434 | return a && b; |
---|
2435 | case 'or': |
---|
2436 | return a || b; |
---|
2437 | default: |
---|
2438 | if (a.compare) { |
---|
2439 | result = a.compare(b); |
---|
2440 | } else if (b.compare) { |
---|
2441 | result = b.compare(a); |
---|
2442 | } else { |
---|
2443 | throw { type: "Type", |
---|
2444 | message: "Unable to perform comparison", |
---|
2445 | index: i }; |
---|
2446 | } |
---|
2447 | switch (result) { |
---|
2448 | case -1: return op === '<' || op === '=<'; |
---|
2449 | case 0: return op === '=' || op === '>=' || op === '=<'; |
---|
2450 | case 1: return op === '>' || op === '>='; |
---|
2451 | } |
---|
2452 | } |
---|
2453 | })(this.op); |
---|
2454 | return this.negate ? !result : result; |
---|
2455 | }; |
---|
2456 | |
---|
2457 | })(require('../tree')); |
---|
2458 | (function (tree) { |
---|
2459 | |
---|
2460 | // |
---|
2461 | // A number with a unit |
---|
2462 | // |
---|
2463 | tree.Dimension = function (value, unit) { |
---|
2464 | this.value = parseFloat(value); |
---|
2465 | this.unit = unit || null; |
---|
2466 | }; |
---|
2467 | |
---|
2468 | tree.Dimension.prototype = { |
---|
2469 | eval: function () { return this }, |
---|
2470 | toColor: function () { |
---|
2471 | return new(tree.Color)([this.value, this.value, this.value]); |
---|
2472 | }, |
---|
2473 | toCSS: function () { |
---|
2474 | var css = this.value + this.unit; |
---|
2475 | return css; |
---|
2476 | }, |
---|
2477 | |
---|
2478 | // In an operation between two Dimensions, |
---|
2479 | // we default to the first Dimension's unit, |
---|
2480 | // so `1px + 2em` will yield `3px`. |
---|
2481 | // In the future, we could implement some unit |
---|
2482 | // conversions such that `100cm + 10mm` would yield |
---|
2483 | // `101cm`. |
---|
2484 | operate: function (op, other) { |
---|
2485 | return new(tree.Dimension) |
---|
2486 | (tree.operate(op, this.value, other.value), |
---|
2487 | this.unit || other.unit); |
---|
2488 | }, |
---|
2489 | |
---|
2490 | compare: function (other) { |
---|
2491 | if (other instanceof tree.Dimension) { |
---|
2492 | if (other.value > this.value) { |
---|
2493 | return -1; |
---|
2494 | } else if (other.value < this.value) { |
---|
2495 | return 1; |
---|
2496 | } else { |
---|
2497 | if (other.unit && this.unit !== other.unit) { |
---|
2498 | return -1; |
---|
2499 | } |
---|
2500 | return 0; |
---|
2501 | } |
---|
2502 | } else { |
---|
2503 | return -1; |
---|
2504 | } |
---|
2505 | } |
---|
2506 | }; |
---|
2507 | |
---|
2508 | })(require('../tree')); |
---|
2509 | (function (tree) { |
---|
2510 | |
---|
2511 | tree.Directive = function (name, value) { |
---|
2512 | this.name = name; |
---|
2513 | |
---|
2514 | if (Array.isArray(value)) { |
---|
2515 | this.ruleset = new(tree.Ruleset)([], value); |
---|
2516 | this.ruleset.allowImports = true; |
---|
2517 | } else { |
---|
2518 | this.value = value; |
---|
2519 | } |
---|
2520 | }; |
---|
2521 | tree.Directive.prototype = { |
---|
2522 | toCSS: function (ctx, env) { |
---|
2523 | if (this.ruleset) { |
---|
2524 | this.ruleset.root = true; |
---|
2525 | return this.name + (env.compress ? '{' : ' {\n ') + |
---|
2526 | this.ruleset.toCSS(ctx, env).trim().replace(/\n/g, '\n ') + |
---|
2527 | (env.compress ? '}': '\n}\n'); |
---|
2528 | } else { |
---|
2529 | return this.name + ' ' + this.value.toCSS() + ';\n'; |
---|
2530 | } |
---|
2531 | }, |
---|
2532 | eval: function (env) { |
---|
2533 | var evaldDirective = this; |
---|
2534 | if (this.ruleset) { |
---|
2535 | env.frames.unshift(this); |
---|
2536 | evaldDirective = new(tree.Directive)(this.name); |
---|
2537 | evaldDirective.ruleset = this.ruleset.eval(env); |
---|
2538 | env.frames.shift(); |
---|
2539 | } |
---|
2540 | return evaldDirective; |
---|
2541 | }, |
---|
2542 | variable: function (name) { return tree.Ruleset.prototype.variable.call(this.ruleset, name) }, |
---|
2543 | find: function () { return tree.Ruleset.prototype.find.apply(this.ruleset, arguments) }, |
---|
2544 | rulesets: function () { return tree.Ruleset.prototype.rulesets.apply(this.ruleset) } |
---|
2545 | }; |
---|
2546 | |
---|
2547 | })(require('../tree')); |
---|
2548 | (function (tree) { |
---|
2549 | |
---|
2550 | tree.Element = function (combinator, value, index) { |
---|
2551 | this.combinator = combinator instanceof tree.Combinator ? |
---|
2552 | combinator : new(tree.Combinator)(combinator); |
---|
2553 | |
---|
2554 | if (typeof(value) === 'string') { |
---|
2555 | this.value = value.trim(); |
---|
2556 | } else if (value) { |
---|
2557 | this.value = value; |
---|
2558 | } else { |
---|
2559 | this.value = ""; |
---|
2560 | } |
---|
2561 | this.index = index; |
---|
2562 | }; |
---|
2563 | tree.Element.prototype.eval = function (env) { |
---|
2564 | return new(tree.Element)(this.combinator, |
---|
2565 | this.value.eval ? this.value.eval(env) : this.value, |
---|
2566 | this.index); |
---|
2567 | }; |
---|
2568 | tree.Element.prototype.toCSS = function (env) { |
---|
2569 | var value = (this.value.toCSS ? this.value.toCSS(env) : this.value); |
---|
2570 | if (value == '' && this.combinator.value.charAt(0) == '&') { |
---|
2571 | return ''; |
---|
2572 | } else { |
---|
2573 | return this.combinator.toCSS(env || {}) + value; |
---|
2574 | } |
---|
2575 | }; |
---|
2576 | |
---|
2577 | tree.Combinator = function (value) { |
---|
2578 | if (value === ' ') { |
---|
2579 | this.value = ' '; |
---|
2580 | } else { |
---|
2581 | this.value = value ? value.trim() : ""; |
---|
2582 | } |
---|
2583 | }; |
---|
2584 | tree.Combinator.prototype.toCSS = function (env) { |
---|
2585 | return { |
---|
2586 | '' : '', |
---|
2587 | ' ' : ' ', |
---|
2588 | ':' : ' :', |
---|
2589 | '+' : env.compress ? '+' : ' + ', |
---|
2590 | '~' : env.compress ? '~' : ' ~ ', |
---|
2591 | '>' : env.compress ? '>' : ' > ', |
---|
2592 | '|' : env.compress ? '|' : ' | ' |
---|
2593 | }[this.value]; |
---|
2594 | }; |
---|
2595 | |
---|
2596 | })(require('../tree')); |
---|
2597 | (function (tree) { |
---|
2598 | |
---|
2599 | tree.Expression = function (value) { this.value = value }; |
---|
2600 | tree.Expression.prototype = { |
---|
2601 | eval: function (env) { |
---|
2602 | if (this.value.length > 1) { |
---|
2603 | return new(tree.Expression)(this.value.map(function (e) { |
---|
2604 | return e.eval(env); |
---|
2605 | })); |
---|
2606 | } else if (this.value.length === 1) { |
---|
2607 | return this.value[0].eval(env); |
---|
2608 | } else { |
---|
2609 | return this; |
---|
2610 | } |
---|
2611 | }, |
---|
2612 | toCSS: function (env) { |
---|
2613 | return this.value.map(function (e) { |
---|
2614 | return e.toCSS ? e.toCSS(env) : ''; |
---|
2615 | }).join(' '); |
---|
2616 | } |
---|
2617 | }; |
---|
2618 | |
---|
2619 | })(require('../tree')); |
---|
2620 | (function (tree) { |
---|
2621 | // |
---|
2622 | // CSS @import node |
---|
2623 | // |
---|
2624 | // The general strategy here is that we don't want to wait |
---|
2625 | // for the parsing to be completed, before we start importing |
---|
2626 | // the file. That's because in the context of a browser, |
---|
2627 | // most of the time will be spent waiting for the server to respond. |
---|
2628 | // |
---|
2629 | // On creation, we push the import path to our import queue, though |
---|
2630 | // `import,push`, we also pass it a callback, which it'll call once |
---|
2631 | // the file has been fetched, and parsed. |
---|
2632 | // |
---|
2633 | tree.Import = function (path, imports, features, once, index, rootpath) { |
---|
2634 | var that = this; |
---|
2635 | |
---|
2636 | this.once = once; |
---|
2637 | this.index = index; |
---|
2638 | this._path = path; |
---|
2639 | this.features = features && new(tree.Value)(features); |
---|
2640 | this.rootpath = rootpath; |
---|
2641 | |
---|
2642 | // The '.less' extension is optional |
---|
2643 | if (path instanceof tree.Quoted) { |
---|
2644 | this.path = /(\.[a-z]*$)|([\?;].*)$/.test(path.value) ? path.value : path.value + '.less'; |
---|
2645 | } else { |
---|
2646 | this.path = path.value.value || path.value; |
---|
2647 | } |
---|
2648 | |
---|
2649 | this.css = /css([\?;].*)?$/.test(this.path); |
---|
2650 | |
---|
2651 | // Only pre-compile .less files |
---|
2652 | if (! this.css) { |
---|
2653 | imports.push(this.path, function (e, root, imported) { |
---|
2654 | if (e) { e.index = index } |
---|
2655 | if (imported && that.once) that.skip = imported; |
---|
2656 | that.root = root || new(tree.Ruleset)([], []); |
---|
2657 | }); |
---|
2658 | } |
---|
2659 | }; |
---|
2660 | |
---|
2661 | // |
---|
2662 | // The actual import node doesn't return anything, when converted to CSS. |
---|
2663 | // The reason is that it's used at the evaluation stage, so that the rules |
---|
2664 | // it imports can be treated like any other rules. |
---|
2665 | // |
---|
2666 | // In `eval`, we make sure all Import nodes get evaluated, recursively, so |
---|
2667 | // we end up with a flat structure, which can easily be imported in the parent |
---|
2668 | // ruleset. |
---|
2669 | // |
---|
2670 | tree.Import.prototype = { |
---|
2671 | toCSS: function (env) { |
---|
2672 | var features = this.features ? ' ' + this.features.toCSS(env) : ''; |
---|
2673 | |
---|
2674 | if (this.css) { |
---|
2675 | // Add the base path if the import is relative |
---|
2676 | if (typeof this._path.value === "string" && !/^(?:[a-z-]+:|\/)/.test(this._path.value)) { |
---|
2677 | this._path.value = this.rootpath + this._path.value; |
---|
2678 | } |
---|
2679 | return "@import " + this._path.toCSS() + features + ';\n'; |
---|
2680 | } else { |
---|
2681 | return ""; |
---|
2682 | } |
---|
2683 | }, |
---|
2684 | eval: function (env) { |
---|
2685 | var ruleset, features = this.features && this.features.eval(env); |
---|
2686 | |
---|
2687 | if (this.skip) return []; |
---|
2688 | |
---|
2689 | if (this.css) { |
---|
2690 | return this; |
---|
2691 | } else { |
---|
2692 | ruleset = new(tree.Ruleset)([], this.root.rules.slice(0)); |
---|
2693 | |
---|
2694 | ruleset.evalImports(env); |
---|
2695 | |
---|
2696 | return this.features ? new(tree.Media)(ruleset.rules, this.features.value) : ruleset.rules; |
---|
2697 | } |
---|
2698 | } |
---|
2699 | }; |
---|
2700 | |
---|
2701 | })(require('../tree')); |
---|
2702 | (function (tree) { |
---|
2703 | |
---|
2704 | tree.JavaScript = function (string, index, escaped) { |
---|
2705 | this.escaped = escaped; |
---|
2706 | this.expression = string; |
---|
2707 | this.index = index; |
---|
2708 | }; |
---|
2709 | tree.JavaScript.prototype = { |
---|
2710 | eval: function (env) { |
---|
2711 | var result, |
---|
2712 | that = this, |
---|
2713 | context = {}; |
---|
2714 | |
---|
2715 | var expression = this.expression.replace(/@\{([\w-]+)\}/g, function (_, name) { |
---|
2716 | return tree.jsify(new(tree.Variable)('@' + name, that.index).eval(env)); |
---|
2717 | }); |
---|
2718 | |
---|
2719 | try { |
---|
2720 | expression = new(Function)('return (' + expression + ')'); |
---|
2721 | } catch (e) { |
---|
2722 | throw { message: "JavaScript evaluation error: `" + expression + "`" , |
---|
2723 | index: this.index }; |
---|
2724 | } |
---|
2725 | |
---|
2726 | for (var k in env.frames[0].variables()) { |
---|
2727 | context[k.slice(1)] = { |
---|
2728 | value: env.frames[0].variables()[k].value, |
---|
2729 | toJS: function () { |
---|
2730 | return this.value.eval(env).toCSS(); |
---|
2731 | } |
---|
2732 | }; |
---|
2733 | } |
---|
2734 | |
---|
2735 | try { |
---|
2736 | result = expression.call(context); |
---|
2737 | } catch (e) { |
---|
2738 | throw { message: "JavaScript evaluation error: '" + e.name + ': ' + e.message + "'" , |
---|
2739 | index: this.index }; |
---|
2740 | } |
---|
2741 | if (typeof(result) === 'string') { |
---|
2742 | return new(tree.Quoted)('"' + result + '"', result, this.escaped, this.index); |
---|
2743 | } else if (Array.isArray(result)) { |
---|
2744 | return new(tree.Anonymous)(result.join(', ')); |
---|
2745 | } else { |
---|
2746 | return new(tree.Anonymous)(result); |
---|
2747 | } |
---|
2748 | } |
---|
2749 | }; |
---|
2750 | |
---|
2751 | })(require('../tree')); |
---|
2752 | |
---|
2753 | (function (tree) { |
---|
2754 | |
---|
2755 | tree.Keyword = function (value) { this.value = value }; |
---|
2756 | tree.Keyword.prototype = { |
---|
2757 | eval: function () { return this }, |
---|
2758 | toCSS: function () { return this.value }, |
---|
2759 | compare: function (other) { |
---|
2760 | if (other instanceof tree.Keyword) { |
---|
2761 | return other.value === this.value ? 0 : 1; |
---|
2762 | } else { |
---|
2763 | return -1; |
---|
2764 | } |
---|
2765 | } |
---|
2766 | }; |
---|
2767 | |
---|
2768 | tree.True = new(tree.Keyword)('true'); |
---|
2769 | tree.False = new(tree.Keyword)('false'); |
---|
2770 | |
---|
2771 | })(require('../tree')); |
---|
2772 | (function (tree) { |
---|
2773 | |
---|
2774 | tree.Media = function (value, features) { |
---|
2775 | var selectors = this.emptySelectors(); |
---|
2776 | |
---|
2777 | this.features = new(tree.Value)(features); |
---|
2778 | this.ruleset = new(tree.Ruleset)(selectors, value); |
---|
2779 | this.ruleset.allowImports = true; |
---|
2780 | }; |
---|
2781 | tree.Media.prototype = { |
---|
2782 | toCSS: function (ctx, env) { |
---|
2783 | var features = this.features.toCSS(env); |
---|
2784 | |
---|
2785 | this.ruleset.root = (ctx.length === 0 || ctx[0].multiMedia); |
---|
2786 | return '@media ' + features + (env.compress ? '{' : ' {\n ') + |
---|
2787 | this.ruleset.toCSS(ctx, env).trim().replace(/\n/g, '\n ') + |
---|
2788 | (env.compress ? '}': '\n}\n'); |
---|
2789 | }, |
---|
2790 | eval: function (env) { |
---|
2791 | if (!env.mediaBlocks) { |
---|
2792 | env.mediaBlocks = []; |
---|
2793 | env.mediaPath = []; |
---|
2794 | } |
---|
2795 | |
---|
2796 | var media = new(tree.Media)([], []); |
---|
2797 | if(this.debugInfo) { |
---|
2798 | this.ruleset.debugInfo = this.debugInfo; |
---|
2799 | media.debugInfo = this.debugInfo; |
---|
2800 | } |
---|
2801 | media.features = this.features.eval(env); |
---|
2802 | |
---|
2803 | env.mediaPath.push(media); |
---|
2804 | env.mediaBlocks.push(media); |
---|
2805 | |
---|
2806 | env.frames.unshift(this.ruleset); |
---|
2807 | media.ruleset = this.ruleset.eval(env); |
---|
2808 | env.frames.shift(); |
---|
2809 | |
---|
2810 | env.mediaPath.pop(); |
---|
2811 | |
---|
2812 | return env.mediaPath.length === 0 ? media.evalTop(env) : |
---|
2813 | media.evalNested(env) |
---|
2814 | }, |
---|
2815 | variable: function (name) { return tree.Ruleset.prototype.variable.call(this.ruleset, name) }, |
---|
2816 | find: function () { return tree.Ruleset.prototype.find.apply(this.ruleset, arguments) }, |
---|
2817 | rulesets: function () { return tree.Ruleset.prototype.rulesets.apply(this.ruleset) }, |
---|
2818 | emptySelectors: function() { |
---|
2819 | var el = new(tree.Element)('', '&', 0); |
---|
2820 | return [new(tree.Selector)([el])]; |
---|
2821 | }, |
---|
2822 | |
---|
2823 | evalTop: function (env) { |
---|
2824 | var result = this; |
---|
2825 | |
---|
2826 | // Render all dependent Media blocks. |
---|
2827 | if (env.mediaBlocks.length > 1) { |
---|
2828 | var selectors = this.emptySelectors(); |
---|
2829 | result = new(tree.Ruleset)(selectors, env.mediaBlocks); |
---|
2830 | result.multiMedia = true; |
---|
2831 | } |
---|
2832 | |
---|
2833 | delete env.mediaBlocks; |
---|
2834 | delete env.mediaPath; |
---|
2835 | |
---|
2836 | return result; |
---|
2837 | }, |
---|
2838 | evalNested: function (env) { |
---|
2839 | var i, value, |
---|
2840 | path = env.mediaPath.concat([this]); |
---|
2841 | |
---|
2842 | // Extract the media-query conditions separated with `,` (OR). |
---|
2843 | for (i = 0; i < path.length; i++) { |
---|
2844 | value = path[i].features instanceof tree.Value ? |
---|
2845 | path[i].features.value : path[i].features; |
---|
2846 | path[i] = Array.isArray(value) ? value : [value]; |
---|
2847 | } |
---|
2848 | |
---|
2849 | // Trace all permutations to generate the resulting media-query. |
---|
2850 | // |
---|
2851 | // (a, b and c) with nested (d, e) -> |
---|
2852 | // a and d |
---|
2853 | // a and e |
---|
2854 | // b and c and d |
---|
2855 | // b and c and e |
---|
2856 | this.features = new(tree.Value)(this.permute(path).map(function (path) { |
---|
2857 | path = path.map(function (fragment) { |
---|
2858 | return fragment.toCSS ? fragment : new(tree.Anonymous)(fragment); |
---|
2859 | }); |
---|
2860 | |
---|
2861 | for(i = path.length - 1; i > 0; i--) { |
---|
2862 | path.splice(i, 0, new(tree.Anonymous)("and")); |
---|
2863 | } |
---|
2864 | |
---|
2865 | return new(tree.Expression)(path); |
---|
2866 | })); |
---|
2867 | |
---|
2868 | // Fake a tree-node that doesn't output anything. |
---|
2869 | return new(tree.Ruleset)([], []); |
---|
2870 | }, |
---|
2871 | permute: function (arr) { |
---|
2872 | if (arr.length === 0) { |
---|
2873 | return []; |
---|
2874 | } else if (arr.length === 1) { |
---|
2875 | return arr[0]; |
---|
2876 | } else { |
---|
2877 | var result = []; |
---|
2878 | var rest = this.permute(arr.slice(1)); |
---|
2879 | for (var i = 0; i < rest.length; i++) { |
---|
2880 | for (var j = 0; j < arr[0].length; j++) { |
---|
2881 | result.push([arr[0][j]].concat(rest[i])); |
---|
2882 | } |
---|
2883 | } |
---|
2884 | return result; |
---|
2885 | } |
---|
2886 | }, |
---|
2887 | bubbleSelectors: function (selectors) { |
---|
2888 | this.ruleset = new(tree.Ruleset)(selectors.slice(0), [this.ruleset]); |
---|
2889 | } |
---|
2890 | }; |
---|
2891 | |
---|
2892 | })(require('../tree')); |
---|
2893 | (function (tree) { |
---|
2894 | |
---|
2895 | tree.mixin = {}; |
---|
2896 | tree.mixin.Call = function (elements, args, index, filename, important) { |
---|
2897 | this.selector = new(tree.Selector)(elements); |
---|
2898 | this.arguments = args; |
---|
2899 | this.index = index; |
---|
2900 | this.filename = filename; |
---|
2901 | this.important = important; |
---|
2902 | }; |
---|
2903 | tree.mixin.Call.prototype = { |
---|
2904 | eval: function (env) { |
---|
2905 | var mixins, mixin, args, rules = [], match = false, i, m, f, isRecursive, isOneFound; |
---|
2906 | |
---|
2907 | args = this.arguments && this.arguments.map(function (a) { |
---|
2908 | return { name: a.name, value: a.value.eval(env) }; |
---|
2909 | }); |
---|
2910 | |
---|
2911 | for (i = 0; i < env.frames.length; i++) { |
---|
2912 | if ((mixins = env.frames[i].find(this.selector)).length > 0) { |
---|
2913 | isOneFound = true; |
---|
2914 | for (m = 0; m < mixins.length; m++) { |
---|
2915 | mixin = mixins[m]; |
---|
2916 | isRecursive = false; |
---|
2917 | for(f = 0; f < env.frames.length; f++) { |
---|
2918 | if ((!(mixin instanceof tree.mixin.Definition)) && mixin === (env.frames[f].originalRuleset || env.frames[f])) { |
---|
2919 | isRecursive = true; |
---|
2920 | break; |
---|
2921 | } |
---|
2922 | } |
---|
2923 | if (isRecursive) { |
---|
2924 | continue; |
---|
2925 | } |
---|
2926 | if (mixin.matchArgs(args, env)) { |
---|
2927 | if (!mixin.matchCondition || mixin.matchCondition(args, env)) { |
---|
2928 | try { |
---|
2929 | Array.prototype.push.apply( |
---|
2930 | rules, mixin.eval(env, args, this.important).rules); |
---|
2931 | } catch (e) { |
---|
2932 | throw { message: e.message, index: this.index, filename: this.filename, stack: e.stack }; |
---|
2933 | } |
---|
2934 | } |
---|
2935 | match = true; |
---|
2936 | } |
---|
2937 | } |
---|
2938 | if (match) { |
---|
2939 | return rules; |
---|
2940 | } |
---|
2941 | } |
---|
2942 | } |
---|
2943 | if (isOneFound) { |
---|
2944 | throw { type: 'Runtime', |
---|
2945 | message: 'No matching definition was found for `' + |
---|
2946 | this.selector.toCSS().trim() + '(' + |
---|
2947 | (args ? args.map(function (a) { |
---|
2948 | var argValue = ""; |
---|
2949 | if (a.name) { |
---|
2950 | argValue += a.name + ":"; |
---|
2951 | } |
---|
2952 | if (a.value.toCSS) { |
---|
2953 | argValue += a.value.toCSS(); |
---|
2954 | } else { |
---|
2955 | argValue += "???"; |
---|
2956 | } |
---|
2957 | return argValue; |
---|
2958 | }).join(', ') : "") + ")`", |
---|
2959 | index: this.index, filename: this.filename }; |
---|
2960 | } else { |
---|
2961 | throw { type: 'Name', |
---|
2962 | message: this.selector.toCSS().trim() + " is undefined", |
---|
2963 | index: this.index, filename: this.filename }; |
---|
2964 | } |
---|
2965 | } |
---|
2966 | }; |
---|
2967 | |
---|
2968 | tree.mixin.Definition = function (name, params, rules, condition, variadic) { |
---|
2969 | this.name = name; |
---|
2970 | this.selectors = [new(tree.Selector)([new(tree.Element)(null, name)])]; |
---|
2971 | this.params = params; |
---|
2972 | this.condition = condition; |
---|
2973 | this.variadic = variadic; |
---|
2974 | this.arity = params.length; |
---|
2975 | this.rules = rules; |
---|
2976 | this._lookups = {}; |
---|
2977 | this.required = params.reduce(function (count, p) { |
---|
2978 | if (!p.name || (p.name && !p.value)) { return count + 1 } |
---|
2979 | else { return count } |
---|
2980 | }, 0); |
---|
2981 | this.parent = tree.Ruleset.prototype; |
---|
2982 | this.frames = []; |
---|
2983 | }; |
---|
2984 | tree.mixin.Definition.prototype = { |
---|
2985 | toCSS: function () { return "" }, |
---|
2986 | variable: function (name) { return this.parent.variable.call(this, name) }, |
---|
2987 | variables: function () { return this.parent.variables.call(this) }, |
---|
2988 | find: function () { return this.parent.find.apply(this, arguments) }, |
---|
2989 | rulesets: function () { return this.parent.rulesets.apply(this) }, |
---|
2990 | |
---|
2991 | evalParams: function (env, mixinEnv, args, evaldArguments) { |
---|
2992 | var frame = new(tree.Ruleset)(null, []), varargs, arg, params = this.params.slice(0), i, j, val, name, isNamedFound, argIndex; |
---|
2993 | |
---|
2994 | if (args) { |
---|
2995 | args = args.slice(0); |
---|
2996 | |
---|
2997 | for(i = 0; i < args.length; i++) { |
---|
2998 | arg = args[i]; |
---|
2999 | if (name = (arg && arg.name)) { |
---|
3000 | isNamedFound = false; |
---|
3001 | for(j = 0; j < params.length; j++) { |
---|
3002 | if (!evaldArguments[j] && name === params[j].name) { |
---|
3003 | evaldArguments[j] = arg.value.eval(env); |
---|
3004 | frame.rules.unshift(new(tree.Rule)(name, arg.value.eval(env))); |
---|
3005 | isNamedFound = true; |
---|
3006 | break; |
---|
3007 | } |
---|
3008 | } |
---|
3009 | if (isNamedFound) { |
---|
3010 | args.splice(i, 1); |
---|
3011 | i--; |
---|
3012 | continue; |
---|
3013 | } else { |
---|
3014 | throw { type: 'Runtime', message: "Named argument for " + this.name + |
---|
3015 | ' ' + args[i].name + ' not found' }; |
---|
3016 | } |
---|
3017 | } |
---|
3018 | } |
---|
3019 | } |
---|
3020 | argIndex = 0; |
---|
3021 | for (i = 0; i < params.length; i++) { |
---|
3022 | if (evaldArguments[i]) continue; |
---|
3023 | |
---|
3024 | arg = args && args[argIndex]; |
---|
3025 | |
---|
3026 | if (name = params[i].name) { |
---|
3027 | if (params[i].variadic && args) { |
---|
3028 | varargs = []; |
---|
3029 | for (j = argIndex; j < args.length; j++) { |
---|
3030 | varargs.push(args[j].value.eval(env)); |
---|
3031 | } |
---|
3032 | frame.rules.unshift(new(tree.Rule)(name, new(tree.Expression)(varargs).eval(env))); |
---|
3033 | } else { |
---|
3034 | val = arg && arg.value; |
---|
3035 | if (val) { |
---|
3036 | val = val.eval(env); |
---|
3037 | } else if (params[i].value) { |
---|
3038 | val = params[i].value.eval(mixinEnv); |
---|
3039 | } else { |
---|
3040 | throw { type: 'Runtime', message: "wrong number of arguments for " + this.name + |
---|
3041 | ' (' + args.length + ' for ' + this.arity + ')' }; |
---|
3042 | } |
---|
3043 | |
---|
3044 | frame.rules.unshift(new(tree.Rule)(name, val)); |
---|
3045 | evaldArguments[i] = val; |
---|
3046 | } |
---|
3047 | } |
---|
3048 | |
---|
3049 | if (params[i].variadic && args) { |
---|
3050 | for (j = argIndex; j < args.length; j++) { |
---|
3051 | evaldArguments[j] = args[j].value.eval(env); |
---|
3052 | } |
---|
3053 | } |
---|
3054 | argIndex++; |
---|
3055 | } |
---|
3056 | |
---|
3057 | return frame; |
---|
3058 | }, |
---|
3059 | eval: function (env, args, important) { |
---|
3060 | var _arguments = [], |
---|
3061 | mixinFrames = this.frames.concat(env.frames), |
---|
3062 | frame = this.evalParams(env, {frames: mixinFrames}, args, _arguments), |
---|
3063 | context, rules, start, ruleset; |
---|
3064 | |
---|
3065 | frame.rules.unshift(new(tree.Rule)('@arguments', new(tree.Expression)(_arguments).eval(env))); |
---|
3066 | |
---|
3067 | rules = important ? |
---|
3068 | this.parent.makeImportant.apply(this).rules : this.rules.slice(0); |
---|
3069 | |
---|
3070 | ruleset = new(tree.Ruleset)(null, rules).eval({ |
---|
3071 | frames: [this, frame].concat(mixinFrames) |
---|
3072 | }); |
---|
3073 | ruleset.originalRuleset = this; |
---|
3074 | return ruleset; |
---|
3075 | }, |
---|
3076 | matchCondition: function (args, env) { |
---|
3077 | if (this.condition && !this.condition.eval({ |
---|
3078 | frames: [this.evalParams(env, {frames: this.frames.concat(env.frames)}, args, [])].concat(env.frames) |
---|
3079 | })) { return false } |
---|
3080 | return true; |
---|
3081 | }, |
---|
3082 | matchArgs: function (args, env) { |
---|
3083 | var argsLength = (args && args.length) || 0, len, frame; |
---|
3084 | |
---|
3085 | if (! this.variadic) { |
---|
3086 | if (argsLength < this.required) { return false } |
---|
3087 | if (argsLength > this.params.length) { return false } |
---|
3088 | if ((this.required > 0) && (argsLength > this.params.length)) { return false } |
---|
3089 | } |
---|
3090 | |
---|
3091 | len = Math.min(argsLength, this.arity); |
---|
3092 | |
---|
3093 | for (var i = 0; i < len; i++) { |
---|
3094 | if (!this.params[i].name && !this.params[i].variadic) { |
---|
3095 | if (args[i].value.eval(env).toCSS() != this.params[i].value.eval(env).toCSS()) { |
---|
3096 | return false; |
---|
3097 | } |
---|
3098 | } |
---|
3099 | } |
---|
3100 | return true; |
---|
3101 | } |
---|
3102 | }; |
---|
3103 | |
---|
3104 | })(require('../tree')); |
---|
3105 | (function (tree) { |
---|
3106 | |
---|
3107 | tree.Operation = function (op, operands) { |
---|
3108 | this.op = op.trim(); |
---|
3109 | this.operands = operands; |
---|
3110 | }; |
---|
3111 | tree.Operation.prototype.eval = function (env) { |
---|
3112 | var a = this.operands[0].eval(env), |
---|
3113 | b = this.operands[1].eval(env), |
---|
3114 | temp; |
---|
3115 | |
---|
3116 | if (a instanceof tree.Dimension && b instanceof tree.Color) { |
---|
3117 | if (this.op === '*' || this.op === '+') { |
---|
3118 | temp = b, b = a, a = temp; |
---|
3119 | } else { |
---|
3120 | throw { name: "OperationError", |
---|
3121 | message: "Can't substract or divide a color from a number" }; |
---|
3122 | } |
---|
3123 | } |
---|
3124 | if (!a.operate) { |
---|
3125 | throw { name: "OperationError", |
---|
3126 | message: "Operation on an invalid type" }; |
---|
3127 | } |
---|
3128 | |
---|
3129 | return a.operate(this.op, b); |
---|
3130 | }; |
---|
3131 | |
---|
3132 | tree.operate = function (op, a, b) { |
---|
3133 | switch (op) { |
---|
3134 | case '+': return a + b; |
---|
3135 | case '-': return a - b; |
---|
3136 | case '*': return a * b; |
---|
3137 | case '/': return a / b; |
---|
3138 | } |
---|
3139 | }; |
---|
3140 | |
---|
3141 | })(require('../tree')); |
---|
3142 | |
---|
3143 | (function (tree) { |
---|
3144 | |
---|
3145 | tree.Paren = function (node) { |
---|
3146 | this.value = node; |
---|
3147 | }; |
---|
3148 | tree.Paren.prototype = { |
---|
3149 | toCSS: function (env) { |
---|
3150 | return '(' + this.value.toCSS(env) + ')'; |
---|
3151 | }, |
---|
3152 | eval: function (env) { |
---|
3153 | return new(tree.Paren)(this.value.eval(env)); |
---|
3154 | } |
---|
3155 | }; |
---|
3156 | |
---|
3157 | })(require('../tree')); |
---|
3158 | (function (tree) { |
---|
3159 | |
---|
3160 | tree.Quoted = function (str, content, escaped, i) { |
---|
3161 | this.escaped = escaped; |
---|
3162 | this.value = content || ''; |
---|
3163 | this.quote = str.charAt(0); |
---|
3164 | this.index = i; |
---|
3165 | }; |
---|
3166 | tree.Quoted.prototype = { |
---|
3167 | toCSS: function () { |
---|
3168 | if (this.escaped) { |
---|
3169 | return this.value; |
---|
3170 | } else { |
---|
3171 | return this.quote + this.value + this.quote; |
---|
3172 | } |
---|
3173 | }, |
---|
3174 | eval: function (env) { |
---|
3175 | var that = this; |
---|
3176 | var value = this.value.replace(/`([^`]+)`/g, function (_, exp) { |
---|
3177 | return new(tree.JavaScript)(exp, that.index, true).eval(env).value; |
---|
3178 | }).replace(/@\{([\w-]+)\}/g, function (_, name) { |
---|
3179 | var v = new(tree.Variable)('@' + name, that.index).eval(env); |
---|
3180 | return (v instanceof tree.Quoted) ? v.value : v.toCSS(); |
---|
3181 | }); |
---|
3182 | return new(tree.Quoted)(this.quote + value + this.quote, value, this.escaped, this.index); |
---|
3183 | }, |
---|
3184 | compare: function (x) { |
---|
3185 | if (!x.toCSS) { |
---|
3186 | return -1; |
---|
3187 | } |
---|
3188 | |
---|
3189 | var left = this.toCSS(), |
---|
3190 | right = x.toCSS(); |
---|
3191 | |
---|
3192 | if (left === right) { |
---|
3193 | return 0; |
---|
3194 | } |
---|
3195 | |
---|
3196 | return left < right ? -1 : 1; |
---|
3197 | } |
---|
3198 | }; |
---|
3199 | |
---|
3200 | })(require('../tree')); |
---|
3201 | (function (tree) { |
---|
3202 | |
---|
3203 | tree.Ratio = function (value) { |
---|
3204 | this.value = value; |
---|
3205 | }; |
---|
3206 | tree.Ratio.prototype = { |
---|
3207 | toCSS: function (env) { |
---|
3208 | return this.value; |
---|
3209 | }, |
---|
3210 | eval: function () { return this } |
---|
3211 | }; |
---|
3212 | |
---|
3213 | })(require('../tree')); |
---|
3214 | (function (tree) { |
---|
3215 | |
---|
3216 | tree.Rule = function (name, value, important, index, inline) { |
---|
3217 | this.name = name; |
---|
3218 | this.value = (value instanceof tree.Value) ? value : new(tree.Value)([value]); |
---|
3219 | this.important = important ? ' ' + important.trim() : ''; |
---|
3220 | this.index = index; |
---|
3221 | this.inline = inline || false; |
---|
3222 | |
---|
3223 | if (name.charAt(0) === '@') { |
---|
3224 | this.variable = true; |
---|
3225 | } else { this.variable = false } |
---|
3226 | }; |
---|
3227 | tree.Rule.prototype.toCSS = function (env) { |
---|
3228 | if (this.variable) { return "" } |
---|
3229 | else { |
---|
3230 | return this.name + (env.compress ? ':' : ': ') + |
---|
3231 | this.value.toCSS(env) + |
---|
3232 | this.important + (this.inline ? "" : ";"); |
---|
3233 | } |
---|
3234 | }; |
---|
3235 | |
---|
3236 | tree.Rule.prototype.eval = function (context) { |
---|
3237 | return new(tree.Rule)(this.name, |
---|
3238 | this.value.eval(context), |
---|
3239 | this.important, |
---|
3240 | this.index, this.inline); |
---|
3241 | }; |
---|
3242 | |
---|
3243 | tree.Rule.prototype.makeImportant = function () { |
---|
3244 | return new(tree.Rule)(this.name, |
---|
3245 | this.value, |
---|
3246 | "!important", |
---|
3247 | this.index, this.inline); |
---|
3248 | }; |
---|
3249 | |
---|
3250 | tree.Shorthand = function (a, b) { |
---|
3251 | this.a = a; |
---|
3252 | this.b = b; |
---|
3253 | }; |
---|
3254 | |
---|
3255 | tree.Shorthand.prototype = { |
---|
3256 | toCSS: function (env) { |
---|
3257 | return this.a.toCSS(env) + "/" + this.b.toCSS(env); |
---|
3258 | }, |
---|
3259 | eval: function () { return this } |
---|
3260 | }; |
---|
3261 | |
---|
3262 | })(require('../tree')); |
---|
3263 | (function (tree) { |
---|
3264 | |
---|
3265 | tree.Ruleset = function (selectors, rules, strictImports) { |
---|
3266 | this.selectors = selectors; |
---|
3267 | this.rules = rules; |
---|
3268 | this._lookups = {}; |
---|
3269 | this.strictImports = strictImports; |
---|
3270 | }; |
---|
3271 | tree.Ruleset.prototype = { |
---|
3272 | eval: function (env) { |
---|
3273 | var selectors = this.selectors && this.selectors.map(function (s) { return s.eval(env) }); |
---|
3274 | var ruleset = new(tree.Ruleset)(selectors, this.rules.slice(0), this.strictImports); |
---|
3275 | var rules; |
---|
3276 | |
---|
3277 | ruleset.originalRuleset = this; |
---|
3278 | ruleset.root = this.root; |
---|
3279 | ruleset.allowImports = this.allowImports; |
---|
3280 | |
---|
3281 | if(this.debugInfo) { |
---|
3282 | ruleset.debugInfo = this.debugInfo; |
---|
3283 | } |
---|
3284 | |
---|
3285 | // push the current ruleset to the frames stack |
---|
3286 | env.frames.unshift(ruleset); |
---|
3287 | |
---|
3288 | // Evaluate imports |
---|
3289 | if (ruleset.root || ruleset.allowImports || !ruleset.strictImports) { |
---|
3290 | ruleset.evalImports(env); |
---|
3291 | } |
---|
3292 | |
---|
3293 | // Store the frames around mixin definitions, |
---|
3294 | // so they can be evaluated like closures when the time comes. |
---|
3295 | for (var i = 0; i < ruleset.rules.length; i++) { |
---|
3296 | if (ruleset.rules[i] instanceof tree.mixin.Definition) { |
---|
3297 | ruleset.rules[i].frames = env.frames.slice(0); |
---|
3298 | } |
---|
3299 | } |
---|
3300 | |
---|
3301 | var mediaBlockCount = (env.mediaBlocks && env.mediaBlocks.length) || 0; |
---|
3302 | |
---|
3303 | // Evaluate mixin calls. |
---|
3304 | for (var i = 0; i < ruleset.rules.length; i++) { |
---|
3305 | if (ruleset.rules[i] instanceof tree.mixin.Call) { |
---|
3306 | rules = ruleset.rules[i].eval(env); |
---|
3307 | ruleset.rules.splice.apply(ruleset.rules, [i, 1].concat(rules)); |
---|
3308 | i += rules.length-1; |
---|
3309 | ruleset.resetCache(); |
---|
3310 | } |
---|
3311 | } |
---|
3312 | |
---|
3313 | // Evaluate everything else |
---|
3314 | for (var i = 0, rule; i < ruleset.rules.length; i++) { |
---|
3315 | rule = ruleset.rules[i]; |
---|
3316 | |
---|
3317 | if (! (rule instanceof tree.mixin.Definition)) { |
---|
3318 | ruleset.rules[i] = rule.eval ? rule.eval(env) : rule; |
---|
3319 | } |
---|
3320 | } |
---|
3321 | |
---|
3322 | // Pop the stack |
---|
3323 | env.frames.shift(); |
---|
3324 | |
---|
3325 | if (env.mediaBlocks) { |
---|
3326 | for(var i = mediaBlockCount; i < env.mediaBlocks.length; i++) { |
---|
3327 | env.mediaBlocks[i].bubbleSelectors(selectors); |
---|
3328 | } |
---|
3329 | } |
---|
3330 | |
---|
3331 | return ruleset; |
---|
3332 | }, |
---|
3333 | evalImports: function(env) { |
---|
3334 | var i, rules; |
---|
3335 | for (i = 0; i < this.rules.length; i++) { |
---|
3336 | if (this.rules[i] instanceof tree.Import) { |
---|
3337 | rules = this.rules[i].eval(env); |
---|
3338 | if (typeof rules.length === "number") { |
---|
3339 | this.rules.splice.apply(this.rules, [i, 1].concat(rules)); |
---|
3340 | i+= rules.length-1; |
---|
3341 | } else { |
---|
3342 | this.rules.splice(i, 1, rules); |
---|
3343 | } |
---|
3344 | this.resetCache(); |
---|
3345 | } |
---|
3346 | } |
---|
3347 | }, |
---|
3348 | makeImportant: function() { |
---|
3349 | return new tree.Ruleset(this.selectors, this.rules.map(function (r) { |
---|
3350 | if (r.makeImportant) { |
---|
3351 | return r.makeImportant(); |
---|
3352 | } else { |
---|
3353 | return r; |
---|
3354 | } |
---|
3355 | }), this.strictImports); |
---|
3356 | }, |
---|
3357 | matchArgs: function (args) { |
---|
3358 | return !args || args.length === 0; |
---|
3359 | }, |
---|
3360 | resetCache: function () { |
---|
3361 | this._rulesets = null; |
---|
3362 | this._variables = null; |
---|
3363 | this._lookups = {}; |
---|
3364 | }, |
---|
3365 | variables: function () { |
---|
3366 | if (this._variables) { return this._variables } |
---|
3367 | else { |
---|
3368 | return this._variables = this.rules.reduce(function (hash, r) { |
---|
3369 | if (r instanceof tree.Rule && r.variable === true) { |
---|
3370 | hash[r.name] = r; |
---|
3371 | } |
---|
3372 | return hash; |
---|
3373 | }, {}); |
---|
3374 | } |
---|
3375 | }, |
---|
3376 | variable: function (name) { |
---|
3377 | return this.variables()[name]; |
---|
3378 | }, |
---|
3379 | rulesets: function () { |
---|
3380 | if (this._rulesets) { return this._rulesets } |
---|
3381 | else { |
---|
3382 | return this._rulesets = this.rules.filter(function (r) { |
---|
3383 | return (r instanceof tree.Ruleset) || (r instanceof tree.mixin.Definition); |
---|
3384 | }); |
---|
3385 | } |
---|
3386 | }, |
---|
3387 | find: function (selector, self) { |
---|
3388 | self = self || this; |
---|
3389 | var rules = [], rule, match, |
---|
3390 | key = selector.toCSS(); |
---|
3391 | |
---|
3392 | if (key in this._lookups) { return this._lookups[key] } |
---|
3393 | |
---|
3394 | this.rulesets().forEach(function (rule) { |
---|
3395 | if (rule !== self) { |
---|
3396 | for (var j = 0; j < rule.selectors.length; j++) { |
---|
3397 | if (match = selector.match(rule.selectors[j])) { |
---|
3398 | if (selector.elements.length > rule.selectors[j].elements.length) { |
---|
3399 | Array.prototype.push.apply(rules, rule.find( |
---|
3400 | new(tree.Selector)(selector.elements.slice(1)), self)); |
---|
3401 | } else { |
---|
3402 | rules.push(rule); |
---|
3403 | } |
---|
3404 | break; |
---|
3405 | } |
---|
3406 | } |
---|
3407 | } |
---|
3408 | }); |
---|
3409 | return this._lookups[key] = rules; |
---|
3410 | }, |
---|
3411 | // |
---|
3412 | // Entry point for code generation |
---|
3413 | // |
---|
3414 | // `context` holds an array of arrays. |
---|
3415 | // |
---|
3416 | toCSS: function (context, env) { |
---|
3417 | var css = [], // The CSS output |
---|
3418 | rules = [], // node.Rule instances |
---|
3419 | _rules = [], // |
---|
3420 | rulesets = [], // node.Ruleset instances |
---|
3421 | paths = [], // Current selectors |
---|
3422 | selector, // The fully rendered selector |
---|
3423 | debugInfo, // Line number debugging |
---|
3424 | rule; |
---|
3425 | |
---|
3426 | if (! this.root) { |
---|
3427 | this.joinSelectors(paths, context, this.selectors); |
---|
3428 | } |
---|
3429 | |
---|
3430 | // Compile rules and rulesets |
---|
3431 | for (var i = 0; i < this.rules.length; i++) { |
---|
3432 | rule = this.rules[i]; |
---|
3433 | |
---|
3434 | if (rule.rules || (rule instanceof tree.Media)) { |
---|
3435 | rulesets.push(rule.toCSS(paths, env)); |
---|
3436 | } else if (rule instanceof tree.Directive) { |
---|
3437 | var cssValue = rule.toCSS(paths, env); |
---|
3438 | // Output only the first @charset definition as such - convert the others |
---|
3439 | // to comments in case debug is enabled |
---|
3440 | if (rule.name === "@charset") { |
---|
3441 | // Only output the debug info together with subsequent @charset definitions |
---|
3442 | // a comment (or @media statement) before the actual @charset directive would |
---|
3443 | // be considered illegal css as it has to be on the first line |
---|
3444 | if (env.charset) { |
---|
3445 | if (rule.debugInfo) { |
---|
3446 | rulesets.push(tree.debugInfo(env, rule)); |
---|
3447 | rulesets.push(new tree.Comment("/* "+cssValue.replace(/\n/g, "")+" */\n").toCSS(env)); |
---|
3448 | } |
---|
3449 | continue; |
---|
3450 | } |
---|
3451 | env.charset = true; |
---|
3452 | } |
---|
3453 | rulesets.push(cssValue); |
---|
3454 | } else if (rule instanceof tree.Comment) { |
---|
3455 | if (!rule.silent) { |
---|
3456 | if (this.root) { |
---|
3457 | rulesets.push(rule.toCSS(env)); |
---|
3458 | } else { |
---|
3459 | rules.push(rule.toCSS(env)); |
---|
3460 | } |
---|
3461 | } |
---|
3462 | } else { |
---|
3463 | if (rule.toCSS && !rule.variable) { |
---|
3464 | rules.push(rule.toCSS(env)); |
---|
3465 | } else if (rule.value && !rule.variable) { |
---|
3466 | rules.push(rule.value.toString()); |
---|
3467 | } |
---|
3468 | } |
---|
3469 | } |
---|
3470 | |
---|
3471 | rulesets = rulesets.join(''); |
---|
3472 | |
---|
3473 | // If this is the root node, we don't render |
---|
3474 | // a selector, or {}. |
---|
3475 | // Otherwise, only output if this ruleset has rules. |
---|
3476 | if (this.root) { |
---|
3477 | css.push(rules.join(env.compress ? '' : '\n')); |
---|
3478 | } else { |
---|
3479 | if (rules.length > 0) { |
---|
3480 | debugInfo = tree.debugInfo(env, this); |
---|
3481 | selector = paths.map(function (p) { |
---|
3482 | return p.map(function (s) { |
---|
3483 | return s.toCSS(env); |
---|
3484 | }).join('').trim(); |
---|
3485 | }).join(env.compress ? ',' : ',\n'); |
---|
3486 | |
---|
3487 | // Remove duplicates |
---|
3488 | for (var i = rules.length - 1; i >= 0; i--) { |
---|
3489 | if (_rules.indexOf(rules[i]) === -1) { |
---|
3490 | _rules.unshift(rules[i]); |
---|
3491 | } |
---|
3492 | } |
---|
3493 | rules = _rules; |
---|
3494 | |
---|
3495 | css.push(debugInfo + selector + |
---|
3496 | (env.compress ? '{' : ' {\n ') + |
---|
3497 | rules.join(env.compress ? '' : '\n ') + |
---|
3498 | (env.compress ? '}' : '\n}\n')); |
---|
3499 | } |
---|
3500 | } |
---|
3501 | css.push(rulesets); |
---|
3502 | |
---|
3503 | return css.join('') + (env.compress ? '\n' : ''); |
---|
3504 | }, |
---|
3505 | |
---|
3506 | joinSelectors: function (paths, context, selectors) { |
---|
3507 | for (var s = 0; s < selectors.length; s++) { |
---|
3508 | this.joinSelector(paths, context, selectors[s]); |
---|
3509 | } |
---|
3510 | }, |
---|
3511 | |
---|
3512 | joinSelector: function (paths, context, selector) { |
---|
3513 | |
---|
3514 | var i, j, k, |
---|
3515 | hasParentSelector, newSelectors, el, sel, parentSel, |
---|
3516 | newSelectorPath, afterParentJoin, newJoinedSelector, |
---|
3517 | newJoinedSelectorEmpty, lastSelector, currentElements, |
---|
3518 | selectorsMultiplied; |
---|
3519 | |
---|
3520 | for (i = 0; i < selector.elements.length; i++) { |
---|
3521 | el = selector.elements[i]; |
---|
3522 | if (el.value === '&') { |
---|
3523 | hasParentSelector = true; |
---|
3524 | } |
---|
3525 | } |
---|
3526 | |
---|
3527 | if (!hasParentSelector) { |
---|
3528 | if (context.length > 0) { |
---|
3529 | for(i = 0; i < context.length; i++) { |
---|
3530 | paths.push(context[i].concat(selector)); |
---|
3531 | } |
---|
3532 | } |
---|
3533 | else { |
---|
3534 | paths.push([selector]); |
---|
3535 | } |
---|
3536 | return; |
---|
3537 | } |
---|
3538 | |
---|
3539 | // The paths are [[Selector]] |
---|
3540 | // The first list is a list of comma seperated selectors |
---|
3541 | // The inner list is a list of inheritance seperated selectors |
---|
3542 | // e.g. |
---|
3543 | // .a, .b { |
---|
3544 | // .c { |
---|
3545 | // } |
---|
3546 | // } |
---|
3547 | // == [[.a] [.c]] [[.b] [.c]] |
---|
3548 | // |
---|
3549 | |
---|
3550 | // the elements from the current selector so far |
---|
3551 | currentElements = []; |
---|
3552 | // the current list of new selectors to add to the path. |
---|
3553 | // We will build it up. We initiate it with one empty selector as we "multiply" the new selectors |
---|
3554 | // by the parents |
---|
3555 | newSelectors = [[]]; |
---|
3556 | |
---|
3557 | for (i = 0; i < selector.elements.length; i++) { |
---|
3558 | el = selector.elements[i]; |
---|
3559 | // non parent reference elements just get added |
---|
3560 | if (el.value !== "&") { |
---|
3561 | currentElements.push(el); |
---|
3562 | } else { |
---|
3563 | // the new list of selectors to add |
---|
3564 | selectorsMultiplied = []; |
---|
3565 | |
---|
3566 | // merge the current list of non parent selector elements |
---|
3567 | // on to the current list of selectors to add |
---|
3568 | if (currentElements.length > 0) { |
---|
3569 | this.mergeElementsOnToSelectors(currentElements, newSelectors); |
---|
3570 | } |
---|
3571 | |
---|
3572 | // loop through our current selectors |
---|
3573 | for(j = 0; j < newSelectors.length; j++) { |
---|
3574 | sel = newSelectors[j]; |
---|
3575 | // if we don't have any parent paths, the & might be in a mixin so that it can be used |
---|
3576 | // whether there are parents or not |
---|
3577 | if (context.length == 0) { |
---|
3578 | // the combinator used on el should now be applied to the next element instead so that |
---|
3579 | // it is not lost |
---|
3580 | if (sel.length > 0) { |
---|
3581 | sel[0].elements = sel[0].elements.slice(0); |
---|
3582 | sel[0].elements.push(new(tree.Element)(el.combinator, '', 0)); //new Element(el.Combinator, "")); |
---|
3583 | } |
---|
3584 | selectorsMultiplied.push(sel); |
---|
3585 | } |
---|
3586 | else { |
---|
3587 | // and the parent selectors |
---|
3588 | for(k = 0; k < context.length; k++) { |
---|
3589 | parentSel = context[k]; |
---|
3590 | // We need to put the current selectors |
---|
3591 | // then join the last selector's elements on to the parents selectors |
---|
3592 | |
---|
3593 | // our new selector path |
---|
3594 | newSelectorPath = []; |
---|
3595 | // selectors from the parent after the join |
---|
3596 | afterParentJoin = []; |
---|
3597 | newJoinedSelectorEmpty = true; |
---|
3598 | |
---|
3599 | //construct the joined selector - if & is the first thing this will be empty, |
---|
3600 | // if not newJoinedSelector will be the last set of elements in the selector |
---|
3601 | if (sel.length > 0) { |
---|
3602 | newSelectorPath = sel.slice(0); |
---|
3603 | lastSelector = newSelectorPath.pop(); |
---|
3604 | newJoinedSelector = new(tree.Selector)(lastSelector.elements.slice(0)); |
---|
3605 | newJoinedSelectorEmpty = false; |
---|
3606 | } |
---|
3607 | else { |
---|
3608 | newJoinedSelector = new(tree.Selector)([]); |
---|
3609 | } |
---|
3610 | |
---|
3611 | //put together the parent selectors after the join |
---|
3612 | if (parentSel.length > 1) { |
---|
3613 | afterParentJoin = afterParentJoin.concat(parentSel.slice(1)); |
---|
3614 | } |
---|
3615 | |
---|
3616 | if (parentSel.length > 0) { |
---|
3617 | newJoinedSelectorEmpty = false; |
---|
3618 | |
---|
3619 | // join the elements so far with the first part of the parent |
---|
3620 | newJoinedSelector.elements.push(new(tree.Element)(el.combinator, parentSel[0].elements[0].value, 0)); |
---|
3621 | newJoinedSelector.elements = newJoinedSelector.elements.concat(parentSel[0].elements.slice(1)); |
---|
3622 | } |
---|
3623 | |
---|
3624 | if (!newJoinedSelectorEmpty) { |
---|
3625 | // now add the joined selector |
---|
3626 | newSelectorPath.push(newJoinedSelector); |
---|
3627 | } |
---|
3628 | |
---|
3629 | // and the rest of the parent |
---|
3630 | newSelectorPath = newSelectorPath.concat(afterParentJoin); |
---|
3631 | |
---|
3632 | // add that to our new set of selectors |
---|
3633 | selectorsMultiplied.push(newSelectorPath); |
---|
3634 | } |
---|
3635 | } |
---|
3636 | } |
---|
3637 | |
---|
3638 | // our new selectors has been multiplied, so reset the state |
---|
3639 | newSelectors = selectorsMultiplied; |
---|
3640 | currentElements = []; |
---|
3641 | } |
---|
3642 | } |
---|
3643 | |
---|
3644 | // if we have any elements left over (e.g. .a& .b == .b) |
---|
3645 | // add them on to all the current selectors |
---|
3646 | if (currentElements.length > 0) { |
---|
3647 | this.mergeElementsOnToSelectors(currentElements, newSelectors); |
---|
3648 | } |
---|
3649 | |
---|
3650 | for(i = 0; i < newSelectors.length; i++) { |
---|
3651 | paths.push(newSelectors[i]); |
---|
3652 | } |
---|
3653 | }, |
---|
3654 | |
---|
3655 | mergeElementsOnToSelectors: function(elements, selectors) { |
---|
3656 | var i, sel; |
---|
3657 | |
---|
3658 | if (selectors.length == 0) { |
---|
3659 | selectors.push([ new(tree.Selector)(elements) ]); |
---|
3660 | return; |
---|
3661 | } |
---|
3662 | |
---|
3663 | for(i = 0; i < selectors.length; i++) { |
---|
3664 | sel = selectors[i]; |
---|
3665 | |
---|
3666 | // if the previous thing in sel is a parent this needs to join on to it |
---|
3667 | if (sel.length > 0) { |
---|
3668 | sel[sel.length - 1] = new(tree.Selector)(sel[sel.length - 1].elements.concat(elements)); |
---|
3669 | } |
---|
3670 | else { |
---|
3671 | sel.push(new(tree.Selector)(elements)); |
---|
3672 | } |
---|
3673 | } |
---|
3674 | } |
---|
3675 | }; |
---|
3676 | })(require('../tree')); |
---|
3677 | (function (tree) { |
---|
3678 | |
---|
3679 | tree.Selector = function (elements) { |
---|
3680 | this.elements = elements; |
---|
3681 | }; |
---|
3682 | tree.Selector.prototype.match = function (other) { |
---|
3683 | var elements = this.elements, |
---|
3684 | len = elements.length, |
---|
3685 | oelements, olen, max, i; |
---|
3686 | |
---|
3687 | oelements = other.elements.slice( |
---|
3688 | (other.elements.length && other.elements[0].value === "&") ? 1 : 0); |
---|
3689 | olen = oelements.length; |
---|
3690 | max = Math.min(len, olen) |
---|
3691 | |
---|
3692 | if (olen === 0 || len < olen) { |
---|
3693 | return false; |
---|
3694 | } else { |
---|
3695 | for (i = 0; i < max; i++) { |
---|
3696 | if (elements[i].value !== oelements[i].value) { |
---|
3697 | return false; |
---|
3698 | } |
---|
3699 | } |
---|
3700 | } |
---|
3701 | return true; |
---|
3702 | }; |
---|
3703 | tree.Selector.prototype.eval = function (env) { |
---|
3704 | return new(tree.Selector)(this.elements.map(function (e) { |
---|
3705 | return e.eval(env); |
---|
3706 | })); |
---|
3707 | }; |
---|
3708 | tree.Selector.prototype.toCSS = function (env) { |
---|
3709 | if (this._css) { return this._css } |
---|
3710 | |
---|
3711 | if (this.elements[0].combinator.value === "") { |
---|
3712 | this._css = ' '; |
---|
3713 | } else { |
---|
3714 | this._css = ''; |
---|
3715 | } |
---|
3716 | |
---|
3717 | this._css += this.elements.map(function (e) { |
---|
3718 | if (typeof(e) === 'string') { |
---|
3719 | return ' ' + e.trim(); |
---|
3720 | } else { |
---|
3721 | return e.toCSS(env); |
---|
3722 | } |
---|
3723 | }).join(''); |
---|
3724 | |
---|
3725 | return this._css; |
---|
3726 | }; |
---|
3727 | |
---|
3728 | })(require('../tree')); |
---|
3729 | (function (tree) { |
---|
3730 | |
---|
3731 | tree.UnicodeDescriptor = function (value) { |
---|
3732 | this.value = value; |
---|
3733 | }; |
---|
3734 | tree.UnicodeDescriptor.prototype = { |
---|
3735 | toCSS: function (env) { |
---|
3736 | return this.value; |
---|
3737 | }, |
---|
3738 | eval: function () { return this } |
---|
3739 | }; |
---|
3740 | |
---|
3741 | })(require('../tree')); |
---|
3742 | (function (tree) { |
---|
3743 | |
---|
3744 | tree.URL = function (val, rootpath) { |
---|
3745 | this.value = val; |
---|
3746 | this.rootpath = rootpath; |
---|
3747 | }; |
---|
3748 | tree.URL.prototype = { |
---|
3749 | toCSS: function () { |
---|
3750 | return "url(" + this.value.toCSS() + ")"; |
---|
3751 | }, |
---|
3752 | eval: function (ctx) { |
---|
3753 | var val = this.value.eval(ctx), rootpath; |
---|
3754 | |
---|
3755 | // Add the base path if the URL is relative |
---|
3756 | if (typeof val.value === "string" && !/^(?:[a-z-]+:|\/)/.test(val.value)) { |
---|
3757 | rootpath = this.rootpath; |
---|
3758 | if (!val.quote) { |
---|
3759 | rootpath = rootpath.replace(/[\(\)'"\s]/g, function(match) { return "\\"+match; }); |
---|
3760 | } |
---|
3761 | val.value = rootpath + val.value; |
---|
3762 | } |
---|
3763 | |
---|
3764 | return new(tree.URL)(val, this.rootpath); |
---|
3765 | } |
---|
3766 | }; |
---|
3767 | |
---|
3768 | })(require('../tree')); |
---|
3769 | (function (tree) { |
---|
3770 | |
---|
3771 | tree.Value = function (value) { |
---|
3772 | this.value = value; |
---|
3773 | this.is = 'value'; |
---|
3774 | }; |
---|
3775 | tree.Value.prototype = { |
---|
3776 | eval: function (env) { |
---|
3777 | if (this.value.length === 1) { |
---|
3778 | return this.value[0].eval(env); |
---|
3779 | } else { |
---|
3780 | return new(tree.Value)(this.value.map(function (v) { |
---|
3781 | return v.eval(env); |
---|
3782 | })); |
---|
3783 | } |
---|
3784 | }, |
---|
3785 | toCSS: function (env) { |
---|
3786 | return this.value.map(function (e) { |
---|
3787 | return e.toCSS(env); |
---|
3788 | }).join(env.compress ? ',' : ', '); |
---|
3789 | } |
---|
3790 | }; |
---|
3791 | |
---|
3792 | })(require('../tree')); |
---|
3793 | (function (tree) { |
---|
3794 | |
---|
3795 | tree.Variable = function (name, index, file) { this.name = name, this.index = index, this.file = file }; |
---|
3796 | tree.Variable.prototype = { |
---|
3797 | eval: function (env) { |
---|
3798 | var variable, v, name = this.name; |
---|
3799 | |
---|
3800 | if (name.indexOf('@@') == 0) { |
---|
3801 | name = '@' + new(tree.Variable)(name.slice(1)).eval(env).value; |
---|
3802 | } |
---|
3803 | |
---|
3804 | if (this.evaluating) { |
---|
3805 | throw { type: 'Name', |
---|
3806 | message: "Recursive variable definition for " + name, |
---|
3807 | filename: this.file, |
---|
3808 | index: this.index }; |
---|
3809 | } |
---|
3810 | |
---|
3811 | this.evaluating = true; |
---|
3812 | |
---|
3813 | if (variable = tree.find(env.frames, function (frame) { |
---|
3814 | if (v = frame.variable(name)) { |
---|
3815 | return v.value.eval(env); |
---|
3816 | } |
---|
3817 | })) { |
---|
3818 | this.evaluating = false; |
---|
3819 | return variable; |
---|
3820 | } |
---|
3821 | else { |
---|
3822 | throw { type: 'Name', |
---|
3823 | message: "variable " + name + " is undefined", |
---|
3824 | filename: this.file, |
---|
3825 | index: this.index }; |
---|
3826 | } |
---|
3827 | } |
---|
3828 | }; |
---|
3829 | |
---|
3830 | })(require('../tree')); |
---|
3831 | (function (tree) { |
---|
3832 | |
---|
3833 | tree.debugInfo = function(env, ctx) { |
---|
3834 | var result=""; |
---|
3835 | if (env.dumpLineNumbers && !env.compress) { |
---|
3836 | switch(env.dumpLineNumbers) { |
---|
3837 | case 'comments': |
---|
3838 | result = tree.debugInfo.asComment(ctx); |
---|
3839 | break; |
---|
3840 | case 'mediaquery': |
---|
3841 | result = tree.debugInfo.asMediaQuery(ctx); |
---|
3842 | break; |
---|
3843 | case 'all': |
---|
3844 | result = tree.debugInfo.asComment(ctx)+tree.debugInfo.asMediaQuery(ctx); |
---|
3845 | break; |
---|
3846 | } |
---|
3847 | } |
---|
3848 | return result; |
---|
3849 | }; |
---|
3850 | |
---|
3851 | tree.debugInfo.asComment = function(ctx) { |
---|
3852 | return '/* line ' + ctx.debugInfo.lineNumber + ', ' + ctx.debugInfo.fileName + ' */\n'; |
---|
3853 | }; |
---|
3854 | |
---|
3855 | tree.debugInfo.asMediaQuery = function(ctx) { |
---|
3856 | return '@media -sass-debug-info{filename{font-family:' + |
---|
3857 | ('file://' + ctx.debugInfo.fileName).replace(/[\/:.]/g, '\\$&') + |
---|
3858 | '}line{font-family:\\00003' + ctx.debugInfo.lineNumber + '}}\n'; |
---|
3859 | }; |
---|
3860 | |
---|
3861 | tree.find = function (obj, fun) { |
---|
3862 | for (var i = 0, r; i < obj.length; i++) { |
---|
3863 | if (r = fun.call(obj, obj[i])) { return r } |
---|
3864 | } |
---|
3865 | return null; |
---|
3866 | }; |
---|
3867 | tree.jsify = function (obj) { |
---|
3868 | if (Array.isArray(obj.value) && (obj.value.length > 1)) { |
---|
3869 | return '[' + obj.value.map(function (v) { return v.toCSS(false) }).join(', ') + ']'; |
---|
3870 | } else { |
---|
3871 | return obj.toCSS(false); |
---|
3872 | } |
---|
3873 | }; |
---|
3874 | |
---|
3875 | })(require('./tree')); |
---|
3876 | // |
---|
3877 | // browser.js - client-side engine |
---|
3878 | // |
---|
3879 | |
---|
3880 | var isFileProtocol = /^(file|chrome(-extension)?|resource|qrc|app):/.test(location.protocol); |
---|
3881 | |
---|
3882 | less.env = less.env || (location.hostname == '127.0.0.1' || |
---|
3883 | location.hostname == '0.0.0.0' || |
---|
3884 | location.hostname == 'localhost' || |
---|
3885 | location.port.length > 0 || |
---|
3886 | isFileProtocol ? 'development' |
---|
3887 | : 'production'); |
---|
3888 | |
---|
3889 | // Load styles asynchronously (default: false) |
---|
3890 | // |
---|
3891 | // This is set to `false` by default, so that the body |
---|
3892 | // doesn't start loading before the stylesheets are parsed. |
---|
3893 | // Setting this to `true` can result in flickering. |
---|
3894 | // |
---|
3895 | less.async = less.async || false; |
---|
3896 | less.fileAsync = less.fileAsync || false; |
---|
3897 | |
---|
3898 | // Interval between watch polls |
---|
3899 | less.poll = less.poll || (isFileProtocol ? 1000 : 1500); |
---|
3900 | |
---|
3901 | //Setup user functions |
---|
3902 | if (less.functions) { |
---|
3903 | for(var func in less.functions) { |
---|
3904 | less.tree.functions[func] = less.functions[func]; |
---|
3905 | } |
---|
3906 | } |
---|
3907 | |
---|
3908 | var dumpLineNumbers = /!dumpLineNumbers:(comments|mediaquery|all)/.exec(location.hash); |
---|
3909 | if (dumpLineNumbers) { |
---|
3910 | less.dumpLineNumbers = dumpLineNumbers[1]; |
---|
3911 | } |
---|
3912 | |
---|
3913 | // |
---|
3914 | // Watch mode |
---|
3915 | // |
---|
3916 | less.watch = function () { |
---|
3917 | if (!less.watchMode ){ |
---|
3918 | less.env = 'development'; |
---|
3919 | initRunningMode(); |
---|
3920 | } |
---|
3921 | return this.watchMode = true |
---|
3922 | }; |
---|
3923 | |
---|
3924 | less.unwatch = function () {clearInterval(less.watchTimer); return this.watchMode = false; }; |
---|
3925 | |
---|
3926 | function initRunningMode(){ |
---|
3927 | if (less.env === 'development') { |
---|
3928 | less.optimization = 0; |
---|
3929 | less.watchTimer = setInterval(function () { |
---|
3930 | if (less.watchMode) { |
---|
3931 | loadStyleSheets(function (e, root, _, sheet, env) { |
---|
3932 | if (root) { |
---|
3933 | createCSS(root.toCSS(), sheet, env.lastModified); |
---|
3934 | } |
---|
3935 | }); |
---|
3936 | } |
---|
3937 | }, less.poll); |
---|
3938 | } else { |
---|
3939 | less.optimization = 3; |
---|
3940 | } |
---|
3941 | } |
---|
3942 | |
---|
3943 | if (/!watch/.test(location.hash)) { |
---|
3944 | less.watch(); |
---|
3945 | } |
---|
3946 | |
---|
3947 | var cache = null; |
---|
3948 | |
---|
3949 | if (less.env != 'development') { |
---|
3950 | try { |
---|
3951 | cache = (typeof(window.localStorage) === 'undefined') ? null : window.localStorage; |
---|
3952 | } catch (_) {} |
---|
3953 | } |
---|
3954 | |
---|
3955 | // |
---|
3956 | // Get all <link> tags with the 'rel' attribute set to "stylesheet/less" |
---|
3957 | // |
---|
3958 | var links = document.getElementsByTagName('link'); |
---|
3959 | var typePattern = /^text\/(x-)?less$/; |
---|
3960 | |
---|
3961 | less.sheets = []; |
---|
3962 | |
---|
3963 | for (var i = 0; i < links.length; i++) { |
---|
3964 | if (links[i].rel === 'stylesheet/less' || (links[i].rel.match(/stylesheet/) && |
---|
3965 | (links[i].type.match(typePattern)))) { |
---|
3966 | less.sheets.push(links[i]); |
---|
3967 | } |
---|
3968 | } |
---|
3969 | |
---|
3970 | // |
---|
3971 | // With this function, it's possible to alter variables and re-render |
---|
3972 | // CSS without reloading less-files |
---|
3973 | // |
---|
3974 | var session_cache = ''; |
---|
3975 | less.modifyVars = function(record) { |
---|
3976 | var str = session_cache; |
---|
3977 | for (name in record) { |
---|
3978 | str += ((name.slice(0,1) === '@')? '' : '@') + name +': '+ |
---|
3979 | ((record[name].slice(-1) === ';')? record[name] : record[name] +';'); |
---|
3980 | } |
---|
3981 | new(less.Parser)().parse(str, function (e, root) { |
---|
3982 | createCSS(root.toCSS(), less.sheets[less.sheets.length - 1]); |
---|
3983 | }); |
---|
3984 | }; |
---|
3985 | |
---|
3986 | less.refresh = function (reload) { |
---|
3987 | var startTime, endTime; |
---|
3988 | startTime = endTime = new(Date); |
---|
3989 | |
---|
3990 | loadStyleSheets(function (e, root, _, sheet, env) { |
---|
3991 | if (env.local) { |
---|
3992 | log("loading " + sheet.href + " from cache."); |
---|
3993 | } else { |
---|
3994 | log("parsed " + sheet.href + " successfully."); |
---|
3995 | createCSS(root.toCSS(), sheet, env.lastModified); |
---|
3996 | } |
---|
3997 | log("css for " + sheet.href + " generated in " + (new(Date) - endTime) + 'ms'); |
---|
3998 | (env.remaining === 0) && log("css generated in " + (new(Date) - startTime) + 'ms'); |
---|
3999 | endTime = new(Date); |
---|
4000 | }, reload); |
---|
4001 | |
---|
4002 | loadStyles(); |
---|
4003 | }; |
---|
4004 | less.refreshStyles = loadStyles; |
---|
4005 | |
---|
4006 | less.refresh(less.env === 'development'); |
---|
4007 | |
---|
4008 | function loadStyles() { |
---|
4009 | var styles = document.getElementsByTagName('style'); |
---|
4010 | for (var i = 0; i < styles.length; i++) { |
---|
4011 | if (styles[i].type.match(typePattern)) { |
---|
4012 | new(less.Parser)({ |
---|
4013 | filename: document.location.href.replace(/#.*$/, ''), |
---|
4014 | dumpLineNumbers: less.dumpLineNumbers |
---|
4015 | }).parse(styles[i].innerHTML || '', function (e, tree) { |
---|
4016 | var css = tree.toCSS(); |
---|
4017 | var style = styles[i]; |
---|
4018 | style.type = 'text/css'; |
---|
4019 | if (style.styleSheet) { |
---|
4020 | style.styleSheet.cssText = css; |
---|
4021 | } else { |
---|
4022 | style.innerHTML = css; |
---|
4023 | } |
---|
4024 | }); |
---|
4025 | } |
---|
4026 | } |
---|
4027 | } |
---|
4028 | |
---|
4029 | function loadStyleSheets(callback, reload) { |
---|
4030 | for (var i = 0; i < less.sheets.length; i++) { |
---|
4031 | loadStyleSheet(less.sheets[i], callback, reload, less.sheets.length - (i + 1)); |
---|
4032 | } |
---|
4033 | } |
---|
4034 | |
---|
4035 | function pathDiff(url, baseUrl) { |
---|
4036 | // diff between two paths to create a relative path |
---|
4037 | |
---|
4038 | var urlParts = extractUrlParts(url), |
---|
4039 | baseUrlParts = extractUrlParts(baseUrl), |
---|
4040 | i, max, urlDirectories, baseUrlDirectories, diff = ""; |
---|
4041 | if (urlParts.hostPart !== baseUrlParts.hostPart) { |
---|
4042 | return ""; |
---|
4043 | } |
---|
4044 | max = Math.max(baseUrlParts.directories.length, urlParts.directories.length); |
---|
4045 | for(i = 0; i < max; i++) { |
---|
4046 | if (baseUrlParts.directories[i] !== urlParts.directories[i]) { break; } |
---|
4047 | } |
---|
4048 | baseUrlDirectories = baseUrlParts.directories.slice(i); |
---|
4049 | urlDirectories = urlParts.directories.slice(i); |
---|
4050 | for(i = 0; i < baseUrlDirectories.length-1; i++) { |
---|
4051 | diff += "../"; |
---|
4052 | } |
---|
4053 | for(i = 0; i < urlDirectories.length-1; i++) { |
---|
4054 | diff += urlDirectories[i] + "/"; |
---|
4055 | } |
---|
4056 | return diff; |
---|
4057 | } |
---|
4058 | |
---|
4059 | function extractUrlParts(url, baseUrl) { |
---|
4060 | // urlParts[1] = protocol&hostname || / |
---|
4061 | // urlParts[2] = / if path relative to host base |
---|
4062 | // urlParts[3] = directories |
---|
4063 | // urlParts[4] = filename |
---|
4064 | // urlParts[5] = parameters |
---|
4065 | |
---|
4066 | var urlPartsRegex = /^((?:[a-z-]+:)?\/\/(?:[^\/\?#]+\/)|([\/\\]))?((?:[^\/\\\?#]+[\/\\])*)([^\/\\\?#]*)([#\?].*)?$/, |
---|
4067 | urlParts = url.match(urlPartsRegex), |
---|
4068 | returner = {}, directories = [], i, baseUrlParts; |
---|
4069 | |
---|
4070 | if (!urlParts) { |
---|
4071 | throw new Error("Could not parse sheet href - '"+url+"'"); |
---|
4072 | } |
---|
4073 | |
---|
4074 | // Stylesheets in IE don't always return the full path |
---|
4075 | if (!urlParts[1] || urlParts[2]) { |
---|
4076 | baseUrlParts = baseUrl.match(urlPartsRegex); |
---|
4077 | if (!baseUrlParts) { |
---|
4078 | throw new Error("Could not parse page url - '"+baseUrl+"'"); |
---|
4079 | } |
---|
4080 | urlParts[1] = baseUrlParts[1]; |
---|
4081 | if (!urlParts[2]) { |
---|
4082 | urlParts[3] = baseUrlParts[3] + urlParts[3]; |
---|
4083 | } |
---|
4084 | } |
---|
4085 | |
---|
4086 | if (urlParts[3]) { |
---|
4087 | directories = urlParts[3].replace("\\", "/").split("/"); |
---|
4088 | |
---|
4089 | for(i = 0; i < directories.length; i++) { |
---|
4090 | if (directories[i] === ".." && i > 0) { |
---|
4091 | directories.splice(i-1, 2); |
---|
4092 | i -= 2; |
---|
4093 | } |
---|
4094 | } |
---|
4095 | } |
---|
4096 | |
---|
4097 | returner.hostPart = urlParts[1]; |
---|
4098 | returner.directories = directories; |
---|
4099 | returner.path = urlParts[1] + directories.join("/"); |
---|
4100 | returner.fileUrl = returner.path + (urlParts[4] || ""); |
---|
4101 | returner.url = returner.fileUrl + (urlParts[5] || ""); |
---|
4102 | return returner; |
---|
4103 | } |
---|
4104 | |
---|
4105 | function loadStyleSheet(sheet, callback, reload, remaining) { |
---|
4106 | // sheet may be set to the stylesheet for the initial load or a collection of properties including |
---|
4107 | // some env variables for imports |
---|
4108 | var contents = sheet.contents || {}; |
---|
4109 | var files = sheet.files || {}; |
---|
4110 | var hrefParts = extractUrlParts(sheet.href, window.location.href); |
---|
4111 | var href = hrefParts.url; |
---|
4112 | var css = cache && cache.getItem(href); |
---|
4113 | var timestamp = cache && cache.getItem(href + ':timestamp'); |
---|
4114 | var styles = { css: css, timestamp: timestamp }; |
---|
4115 | var rootpath; |
---|
4116 | |
---|
4117 | if (less.relativeUrls) { |
---|
4118 | if (less.rootpath) { |
---|
4119 | if (sheet.entryPath) { |
---|
4120 | rootpath = extractUrlParts(less.rootpath + pathDiff(hrefParts.path, sheet.entryPath)).path; |
---|
4121 | } else { |
---|
4122 | rootpath = less.rootpath; |
---|
4123 | } |
---|
4124 | } else { |
---|
4125 | rootpath = hrefParts.path; |
---|
4126 | } |
---|
4127 | } else { |
---|
4128 | if (less.rootpath) { |
---|
4129 | rootpath = less.rootpath; |
---|
4130 | } else { |
---|
4131 | if (sheet.entryPath) { |
---|
4132 | rootpath = sheet.entryPath; |
---|
4133 | } else { |
---|
4134 | rootpath = hrefParts.path; |
---|
4135 | } |
---|
4136 | } |
---|
4137 | } |
---|
4138 | |
---|
4139 | xhr(href, sheet.type, function (data, lastModified) { |
---|
4140 | // Store data this session |
---|
4141 | session_cache += data.replace(/@import .+?;/ig, ''); |
---|
4142 | |
---|
4143 | if (!reload && styles && lastModified && |
---|
4144 | (new(Date)(lastModified).valueOf() === |
---|
4145 | new(Date)(styles.timestamp).valueOf())) { |
---|
4146 | // Use local copy |
---|
4147 | createCSS(styles.css, sheet); |
---|
4148 | callback(null, null, data, sheet, { local: true, remaining: remaining }, href); |
---|
4149 | } else { |
---|
4150 | // Use remote copy (re-parse) |
---|
4151 | try { |
---|
4152 | contents[href] = data; // Updating top importing parser content cache |
---|
4153 | new(less.Parser)({ |
---|
4154 | optimization: less.optimization, |
---|
4155 | paths: [hrefParts.path], |
---|
4156 | entryPath: sheet.entryPath || hrefParts.path, |
---|
4157 | mime: sheet.type, |
---|
4158 | filename: href, |
---|
4159 | rootpath: rootpath, |
---|
4160 | relativeUrls: sheet.relativeUrls, |
---|
4161 | contents: contents, // Passing top importing parser content cache ref down. |
---|
4162 | files: files, |
---|
4163 | dumpLineNumbers: less.dumpLineNumbers |
---|
4164 | }).parse(data, function (e, root) { |
---|
4165 | if (e) { return error(e, href) } |
---|
4166 | try { |
---|
4167 | callback(e, root, data, sheet, { local: false, lastModified: lastModified, remaining: remaining }, href); |
---|
4168 | removeNode(document.getElementById('less-error-message:' + extractId(href))); |
---|
4169 | } catch (e) { |
---|
4170 | error(e, href); |
---|
4171 | } |
---|
4172 | }); |
---|
4173 | } catch (e) { |
---|
4174 | error(e, href); |
---|
4175 | } |
---|
4176 | } |
---|
4177 | }, function (status, url) { |
---|
4178 | throw new(Error)("Couldn't load " + url + " (" + status + ")"); |
---|
4179 | }); |
---|
4180 | } |
---|
4181 | |
---|
4182 | function extractId(href) { |
---|
4183 | return href.replace(/^[a-z]+:\/\/?[^\/]+/, '' ) // Remove protocol & domain |
---|
4184 | .replace(/^\//, '' ) // Remove root / |
---|
4185 | .replace(/\.[a-zA-Z]+$/, '' ) // Remove simple extension |
---|
4186 | .replace(/[^\.\w-]+/g, '-') // Replace illegal characters |
---|
4187 | .replace(/\./g, ':'); // Replace dots with colons(for valid id) |
---|
4188 | } |
---|
4189 | |
---|
4190 | function createCSS(styles, sheet, lastModified) { |
---|
4191 | var css; |
---|
4192 | |
---|
4193 | // Strip the query-string |
---|
4194 | var href = sheet.href || ''; |
---|
4195 | |
---|
4196 | // If there is no title set, use the filename, minus the extension |
---|
4197 | var id = 'less:' + (sheet.title || extractId(href)); |
---|
4198 | |
---|
4199 | // If the stylesheet doesn't exist, create a new node |
---|
4200 | if ((css = document.getElementById(id)) === null) { |
---|
4201 | css = document.createElement('style'); |
---|
4202 | css.type = 'text/css'; |
---|
4203 | if( sheet.media ){ css.media = sheet.media; } |
---|
4204 | css.id = id; |
---|
4205 | var nextEl = sheet && sheet.nextSibling || null; |
---|
4206 | (nextEl || document.getElementsByTagName('head')[0]).parentNode.insertBefore(css, nextEl); |
---|
4207 | } |
---|
4208 | |
---|
4209 | if (css.styleSheet) { // IE |
---|
4210 | try { |
---|
4211 | css.styleSheet.cssText = styles; |
---|
4212 | } catch (e) { |
---|
4213 | throw new(Error)("Couldn't reassign styleSheet.cssText."); |
---|
4214 | } |
---|
4215 | } else { |
---|
4216 | (function (node) { |
---|
4217 | if (css.childNodes.length > 0) { |
---|
4218 | if (css.firstChild.nodeValue !== node.nodeValue) { |
---|
4219 | css.replaceChild(node, css.firstChild); |
---|
4220 | } |
---|
4221 | } else { |
---|
4222 | css.appendChild(node); |
---|
4223 | } |
---|
4224 | })(document.createTextNode(styles)); |
---|
4225 | } |
---|
4226 | |
---|
4227 | // Don't update the local store if the file wasn't modified |
---|
4228 | if (lastModified && cache) { |
---|
4229 | log('saving ' + href + ' to cache.'); |
---|
4230 | try { |
---|
4231 | cache.setItem(href, styles); |
---|
4232 | cache.setItem(href + ':timestamp', lastModified); |
---|
4233 | } catch(e) { |
---|
4234 | //TODO - could do with adding more robust error handling |
---|
4235 | log('failed to save'); |
---|
4236 | } |
---|
4237 | } |
---|
4238 | } |
---|
4239 | |
---|
4240 | function xhr(url, type, callback, errback) { |
---|
4241 | var xhr = getXMLHttpRequest(); |
---|
4242 | var async = isFileProtocol ? less.fileAsync : less.async; |
---|
4243 | |
---|
4244 | if (typeof(xhr.overrideMimeType) === 'function') { |
---|
4245 | xhr.overrideMimeType('text/css'); |
---|
4246 | } |
---|
4247 | xhr.open('GET', url, async); |
---|
4248 | xhr.setRequestHeader('Accept', type || 'text/x-less, text/css; q=0.9, */*; q=0.5'); |
---|
4249 | xhr.send(null); |
---|
4250 | |
---|
4251 | if (isFileProtocol && !less.fileAsync) { |
---|
4252 | if (xhr.status === 0 || (xhr.status >= 200 && xhr.status < 300)) { |
---|
4253 | callback(xhr.responseText); |
---|
4254 | } else { |
---|
4255 | errback(xhr.status, url); |
---|
4256 | } |
---|
4257 | } else if (async) { |
---|
4258 | xhr.onreadystatechange = function () { |
---|
4259 | if (xhr.readyState == 4) { |
---|
4260 | handleResponse(xhr, callback, errback); |
---|
4261 | } |
---|
4262 | }; |
---|
4263 | } else { |
---|
4264 | handleResponse(xhr, callback, errback); |
---|
4265 | } |
---|
4266 | |
---|
4267 | function handleResponse(xhr, callback, errback) { |
---|
4268 | if (xhr.status >= 200 && xhr.status < 300) { |
---|
4269 | callback(xhr.responseText, |
---|
4270 | xhr.getResponseHeader("Last-Modified")); |
---|
4271 | } else if (typeof(errback) === 'function') { |
---|
4272 | errback(xhr.status, url); |
---|
4273 | } |
---|
4274 | } |
---|
4275 | } |
---|
4276 | |
---|
4277 | function getXMLHttpRequest() { |
---|
4278 | if (window.XMLHttpRequest) { |
---|
4279 | return new(XMLHttpRequest); |
---|
4280 | } else { |
---|
4281 | try { |
---|
4282 | return new(ActiveXObject)("MSXML2.XMLHTTP.3.0"); |
---|
4283 | } catch (e) { |
---|
4284 | log("browser doesn't support AJAX."); |
---|
4285 | return null; |
---|
4286 | } |
---|
4287 | } |
---|
4288 | } |
---|
4289 | |
---|
4290 | function removeNode(node) { |
---|
4291 | return node && node.parentNode.removeChild(node); |
---|
4292 | } |
---|
4293 | |
---|
4294 | function log(str) { |
---|
4295 | if (less.env == 'development' && typeof(console) !== "undefined") { console.log('less: ' + str) } |
---|
4296 | } |
---|
4297 | |
---|
4298 | function error(e, href) { |
---|
4299 | var id = 'less-error-message:' + extractId(href); |
---|
4300 | var template = '<li><label>{line}</label><pre class="{class}">{content}</pre></li>'; |
---|
4301 | var elem = document.createElement('div'), timer, content, error = []; |
---|
4302 | var filename = e.filename || href; |
---|
4303 | var filenameNoPath = filename.match(/([^\/]+(\?.*)?)$/)[1]; |
---|
4304 | |
---|
4305 | elem.id = id; |
---|
4306 | elem.className = "less-error-message"; |
---|
4307 | |
---|
4308 | content = '<h3>' + (e.message || 'There is an error in your .less file') + |
---|
4309 | '</h3>' + '<p>in <a href="' + filename + '">' + filenameNoPath + "</a> "; |
---|
4310 | |
---|
4311 | var errorline = function (e, i, classname) { |
---|
4312 | if (e.extract[i]) { |
---|
4313 | error.push(template.replace(/\{line\}/, parseInt(e.line) + (i - 1)) |
---|
4314 | .replace(/\{class\}/, classname) |
---|
4315 | .replace(/\{content\}/, e.extract[i])); |
---|
4316 | } |
---|
4317 | }; |
---|
4318 | |
---|
4319 | if (e.stack) { |
---|
4320 | content += '<br/>' + e.stack.split('\n').slice(1).join('<br/>'); |
---|
4321 | } else if (e.extract) { |
---|
4322 | errorline(e, 0, ''); |
---|
4323 | errorline(e, 1, 'line'); |
---|
4324 | errorline(e, 2, ''); |
---|
4325 | content += 'on line ' + e.line + ', column ' + (e.column + 1) + ':</p>' + |
---|
4326 | '<ul>' + error.join('') + '</ul>'; |
---|
4327 | } |
---|
4328 | elem.innerHTML = content; |
---|
4329 | |
---|
4330 | // CSS for error messages |
---|
4331 | createCSS([ |
---|
4332 | '.less-error-message ul, .less-error-message li {', |
---|
4333 | 'list-style-type: none;', |
---|
4334 | 'margin-right: 15px;', |
---|
4335 | 'padding: 4px 0;', |
---|
4336 | 'margin: 0;', |
---|
4337 | '}', |
---|
4338 | '.less-error-message label {', |
---|
4339 | 'font-size: 12px;', |
---|
4340 | 'margin-right: 15px;', |
---|
4341 | 'padding: 4px 0;', |
---|
4342 | 'color: #cc7777;', |
---|
4343 | '}', |
---|
4344 | '.less-error-message pre {', |
---|
4345 | 'color: #dd6666;', |
---|
4346 | 'padding: 4px 0;', |
---|
4347 | 'margin: 0;', |
---|
4348 | 'display: inline-block;', |
---|
4349 | '}', |
---|
4350 | '.less-error-message pre.line {', |
---|
4351 | 'color: #ff0000;', |
---|
4352 | '}', |
---|
4353 | '.less-error-message h3 {', |
---|
4354 | 'font-size: 20px;', |
---|
4355 | 'font-weight: bold;', |
---|
4356 | 'padding: 15px 0 5px 0;', |
---|
4357 | 'margin: 0;', |
---|
4358 | '}', |
---|
4359 | '.less-error-message a {', |
---|
4360 | 'color: #10a', |
---|
4361 | '}', |
---|
4362 | '.less-error-message .error {', |
---|
4363 | 'color: red;', |
---|
4364 | 'font-weight: bold;', |
---|
4365 | 'padding-bottom: 2px;', |
---|
4366 | 'border-bottom: 1px dashed red;', |
---|
4367 | '}' |
---|
4368 | ].join('\n'), { title: 'error-message' }); |
---|
4369 | |
---|
4370 | elem.style.cssText = [ |
---|
4371 | "font-family: Arial, sans-serif", |
---|
4372 | "border: 1px solid #e00", |
---|
4373 | "background-color: #eee", |
---|
4374 | "border-radius: 5px", |
---|
4375 | "-webkit-border-radius: 5px", |
---|
4376 | "-moz-border-radius: 5px", |
---|
4377 | "color: #e00", |
---|
4378 | "padding: 15px", |
---|
4379 | "margin-bottom: 15px" |
---|
4380 | ].join(';'); |
---|
4381 | |
---|
4382 | if (less.env == 'development') { |
---|
4383 | timer = setInterval(function () { |
---|
4384 | if (document.body) { |
---|
4385 | if (document.getElementById(id)) { |
---|
4386 | document.body.replaceChild(elem, document.getElementById(id)); |
---|
4387 | } else { |
---|
4388 | document.body.insertBefore(elem, document.body.firstChild); |
---|
4389 | } |
---|
4390 | clearInterval(timer); |
---|
4391 | } |
---|
4392 | }, 10); |
---|
4393 | } |
---|
4394 | } |
---|
4395 | // amd.js |
---|
4396 | // |
---|
4397 | // Define Less as an AMD module. |
---|
4398 | if (typeof define === "function" && define.amd) { |
---|
4399 | define("less", [], function () { return less; } ); |
---|
4400 | } |
---|
4401 | })(window); |
---|