2 * LESS - Leaner CSS v1.5.0
5 * Copyright (c) 2009-2013, Alexis Sellier <self@cloudhead.net>
6 * Licensed under the Apache v2 License.
13 (function (window, undefined) {//
14 // Stub out `require` in the browser
16 function require(arg) {
17 return window.less[arg.split('/')[1]];
21 if (typeof(window.less) === 'undefined' || typeof(window.less.nodeType) !== 'undefined') { window.less = {}; }
23 tree = window.less.tree = {};
24 less.mode = 'browser';
28 // Node.js does not have a header file added which defines less
29 if (less === undefined) {
31 tree = require('./tree');
37 // A relatively straight-forward predictive parser.
38 // There is no tokenization/lexing stage, the input is parsed
41 // To make the parser fast enough to run in the browser, several
42 // optimization had to be made:
44 // - Matching and slicing on a huge input is often cause of slowdowns.
45 // The solution is to chunkify the input into smaller strings.
46 // The chunks are stored in the `chunks` var,
47 // `j` holds the current chunk index, and `current` holds
48 // the index of the current chunk in relation to `input`.
49 // This gives us an almost 4x speed-up.
51 // - In many cases, we don't need to match individual tokens;
52 // for example, if a value doesn't hold any variables, operations
53 // or dynamic references, the parser can effectively 'skip' it,
54 // treating it as a literal.
55 // An example would be '1px solid #000' - which evaluates to itself,
56 // we don't need to know what the individual components are.
57 // The drawback, of course is that you don't get the benefits of
58 // syntax-checking on the CSS. This gives us a 50% speed-up in the parser,
59 // and a smaller speed-up in the code-gen.
62 // Token matching is done with the `$` function, which either takes
63 // a terminal string or regexp, or a non-terminal function to call.
64 // It also takes care of moving all the indices forwards.
67 less.Parser = function Parser(env) {
68 var input, // LeSS input string
69 i, // current index in `input`
71 temp, // temporarily holds a chunk's state, for backtracking
72 memo, // temporarily holds `i`, when backtracking
73 furthest, // furthest index the parser has gone to
74 chunks, // chunkified input
75 current, // index of current chunk, in `input`
77 rootFilename = env && env.filename;
79 // Top parser on an import tree must be sure there is one "env"
80 // which will then be passed around by reference.
81 if (!(env instanceof tree.parseEnv)) {
82 env = new tree.parseEnv(env);
85 var imports = this.imports = {
86 paths: env.paths || [], // Search paths, when importing
87 queue: [], // Files which haven't been imported yet
88 files: env.files, // Holds the imported parse trees
89 contents: env.contents, // Holds the imported file contents
90 mime: env.mime, // MIME type of .less files
91 error: null, // Error in parsing/evaluating an import
92 push: function (path, currentFileInfo, importOptions, callback) {
93 var parserImports = this;
94 this.queue.push(path);
96 var fileParsedFunc = function (e, root, fullPath) {
97 parserImports.queue.splice(parserImports.queue.indexOf(path), 1); // Remove the path from the queue
99 var importedPreviously = fullPath in parserImports.files || fullPath === rootFilename;
101 parserImports.files[fullPath] = root; // Store the root
103 if (e && !parserImports.error) { parserImports.error = e; }
105 callback(e, root, importedPreviously, fullPath);
108 if (less.Parser.importer) {
109 less.Parser.importer(path, currentFileInfo, fileParsedFunc, env);
111 less.Parser.fileLoader(path, currentFileInfo, function(e, contents, fullPath, newFileInfo) {
112 if (e) {fileParsedFunc(e); return;}
114 var newEnv = new tree.parseEnv(env);
116 newEnv.currentFileInfo = newFileInfo;
117 newEnv.processImports = false;
118 newEnv.contents[fullPath] = contents;
120 if (currentFileInfo.reference || importOptions.reference) {
121 newFileInfo.reference = true;
124 if (importOptions.inline) {
125 fileParsedFunc(null, contents, fullPath);
127 new(less.Parser)(newEnv).parse(contents, function (e, root) {
128 fileParsedFunc(e, root, fullPath);
136 function save() { temp = chunks[j], memo = i, current = i; }
137 function restore() { chunks[j] = temp, i = memo, current = i; }
141 chunks[j] = chunks[j].slice(i - current);
145 function isWhitespace(c) {
146 // Could change to \s?
147 var code = c.charCodeAt(0);
148 return code === 32 || code === 10 || code === 9;
151 // Parse from a token, regexp or string, and move forward if match
159 if (tok instanceof Function) {
160 return tok.call(parser.parsers);
164 // Either match a single character in the input,
165 // or match a regexp in the current chunk (chunk[j]).
167 } else if (typeof(tok) === 'string') {
168 match = input.charAt(i) === tok ? tok : null;
174 if (match = tok.exec(chunks[j])) {
175 length = match[0].length;
181 // The match is confirmed, add the match length to `i`,
182 // and consume any extra white-space characters (' ' || '\n')
183 // which come after that. The reason for this is that LeSS's
184 // grammar is mostly white-space insensitive.
187 skipWhitespace(length);
189 if(typeof(match) === 'string') {
192 return match.length === 1 ? match[0] : match;
197 function skipWhitespace(length) {
198 var oldi = i, oldj = j,
199 endIndex = i + chunks[j].length,
202 while (i < endIndex) {
203 if (! isWhitespace(input.charAt(i))) { break; }
206 chunks[j] = chunks[j].slice(length + (i - mem));
209 if (chunks[j].length === 0 && j < chunks.length - 1) { j++; }
211 return oldi !== i || oldj !== j;
214 function expect(arg, msg) {
217 error(msg || (typeof(arg) === 'string' ? "expected '" + arg + "' got '" + input.charAt(i) + "'"
218 : "unexpected token"));
224 function error(msg, type) {
225 var e = new Error(msg);
227 e.type = type || 'Syntax';
231 // Same as $(), but don't change the state of the parser,
232 // just return the match.
234 if (typeof(tok) === 'string') {
235 return input.charAt(i) === tok;
237 return tok.test(chunks[j]);
241 function getInput(e, env) {
242 if (e.filename && env.currentFileInfo.filename && (e.filename !== env.currentFileInfo.filename)) {
243 return parser.imports.contents[e.filename];
249 function getLocation(index, inputStream) {
254 while (--n >= 0 && inputStream.charAt(n) !== '\n') {
258 if (typeof index === 'number') {
259 line = (inputStream.slice(0, index).match(/\n/g) || "").length;
268 function getDebugInfo(index, inputStream, env) {
269 var filename = env.currentFileInfo.filename;
270 if(less.mode !== 'browser' && less.mode !== 'rhino') {
271 filename = require('path').resolve(filename);
275 lineNumber: getLocation(index, inputStream).line + 1,
280 function LessError(e, env) {
281 var input = getInput(e, env),
282 loc = getLocation(e.index, input),
285 callLine = e.call && getLocation(e.call, input).line,
286 lines = input.split('\n');
288 this.type = e.type || 'Syntax';
289 this.message = e.message;
290 this.filename = e.filename || env.currentFileInfo.filename;
291 this.index = e.index;
292 this.line = typeof(line) === 'number' ? line + 1 : null;
293 this.callLine = callLine + 1;
294 this.callExtract = lines[callLine];
295 this.stack = e.stack;
304 LessError.prototype = new Error();
305 LessError.prototype.constructor = LessError;
307 this.env = env = env || {};
309 // The optimization level dictates the thoroughness of the parser,
310 // the lower the number, the less nodes it will create in the tree.
311 // This could matter for debugging, or if you want to access
312 // the individual nodes in the tree.
313 this.optimization = ('optimization' in this.env) ? this.env.optimization : 1;
322 // Parse an input string into an abstract syntax tree,
323 // call `callback` when done.
325 parse: function (str, callback) {
326 var root, line, lines, error = null;
328 i = j = current = furthest = 0;
329 input = str.replace(/\r\n/g, '\n');
331 // Remove potential UTF Byte Order Mark
332 input = input.replace(/^\uFEFF/, '');
334 parser.imports.contents[env.currentFileInfo.filename] = input;
336 // Split the input into chunks.
337 chunks = (function (chunks) {
339 skip = /(?:@\{[\w-]+\}|[^"'`\{\}\/\(\)\\])+/g,
340 comment = /\/\*(?:[^*]|\*+[^\/*])*\*+\/|\/\/.*/g,
341 string = /"((?:[^"\\\r\n]|\\.)*)"|'((?:[^'\\\r\n]|\\.)*)'|`((?:[^`]|\\.)*)`/g,
347 for (var i = 0, c, cc; i < input.length;) {
349 if (match = skip.exec(input)) {
350 if (match.index === i) {
351 i += match[0].length;
352 chunk.push(match[0]);
356 comment.lastIndex = string.lastIndex = i;
358 if (match = string.exec(input)) {
359 if (match.index === i) {
360 i += match[0].length;
361 chunk.push(match[0]);
366 if (!inParam && c === '/') {
367 cc = input.charAt(i + 1);
368 if (cc === '/' || cc === '*') {
369 if (match = comment.exec(input)) {
370 if (match.index === i) {
371 i += match[0].length;
372 chunk.push(match[0]);
391 chunks[++j] = chunk = [];
416 error = new(LessError)({
419 message: (level > 0) ? "missing closing `}`" : "missing opening `{`",
420 filename: env.currentFileInfo.filename
424 return chunks.map(function (c) { return c.join(''); });
428 return callback(new(LessError)(error, env));
431 // Start with the primary rule.
432 // The whole syntax tree is held under a Ruleset node,
433 // with the `root` property set to true, so no `{}` are
434 // output. The callback is called when the input is parsed.
436 root = new(tree.Ruleset)([], $(this.parsers.primary));
438 root.firstRoot = true;
440 return callback(new(LessError)(e, env));
443 root.toCSS = (function (evaluate) {
444 return function (options, variables) {
445 options = options || {};
448 evalEnv = new tree.evalEnv(options);
451 // Allows setting variables with a hash, so:
453 // `{ color: new(tree.Color)('#f01') }` will become:
455 // new(tree.Rule)('@color',
457 // new(tree.Expression)([
458 // new(tree.Color)('#f01')
463 if (typeof(variables) === 'object' && !Array.isArray(variables)) {
464 variables = Object.keys(variables).map(function (k) {
465 var value = variables[k];
467 if (! (value instanceof tree.Value)) {
468 if (! (value instanceof tree.Expression)) {
469 value = new(tree.Expression)([value]);
471 value = new(tree.Value)([value]);
473 return new(tree.Rule)('@' + k, value, false, null, 0);
475 evalEnv.frames = [new(tree.Ruleset)(null, variables)];
479 evaldRoot = evaluate.call(this, evalEnv);
481 new(tree.joinSelectorVisitor)()
484 new(tree.processExtendsVisitor)()
487 new(tree.toCSSVisitor)({compress: Boolean(options.compress)})
490 if (options.sourceMap) {
491 evaldRoot = new tree.sourceMapOutput(
493 writeSourceMap: options.writeSourceMap,
495 contentsMap: parser.imports.contents,
496 sourceMapFilename: options.sourceMapFilename,
497 outputFilename: options.sourceMapOutputFilename,
498 sourceMapBasepath: options.sourceMapBasepath,
499 sourceMapRootpath: options.sourceMapRootpath,
500 outputSourceFiles: options.outputSourceFiles,
501 sourceMapGenerator: options.sourceMapGenerator
505 css = evaldRoot.toCSS({
506 compress: Boolean(options.compress),
507 dumpLineNumbers: env.dumpLineNumbers,
508 strictUnits: Boolean(options.strictUnits)});
510 throw new(LessError)(e, env);
513 if (options.cleancss && less.mode === 'node') {
514 var CleanCSS = require('clean-css');
515 //TODO would be nice for no advanced to be an option
516 return new CleanCSS({keepSpecialComments: '*', processImport: false, noRebase: true, noAdvanced: true}).minify(css);
517 } else if (options.compress) {
518 return css.replace(/(^(\s)+)|((\s)+$)/g, "");
525 // If `i` is smaller than the `input.length - 1`,
526 // it means the parser wasn't able to parse the whole
527 // string, so we've got a parsing error.
529 // We try to extract a \n delimited string,
530 // showing the line where the parse error occured.
531 // We split it up into two parts (the part which parsed,
532 // and the part which didn't), so we can color them differently.
533 if (i < input.length - 1) {
535 var loc = getLocation(i, input);
536 lines = input.split('\n');
541 message: "Unrecognised input",
543 filename: env.currentFileInfo.filename,
554 var finish = function (e) {
555 e = error || e || parser.imports.error;
558 if (!(e instanceof LessError)) {
559 e = new(LessError)(e, env);
565 return callback(null, root);
569 if (env.processImports !== false) {
570 new tree.importVisitor(this.imports, finish)
578 // Here in, the parsing rules/functions
580 // The basic structure of the syntax tree generated is as follows:
582 // Ruleset -> Rule -> Value -> Expression -> Entity
584 // Here's some LESS code:
588 // border: 1px solid #000;
593 // And here's what the parse tree might look like:
595 // Ruleset (Selector '.class', [
596 // Rule ("color", Value ([Expression [Color #fff]]))
597 // Rule ("border", Value ([Expression [Dimension 1px][Keyword "solid"][Color #000]]))
598 // Rule ("width", Value ([Expression [Operation "+" [Variable "@w"][Dimension 4px]]]))
599 // Ruleset (Selector [Element '>', '.child'], [...])
602 // In general, most rules will try to parse a token with the `$()` function, and if the return
603 // value is truly, will return a new node, of the relevant type. Sometimes, we need to check
604 // first, before parsing, that's when we use `peek()`.
608 // The `primary` rule is the *entry* and *exit* point of the parser.
609 // The rules here can appear at any level of the parse tree.
611 // The recursive nature of the grammar is an interplay between the `block`
612 // rule, which represents `{ ... }`, the `ruleset` rule, and this `primary` rule,
613 // as represented by this simplified grammar:
615 // primary → (ruleset | rule)+
616 // ruleset → selector+ block
617 // block → '{' primary '}'
619 // Only at one point is the primary rule not called from the
620 // block rule: at the root level.
622 primary: function () {
625 while ((node = $(this.extendRule) || $(this.mixin.definition) || $(this.rule) || $(this.ruleset) ||
626 $(this.mixin.call) || $(this.comment) || $(this.directive))
627 || $(/^[\s\n]+/) || $(/^;+/)) {
628 node && root.push(node);
633 // We create a Comment node for CSS comments `/* */`,
634 // but keep the LeSS comments `//` silent, by just skipping
636 comment: function () {
639 if (input.charAt(i) !== '/') { return; }
641 if (input.charAt(i + 1) === '/') {
642 return new(tree.Comment)($(/^\/\/.*/), true, i, env.currentFileInfo);
643 } else if (comment = $(/^\/\*(?:[^*]|\*+[^\/*])*\*+\/\n?/)) {
644 return new(tree.Comment)(comment, false, i, env.currentFileInfo);
648 comments: function () {
649 var comment, comments = [];
651 while(comment = $(this.comment)) {
652 comments.push(comment);
659 // Entities are tokens which can be found inside an Expression
663 // A string, which supports escaping " and '
665 // "milky way" 'he\'s the one!'
667 quoted: function () {
668 var str, j = i, e, index = i;
670 if (input.charAt(j) === '~') { j++, e = true; } // Escaped strings
671 if (input.charAt(j) !== '"' && input.charAt(j) !== "'") { return; }
675 if (str = $(/^"((?:[^"\\\r\n]|\\.)*)"|'((?:[^'\\\r\n]|\\.)*)'/)) {
676 return new(tree.Quoted)(str[0], str[1] || str[2], e, index, env.currentFileInfo);
681 // A catch-all word, such as:
683 // black border-collapse
685 keyword: function () {
688 if (k = $(/^[_A-Za-z-][_A-Za-z0-9-]*/)) {
689 var color = tree.Color.fromKeyword(k);
693 return new(tree.Keyword)(k);
702 // We also try to catch IE's `alpha()`, but let the `alpha` parser
703 // deal with the details.
705 // The arguments are parsed with the `entities.arguments` parser.
708 var name, nameLC, args, alpha_ret, index = i;
710 if (! (name = /^([\w-]+|%|progid:[\w\.]+)\(/.exec(chunks[j]))) { return; }
713 nameLC = name.toLowerCase();
715 if (nameLC === 'url') { return null; }
716 else { i += name.length; }
718 if (nameLC === 'alpha') {
719 alpha_ret = $(this.alpha);
720 if(typeof alpha_ret !== 'undefined') {
725 $('('); // Parse the '(' and consume whitespace.
727 args = $(this.entities.arguments);
733 if (name) { return new(tree.Call)(name, args, index, env.currentFileInfo); }
735 arguments: function () {
738 while (arg = $(this.entities.assignment) || $(this.expression)) {
746 literal: function () {
747 return $(this.entities.dimension) ||
748 $(this.entities.color) ||
749 $(this.entities.quoted) ||
750 $(this.entities.unicodeDescriptor);
753 // Assignments are argument entities for calls.
754 // They are present in ie filter properties as shown below.
756 // filter: progid:DXImageTransform.Microsoft.Alpha( *opacity=50* )
759 assignment: function () {
761 if ((key = $(/^\w+(?=\s?=)/i)) && $('=') && (value = $(this.entity))) {
762 return new(tree.Assignment)(key, value);
767 // Parse url() tokens
769 // We use a specific rule for urls, because they don't really behave like
770 // standard function calls. The difference is that the argument doesn't have
771 // to be enclosed within a string, so it can't be parsed as an Expression.
776 if (input.charAt(i) !== 'u' || !$(/^url\(/)) {
780 value = $(this.entities.quoted) || $(this.entities.variable) ||
781 $(/^(?:(?:\\[\(\)'"])|[^\(\)'"])+/) || "";
785 /*jshint eqnull:true */
786 return new(tree.URL)((value.value != null || value instanceof tree.Variable)
787 ? value : new(tree.Anonymous)(value), env.currentFileInfo);
791 // A Variable entity, such as `@fink`, in
793 // width: @fink + 2px
795 // We use a different parser for variable definitions,
796 // see `parsers.variable`.
798 variable: function () {
801 if (input.charAt(i) === '@' && (name = $(/^@@?[\w-]+/))) {
802 return new(tree.Variable)(name, index, env.currentFileInfo);
806 // A variable entity useing the protective {} e.g. @{var}
807 variableCurly: function () {
808 var curly, index = i;
810 if (input.charAt(i) === '@' && (curly = $(/^@\{([\w-]+)\}/))) {
811 return new(tree.Variable)("@" + curly[1], index, env.currentFileInfo);
816 // A Hexadecimal color
820 // `rgb` and `hsl` colors are parsed through the `entities.call` parser.
825 if (input.charAt(i) === '#' && (rgb = $(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})/))) {
826 return new(tree.Color)(rgb[1]);
831 // A Dimension, that is, a number and a unit
835 dimension: function () {
836 var value, c = input.charCodeAt(i);
837 //Is the first char of the dimension 0-9, '.', '+' or '-'
838 if ((c > 57 || c < 43) || c === 47 || c == 44) {
842 if (value = $(/^([+-]?\d*\.?\d+)(%|[a-z]+)?/)) {
843 return new(tree.Dimension)(value[1], value[2]);
848 // A unicode descriptor, as is used in unicode-range
850 // U+0?? or U+00A1-00A9
852 unicodeDescriptor: function () {
855 if (ud = $(/^U\+[0-9a-fA-F?]+(\-[0-9a-fA-F?]+)?/)) {
856 return new(tree.UnicodeDescriptor)(ud[0]);
861 // JavaScript code to be evaluated
863 // `window.location.href`
865 javascript: function () {
868 if (input.charAt(j) === '~') { j++; e = true; } // Escaped strings
869 if (input.charAt(j) !== '`') { return; }
870 if (env.javascriptEnabled !== undefined && !env.javascriptEnabled) {
871 error("You are using JavaScript, which has been disabled.");
876 if (str = $(/^`([^`]*)`/)) {
877 return new(tree.JavaScript)(str[1], i, e);
883 // The variable part of a variable definition. Used in the `rule` parser
887 variable: function () {
890 if (input.charAt(i) === '@' && (name = $(/^(@[\w-]+)\s*:/))) { return name[1]; }
894 // extend syntax - used to extend selectors
896 extend: function(isRule) {
897 var elements, e, index = i, option, extendList = [];
899 if (!$(isRule ? /^&:extend\(/ : /^:extend\(/)) { return; }
905 option = $(/^(all)(?=\s*(\)|,))/);
906 if (option) { break; }
912 option = option && option[1];
914 extendList.push(new(tree.Extend)(new(tree.Selector)(elements), option, index));
928 // extendRule - used in a rule to extend all the parent selectors
930 extendRule: function() {
931 return this.extend(true);
939 // A Mixin call, with an optional argument list
941 // #mixins > .square(#fff);
942 // .rounded(4px, black);
945 // The `while` loop is there because mixins can be
946 // namespaced, but we only support the child and descendant
950 var elements = [], e, c, args, index = i, s = input.charAt(i), important = false;
952 if (s !== '.' && s !== '#') { return; }
954 save(); // stop us absorbing part of an invalid selector
956 while (e = $(/^[#.](?:[\w-]|\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+/)) {
957 elements.push(new(tree.Element)(c, e, i, env.currentFileInfo));
961 args = this.mixin.args.call(this, true).args;
967 if ($(this.important)) {
971 if (elements.length > 0 && ($(';') || peek('}'))) {
972 return new(tree.mixin.Call)(elements, args, index, env.currentFileInfo, important);
977 args: function (isCall) {
978 var expressions = [], argsSemiColon = [], isSemiColonSeperated, argsComma = [], expressionContainsNamed, name, nameLoop, value, arg,
979 returner = {args:null, variadic: false};
982 arg = $(this.expression);
985 if (input.charAt(i) === '.' && $(/^\.{3}/)) {
986 returner.variadic = true;
987 if ($(";") && !isSemiColonSeperated) {
988 isSemiColonSeperated = true;
990 (isSemiColonSeperated ? argsSemiColon : argsComma)
991 .push({ variadic: true });
994 arg = $(this.entities.variable) || $(this.entities.literal)
995 || $(this.entities.keyword);
1003 if (arg.throwAwayComments) {
1004 arg.throwAwayComments();
1011 if (arg.value.length == 1) {
1018 if (val && val instanceof tree.Variable) {
1020 if (expressions.length > 0) {
1021 if (isSemiColonSeperated) {
1022 error("Cannot mix ; and , as delimiter types");
1024 expressionContainsNamed = true;
1026 value = expect(this.expression);
1027 nameLoop = (name = val.name);
1028 } else if (!isCall && $(/^\.{3}/)) {
1029 returner.variadic = true;
1030 if ($(";") && !isSemiColonSeperated) {
1031 isSemiColonSeperated = true;
1033 (isSemiColonSeperated ? argsSemiColon : argsComma)
1034 .push({ name: arg.name, variadic: true });
1036 } else if (!isCall) {
1037 name = nameLoop = val.name;
1043 expressions.push(value);
1046 argsComma.push({ name:nameLoop, value:value });
1052 if ($(';') || isSemiColonSeperated) {
1054 if (expressionContainsNamed) {
1055 error("Cannot mix ; and , as delimiter types");
1058 isSemiColonSeperated = true;
1060 if (expressions.length > 1) {
1061 value = new(tree.Value)(expressions);
1063 argsSemiColon.push({ name:name, value:value });
1067 expressionContainsNamed = false;
1071 returner.args = isSemiColonSeperated ? argsSemiColon : argsComma;
1075 // A Mixin definition, with a list of parameters
1077 // .rounded (@radius: 2px, @color) {
1081 // Until we have a finer grained state-machine, we have to
1082 // do a look-ahead, to make sure we don't have a mixin call.
1083 // See the `rule` function for more information.
1085 // We start by matching `.rounded (`, and then proceed on to
1086 // the argument list, which has optional default values.
1087 // We store the parameters in `params`, with a `value` key,
1088 // if there is a value, such as in the case of `@radius`.
1090 // Once we've got our params list, and a closing `)`, we parse
1091 // the `{...}` block.
1093 definition: function () {
1094 var name, params = [], match, ruleset, cond, variadic = false;
1095 if ((input.charAt(i) !== '.' && input.charAt(i) !== '#') ||
1102 if (match = $(/^([#.](?:[\w-]|\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+)\s*\(/)) {
1105 var argInfo = this.mixin.args.call(this, false);
1106 params = argInfo.args;
1107 variadic = argInfo.variadic;
1109 // .mixincall("@{a}");
1110 // looks a bit like a mixin definition.. so we have to be nice and restore
1118 if ($(/^when/)) { // Guard
1119 cond = expect(this.conditions, 'expected condition');
1122 ruleset = $(this.block);
1125 return new(tree.mixin.Definition)(name, params, ruleset, cond, variadic);
1134 // Entities are the smallest recognized token,
1135 // and can be found inside a rule's value.
1137 entity: function () {
1138 return $(this.entities.literal) || $(this.entities.variable) || $(this.entities.url) ||
1139 $(this.entities.call) || $(this.entities.keyword) ||$(this.entities.javascript) ||
1144 // A Rule terminator. Note that we use `peek()` to check for '}',
1145 // because the `block` rule will be expecting it, but we still need to make sure
1146 // it's there, if ';' was ommitted.
1149 return $(';') || peek('}');
1153 // IE's alpha function
1155 // alpha(opacity=88)
1157 alpha: function () {
1160 if (! $(/^\(opacity=/i)) { return; }
1161 if (value = $(/^\d+/) || $(this.entities.variable)) {
1163 return new(tree.Alpha)(value);
1168 // A Selector Element
1173 // input[type="text"]
1175 // Elements are the building blocks for Selectors,
1176 // they are made out of a `Combinator` (see combinator rule),
1177 // and an element name, such as a tag a class, or `*`.
1179 element: function () {
1182 c = $(this.combinator);
1184 e = $(/^(?:\d+\.\d+|\d+)%/) || $(/^(?:[.#]?|:*)(?:[\w-]|[^\x00-\x9f]|\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+/) ||
1185 $('*') || $('&') || $(this.attribute) || $(/^\([^()@]+\)/) || $(/^[\.#](?=@)/) || $(this.entities.variableCurly);
1189 if ((v = ($(this.selector))) &&
1191 e = new(tree.Paren)(v);
1196 if (e) { return new(tree.Element)(c, e, i, env.currentFileInfo); }
1200 // Combinators combine elements together, in a Selector.
1202 // Because our parser isn't white-space sensitive, special care
1203 // has to be taken, when parsing the descendant combinator, ` `,
1204 // as it's an empty space. We have to check the previous character
1205 // in the input, to see if it's a ` ` character. More info on how
1206 // we deal with this in *combinator.js*.
1208 combinator: function () {
1209 var c = input.charAt(i);
1211 if (c === '>' || c === '+' || c === '~' || c === '|') {
1213 while (input.charAt(i).match(/\s/)) { i++; }
1214 return new(tree.Combinator)(c);
1215 } else if (input.charAt(i - 1).match(/\s/)) {
1216 return new(tree.Combinator)(" ");
1218 return new(tree.Combinator)(null);
1222 // A CSS selector (see selector below)
1223 // with less extensions e.g. the ability to extend and guard
1225 lessSelector: function () {
1226 return this.selector(true);
1231 // .class > div + h1
1234 // Selectors are made out of one or more Elements, see above.
1236 selector: function (isLess) {
1237 var e, elements = [], c, extend, extendList = [], when, condition;
1239 while ((isLess && (extend = $(this.extend))) || (isLess && (when = $(/^when/))) || (e = $(this.element))) {
1241 condition = expect(this.conditions, 'expected condition');
1242 } else if (condition) {
1243 error("CSS guard can only be used at the end of selector");
1244 } else if (extend) {
1245 extendList.push.apply(extendList, extend);
1247 if (extendList.length) {
1248 error("Extend can only be used at the end of selector");
1250 c = input.charAt(i);
1254 if (c === '{' || c === '}' || c === ';' || c === ',' || c === ')') {
1259 if (elements.length > 0) { return new(tree.Selector)(elements, extendList, condition, i, env.currentFileInfo); }
1260 if (extendList.length) { error("Extend must be used to extend a selector, it cannot be used on its own"); }
1262 attribute: function () {
1265 if (! $('[')) { return; }
1267 if (!(key = $(this.entities.variableCurly))) {
1268 key = expect(/^(?:[_A-Za-z0-9-\*]*\|)?(?:[_A-Za-z0-9-]|\\.)+/);
1271 if ((op = $(/^[|~*$^]?=/))) {
1272 val = $(this.entities.quoted) || $(/^[0-9]+%/) || $(/^[\w-]+/) || $(this.entities.variableCurly);
1277 return new(tree.Attribute)(key, op, val);
1281 // The `block` rule is used by `ruleset` and `mixin.definition`.
1282 // It's a wrapper around the `primary` rule, with added `{}`.
1284 block: function () {
1286 if ($('{') && (content = $(this.primary)) && $('}')) {
1292 // div, .class, body > p {...}
1294 ruleset: function () {
1295 var selectors = [], s, rules, debugInfo;
1299 if (env.dumpLineNumbers) {
1300 debugInfo = getDebugInfo(i, input, env);
1303 while (s = $(this.lessSelector)) {
1306 if (! $(',')) { break; }
1308 error("Guards are only currently allowed on a single selector.");
1313 if (selectors.length > 0 && (rules = $(this.block))) {
1314 var ruleset = new(tree.Ruleset)(selectors, rules, env.strictImports);
1315 if (env.dumpLineNumbers) {
1316 ruleset.debugInfo = debugInfo;
1325 rule: function (tryAnonymous) {
1326 var name, value, c = input.charAt(i), important, merge = false;
1329 if (c === '.' || c === '#' || c === '&') { return; }
1331 if (name = $(this.variable) || $(this.ruleProperty)) {
1332 // prefer to try to parse first if its a variable or we are compressing
1333 // but always fallback on the other one
1334 value = !tryAnonymous && (env.compress || (name.charAt(0) === '@')) ?
1335 ($(this.value) || $(this.anonymousValue)) :
1336 ($(this.anonymousValue) || $(this.value));
1339 important = $(this.important);
1340 if (name[name.length-1] === "+") {
1342 name = name.substr(0, name.length - 1);
1345 if (value && $(this.end)) {
1346 return new (tree.Rule)(name, value, important, merge, memo, env.currentFileInfo);
1350 if (value && !tryAnonymous) {
1351 return this.rule(true);
1356 anonymousValue: function () {
1358 if (match = /^([^@+\/'"*`(;{}-]*);/.exec(chunks[j])) {
1359 i += match[0].length - 1;
1360 return new(tree.Anonymous)(match[1]);
1365 // An @import directive
1369 // Depending on our environemnt, importing is done differently:
1370 // In the browser, it's an XHR request, in Node, it would be a
1371 // file-system operation. The function used for importing is
1372 // stored in `import`, which we pass to the Import constructor.
1374 "import": function () {
1375 var path, features, index = i;
1379 var dir = $(/^@import?\s+/);
1381 var options = (dir ? $(this.importOptions) : null) || {};
1383 if (dir && (path = $(this.entities.quoted) || $(this.entities.url))) {
1384 features = $(this.mediaFeatures);
1386 features = features && new(tree.Value)(features);
1387 return new(tree.Import)(path, features, options, index, env.currentFileInfo);
1394 importOptions: function() {
1395 var o, options = {}, optionName, value;
1397 // list of options, surrounded by parens
1398 if (! $('(')) { return null; }
1400 if (o = $(this.importOption)) {
1403 switch(optionName) {
1405 optionName = "less";
1409 optionName = "multiple";
1413 options[optionName] = value;
1414 if (! $(',')) { break; }
1421 importOption: function() {
1422 var opt = $(/^(less|css|multiple|once|inline|reference)/);
1428 mediaFeature: function () {
1429 var e, p, nodes = [];
1432 if (e = ($(this.entities.keyword) || $(this.entities.variable))) {
1434 } else if ($('(')) {
1435 p = $(this.property);
1439 nodes.push(new(tree.Paren)(new(tree.Rule)(p, e, null, null, i, env.currentFileInfo, true)));
1441 nodes.push(new(tree.Paren)(e));
1445 } else { return null; }
1449 if (nodes.length > 0) {
1450 return new(tree.Expression)(nodes);
1454 mediaFeatures: function () {
1455 var e, features = [];
1458 if (e = $(this.mediaFeature)) {
1460 if (! $(',')) { break; }
1461 } else if (e = $(this.entities.variable)) {
1463 if (! $(',')) { break; }
1467 return features.length > 0 ? features : null;
1470 media: function () {
1471 var features, rules, media, debugInfo;
1473 if (env.dumpLineNumbers) {
1474 debugInfo = getDebugInfo(i, input, env);
1478 features = $(this.mediaFeatures);
1480 if (rules = $(this.block)) {
1481 media = new(tree.Media)(rules, features, i, env.currentFileInfo);
1482 if (env.dumpLineNumbers) {
1483 media.debugInfo = debugInfo;
1493 // @charset "utf-8";
1495 directive: function () {
1496 var name, value, rules, nonVendorSpecificName,
1497 hasBlock, hasIdentifier, hasExpression, identifier;
1499 if (input.charAt(i) !== '@') { return; }
1501 if (value = $(this['import']) || $(this.media)) {
1507 name = $(/^@[a-z-]+/);
1509 if (!name) { return; }
1511 nonVendorSpecificName = name;
1512 if (name.charAt(1) == '-' && name.indexOf('-', 2) > 0) {
1513 nonVendorSpecificName = "@" + name.slice(name.indexOf('-', 2) + 1);
1516 switch(nonVendorSpecificName) {
1522 case "@top-left-corner":
1525 case "@top-right-corner":
1526 case "@bottom-left":
1527 case "@bottom-left-corner":
1528 case "@bottom-center":
1529 case "@bottom-right":
1530 case "@bottom-right-corner":
1532 case "@left-middle":
1533 case "@left-bottom":
1535 case "@right-middle":
1536 case "@right-bottom":
1545 hasIdentifier = true;
1548 hasExpression = true;
1552 if (hasIdentifier) {
1553 identifier = ($(/^[^{]+/) || '').trim();
1555 name += " " + identifier;
1561 if (rules = $(this.block)) {
1562 return new(tree.Directive)(name, rules, i, env.currentFileInfo);
1565 if ((value = hasExpression ? $(this.expression) : $(this.entity)) && $(';')) {
1566 var directive = new(tree.Directive)(name, value, i, env.currentFileInfo);
1567 if (env.dumpLineNumbers) {
1568 directive.debugInfo = getDebugInfo(i, input, env);
1578 // A Value is a comma-delimited list of Expressions
1580 // font-family: Baskerville, Georgia, serif;
1582 // In a Rule, a Value represents everything after the `:`,
1583 // and before the `;`.
1585 value: function () {
1586 var e, expressions = [];
1588 while (e = $(this.expression)) {
1589 expressions.push(e);
1590 if (! $(',')) { break; }
1593 if (expressions.length > 0) {
1594 return new(tree.Value)(expressions);
1597 important: function () {
1598 if (input.charAt(i) === '!') {
1599 return $(/^! *important/);
1606 if (a = $(this.addition)) {
1607 e = new(tree.Expression)([a]);
1614 multiplication: function () {
1615 var m, a, op, operation, isSpaced;
1616 if (m = $(this.operand)) {
1617 isSpaced = isWhitespace(input.charAt(i - 1));
1618 while (!peek(/^\/[*\/]/) && (op = ($('/') || $('*')))) {
1619 if (a = $(this.operand)) {
1620 m.parensInOp = true;
1621 a.parensInOp = true;
1622 operation = new(tree.Operation)(op, [operation || m, a], isSpaced);
1623 isSpaced = isWhitespace(input.charAt(i - 1));
1628 return operation || m;
1631 addition: function () {
1632 var m, a, op, operation, isSpaced;
1633 if (m = $(this.multiplication)) {
1634 isSpaced = isWhitespace(input.charAt(i - 1));
1635 while ((op = $(/^[-+]\s+/) || (!isSpaced && ($('+') || $('-')))) &&
1636 (a = $(this.multiplication))) {
1637 m.parensInOp = true;
1638 a.parensInOp = true;
1639 operation = new(tree.Operation)(op, [operation || m, a], isSpaced);
1640 isSpaced = isWhitespace(input.charAt(i - 1));
1642 return operation || m;
1645 conditions: function () {
1646 var a, b, index = i, condition;
1648 if (a = $(this.condition)) {
1649 while (peek(/^,\s*(not\s*)?\(/) && $(',') && (b = $(this.condition))) {
1650 condition = new(tree.Condition)('or', condition || a, b, index);
1652 return condition || a;
1655 condition: function () {
1656 var a, b, c, op, index = i, negate = false;
1658 if ($(/^not/)) { negate = true; }
1660 if (a = $(this.addition) || $(this.entities.keyword) || $(this.entities.quoted)) {
1661 if (op = $(/^(?:>=|<=|=<|[<=>])/)) {
1662 if (b = $(this.addition) || $(this.entities.keyword) || $(this.entities.quoted)) {
1663 c = new(tree.Condition)(op, a, b, index, negate);
1665 error('expected expression');
1668 c = new(tree.Condition)('=', a, new(tree.Keyword)('true'), index, negate);
1671 return $(/^and/) ? new(tree.Condition)('and', c, $(this.condition)) : c;
1676 // An operand is anything that can be part of an operation,
1677 // such as a Color, or a Variable
1679 operand: function () {
1680 var negate, p = input.charAt(i + 1);
1682 if (input.charAt(i) === '-' && (p === '@' || p === '(')) { negate = $('-'); }
1683 var o = $(this.sub) || $(this.entities.dimension) ||
1684 $(this.entities.color) || $(this.entities.variable) ||
1685 $(this.entities.call);
1688 o.parensInOp = true;
1689 o = new(tree.Negative)(o);
1696 // Expressions either represent mathematical operations,
1697 // or white-space delimited Entities.
1702 expression: function () {
1703 var e, delim, entities = [];
1705 while (e = $(this.addition) || $(this.entity)) {
1707 // operations do not allow keyword "/" dimension (e.g. small/20px) so we support that here
1708 if (!peek(/^\/[\/*]/) && (delim = $('/'))) {
1709 entities.push(new(tree.Anonymous)(delim));
1712 if (entities.length > 0) {
1713 return new(tree.Expression)(entities);
1716 property: function () {
1719 if (name = $(/^(\*?-?[_a-zA-Z0-9-]+)\s*:/)) {
1723 ruleProperty: function () {
1726 if (name = $(/^(\*?-?[_a-zA-Z0-9-]+)\s*(\+?)\s*:/)) {
1727 return name[1] + (name[2] || "");
1738 rgb: function (r, g, b) {
1739 return this.rgba(r, g, b, 1.0);
1741 rgba: function (r, g, b, a) {
1742 var rgb = [r, g, b].map(function (c) { return scaled(c, 256); });
1744 return new(tree.Color)(rgb, a);
1746 hsl: function (h, s, l) {
1747 return this.hsla(h, s, l, 1.0);
1749 hsla: function (h, s, l, a) {
1751 h = h < 0 ? h + 1 : (h > 1 ? h - 1 : h);
1752 if (h * 6 < 1) { return m1 + (m2 - m1) * h * 6; }
1753 else if (h * 2 < 1) { return m2; }
1754 else if (h * 3 < 2) { return m1 + (m2 - m1) * (2/3 - h) * 6; }
1758 h = (number(h) % 360) / 360;
1759 s = clamp(number(s)); l = clamp(number(l)); a = clamp(number(a));
1761 var m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s;
1762 var m1 = l * 2 - m2;
1764 return this.rgba(hue(h + 1/3) * 255,
1770 hsv: function(h, s, v) {
1771 return this.hsva(h, s, v, 1.0);
1774 hsva: function(h, s, v, a) {
1775 h = ((number(h) % 360) / 360) * 360;
1776 s = number(s); v = number(v); a = number(a);
1779 i = Math.floor((h / 60) % 6);
1785 v * (1 - (1 - f) * s)];
1786 var perm = [[0, 3, 1],
1793 return this.rgba(vs[perm[i][0]] * 255,
1794 vs[perm[i][1]] * 255,
1795 vs[perm[i][2]] * 255,
1799 hue: function (color) {
1800 return new(tree.Dimension)(Math.round(color.toHSL().h));
1802 saturation: function (color) {
1803 return new(tree.Dimension)(Math.round(color.toHSL().s * 100), '%');
1805 lightness: function (color) {
1806 return new(tree.Dimension)(Math.round(color.toHSL().l * 100), '%');
1808 hsvhue: function(color) {
1809 return new(tree.Dimension)(Math.round(color.toHSV().h));
1811 hsvsaturation: function (color) {
1812 return new(tree.Dimension)(Math.round(color.toHSV().s * 100), '%');
1814 hsvvalue: function (color) {
1815 return new(tree.Dimension)(Math.round(color.toHSV().v * 100), '%');
1817 red: function (color) {
1818 return new(tree.Dimension)(color.rgb[0]);
1820 green: function (color) {
1821 return new(tree.Dimension)(color.rgb[1]);
1823 blue: function (color) {
1824 return new(tree.Dimension)(color.rgb[2]);
1826 alpha: function (color) {
1827 return new(tree.Dimension)(color.toHSL().a);
1829 luma: function (color) {
1830 return new(tree.Dimension)(Math.round(color.luma() * color.alpha * 100), '%');
1832 saturate: function (color, amount) {
1833 // filter: saturate(3.2);
1834 // should be kept as is, so check for color
1838 var hsl = color.toHSL();
1840 hsl.s += amount.value / 100;
1841 hsl.s = clamp(hsl.s);
1844 desaturate: function (color, amount) {
1845 var hsl = color.toHSL();
1847 hsl.s -= amount.value / 100;
1848 hsl.s = clamp(hsl.s);
1851 lighten: function (color, amount) {
1852 var hsl = color.toHSL();
1854 hsl.l += amount.value / 100;
1855 hsl.l = clamp(hsl.l);
1858 darken: function (color, amount) {
1859 var hsl = color.toHSL();
1861 hsl.l -= amount.value / 100;
1862 hsl.l = clamp(hsl.l);
1865 fadein: function (color, amount) {
1866 var hsl = color.toHSL();
1868 hsl.a += amount.value / 100;
1869 hsl.a = clamp(hsl.a);
1872 fadeout: function (color, amount) {
1873 var hsl = color.toHSL();
1875 hsl.a -= amount.value / 100;
1876 hsl.a = clamp(hsl.a);
1879 fade: function (color, amount) {
1880 var hsl = color.toHSL();
1882 hsl.a = amount.value / 100;
1883 hsl.a = clamp(hsl.a);
1886 spin: function (color, amount) {
1887 var hsl = color.toHSL();
1888 var hue = (hsl.h + amount.value) % 360;
1890 hsl.h = hue < 0 ? 360 + hue : hue;
1895 // Copyright (c) 2006-2009 Hampton Catlin, Nathan Weizenbaum, and Chris Eppstein
1896 // http://sass-lang.com
1898 mix: function (color1, color2, weight) {
1900 weight = new(tree.Dimension)(50);
1902 var p = weight.value / 100.0;
1904 var a = color1.toHSL().a - color2.toHSL().a;
1906 var w1 = (((w * a == -1) ? w : (w + a) / (1 + w * a)) + 1) / 2.0;
1909 var rgb = [color1.rgb[0] * w1 + color2.rgb[0] * w2,
1910 color1.rgb[1] * w1 + color2.rgb[1] * w2,
1911 color1.rgb[2] * w1 + color2.rgb[2] * w2];
1913 var alpha = color1.alpha * p + color2.alpha * (1 - p);
1915 return new(tree.Color)(rgb, alpha);
1917 greyscale: function (color) {
1918 return this.desaturate(color, new(tree.Dimension)(100));
1920 contrast: function (color, dark, light, threshold) {
1921 // filter: contrast(3.2);
1922 // should be kept as is, so check for color
1926 if (typeof light === 'undefined') {
1927 light = this.rgba(255, 255, 255, 1.0);
1929 if (typeof dark === 'undefined') {
1930 dark = this.rgba(0, 0, 0, 1.0);
1932 //Figure out which is actually light and dark!
1933 if (dark.luma() > light.luma()) {
1938 if (typeof threshold === 'undefined') {
1941 threshold = number(threshold);
1943 if ((color.luma() * color.alpha) < threshold) {
1950 return new(tree.Anonymous)(str instanceof tree.JavaScript ? str.evaluated : str);
1952 escape: function (str) {
1953 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"));
1955 '%': function (quoted /* arg, arg, ...*/) {
1956 var args = Array.prototype.slice.call(arguments, 1),
1959 for (var i = 0; i < args.length; i++) {
1960 /*jshint loopfunc:true */
1961 str = str.replace(/%[sda]/i, function(token) {
1962 var value = token.match(/s/i) ? args[i].value : args[i].toCSS();
1963 return token.match(/[A-Z]$/) ? encodeURIComponent(value) : value;
1966 str = str.replace(/%%/g, '%');
1967 return new(tree.Quoted)('"' + str + '"', str);
1969 unit: function (val, unit) {
1970 if(!(val instanceof tree.Dimension)) {
1971 throw { type: "Argument", message: "the first argument to unit must be a number" + (val instanceof tree.Operation ? ". Have you forgotten parenthesis?" : "") };
1973 return new(tree.Dimension)(val.value, unit ? unit.toCSS() : "");
1975 convert: function (val, unit) {
1976 return val.convertTo(unit.value);
1978 round: function (n, f) {
1979 var fraction = typeof(f) === "undefined" ? 0 : f.value;
1980 return this._math(function(num) { return num.toFixed(fraction); }, null, n);
1983 return new(tree.Dimension)(Math.PI);
1985 mod: function(a, b) {
1986 return new(tree.Dimension)(a.value % b.value, a.unit);
1988 pow: function(x, y) {
1989 if (typeof x === "number" && typeof y === "number") {
1990 x = new(tree.Dimension)(x);
1991 y = new(tree.Dimension)(y);
1992 } else if (!(x instanceof tree.Dimension) || !(y instanceof tree.Dimension)) {
1993 throw { type: "Argument", message: "arguments must be numbers" };
1996 return new(tree.Dimension)(Math.pow(x.value, y.value), x.unit);
1998 _math: function (fn, unit, n) {
1999 if (n instanceof tree.Dimension) {
2000 /*jshint eqnull:true */
2001 return new(tree.Dimension)(fn(parseFloat(n.value)), unit == null ? n.unit : unit);
2002 } else if (typeof(n) === 'number') {
2005 throw { type: "Argument", message: "argument must be a number" };
2008 _minmax: function (isMin, args) {
2009 args = Array.prototype.slice.call(args);
2010 switch(args.length) {
2011 case 0: throw { type: "Argument", message: "one or more arguments required" };
2012 case 1: return args[0];
2014 var i, j, current, currentUnified, referenceUnified, unit,
2015 order = [], // elems only contains original argument values.
2016 values = {}; // key is the unit.toString() for unified tree.Dimension values,
2017 // value is the index into the order array.
2018 for (i = 0; i < args.length; i++) {
2020 if (!(current instanceof tree.Dimension)) {
2021 order.push(current);
2024 currentUnified = current.unify();
2025 unit = currentUnified.unit.toString();
2027 if (j === undefined) {
2028 values[unit] = order.length;
2029 order.push(current);
2032 referenceUnified = order[j].unify();
2033 if ( isMin && currentUnified.value < referenceUnified.value ||
2034 !isMin && currentUnified.value > referenceUnified.value) {
2038 if (order.length == 1) {
2041 args = order.map(function (a) { return a.toCSS(this.env); })
2042 .join(this.env.compress ? "," : ", ");
2043 return new(tree.Anonymous)((isMin ? "min" : "max") + "(" + args + ")");
2046 return this._minmax(true, arguments);
2049 return this._minmax(false, arguments);
2051 argb: function (color) {
2052 return new(tree.Anonymous)(color.toARGB());
2055 percentage: function (n) {
2056 return new(tree.Dimension)(n.value * 100, '%');
2058 color: function (n) {
2059 if (n instanceof tree.Quoted) {
2060 var colorCandidate = n.value,
2062 returnColor = tree.Color.fromKeyword(colorCandidate);
2066 if (/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})/.test(colorCandidate)) {
2067 return new(tree.Color)(colorCandidate.slice(1));
2069 throw { type: "Argument", message: "argument must be a color keyword or 3/6 digit hex e.g. #FFF" };
2071 throw { type: "Argument", message: "argument must be a string" };
2074 iscolor: function (n) {
2075 return this._isa(n, tree.Color);
2077 isnumber: function (n) {
2078 return this._isa(n, tree.Dimension);
2080 isstring: function (n) {
2081 return this._isa(n, tree.Quoted);
2083 iskeyword: function (n) {
2084 return this._isa(n, tree.Keyword);
2086 isurl: function (n) {
2087 return this._isa(n, tree.URL);
2089 ispixel: function (n) {
2090 return this.isunit(n, 'px');
2092 ispercentage: function (n) {
2093 return this.isunit(n, '%');
2095 isem: function (n) {
2096 return this.isunit(n, 'em');
2098 isunit: function (n, unit) {
2099 return (n instanceof tree.Dimension) && n.unit.is(unit.value || unit) ? tree.True : tree.False;
2101 _isa: function (n, Type) {
2102 return (n instanceof Type) ? tree.True : tree.False;
2105 /* Blending modes */
2107 multiply: function(color1, color2) {
2108 var r = color1.rgb[0] * color2.rgb[0] / 255;
2109 var g = color1.rgb[1] * color2.rgb[1] / 255;
2110 var b = color1.rgb[2] * color2.rgb[2] / 255;
2111 return this.rgb(r, g, b);
2113 screen: function(color1, color2) {
2114 var r = 255 - (255 - color1.rgb[0]) * (255 - color2.rgb[0]) / 255;
2115 var g = 255 - (255 - color1.rgb[1]) * (255 - color2.rgb[1]) / 255;
2116 var b = 255 - (255 - color1.rgb[2]) * (255 - color2.rgb[2]) / 255;
2117 return this.rgb(r, g, b);
2119 overlay: function(color1, color2) {
2120 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;
2121 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;
2122 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;
2123 return this.rgb(r, g, b);
2125 softlight: function(color1, color2) {
2126 var t = color2.rgb[0] * color1.rgb[0] / 255;
2127 var r = t + color1.rgb[0] * (255 - (255 - color1.rgb[0]) * (255 - color2.rgb[0]) / 255 - t) / 255;
2128 t = color2.rgb[1] * color1.rgb[1] / 255;
2129 var g = t + color1.rgb[1] * (255 - (255 - color1.rgb[1]) * (255 - color2.rgb[1]) / 255 - t) / 255;
2130 t = color2.rgb[2] * color1.rgb[2] / 255;
2131 var b = t + color1.rgb[2] * (255 - (255 - color1.rgb[2]) * (255 - color2.rgb[2]) / 255 - t) / 255;
2132 return this.rgb(r, g, b);
2134 hardlight: function(color1, color2) {
2135 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;
2136 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;
2137 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;
2138 return this.rgb(r, g, b);
2140 difference: function(color1, color2) {
2141 var r = Math.abs(color1.rgb[0] - color2.rgb[0]);
2142 var g = Math.abs(color1.rgb[1] - color2.rgb[1]);
2143 var b = Math.abs(color1.rgb[2] - color2.rgb[2]);
2144 return this.rgb(r, g, b);
2146 exclusion: function(color1, color2) {
2147 var r = color1.rgb[0] + color2.rgb[0] * (255 - color1.rgb[0] - color1.rgb[0]) / 255;
2148 var g = color1.rgb[1] + color2.rgb[1] * (255 - color1.rgb[1] - color1.rgb[1]) / 255;
2149 var b = color1.rgb[2] + color2.rgb[2] * (255 - color1.rgb[2] - color1.rgb[2]) / 255;
2150 return this.rgb(r, g, b);
2152 average: function(color1, color2) {
2153 var r = (color1.rgb[0] + color2.rgb[0]) / 2;
2154 var g = (color1.rgb[1] + color2.rgb[1]) / 2;
2155 var b = (color1.rgb[2] + color2.rgb[2]) / 2;
2156 return this.rgb(r, g, b);
2158 negation: function(color1, color2) {
2159 var r = 255 - Math.abs(255 - color2.rgb[0] - color1.rgb[0]);
2160 var g = 255 - Math.abs(255 - color2.rgb[1] - color1.rgb[1]);
2161 var b = 255 - Math.abs(255 - color2.rgb[2] - color1.rgb[2]);
2162 return this.rgb(r, g, b);
2164 tint: function(color, amount) {
2165 return this.mix(this.rgb(255,255,255), color, amount);
2167 shade: function(color, amount) {
2168 return this.mix(this.rgb(0, 0, 0), color, amount);
2170 extract: function(values, index) {
2171 index = index.value - 1; // (1-based index)
2172 // handle non-array values as an array of length 1
2173 // return 'undefined' if index is invalid
2174 return Array.isArray(values.value)
2175 ? values.value[index] : Array(values)[index];
2177 length: function(values) {
2178 var n = Array.isArray(values.value) ? values.value.length : 1;
2179 return new tree.Dimension(n);
2182 "data-uri": function(mimetypeNode, filePathNode) {
2184 if (typeof window !== 'undefined') {
2185 return new tree.URL(filePathNode || mimetypeNode, this.currentFileInfo).eval(this.env);
2188 var mimetype = mimetypeNode.value;
2189 var filePath = (filePathNode && filePathNode.value);
2191 var fs = require("fs"),
2192 path = require("path"),
2195 if (arguments.length < 2) {
2196 filePath = mimetype;
2199 if (this.env.isPathRelative(filePath)) {
2200 if (this.currentFileInfo.relativeUrls) {
2201 filePath = path.join(this.currentFileInfo.currentDirectory, filePath);
2203 filePath = path.join(this.currentFileInfo.entryPath, filePath);
2207 // detect the mimetype if not given
2208 if (arguments.length < 2) {
2211 mime = require('mime');
2216 mimetype = mime.lookup(filePath);
2218 // use base 64 unless it's an ASCII or UTF-8 format
2219 var charset = mime.charsets.lookup(mimetype);
2220 useBase64 = ['US-ASCII', 'UTF-8'].indexOf(charset) < 0;
2221 if (useBase64) { mimetype += ';base64'; }
2224 useBase64 = /;base64$/.test(mimetype);
2227 var buf = fs.readFileSync(filePath);
2229 // IE8 cannot handle a data-uri larger than 32KB. If this is exceeded
2230 // and the --ieCompat flag is enabled, return a normal url() instead.
2231 var DATA_URI_MAX_KB = 32,
2232 fileSizeInKB = parseInt((buf.length / 1024), 10);
2233 if (fileSizeInKB >= DATA_URI_MAX_KB) {
2235 if (this.env.ieCompat !== false) {
2236 if (!this.env.silent) {
2237 console.warn("Skipped data-uri embedding of %s because its size (%dKB) exceeds IE8-safe %dKB!", filePath, fileSizeInKB, DATA_URI_MAX_KB);
2240 return new tree.URL(filePathNode || mimetypeNode, this.currentFileInfo).eval(this.env);
2244 buf = useBase64 ? buf.toString('base64')
2245 : encodeURIComponent(buf);
2247 var uri = "'data:" + mimetype + ',' + buf + "'";
2248 return new(tree.URL)(new(tree.Anonymous)(uri));
2251 "svg-gradient": function(direction) {
2253 function throwArgumentDescriptor() {
2254 throw { type: "Argument", message: "svg-gradient expects direction, start_color [start_position], [color position,]..., end_color [end_position]" };
2257 if (arguments.length < 3) {
2258 throwArgumentDescriptor();
2260 var stops = Array.prototype.slice.call(arguments, 1),
2261 gradientDirectionSvg,
2262 gradientType = "linear",
2263 rectangleDimension = 'x="0" y="0" width="1" height="1"',
2265 renderEnv = {compress: false},
2267 directionValue = direction.toCSS(renderEnv),
2268 i, color, position, positionValue, alpha;
2270 switch (directionValue) {
2272 gradientDirectionSvg = 'x1="0%" y1="0%" x2="0%" y2="100%"';
2275 gradientDirectionSvg = 'x1="0%" y1="0%" x2="100%" y2="0%"';
2277 case "to bottom right":
2278 gradientDirectionSvg = 'x1="0%" y1="0%" x2="100%" y2="100%"';
2280 case "to top right":
2281 gradientDirectionSvg = 'x1="0%" y1="100%" x2="100%" y2="0%"';
2284 case "ellipse at center":
2285 gradientType = "radial";
2286 gradientDirectionSvg = 'cx="50%" cy="50%" r="75%"';
2287 rectangleDimension = 'x="-50" y="-50" width="101" height="101"';
2290 throw { type: "Argument", message: "svg-gradient direction must be 'to bottom', 'to right', 'to bottom right', 'to top right' or 'ellipse at center'" };
2292 returner = '<?xml version="1.0" ?>' +
2293 '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="100%" height="100%" viewBox="0 0 1 1" preserveAspectRatio="none">' +
2294 '<' + gradientType + 'Gradient id="gradient" gradientUnits="userSpaceOnUse" ' + gradientDirectionSvg + '>';
2296 for (i = 0; i < stops.length; i+= 1) {
2297 if (stops[i].value) {
2298 color = stops[i].value[0];
2299 position = stops[i].value[1];
2302 position = undefined;
2305 if (!(color instanceof tree.Color) || (!((i === 0 || i+1 === stops.length) && position === undefined) && !(position instanceof tree.Dimension))) {
2306 throwArgumentDescriptor();
2308 positionValue = position ? position.toCSS(renderEnv) : i === 0 ? "0%" : "100%";
2309 alpha = color.alpha;
2310 returner += '<stop offset="' + positionValue + '" stop-color="' + color.toRGB() + '"' + (alpha < 1 ? ' stop-opacity="' + alpha + '"' : '') + '/>';
2312 returner += '</' + gradientType + 'Gradient>' +
2313 '<rect ' + rectangleDimension + ' fill="url(#gradient)" /></svg>';
2316 // only works in node, needs interface to what is supported in environment
2318 returner = new Buffer(returner).toString('base64');
2324 returner = "'data:image/svg+xml" + (useBase64 ? ";base64" : "") + "," + returner + "'";
2325 return new(tree.URL)(new(tree.Anonymous)(returner));
2329 // these static methods are used as a fallback when the optional 'mime' dependency is missing
2331 // this map is intentionally incomplete
2332 // if you want more, install 'mime' dep
2334 '.htm' : 'text/html',
2335 '.html': 'text/html',
2336 '.gif' : 'image/gif',
2337 '.jpg' : 'image/jpeg',
2338 '.jpeg': 'image/jpeg',
2339 '.png' : 'image/png'
2341 lookup: function (filepath) {
2342 var ext = require('path').extname(filepath),
2343 type = tree._mime._types[ext];
2344 if (type === undefined) {
2345 throw new Error('Optional dependency "mime" is required for ' + ext);
2350 lookup: function (type) {
2351 // assumes all text types are UTF-8
2352 return type && (/^text\//).test(type) ? 'UTF-8' : '';
2357 var mathFunctions = [{name:"ceil"}, {name:"floor"}, {name: "sqrt"}, {name:"abs"},
2358 {name:"tan", unit: ""}, {name:"sin", unit: ""}, {name:"cos", unit: ""},
2359 {name:"atan", unit: "rad"}, {name:"asin", unit: "rad"}, {name:"acos", unit: "rad"}],
2360 createMathFunction = function(name, unit) {
2361 return function(n) {
2362 /*jshint eqnull:true */
2366 return this._math(Math[name], unit, n);
2370 for(var i = 0; i < mathFunctions.length; i++) {
2371 tree.functions[mathFunctions[i].name] = createMathFunction(mathFunctions[i].name, mathFunctions[i].unit);
2374 function hsla(color) {
2375 return tree.functions.hsla(color.h, color.s, color.l, color.a);
2378 function scaled(n, size) {
2379 if (n instanceof tree.Dimension && n.unit.is('%')) {
2380 return parseFloat(n.value * size / 100);
2386 function number(n) {
2387 if (n instanceof tree.Dimension) {
2388 return parseFloat(n.unit.is('%') ? n.value / 100 : n.value);
2389 } else if (typeof(n) === 'number') {
2393 error: "RuntimeError",
2394 message: "color functions take numbers as parameters"
2399 function clamp(val) {
2400 return Math.min(1, Math.max(0, val));
2403 tree.functionCall = function(env, currentFileInfo) {
2405 this.currentFileInfo = currentFileInfo;
2408 tree.functionCall.prototype = tree.functions;
2410 })(require('./tree'));
2414 'aliceblue':'#f0f8ff',
2415 'antiquewhite':'#faebd7',
2417 'aquamarine':'#7fffd4',
2422 'blanchedalmond':'#ffebcd',
2424 'blueviolet':'#8a2be2',
2426 'burlywood':'#deb887',
2427 'cadetblue':'#5f9ea0',
2428 'chartreuse':'#7fff00',
2429 'chocolate':'#d2691e',
2431 'cornflowerblue':'#6495ed',
2432 'cornsilk':'#fff8dc',
2433 'crimson':'#dc143c',
2435 'darkblue':'#00008b',
2436 'darkcyan':'#008b8b',
2437 'darkgoldenrod':'#b8860b',
2438 'darkgray':'#a9a9a9',
2439 'darkgrey':'#a9a9a9',
2440 'darkgreen':'#006400',
2441 'darkkhaki':'#bdb76b',
2442 'darkmagenta':'#8b008b',
2443 'darkolivegreen':'#556b2f',
2444 'darkorange':'#ff8c00',
2445 'darkorchid':'#9932cc',
2446 'darkred':'#8b0000',
2447 'darksalmon':'#e9967a',
2448 'darkseagreen':'#8fbc8f',
2449 'darkslateblue':'#483d8b',
2450 'darkslategray':'#2f4f4f',
2451 'darkslategrey':'#2f4f4f',
2452 'darkturquoise':'#00ced1',
2453 'darkviolet':'#9400d3',
2454 'deeppink':'#ff1493',
2455 'deepskyblue':'#00bfff',
2456 'dimgray':'#696969',
2457 'dimgrey':'#696969',
2458 'dodgerblue':'#1e90ff',
2459 'firebrick':'#b22222',
2460 'floralwhite':'#fffaf0',
2461 'forestgreen':'#228b22',
2462 'fuchsia':'#ff00ff',
2463 'gainsboro':'#dcdcdc',
2464 'ghostwhite':'#f8f8ff',
2466 'goldenrod':'#daa520',
2470 'greenyellow':'#adff2f',
2471 'honeydew':'#f0fff0',
2472 'hotpink':'#ff69b4',
2473 'indianred':'#cd5c5c',
2477 'lavender':'#e6e6fa',
2478 'lavenderblush':'#fff0f5',
2479 'lawngreen':'#7cfc00',
2480 'lemonchiffon':'#fffacd',
2481 'lightblue':'#add8e6',
2482 'lightcoral':'#f08080',
2483 'lightcyan':'#e0ffff',
2484 'lightgoldenrodyellow':'#fafad2',
2485 'lightgray':'#d3d3d3',
2486 'lightgrey':'#d3d3d3',
2487 'lightgreen':'#90ee90',
2488 'lightpink':'#ffb6c1',
2489 'lightsalmon':'#ffa07a',
2490 'lightseagreen':'#20b2aa',
2491 'lightskyblue':'#87cefa',
2492 'lightslategray':'#778899',
2493 'lightslategrey':'#778899',
2494 'lightsteelblue':'#b0c4de',
2495 'lightyellow':'#ffffe0',
2497 'limegreen':'#32cd32',
2499 'magenta':'#ff00ff',
2501 'mediumaquamarine':'#66cdaa',
2502 'mediumblue':'#0000cd',
2503 'mediumorchid':'#ba55d3',
2504 'mediumpurple':'#9370d8',
2505 'mediumseagreen':'#3cb371',
2506 'mediumslateblue':'#7b68ee',
2507 'mediumspringgreen':'#00fa9a',
2508 'mediumturquoise':'#48d1cc',
2509 'mediumvioletred':'#c71585',
2510 'midnightblue':'#191970',
2511 'mintcream':'#f5fffa',
2512 'mistyrose':'#ffe4e1',
2513 'moccasin':'#ffe4b5',
2514 'navajowhite':'#ffdead',
2516 'oldlace':'#fdf5e6',
2518 'olivedrab':'#6b8e23',
2520 'orangered':'#ff4500',
2522 'palegoldenrod':'#eee8aa',
2523 'palegreen':'#98fb98',
2524 'paleturquoise':'#afeeee',
2525 'palevioletred':'#d87093',
2526 'papayawhip':'#ffefd5',
2527 'peachpuff':'#ffdab9',
2531 'powderblue':'#b0e0e6',
2534 'rosybrown':'#bc8f8f',
2535 'royalblue':'#4169e1',
2536 'saddlebrown':'#8b4513',
2538 'sandybrown':'#f4a460',
2539 'seagreen':'#2e8b57',
2540 'seashell':'#fff5ee',
2543 'skyblue':'#87ceeb',
2544 'slateblue':'#6a5acd',
2545 'slategray':'#708090',
2546 'slategrey':'#708090',
2548 'springgreen':'#00ff7f',
2549 'steelblue':'#4682b4',
2552 'thistle':'#d8bfd8',
2554 'turquoise':'#40e0d0',
2558 'whitesmoke':'#f5f5f5',
2560 'yellowgreen':'#9acd32'
2562 })(require('./tree'));
2566 tree.debugInfo = function(env, ctx, lineSeperator) {
2568 if (env.dumpLineNumbers && !env.compress) {
2569 switch(env.dumpLineNumbers) {
2571 result = tree.debugInfo.asComment(ctx);
2574 result = tree.debugInfo.asMediaQuery(ctx);
2577 result = tree.debugInfo.asComment(ctx) + (lineSeperator || "") + tree.debugInfo.asMediaQuery(ctx);
2584 tree.debugInfo.asComment = function(ctx) {
2585 return '/* line ' + ctx.debugInfo.lineNumber + ', ' + ctx.debugInfo.fileName + ' */\n';
2588 tree.debugInfo.asMediaQuery = function(ctx) {
2589 return '@media -sass-debug-info{filename{font-family:' +
2590 ('file://' + ctx.debugInfo.fileName).replace(/([.:/\\])/g, function (a) {
2596 '}line{font-family:\\00003' + ctx.debugInfo.lineNumber + '}}\n';
2599 tree.find = function (obj, fun) {
2600 for (var i = 0, r; i < obj.length; i++) {
2601 if (r = fun.call(obj, obj[i])) { return r; }
2606 tree.jsify = function (obj) {
2607 if (Array.isArray(obj.value) && (obj.value.length > 1)) {
2608 return '[' + obj.value.map(function (v) { return v.toCSS(false); }).join(', ') + ']';
2610 return obj.toCSS(false);
2614 tree.toCSS = function (env) {
2617 add: function(chunk, fileInfo, index) {
2620 isEmpty: function () {
2621 return strs.length === 0;
2624 return strs.join('');
2627 tree.outputRuleset = function (env, output, rules) {
2628 output.add((env.compress ? '{' : ' {\n'));
2629 env.tabLevel = (env.tabLevel || 0) + 1;
2630 var tabRuleStr = env.compress ? '' : Array(env.tabLevel + 1).join(" "),
2631 tabSetStr = env.compress ? '' : Array(env.tabLevel).join(" ");
2632 for(var i = 0; i < rules.length; i++) {
2633 output.add(tabRuleStr);
2634 rules[i].genCSS(env, output);
2635 output.add(env.compress ? '' : '\n');
2638 output.add(tabSetStr + "}");
2641 })(require('./tree'));
2645 tree.Alpha = function (val) {
2648 tree.Alpha.prototype = {
2650 accept: function (visitor) {
2651 this.value = visitor.visit(this.value);
2653 eval: function (env) {
2654 if (this.value.eval) { return new tree.Alpha(this.value.eval(env)); }
2657 genCSS: function (env, output) {
2658 output.add("alpha(opacity=");
2660 if (this.value.genCSS) {
2661 this.value.genCSS(env, output);
2663 output.add(this.value);
2671 })(require('../tree'));
2675 tree.Anonymous = function (string, index, currentFileInfo, mapLines) {
2676 this.value = string.value || string;
2678 this.mapLines = mapLines;
2679 this.currentFileInfo = currentFileInfo;
2681 tree.Anonymous.prototype = {
2683 eval: function () { return this; },
2684 compare: function (x) {
2689 var left = this.toCSS(),
2692 if (left === right) {
2696 return left < right ? -1 : 1;
2698 genCSS: function (env, output) {
2699 output.add(this.value, this.currentFileInfo, this.index, this.mapLines);
2704 })(require('../tree'));
2708 tree.Assignment = function (key, val) {
2712 tree.Assignment.prototype = {
2714 accept: function (visitor) {
2715 this.value = visitor.visit(this.value);
2717 eval: function (env) {
2718 if (this.value.eval) {
2719 return new(tree.Assignment)(this.key, this.value.eval(env));
2723 genCSS: function (env, output) {
2724 output.add(this.key + '=');
2725 if (this.value.genCSS) {
2726 this.value.genCSS(env, output);
2728 output.add(this.value);
2734 })(require('../tree'));
2739 // A function call node.
2741 tree.Call = function (name, args, index, currentFileInfo) {
2745 this.currentFileInfo = currentFileInfo;
2747 tree.Call.prototype = {
2749 accept: function (visitor) {
2750 this.args = visitor.visit(this.args);
2753 // When evaluating a function call,
2754 // we either find the function in `tree.functions` [1],
2755 // in which case we call it, passing the evaluated arguments,
2756 // if this returns null or we cannot find the function, we
2757 // simply print it out as it appeared originally [2].
2759 // The *functions.js* file contains the built-in functions.
2761 // The reason why we evaluate the arguments, is in the case where
2762 // we try to pass a variable to a function, like: `saturate(@color)`.
2763 // The function should receive the value, not the variable.
2765 eval: function (env) {
2766 var args = this.args.map(function (a) { return a.eval(env); }),
2767 nameLC = this.name.toLowerCase(),
2770 if (nameLC in tree.functions) { // 1.
2772 func = new tree.functionCall(env, this.currentFileInfo);
2773 result = func[nameLC].apply(func, args);
2774 /*jshint eqnull:true */
2775 if (result != null) {
2779 throw { type: e.type || "Runtime",
2780 message: "error evaluating function `" + this.name + "`" +
2781 (e.message ? ': ' + e.message : ''),
2782 index: this.index, filename: this.currentFileInfo.filename };
2786 return new tree.Call(this.name, args, this.index, this.currentFileInfo);
2789 genCSS: function (env, output) {
2790 output.add(this.name + "(", this.currentFileInfo, this.index);
2792 for(var i = 0; i < this.args.length; i++) {
2793 this.args[i].genCSS(env, output);
2794 if (i + 1 < this.args.length) {
2805 })(require('../tree'));
2809 // RGB Colors - #ff0014, #eee
2811 tree.Color = function (rgb, a) {
2813 // The end goal here, is to parse the arguments
2814 // into an integer triplet, such as `128, 255, 0`
2816 // This facilitates operations and conversions.
2818 if (Array.isArray(rgb)) {
2820 } else if (rgb.length == 6) {
2821 this.rgb = rgb.match(/.{2}/g).map(function (c) {
2822 return parseInt(c, 16);
2825 this.rgb = rgb.split('').map(function (c) {
2826 return parseInt(c + c, 16);
2829 this.alpha = typeof(a) === 'number' ? a : 1;
2832 var transparentKeyword = "transparent";
2834 tree.Color.prototype = {
2836 eval: function () { return this; },
2837 luma: function () { return (0.2126 * this.rgb[0] / 255) + (0.7152 * this.rgb[1] / 255) + (0.0722 * this.rgb[2] / 255); },
2839 genCSS: function (env, output) {
2840 output.add(this.toCSS(env));
2842 toCSS: function (env, doNotCompress) {
2843 var compress = env && env.compress && !doNotCompress;
2845 // If we have some transparency, the only way to represent it
2846 // is via `rgba`. Otherwise, we use the hex representation,
2847 // which has better compatibility with older browsers.
2848 // Values are capped between `0` and `255`, rounded and zero-padded.
2849 if (this.alpha < 1.0) {
2850 if (this.alpha === 0 && this.isTransparentKeyword) {
2851 return transparentKeyword;
2853 return "rgba(" + this.rgb.map(function (c) {
2854 return Math.round(c);
2855 }).concat(this.alpha).join(',' + (compress ? '' : ' ')) + ")";
2857 var color = this.toRGB();
2860 var splitcolor = color.split('');
2862 // Convert color to short format
2863 if (splitcolor[1] === splitcolor[2] && splitcolor[3] === splitcolor[4] && splitcolor[5] === splitcolor[6]) {
2864 color = '#' + splitcolor[1] + splitcolor[3] + splitcolor[5];
2873 // Operations have to be done per-channel, if not,
2874 // channels will spill onto each other. Once we have
2875 // our result, in the form of an integer triplet,
2876 // we create a new Color node to hold the result.
2878 operate: function (env, op, other) {
2881 if (! (other instanceof tree.Color)) {
2882 other = other.toColor();
2885 for (var c = 0; c < 3; c++) {
2886 result[c] = tree.operate(env, op, this.rgb[c], other.rgb[c]);
2888 return new(tree.Color)(result, this.alpha + other.alpha);
2891 toRGB: function () {
2892 return '#' + this.rgb.map(function (i) {
2894 i = (i > 255 ? 255 : (i < 0 ? 0 : i)).toString(16);
2895 return i.length === 1 ? '0' + i : i;
2899 toHSL: function () {
2900 var r = this.rgb[0] / 255,
2901 g = this.rgb[1] / 255,
2902 b = this.rgb[2] / 255,
2905 var max = Math.max(r, g, b), min = Math.min(r, g, b);
2906 var h, s, l = (max + min) / 2, d = max - min;
2911 s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
2914 case r: h = (g - b) / d + (g < b ? 6 : 0); break;
2915 case g: h = (b - r) / d + 2; break;
2916 case b: h = (r - g) / d + 4; break;
2920 return { h: h * 360, s: s, l: l, a: a };
2922 //Adapted from http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
2923 toHSV: function () {
2924 var r = this.rgb[0] / 255,
2925 g = this.rgb[1] / 255,
2926 b = this.rgb[2] / 255,
2929 var max = Math.max(r, g, b), min = Math.min(r, g, b);
2943 case r: h = (g - b) / d + (g < b ? 6 : 0); break;
2944 case g: h = (b - r) / d + 2; break;
2945 case b: h = (r - g) / d + 4; break;
2949 return { h: h * 360, s: s, v: v, a: a };
2951 toARGB: function () {
2952 var argb = [Math.round(this.alpha * 255)].concat(this.rgb);
2953 return '#' + argb.map(function (i) {
2955 i = (i > 255 ? 255 : (i < 0 ? 0 : i)).toString(16);
2956 return i.length === 1 ? '0' + i : i;
2959 compare: function (x) {
2964 return (x.rgb[0] === this.rgb[0] &&
2965 x.rgb[1] === this.rgb[1] &&
2966 x.rgb[2] === this.rgb[2] &&
2967 x.alpha === this.alpha) ? 0 : -1;
2971 tree.Color.fromKeyword = function(keyword) {
2972 if (tree.colors.hasOwnProperty(keyword)) {
2973 // detect named color
2974 return new(tree.Color)(tree.colors[keyword].slice(1));
2976 if (keyword === transparentKeyword) {
2977 var transparent = new(tree.Color)([0, 0, 0], 0);
2978 transparent.isTransparentKeyword = true;
2984 })(require('../tree'));
2988 tree.Comment = function (value, silent, index, currentFileInfo) {
2990 this.silent = !!silent;
2991 this.currentFileInfo = currentFileInfo;
2993 tree.Comment.prototype = {
2995 genCSS: function (env, output) {
2996 if (this.debugInfo) {
2997 output.add(tree.debugInfo(env, this), this.currentFileInfo, this.index);
2999 output.add(this.value.trim()); //TODO shouldn't need to trim, we shouldn't grab the \n
3002 isSilent: function(env) {
3003 var isReference = (this.currentFileInfo && this.currentFileInfo.reference && !this.isReferenced),
3004 isCompressed = env.compress && !this.value.match(/^\/\*!/);
3005 return this.silent || isReference || isCompressed;
3007 eval: function () { return this; },
3008 markReferenced: function () {
3009 this.isReferenced = true;
3013 })(require('../tree'));
3017 tree.Condition = function (op, l, r, i, negate) {
3018 this.op = op.trim();
3022 this.negate = negate;
3024 tree.Condition.prototype = {
3026 accept: function (visitor) {
3027 this.lvalue = visitor.visit(this.lvalue);
3028 this.rvalue = visitor.visit(this.rvalue);
3030 eval: function (env) {
3031 var a = this.lvalue.eval(env),
3032 b = this.rvalue.eval(env);
3034 var i = this.index, result;
3036 result = (function (op) {
3044 result = a.compare(b);
3045 } else if (b.compare) {
3046 result = b.compare(a);
3048 throw { type: "Type",
3049 message: "Unable to perform comparison",
3053 case -1: return op === '<' || op === '=<' || op === '<=';
3054 case 0: return op === '=' || op === '>=' || op === '=<' || op === '<=';
3055 case 1: return op === '>' || op === '>=';
3059 return this.negate ? !result : result;
3063 })(require('../tree'));
3068 // A number with a unit
3070 tree.Dimension = function (value, unit) {
3071 this.value = parseFloat(value);
3072 this.unit = (unit && unit instanceof tree.Unit) ? unit :
3073 new(tree.Unit)(unit ? [unit] : undefined);
3076 tree.Dimension.prototype = {
3078 accept: function (visitor) {
3079 this.unit = visitor.visit(this.unit);
3081 eval: function (env) {
3084 toColor: function () {
3085 return new(tree.Color)([this.value, this.value, this.value]);
3087 genCSS: function (env, output) {
3088 if ((env && env.strictUnits) && !this.unit.isSingular()) {
3089 throw new Error("Multiple units in dimension. Correct the units or use the unit function. Bad unit: "+this.unit.toString());
3092 var value = this.value,
3093 strValue = String(value);
3095 if (value !== 0 && value < 0.000001 && value > -0.000001) {
3096 // would be output 1e-6 etc.
3097 strValue = value.toFixed(20).replace(/0+$/, "");
3100 if (env && env.compress) {
3101 // Zero values doesn't need a unit
3102 if (value === 0 && this.unit.isLength()) {
3103 output.add(strValue);
3107 // Float values doesn't need a leading zero
3108 if (value > 0 && value < 1) {
3109 strValue = (strValue).substr(1);
3113 output.add(strValue);
3114 this.unit.genCSS(env, output);
3118 // In an operation between two Dimensions,
3119 // we default to the first Dimension's unit,
3120 // so `1px + 2` will yield `3px`.
3121 operate: function (env, op, other) {
3122 /*jshint noempty:false */
3123 var value = tree.operate(env, op, this.value, other.value),
3124 unit = this.unit.clone();
3126 if (op === '+' || op === '-') {
3127 if (unit.numerator.length === 0 && unit.denominator.length === 0) {
3128 unit.numerator = other.unit.numerator.slice(0);
3129 unit.denominator = other.unit.denominator.slice(0);
3130 } else if (other.unit.numerator.length === 0 && unit.denominator.length === 0) {
3133 other = other.convertTo(this.unit.usedUnits());
3135 if(env.strictUnits && other.unit.toString() !== unit.toString()) {
3136 throw new Error("Incompatible units. Change the units or use the unit function. Bad units: '" + unit.toString() +
3137 "' and '" + other.unit.toString() + "'.");
3140 value = tree.operate(env, op, this.value, other.value);
3142 } else if (op === '*') {
3143 unit.numerator = unit.numerator.concat(other.unit.numerator).sort();
3144 unit.denominator = unit.denominator.concat(other.unit.denominator).sort();
3146 } else if (op === '/') {
3147 unit.numerator = unit.numerator.concat(other.unit.denominator).sort();
3148 unit.denominator = unit.denominator.concat(other.unit.numerator).sort();
3151 return new(tree.Dimension)(value, unit);
3154 compare: function (other) {
3155 if (other instanceof tree.Dimension) {
3156 var a = this.unify(), b = other.unify(),
3157 aValue = a.value, bValue = b.value;
3159 if (bValue > aValue) {
3161 } else if (bValue < aValue) {
3164 if (!b.unit.isEmpty() && a.unit.compare(b.unit) !== 0) {
3174 unify: function () {
3175 return this.convertTo({ length: 'm', duration: 's', angle: 'rad' });
3178 convertTo: function (conversions) {
3179 var value = this.value, unit = this.unit.clone(),
3180 i, groupName, group, targetUnit, derivedConversions = {}, applyUnit;
3182 if (typeof conversions === 'string') {
3183 for(i in tree.UnitConversions) {
3184 if (tree.UnitConversions[i].hasOwnProperty(conversions)) {
3185 derivedConversions = {};
3186 derivedConversions[i] = conversions;
3189 conversions = derivedConversions;
3191 applyUnit = function (atomicUnit, denominator) {
3192 /*jshint loopfunc:true */
3193 if (group.hasOwnProperty(atomicUnit)) {
3195 value = value / (group[atomicUnit] / group[targetUnit]);
3197 value = value * (group[atomicUnit] / group[targetUnit]);
3206 for (groupName in conversions) {
3207 if (conversions.hasOwnProperty(groupName)) {
3208 targetUnit = conversions[groupName];
3209 group = tree.UnitConversions[groupName];
3211 unit.map(applyUnit);
3217 return new(tree.Dimension)(value, unit);
3221 // http://www.w3.org/TR/css3-values/#absolute-lengths
3222 tree.UnitConversions = {
3229 'pc': 0.0254 / 72 * 12
3236 'rad': 1/(2*Math.PI),
3243 tree.Unit = function (numerator, denominator, backupUnit) {
3244 this.numerator = numerator ? numerator.slice(0).sort() : [];
3245 this.denominator = denominator ? denominator.slice(0).sort() : [];
3246 this.backupUnit = backupUnit;
3249 tree.Unit.prototype = {
3251 clone: function () {
3252 return new tree.Unit(this.numerator.slice(0), this.denominator.slice(0), this.backupUnit);
3254 genCSS: function (env, output) {
3255 if (this.numerator.length >= 1) {
3256 output.add(this.numerator[0]);
3258 if (this.denominator.length >= 1) {
3259 output.add(this.denominator[0]);
3261 if ((!env || !env.strictUnits) && this.backupUnit) {
3262 output.add(this.backupUnit);
3267 toString: function () {
3268 var i, returnStr = this.numerator.join("*");
3269 for (i = 0; i < this.denominator.length; i++) {
3270 returnStr += "/" + this.denominator[i];
3275 compare: function (other) {
3276 return this.is(other.toString()) ? 0 : -1;
3279 is: function (unitString) {
3280 return this.toString() === unitString;
3283 isLength: function () {
3284 return Boolean(this.toCSS().match(/px|em|%|in|cm|mm|pc|pt|ex/));
3287 isEmpty: function () {
3288 return this.numerator.length === 0 && this.denominator.length === 0;
3291 isSingular: function() {
3292 return this.numerator.length <= 1 && this.denominator.length === 0;
3295 map: function(callback) {
3298 for (i = 0; i < this.numerator.length; i++) {
3299 this.numerator[i] = callback(this.numerator[i], false);
3302 for (i = 0; i < this.denominator.length; i++) {
3303 this.denominator[i] = callback(this.denominator[i], true);
3307 usedUnits: function() {
3308 var group, result = {}, mapUnit;
3310 mapUnit = function (atomicUnit) {
3311 /*jshint loopfunc:true */
3312 if (group.hasOwnProperty(atomicUnit) && !result[groupName]) {
3313 result[groupName] = atomicUnit;
3319 for (var groupName in tree.UnitConversions) {
3320 if (tree.UnitConversions.hasOwnProperty(groupName)) {
3321 group = tree.UnitConversions[groupName];
3330 cancel: function () {
3331 var counter = {}, atomicUnit, i, backup;
3333 for (i = 0; i < this.numerator.length; i++) {
3334 atomicUnit = this.numerator[i];
3336 backup = atomicUnit;
3338 counter[atomicUnit] = (counter[atomicUnit] || 0) + 1;
3341 for (i = 0; i < this.denominator.length; i++) {
3342 atomicUnit = this.denominator[i];
3344 backup = atomicUnit;
3346 counter[atomicUnit] = (counter[atomicUnit] || 0) - 1;
3349 this.numerator = [];
3350 this.denominator = [];
3352 for (atomicUnit in counter) {
3353 if (counter.hasOwnProperty(atomicUnit)) {
3354 var count = counter[atomicUnit];
3357 for (i = 0; i < count; i++) {
3358 this.numerator.push(atomicUnit);
3360 } else if (count < 0) {
3361 for (i = 0; i < -count; i++) {
3362 this.denominator.push(atomicUnit);
3368 if (this.numerator.length === 0 && this.denominator.length === 0 && backup) {
3369 this.backupUnit = backup;
3372 this.numerator.sort();
3373 this.denominator.sort();
3377 })(require('../tree'));
3381 tree.Directive = function (name, value, index, currentFileInfo) {
3384 if (Array.isArray(value)) {
3385 this.rules = [new(tree.Ruleset)([], value)];
3386 this.rules[0].allowImports = true;
3390 this.currentFileInfo = currentFileInfo;
3393 tree.Directive.prototype = {
3395 accept: function (visitor) {
3396 this.rules = visitor.visit(this.rules);
3397 this.value = visitor.visit(this.value);
3399 genCSS: function (env, output) {
3400 output.add(this.name, this.currentFileInfo, this.index);
3402 tree.outputRuleset(env, output, this.rules);
3405 this.value.genCSS(env, output);
3410 eval: function (env) {
3411 var evaldDirective = this;
3413 env.frames.unshift(this);
3414 evaldDirective = new(tree.Directive)(this.name, null, this.index, this.currentFileInfo);
3415 evaldDirective.rules = [this.rules[0].eval(env)];
3416 evaldDirective.rules[0].root = true;
3419 return evaldDirective;
3421 variable: function (name) { return tree.Ruleset.prototype.variable.call(this.rules[0], name); },
3422 find: function () { return tree.Ruleset.prototype.find.apply(this.rules[0], arguments); },
3423 rulesets: function () { return tree.Ruleset.prototype.rulesets.apply(this.rules[0]); },
3424 markReferenced: function () {
3426 this.isReferenced = true;
3428 rules = this.rules[0].rules;
3429 for (i = 0; i < rules.length; i++) {
3430 if (rules[i].markReferenced) {
3431 rules[i].markReferenced();
3438 })(require('../tree'));
3442 tree.Element = function (combinator, value, index, currentFileInfo) {
3443 this.combinator = combinator instanceof tree.Combinator ?
3444 combinator : new(tree.Combinator)(combinator);
3446 if (typeof(value) === 'string') {
3447 this.value = value.trim();
3454 this.currentFileInfo = currentFileInfo;
3456 tree.Element.prototype = {
3458 accept: function (visitor) {
3459 this.combinator = visitor.visit(this.combinator);
3460 this.value = visitor.visit(this.value);
3462 eval: function (env) {
3463 return new(tree.Element)(this.combinator,
3464 this.value.eval ? this.value.eval(env) : this.value,
3466 this.currentFileInfo);
3468 genCSS: function (env, output) {
3469 output.add(this.toCSS(env), this.currentFileInfo, this.index);
3471 toCSS: function (env) {
3472 var value = (this.value.toCSS ? this.value.toCSS(env) : this.value);
3473 if (value === '' && this.combinator.value.charAt(0) === '&') {
3476 return this.combinator.toCSS(env || {}) + value;
3481 tree.Attribute = function (key, op, value) {
3486 tree.Attribute.prototype = {
3488 accept: function (visitor) {
3489 this.value = visitor.visit(this.value);
3491 eval: function (env) {
3492 return new(tree.Attribute)(this.key.eval ? this.key.eval(env) : this.key,
3493 this.op, (this.value && this.value.eval) ? this.value.eval(env) : this.value);
3495 genCSS: function (env, output) {
3496 output.add(this.toCSS(env));
3498 toCSS: function (env) {
3499 var value = this.key.toCSS ? this.key.toCSS(env) : this.key;
3503 value += (this.value.toCSS ? this.value.toCSS(env) : this.value);
3506 return '[' + value + ']';
3510 tree.Combinator = function (value) {
3511 if (value === ' ') {
3514 this.value = value ? value.trim() : "";
3517 tree.Combinator.prototype = {
3528 _outputMapCompressed: {
3537 genCSS: function (env, output) {
3538 output.add((env.compress ? this._outputMapCompressed : this._outputMap)[this.value]);
3543 })(require('../tree'));
3547 tree.Expression = function (value) { this.value = value; };
3548 tree.Expression.prototype = {
3550 accept: function (visitor) {
3551 this.value = visitor.visit(this.value);
3553 eval: function (env) {
3555 inParenthesis = this.parens && !this.parensInOp,
3556 doubleParen = false;
3557 if (inParenthesis) {
3558 env.inParenthesis();
3560 if (this.value.length > 1) {
3561 returnValue = new(tree.Expression)(this.value.map(function (e) {
3564 } else if (this.value.length === 1) {
3565 if (this.value[0].parens && !this.value[0].parensInOp) {
3568 returnValue = this.value[0].eval(env);
3572 if (inParenthesis) {
3573 env.outOfParenthesis();
3575 if (this.parens && this.parensInOp && !(env.isMathOn()) && !doubleParen) {
3576 returnValue = new(tree.Paren)(returnValue);
3580 genCSS: function (env, output) {
3581 for(var i = 0; i < this.value.length; i++) {
3582 this.value[i].genCSS(env, output);
3583 if (i + 1 < this.value.length) {
3589 throwAwayComments: function () {
3590 this.value = this.value.filter(function(v) {
3591 return !(v instanceof tree.Comment);
3596 })(require('../tree'));
3600 tree.Extend = function Extend(selector, option, index) {
3601 this.selector = selector;
3602 this.option = option;
3607 this.allowBefore = true;
3608 this.allowAfter = true;
3611 this.allowBefore = false;
3612 this.allowAfter = false;
3617 tree.Extend.prototype = {
3619 accept: function (visitor) {
3620 this.selector = visitor.visit(this.selector);
3622 eval: function (env) {
3623 return new(tree.Extend)(this.selector.eval(env), this.option, this.index);
3625 clone: function (env) {
3626 return new(tree.Extend)(this.selector, this.option, this.index);
3628 findSelfSelectors: function (selectors) {
3629 var selfElements = [],
3633 for(i = 0; i < selectors.length; i++) {
3634 selectorElements = selectors[i].elements;
3635 // duplicate the logic in genCSS function inside the selector node.
3636 // future TODO - move both logics into the selector joiner visitor
3637 if (i > 0 && selectorElements.length && selectorElements[0].combinator.value === "") {
3638 selectorElements[0].combinator.value = ' ';
3640 selfElements = selfElements.concat(selectors[i].elements);
3643 this.selfSelectors = [{ elements: selfElements }];
3647 })(require('../tree'));
3653 // The general strategy here is that we don't want to wait
3654 // for the parsing to be completed, before we start importing
3655 // the file. That's because in the context of a browser,
3656 // most of the time will be spent waiting for the server to respond.
3658 // On creation, we push the import path to our import queue, though
3659 // `import,push`, we also pass it a callback, which it'll call once
3660 // the file has been fetched, and parsed.
3662 tree.Import = function (path, features, options, index, currentFileInfo) {
3663 this.options = options;
3666 this.features = features;
3667 this.currentFileInfo = currentFileInfo;
3669 if (this.options.less !== undefined || this.options.inline) {
3670 this.css = !this.options.less || this.options.inline;
3672 var pathValue = this.getPath();
3673 if (pathValue && /css([\?;].*)?$/.test(pathValue)) {
3680 // The actual import node doesn't return anything, when converted to CSS.
3681 // The reason is that it's used at the evaluation stage, so that the rules
3682 // it imports can be treated like any other rules.
3684 // In `eval`, we make sure all Import nodes get evaluated, recursively, so
3685 // we end up with a flat structure, which can easily be imported in the parent
3688 tree.Import.prototype = {
3690 accept: function (visitor) {
3691 this.features = visitor.visit(this.features);
3692 this.path = visitor.visit(this.path);
3693 if (!this.options.inline) {
3694 this.root = visitor.visit(this.root);
3697 genCSS: function (env, output) {
3699 output.add("@import ", this.currentFileInfo, this.index);
3700 this.path.genCSS(env, output);
3701 if (this.features) {
3703 this.features.genCSS(env, output);
3709 getPath: function () {
3710 if (this.path instanceof tree.Quoted) {
3711 var path = this.path.value;
3712 return (this.css !== undefined || /(\.[a-z]*$)|([\?;].*)$/.test(path)) ? path : path + '.less';
3713 } else if (this.path instanceof tree.URL) {
3714 return this.path.value.value;
3718 evalForImport: function (env) {
3719 return new(tree.Import)(this.path.eval(env), this.features, this.options, this.index, this.currentFileInfo);
3721 evalPath: function (env) {
3722 var path = this.path.eval(env);
3723 var rootpath = this.currentFileInfo && this.currentFileInfo.rootpath;
3725 if (!(path instanceof tree.URL)) {
3727 var pathValue = path.value;
3728 // Add the base path if the import is relative
3729 if (pathValue && env.isPathRelative(pathValue)) {
3730 path.value = rootpath + pathValue;
3733 path.value = env.normalizePath(path.value);
3738 eval: function (env) {
3739 var ruleset, features = this.features && this.features.eval(env);
3741 if (this.skip) { return []; }
3743 if (this.options.inline) {
3744 //todo needs to reference css file not import
3745 var contents = new(tree.Anonymous)(this.root, 0, {filename: this.importedFilename}, true);
3746 return this.features ? new(tree.Media)([contents], this.features.value) : [contents];
3747 } else if (this.css) {
3748 var newImport = new(tree.Import)(this.evalPath(env), features, this.options, this.index);
3749 if (!newImport.css && this.error) {
3754 ruleset = new(tree.Ruleset)([], this.root.rules.slice(0));
3756 ruleset.evalImports(env);
3758 return this.features ? new(tree.Media)(ruleset.rules, this.features.value) : ruleset.rules;
3763 })(require('../tree'));
3767 tree.JavaScript = function (string, index, escaped) {
3768 this.escaped = escaped;
3769 this.expression = string;
3772 tree.JavaScript.prototype = {
3774 eval: function (env) {
3779 var expression = this.expression.replace(/@\{([\w-]+)\}/g, function (_, name) {
3780 return tree.jsify(new(tree.Variable)('@' + name, that.index).eval(env));
3784 expression = new(Function)('return (' + expression + ')');
3786 throw { message: "JavaScript evaluation error: " + e.message + " from `" + expression + "`" ,
3787 index: this.index };
3790 for (var k in env.frames[0].variables()) {
3791 /*jshint loopfunc:true */
3792 context[k.slice(1)] = {
3793 value: env.frames[0].variables()[k].value,
3795 return this.value.eval(env).toCSS();
3801 result = expression.call(context);
3803 throw { message: "JavaScript evaluation error: '" + e.name + ': ' + e.message + "'" ,
3804 index: this.index };
3806 if (typeof(result) === 'string') {
3807 return new(tree.Quoted)('"' + result + '"', result, this.escaped, this.index);
3808 } else if (Array.isArray(result)) {
3809 return new(tree.Anonymous)(result.join(', '));
3811 return new(tree.Anonymous)(result);
3816 })(require('../tree'));
3821 tree.Keyword = function (value) { this.value = value; };
3822 tree.Keyword.prototype = {
3824 eval: function () { return this; },
3825 genCSS: function (env, output) {
3826 output.add(this.value);
3829 compare: function (other) {
3830 if (other instanceof tree.Keyword) {
3831 return other.value === this.value ? 0 : 1;
3838 tree.True = new(tree.Keyword)('true');
3839 tree.False = new(tree.Keyword)('false');
3841 })(require('../tree'));
3845 tree.Media = function (value, features, index, currentFileInfo) {
3847 this.currentFileInfo = currentFileInfo;
3849 var selectors = this.emptySelectors();
3851 this.features = new(tree.Value)(features);
3852 this.rules = [new(tree.Ruleset)(selectors, value)];
3853 this.rules[0].allowImports = true;
3855 tree.Media.prototype = {
3857 accept: function (visitor) {
3858 this.features = visitor.visit(this.features);
3859 this.rules = visitor.visit(this.rules);
3861 genCSS: function (env, output) {
3862 output.add('@media ', this.currentFileInfo, this.index);
3863 this.features.genCSS(env, output);
3864 tree.outputRuleset(env, output, this.rules);
3867 eval: function (env) {
3868 if (!env.mediaBlocks) {
3869 env.mediaBlocks = [];
3873 var media = new(tree.Media)([], [], this.index, this.currentFileInfo);
3874 if(this.debugInfo) {
3875 this.rules[0].debugInfo = this.debugInfo;
3876 media.debugInfo = this.debugInfo;
3878 var strictMathBypass = false;
3879 if (!env.strictMath) {
3880 strictMathBypass = true;
3881 env.strictMath = true;
3884 media.features = this.features.eval(env);
3887 if (strictMathBypass) {
3888 env.strictMath = false;
3892 env.mediaPath.push(media);
3893 env.mediaBlocks.push(media);
3895 env.frames.unshift(this.rules[0]);
3896 media.rules = [this.rules[0].eval(env)];
3899 env.mediaPath.pop();
3901 return env.mediaPath.length === 0 ? media.evalTop(env) :
3902 media.evalNested(env);
3904 variable: function (name) { return tree.Ruleset.prototype.variable.call(this.rules[0], name); },
3905 find: function () { return tree.Ruleset.prototype.find.apply(this.rules[0], arguments); },
3906 rulesets: function () { return tree.Ruleset.prototype.rulesets.apply(this.rules[0]); },
3907 emptySelectors: function() {
3908 var el = new(tree.Element)('', '&', this.index, this.currentFileInfo);
3909 return [new(tree.Selector)([el], null, null, this.index, this.currentFileInfo)];
3911 markReferenced: function () {
3912 var i, rules = this.rules[0].rules;
3913 this.isReferenced = true;
3914 for (i = 0; i < rules.length; i++) {
3915 if (rules[i].markReferenced) {
3916 rules[i].markReferenced();
3921 evalTop: function (env) {
3924 // Render all dependent Media blocks.
3925 if (env.mediaBlocks.length > 1) {
3926 var selectors = this.emptySelectors();
3927 result = new(tree.Ruleset)(selectors, env.mediaBlocks);
3928 result.multiMedia = true;
3931 delete env.mediaBlocks;
3932 delete env.mediaPath;
3936 evalNested: function (env) {
3938 path = env.mediaPath.concat([this]);
3940 // Extract the media-query conditions separated with `,` (OR).
3941 for (i = 0; i < path.length; i++) {
3942 value = path[i].features instanceof tree.Value ?
3943 path[i].features.value : path[i].features;
3944 path[i] = Array.isArray(value) ? value : [value];
3947 // Trace all permutations to generate the resulting media-query.
3949 // (a, b and c) with nested (d, e) ->
3954 this.features = new(tree.Value)(this.permute(path).map(function (path) {
3955 path = path.map(function (fragment) {
3956 return fragment.toCSS ? fragment : new(tree.Anonymous)(fragment);
3959 for(i = path.length - 1; i > 0; i--) {
3960 path.splice(i, 0, new(tree.Anonymous)("and"));
3963 return new(tree.Expression)(path);
3966 // Fake a tree-node that doesn't output anything.
3967 return new(tree.Ruleset)([], []);
3969 permute: function (arr) {
3970 if (arr.length === 0) {
3972 } else if (arr.length === 1) {
3976 var rest = this.permute(arr.slice(1));
3977 for (var i = 0; i < rest.length; i++) {
3978 for (var j = 0; j < arr[0].length; j++) {
3979 result.push([arr[0][j]].concat(rest[i]));
3985 bubbleSelectors: function (selectors) {
3986 this.rules = [new(tree.Ruleset)(selectors.slice(0), [this.rules[0]])];
3990 })(require('../tree'));
3995 tree.mixin.Call = function (elements, args, index, currentFileInfo, important) {
3996 this.selector = new(tree.Selector)(elements);
3997 this.arguments = args;
3999 this.currentFileInfo = currentFileInfo;
4000 this.important = important;
4002 tree.mixin.Call.prototype = {
4004 accept: function (visitor) {
4005 this.selector = visitor.visit(this.selector);
4006 this.arguments = visitor.visit(this.arguments);
4008 eval: function (env) {
4009 var mixins, mixin, args, rules = [], match = false, i, m, f, isRecursive, isOneFound, rule;
4011 args = this.arguments && this.arguments.map(function (a) {
4012 return { name: a.name, value: a.value.eval(env) };
4015 for (i = 0; i < env.frames.length; i++) {
4016 if ((mixins = env.frames[i].find(this.selector)).length > 0) {
4018 for (m = 0; m < mixins.length; m++) {
4020 isRecursive = false;
4021 for(f = 0; f < env.frames.length; f++) {
4022 if ((!(mixin instanceof tree.mixin.Definition)) && mixin === (env.frames[f].originalRuleset || env.frames[f])) {
4030 if (mixin.matchArgs(args, env)) {
4031 if (!mixin.matchCondition || mixin.matchCondition(args, env)) {
4033 if (!(mixin instanceof tree.mixin.Definition)) {
4034 mixin = new tree.mixin.Definition("", [], mixin.rules, null, false);
4035 mixin.originalRuleset = mixins[m].originalRuleset || mixins[m];
4037 //if (this.important) {
4038 // isImportant = env.isImportant;
4039 // env.isImportant = true;
4041 Array.prototype.push.apply(
4042 rules, mixin.eval(env, args, this.important).rules);
4043 //if (this.important) {
4044 // env.isImportant = isImportant;
4047 throw { message: e.message, index: this.index, filename: this.currentFileInfo.filename, stack: e.stack };
4054 if (!this.currentFileInfo || !this.currentFileInfo.reference) {
4055 for (i = 0; i < rules.length; i++) {
4057 if (rule.markReferenced) {
4058 rule.markReferenced();
4067 throw { type: 'Runtime',
4068 message: 'No matching definition was found for `' +
4069 this.selector.toCSS().trim() + '(' +
4070 (args ? args.map(function (a) {
4073 argValue += a.name + ":";
4075 if (a.value.toCSS) {
4076 argValue += a.value.toCSS();
4081 }).join(', ') : "") + ")`",
4082 index: this.index, filename: this.currentFileInfo.filename };
4084 throw { type: 'Name',
4085 message: this.selector.toCSS().trim() + " is undefined",
4086 index: this.index, filename: this.currentFileInfo.filename };
4091 tree.mixin.Definition = function (name, params, rules, condition, variadic) {
4093 this.selectors = [new(tree.Selector)([new(tree.Element)(null, name, this.index, this.currentFileInfo)])];
4094 this.params = params;
4095 this.condition = condition;
4096 this.variadic = variadic;
4097 this.arity = params.length;
4100 this.required = params.reduce(function (count, p) {
4101 if (!p.name || (p.name && !p.value)) { return count + 1; }
4102 else { return count; }
4104 this.parent = tree.Ruleset.prototype;
4107 tree.mixin.Definition.prototype = {
4108 type: "MixinDefinition",
4109 accept: function (visitor) {
4110 this.params = visitor.visit(this.params);
4111 this.rules = visitor.visit(this.rules);
4112 this.condition = visitor.visit(this.condition);
4114 variable: function (name) { return this.parent.variable.call(this, name); },
4115 variables: function () { return this.parent.variables.call(this); },
4116 find: function () { return this.parent.find.apply(this, arguments); },
4117 rulesets: function () { return this.parent.rulesets.apply(this); },
4119 evalParams: function (env, mixinEnv, args, evaldArguments) {
4120 /*jshint boss:true */
4121 var frame = new(tree.Ruleset)(null, []),
4123 params = this.params.slice(0),
4124 i, j, val, name, isNamedFound, argIndex;
4126 mixinEnv = new tree.evalEnv(mixinEnv, [frame].concat(mixinEnv.frames));
4129 args = args.slice(0);
4131 for(i = 0; i < args.length; i++) {
4133 if (name = (arg && arg.name)) {
4134 isNamedFound = false;
4135 for(j = 0; j < params.length; j++) {
4136 if (!evaldArguments[j] && name === params[j].name) {
4137 evaldArguments[j] = arg.value.eval(env);
4138 frame.rules.unshift(new(tree.Rule)(name, arg.value.eval(env)));
4139 isNamedFound = true;
4148 throw { type: 'Runtime', message: "Named argument for " + this.name +
4149 ' ' + args[i].name + ' not found' };
4155 for (i = 0; i < params.length; i++) {
4156 if (evaldArguments[i]) { continue; }
4158 arg = args && args[argIndex];
4160 if (name = params[i].name) {
4161 if (params[i].variadic && args) {
4163 for (j = argIndex; j < args.length; j++) {
4164 varargs.push(args[j].value.eval(env));
4166 frame.rules.unshift(new(tree.Rule)(name, new(tree.Expression)(varargs).eval(env)));
4168 val = arg && arg.value;
4170 val = val.eval(env);
4171 } else if (params[i].value) {
4172 val = params[i].value.eval(mixinEnv);
4175 throw { type: 'Runtime', message: "wrong number of arguments for " + this.name +
4176 ' (' + args.length + ' for ' + this.arity + ')' };
4179 frame.rules.unshift(new(tree.Rule)(name, val));
4180 evaldArguments[i] = val;
4184 if (params[i].variadic && args) {
4185 for (j = argIndex; j < args.length; j++) {
4186 evaldArguments[j] = args[j].value.eval(env);
4194 eval: function (env, args, important) {
4195 var _arguments = [],
4196 mixinFrames = this.frames.concat(env.frames),
4197 frame = this.evalParams(env, new(tree.evalEnv)(env, mixinFrames), args, _arguments),
4200 frame.rules.unshift(new(tree.Rule)('@arguments', new(tree.Expression)(_arguments).eval(env)));
4202 rules = this.rules.slice(0);
4204 ruleset = new(tree.Ruleset)(null, rules);
4205 ruleset.originalRuleset = this;
4206 ruleset = ruleset.eval(new(tree.evalEnv)(env, [this, frame].concat(mixinFrames)));
4208 ruleset = this.parent.makeImportant.apply(ruleset);
4212 matchCondition: function (args, env) {
4213 if (this.condition && !this.condition.eval(
4214 new(tree.evalEnv)(env,
4215 [this.evalParams(env, new(tree.evalEnv)(env, this.frames.concat(env.frames)), args, [])] // the parameter variables
4216 .concat(this.frames) // the parent namespace/mixin frames
4217 .concat(env.frames)))) { // the current environment frames
4222 matchArgs: function (args, env) {
4223 var argsLength = (args && args.length) || 0, len;
4225 if (! this.variadic) {
4226 if (argsLength < this.required) { return false; }
4227 if (argsLength > this.params.length) { return false; }
4229 if (argsLength < (this.required - 1)) { return false; }
4232 len = Math.min(argsLength, this.arity);
4234 for (var i = 0; i < len; i++) {
4235 if (!this.params[i].name && !this.params[i].variadic) {
4236 if (args[i].value.eval(env).toCSS() != this.params[i].value.eval(env).toCSS()) {
4245 })(require('../tree'));
4249 tree.Negative = function (node) {
4252 tree.Negative.prototype = {
4254 accept: function (visitor) {
4255 this.value = visitor.visit(this.value);
4257 genCSS: function (env, output) {
4259 this.value.genCSS(env, output);
4262 eval: function (env) {
4263 if (env.isMathOn()) {
4264 return (new(tree.Operation)('*', [new(tree.Dimension)(-1), this.value])).eval(env);
4266 return new(tree.Negative)(this.value.eval(env));
4270 })(require('../tree'));
4274 tree.Operation = function (op, operands, isSpaced) {
4275 this.op = op.trim();
4276 this.operands = operands;
4277 this.isSpaced = isSpaced;
4279 tree.Operation.prototype = {
4281 accept: function (visitor) {
4282 this.operands = visitor.visit(this.operands);
4284 eval: function (env) {
4285 var a = this.operands[0].eval(env),
4286 b = this.operands[1].eval(env),
4289 if (env.isMathOn()) {
4290 if (a instanceof tree.Dimension && b instanceof tree.Color) {
4291 if (this.op === '*' || this.op === '+') {
4292 temp = b, b = a, a = temp;
4294 throw { type: "Operation",
4295 message: "Can't substract or divide a color from a number" };
4299 throw { type: "Operation",
4300 message: "Operation on an invalid type" };
4303 return a.operate(env, this.op, b);
4305 return new(tree.Operation)(this.op, [a, b], this.isSpaced);
4308 genCSS: function (env, output) {
4309 this.operands[0].genCSS(env, output);
4310 if (this.isSpaced) {
4313 output.add(this.op);
4314 if (this.isSpaced) {
4317 this.operands[1].genCSS(env, output);
4322 tree.operate = function (env, op, a, b) {
4324 case '+': return a + b;
4325 case '-': return a - b;
4326 case '*': return a * b;
4327 case '/': return a / b;
4331 })(require('../tree'));
4336 tree.Paren = function (node) {
4339 tree.Paren.prototype = {
4341 accept: function (visitor) {
4342 this.value = visitor.visit(this.value);
4344 genCSS: function (env, output) {
4346 this.value.genCSS(env, output);
4350 eval: function (env) {
4351 return new(tree.Paren)(this.value.eval(env));
4355 })(require('../tree'));
4359 tree.Quoted = function (str, content, escaped, index, currentFileInfo) {
4360 this.escaped = escaped;
4361 this.value = content || '';
4362 this.quote = str.charAt(0);
4364 this.currentFileInfo = currentFileInfo;
4366 tree.Quoted.prototype = {
4368 genCSS: function (env, output) {
4369 if (!this.escaped) {
4370 output.add(this.quote, this.currentFileInfo, this.index);
4372 output.add(this.value);
4373 if (!this.escaped) {
4374 output.add(this.quote);
4378 eval: function (env) {
4380 var value = this.value.replace(/`([^`]+)`/g, function (_, exp) {
4381 return new(tree.JavaScript)(exp, that.index, true).eval(env).value;
4382 }).replace(/@\{([\w-]+)\}/g, function (_, name) {
4383 var v = new(tree.Variable)('@' + name, that.index, that.currentFileInfo).eval(env, true);
4384 return (v instanceof tree.Quoted) ? v.value : v.toCSS();
4386 return new(tree.Quoted)(this.quote + value + this.quote, value, this.escaped, this.index, this.currentFileInfo);
4388 compare: function (x) {
4393 var left = this.toCSS(),
4396 if (left === right) {
4400 return left < right ? -1 : 1;
4404 })(require('../tree'));
4408 tree.Rule = function (name, value, important, merge, index, currentFileInfo, inline) {
4410 this.value = (value instanceof tree.Value) ? value : new(tree.Value)([value]);
4411 this.important = important ? ' ' + important.trim() : '';
4414 this.currentFileInfo = currentFileInfo;
4415 this.inline = inline || false;
4416 this.variable = (name.charAt(0) === '@');
4419 tree.Rule.prototype = {
4421 accept: function (visitor) {
4422 this.value = visitor.visit(this.value);
4424 genCSS: function (env, output) {
4425 output.add(this.name + (env.compress ? ':' : ': '), this.currentFileInfo, this.index);
4427 this.value.genCSS(env, output);
4430 e.index = this.index;
4431 e.filename = this.currentFileInfo.filename;
4434 output.add(this.important + ((this.inline || (env.lastRule && env.compress)) ? "" : ";"), this.currentFileInfo, this.index);
4437 eval: function (env) {
4438 var strictMathBypass = false;
4439 if (this.name === "font" && !env.strictMath) {
4440 strictMathBypass = true;
4441 env.strictMath = true;
4444 return new(tree.Rule)(this.name,
4445 this.value.eval(env),
4448 this.index, this.currentFileInfo, this.inline);
4451 if (strictMathBypass) {
4452 env.strictMath = false;
4456 makeImportant: function () {
4457 return new(tree.Rule)(this.name,
4461 this.index, this.currentFileInfo, this.inline);
4465 })(require('../tree'));
4469 tree.Ruleset = function (selectors, rules, strictImports) {
4470 this.selectors = selectors;
4473 this.strictImports = strictImports;
4475 tree.Ruleset.prototype = {
4477 accept: function (visitor) {
4479 for(var i = 0; i < this.paths.length; i++) {
4480 this.paths[i] = visitor.visit(this.paths[i]);
4483 this.selectors = visitor.visit(this.selectors);
4485 this.rules = visitor.visit(this.rules);
4487 eval: function (env) {
4488 var selectors = this.selectors && this.selectors.map(function (s) { return s.eval(env); });
4489 var ruleset = new(tree.Ruleset)(selectors, this.rules.slice(0), this.strictImports);
4494 ruleset.originalRuleset = this;
4495 ruleset.root = this.root;
4496 ruleset.firstRoot = this.firstRoot;
4497 ruleset.allowImports = this.allowImports;
4499 if(this.debugInfo) {
4500 ruleset.debugInfo = this.debugInfo;
4503 // push the current ruleset to the frames stack
4504 env.frames.unshift(ruleset);
4506 // currrent selectors
4507 if (!env.selectors) {
4510 env.selectors.unshift(this.selectors);
4513 if (ruleset.root || ruleset.allowImports || !ruleset.strictImports) {
4514 ruleset.evalImports(env);
4517 // Store the frames around mixin definitions,
4518 // so they can be evaluated like closures when the time comes.
4519 for (i = 0; i < ruleset.rules.length; i++) {
4520 if (ruleset.rules[i] instanceof tree.mixin.Definition) {
4521 ruleset.rules[i].frames = env.frames.slice(0);
4525 var mediaBlockCount = (env.mediaBlocks && env.mediaBlocks.length) || 0;
4527 // Evaluate mixin calls.
4528 for (i = 0; i < ruleset.rules.length; i++) {
4529 if (ruleset.rules[i] instanceof tree.mixin.Call) {
4530 /*jshint loopfunc:true */
4531 rules = ruleset.rules[i].eval(env).filter(function(r) {
4532 if ((r instanceof tree.Rule) && r.variable) {
4533 // do not pollute the scope if the variable is
4534 // already there. consider returning false here
4535 // but we need a way to "return" variable from mixins
4536 return !(ruleset.variable(r.name));
4540 ruleset.rules.splice.apply(ruleset.rules, [i, 1].concat(rules));
4541 i += rules.length-1;
4542 ruleset.resetCache();
4546 // Evaluate everything else
4547 for (i = 0; i < ruleset.rules.length; i++) {
4548 rule = ruleset.rules[i];
4550 if (! (rule instanceof tree.mixin.Definition)) {
4551 ruleset.rules[i] = rule.eval ? rule.eval(env) : rule;
4557 env.selectors.shift();
4559 if (env.mediaBlocks) {
4560 for (i = mediaBlockCount; i < env.mediaBlocks.length; i++) {
4561 env.mediaBlocks[i].bubbleSelectors(selectors);
4567 evalImports: function(env) {
4569 for (i = 0; i < this.rules.length; i++) {
4570 if (this.rules[i] instanceof tree.Import) {
4571 rules = this.rules[i].eval(env);
4572 if (typeof rules.length === "number") {
4573 this.rules.splice.apply(this.rules, [i, 1].concat(rules));
4576 this.rules.splice(i, 1, rules);
4582 makeImportant: function() {
4583 return new tree.Ruleset(this.selectors, this.rules.map(function (r) {
4584 if (r.makeImportant) {
4585 return r.makeImportant();
4589 }), this.strictImports);
4591 matchArgs: function (args) {
4592 return !args || args.length === 0;
4594 matchCondition: function (args, env) {
4595 var lastSelector = this.selectors[this.selectors.length-1];
4596 if (lastSelector.condition &&
4597 !lastSelector.condition.eval(
4598 new(tree.evalEnv)(env,
4604 resetCache: function () {
4605 this._rulesets = null;
4606 this._variables = null;
4609 variables: function () {
4610 if (this._variables) { return this._variables; }
4612 return this._variables = this.rules.reduce(function (hash, r) {
4613 if (r instanceof tree.Rule && r.variable === true) {
4620 variable: function (name) {
4621 return this.variables()[name];
4623 rulesets: function () {
4624 return this.rules.filter(function (r) {
4625 return (r instanceof tree.Ruleset) || (r instanceof tree.mixin.Definition);
4628 find: function (selector, self) {
4629 self = self || this;
4630 var rules = [], match,
4631 key = selector.toCSS();
4633 if (key in this._lookups) { return this._lookups[key]; }
4635 this.rulesets().forEach(function (rule) {
4636 if (rule !== self) {
4637 for (var j = 0; j < rule.selectors.length; j++) {
4638 if (match = selector.match(rule.selectors[j])) {
4639 if (selector.elements.length > match) {
4640 Array.prototype.push.apply(rules, rule.find(
4641 new(tree.Selector)(selector.elements.slice(match)), self));
4650 return this._lookups[key] = rules;
4652 genCSS: function (env, output) {
4656 debugInfo, // Line number debugging
4658 firstRuleset = true,
4661 env.tabLevel = (env.tabLevel || 0);
4667 var tabRuleStr = env.compress ? '' : Array(env.tabLevel + 1).join(" "),
4668 tabSetStr = env.compress ? '' : Array(env.tabLevel).join(" ");
4670 for (i = 0; i < this.rules.length; i++) {
4671 rule = this.rules[i];
4672 if (rule.rules || (rule instanceof tree.Media) || rule instanceof tree.Directive || (this.root && rule instanceof tree.Comment)) {
4673 rulesetNodes.push(rule);
4675 ruleNodes.push(rule);
4679 // If this is the root node, we don't render
4680 // a selector, or {}.
4682 debugInfo = tree.debugInfo(env, this, tabSetStr);
4685 output.add(debugInfo);
4686 output.add(tabSetStr);
4689 for(i = 0; i < this.paths.length; i++) {
4690 path = this.paths[i];
4691 env.firstSelector = true;
4692 for(j = 0; j < path.length; j++) {
4693 path[j].genCSS(env, output);
4694 env.firstSelector = false;
4696 if (i + 1 < this.paths.length) {
4697 output.add(env.compress ? ',' : (',\n' + tabSetStr));
4701 output.add((env.compress ? '{' : ' {\n') + tabRuleStr);
4704 // Compile rules and rulesets
4705 for (i = 0; i < ruleNodes.length; i++) {
4706 rule = ruleNodes[i];
4708 // @page{ directive ends up with root elements inside it, a mix of rules and rulesets
4709 // In this instance we do not know whether it is the last property
4710 if (i + 1 === ruleNodes.length && (!this.root || rulesetNodes.length === 0 || this.firstRoot)) {
4711 env.lastRule = true;
4715 rule.genCSS(env, output);
4716 } else if (rule.value) {
4717 output.add(rule.value.toString());
4720 if (!env.lastRule) {
4721 output.add(env.compress ? '' : ('\n' + tabRuleStr));
4723 env.lastRule = false;
4728 output.add((env.compress ? '}' : '\n' + tabSetStr + '}'));
4732 for (i = 0; i < rulesetNodes.length; i++) {
4733 if (ruleNodes.length && firstRuleset) {
4734 output.add((env.compress ? "" : "\n") + (this.root ? tabRuleStr : tabSetStr));
4736 if (!firstRuleset) {
4737 output.add((env.compress ? "" : "\n") + (this.root ? tabRuleStr : tabSetStr));
4739 firstRuleset = false;
4740 rulesetNodes[i].genCSS(env, output);
4743 if (!output.isEmpty() && !env.compress && this.firstRoot) {
4750 markReferenced: function () {
4751 for (var s = 0; s < this.selectors.length; s++) {
4752 this.selectors[s].markReferenced();
4756 joinSelectors: function (paths, context, selectors) {
4757 for (var s = 0; s < selectors.length; s++) {
4758 this.joinSelector(paths, context, selectors[s]);
4762 joinSelector: function (paths, context, selector) {
4765 hasParentSelector, newSelectors, el, sel, parentSel,
4766 newSelectorPath, afterParentJoin, newJoinedSelector,
4767 newJoinedSelectorEmpty, lastSelector, currentElements,
4768 selectorsMultiplied;
4770 for (i = 0; i < selector.elements.length; i++) {
4771 el = selector.elements[i];
4772 if (el.value === '&') {
4773 hasParentSelector = true;
4777 if (!hasParentSelector) {
4778 if (context.length > 0) {
4779 for (i = 0; i < context.length; i++) {
4780 paths.push(context[i].concat(selector));
4784 paths.push([selector]);
4789 // The paths are [[Selector]]
4790 // The first list is a list of comma seperated selectors
4791 // The inner list is a list of inheritance seperated selectors
4797 // == [[.a] [.c]] [[.b] [.c]]
4800 // the elements from the current selector so far
4801 currentElements = [];
4802 // the current list of new selectors to add to the path.
4803 // We will build it up. We initiate it with one empty selector as we "multiply" the new selectors
4805 newSelectors = [[]];
4807 for (i = 0; i < selector.elements.length; i++) {
4808 el = selector.elements[i];
4809 // non parent reference elements just get added
4810 if (el.value !== "&") {
4811 currentElements.push(el);
4813 // the new list of selectors to add
4814 selectorsMultiplied = [];
4816 // merge the current list of non parent selector elements
4817 // on to the current list of selectors to add
4818 if (currentElements.length > 0) {
4819 this.mergeElementsOnToSelectors(currentElements, newSelectors);
4822 // loop through our current selectors
4823 for (j = 0; j < newSelectors.length; j++) {
4824 sel = newSelectors[j];
4825 // if we don't have any parent paths, the & might be in a mixin so that it can be used
4826 // whether there are parents or not
4827 if (context.length === 0) {
4828 // the combinator used on el should now be applied to the next element instead so that
4830 if (sel.length > 0) {
4831 sel[0].elements = sel[0].elements.slice(0);
4832 sel[0].elements.push(new(tree.Element)(el.combinator, '', 0, el.index, el.currentFileInfo));
4834 selectorsMultiplied.push(sel);
4837 // and the parent selectors
4838 for (k = 0; k < context.length; k++) {
4839 parentSel = context[k];
4840 // We need to put the current selectors
4841 // then join the last selector's elements on to the parents selectors
4843 // our new selector path
4844 newSelectorPath = [];
4845 // selectors from the parent after the join
4846 afterParentJoin = [];
4847 newJoinedSelectorEmpty = true;
4849 //construct the joined selector - if & is the first thing this will be empty,
4850 // if not newJoinedSelector will be the last set of elements in the selector
4851 if (sel.length > 0) {
4852 newSelectorPath = sel.slice(0);
4853 lastSelector = newSelectorPath.pop();
4854 newJoinedSelector = selector.createDerived(lastSelector.elements.slice(0));
4855 newJoinedSelectorEmpty = false;
4858 newJoinedSelector = selector.createDerived([]);
4861 //put together the parent selectors after the join
4862 if (parentSel.length > 1) {
4863 afterParentJoin = afterParentJoin.concat(parentSel.slice(1));
4866 if (parentSel.length > 0) {
4867 newJoinedSelectorEmpty = false;
4869 // join the elements so far with the first part of the parent
4870 newJoinedSelector.elements.push(new(tree.Element)(el.combinator, parentSel[0].elements[0].value, el.index, el.currentFileInfo));
4871 newJoinedSelector.elements = newJoinedSelector.elements.concat(parentSel[0].elements.slice(1));
4874 if (!newJoinedSelectorEmpty) {
4875 // now add the joined selector
4876 newSelectorPath.push(newJoinedSelector);
4879 // and the rest of the parent
4880 newSelectorPath = newSelectorPath.concat(afterParentJoin);
4882 // add that to our new set of selectors
4883 selectorsMultiplied.push(newSelectorPath);
4888 // our new selectors has been multiplied, so reset the state
4889 newSelectors = selectorsMultiplied;
4890 currentElements = [];
4894 // if we have any elements left over (e.g. .a& .b == .b)
4895 // add them on to all the current selectors
4896 if (currentElements.length > 0) {
4897 this.mergeElementsOnToSelectors(currentElements, newSelectors);
4900 for (i = 0; i < newSelectors.length; i++) {
4901 if (newSelectors[i].length > 0) {
4902 paths.push(newSelectors[i]);
4907 mergeElementsOnToSelectors: function(elements, selectors) {
4910 if (selectors.length === 0) {
4911 selectors.push([ new(tree.Selector)(elements) ]);
4915 for (i = 0; i < selectors.length; i++) {
4918 // if the previous thing in sel is a parent this needs to join on to it
4919 if (sel.length > 0) {
4920 sel[sel.length - 1] = sel[sel.length - 1].createDerived(sel[sel.length - 1].elements.concat(elements));
4923 sel.push(new(tree.Selector)(elements));
4928 })(require('../tree'));
4932 tree.Selector = function (elements, extendList, condition, index, currentFileInfo, isReferenced) {
4933 this.elements = elements;
4934 this.extendList = extendList || [];
4935 this.condition = condition;
4936 this.currentFileInfo = currentFileInfo || {};
4937 this.isReferenced = isReferenced;
4939 this.evaldCondition = true;
4942 tree.Selector.prototype = {
4944 accept: function (visitor) {
4945 this.elements = visitor.visit(this.elements);
4946 this.extendList = visitor.visit(this.extendList);
4947 this.condition = visitor.visit(this.condition);
4949 createDerived: function(elements, extendList, evaldCondition) {
4950 /*jshint eqnull:true */
4951 evaldCondition = evaldCondition != null ? evaldCondition : this.evaldCondition;
4952 var newSelector = new(tree.Selector)(elements, extendList || this.extendList, this.condition, this.index, this.currentFileInfo, this.isReferenced);
4953 newSelector.evaldCondition = evaldCondition;
4956 match: function (other) {
4957 var elements = this.elements,
4958 len = elements.length,
4959 oelements, olen, max, i;
4961 oelements = other.elements.slice(
4962 (other.elements.length && other.elements[0].value === "&") ? 1 : 0);
4963 olen = oelements.length;
4964 max = Math.min(len, olen);
4966 if (olen === 0 || len < olen) {
4969 for (i = 0; i < max; i++) {
4970 if (elements[i].value !== oelements[i].value) {
4975 return max; // return number of matched selectors
4977 eval: function (env) {
4978 var evaldCondition = this.condition && this.condition.eval(env);
4980 return this.createDerived(this.elements.map(function (e) {
4982 }), this.extendList.map(function(extend) {
4983 return extend.eval(env);
4984 }), evaldCondition);
4986 genCSS: function (env, output) {
4988 if ((!env || !env.firstSelector) && this.elements[0].combinator.value === "") {
4989 output.add(' ', this.currentFileInfo, this.index);
4992 //TODO caching? speed comparison?
4993 for(i = 0; i < this.elements.length; i++) {
4994 element = this.elements[i];
4995 element.genCSS(env, output);
5000 markReferenced: function () {
5001 this.isReferenced = true;
5003 getIsReferenced: function() {
5004 return !this.currentFileInfo.reference || this.isReferenced;
5006 getIsOutput: function() {
5007 return this.evaldCondition;
5011 })(require('../tree'));
5015 tree.UnicodeDescriptor = function (value) {
5018 tree.UnicodeDescriptor.prototype = {
5019 type: "UnicodeDescriptor",
5020 genCSS: function (env, output) {
5021 output.add(this.value);
5024 eval: function () { return this; }
5027 })(require('../tree'));
5031 tree.URL = function (val, currentFileInfo) {
5033 this.currentFileInfo = currentFileInfo;
5035 tree.URL.prototype = {
5037 accept: function (visitor) {
5038 this.value = visitor.visit(this.value);
5040 genCSS: function (env, output) {
5042 this.value.genCSS(env, output);
5046 eval: function (ctx) {
5047 var val = this.value.eval(ctx), rootpath;
5049 // Add the base path if the URL is relative
5050 rootpath = this.currentFileInfo && this.currentFileInfo.rootpath;
5051 if (rootpath && typeof val.value === "string" && ctx.isPathRelative(val.value)) {
5053 rootpath = rootpath.replace(/[\(\)'"\s]/g, function(match) { return "\\"+match; });
5055 val.value = rootpath + val.value;
5058 val.value = ctx.normalizePath(val.value);
5060 return new(tree.URL)(val, null);
5064 })(require('../tree'));
5068 tree.Value = function (value) {
5071 tree.Value.prototype = {
5073 accept: function (visitor) {
5074 this.value = visitor.visit(this.value);
5076 eval: function (env) {
5077 if (this.value.length === 1) {
5078 return this.value[0].eval(env);
5080 return new(tree.Value)(this.value.map(function (v) {
5085 genCSS: function (env, output) {
5087 for(i = 0; i < this.value.length; i++) {
5088 this.value[i].genCSS(env, output);
5089 if (i+1 < this.value.length) {
5090 output.add((env && env.compress) ? ',' : ', ');
5097 })(require('../tree'));
5101 tree.Variable = function (name, index, currentFileInfo) {
5104 this.currentFileInfo = currentFileInfo;
5106 tree.Variable.prototype = {
5108 eval: function (env) {
5109 var variable, v, name = this.name;
5111 if (name.indexOf('@@') === 0) {
5112 name = '@' + new(tree.Variable)(name.slice(1)).eval(env).value;
5115 if (this.evaluating) {
5116 throw { type: 'Name',
5117 message: "Recursive variable definition for " + name,
5118 filename: this.currentFileInfo.file,
5119 index: this.index };
5122 this.evaluating = true;
5124 if (variable = tree.find(env.frames, function (frame) {
5125 if (v = frame.variable(name)) {
5126 return v.value.eval(env);
5129 this.evaluating = false;
5133 throw { type: 'Name',
5134 message: "variable " + name + " is undefined",
5135 filename: this.currentFileInfo.filename,
5136 index: this.index };
5141 })(require('../tree'));
5145 var parseCopyProperties = [
5146 'paths', // option - unmodified - paths to search for imports on
5147 'optimization', // option - optimization level (for the chunker)
5148 'files', // list of files that have been imported, used for import-once
5149 'contents', // browser-only, contents of all the files
5150 'relativeUrls', // option - whether to adjust URL's to be relative
5151 'rootpath', // option - rootpath to append to URL's
5152 'strictImports', // option -
5153 'insecure', // option - whether to allow imports from insecure ssl hosts
5154 'dumpLineNumbers', // option - whether to dump line numbers
5155 'compress', // option - whether to compress
5156 'processImports', // option - whether to process imports. if false then imports will not be imported
5157 'syncImport', // option - whether to import synchronously
5158 'javascriptEnabled',// option - whether JavaScript is enabled. if undefined, defaults to true
5159 'mime', // browser only - mime type for sheet import
5160 'useFileCache', // browser only - whether to use the per file session cache
5161 'currentFileInfo' // information about the current file - for error reporting and importing and making urls relative etc.
5164 //currentFileInfo = {
5165 // 'relativeUrls' - option - whether to adjust URL's to be relative
5166 // 'filename' - full resolved filename of current file
5167 // 'rootpath' - path to append to normal URLs for this node
5168 // 'currentDirectory' - path to the current file, absolute
5169 // 'rootFilename' - filename of the base file
5170 // 'entryPath' - absolute path to the entry file
5171 // 'reference' - whether the file should not be output and only output parts that are referenced
5173 tree.parseEnv = function(options) {
5174 copyFromOriginal(options, this, parseCopyProperties);
5176 if (!this.contents) { this.contents = {}; }
5177 if (!this.files) { this.files = {}; }
5179 if (!this.currentFileInfo) {
5180 var filename = (options && options.filename) || "input";
5181 var entryPath = filename.replace(/[^\/\\]*$/, "");
5183 options.filename = null;
5185 this.currentFileInfo = {
5187 relativeUrls: this.relativeUrls,
5188 rootpath: (options && options.rootpath) || "",
5189 currentDirectory: entryPath,
5190 entryPath: entryPath,
5191 rootFilename: filename
5196 var evalCopyProperties = [
5197 'silent', // whether to swallow errors and warnings
5198 'verbose', // whether to log more activity
5199 'compress', // whether to compress
5200 'yuicompress', // whether to compress with the outside tool yui compressor
5201 'ieCompat', // whether to enforce IE compatibility (IE8 data-uri)
5202 'strictMath', // whether math has to be within parenthesis
5203 'strictUnits', // whether units need to evaluate correctly
5204 'cleancss', // whether to compress with clean-css
5205 'sourceMap', // whether to output a source map
5206 'importMultiple'// whether we are currently importing multiple copies
5209 tree.evalEnv = function(options, frames) {
5210 copyFromOriginal(options, this, evalCopyProperties);
5212 this.frames = frames || [];
5215 tree.evalEnv.prototype.inParenthesis = function () {
5216 if (!this.parensStack) {
5217 this.parensStack = [];
5219 this.parensStack.push(true);
5222 tree.evalEnv.prototype.outOfParenthesis = function () {
5223 this.parensStack.pop();
5226 tree.evalEnv.prototype.isMathOn = function () {
5227 return this.strictMath ? (this.parensStack && this.parensStack.length) : true;
5230 tree.evalEnv.prototype.isPathRelative = function (path) {
5231 return !/^(?:[a-z-]+:|\/)/.test(path);
5234 tree.evalEnv.prototype.normalizePath = function( path ) {
5236 segments = path.split("/").reverse(),
5240 while (segments.length !== 0 ) {
5241 segment = segments.pop();
5246 if ((path.length === 0) || (path[path.length - 1] === "..")) {
5247 path.push( segment );
5253 path.push( segment );
5258 return path.join("/");
5261 //todo - do the same for the toCSS env
5262 //tree.toCSSEnv = function (options) {
5265 var copyFromOriginal = function(original, destination, propertiesToCopy) {
5266 if (!original) { return; }
5268 for(var i = 0; i < propertiesToCopy.length; i++) {
5269 if (original.hasOwnProperty(propertiesToCopy[i])) {
5270 destination[propertiesToCopy[i]] = original[propertiesToCopy[i]];
5275 })(require('./tree'));
5279 tree.visitor = function(implementation) {
5280 this._implementation = implementation;
5283 tree.visitor.prototype = {
5284 visit: function(node) {
5286 if (node instanceof Array) {
5287 return this.visitArray(node);
5290 if (!node || !node.type) {
5294 var funcName = "visit" + node.type,
5295 func = this._implementation[funcName],
5298 visitArgs = {visitDeeper: true};
5299 newNode = func.call(this._implementation, node, visitArgs);
5300 if (this._implementation.isReplacing) {
5304 if ((!visitArgs || visitArgs.visitDeeper) && node && node.accept) {
5307 funcName = funcName + "Out";
5308 if (this._implementation[funcName]) {
5309 this._implementation[funcName](node);
5313 visitArray: function(nodes) {
5314 var i, newNodes = [];
5315 for(i = 0; i < nodes.length; i++) {
5316 var evald = this.visit(nodes[i]);
5317 if (evald instanceof Array) {
5318 evald = this.flatten(evald);
5319 newNodes = newNodes.concat(evald);
5321 newNodes.push(evald);
5324 if (this._implementation.isReplacing) {
5329 doAccept: function (node) {
5332 flatten: function(arr, master) {
5333 return arr.reduce(this.flattenReduce.bind(this), master || []);
5335 flattenReduce: function(sum, element) {
5336 if (element instanceof Array) {
5337 sum = this.flatten(element, sum);
5345 })(require('./tree'));
5347 tree.importVisitor = function(importer, finish, evalEnv) {
5348 this._visitor = new tree.visitor(this);
5349 this._importer = importer;
5350 this._finish = finish;
5351 this.env = evalEnv || new tree.evalEnv();
5352 this.importCount = 0;
5355 tree.importVisitor.prototype = {
5357 run: function (root) {
5360 // process the contents
5361 this._visitor.visit(root);
5367 this.isFinished = true;
5369 if (this.importCount === 0) {
5370 this._finish(error);
5373 visitImport: function (importNode, visitArgs) {
5374 var importVisitor = this,
5376 inlineCSS = importNode.options.inline;
5378 if (!importNode.css || inlineCSS) {
5381 evaldImportNode = importNode.evalForImport(this.env);
5383 if (!e.filename) { e.index = importNode.index; e.filename = importNode.currentFileInfo.filename; }
5384 // attempt to eval properly and treat as css
5385 importNode.css = true;
5386 // if that fails, this error will be thrown
5387 importNode.error = e;
5390 if (evaldImportNode && (!evaldImportNode.css || inlineCSS)) {
5391 importNode = evaldImportNode;
5393 var env = new tree.evalEnv(this.env, this.env.frames.slice(0));
5395 if (importNode.options.multiple) {
5396 env.importMultiple = true;
5399 this._importer.push(importNode.getPath(), importNode.currentFileInfo, importNode.options, function (e, root, imported, fullPath) {
5400 if (e && !e.filename) { e.index = importNode.index; e.filename = importNode.currentFileInfo.filename; }
5402 if (imported && !env.importMultiple) { importNode.skip = imported; }
5404 var subFinish = function(e) {
5405 importVisitor.importCount--;
5407 if (importVisitor.importCount === 0 && importVisitor.isFinished) {
5408 importVisitor._finish(e);
5413 importNode.root = root;
5414 importNode.importedFilename = fullPath;
5415 if (!inlineCSS && !importNode.skip) {
5416 new(tree.importVisitor)(importVisitor._importer, subFinish, env)
5426 visitArgs.visitDeeper = false;
5429 visitRule: function (ruleNode, visitArgs) {
5430 visitArgs.visitDeeper = false;
5433 visitDirective: function (directiveNode, visitArgs) {
5434 this.env.frames.unshift(directiveNode);
5435 return directiveNode;
5437 visitDirectiveOut: function (directiveNode) {
5438 this.env.frames.shift();
5440 visitMixinDefinition: function (mixinDefinitionNode, visitArgs) {
5441 this.env.frames.unshift(mixinDefinitionNode);
5442 return mixinDefinitionNode;
5444 visitMixinDefinitionOut: function (mixinDefinitionNode) {
5445 this.env.frames.shift();
5447 visitRuleset: function (rulesetNode, visitArgs) {
5448 this.env.frames.unshift(rulesetNode);
5451 visitRulesetOut: function (rulesetNode) {
5452 this.env.frames.shift();
5454 visitMedia: function (mediaNode, visitArgs) {
5455 this.env.frames.unshift(mediaNode.ruleset);
5458 visitMediaOut: function (mediaNode) {
5459 this.env.frames.shift();
5463 })(require('./tree'));
5465 tree.joinSelectorVisitor = function() {
5466 this.contexts = [[]];
5467 this._visitor = new tree.visitor(this);
5470 tree.joinSelectorVisitor.prototype = {
5471 run: function (root) {
5472 return this._visitor.visit(root);
5474 visitRule: function (ruleNode, visitArgs) {
5475 visitArgs.visitDeeper = false;
5477 visitMixinDefinition: function (mixinDefinitionNode, visitArgs) {
5478 visitArgs.visitDeeper = false;
5481 visitRuleset: function (rulesetNode, visitArgs) {
5482 var context = this.contexts[this.contexts.length - 1];
5484 this.contexts.push(paths);
5486 if (! rulesetNode.root) {
5487 rulesetNode.selectors = rulesetNode.selectors.filter(function(selector) { return selector.getIsOutput(); });
5488 if (rulesetNode.selectors.length === 0) {
5489 rulesetNode.rules.length = 0;
5491 rulesetNode.joinSelectors(paths, context, rulesetNode.selectors);
5492 rulesetNode.paths = paths;
5495 visitRulesetOut: function (rulesetNode) {
5496 this.contexts.length = this.contexts.length - 1;
5498 visitMedia: function (mediaNode, visitArgs) {
5499 var context = this.contexts[this.contexts.length - 1];
5500 mediaNode.rules[0].root = (context.length === 0 || context[0].multiMedia);
5504 })(require('./tree'));
5506 tree.toCSSVisitor = function(env) {
5507 this._visitor = new tree.visitor(this);
5511 tree.toCSSVisitor.prototype = {
5513 run: function (root) {
5514 return this._visitor.visit(root);
5517 visitRule: function (ruleNode, visitArgs) {
5518 if (ruleNode.variable) {
5524 visitMixinDefinition: function (mixinNode, visitArgs) {
5528 visitExtend: function (extendNode, visitArgs) {
5532 visitComment: function (commentNode, visitArgs) {
5533 if (commentNode.isSilent(this._env)) {
5539 visitMedia: function(mediaNode, visitArgs) {
5540 mediaNode.accept(this._visitor);
5541 visitArgs.visitDeeper = false;
5543 if (!mediaNode.rules.length) {
5549 visitDirective: function(directiveNode, visitArgs) {
5550 if (directiveNode.currentFileInfo.reference && !directiveNode.isReferenced) {
5553 if (directiveNode.name === "@charset") {
5554 // Only output the debug info together with subsequent @charset definitions
5555 // a comment (or @media statement) before the actual @charset directive would
5556 // be considered illegal css as it has to be on the first line
5558 if (directiveNode.debugInfo) {
5559 var comment = new tree.Comment("/* " + directiveNode.toCSS(this._env).replace(/\n/g, "")+" */\n");
5560 comment.debugInfo = directiveNode.debugInfo;
5561 return this._visitor.visit(comment);
5565 this.charset = true;
5567 return directiveNode;
5570 checkPropertiesInRoot: function(rules) {
5572 for(var i = 0; i < rules.length; i++) {
5573 ruleNode = rules[i];
5574 if (ruleNode instanceof tree.Rule && !ruleNode.variable) {
5575 throw { message: "properties must be inside selector blocks, they cannot be in the root.",
5576 index: ruleNode.index, filename: ruleNode.currentFileInfo ? ruleNode.currentFileInfo.filename : null};
5581 visitRuleset: function (rulesetNode, visitArgs) {
5582 var rule, rulesets = [];
5583 if (rulesetNode.firstRoot) {
5584 this.checkPropertiesInRoot(rulesetNode.rules);
5586 if (! rulesetNode.root) {
5588 rulesetNode.paths = rulesetNode.paths
5589 .filter(function(p) {
5591 if (p[0].elements[0].combinator.value === ' ') {
5592 p[0].elements[0].combinator = new(tree.Combinator)('');
5594 for(i = 0; i < p.length; i++) {
5595 if (p[i].getIsReferenced() && p[i].getIsOutput()) {
5602 // Compile rules and rulesets
5603 for (var i = 0; i < rulesetNode.rules.length; i++) {
5604 rule = rulesetNode.rules[i];
5607 // visit because we are moving them out from being a child
5608 rulesets.push(this._visitor.visit(rule));
5609 rulesetNode.rules.splice(i, 1);
5614 // accept the visitor to remove rules and refactor itself
5615 // then we can decide now whether we want it or not
5616 if (rulesetNode.rules.length > 0) {
5617 rulesetNode.accept(this._visitor);
5619 visitArgs.visitDeeper = false;
5621 this._mergeRules(rulesetNode.rules);
5622 this._removeDuplicateRules(rulesetNode.rules);
5624 // now decide whether we keep the ruleset
5625 if (rulesetNode.rules.length > 0 && rulesetNode.paths.length > 0) {
5626 rulesets.splice(0, 0, rulesetNode);
5629 rulesetNode.accept(this._visitor);
5630 visitArgs.visitDeeper = false;
5631 if (rulesetNode.firstRoot || rulesetNode.rules.length > 0) {
5632 rulesets.splice(0, 0, rulesetNode);
5635 if (rulesets.length === 1) {
5641 _removeDuplicateRules: function(rules) {
5642 // remove duplicates
5645 for(i = rules.length - 1; i >= 0 ; i--) {
5647 if (rule instanceof tree.Rule) {
5648 if (!ruleCache[rule.name]) {
5649 ruleCache[rule.name] = rule;
5651 ruleList = ruleCache[rule.name];
5652 if (ruleList instanceof tree.Rule) {
5653 ruleList = ruleCache[rule.name] = [ruleCache[rule.name].toCSS(this._env)];
5655 var ruleCSS = rule.toCSS(this._env);
5656 if (ruleList.indexOf(ruleCSS) !== -1) {
5659 ruleList.push(ruleCSS);
5666 _mergeRules: function (rules) {
5672 for (var i = 0; i < rules.length; i++) {
5675 if ((rule instanceof tree.Rule) && rule.merge) {
5677 rule.important ? "!" : ""].join(",");
5680 parts = groups[key] = [];
5682 rules.splice(i--, 1);
5689 Object.keys(groups).map(function (k) {
5692 if (parts.length > 1) {
5695 rule.value = new (tree.Value)(parts.map(function (p) {
5703 })(require('./tree'));
5705 /*jshint loopfunc:true */
5707 tree.extendFinderVisitor = function() {
5708 this._visitor = new tree.visitor(this);
5710 this.allExtendsStack = [[]];
5713 tree.extendFinderVisitor.prototype = {
5714 run: function (root) {
5715 root = this._visitor.visit(root);
5716 root.allExtends = this.allExtendsStack[0];
5719 visitRule: function (ruleNode, visitArgs) {
5720 visitArgs.visitDeeper = false;
5722 visitMixinDefinition: function (mixinDefinitionNode, visitArgs) {
5723 visitArgs.visitDeeper = false;
5725 visitRuleset: function (rulesetNode, visitArgs) {
5727 if (rulesetNode.root) {
5731 var i, j, extend, allSelectorsExtendList = [], extendList;
5733 // get &:extend(.a); rules which apply to all selectors in this ruleset
5734 for(i = 0; i < rulesetNode.rules.length; i++) {
5735 if (rulesetNode.rules[i] instanceof tree.Extend) {
5736 allSelectorsExtendList.push(rulesetNode.rules[i]);
5737 rulesetNode.extendOnEveryPath = true;
5741 // now find every selector and apply the extends that apply to all extends
5742 // and the ones which apply to an individual extend
5743 for(i = 0; i < rulesetNode.paths.length; i++) {
5744 var selectorPath = rulesetNode.paths[i],
5745 selector = selectorPath[selectorPath.length-1];
5746 extendList = selector.extendList.slice(0).concat(allSelectorsExtendList).map(function(allSelectorsExtend) {
5747 return allSelectorsExtend.clone();
5749 for(j = 0; j < extendList.length; j++) {
5750 this.foundExtends = true;
5751 extend = extendList[j];
5752 extend.findSelfSelectors(selectorPath);
5753 extend.ruleset = rulesetNode;
5754 if (j === 0) { extend.firstExtendOnThisSelectorPath = true; }
5755 this.allExtendsStack[this.allExtendsStack.length-1].push(extend);
5759 this.contexts.push(rulesetNode.selectors);
5761 visitRulesetOut: function (rulesetNode) {
5762 if (!rulesetNode.root) {
5763 this.contexts.length = this.contexts.length - 1;
5766 visitMedia: function (mediaNode, visitArgs) {
5767 mediaNode.allExtends = [];
5768 this.allExtendsStack.push(mediaNode.allExtends);
5770 visitMediaOut: function (mediaNode) {
5771 this.allExtendsStack.length = this.allExtendsStack.length - 1;
5773 visitDirective: function (directiveNode, visitArgs) {
5774 directiveNode.allExtends = [];
5775 this.allExtendsStack.push(directiveNode.allExtends);
5777 visitDirectiveOut: function (directiveNode) {
5778 this.allExtendsStack.length = this.allExtendsStack.length - 1;
5782 tree.processExtendsVisitor = function() {
5783 this._visitor = new tree.visitor(this);
5786 tree.processExtendsVisitor.prototype = {
5787 run: function(root) {
5788 var extendFinder = new tree.extendFinderVisitor();
5789 extendFinder.run(root);
5790 if (!extendFinder.foundExtends) { return root; }
5791 root.allExtends = root.allExtends.concat(this.doExtendChaining(root.allExtends, root.allExtends));
5792 this.allExtendsStack = [root.allExtends];
5793 return this._visitor.visit(root);
5795 doExtendChaining: function (extendsList, extendsListTarget, iterationCount) {
5797 // chaining is different from normal extension.. if we extend an extend then we are not just copying, altering and pasting
5798 // the selector we would do normally, but we are also adding an extend with the same target selector
5799 // this means this new extend can then go and alter other extends
5801 // this method deals with all the chaining work - without it, extend is flat and doesn't work on other extend selectors
5802 // this is also the most expensive.. and a match on one selector can cause an extension of a selector we had already processed if
5803 // we look at each selector at a time, as is done in visitRuleset
5805 var extendIndex, targetExtendIndex, matches, extendsToAdd = [], newSelector, extendVisitor = this, selectorPath, extend, targetExtend, newExtend;
5807 iterationCount = iterationCount || 0;
5809 //loop through comparing every extend with every target extend.
5810 // a target extend is the one on the ruleset we are looking at copy/edit/pasting in place
5811 // e.g. .a:extend(.b) {} and .b:extend(.c) {} then the first extend extends the second one
5812 // and the second is the target.
5813 // the seperation into two lists allows us to process a subset of chains with a bigger set, as is the
5814 // case when processing media queries
5815 for(extendIndex = 0; extendIndex < extendsList.length; extendIndex++){
5816 for(targetExtendIndex = 0; targetExtendIndex < extendsListTarget.length; targetExtendIndex++){
5818 extend = extendsList[extendIndex];
5819 targetExtend = extendsListTarget[targetExtendIndex];
5821 // look for circular references
5822 if (this.inInheritanceChain(targetExtend, extend)) { continue; }
5824 // find a match in the target extends self selector (the bit before :extend)
5825 selectorPath = [targetExtend.selfSelectors[0]];
5826 matches = extendVisitor.findMatch(extend, selectorPath);
5828 if (matches.length) {
5830 // we found a match, so for each self selector..
5831 extend.selfSelectors.forEach(function(selfSelector) {
5833 // process the extend as usual
5834 newSelector = extendVisitor.extendSelector(matches, selectorPath, selfSelector);
5836 // but now we create a new extend from it
5837 newExtend = new(tree.Extend)(targetExtend.selector, targetExtend.option, 0);
5838 newExtend.selfSelectors = newSelector;
5840 // add the extend onto the list of extends for that selector
5841 newSelector[newSelector.length-1].extendList = [newExtend];
5843 // record that we need to add it.
5844 extendsToAdd.push(newExtend);
5845 newExtend.ruleset = targetExtend.ruleset;
5847 //remember its parents for circular references
5848 newExtend.parents = [targetExtend, extend];
5850 // only process the selector once.. if we have :extend(.a,.b) then multiple
5851 // extends will look at the same selector path, so when extending
5852 // we know that any others will be duplicates in terms of what is added to the css
5853 if (targetExtend.firstExtendOnThisSelectorPath) {
5854 newExtend.firstExtendOnThisSelectorPath = true;
5855 targetExtend.ruleset.paths.push(newSelector);
5862 if (extendsToAdd.length) {
5863 // try to detect circular references to stop a stack overflow.
5864 // may no longer be needed.
5865 this.extendChainCount++;
5866 if (iterationCount > 100) {
5867 var selectorOne = "{unable to calculate}";
5868 var selectorTwo = "{unable to calculate}";
5871 selectorOne = extendsToAdd[0].selfSelectors[0].toCSS();
5872 selectorTwo = extendsToAdd[0].selector.toCSS();
5875 throw {message: "extend circular reference detected. One of the circular extends is currently:"+selectorOne+":extend(" + selectorTwo+")"};
5878 // now process the new extends on the existing rules so that we can handle a extending b extending c ectending d extending e...
5879 return extendsToAdd.concat(extendVisitor.doExtendChaining(extendsToAdd, extendsListTarget, iterationCount+1));
5881 return extendsToAdd;
5884 inInheritanceChain: function (possibleParent, possibleChild) {
5885 if (possibleParent === possibleChild) {
5888 if (possibleChild.parents) {
5889 if (this.inInheritanceChain(possibleParent, possibleChild.parents[0])) {
5892 if (this.inInheritanceChain(possibleParent, possibleChild.parents[1])) {
5898 visitRule: function (ruleNode, visitArgs) {
5899 visitArgs.visitDeeper = false;
5901 visitMixinDefinition: function (mixinDefinitionNode, visitArgs) {
5902 visitArgs.visitDeeper = false;
5904 visitSelector: function (selectorNode, visitArgs) {
5905 visitArgs.visitDeeper = false;
5907 visitRuleset: function (rulesetNode, visitArgs) {
5908 if (rulesetNode.root) {
5911 var matches, pathIndex, extendIndex, allExtends = this.allExtendsStack[this.allExtendsStack.length-1], selectorsToAdd = [], extendVisitor = this, selectorPath;
5913 // look at each selector path in the ruleset, find any extend matches and then copy, find and replace
5915 for(extendIndex = 0; extendIndex < allExtends.length; extendIndex++) {
5916 for(pathIndex = 0; pathIndex < rulesetNode.paths.length; pathIndex++) {
5918 selectorPath = rulesetNode.paths[pathIndex];
5920 // extending extends happens initially, before the main pass
5921 if (rulesetNode.extendOnEveryPath || selectorPath[selectorPath.length-1].extendList.length) { continue; }
5923 matches = this.findMatch(allExtends[extendIndex], selectorPath);
5925 if (matches.length) {
5927 allExtends[extendIndex].selfSelectors.forEach(function(selfSelector) {
5928 selectorsToAdd.push(extendVisitor.extendSelector(matches, selectorPath, selfSelector));
5933 rulesetNode.paths = rulesetNode.paths.concat(selectorsToAdd);
5935 findMatch: function (extend, haystackSelectorPath) {
5937 // look through the haystack selector path to try and find the needle - extend.selector
5938 // returns an array of selector matches that can then be replaced
5940 var haystackSelectorIndex, hackstackSelector, hackstackElementIndex, haystackElement,
5941 targetCombinator, i,
5942 extendVisitor = this,
5943 needleElements = extend.selector.elements,
5944 potentialMatches = [], potentialMatch, matches = [];
5946 // loop through the haystack elements
5947 for(haystackSelectorIndex = 0; haystackSelectorIndex < haystackSelectorPath.length; haystackSelectorIndex++) {
5948 hackstackSelector = haystackSelectorPath[haystackSelectorIndex];
5950 for(hackstackElementIndex = 0; hackstackElementIndex < hackstackSelector.elements.length; hackstackElementIndex++) {
5952 haystackElement = hackstackSelector.elements[hackstackElementIndex];
5954 // if we allow elements before our match we can add a potential match every time. otherwise only at the first element.
5955 if (extend.allowBefore || (haystackSelectorIndex === 0 && hackstackElementIndex === 0)) {
5956 potentialMatches.push({pathIndex: haystackSelectorIndex, index: hackstackElementIndex, matched: 0, initialCombinator: haystackElement.combinator});
5959 for(i = 0; i < potentialMatches.length; i++) {
5960 potentialMatch = potentialMatches[i];
5962 // selectors add " " onto the first element. When we use & it joins the selectors together, but if we don't
5963 // then each selector in haystackSelectorPath has a space before it added in the toCSS phase. so we need to work out
5964 // what the resulting combinator will be
5965 targetCombinator = haystackElement.combinator.value;
5966 if (targetCombinator === '' && hackstackElementIndex === 0) {
5967 targetCombinator = ' ';
5970 // if we don't match, null our match to indicate failure
5971 if (!extendVisitor.isElementValuesEqual(needleElements[potentialMatch.matched].value, haystackElement.value) ||
5972 (potentialMatch.matched > 0 && needleElements[potentialMatch.matched].combinator.value !== targetCombinator)) {
5973 potentialMatch = null;
5975 potentialMatch.matched++;
5978 // if we are still valid and have finished, test whether we have elements after and whether these are allowed
5979 if (potentialMatch) {
5980 potentialMatch.finished = potentialMatch.matched === needleElements.length;
5981 if (potentialMatch.finished &&
5982 (!extend.allowAfter && (hackstackElementIndex+1 < hackstackSelector.elements.length || haystackSelectorIndex+1 < haystackSelectorPath.length))) {
5983 potentialMatch = null;
5986 // if null we remove, if not, we are still valid, so either push as a valid match or continue
5987 if (potentialMatch) {
5988 if (potentialMatch.finished) {
5989 potentialMatch.length = needleElements.length;
5990 potentialMatch.endPathIndex = haystackSelectorIndex;
5991 potentialMatch.endPathElementIndex = hackstackElementIndex + 1; // index after end of match
5992 potentialMatches.length = 0; // we don't allow matches to overlap, so start matching again
5993 matches.push(potentialMatch);
5996 potentialMatches.splice(i, 1);
6004 isElementValuesEqual: function(elementValue1, elementValue2) {
6005 if (typeof elementValue1 === "string" || typeof elementValue2 === "string") {
6006 return elementValue1 === elementValue2;
6008 if (elementValue1 instanceof tree.Attribute) {
6009 if (elementValue1.op !== elementValue2.op || elementValue1.key !== elementValue2.key) {
6012 if (!elementValue1.value || !elementValue2.value) {
6013 if (elementValue1.value || elementValue2.value) {
6018 elementValue1 = elementValue1.value.value || elementValue1.value;
6019 elementValue2 = elementValue2.value.value || elementValue2.value;
6020 return elementValue1 === elementValue2;
6022 elementValue1 = elementValue1.value;
6023 elementValue2 = elementValue2.value;
6024 if (elementValue1 instanceof tree.Selector) {
6025 if (!(elementValue2 instanceof tree.Selector) || elementValue1.elements.length !== elementValue2.elements.length) {
6028 for(var i = 0; i <elementValue1.elements.length; i++) {
6029 if (elementValue1.elements[i].combinator.value !== elementValue2.elements[i].combinator.value) {
6030 if (i !== 0 || (elementValue1.elements[i].combinator.value || ' ') !== (elementValue2.elements[i].combinator.value || ' ')) {
6034 if (!this.isElementValuesEqual(elementValue1.elements[i].value, elementValue2.elements[i].value)) {
6042 extendSelector:function (matches, selectorPath, replacementSelector) {
6044 //for a set of matches, replace each match with the replacement selector
6046 var currentSelectorPathIndex = 0,
6047 currentSelectorPathElementIndex = 0,
6055 for (matchIndex = 0; matchIndex < matches.length; matchIndex++) {
6056 match = matches[matchIndex];
6057 selector = selectorPath[match.pathIndex];
6058 firstElement = new tree.Element(
6059 match.initialCombinator,
6060 replacementSelector.elements[0].value,
6061 replacementSelector.elements[0].index,
6062 replacementSelector.elements[0].currentFileInfo
6065 if (match.pathIndex > currentSelectorPathIndex && currentSelectorPathElementIndex > 0) {
6066 path[path.length - 1].elements = path[path.length - 1].elements.concat(selectorPath[currentSelectorPathIndex].elements.slice(currentSelectorPathElementIndex));
6067 currentSelectorPathElementIndex = 0;
6068 currentSelectorPathIndex++;
6071 newElements = selector.elements
6072 .slice(currentSelectorPathElementIndex, match.index)
6073 .concat([firstElement])
6074 .concat(replacementSelector.elements.slice(1));
6076 if (currentSelectorPathIndex === match.pathIndex && matchIndex > 0) {
6077 path[path.length - 1].elements =
6078 path[path.length - 1].elements.concat(newElements);
6080 path = path.concat(selectorPath.slice(currentSelectorPathIndex, match.pathIndex));
6082 path.push(new tree.Selector(
6086 currentSelectorPathIndex = match.endPathIndex;
6087 currentSelectorPathElementIndex = match.endPathElementIndex;
6088 if (currentSelectorPathElementIndex >= selectorPath[currentSelectorPathIndex].elements.length) {
6089 currentSelectorPathElementIndex = 0;
6090 currentSelectorPathIndex++;
6094 if (currentSelectorPathIndex < selectorPath.length && currentSelectorPathElementIndex > 0) {
6095 path[path.length - 1].elements = path[path.length - 1].elements.concat(selectorPath[currentSelectorPathIndex].elements.slice(currentSelectorPathElementIndex));
6096 currentSelectorPathIndex++;
6099 path = path.concat(selectorPath.slice(currentSelectorPathIndex, selectorPath.length));
6103 visitRulesetOut: function (rulesetNode) {
6105 visitMedia: function (mediaNode, visitArgs) {
6106 var newAllExtends = mediaNode.allExtends.concat(this.allExtendsStack[this.allExtendsStack.length-1]);
6107 newAllExtends = newAllExtends.concat(this.doExtendChaining(newAllExtends, mediaNode.allExtends));
6108 this.allExtendsStack.push(newAllExtends);
6110 visitMediaOut: function (mediaNode) {
6111 this.allExtendsStack.length = this.allExtendsStack.length - 1;
6113 visitDirective: function (directiveNode, visitArgs) {
6114 var newAllExtends = directiveNode.allExtends.concat(this.allExtendsStack[this.allExtendsStack.length-1]);
6115 newAllExtends = newAllExtends.concat(this.doExtendChaining(newAllExtends, directiveNode.allExtends));
6116 this.allExtendsStack.push(newAllExtends);
6118 visitDirectiveOut: function (directiveNode) {
6119 this.allExtendsStack.length = this.allExtendsStack.length - 1;
6123 })(require('./tree'));
6127 tree.sourceMapOutput = function (options) {
6129 this._rootNode = options.rootNode;
6130 this._writeSourceMap = options.writeSourceMap;
6131 this._contentsMap = options.contentsMap;
6132 this._sourceMapFilename = options.sourceMapFilename;
6133 this._outputFilename = options.outputFilename;
6134 this._sourceMapBasepath = options.sourceMapBasepath;
6135 this._sourceMapRootpath = options.sourceMapRootpath;
6136 this._outputSourceFiles = options.outputSourceFiles;
6137 this._sourceMapGeneratorConstructor = options.sourceMapGenerator || require("source-map").SourceMapGenerator;
6139 if (this._sourceMapRootpath && this._sourceMapRootpath.charAt(this._sourceMapRootpath.length-1) !== '/') {
6140 this._sourceMapRootpath += '/';
6143 this._lineNumber = 0;
6147 tree.sourceMapOutput.prototype.normalizeFilename = function(filename) {
6148 if (this._sourceMapBasepath && filename.indexOf(this._sourceMapBasepath) === 0) {
6149 filename = filename.substring(this._sourceMapBasepath.length);
6150 if (filename.charAt(0) === '\\' || filename.charAt(0) === '/') {
6151 filename = filename.substring(1);
6154 return (this._sourceMapRootpath || "") + filename.replace(/\\/g, '/');
6157 tree.sourceMapOutput.prototype.add = function(chunk, fileInfo, index, mapLines) {
6159 //ignore adding empty strings
6171 var inputSource = this._contentsMap[fileInfo.filename].substring(0, index);
6172 sourceLines = inputSource.split("\n");
6173 sourceColumns = sourceLines[sourceLines.length-1];
6176 lines = chunk.split("\n");
6177 columns = lines[lines.length-1];
6181 this._sourceMapGenerator.addMapping({ generated: { line: this._lineNumber + 1, column: this._column},
6182 original: { line: sourceLines.length, column: sourceColumns.length},
6183 source: this.normalizeFilename(fileInfo.filename)});
6185 for(i = 0; i < lines.length; i++) {
6186 this._sourceMapGenerator.addMapping({ generated: { line: this._lineNumber + i + 1, column: i === 0 ? this._column : 0},
6187 original: { line: sourceLines.length + i, column: i === 0 ? sourceColumns.length : 0},
6188 source: this.normalizeFilename(fileInfo.filename)});
6193 if (lines.length === 1) {
6194 this._column += columns.length;
6196 this._lineNumber += lines.length - 1;
6197 this._column = columns.length;
6200 this._css.push(chunk);
6203 tree.sourceMapOutput.prototype.isEmpty = function() {
6204 return this._css.length === 0;
6207 tree.sourceMapOutput.prototype.toCSS = function(env) {
6208 this._sourceMapGenerator = new this._sourceMapGeneratorConstructor({ file: this._outputFilename, sourceRoot: null });
6210 if (this._outputSourceFiles) {
6211 for(var filename in this._contentsMap) {
6212 this._sourceMapGenerator.setSourceContent(this.normalizeFilename(filename), this._contentsMap[filename]);
6216 this._rootNode.genCSS(env, this);
6218 if (this._css.length > 0) {
6219 var sourceMapFilename,
6220 sourceMapContent = JSON.stringify(this._sourceMapGenerator.toJSON());
6222 if (this._sourceMapFilename) {
6223 sourceMapFilename = this.normalizeFilename(this._sourceMapFilename);
6226 if (this._writeSourceMap) {
6227 this._writeSourceMap(sourceMapContent);
6229 sourceMapFilename = "data:application/json," + encodeURIComponent(sourceMapContent);
6232 if (sourceMapFilename) {
6233 this._css.push("/*# sourceMappingURL=" + sourceMapFilename + " */");
6237 return this._css.join('');
6240 })(require('./tree'));
6243 // browser.js - client-side engine
6245 /*global less, window, document, XMLHttpRequest, location */
6247 var isFileProtocol = /^(file|chrome(-extension)?|resource|qrc|app):/.test(location.protocol);
6249 less.env = less.env || (location.hostname == '127.0.0.1' ||
6250 location.hostname == '0.0.0.0' ||
6251 location.hostname == 'localhost' ||
6253 location.port.length > 0) ||
6254 isFileProtocol ? 'development'
6263 // The amount of logging in the javascript console.
6264 // 2 - Information and errors
6268 less.logLevel = typeof(less.logLevel) != 'undefined' ? less.logLevel : logLevel.info;
6270 // Load styles asynchronously (default: false)
6272 // This is set to `false` by default, so that the body
6273 // doesn't start loading before the stylesheets are parsed.
6274 // Setting this to `true` can result in flickering.
6276 less.async = less.async || false;
6277 less.fileAsync = less.fileAsync || false;
6279 // Interval between watch polls
6280 less.poll = less.poll || (isFileProtocol ? 1000 : 1500);
6282 //Setup user functions
6283 if (less.functions) {
6284 for(var func in less.functions) {
6285 less.tree.functions[func] = less.functions[func];
6289 var dumpLineNumbers = /!dumpLineNumbers:(comments|mediaquery|all)/.exec(location.hash);
6290 if (dumpLineNumbers) {
6291 less.dumpLineNumbers = dumpLineNumbers[1];
6294 var typePattern = /^text\/(x-)?less$/;
6299 function log(str, level) {
6300 if (less.env == 'development' && typeof(console) !== 'undefined' && less.logLevel >= level) {
6301 console.log('less: ' + str);
6305 function extractId(href) {
6306 return href.replace(/^[a-z-]+:\/+?[^\/]+/, '' ) // Remove protocol & domain
6307 .replace(/^\//, '' ) // Remove root /
6308 .replace(/\.[a-zA-Z]+$/, '' ) // Remove simple extension
6309 .replace(/[^\.\w-]+/g, '-') // Replace illegal characters
6310 .replace(/\./g, ':'); // Replace dots with colons(for valid id)
6313 function errorConsole(e, rootHref) {
6314 var template = '{line} {content}';
6315 var filename = e.filename || rootHref;
6317 var content = (e.type || "Syntax") + "Error: " + (e.message || 'There is an error in your .less file') +
6318 " in " + filename + " ";
6320 var errorline = function (e, i, classname) {
6321 if (e.extract[i] !== undefined) {
6322 errors.push(template.replace(/\{line\}/, (parseInt(e.line, 10) || 0) + (i - 1))
6323 .replace(/\{class\}/, classname)
6324 .replace(/\{content\}/, e.extract[i]));
6329 errorline(e, 0, '');
6330 errorline(e, 1, 'line');
6331 errorline(e, 2, '');
6332 content += 'on line ' + e.line + ', column ' + (e.column + 1) + ':\n' +
6334 } else if (e.stack) {
6337 log(content, logLevel.errors);
6340 function createCSS(styles, sheet, lastModified) {
6341 // Strip the query-string
6342 var href = sheet.href || '';
6344 // If there is no title set, use the filename, minus the extension
6345 var id = 'less:' + (sheet.title || extractId(href));
6347 // If this has already been inserted into the DOM, we may need to replace it
6348 var oldCss = document.getElementById(id);
6349 var keepOldCss = false;
6351 // Create a new stylesheet node for insertion or (if necessary) replacement
6352 var css = document.createElement('style');
6353 css.setAttribute('type', 'text/css');
6355 css.setAttribute('media', sheet.media);
6359 if (css.styleSheet) { // IE
6361 css.styleSheet.cssText = styles;
6363 throw new(Error)("Couldn't reassign styleSheet.cssText.");
6366 css.appendChild(document.createTextNode(styles));
6368 // If new contents match contents of oldCss, don't replace oldCss
6369 keepOldCss = (oldCss !== null && oldCss.childNodes.length > 0 && css.childNodes.length > 0 &&
6370 oldCss.firstChild.nodeValue === css.firstChild.nodeValue);
6373 var head = document.getElementsByTagName('head')[0];
6375 // If there is no oldCss, just append; otherwise, only append if we need
6376 // to replace oldCss with an updated stylesheet
6377 if (oldCss === null || keepOldCss === false) {
6378 var nextEl = sheet && sheet.nextSibling || null;
6380 nextEl.parentNode.insertBefore(css, nextEl);
6382 head.appendChild(css);
6385 if (oldCss && keepOldCss === false) {
6386 oldCss.parentNode.removeChild(oldCss);
6389 // Don't update the local store if the file wasn't modified
6390 if (lastModified && cache) {
6391 log('saving ' + href + ' to cache.', logLevel.info);
6393 cache.setItem(href, styles);
6394 cache.setItem(href + ':timestamp', lastModified);
6396 //TODO - could do with adding more robust error handling
6397 log('failed to save', logLevel.errors);
6402 function errorHTML(e, rootHref) {
6403 var id = 'less-error-message:' + extractId(rootHref || "");
6404 var template = '<li><label>{line}</label><pre class="{class}">{content}</pre></li>';
6405 var elem = document.createElement('div'), timer, content, errors = [];
6406 var filename = e.filename || rootHref;
6407 var filenameNoPath = filename.match(/([^\/]+(\?.*)?)$/)[1];
6410 elem.className = "less-error-message";
6412 content = '<h3>' + (e.type || "Syntax") + "Error: " + (e.message || 'There is an error in your .less file') +
6413 '</h3>' + '<p>in <a href="' + filename + '">' + filenameNoPath + "</a> ";
6415 var errorline = function (e, i, classname) {
6416 if (e.extract[i] !== undefined) {
6417 errors.push(template.replace(/\{line\}/, (parseInt(e.line, 10) || 0) + (i - 1))
6418 .replace(/\{class\}/, classname)
6419 .replace(/\{content\}/, e.extract[i]));
6424 errorline(e, 0, '');
6425 errorline(e, 1, 'line');
6426 errorline(e, 2, '');
6427 content += 'on line ' + e.line + ', column ' + (e.column + 1) + ':</p>' +
6428 '<ul>' + errors.join('') + '</ul>';
6429 } else if (e.stack) {
6430 content += '<br/>' + e.stack.split('\n').slice(1).join('<br/>');
6432 elem.innerHTML = content;
6434 // CSS for error messages
6436 '.less-error-message ul, .less-error-message li {',
6437 'list-style-type: none;',
6438 'margin-right: 15px;',
6442 '.less-error-message label {',
6444 'margin-right: 15px;',
6448 '.less-error-message pre {',
6452 'display: inline-block;',
6454 '.less-error-message pre.line {',
6457 '.less-error-message h3 {',
6459 'font-weight: bold;',
6460 'padding: 15px 0 5px 0;',
6463 '.less-error-message a {',
6466 '.less-error-message .error {',
6468 'font-weight: bold;',
6469 'padding-bottom: 2px;',
6470 'border-bottom: 1px dashed red;',
6472 ].join('\n'), { title: 'error-message' });
6474 elem.style.cssText = [
6475 "font-family: Arial, sans-serif",
6476 "border: 1px solid #e00",
6477 "background-color: #eee",
6478 "border-radius: 5px",
6479 "-webkit-border-radius: 5px",
6480 "-moz-border-radius: 5px",
6483 "margin-bottom: 15px"
6486 if (less.env == 'development') {
6487 timer = setInterval(function () {
6488 if (document.body) {
6489 if (document.getElementById(id)) {
6490 document.body.replaceChild(elem, document.getElementById(id));
6492 document.body.insertBefore(elem, document.body.firstChild);
6494 clearInterval(timer);
6500 function error(e, rootHref) {
6501 if (!less.errorReporting || less.errorReporting === "html") {
6502 errorHTML(e, rootHref);
6503 } else if (less.errorReporting === "console") {
6504 errorConsole(e, rootHref);
6505 } else if (typeof less.errorReporting === 'function') {
6506 less.errorReporting("add", e, rootHref);
6510 function removeErrorHTML(path) {
6511 var node = document.getElementById('less-error-message:' + extractId(path));
6513 node.parentNode.removeChild(node);
6517 function removeErrorConsole(path) {
6521 function removeError(path) {
6522 if (!less.errorReporting || less.errorReporting === "html") {
6523 removeErrorHTML(path);
6524 } else if (less.errorReporting === "console") {
6525 removeErrorConsole(path);
6526 } else if (typeof less.errorReporting === 'function') {
6527 less.errorReporting("remove", path);
6531 function loadStyles(newVars) {
6532 var styles = document.getElementsByTagName('style'),
6534 for (var i = 0; i < styles.length; i++) {
6536 if (style.type.match(typePattern)) {
6537 var env = new less.tree.parseEnv(less),
6538 lessText = style.innerHTML || '';
6539 env.filename = document.location.href.replace(/#.*$/, '');
6541 if (newVars || varsPre) {
6542 env.useFileCache = true;
6544 lessText = varsPre + lessText;
6547 lessText += "\n" + newVars;
6551 /*jshint loopfunc:true */
6552 // use closure to store current value of i
6553 var callback = (function(style) {
6554 return function (e, cssAST) {
6556 return error(e, "inline");
6558 var css = cssAST.toCSS(less);
6559 style.type = 'text/css';
6560 if (style.styleSheet) {
6561 style.styleSheet.cssText = css;
6563 style.innerHTML = css;
6567 new(less.Parser)(env).parse(lessText, callback);
6572 function extractUrlParts(url, baseUrl) {
6573 // urlParts[1] = protocol&hostname || /
6574 // urlParts[2] = / if path relative to host base
6575 // urlParts[3] = directories
6576 // urlParts[4] = filename
6577 // urlParts[5] = parameters
6579 var urlPartsRegex = /^((?:[a-z-]+:)?\/+?(?:[^\/\?#]*\/)|([\/\\]))?((?:[^\/\\\?#]*[\/\\])*)([^\/\\\?#]*)([#\?].*)?$/i,
6580 urlParts = url.match(urlPartsRegex),
6581 returner = {}, directories = [], i, baseUrlParts;
6584 throw new Error("Could not parse sheet href - '"+url+"'");
6587 // Stylesheets in IE don't always return the full path
6588 if (!urlParts[1] || urlParts[2]) {
6589 baseUrlParts = baseUrl.match(urlPartsRegex);
6590 if (!baseUrlParts) {
6591 throw new Error("Could not parse page url - '"+baseUrl+"'");
6593 urlParts[1] = urlParts[1] || baseUrlParts[1] || "";
6595 urlParts[3] = baseUrlParts[3] + urlParts[3];
6600 directories = urlParts[3].replace(/\\/g, "/").split("/");
6602 // extract out . before .. so .. doesn't absorb a non-directory
6603 for(i = 0; i < directories.length; i++) {
6604 if (directories[i] === ".") {
6605 directories.splice(i, 1);
6610 for(i = 0; i < directories.length; i++) {
6611 if (directories[i] === ".." && i > 0) {
6612 directories.splice(i-1, 2);
6618 returner.hostPart = urlParts[1];
6619 returner.directories = directories;
6620 returner.path = urlParts[1] + directories.join("/");
6621 returner.fileUrl = returner.path + (urlParts[4] || "");
6622 returner.url = returner.fileUrl + (urlParts[5] || "");
6626 function pathDiff(url, baseUrl) {
6627 // diff between two paths to create a relative path
6629 var urlParts = extractUrlParts(url),
6630 baseUrlParts = extractUrlParts(baseUrl),
6631 i, max, urlDirectories, baseUrlDirectories, diff = "";
6632 if (urlParts.hostPart !== baseUrlParts.hostPart) {
6635 max = Math.max(baseUrlParts.directories.length, urlParts.directories.length);
6636 for(i = 0; i < max; i++) {
6637 if (baseUrlParts.directories[i] !== urlParts.directories[i]) { break; }
6639 baseUrlDirectories = baseUrlParts.directories.slice(i);
6640 urlDirectories = urlParts.directories.slice(i);
6641 for(i = 0; i < baseUrlDirectories.length-1; i++) {
6644 for(i = 0; i < urlDirectories.length-1; i++) {
6645 diff += urlDirectories[i] + "/";
6650 function getXMLHttpRequest() {
6651 if (window.XMLHttpRequest) {
6652 return new XMLHttpRequest();
6655 /*global ActiveXObject */
6656 return new ActiveXObject("MSXML2.XMLHTTP.3.0");
6658 log("browser doesn't support AJAX.", logLevel.errors);
6664 function doXHR(url, type, callback, errback) {
6665 var xhr = getXMLHttpRequest();
6666 var async = isFileProtocol ? less.fileAsync : less.async;
6668 if (typeof(xhr.overrideMimeType) === 'function') {
6669 xhr.overrideMimeType('text/css');
6671 log("XHR: Getting '" + url + "'", logLevel.info);
6672 xhr.open('GET', url, async);
6673 xhr.setRequestHeader('Accept', type || 'text/x-less, text/css; q=0.9, */*; q=0.5');
6676 function handleResponse(xhr, callback, errback) {
6677 if (xhr.status >= 200 && xhr.status < 300) {
6678 callback(xhr.responseText,
6679 xhr.getResponseHeader("Last-Modified"));
6680 } else if (typeof(errback) === 'function') {
6681 errback(xhr.status, url);
6685 if (isFileProtocol && !less.fileAsync) {
6686 if (xhr.status === 0 || (xhr.status >= 200 && xhr.status < 300)) {
6687 callback(xhr.responseText);
6689 errback(xhr.status, url);
6692 xhr.onreadystatechange = function () {
6693 if (xhr.readyState == 4) {
6694 handleResponse(xhr, callback, errback);
6698 handleResponse(xhr, callback, errback);
6702 function loadFile(originalHref, currentFileInfo, callback, env, newVars) {
6704 if (currentFileInfo && currentFileInfo.currentDirectory && !/^([a-z-]+:)?\//.test(originalHref)) {
6705 originalHref = currentFileInfo.currentDirectory + originalHref;
6708 // sheet may be set to the stylesheet for the initial load or a collection of properties including
6709 // some env variables for imports
6710 var hrefParts = extractUrlParts(originalHref, window.location.href);
6711 var href = hrefParts.url;
6713 currentDirectory: hrefParts.path,
6717 if (currentFileInfo) {
6718 newFileInfo.entryPath = currentFileInfo.entryPath;
6719 newFileInfo.rootpath = currentFileInfo.rootpath;
6720 newFileInfo.rootFilename = currentFileInfo.rootFilename;
6721 newFileInfo.relativeUrls = currentFileInfo.relativeUrls;
6723 newFileInfo.entryPath = hrefParts.path;
6724 newFileInfo.rootpath = less.rootpath || hrefParts.path;
6725 newFileInfo.rootFilename = href;
6726 newFileInfo.relativeUrls = env.relativeUrls;
6729 if (newFileInfo.relativeUrls) {
6731 newFileInfo.rootpath = extractUrlParts(env.rootpath + pathDiff(hrefParts.path, newFileInfo.entryPath)).path;
6733 newFileInfo.rootpath = hrefParts.path;
6737 if (env.useFileCache && fileCache[href]) {
6739 var lessText = fileCache[href];
6741 lessText += "\n" + newVars;
6743 callback(null, lessText, href, newFileInfo, { lastModified: new Date() });
6745 callback(e, null, href);
6750 doXHR(href, env.mime, function (data, lastModified) {
6751 data = varsPre + data;
6754 fileCache[href] = data;
6756 // Use remote copy (re-parse)
6758 callback(null, data, href, newFileInfo, { lastModified: lastModified });
6760 callback(e, null, href);
6762 }, function (status, url) {
6763 callback({ type: 'File', message: "'" + url + "' wasn't found (" + status + ")" }, null, href);
6767 function loadStyleSheet(sheet, callback, reload, remaining, newVars) {
6769 var env = new less.tree.parseEnv(less);
6770 env.mime = sheet.type;
6772 if (newVars || varsPre) {
6773 env.useFileCache = true;
6776 loadFile(sheet.href, null, function(e, data, path, newFileInfo, webInfo) {
6779 webInfo.remaining = remaining;
6781 var css = cache && cache.getItem(path),
6782 timestamp = cache && cache.getItem(path + ':timestamp');
6784 if (!reload && timestamp && webInfo.lastModified &&
6785 (new(Date)(webInfo.lastModified).valueOf() ===
6786 new(Date)(timestamp).valueOf())) {
6788 createCSS(css, sheet);
6789 webInfo.local = true;
6790 callback(null, null, data, sheet, webInfo, path);
6795 //TODO add tests around how this behaves when reloading
6799 env.currentFileInfo = newFileInfo;
6800 new(less.Parser)(env).parse(data, function (e, root) {
6801 if (e) { return callback(e, null, null, sheet); }
6803 callback(e, root, data, sheet, webInfo, path);
6805 callback(e, null, null, sheet);
6809 callback(e, null, null, sheet, webInfo, path);
6814 function loadStyleSheets(callback, reload, newVars) {
6815 for (var i = 0; i < less.sheets.length; i++) {
6816 loadStyleSheet(less.sheets[i], callback, reload, less.sheets.length - (i + 1), newVars);
6820 function initRunningMode(){
6821 if (less.env === 'development') {
6822 less.optimization = 0;
6823 less.watchTimer = setInterval(function () {
6824 if (less.watchMode) {
6825 loadStyleSheets(function (e, root, _, sheet, env) {
6827 error(e, sheet.href);
6829 createCSS(root.toCSS(less), sheet, env.lastModified);
6835 less.optimization = 3;
6839 function serializeVars(vars) {
6842 for (var name in vars) {
6843 s += ((name.slice(0,1) === '@')? '' : '@') + name +': '+
6844 ((vars[name].slice(-1) === ';')? vars[name] : vars[name] +';');
6854 less.watch = function () {
6855 if (!less.watchMode ){
6856 less.env = 'development';
6859 return this.watchMode = true;
6862 less.unwatch = function () {clearInterval(less.watchTimer); return this.watchMode = false; };
6864 if (/!watch/.test(location.hash)) {
6868 if (less.env != 'development') {
6870 cache = (typeof(window.localStorage) === 'undefined') ? null : window.localStorage;
6875 // Get all <link> tags with the 'rel' attribute set to "stylesheet/less"
6877 var links = document.getElementsByTagName('link');
6881 for (var i = 0; i < links.length; i++) {
6882 if (links[i].rel === 'stylesheet/less' || (links[i].rel.match(/stylesheet/) &&
6883 (links[i].type.match(typePattern)))) {
6884 less.sheets.push(links[i]);
6889 // With this function, it's possible to alter variables and re-render
6890 // CSS without reloading less-files
6892 less.modifyVars = function(record) {
6893 less.refresh(false, serializeVars(record));
6896 less.refresh = function (reload, newVars) {
6897 var startTime, endTime;
6898 startTime = endTime = new Date();
6900 loadStyleSheets(function (e, root, _, sheet, env) {
6902 return error(e, sheet.href);
6905 log("loading " + sheet.href + " from cache.", logLevel.info);
6907 log("parsed " + sheet.href + " successfully.", logLevel.info);
6908 createCSS(root.toCSS(less), sheet, env.lastModified);
6910 log("css for " + sheet.href + " generated in " + (new Date() - endTime) + 'ms', logLevel.info);
6911 if (env.remaining === 0) {
6912 log("css generated in " + (new Date() - startTime) + 'ms', logLevel.info);
6914 endTime = new Date();
6915 }, reload, newVars);
6917 loadStyles(newVars);
6920 if (less.globalVars) {
6921 varsPre = serializeVars(less.globalVars) + "\n";
6924 less.refreshStyles = loadStyles;
6926 less.Parser.fileLoader = loadFile;
6928 less.refresh(less.env === 'development');
6932 // Define Less as an AMD module.
6933 if (typeof define === "function" && define.amd) {
6934 define(function () { return less; } );