2
* LESS - Leaner CSS v1.4.0
5
* Copyright (c) 2009-2013, Alexis Sellier
6
* Licensed under the Apache 2.0 License.
10
(function (window, undefined) {
12
// Stub out `require` in the browser
14
function require(arg) {
15
return window.less[arg.split('/')[1]];
21
// -- kriskowal Kris Kowal Copyright (C) 2009-2010 MIT License
22
// -- tlrobinson Tom Robinson
23
// dantman Daniel Friesen
29
Array.isArray = function(obj) {
30
return Object.prototype.toString.call(obj) === "[object Array]" ||
31
(obj instanceof Array);
34
if (!Array.prototype.forEach) {
35
Array.prototype.forEach = function(block, thisObject) {
36
var len = this.length >>> 0;
37
for (var i = 0; i < len; i++) {
39
block.call(thisObject, this[i], i, this);
44
if (!Array.prototype.map) {
45
Array.prototype.map = function(fun /*, thisp*/) {
46
var len = this.length >>> 0;
47
var res = new Array(len);
48
var thisp = arguments[1];
50
for (var i = 0; i < len; i++) {
52
res[i] = fun.call(thisp, this[i], i, this);
58
if (!Array.prototype.filter) {
59
Array.prototype.filter = function (block /*, thisp */) {
61
var thisp = arguments[1];
62
for (var i = 0; i < this.length; i++) {
63
if (block.call(thisp, this[i])) {
70
if (!Array.prototype.reduce) {
71
Array.prototype.reduce = function(fun /*, initial*/) {
72
var len = this.length >>> 0;
75
// no value to return if no initial value and an empty array
76
if (len === 0 && arguments.length === 1) throw new TypeError();
78
if (arguments.length >= 2) {
79
var rv = arguments[1];
86
// if array contains no values, no initial value to return
87
if (++i >= len) throw new TypeError();
90
for (; i < len; i++) {
92
rv = fun.call(null, rv, this[i], i, this);
98
if (!Array.prototype.indexOf) {
99
Array.prototype.indexOf = function (value /*, fromIndex */ ) {
100
var length = this.length;
101
var i = arguments[1] || 0;
103
if (!length) return -1;
104
if (i >= length) return -1;
105
if (i < 0) i += length;
107
for (; i < length; i++) {
108
if (!Object.prototype.hasOwnProperty.call(this, i)) { continue }
109
if (value === this[i]) return i;
119
Object.keys = function (object) {
121
for (var name in object) {
122
if (Object.prototype.hasOwnProperty.call(object, name)) {
133
if (!String.prototype.trim) {
134
String.prototype.trim = function () {
135
return String(this).replace(/^\s\s*/, '').replace(/\s\s*$/, '');
138
var less, tree, charset;
140
if (typeof environment === "object" && ({}).toString.call(environment) === "[object Environment]") {
142
// Details on how to detect Rhino: https://github.com/ringo/ringojs/issues/88
143
if (typeof(window) === 'undefined') { less = {} }
144
else { less = window.less = {} }
145
tree = less.tree = {};
147
} else if (typeof(window) === 'undefined') {
150
tree = require('./tree');
154
if (typeof(window.less) === 'undefined') { window.less = {} }
156
tree = window.less.tree = {};
157
less.mode = 'browser';
162
// A relatively straight-forward predictive parser.
163
// There is no tokenization/lexing stage, the input is parsed
166
// To make the parser fast enough to run in the browser, several
167
// optimization had to be made:
169
// - Matching and slicing on a huge input is often cause of slowdowns.
170
// The solution is to chunkify the input into smaller strings.
171
// The chunks are stored in the `chunks` var,
172
// `j` holds the current chunk index, and `current` holds
173
// the index of the current chunk in relation to `input`.
174
// This gives us an almost 4x speed-up.
176
// - In many cases, we don't need to match individual tokens;
177
// for example, if a value doesn't hold any variables, operations
178
// or dynamic references, the parser can effectively 'skip' it,
179
// treating it as a literal.
180
// An example would be '1px solid #000' - which evaluates to itself,
181
// we don't need to know what the individual components are.
182
// The drawback, of course is that you don't get the benefits of
183
// syntax-checking on the CSS. This gives us a 50% speed-up in the parser,
184
// and a smaller speed-up in the code-gen.
187
// Token matching is done with the `$` function, which either takes
188
// a terminal string or regexp, or a non-terminal function to call.
189
// It also takes care of moving all the indices forwards.
192
less.Parser = function Parser(env) {
193
var input, // LeSS input string
194
i, // current index in `input`
196
temp, // temporarily holds a chunk's state, for backtracking
197
memo, // temporarily holds `i`, when backtracking
198
furthest, // furthest index the parser has gone to
199
chunks, // chunkified input
200
current, // index of current chunk, in `input`
205
// Top parser on an import tree must be sure there is one "env"
206
// which will then be passed around by reference.
207
if (!(env instanceof tree.parseEnv)) {
208
env = new tree.parseEnv(env);
211
var imports = this.imports = {
212
paths: env.paths || [], // Search paths, when importing
213
queue: [], // Files which haven't been imported yet
214
files: env.files, // Holds the imported parse trees
215
contents: env.contents, // Holds the imported file contents
216
mime: env.mime, // MIME type of .less files
217
error: null, // Error in parsing/evaluating an import
218
push: function (path, currentFileInfo, callback) {
219
var parserImporter = this;
220
this.queue.push(path);
223
// Import a file asynchronously
225
less.Parser.importer(path, currentFileInfo, function (e, root, fullPath) {
226
parserImporter.queue.splice(parserImporter.queue.indexOf(path), 1); // Remove the path from the queue
228
var imported = fullPath in parserImporter.files;
230
parserImporter.files[fullPath] = root; // Store the root
232
if (e && !parserImporter.error) { parserImporter.error = e; }
234
callback(e, root, imported);
239
function save() { temp = chunks[j], memo = i, current = i; }
240
function restore() { chunks[j] = temp, i = memo, current = i; }
244
chunks[j] = chunks[j].slice(i - current);
248
function isWhitespace(c) {
249
// Could change to \s?
250
var code = c.charCodeAt(0);
251
return code === 32 || code === 10 || code === 9;
254
// Parse from a token, regexp or string, and move forward if match
257
var match, args, length, index, k;
262
if (tok instanceof Function) {
263
return tok.call(parser.parsers);
267
// Either match a single character in the input,
268
// or match a regexp in the current chunk (chunk[j]).
270
} else if (typeof(tok) === 'string') {
271
match = input.charAt(i) === tok ? tok : null;
277
if (match = tok.exec(chunks[j])) {
278
length = match[0].length;
284
// The match is confirmed, add the match length to `i`,
285
// and consume any extra white-space characters (' ' || '\n')
286
// which come after that. The reason for this is that LeSS's
287
// grammar is mostly white-space insensitive.
290
skipWhitespace(length);
292
if(typeof(match) === 'string') {
295
return match.length === 1 ? match[0] : match;
300
function skipWhitespace(length) {
301
var oldi = i, oldj = j,
302
endIndex = i + chunks[j].length,
305
while (i < endIndex) {
306
if (! isWhitespace(input.charAt(i))) { break }
309
chunks[j] = chunks[j].slice(length + (i - mem));
312
if (chunks[j].length === 0 && j < chunks.length - 1) { j++ }
314
return oldi !== i || oldj !== j;
317
function expect(arg, msg) {
320
error(msg || (typeof(arg) === 'string' ? "expected '" + arg + "' got '" + input.charAt(i) + "'"
321
: "unexpected token"));
327
function error(msg, type) {
328
var e = new Error(msg);
330
e.type = type || 'Syntax';
334
// Same as $(), but don't change the state of the parser,
335
// just return the match.
337
if (typeof(tok) === 'string') {
338
return input.charAt(i) === tok;
340
if (tok.test(chunks[j])) {
348
function getInput(e, env) {
349
if (e.filename && env.currentFileInfo.filename && (e.filename !== env.currentFileInfo.filename)) {
350
return parser.imports.contents[e.filename];
356
function getLocation(index, input) {
357
for (var n = index, column = -1;
358
n >= 0 && input.charAt(n) !== '\n';
361
return { line: typeof(index) === 'number' ? (input.slice(0, index).match(/\n/g) || "").length : null,
365
function getDebugInfo(index, inputStream, env) {
366
var filename = env.currentFileInfo.filename;
367
if(less.mode !== 'browser' && less.mode !== 'rhino') {
368
filename = require('path').resolve(filename);
372
lineNumber: getLocation(index, inputStream).line + 1,
377
function LessError(e, env) {
378
var input = getInput(e, env),
379
loc = getLocation(e.index, input),
382
lines = input.split('\n');
384
this.type = e.type || 'Syntax';
385
this.message = e.message;
386
this.filename = e.filename || env.currentFileInfo.filename;
387
this.index = e.index;
388
this.line = typeof(line) === 'number' ? line + 1 : null;
389
this.callLine = e.call && (getLocation(e.call, input).line + 1);
390
this.callExtract = lines[getLocation(e.call, input).line];
391
this.stack = e.stack;
400
this.env = env = env || {};
402
// The optimization level dictates the thoroughness of the parser,
403
// the lower the number, the less nodes it will create in the tree.
404
// This could matter for debugging, or if you want to access
405
// the individual nodes in the tree.
406
this.optimization = ('optimization' in this.env) ? this.env.optimization : 1;
415
// Parse an input string into an abstract syntax tree,
416
// call `callback` when done.
418
parse: function (str, callback) {
419
var root, start, end, zone, line, lines, buff = [], c, error = null;
421
i = j = current = furthest = 0;
422
input = str.replace(/\r\n/g, '\n');
424
// Remove potential UTF Byte Order Mark
425
input = input.replace(/^\uFEFF/, '');
427
// Split the input into chunks.
428
chunks = (function (chunks) {
430
skip = /(?:@\{[\w-]+\}|[^"'`\{\}\/\(\)\\])+/g,
431
comment = /\/\*(?:[^*]|\*+[^\/*])*\*+\/|\/\/.*/g,
432
string = /"((?:[^"\\\r\n]|\\.)*)"|'((?:[^'\\\r\n]|\\.)*)'|`((?:[^`]|\\.)*)`/g,
438
for (var i = 0, c, cc; i < input.length;) {
440
if (match = skip.exec(input)) {
441
if (match.index === i) {
442
i += match[0].length;
443
chunk.push(match[0]);
447
comment.lastIndex = string.lastIndex = i;
449
if (match = string.exec(input)) {
450
if (match.index === i) {
451
i += match[0].length;
452
chunk.push(match[0]);
457
if (!inParam && c === '/') {
458
cc = input.charAt(i + 1);
459
if (cc === '/' || cc === '*') {
460
if (match = comment.exec(input)) {
461
if (match.index === i) {
462
i += match[0].length;
463
chunk.push(match[0]);
471
case '{': if (! inParam) { level ++; chunk.push(c); break }
472
case '}': if (! inParam) { level --; chunk.push(c); chunks[++j] = chunk = []; break }
473
case '(': if (! inParam) { inParam = true; chunk.push(c); break }
474
case ')': if ( inParam) { inParam = false; chunk.push(c); break }
475
default: chunk.push(c);
481
error = new(LessError)({
484
message: (level > 0) ? "missing closing `}`" : "missing opening `{`",
485
filename: env.currentFileInfo.filename
489
return chunks.map(function (c) { return c.join('') });;
493
return callback(new(LessError)(error, env));
496
// Start with the primary rule.
497
// The whole syntax tree is held under a Ruleset node,
498
// with the `root` property set to true, so no `{}` are
499
// output. The callback is called when the input is parsed.
501
root = new(tree.Ruleset)([], $(this.parsers.primary));
503
root.firstRoot = true;
505
return callback(new(LessError)(e, env));
508
root.toCSS = (function (evaluate) {
509
var line, lines, column;
511
return function (options, variables) {
512
options = options || {};
514
evalEnv = new tree.evalEnv(options);
517
// Allows setting variables with a hash, so:
519
// `{ color: new(tree.Color)('#f01') }` will become:
521
// new(tree.Rule)('@color',
523
// new(tree.Expression)([
524
// new(tree.Color)('#f01')
529
if (typeof(variables) === 'object' && !Array.isArray(variables)) {
530
variables = Object.keys(variables).map(function (k) {
531
var value = variables[k];
533
if (! (value instanceof tree.Value)) {
534
if (! (value instanceof tree.Expression)) {
535
value = new(tree.Expression)([value]);
537
value = new(tree.Value)([value]);
539
return new(tree.Rule)('@' + k, value, false, 0);
541
evalEnv.frames = [new(tree.Ruleset)(null, variables)];
545
var evaldRoot = evaluate.call(this, evalEnv);
547
new(tree.joinSelectorVisitor)()
550
new(tree.processExtendsVisitor)()
553
var css = evaldRoot.toCSS({
554
compress: options.compress || false,
555
dumpLineNumbers: env.dumpLineNumbers,
556
strictUnits: options.strictUnits === false ? false : true});
558
throw new(LessError)(e, env);
561
if (options.yuicompress && less.mode === 'node') {
562
return require('ycssmin').cssmin(css);
563
} else if (options.compress) {
564
return css.replace(/(\s)+/g, "$1");
571
// If `i` is smaller than the `input.length - 1`,
572
// it means the parser wasn't able to parse the whole
573
// string, so we've got a parsing error.
575
// We try to extract a \n delimited string,
576
// showing the line where the parse error occured.
577
// We split it up into two parts (the part which parsed,
578
// and the part which didn't), so we can color them differently.
579
if (i < input.length - 1) {
581
lines = input.split('\n');
582
line = (input.slice(0, i).match(/\n/g) || "").length + 1;
584
for (var n = i, column = -1; n >= 0 && input.charAt(n) !== '\n'; n--) { column++ }
588
message: "Unrecognised input",
590
filename: env.currentFileInfo.filename,
601
var finish = function (e) {
602
e = error || e || parser.imports.error;
605
if (!(e instanceof LessError)) {
606
e = new(LessError)(e, env);
612
callback(null, root);
616
if (env.processImports !== false) {
617
new tree.importVisitor(this.imports, finish)
625
// Here in, the parsing rules/functions
627
// The basic structure of the syntax tree generated is as follows:
629
// Ruleset -> Rule -> Value -> Expression -> Entity
631
// Here's some LESS code:
635
// border: 1px solid #000;
640
// And here's what the parse tree might look like:
642
// Ruleset (Selector '.class', [
643
// Rule ("color", Value ([Expression [Color #fff]]))
644
// Rule ("border", Value ([Expression [Dimension 1px][Keyword "solid"][Color #000]]))
645
// Rule ("width", Value ([Expression [Operation "+" [Variable "@w"][Dimension 4px]]]))
646
// Ruleset (Selector [Element '>', '.child'], [...])
649
// In general, most rules will try to parse a token with the `$()` function, and if the return
650
// value is truly, will return a new node, of the relevant type. Sometimes, we need to check
651
// first, before parsing, that's when we use `peek()`.
655
// The `primary` rule is the *entry* and *exit* point of the parser.
656
// The rules here can appear at any level of the parse tree.
658
// The recursive nature of the grammar is an interplay between the `block`
659
// rule, which represents `{ ... }`, the `ruleset` rule, and this `primary` rule,
660
// as represented by this simplified grammar:
662
// primary → (ruleset | rule)+
663
// ruleset → selector+ block
664
// block → '{' primary '}'
666
// Only at one point is the primary rule not called from the
667
// block rule: at the root level.
669
primary: function () {
672
while ((node = $(this.extendRule) || $(this.mixin.definition) || $(this.rule) || $(this.ruleset) ||
673
$(this.mixin.call) || $(this.comment) || $(this.directive))
674
|| $(/^[\s\n]+/) || $(/^;+/)) {
675
node && root.push(node);
680
// We create a Comment node for CSS comments `/* */`,
681
// but keep the LeSS comments `//` silent, by just skipping
683
comment: function () {
686
if (input.charAt(i) !== '/') return;
688
if (input.charAt(i + 1) === '/') {
689
return new(tree.Comment)($(/^\/\/.*/), true);
690
} else if (comment = $(/^\/\*(?:[^*]|\*+[^\/*])*\*+\/\n?/)) {
691
return new(tree.Comment)(comment);
696
// Entities are tokens which can be found inside an Expression
700
// A string, which supports escaping " and '
702
// "milky way" 'he\'s the one!'
704
quoted: function () {
705
var str, j = i, e, index = i;
707
if (input.charAt(j) === '~') { j++, e = true } // Escaped strings
708
if (input.charAt(j) !== '"' && input.charAt(j) !== "'") return;
712
if (str = $(/^"((?:[^"\\\r\n]|\\.)*)"|'((?:[^'\\\r\n]|\\.)*)'/)) {
713
return new(tree.Quoted)(str[0], str[1] || str[2], e, index, env.currentFileInfo);
718
// A catch-all word, such as:
720
// black border-collapse
722
keyword: function () {
725
if (k = $(/^[_A-Za-z-][_A-Za-z0-9-]*/)) {
726
if (tree.colors.hasOwnProperty(k)) {
727
// detect named color
728
return new(tree.Color)(tree.colors[k].slice(1));
730
return new(tree.Keyword)(k);
740
// We also try to catch IE's `alpha()`, but let the `alpha` parser
741
// deal with the details.
743
// The arguments are parsed with the `entities.arguments` parser.
746
var name, nameLC, args, alpha_ret, index = i;
748
if (! (name = /^([\w-]+|%|progid:[\w\.]+)\(/.exec(chunks[j]))) return;
751
nameLC = name.toLowerCase();
753
if (nameLC === 'url') { return null }
754
else { i += name.length }
756
if (nameLC === 'alpha') {
757
alpha_ret = $(this.alpha);
758
if(typeof alpha_ret !== 'undefined') {
763
$('('); // Parse the '(' and consume whitespace.
765
args = $(this.entities.arguments);
771
if (name) { return new(tree.Call)(name, args, index, env.currentFileInfo); }
773
arguments: function () {
776
while (arg = $(this.entities.assignment) || $(this.expression)) {
778
if (! $(',')) { break }
782
literal: function () {
783
return $(this.entities.dimension) ||
784
$(this.entities.color) ||
785
$(this.entities.quoted) ||
786
$(this.entities.unicodeDescriptor);
789
// Assignments are argument entities for calls.
790
// They are present in ie filter properties as shown below.
792
// filter: progid:DXImageTransform.Microsoft.Alpha( *opacity=50* )
795
assignment: function () {
797
if ((key = $(/^\w+(?=\s?=)/i)) && $('=') && (value = $(this.entity))) {
798
return new(tree.Assignment)(key, value);
803
// Parse url() tokens
805
// We use a specific rule for urls, because they don't really behave like
806
// standard function calls. The difference is that the argument doesn't have
807
// to be enclosed within a string, so it can't be parsed as an Expression.
812
if (input.charAt(i) !== 'u' || !$(/^url\(/)) return;
813
value = $(this.entities.quoted) || $(this.entities.variable) ||
814
$(/^(?:(?:\\[\(\)'"])|[^\(\)'"])+/) || "";
818
return new(tree.URL)((value.value != null || value instanceof tree.Variable)
819
? value : new(tree.Anonymous)(value), env.currentFileInfo);
823
// A Variable entity, such as `@fink`, in
825
// width: @fink + 2px
827
// We use a different parser for variable definitions,
828
// see `parsers.variable`.
830
variable: function () {
833
if (input.charAt(i) === '@' && (name = $(/^@@?[\w-]+/))) {
834
return new(tree.Variable)(name, index, env.currentFileInfo);
838
// A variable entity useing the protective {} e.g. @{var}
839
variableCurly: function () {
840
var name, curly, index = i;
842
if (input.charAt(i) === '@' && (curly = $(/^@\{([\w-]+)\}/))) {
843
return new(tree.Variable)("@" + curly[1], index, env.currentFileInfo);
848
// A Hexadecimal color
852
// `rgb` and `hsl` colors are parsed through the `entities.call` parser.
857
if (input.charAt(i) === '#' && (rgb = $(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})/))) {
858
return new(tree.Color)(rgb[1]);
863
// A Dimension, that is, a number and a unit
867
dimension: function () {
868
var value, c = input.charCodeAt(i);
869
//Is the first char of the dimension 0-9, '.', '+' or '-'
870
if ((c > 57 || c < 43) || c === 47 || c == 44) return;
872
if (value = $(/^([+-]?\d*\.?\d+)(%|[a-z]+)?/)) {
873
return new(tree.Dimension)(value[1], value[2]);
878
// A unicode descriptor, as is used in unicode-range
880
// U+0?? or U+00A1-00A9
882
unicodeDescriptor: function () {
885
if (ud = $(/^U\+[0-9a-fA-F?]+(\-[0-9a-fA-F?]+)?/)) {
886
return new(tree.UnicodeDescriptor)(ud[0]);
891
// JavaScript code to be evaluated
893
// `window.location.href`
895
javascript: function () {
898
if (input.charAt(j) === '~') { j++, e = true } // Escaped strings
899
if (input.charAt(j) !== '`') { return }
903
if (str = $(/^`([^`]*)`/)) {
904
return new(tree.JavaScript)(str[1], i, e);
910
// The variable part of a variable definition. Used in the `rule` parser
914
variable: function () {
917
if (input.charAt(i) === '@' && (name = $(/^(@[\w-]+)\s*:/))) { return name[1] }
921
// extend syntax - used to extend selectors
923
extend: function(isRule) {
924
var elements, e, index = i, option, extendList = [];
926
if (!$(isRule ? /^&:extend\(/ : /^:extend\(/)) { return; }
932
option = $(/^(all)(?=\s*(\)|,))/);
933
if (option) { break; }
939
option = option && option[1];
941
extendList.push(new(tree.Extend)(new(tree.Selector)(elements), option, index));
955
// extendRule - used in a rule to extend all the parent selectors
957
extendRule: function() {
958
return this.extend(true);
966
// A Mixin call, with an optional argument list
968
// #mixins > .square(#fff);
969
// .rounded(4px, black);
972
// The `while` loop is there because mixins can be
973
// namespaced, but we only support the child and descendant
977
var elements = [], e, c, args, delim, arg, index = i, s = input.charAt(i), important = false;
979
if (s !== '.' && s !== '#') { return }
981
save(); // stop us absorbing part of an invalid selector
983
while (e = $(/^[#.](?:[\w-]|\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+/)) {
984
elements.push(new(tree.Element)(c, e, i));
988
args = this.mixin.args.call(this, true).args;
994
if ($(this.important)) {
998
if (elements.length > 0 && ($(';') || peek('}'))) {
999
return new(tree.mixin.Call)(elements, args, index, env.currentFileInfo, important);
1004
args: function (isCall) {
1005
var expressions = [], argsSemiColon = [], isSemiColonSeperated, argsComma = [], expressionContainsNamed, name, nameLoop, value, arg,
1006
returner = {args:null, variadic: false};
1009
arg = $(this.expression);
1012
if (input.charAt(i) === '.' && $(/^\.{3}/)) {
1013
returner.variadic = true;
1014
if ($(";") && !isSemiColonSeperated) {
1015
isSemiColonSeperated = true;
1017
(isSemiColonSeperated ? argsSemiColon : argsComma)
1018
.push({ variadic: true });
1021
arg = $(this.entities.variable) || $(this.entities.literal)
1022
|| $(this.entities.keyword);
1030
if (arg.throwAwayComments) {
1031
arg.throwAwayComments();
1038
if (arg.value.length == 1) {
1039
var val = arg.value[0];
1045
if (val && val instanceof tree.Variable) {
1047
if (expressions.length > 0) {
1048
if (isSemiColonSeperated) {
1049
error("Cannot mix ; and , as delimiter types");
1051
expressionContainsNamed = true;
1053
value = expect(this.expression);
1054
nameLoop = (name = val.name);
1055
} else if (!isCall && $(/^\.{3}/)) {
1056
returner.variadic = true;
1057
if ($(";") && !isSemiColonSeperated) {
1058
isSemiColonSeperated = true;
1060
(isSemiColonSeperated ? argsSemiColon : argsComma)
1061
.push({ name: arg.name, variadic: true });
1063
} else if (!isCall) {
1064
name = nameLoop = val.name;
1070
expressions.push(value);
1073
argsComma.push({ name:nameLoop, value:value });
1079
if ($(';') || isSemiColonSeperated) {
1081
if (expressionContainsNamed) {
1082
error("Cannot mix ; and , as delimiter types");
1085
isSemiColonSeperated = true;
1087
if (expressions.length > 1) {
1088
value = new (tree.Value)(expressions);
1090
argsSemiColon.push({ name:name, value:value });
1094
expressionContainsNamed = false;
1098
returner.args = isSemiColonSeperated ? argsSemiColon : argsComma;
1102
// A Mixin definition, with a list of parameters
1104
// .rounded (@radius: 2px, @color) {
1108
// Until we have a finer grained state-machine, we have to
1109
// do a look-ahead, to make sure we don't have a mixin call.
1110
// See the `rule` function for more information.
1112
// We start by matching `.rounded (`, and then proceed on to
1113
// the argument list, which has optional default values.
1114
// We store the parameters in `params`, with a `value` key,
1115
// if there is a value, such as in the case of `@radius`.
1117
// Once we've got our params list, and a closing `)`, we parse
1118
// the `{...}` block.
1120
definition: function () {
1121
var name, params = [], match, ruleset, param, value, cond, variadic = false;
1122
if ((input.charAt(i) !== '.' && input.charAt(i) !== '#') ||
1123
peek(/^[^{]*\}/)) return;
1127
if (match = $(/^([#.](?:[\w-]|\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+)\s*\(/)) {
1130
var argInfo = this.mixin.args.call(this, false);
1131
params = argInfo.args;
1132
variadic = argInfo.variadic;
1134
// .mixincall("@{a}");
1135
// looks a bit like a mixin definition.. so we have to be nice and restore
1143
if ($(/^when/)) { // Guard
1144
cond = expect(this.conditions, 'expected condition');
1147
ruleset = $(this.block);
1150
return new(tree.mixin.Definition)(name, params, ruleset, cond, variadic);
1159
// Entities are the smallest recognized token,
1160
// and can be found inside a rule's value.
1162
entity: function () {
1163
return $(this.entities.literal) || $(this.entities.variable) || $(this.entities.url) ||
1164
$(this.entities.call) || $(this.entities.keyword) ||$(this.entities.javascript) ||
1169
// A Rule terminator. Note that we use `peek()` to check for '}',
1170
// because the `block` rule will be expecting it, but we still need to make sure
1171
// it's there, if ';' was ommitted.
1174
return $(';') || peek('}');
1178
// IE's alpha function
1180
// alpha(opacity=88)
1182
alpha: function () {
1185
if (! $(/^\(opacity=/i)) return;
1186
if (value = $(/^\d+/) || $(this.entities.variable)) {
1188
return new(tree.Alpha)(value);
1193
// A Selector Element
1198
// input[type="text"]
1200
// Elements are the building blocks for Selectors,
1201
// they are made out of a `Combinator` (see combinator rule),
1202
// and an element name, such as a tag a class, or `*`.
1204
element: function () {
1207
c = $(this.combinator);
1209
e = $(/^(?:\d+\.\d+|\d+)%/) || $(/^(?:[.#]?|:*)(?:[\w-]|[^\x00-\x9f]|\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+/) ||
1210
$('*') || $('&') || $(this.attribute) || $(/^\([^()@]+\)/) || $(/^[\.#](?=@)/) || $(this.entities.variableCurly);
1214
if ((v = ($(this.selector))) &&
1216
e = new(tree.Paren)(v);
1221
if (e) { return new(tree.Element)(c, e, i) }
1225
// Combinators combine elements together, in a Selector.
1227
// Because our parser isn't white-space sensitive, special care
1228
// has to be taken, when parsing the descendant combinator, ` `,
1229
// as it's an empty space. We have to check the previous character
1230
// in the input, to see if it's a ` ` character. More info on how
1231
// we deal with this in *combinator.js*.
1233
combinator: function () {
1234
var match, c = input.charAt(i);
1236
if (c === '>' || c === '+' || c === '~' || c === '|') {
1238
while (input.charAt(i).match(/\s/)) { i++ }
1239
return new(tree.Combinator)(c);
1240
} else if (input.charAt(i - 1).match(/\s/)) {
1241
return new(tree.Combinator)(" ");
1243
return new(tree.Combinator)(null);
1250
// .class > div + h1
1253
// Selectors are made out of one or more Elements, see above.
1255
selector: function () {
1256
var sel, e, elements = [], c, match, extend, extendList = [];
1258
while ((extend = $(this.extend)) || (e = $(this.element))) {
1260
extendList.push.apply(extendList, extend);
1262
if (extendList.length) {
1263
error("Extend can only be used at the end of selector");
1265
c = input.charAt(i);
1269
if (c === '{' || c === '}' || c === ';' || c === ',' || c === ')') { break }
1272
if (elements.length > 0) { return new(tree.Selector)(elements, extendList); }
1273
if (extendList.length) { error("Extend must be used to extend a selector, it cannot be used on its own"); }
1275
attribute: function () {
1276
var attr = '', key, val, op;
1278
if (! $('[')) return;
1280
if (!(key = $(this.entities.variableCurly))) {
1281
key = expect(/^(?:[_A-Za-z0-9-\*]*\|)?(?:[_A-Za-z0-9-]|\\.)+/);
1284
if ((op = $(/^[|~*$^]?=/))) {
1285
val = $(this.entities.quoted) || $(/^[\w-]+/) || $(this.entities.variableCurly);
1290
return new(tree.Attribute)(key, op, val);
1294
// The `block` rule is used by `ruleset` and `mixin.definition`.
1295
// It's a wrapper around the `primary` rule, with added `{}`.
1297
block: function () {
1299
if ($('{') && (content = $(this.primary)) && $('}')) {
1305
// div, .class, body > p {...}
1307
ruleset: function () {
1308
var selectors = [], s, rules, match, debugInfo;
1312
if (env.dumpLineNumbers)
1313
debugInfo = getDebugInfo(i, input, env);
1315
while (s = $(this.selector)) {
1318
if (! $(',')) { break }
1322
if (selectors.length > 0 && (rules = $(this.block))) {
1323
var ruleset = new(tree.Ruleset)(selectors, rules, env.strictImports);
1324
if (env.dumpLineNumbers)
1325
ruleset.debugInfo = debugInfo;
1333
rule: function (tryAnonymous) {
1334
var name, value, c = input.charAt(i), important, match;
1337
if (c === '.' || c === '#' || c === '&') { return }
1339
if (name = $(this.variable) || $(this.property)) {
1340
// prefer to try to parse first if its a variable or we are compressing
1341
// but always fallback on the other one
1342
value = !tryAnonymous && (env.compress || (name.charAt(0) === '@')) ?
1343
($(this.value) || $(this.anonymousValue)) :
1344
($(this.anonymousValue) || $(this.value));
1346
important = $(this.important);
1348
if (value && $(this.end)) {
1349
return new(tree.Rule)(name, value, important, memo, env.currentFileInfo);
1353
if (value && !tryAnonymous) {
1354
return this.rule(true);
1359
anonymousValue: function () {
1360
if (match = /^([^@+\/'"*`(;{}-]*);/.exec(chunks[j])) {
1361
i += match[0].length - 1;
1362
return new(tree.Anonymous)(match[1]);
1367
// An @import directive
1371
// Depending on our environemnt, importing is done differently:
1372
// In the browser, it's an XHR request, in Node, it would be a
1373
// file-system operation. The function used for importing is
1374
// stored in `import`, which we pass to the Import constructor.
1376
"import": function () {
1377
var path, features, index = i;
1381
var dir = $(/^@import?\s+/);
1383
var options = (dir ? $(this.importOptions) : null) || {};
1385
if (dir && (path = $(this.entities.quoted) || $(this.entities.url))) {
1386
features = $(this.mediaFeatures);
1388
features = features && new(tree.Value)(features);
1389
return new(tree.Import)(path, features, options, index, env.currentFileInfo);
1396
importOptions: function() {
1397
var o, options = {}, optionName, value;
1399
// list of options, surrounded by parens
1400
if (! $('(')) { return null; }
1402
if (o = $(this.importOption)) {
1405
switch(optionName) {
1407
optionName = "less";
1411
optionName = "multiple";
1415
options[optionName] = value;
1416
if (! $(',')) { break }
1423
importOption: function() {
1424
var opt = $(/^(less|css|multiple|once)/);
1430
mediaFeature: function () {
1431
var e, p, nodes = [];
1434
if (e = $(this.entities.keyword)) {
1436
} else if ($('(')) {
1437
p = $(this.property);
1441
nodes.push(new(tree.Paren)(new(tree.Rule)(p, e, null, i, env.currentFileInfo, true)));
1443
nodes.push(new(tree.Paren)(e));
1447
} else { return null }
1451
if (nodes.length > 0) {
1452
return new(tree.Expression)(nodes);
1456
mediaFeatures: function () {
1457
var e, features = [];
1460
if (e = $(this.mediaFeature)) {
1462
if (! $(',')) { break }
1463
} else if (e = $(this.entities.variable)) {
1465
if (! $(',')) { break }
1469
return features.length > 0 ? features : null;
1472
media: function () {
1473
var features, rules, media, debugInfo;
1475
if (env.dumpLineNumbers)
1476
debugInfo = getDebugInfo(i, input, env);
1479
features = $(this.mediaFeatures);
1481
if (rules = $(this.block)) {
1482
media = new(tree.Media)(rules, features);
1483
if(env.dumpLineNumbers)
1484
media.debugInfo = debugInfo;
1493
// @charset "utf-8";
1495
directive: function () {
1496
var name, value, rules, identifier, e, nodes, nonVendorSpecificName,
1497
hasBlock, hasIdentifier, hasExpression;
1499
if (input.charAt(i) !== '@') return;
1501
if (value = $(this['import']) || $(this.media)) {
1507
name = $(/^@[a-z-]+/);
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":
1544
hasIdentifier = true;
1547
hasExpression = true;
1551
if (hasIdentifier) {
1552
name += " " + ($(/^[^{]+/) || '').trim();
1557
if (rules = $(this.block)) {
1558
return new(tree.Directive)(name, rules);
1561
if ((value = hasExpression ? $(this.expression) : $(this.entity)) && $(';')) {
1562
var directive = new(tree.Directive)(name, value);
1563
if (env.dumpLineNumbers) {
1564
directive.debugInfo = getDebugInfo(i, input, env);
1574
// A Value is a comma-delimited list of Expressions
1576
// font-family: Baskerville, Georgia, serif;
1578
// In a Rule, a Value represents everything after the `:`,
1579
// and before the `;`.
1581
value: function () {
1582
var e, expressions = [], important;
1584
while (e = $(this.expression)) {
1585
expressions.push(e);
1586
if (! $(',')) { break }
1589
if (expressions.length > 0) {
1590
return new(tree.Value)(expressions);
1593
important: function () {
1594
if (input.charAt(i) === '!') {
1595
return $(/^! *important/);
1602
if (a = $(this.addition)) {
1603
e = new(tree.Expression)([a]);
1610
multiplication: function () {
1611
var m, a, op, operation, isSpaced, expression = [];
1612
if (m = $(this.operand)) {
1613
isSpaced = isWhitespace(input.charAt(i - 1));
1614
while (!peek(/^\/[*\/]/) && (op = ($('/') || $('*')))) {
1615
if (a = $(this.operand)) {
1616
m.parensInOp = true;
1617
a.parensInOp = true;
1618
operation = new(tree.Operation)(op, [operation || m, a], isSpaced);
1619
isSpaced = isWhitespace(input.charAt(i - 1));
1624
return operation || m;
1627
addition: function () {
1628
var m, a, op, operation, isSpaced;
1629
if (m = $(this.multiplication)) {
1630
isSpaced = isWhitespace(input.charAt(i - 1));
1631
while ((op = $(/^[-+]\s+/) || (!isSpaced && ($('+') || $('-')))) &&
1632
(a = $(this.multiplication))) {
1633
m.parensInOp = true;
1634
a.parensInOp = true;
1635
operation = new(tree.Operation)(op, [operation || m, a], isSpaced);
1636
isSpaced = isWhitespace(input.charAt(i - 1));
1638
return operation || m;
1641
conditions: function () {
1642
var a, b, index = i, condition;
1644
if (a = $(this.condition)) {
1645
while ($(',') && (b = $(this.condition))) {
1646
condition = new(tree.Condition)('or', condition || a, b, index);
1648
return condition || a;
1651
condition: function () {
1652
var a, b, c, op, index = i, negate = false;
1654
if ($(/^not/)) { negate = true }
1656
if (a = $(this.addition) || $(this.entities.keyword) || $(this.entities.quoted)) {
1657
if (op = $(/^(?:>=|=<|[<=>])/)) {
1658
if (b = $(this.addition) || $(this.entities.keyword) || $(this.entities.quoted)) {
1659
c = new(tree.Condition)(op, a, b, index, negate);
1661
error('expected expression');
1664
c = new(tree.Condition)('=', a, new(tree.Keyword)('true'), index, negate);
1667
return $(/^and/) ? new(tree.Condition)('and', c, $(this.condition)) : c;
1672
// An operand is anything that can be part of an operation,
1673
// such as a Color, or a Variable
1675
operand: function () {
1676
var negate, p = input.charAt(i + 1);
1678
if (input.charAt(i) === '-' && (p === '@' || p === '(')) { negate = $('-') }
1679
var o = $(this.sub) || $(this.entities.dimension) ||
1680
$(this.entities.color) || $(this.entities.variable) ||
1681
$(this.entities.call);
1684
o.parensInOp = true;
1685
o = new(tree.Negative)(o);
1692
// Expressions either represent mathematical operations,
1693
// or white-space delimited Entities.
1698
expression: function () {
1699
var e, delim, entities = [], d;
1701
while (e = $(this.addition) || $(this.entity)) {
1703
// operations do not allow keyword "/" dimension (e.g. small/20px) so we support that here
1704
if (!peek(/^\/[\/*]/) && (delim = $('/'))) {
1705
entities.push(new(tree.Anonymous)(delim));
1708
if (entities.length > 0) {
1709
return new(tree.Expression)(entities);
1712
property: function () {
1715
if (name = $(/^(\*?-?[_a-z0-9-]+)\s*:/)) {
1723
if (less.mode === 'browser' || less.mode === 'rhino') {
1725
// Used by `@import` directives
1727
less.Parser.importer = function (path, currentFileInfo, callback, env) {
1728
if (!/^([a-z-]+:)?\//.test(path) && currentFileInfo.currentDirectory) {
1729
path = currentFileInfo.currentDirectory + path;
1731
var sheetEnv = env.toSheet(path);
1732
sheetEnv.processImports = false;
1733
sheetEnv.currentFileInfo = currentFileInfo;
1735
// We pass `true` as 3rd argument, to force the reload of the import.
1736
// This is so we can get the syntax tree as opposed to just the CSS output,
1737
// as we need this to evaluate the current stylesheet.
1738
loadStyleSheet(sheetEnv,
1739
function (e, root, data, sheet, _, path) {
1740
callback.call(null, e, root, path);
1748
rgb: function (r, g, b) {
1749
return this.rgba(r, g, b, 1.0);
1751
rgba: function (r, g, b, a) {
1752
var rgb = [r, g, b].map(function (c) { return scaled(c, 256); });
1754
return new(tree.Color)(rgb, a);
1756
hsl: function (h, s, l) {
1757
return this.hsla(h, s, l, 1.0);
1759
hsla: function (h, s, l, a) {
1760
h = (number(h) % 360) / 360;
1761
s = clamp(number(s)); l = clamp(number(l)); a = clamp(number(a));
1763
var m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s;
1764
var m1 = l * 2 - m2;
1766
return this.rgba(hue(h + 1/3) * 255,
1772
h = h < 0 ? h + 1 : (h > 1 ? h - 1 : h);
1773
if (h * 6 < 1) return m1 + (m2 - m1) * h * 6;
1774
else if (h * 2 < 1) return m2;
1775
else if (h * 3 < 2) return m1 + (m2 - m1) * (2/3 - h) * 6;
1780
hsv: function(h, s, v) {
1781
return this.hsva(h, s, v, 1.0);
1784
hsva: function(h, s, v, a) {
1785
h = ((number(h) % 360) / 360) * 360;
1786
s = number(s); v = number(v); a = number(a);
1789
i = Math.floor((h / 60) % 6);
1795
v * (1 - (1 - f) * s)];
1796
var perm = [[0, 3, 1],
1803
return this.rgba(vs[perm[i][0]] * 255,
1804
vs[perm[i][1]] * 255,
1805
vs[perm[i][2]] * 255,
1809
hue: function (color) {
1810
return new(tree.Dimension)(Math.round(color.toHSL().h));
1812
saturation: function (color) {
1813
return new(tree.Dimension)(Math.round(color.toHSL().s * 100), '%');
1815
lightness: function (color) {
1816
return new(tree.Dimension)(Math.round(color.toHSL().l * 100), '%');
1818
hsvhue: function(color) {
1819
return new(tree.Dimension)(Math.round(color.toHSV().h));
1821
hsvsaturation: function (color) {
1822
return new(tree.Dimension)(Math.round(color.toHSV().s * 100), '%');
1824
hsvvalue: function (color) {
1825
return new(tree.Dimension)(Math.round(color.toHSV().v * 100), '%');
1827
red: function (color) {
1828
return new(tree.Dimension)(color.rgb[0]);
1830
green: function (color) {
1831
return new(tree.Dimension)(color.rgb[1]);
1833
blue: function (color) {
1834
return new(tree.Dimension)(color.rgb[2]);
1836
alpha: function (color) {
1837
return new(tree.Dimension)(color.toHSL().a);
1839
luma: function (color) {
1840
return new(tree.Dimension)(Math.round(color.luma() * color.alpha * 100), '%');
1842
saturate: function (color, amount) {
1843
var hsl = color.toHSL();
1845
hsl.s += amount.value / 100;
1846
hsl.s = clamp(hsl.s);
1849
desaturate: function (color, amount) {
1850
var hsl = color.toHSL();
1852
hsl.s -= amount.value / 100;
1853
hsl.s = clamp(hsl.s);
1856
lighten: function (color, amount) {
1857
var hsl = color.toHSL();
1859
hsl.l += amount.value / 100;
1860
hsl.l = clamp(hsl.l);
1863
darken: function (color, amount) {
1864
var hsl = color.toHSL();
1866
hsl.l -= amount.value / 100;
1867
hsl.l = clamp(hsl.l);
1870
fadein: function (color, amount) {
1871
var hsl = color.toHSL();
1873
hsl.a += amount.value / 100;
1874
hsl.a = clamp(hsl.a);
1877
fadeout: function (color, amount) {
1878
var hsl = color.toHSL();
1880
hsl.a -= amount.value / 100;
1881
hsl.a = clamp(hsl.a);
1884
fade: function (color, amount) {
1885
var hsl = color.toHSL();
1887
hsl.a = amount.value / 100;
1888
hsl.a = clamp(hsl.a);
1891
spin: function (color, amount) {
1892
var hsl = color.toHSL();
1893
var hue = (hsl.h + amount.value) % 360;
1895
hsl.h = hue < 0 ? 360 + hue : hue;
1900
// Copyright (c) 2006-2009 Hampton Catlin, Nathan Weizenbaum, and Chris Eppstein
1901
// http://sass-lang.com
1903
mix: function (color1, color2, weight) {
1905
weight = new(tree.Dimension)(50);
1907
var p = weight.value / 100.0;
1909
var a = color1.toHSL().a - color2.toHSL().a;
1911
var w1 = (((w * a == -1) ? w : (w + a) / (1 + w * a)) + 1) / 2.0;
1914
var rgb = [color1.rgb[0] * w1 + color2.rgb[0] * w2,
1915
color1.rgb[1] * w1 + color2.rgb[1] * w2,
1916
color1.rgb[2] * w1 + color2.rgb[2] * w2];
1918
var alpha = color1.alpha * p + color2.alpha * (1 - p);
1920
return new(tree.Color)(rgb, alpha);
1922
greyscale: function (color) {
1923
return this.desaturate(color, new(tree.Dimension)(100));
1925
contrast: function (color, dark, light, threshold) {
1926
// filter: contrast(3.2);
1927
// should be kept as is, so check for color
1931
if (typeof light === 'undefined') {
1932
light = this.rgba(255, 255, 255, 1.0);
1934
if (typeof dark === 'undefined') {
1935
dark = this.rgba(0, 0, 0, 1.0);
1937
//Figure out which is actually light and dark!
1938
if (dark.luma() > light.luma()) {
1943
if (typeof threshold === 'undefined') {
1946
threshold = number(threshold);
1948
if ((color.luma() * color.alpha) < threshold) {
1955
return new(tree.Anonymous)(str instanceof tree.JavaScript ? str.evaluated : str);
1957
escape: function (str) {
1958
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"));
1960
'%': function (quoted /* arg, arg, ...*/) {
1961
var args = Array.prototype.slice.call(arguments, 1),
1964
for (var i = 0; i < args.length; i++) {
1965
str = str.replace(/%[sda]/i, function(token) {
1966
var value = token.match(/s/i) ? args[i].value : args[i].toCSS();
1967
return token.match(/[A-Z]$/) ? encodeURIComponent(value) : value;
1970
str = str.replace(/%%/g, '%');
1971
return new(tree.Quoted)('"' + str + '"', str);
1973
unit: function (val, unit) {
1974
return new(tree.Dimension)(val.value, unit ? unit.toCSS() : "");
1976
convert: function (val, unit) {
1977
return val.convertTo(unit.value);
1979
round: function (n, f) {
1980
var fraction = typeof(f) === "undefined" ? 0 : f.value;
1981
return this._math(function(num) { return num.toFixed(fraction); }, null, n);
1984
return new(tree.Dimension)(Math.PI);
1986
mod: function(a, b) {
1987
return new(tree.Dimension)(a.value % b.value, a.unit);
1989
pow: function(x, y) {
1990
if (typeof x === "number" && typeof y === "number") {
1991
x = new(tree.Dimension)(x);
1992
y = new(tree.Dimension)(y);
1993
} else if (!(x instanceof tree.Dimension) || !(y instanceof tree.Dimension)) {
1994
throw { type: "Argument", message: "arguments must be numbers" };
1997
return new(tree.Dimension)(Math.pow(x.value, y.value), x.unit);
1999
_math: function (fn, unit, n) {
2000
if (n instanceof tree.Dimension) {
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
argb: function (color) {
2009
return new(tree.Anonymous)(color.toARGB());
2012
percentage: function (n) {
2013
return new(tree.Dimension)(n.value * 100, '%');
2015
color: function (n) {
2016
if (n instanceof tree.Quoted) {
2017
return new(tree.Color)(n.value.slice(1));
2019
throw { type: "Argument", message: "argument must be a string" };
2022
iscolor: function (n) {
2023
return this._isa(n, tree.Color);
2025
isnumber: function (n) {
2026
return this._isa(n, tree.Dimension);
2028
isstring: function (n) {
2029
return this._isa(n, tree.Quoted);
2031
iskeyword: function (n) {
2032
return this._isa(n, tree.Keyword);
2034
isurl: function (n) {
2035
return this._isa(n, tree.URL);
2037
ispixel: function (n) {
2038
return (n instanceof tree.Dimension) && n.unit.is('px') ? tree.True : tree.False;
2040
ispercentage: function (n) {
2041
return (n instanceof tree.Dimension) && n.unit.is('%') ? tree.True : tree.False;
2043
isem: function (n) {
2044
return (n instanceof tree.Dimension) && n.unit.is('em') ? tree.True : tree.False;
2046
_isa: function (n, Type) {
2047
return (n instanceof Type) ? tree.True : tree.False;
2050
/* Blending modes */
2052
multiply: function(color1, color2) {
2053
var r = color1.rgb[0] * color2.rgb[0] / 255;
2054
var g = color1.rgb[1] * color2.rgb[1] / 255;
2055
var b = color1.rgb[2] * color2.rgb[2] / 255;
2056
return this.rgb(r, g, b);
2058
screen: function(color1, color2) {
2059
var r = 255 - (255 - color1.rgb[0]) * (255 - color2.rgb[0]) / 255;
2060
var g = 255 - (255 - color1.rgb[1]) * (255 - color2.rgb[1]) / 255;
2061
var b = 255 - (255 - color1.rgb[2]) * (255 - color2.rgb[2]) / 255;
2062
return this.rgb(r, g, b);
2064
overlay: function(color1, color2) {
2065
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;
2066
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;
2067
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;
2068
return this.rgb(r, g, b);
2070
softlight: function(color1, color2) {
2071
var t = color2.rgb[0] * color1.rgb[0] / 255;
2072
var r = t + color1.rgb[0] * (255 - (255 - color1.rgb[0]) * (255 - color2.rgb[0]) / 255 - t) / 255;
2073
t = color2.rgb[1] * color1.rgb[1] / 255;
2074
var g = t + color1.rgb[1] * (255 - (255 - color1.rgb[1]) * (255 - color2.rgb[1]) / 255 - t) / 255;
2075
t = color2.rgb[2] * color1.rgb[2] / 255;
2076
var b = t + color1.rgb[2] * (255 - (255 - color1.rgb[2]) * (255 - color2.rgb[2]) / 255 - t) / 255;
2077
return this.rgb(r, g, b);
2079
hardlight: function(color1, color2) {
2080
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;
2081
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;
2082
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;
2083
return this.rgb(r, g, b);
2085
difference: function(color1, color2) {
2086
var r = Math.abs(color1.rgb[0] - color2.rgb[0]);
2087
var g = Math.abs(color1.rgb[1] - color2.rgb[1]);
2088
var b = Math.abs(color1.rgb[2] - color2.rgb[2]);
2089
return this.rgb(r, g, b);
2091
exclusion: function(color1, color2) {
2092
var r = color1.rgb[0] + color2.rgb[0] * (255 - color1.rgb[0] - color1.rgb[0]) / 255;
2093
var g = color1.rgb[1] + color2.rgb[1] * (255 - color1.rgb[1] - color1.rgb[1]) / 255;
2094
var b = color1.rgb[2] + color2.rgb[2] * (255 - color1.rgb[2] - color1.rgb[2]) / 255;
2095
return this.rgb(r, g, b);
2097
average: function(color1, color2) {
2098
var r = (color1.rgb[0] + color2.rgb[0]) / 2;
2099
var g = (color1.rgb[1] + color2.rgb[1]) / 2;
2100
var b = (color1.rgb[2] + color2.rgb[2]) / 2;
2101
return this.rgb(r, g, b);
2103
negation: function(color1, color2) {
2104
var r = 255 - Math.abs(255 - color2.rgb[0] - color1.rgb[0]);
2105
var g = 255 - Math.abs(255 - color2.rgb[1] - color1.rgb[1]);
2106
var b = 255 - Math.abs(255 - color2.rgb[2] - color1.rgb[2]);
2107
return this.rgb(r, g, b);
2109
tint: function(color, amount) {
2110
return this.mix(this.rgb(255,255,255), color, amount);
2112
shade: function(color, amount) {
2113
return this.mix(this.rgb(0, 0, 0), color, amount);
2115
extract: function(values, index) {
2116
index = index.value - 1; // (1-based index)
2117
return values.value[index];
2120
"data-uri": function(mimetypeNode, filePathNode) {
2122
if (typeof window !== 'undefined') {
2123
return new tree.URL(filePathNode || mimetypeNode, this.currentFileInfo).eval(this.env);
2126
var mimetype = mimetypeNode.value;
2127
var filePath = (filePathNode && filePathNode.value);
2129
var fs = require("fs"),
2130
path = require("path"),
2133
if (arguments.length < 2) {
2134
filePath = mimetype;
2137
if (this.env.isPathRelative(filePath)) {
2138
if (this.currentFileInfo.relativeUrls) {
2139
filePath = path.join(this.currentFileInfo.currentDirectory, filePath);
2141
filePath = path.join(this.currentFileInfo.entryPath, filePath);
2145
// detect the mimetype if not given
2146
if (arguments.length < 2) {
2149
mime = require('mime');
2154
mimetype = mime.lookup(filePath);
2156
// use base 64 unless it's an ASCII or UTF-8 format
2157
var charset = mime.charsets.lookup(mimetype);
2158
useBase64 = ['US-ASCII', 'UTF-8'].indexOf(charset) < 0;
2159
if (useBase64) mimetype += ';base64';
2162
useBase64 = /;base64$/.test(mimetype)
2165
var buf = fs.readFileSync(filePath);
2167
// IE8 cannot handle a data-uri larger than 32KB. If this is exceeded
2168
// and the --ieCompat flag is enabled, return a normal url() instead.
2169
var DATA_URI_MAX_KB = 32,
2170
fileSizeInKB = parseInt((buf.length / 1024), 10);
2171
if (fileSizeInKB >= DATA_URI_MAX_KB) {
2173
if (this.env.ieCompat !== false) {
2174
if (!this.env.silent) {
2175
console.warn("Skipped data-uri embedding of %s because its size (%dKB) exceeds IE8-safe %dKB!", filePath, fileSizeInKB, DATA_URI_MAX_KB);
2178
return new tree.URL(filePathNode || mimetypeNode, this.currentFileInfo).eval(this.env);
2179
} else if (!this.env.silent) {
2180
// if explicitly disabled (via --no-ie-compat on CLI, or env.ieCompat === false), merely warn
2181
console.warn("WARNING: Embedding %s (%dKB) exceeds IE8's data-uri size limit of %dKB!", filePath, fileSizeInKB, DATA_URI_MAX_KB);
2185
buf = useBase64 ? buf.toString('base64')
2186
: encodeURIComponent(buf);
2188
var uri = "'data:" + mimetype + ',' + buf + "'";
2189
return new(tree.URL)(new(tree.Anonymous)(uri));
2193
// these static methods are used as a fallback when the optional 'mime' dependency is missing
2195
// this map is intentionally incomplete
2196
// if you want more, install 'mime' dep
2198
'.htm' : 'text/html',
2199
'.html': 'text/html',
2200
'.gif' : 'image/gif',
2201
'.jpg' : 'image/jpeg',
2202
'.jpeg': 'image/jpeg',
2203
'.png' : 'image/png'
2205
lookup: function (filepath) {
2206
var ext = require('path').extname(filepath),
2207
type = tree._mime._types[ext];
2208
if (type === undefined) {
2209
throw new Error('Optional dependency "mime" is required for ' + ext);
2214
lookup: function (type) {
2215
// assumes all text types are UTF-8
2216
return type && (/^text\//).test(type) ? 'UTF-8' : '';
2221
var mathFunctions = [{name:"ceil"}, {name:"floor"}, {name: "sqrt"}, {name:"abs"},
2222
{name:"tan", unit: ""}, {name:"sin", unit: ""}, {name:"cos", unit: ""},
2223
{name:"atan", unit: "rad"}, {name:"asin", unit: "rad"}, {name:"acos", unit: "rad"}],
2224
createMathFunction = function(name, unit) {
2225
return function(n) {
2229
return this._math(Math[name], unit, n);
2233
for(var i = 0; i < mathFunctions.length; i++) {
2234
tree.functions[mathFunctions[i].name] = createMathFunction(mathFunctions[i].name, mathFunctions[i].unit);
2237
function hsla(color) {
2238
return tree.functions.hsla(color.h, color.s, color.l, color.a);
2241
function scaled(n, size) {
2242
if (n instanceof tree.Dimension && n.unit.is('%')) {
2243
return parseFloat(n.value * size / 100);
2249
function number(n) {
2250
if (n instanceof tree.Dimension) {
2251
return parseFloat(n.unit.is('%') ? n.value / 100 : n.value);
2252
} else if (typeof(n) === 'number') {
2256
error: "RuntimeError",
2257
message: "color functions take numbers as parameters"
2262
function clamp(val) {
2263
return Math.min(1, Math.max(0, val));
2266
tree.functionCall = function(env, currentFileInfo) {
2268
this.currentFileInfo = currentFileInfo;
2271
tree.functionCall.prototype = tree.functions;
2273
})(require('./tree'));
2276
'aliceblue':'#f0f8ff',
2277
'antiquewhite':'#faebd7',
2279
'aquamarine':'#7fffd4',
2284
'blanchedalmond':'#ffebcd',
2286
'blueviolet':'#8a2be2',
2288
'burlywood':'#deb887',
2289
'cadetblue':'#5f9ea0',
2290
'chartreuse':'#7fff00',
2291
'chocolate':'#d2691e',
2293
'cornflowerblue':'#6495ed',
2294
'cornsilk':'#fff8dc',
2295
'crimson':'#dc143c',
2297
'darkblue':'#00008b',
2298
'darkcyan':'#008b8b',
2299
'darkgoldenrod':'#b8860b',
2300
'darkgray':'#a9a9a9',
2301
'darkgrey':'#a9a9a9',
2302
'darkgreen':'#006400',
2303
'darkkhaki':'#bdb76b',
2304
'darkmagenta':'#8b008b',
2305
'darkolivegreen':'#556b2f',
2306
'darkorange':'#ff8c00',
2307
'darkorchid':'#9932cc',
2308
'darkred':'#8b0000',
2309
'darksalmon':'#e9967a',
2310
'darkseagreen':'#8fbc8f',
2311
'darkslateblue':'#483d8b',
2312
'darkslategray':'#2f4f4f',
2313
'darkslategrey':'#2f4f4f',
2314
'darkturquoise':'#00ced1',
2315
'darkviolet':'#9400d3',
2316
'deeppink':'#ff1493',
2317
'deepskyblue':'#00bfff',
2318
'dimgray':'#696969',
2319
'dimgrey':'#696969',
2320
'dodgerblue':'#1e90ff',
2321
'firebrick':'#b22222',
2322
'floralwhite':'#fffaf0',
2323
'forestgreen':'#228b22',
2324
'fuchsia':'#ff00ff',
2325
'gainsboro':'#dcdcdc',
2326
'ghostwhite':'#f8f8ff',
2328
'goldenrod':'#daa520',
2332
'greenyellow':'#adff2f',
2333
'honeydew':'#f0fff0',
2334
'hotpink':'#ff69b4',
2335
'indianred':'#cd5c5c',
2339
'lavender':'#e6e6fa',
2340
'lavenderblush':'#fff0f5',
2341
'lawngreen':'#7cfc00',
2342
'lemonchiffon':'#fffacd',
2343
'lightblue':'#add8e6',
2344
'lightcoral':'#f08080',
2345
'lightcyan':'#e0ffff',
2346
'lightgoldenrodyellow':'#fafad2',
2347
'lightgray':'#d3d3d3',
2348
'lightgrey':'#d3d3d3',
2349
'lightgreen':'#90ee90',
2350
'lightpink':'#ffb6c1',
2351
'lightsalmon':'#ffa07a',
2352
'lightseagreen':'#20b2aa',
2353
'lightskyblue':'#87cefa',
2354
'lightslategray':'#778899',
2355
'lightslategrey':'#778899',
2356
'lightsteelblue':'#b0c4de',
2357
'lightyellow':'#ffffe0',
2359
'limegreen':'#32cd32',
2361
'magenta':'#ff00ff',
2363
'mediumaquamarine':'#66cdaa',
2364
'mediumblue':'#0000cd',
2365
'mediumorchid':'#ba55d3',
2366
'mediumpurple':'#9370d8',
2367
'mediumseagreen':'#3cb371',
2368
'mediumslateblue':'#7b68ee',
2369
'mediumspringgreen':'#00fa9a',
2370
'mediumturquoise':'#48d1cc',
2371
'mediumvioletred':'#c71585',
2372
'midnightblue':'#191970',
2373
'mintcream':'#f5fffa',
2374
'mistyrose':'#ffe4e1',
2375
'moccasin':'#ffe4b5',
2376
'navajowhite':'#ffdead',
2378
'oldlace':'#fdf5e6',
2380
'olivedrab':'#6b8e23',
2382
'orangered':'#ff4500',
2384
'palegoldenrod':'#eee8aa',
2385
'palegreen':'#98fb98',
2386
'paleturquoise':'#afeeee',
2387
'palevioletred':'#d87093',
2388
'papayawhip':'#ffefd5',
2389
'peachpuff':'#ffdab9',
2393
'powderblue':'#b0e0e6',
2396
'rosybrown':'#bc8f8f',
2397
'royalblue':'#4169e1',
2398
'saddlebrown':'#8b4513',
2400
'sandybrown':'#f4a460',
2401
'seagreen':'#2e8b57',
2402
'seashell':'#fff5ee',
2405
'skyblue':'#87ceeb',
2406
'slateblue':'#6a5acd',
2407
'slategray':'#708090',
2408
'slategrey':'#708090',
2410
'springgreen':'#00ff7f',
2411
'steelblue':'#4682b4',
2414
'thistle':'#d8bfd8',
2416
// 'transparent':'rgba(0,0,0,0)',
2417
'turquoise':'#40e0d0',
2421
'whitesmoke':'#f5f5f5',
2423
'yellowgreen':'#9acd32'
2425
})(require('./tree'));
2428
tree.Alpha = function (val) {
2431
tree.Alpha.prototype = {
2433
accept: function (visitor) {
2434
this.value = visitor.visit(this.value);
2436
eval: function (env) {
2437
if (this.value.eval) { this.value = this.value.eval(env) }
2440
toCSS: function () {
2441
return "alpha(opacity=" +
2442
(this.value.toCSS ? this.value.toCSS() : this.value) + ")";
2446
})(require('../tree'));
2449
tree.Anonymous = function (string) {
2450
this.value = string.value || string;
2452
tree.Anonymous.prototype = {
2454
toCSS: function () {
2457
eval: function () { return this },
2458
compare: function (x) {
2463
var left = this.toCSS(),
2466
if (left === right) {
2470
return left < right ? -1 : 1;
2474
})(require('../tree'));
2477
tree.Assignment = function (key, val) {
2481
tree.Assignment.prototype = {
2483
accept: function (visitor) {
2484
this.value = visitor.visit(this.value);
2486
toCSS: function () {
2487
return this.key + '=' + (this.value.toCSS ? this.value.toCSS() : this.value);
2489
eval: function (env) {
2490
if (this.value.eval) {
2491
return new(tree.Assignment)(this.key, this.value.eval(env));
2497
})(require('../tree'));(function (tree) {
2500
// A function call node.
2502
tree.Call = function (name, args, index, currentFileInfo) {
2506
this.currentFileInfo = currentFileInfo;
2508
tree.Call.prototype = {
2510
accept: function (visitor) {
2511
this.args = visitor.visit(this.args);
2514
// When evaluating a function call,
2515
// we either find the function in `tree.functions` [1],
2516
// in which case we call it, passing the evaluated arguments,
2517
// if this returns null or we cannot find the function, we
2518
// simply print it out as it appeared originally [2].
2520
// The *functions.js* file contains the built-in functions.
2522
// The reason why we evaluate the arguments, is in the case where
2523
// we try to pass a variable to a function, like: `saturate(@color)`.
2524
// The function should receive the value, not the variable.
2526
eval: function (env) {
2527
var args = this.args.map(function (a) { return a.eval(env); }),
2528
nameLC = this.name.toLowerCase(),
2531
if (nameLC in tree.functions) { // 1.
2533
func = new tree.functionCall(env, this.currentFileInfo);
2534
result = func[nameLC].apply(func, args);
2535
if (result != null) {
2539
throw { type: e.type || "Runtime",
2540
message: "error evaluating function `" + this.name + "`" +
2541
(e.message ? ': ' + e.message : ''),
2542
index: this.index, filename: this.currentFileInfo.filename };
2547
return new(tree.Anonymous)(this.name +
2548
"(" + args.map(function (a) { return a.toCSS(env); }).join(', ') + ")");
2551
toCSS: function (env) {
2552
return this.eval(env).toCSS();
2556
})(require('../tree'));
2559
// RGB Colors - #ff0014, #eee
2561
tree.Color = function (rgb, a) {
2563
// The end goal here, is to parse the arguments
2564
// into an integer triplet, such as `128, 255, 0`
2566
// This facilitates operations and conversions.
2568
if (Array.isArray(rgb)) {
2570
} else if (rgb.length == 6) {
2571
this.rgb = rgb.match(/.{2}/g).map(function (c) {
2572
return parseInt(c, 16);
2575
this.rgb = rgb.split('').map(function (c) {
2576
return parseInt(c + c, 16);
2579
this.alpha = typeof(a) === 'number' ? a : 1;
2581
tree.Color.prototype = {
2583
eval: function () { return this },
2584
luma: function () { return (0.2126 * this.rgb[0] / 255) + (0.7152 * this.rgb[1] / 255) + (0.0722 * this.rgb[2] / 255); },
2587
// If we have some transparency, the only way to represent it
2588
// is via `rgba`. Otherwise, we use the hex representation,
2589
// which has better compatibility with older browsers.
2590
// Values are capped between `0` and `255`, rounded and zero-padded.
2592
toCSS: function (env, doNotCompress) {
2593
var compress = env && env.compress && !doNotCompress;
2594
if (this.alpha < 1.0) {
2595
return "rgba(" + this.rgb.map(function (c) {
2596
return Math.round(c);
2597
}).concat(this.alpha).join(',' + (compress ? '' : ' ')) + ")";
2599
var color = this.rgb.map(function (i) {
2601
i = (i > 255 ? 255 : (i < 0 ? 0 : i)).toString(16);
2602
return i.length === 1 ? '0' + i : i;
2606
color = color.split('');
2608
// Convert color to short format
2609
if (color[0] == color[1] && color[2] == color[3] && color[4] == color[5]) {
2610
color = color[0] + color[2] + color[4];
2612
color = color.join('');
2621
// Operations have to be done per-channel, if not,
2622
// channels will spill onto each other. Once we have
2623
// our result, in the form of an integer triplet,
2624
// we create a new Color node to hold the result.
2626
operate: function (env, op, other) {
2629
if (! (other instanceof tree.Color)) {
2630
other = other.toColor();
2633
for (var c = 0; c < 3; c++) {
2634
result[c] = tree.operate(env, op, this.rgb[c], other.rgb[c]);
2636
return new(tree.Color)(result, this.alpha + other.alpha);
2639
toHSL: function () {
2640
var r = this.rgb[0] / 255,
2641
g = this.rgb[1] / 255,
2642
b = this.rgb[2] / 255,
2645
var max = Math.max(r, g, b), min = Math.min(r, g, b);
2646
var h, s, l = (max + min) / 2, d = max - min;
2651
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
2654
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
2655
case g: h = (b - r) / d + 2; break;
2656
case b: h = (r - g) / d + 4; break;
2660
return { h: h * 360, s: s, l: l, a: a };
2662
//Adapted from http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
2663
toHSV: function () {
2664
var r = this.rgb[0] / 255,
2665
g = this.rgb[1] / 255,
2666
b = this.rgb[2] / 255,
2669
var max = Math.max(r, g, b), min = Math.min(r, g, b);
2683
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
2684
case g: h = (b - r) / d + 2; break;
2685
case b: h = (r - g) / d + 4; break;
2689
return { h: h * 360, s: s, v: v, a: a };
2691
toARGB: function () {
2692
var argb = [Math.round(this.alpha * 255)].concat(this.rgb);
2693
return '#' + argb.map(function (i) {
2695
i = (i > 255 ? 255 : (i < 0 ? 0 : i)).toString(16);
2696
return i.length === 1 ? '0' + i : i;
2699
compare: function (x) {
2704
return (x.rgb[0] === this.rgb[0] &&
2705
x.rgb[1] === this.rgb[1] &&
2706
x.rgb[2] === this.rgb[2] &&
2707
x.alpha === this.alpha) ? 0 : -1;
2712
})(require('../tree'));
2715
tree.Comment = function (value, silent) {
2717
this.silent = !!silent;
2719
tree.Comment.prototype = {
2721
toCSS: function (env) {
2722
return env.compress ? '' : this.value;
2724
eval: function () { return this }
2727
})(require('../tree'));
2730
tree.Condition = function (op, l, r, i, negate) {
2731
this.op = op.trim();
2735
this.negate = negate;
2737
tree.Condition.prototype = {
2739
accept: function (visitor) {
2740
this.lvalue = visitor.visit(this.lvalue);
2741
this.rvalue = visitor.visit(this.rvalue);
2743
eval: function (env) {
2744
var a = this.lvalue.eval(env),
2745
b = this.rvalue.eval(env);
2747
var i = this.index, result;
2749
var result = (function (op) {
2757
result = a.compare(b);
2758
} else if (b.compare) {
2759
result = b.compare(a);
2761
throw { type: "Type",
2762
message: "Unable to perform comparison",
2766
case -1: return op === '<' || op === '=<';
2767
case 0: return op === '=' || op === '>=' || op === '=<';
2768
case 1: return op === '>' || op === '>=';
2772
return this.negate ? !result : result;
2776
})(require('../tree'));
2780
// A number with a unit
2782
tree.Dimension = function (value, unit) {
2783
this.value = parseFloat(value);
2784
this.unit = (unit && unit instanceof tree.Unit) ? unit :
2785
new(tree.Unit)(unit ? [unit] : undefined);
2788
tree.Dimension.prototype = {
2790
accept: function (visitor) {
2791
this.unit = visitor.visit(this.unit);
2793
eval: function (env) {
2796
toColor: function () {
2797
return new(tree.Color)([this.value, this.value, this.value]);
2799
toCSS: function (env) {
2800
if ((!env || env.strictUnits !== false) && !this.unit.isSingular()) {
2801
throw new Error("Multiple units in dimension. Correct the units or use the unit function. Bad unit: "+this.unit.toString());
2804
var value = this.value,
2805
strValue = String(value);
2807
if (value !== 0 && value < 0.000001 && value > -0.000001) {
2808
// would be output 1e-6 etc.
2809
strValue = value.toFixed(20).replace(/0+$/, "");
2812
if (env && env.compress) {
2813
// Zero values doesn't need a unit
2814
if (value === 0 && !this.unit.isAngle()) {
2818
// Float values doesn't need a leading zero
2819
if (value > 0 && value < 1) {
2820
strValue = (strValue).substr(1);
2824
return this.unit.isEmpty() ? strValue : (strValue + this.unit.toCSS());
2827
// In an operation between two Dimensions,
2828
// we default to the first Dimension's unit,
2829
// so `1px + 2` will yield `3px`.
2830
operate: function (env, op, other) {
2831
var value = tree.operate(env, op, this.value, other.value),
2832
unit = this.unit.clone();
2834
if (op === '+' || op === '-') {
2835
if (unit.numerator.length === 0 && unit.denominator.length === 0) {
2836
unit.numerator = other.unit.numerator.slice(0);
2837
unit.denominator = other.unit.denominator.slice(0);
2838
} else if (other.unit.numerator.length == 0 && unit.denominator.length == 0) {
2841
other = other.convertTo(this.unit.usedUnits());
2843
if(env.strictUnits !== false && other.unit.toString() !== unit.toString()) {
2844
throw new Error("Incompatible units. Change the units or use the unit function. Bad units: '" + unit.toString() +
2845
"' and '" + other.unit.toString() + "'.");
2848
value = tree.operate(env, op, this.value, other.value);
2850
} else if (op === '*') {
2851
unit.numerator = unit.numerator.concat(other.unit.numerator).sort();
2852
unit.denominator = unit.denominator.concat(other.unit.denominator).sort();
2854
} else if (op === '/') {
2855
unit.numerator = unit.numerator.concat(other.unit.denominator).sort();
2856
unit.denominator = unit.denominator.concat(other.unit.numerator).sort();
2859
return new(tree.Dimension)(value, unit);
2862
compare: function (other) {
2863
if (other instanceof tree.Dimension) {
2864
var a = this.unify(), b = other.unify(),
2865
aValue = a.value, bValue = b.value;
2867
if (bValue > aValue) {
2869
} else if (bValue < aValue) {
2872
if (!b.unit.isEmpty() && a.unit.compare(b.unit) !== 0) {
2882
unify: function () {
2883
return this.convertTo({ length: 'm', duration: 's', angle: 'rad' });
2886
convertTo: function (conversions) {
2887
var value = this.value, unit = this.unit.clone(),
2888
i, groupName, group, conversion, targetUnit, derivedConversions = {};
2890
if (typeof conversions === 'string') {
2891
for(i in tree.UnitConversions) {
2892
if (tree.UnitConversions[i].hasOwnProperty(conversions)) {
2893
derivedConversions = {};
2894
derivedConversions[i] = conversions;
2897
conversions = derivedConversions;
2900
for (groupName in conversions) {
2901
if (conversions.hasOwnProperty(groupName)) {
2902
targetUnit = conversions[groupName];
2903
group = tree.UnitConversions[groupName];
2905
unit.map(function (atomicUnit, denominator) {
2906
if (group.hasOwnProperty(atomicUnit)) {
2908
value = value / (group[atomicUnit] / group[targetUnit]);
2910
value = value * (group[atomicUnit] / group[targetUnit]);
2923
return new(tree.Dimension)(value, unit);
2927
// http://www.w3.org/TR/css3-values/#absolute-lengths
2928
tree.UnitConversions = {
2935
'pc': 0.0254 / 72 * 12
2942
'rad': 1/(2*Math.PI),
2949
tree.Unit = function (numerator, denominator) {
2950
this.numerator = numerator ? numerator.slice(0).sort() : [];
2951
this.denominator = denominator ? denominator.slice(0).sort() : [];
2954
tree.Unit.prototype = {
2956
clone: function () {
2957
return new tree.Unit(this.numerator.slice(0), this.denominator.slice(0));
2960
toCSS: function () {
2961
if (this.numerator.length >= 1) {
2962
return this.numerator[0];
2964
if (this.denominator.length >= 1) {
2965
return this.denominator[0];
2970
toString: function () {
2971
var i, returnStr = this.numerator.join("*");
2972
for (i = 0; i < this.denominator.length; i++) {
2973
returnStr += "/" + this.denominator[i];
2978
compare: function (other) {
2979
return this.is(other.toCSS()) ? 0 : -1;
2982
is: function (unitString) {
2983
return this.toCSS() === unitString;
2986
isAngle: function () {
2987
return tree.UnitConversions.angle.hasOwnProperty(this.toCSS());
2990
isEmpty: function () {
2991
return this.numerator.length == 0 && this.denominator.length == 0;
2994
isSingular: function() {
2995
return this.numerator.length <= 1 && this.denominator.length == 0;
2998
map: function(callback) {
3001
for (i = 0; i < this.numerator.length; i++) {
3002
this.numerator[i] = callback(this.numerator[i], false);
3005
for (i = 0; i < this.denominator.length; i++) {
3006
this.denominator[i] = callback(this.denominator[i], true);
3010
usedUnits: function() {
3011
var group, groupName, result = {};
3013
for (groupName in tree.UnitConversions) {
3014
if (tree.UnitConversions.hasOwnProperty(groupName)) {
3015
group = tree.UnitConversions[groupName];
3017
this.map(function (atomicUnit) {
3018
if (group.hasOwnProperty(atomicUnit) && !result[groupName]) {
3019
result[groupName] = atomicUnit;
3030
cancel: function () {
3031
var counter = {}, atomicUnit, i;
3033
for (i = 0; i < this.numerator.length; i++) {
3034
atomicUnit = this.numerator[i];
3035
counter[atomicUnit] = (counter[atomicUnit] || 0) + 1;
3038
for (i = 0; i < this.denominator.length; i++) {
3039
atomicUnit = this.denominator[i];
3040
counter[atomicUnit] = (counter[atomicUnit] || 0) - 1;
3043
this.numerator = [];
3044
this.denominator = [];
3046
for (atomicUnit in counter) {
3047
if (counter.hasOwnProperty(atomicUnit)) {
3048
var count = counter[atomicUnit];
3051
for (i = 0; i < count; i++) {
3052
this.numerator.push(atomicUnit);
3054
} else if (count < 0) {
3055
for (i = 0; i < -count; i++) {
3056
this.denominator.push(atomicUnit);
3062
this.numerator.sort();
3063
this.denominator.sort();
3067
})(require('../tree'));
3070
tree.Directive = function (name, value) {
3073
if (Array.isArray(value)) {
3074
this.ruleset = new(tree.Ruleset)([], value);
3075
this.ruleset.allowImports = true;
3080
tree.Directive.prototype = {
3082
accept: function (visitor) {
3083
this.ruleset = visitor.visit(this.ruleset);
3084
this.value = visitor.visit(this.value);
3086
toCSS: function (env) {
3088
this.ruleset.root = true;
3089
return this.name + (env.compress ? '{' : ' {\n ') +
3090
this.ruleset.toCSS(env).trim().replace(/\n/g, '\n ') +
3091
(env.compress ? '}': '\n}\n');
3093
return this.name + ' ' + this.value.toCSS() + ';\n';
3096
eval: function (env) {
3097
var evaldDirective = this;
3099
env.frames.unshift(this);
3100
evaldDirective = new(tree.Directive)(this.name);
3101
evaldDirective.ruleset = this.ruleset.eval(env);
3104
return evaldDirective;
3106
variable: function (name) { return tree.Ruleset.prototype.variable.call(this.ruleset, name) },
3107
find: function () { return tree.Ruleset.prototype.find.apply(this.ruleset, arguments) },
3108
rulesets: function () { return tree.Ruleset.prototype.rulesets.apply(this.ruleset) }
3111
})(require('../tree'));
3114
tree.Element = function (combinator, value, index) {
3115
this.combinator = combinator instanceof tree.Combinator ?
3116
combinator : new(tree.Combinator)(combinator);
3118
if (typeof(value) === 'string') {
3119
this.value = value.trim();
3127
tree.Element.prototype = {
3129
accept: function (visitor) {
3130
this.combinator = visitor.visit(this.combinator);
3131
this.value = visitor.visit(this.value);
3133
eval: function (env) {
3134
return new(tree.Element)(this.combinator,
3135
this.value.eval ? this.value.eval(env) : this.value,
3138
toCSS: function (env) {
3139
var value = (this.value.toCSS ? this.value.toCSS(env) : this.value);
3140
if (value == '' && this.combinator.value.charAt(0) == '&') {
3143
return this.combinator.toCSS(env || {}) + value;
3148
tree.Attribute = function (key, op, value) {
3153
tree.Attribute.prototype = {
3155
accept: function (visitor) {
3156
this.value = visitor.visit(this.value);
3158
eval: function (env) {
3159
return new(tree.Attribute)(this.key.eval ? this.key.eval(env) : this.key,
3160
this.op, (this.value && this.value.eval) ? this.value.eval(env) : this.value);
3162
toCSS: function (env) {
3163
var value = this.key.toCSS ? this.key.toCSS(env) : this.key;
3167
value += (this.value.toCSS ? this.value.toCSS(env) : this.value);
3170
return '[' + value + ']';
3174
tree.Combinator = function (value) {
3175
if (value === ' ') {
3178
this.value = value ? value.trim() : "";
3181
tree.Combinator.prototype = {
3183
toCSS: function (env) {
3188
'+' : env.compress ? '+' : ' + ',
3189
'~' : env.compress ? '~' : ' ~ ',
3190
'>' : env.compress ? '>' : ' > ',
3191
'|' : env.compress ? '|' : ' | '
3196
})(require('../tree'));
3199
tree.Expression = function (value) { this.value = value; };
3200
tree.Expression.prototype = {
3202
accept: function (visitor) {
3203
this.value = visitor.visit(this.value);
3205
eval: function (env) {
3207
inParenthesis = this.parens && !this.parensInOp,
3208
doubleParen = false;
3209
if (inParenthesis) {
3210
env.inParenthesis();
3212
if (this.value.length > 1) {
3213
returnValue = new(tree.Expression)(this.value.map(function (e) {
3216
} else if (this.value.length === 1) {
3217
if (this.value[0].parens && !this.value[0].parensInOp) {
3220
returnValue = this.value[0].eval(env);
3224
if (inParenthesis) {
3225
env.outOfParenthesis();
3227
if (this.parens && this.parensInOp && !(env.isMathsOn()) && !doubleParen) {
3228
returnValue = new(tree.Paren)(returnValue);
3232
toCSS: function (env) {
3233
return this.value.map(function (e) {
3234
return e.toCSS ? e.toCSS(env) : '';
3237
throwAwayComments: function () {
3238
this.value = this.value.filter(function(v) {
3239
return !(v instanceof tree.Comment);
3244
})(require('../tree'));
3247
tree.Extend = function Extend(selector, option, index) {
3248
this.selector = selector;
3249
this.option = option;
3254
this.allowBefore = true;
3255
this.allowAfter = true;
3258
this.allowBefore = false;
3259
this.allowAfter = false;
3264
tree.Extend.prototype = {
3266
accept: function (visitor) {
3267
this.selector = visitor.visit(this.selector);
3269
eval: function (env) {
3270
return new(tree.Extend)(this.selector.eval(env), this.option, this.index);
3272
clone: function (env) {
3273
return new(tree.Extend)(this.selector, this.option, this.index);
3275
findSelfSelectors: function (selectors) {
3276
var selfElements = [];
3278
for(i = 0; i < selectors.length; i++) {
3279
selfElements = selfElements.concat(selectors[i].elements);
3282
this.selfSelectors = [{ elements: selfElements }];
3286
})(require('../tree'));
3291
// The general strategy here is that we don't want to wait
3292
// for the parsing to be completed, before we start importing
3293
// the file. That's because in the context of a browser,
3294
// most of the time will be spent waiting for the server to respond.
3296
// On creation, we push the import path to our import queue, though
3297
// `import,push`, we also pass it a callback, which it'll call once
3298
// the file has been fetched, and parsed.
3300
tree.Import = function (path, features, options, index, currentFileInfo) {
3303
this.options = options;
3306
this.features = features;
3307
this.currentFileInfo = currentFileInfo;
3309
if (this.options.less !== undefined) {
3310
this.css = !this.options.less;
3312
var pathValue = this.getPath();
3313
if (pathValue && /css([\?;].*)?$/.test(pathValue)) {
3320
// The actual import node doesn't return anything, when converted to CSS.
3321
// The reason is that it's used at the evaluation stage, so that the rules
3322
// it imports can be treated like any other rules.
3324
// In `eval`, we make sure all Import nodes get evaluated, recursively, so
3325
// we end up with a flat structure, which can easily be imported in the parent
3328
tree.Import.prototype = {
3330
accept: function (visitor) {
3331
this.features = visitor.visit(this.features);
3332
this.path = visitor.visit(this.path);
3333
this.root = visitor.visit(this.root);
3335
toCSS: function (env) {
3336
var features = this.features ? ' ' + this.features.toCSS(env) : '';
3339
return "@import " + this.path.toCSS() + features + ';\n';
3344
getPath: function () {
3345
if (this.path instanceof tree.Quoted) {
3346
var path = this.path.value;
3347
return (this.css !== undefined || /(\.[a-z]*$)|([\?;].*)$/.test(path)) ? path : path + '.less';
3348
} else if (this.path instanceof tree.URL) {
3349
return this.path.value.value;
3353
evalForImport: function (env) {
3354
return new(tree.Import)(this.path.eval(env), this.features, this.options, this.index, this.currentFileInfo);
3356
evalPath: function (env) {
3357
var path = this.path.eval(env);
3358
var rootpath = this.currentFileInfo && this.currentFileInfo.rootpath;
3359
if (rootpath && !(path instanceof tree.URL)) {
3360
var pathValue = path.value;
3361
// Add the base path if the import is relative
3362
if (pathValue && env.isPathRelative(pathValue)) {
3363
path.value = rootpath + pathValue;
3368
eval: function (env) {
3369
var ruleset, features = this.features && this.features.eval(env);
3371
if (this.skip) { return []; }
3374
var newImport = new(tree.Import)(this.evalPath(env), features, this.options, this.index);
3375
if (!newImport.css && this.error) {
3380
ruleset = new(tree.Ruleset)([], this.root.rules.slice(0));
3382
ruleset.evalImports(env);
3384
return this.features ? new(tree.Media)(ruleset.rules, this.features.value) : ruleset.rules;
3389
})(require('../tree'));
3392
tree.JavaScript = function (string, index, escaped) {
3393
this.escaped = escaped;
3394
this.expression = string;
3397
tree.JavaScript.prototype = {
3399
eval: function (env) {
3404
var expression = this.expression.replace(/@\{([\w-]+)\}/g, function (_, name) {
3405
return tree.jsify(new(tree.Variable)('@' + name, that.index).eval(env));
3409
expression = new(Function)('return (' + expression + ')');
3411
throw { message: "JavaScript evaluation error: `" + expression + "`" ,
3412
index: this.index };
3415
for (var k in env.frames[0].variables()) {
3416
context[k.slice(1)] = {
3417
value: env.frames[0].variables()[k].value,
3419
return this.value.eval(env).toCSS();
3425
result = expression.call(context);
3427
throw { message: "JavaScript evaluation error: '" + e.name + ': ' + e.message + "'" ,
3428
index: this.index };
3430
if (typeof(result) === 'string') {
3431
return new(tree.Quoted)('"' + result + '"', result, this.escaped, this.index);
3432
} else if (Array.isArray(result)) {
3433
return new(tree.Anonymous)(result.join(', '));
3435
return new(tree.Anonymous)(result);
3440
})(require('../tree'));
3444
tree.Keyword = function (value) { this.value = value };
3445
tree.Keyword.prototype = {
3447
eval: function () { return this; },
3448
toCSS: function () { return this.value; },
3449
compare: function (other) {
3450
if (other instanceof tree.Keyword) {
3451
return other.value === this.value ? 0 : 1;
3458
tree.True = new(tree.Keyword)('true');
3459
tree.False = new(tree.Keyword)('false');
3461
})(require('../tree'));
3464
tree.Media = function (value, features) {
3465
var selectors = this.emptySelectors();
3467
this.features = new(tree.Value)(features);
3468
this.ruleset = new(tree.Ruleset)(selectors, value);
3469
this.ruleset.allowImports = true;
3471
tree.Media.prototype = {
3473
accept: function (visitor) {
3474
this.features = visitor.visit(this.features);
3475
this.ruleset = visitor.visit(this.ruleset);
3477
toCSS: function (env) {
3478
var features = this.features.toCSS(env);
3480
return '@media ' + features + (env.compress ? '{' : ' {\n ') +
3481
this.ruleset.toCSS(env).trim().replace(/\n/g, '\n ') +
3482
(env.compress ? '}': '\n}\n');
3484
eval: function (env) {
3485
if (!env.mediaBlocks) {
3486
env.mediaBlocks = [];
3490
var media = new(tree.Media)([], []);
3491
if(this.debugInfo) {
3492
this.ruleset.debugInfo = this.debugInfo;
3493
media.debugInfo = this.debugInfo;
3495
var strictMathsBypass = false;
3496
if (env.strictMaths === false) {
3497
strictMathsBypass = true;
3498
env.strictMaths = true;
3501
media.features = this.features.eval(env);
3504
if (strictMathsBypass) {
3505
env.strictMaths = false;
3509
env.mediaPath.push(media);
3510
env.mediaBlocks.push(media);
3512
env.frames.unshift(this.ruleset);
3513
media.ruleset = this.ruleset.eval(env);
3516
env.mediaPath.pop();
3518
return env.mediaPath.length === 0 ? media.evalTop(env) :
3519
media.evalNested(env)
3521
variable: function (name) { return tree.Ruleset.prototype.variable.call(this.ruleset, name) },
3522
find: function () { return tree.Ruleset.prototype.find.apply(this.ruleset, arguments) },
3523
rulesets: function () { return tree.Ruleset.prototype.rulesets.apply(this.ruleset) },
3524
emptySelectors: function() {
3525
var el = new(tree.Element)('', '&', 0);
3526
return [new(tree.Selector)([el])];
3529
evalTop: function (env) {
3532
// Render all dependent Media blocks.
3533
if (env.mediaBlocks.length > 1) {
3534
var selectors = this.emptySelectors();
3535
result = new(tree.Ruleset)(selectors, env.mediaBlocks);
3536
result.multiMedia = true;
3539
delete env.mediaBlocks;
3540
delete env.mediaPath;
3544
evalNested: function (env) {
3546
path = env.mediaPath.concat([this]);
3548
// Extract the media-query conditions separated with `,` (OR).
3549
for (i = 0; i < path.length; i++) {
3550
value = path[i].features instanceof tree.Value ?
3551
path[i].features.value : path[i].features;
3552
path[i] = Array.isArray(value) ? value : [value];
3555
// Trace all permutations to generate the resulting media-query.
3557
// (a, b and c) with nested (d, e) ->
3562
this.features = new(tree.Value)(this.permute(path).map(function (path) {
3563
path = path.map(function (fragment) {
3564
return fragment.toCSS ? fragment : new(tree.Anonymous)(fragment);
3567
for(i = path.length - 1; i > 0; i--) {
3568
path.splice(i, 0, new(tree.Anonymous)("and"));
3571
return new(tree.Expression)(path);
3574
// Fake a tree-node that doesn't output anything.
3575
return new(tree.Ruleset)([], []);
3577
permute: function (arr) {
3578
if (arr.length === 0) {
3580
} else if (arr.length === 1) {
3584
var rest = this.permute(arr.slice(1));
3585
for (var i = 0; i < rest.length; i++) {
3586
for (var j = 0; j < arr[0].length; j++) {
3587
result.push([arr[0][j]].concat(rest[i]));
3593
bubbleSelectors: function (selectors) {
3594
this.ruleset = new(tree.Ruleset)(selectors.slice(0), [this.ruleset]);
3598
})(require('../tree'));
3602
tree.mixin.Call = function (elements, args, index, currentFileInfo, important) {
3603
this.selector = new(tree.Selector)(elements);
3604
this.arguments = args;
3606
this.currentFileInfo = currentFileInfo;
3607
this.important = important;
3609
tree.mixin.Call.prototype = {
3611
accept: function (visitor) {
3612
this.selector = visitor.visit(this.selector);
3613
this.arguments = visitor.visit(this.arguments);
3615
eval: function (env) {
3616
var mixins, mixin, args, rules = [], match = false, i, m, f, isRecursive, isOneFound;
3618
args = this.arguments && this.arguments.map(function (a) {
3619
return { name: a.name, value: a.value.eval(env) };
3622
for (i = 0; i < env.frames.length; i++) {
3623
if ((mixins = env.frames[i].find(this.selector)).length > 0) {
3625
for (m = 0; m < mixins.length; m++) {
3627
isRecursive = false;
3628
for(f = 0; f < env.frames.length; f++) {
3629
if ((!(mixin instanceof tree.mixin.Definition)) && mixin === (env.frames[f].originalRuleset || env.frames[f])) {
3637
if (mixin.matchArgs(args, env)) {
3638
if (!mixin.matchCondition || mixin.matchCondition(args, env)) {
3640
Array.prototype.push.apply(
3641
rules, mixin.eval(env, args, this.important).rules);
3643
throw { message: e.message, index: this.index, filename: this.currentFileInfo.filename, stack: e.stack };
3655
throw { type: 'Runtime',
3656
message: 'No matching definition was found for `' +
3657
this.selector.toCSS().trim() + '(' +
3658
(args ? args.map(function (a) {
3661
argValue += a.name + ":";
3663
if (a.value.toCSS) {
3664
argValue += a.value.toCSS();
3669
}).join(', ') : "") + ")`",
3670
index: this.index, filename: this.currentFileInfo.filename };
3672
throw { type: 'Name',
3673
message: this.selector.toCSS().trim() + " is undefined",
3674
index: this.index, filename: this.currentFileInfo.filename };
3679
tree.mixin.Definition = function (name, params, rules, condition, variadic) {
3681
this.selectors = [new(tree.Selector)([new(tree.Element)(null, name)])];
3682
this.params = params;
3683
this.condition = condition;
3684
this.variadic = variadic;
3685
this.arity = params.length;
3688
this.required = params.reduce(function (count, p) {
3689
if (!p.name || (p.name && !p.value)) { return count + 1 }
3690
else { return count }
3692
this.parent = tree.Ruleset.prototype;
3695
tree.mixin.Definition.prototype = {
3696
type: "MixinDefinition",
3697
accept: function (visitor) {
3698
this.params = visitor.visit(this.params);
3699
this.rules = visitor.visit(this.rules);
3700
this.condition = visitor.visit(this.condition);
3702
toCSS: function () { return ""; },
3703
variable: function (name) { return this.parent.variable.call(this, name); },
3704
variables: function () { return this.parent.variables.call(this); },
3705
find: function () { return this.parent.find.apply(this, arguments); },
3706
rulesets: function () { return this.parent.rulesets.apply(this); },
3708
evalParams: function (env, mixinEnv, args, evaldArguments) {
3709
var frame = new(tree.Ruleset)(null, []),
3711
params = this.params.slice(0),
3712
i, j, val, name, isNamedFound, argIndex;
3714
mixinEnv = new tree.evalEnv(mixinEnv, [frame].concat(mixinEnv.frames));
3717
args = args.slice(0);
3719
for(i = 0; i < args.length; i++) {
3721
if (name = (arg && arg.name)) {
3722
isNamedFound = false;
3723
for(j = 0; j < params.length; j++) {
3724
if (!evaldArguments[j] && name === params[j].name) {
3725
evaldArguments[j] = arg.value.eval(env);
3726
frame.rules.unshift(new(tree.Rule)(name, arg.value.eval(env)));
3727
isNamedFound = true;
3736
throw { type: 'Runtime', message: "Named argument for " + this.name +
3737
' ' + args[i].name + ' not found' };
3743
for (i = 0; i < params.length; i++) {
3744
if (evaldArguments[i]) continue;
3746
arg = args && args[argIndex];
3748
if (name = params[i].name) {
3749
if (params[i].variadic && args) {
3751
for (j = argIndex; j < args.length; j++) {
3752
varargs.push(args[j].value.eval(env));
3754
frame.rules.unshift(new(tree.Rule)(name, new(tree.Expression)(varargs).eval(env)));
3756
val = arg && arg.value;
3758
val = val.eval(env);
3759
} else if (params[i].value) {
3760
val = params[i].value.eval(mixinEnv);
3763
throw { type: 'Runtime', message: "wrong number of arguments for " + this.name +
3764
' (' + args.length + ' for ' + this.arity + ')' };
3767
frame.rules.unshift(new(tree.Rule)(name, val));
3768
evaldArguments[i] = val;
3772
if (params[i].variadic && args) {
3773
for (j = argIndex; j < args.length; j++) {
3774
evaldArguments[j] = args[j].value.eval(env);
3782
eval: function (env, args, important) {
3783
var _arguments = [],
3784
mixinFrames = this.frames.concat(env.frames),
3785
frame = this.evalParams(env, new(tree.evalEnv)(env, mixinFrames), args, _arguments),
3786
context, rules, start, ruleset;
3788
frame.rules.unshift(new(tree.Rule)('@arguments', new(tree.Expression)(_arguments).eval(env)));
3791
this.parent.makeImportant.apply(this).rules : this.rules.slice(0);
3793
ruleset = new(tree.Ruleset)(null, rules).eval(new(tree.evalEnv)(env,
3794
[this, frame].concat(mixinFrames)));
3795
ruleset.originalRuleset = this;
3798
matchCondition: function (args, env) {
3800
if (this.condition && !this.condition.eval(
3801
new(tree.evalEnv)(env,
3802
[this.evalParams(env, new(tree.evalEnv)(env, this.frames.concat(env.frames)), args, [])]
3803
.concat(env.frames)))) {
3808
matchArgs: function (args, env) {
3809
var argsLength = (args && args.length) || 0, len, frame;
3811
if (! this.variadic) {
3812
if (argsLength < this.required) { return false }
3813
if (argsLength > this.params.length) { return false }
3814
if ((this.required > 0) && (argsLength > this.params.length)) { return false }
3817
len = Math.min(argsLength, this.arity);
3819
for (var i = 0; i < len; i++) {
3820
if (!this.params[i].name && !this.params[i].variadic) {
3821
if (args[i].value.eval(env).toCSS() != this.params[i].value.eval(env).toCSS()) {
3830
})(require('../tree'));
3833
tree.Negative = function (node) {
3836
tree.Negative.prototype = {
3838
accept: function (visitor) {
3839
this.value = visitor.visit(this.value);
3841
toCSS: function (env) {
3842
return '-' + this.value.toCSS(env);
3844
eval: function (env) {
3845
if (env.isMathsOn()) {
3846
return (new(tree.Operation)('*', [new(tree.Dimension)(-1), this.value])).eval(env);
3848
return new(tree.Negative)(this.value.eval(env));
3852
})(require('../tree'));
3855
tree.Operation = function (op, operands, isSpaced) {
3856
this.op = op.trim();
3857
this.operands = operands;
3858
this.isSpaced = isSpaced;
3860
tree.Operation.prototype = {
3862
accept: function (visitor) {
3863
this.operands = visitor.visit(this.operands);
3865
eval: function (env) {
3866
var a = this.operands[0].eval(env),
3867
b = this.operands[1].eval(env),
3870
if (env.isMathsOn()) {
3871
if (a instanceof tree.Dimension && b instanceof tree.Color) {
3872
if (this.op === '*' || this.op === '+') {
3873
temp = b, b = a, a = temp;
3875
throw { type: "Operation",
3876
message: "Can't substract or divide a color from a number" };
3880
throw { type: "Operation",
3881
message: "Operation on an invalid type" };
3884
return a.operate(env, this.op, b);
3886
return new(tree.Operation)(this.op, [a, b], this.isSpaced);
3889
toCSS: function (env) {
3890
var separator = this.isSpaced ? " " : "";
3891
return this.operands[0].toCSS() + separator + this.op + separator + this.operands[1].toCSS();
3895
tree.operate = function (env, op, a, b) {
3897
case '+': return a + b;
3898
case '-': return a - b;
3899
case '*': return a * b;
3900
case '/': return a / b;
3904
})(require('../tree'));
3908
tree.Paren = function (node) {
3911
tree.Paren.prototype = {
3913
accept: function (visitor) {
3914
this.value = visitor.visit(this.value);
3916
toCSS: function (env) {
3917
return '(' + this.value.toCSS(env).trim() + ')';
3919
eval: function (env) {
3920
return new(tree.Paren)(this.value.eval(env));
3924
})(require('../tree'));
3927
tree.Quoted = function (str, content, escaped, index, currentFileInfo) {
3928
this.escaped = escaped;
3929
this.value = content || '';
3930
this.quote = str.charAt(0);
3932
this.currentFileInfo = currentFileInfo;
3934
tree.Quoted.prototype = {
3936
toCSS: function () {
3940
return this.quote + this.value + this.quote;
3943
eval: function (env) {
3945
var value = this.value.replace(/`([^`]+)`/g, function (_, exp) {
3946
return new(tree.JavaScript)(exp, that.index, true).eval(env).value;
3947
}).replace(/@\{([\w-]+)\}/g, function (_, name) {
3948
var v = new(tree.Variable)('@' + name, that.index, that.currentFileInfo).eval(env, true);
3949
return (v instanceof tree.Quoted) ? v.value : v.toCSS();
3951
return new(tree.Quoted)(this.quote + value + this.quote, value, this.escaped, this.index);
3953
compare: function (x) {
3958
var left = this.toCSS(),
3961
if (left === right) {
3965
return left < right ? -1 : 1;
3969
})(require('../tree'));
3972
tree.Rule = function (name, value, important, index, currentFileInfo, inline) {
3974
this.value = (value instanceof tree.Value) ? value : new(tree.Value)([value]);
3975
this.important = important ? ' ' + important.trim() : '';
3977
this.currentFileInfo = currentFileInfo;
3978
this.inline = inline || false;
3980
if (name.charAt(0) === '@') {
3981
this.variable = true;
3982
} else { this.variable = false }
3985
tree.Rule.prototype = {
3987
accept: function (visitor) {
3988
this.value = visitor.visit(this.value);
3990
toCSS: function (env) {
3991
if (this.variable) { return "" }
3994
return this.name + (env.compress ? ':' : ': ') +
3995
this.value.toCSS(env) +
3996
this.important + (this.inline ? "" : ";");
3999
e.index = this.index;
4000
e.filename = this.currentFileInfo.filename;
4005
eval: function (env) {
4006
var strictMathsBypass = false;
4007
if (this.name === "font" && env.strictMaths === false) {
4008
strictMathsBypass = true;
4009
env.strictMaths = true;
4012
return new(tree.Rule)(this.name,
4013
this.value.eval(env),
4015
this.index, this.currentFileInfo, this.inline);
4018
if (strictMathsBypass) {
4019
env.strictMaths = false;
4023
makeImportant: function () {
4024
return new(tree.Rule)(this.name,
4027
this.index, this.currentFileInfo, this.inline);
4031
})(require('../tree'));
4034
tree.Ruleset = function (selectors, rules, strictImports) {
4035
this.selectors = selectors;
4038
this.strictImports = strictImports;
4040
tree.Ruleset.prototype = {
4042
accept: function (visitor) {
4043
this.selectors = visitor.visit(this.selectors);
4044
this.rules = visitor.visit(this.rules);
4046
eval: function (env) {
4047
var selectors = this.selectors && this.selectors.map(function (s) { return s.eval(env) });
4048
var ruleset = new(tree.Ruleset)(selectors, this.rules.slice(0), this.strictImports);
4051
ruleset.originalRuleset = this;
4052
ruleset.root = this.root;
4053
ruleset.firstRoot = this.firstRoot;
4054
ruleset.allowImports = this.allowImports;
4056
if(this.debugInfo) {
4057
ruleset.debugInfo = this.debugInfo;
4060
// push the current ruleset to the frames stack
4061
env.frames.unshift(ruleset);
4063
// currrent selectors
4064
if (!env.selectors) {
4067
env.selectors.unshift(this.selectors);
4070
if (ruleset.root || ruleset.allowImports || !ruleset.strictImports) {
4071
ruleset.evalImports(env);
4074
// Store the frames around mixin definitions,
4075
// so they can be evaluated like closures when the time comes.
4076
for (var i = 0; i < ruleset.rules.length; i++) {
4077
if (ruleset.rules[i] instanceof tree.mixin.Definition) {
4078
ruleset.rules[i].frames = env.frames.slice(0);
4082
var mediaBlockCount = (env.mediaBlocks && env.mediaBlocks.length) || 0;
4084
// Evaluate mixin calls.
4085
for (var i = 0; i < ruleset.rules.length; i++) {
4086
if (ruleset.rules[i] instanceof tree.mixin.Call) {
4087
rules = ruleset.rules[i].eval(env).filter(function(r) {
4088
if ((r instanceof tree.Rule) && r.variable) {
4089
// do not pollute the scope if the variable is
4090
// already there. consider returning false here
4091
// but we need a way to "return" variable from mixins
4092
return !(ruleset.variable(r.name));
4096
ruleset.rules.splice.apply(ruleset.rules, [i, 1].concat(rules));
4097
i += rules.length-1;
4098
ruleset.resetCache();
4102
// Evaluate everything else
4103
for (var i = 0, rule; i < ruleset.rules.length; i++) {
4104
rule = ruleset.rules[i];
4106
if (! (rule instanceof tree.mixin.Definition)) {
4107
ruleset.rules[i] = rule.eval ? rule.eval(env) : rule;
4113
env.selectors.shift();
4115
if (env.mediaBlocks) {
4116
for(var i = mediaBlockCount; i < env.mediaBlocks.length; i++) {
4117
env.mediaBlocks[i].bubbleSelectors(selectors);
4123
evalImports: function(env) {
4125
for (i = 0; i < this.rules.length; i++) {
4126
if (this.rules[i] instanceof tree.Import) {
4127
rules = this.rules[i].eval(env);
4128
if (typeof rules.length === "number") {
4129
this.rules.splice.apply(this.rules, [i, 1].concat(rules));
4132
this.rules.splice(i, 1, rules);
4138
makeImportant: function() {
4139
return new tree.Ruleset(this.selectors, this.rules.map(function (r) {
4140
if (r.makeImportant) {
4141
return r.makeImportant();
4145
}), this.strictImports);
4147
matchArgs: function (args) {
4148
return !args || args.length === 0;
4150
resetCache: function () {
4151
this._rulesets = null;
4152
this._variables = null;
4155
variables: function () {
4156
if (this._variables) { return this._variables }
4158
return this._variables = this.rules.reduce(function (hash, r) {
4159
if (r instanceof tree.Rule && r.variable === true) {
4166
variable: function (name) {
4167
return this.variables()[name];
4169
rulesets: function () {
4170
return this.rules.filter(function (r) {
4171
return (r instanceof tree.Ruleset) || (r instanceof tree.mixin.Definition);
4174
find: function (selector, self) {
4175
self = self || this;
4176
var rules = [], rule, match,
4177
key = selector.toCSS();
4179
if (key in this._lookups) { return this._lookups[key] }
4181
this.rulesets().forEach(function (rule) {
4182
if (rule !== self) {
4183
for (var j = 0; j < rule.selectors.length; j++) {
4184
if (match = selector.match(rule.selectors[j])) {
4185
if (selector.elements.length > rule.selectors[j].elements.length) {
4186
Array.prototype.push.apply(rules, rule.find(
4187
new(tree.Selector)(selector.elements.slice(1)), self));
4196
return this._lookups[key] = rules;
4199
// Entry point for code generation
4201
// `context` holds an array of arrays.
4203
toCSS: function (env) {
4204
var css = [], // The CSS output
4205
rules = [], // node.Rule instances
4207
rulesets = [], // node.Ruleset instances
4208
selector, // The fully rendered selector
4209
debugInfo, // Line number debugging
4212
// Compile rules and rulesets
4213
for (var i = 0; i < this.rules.length; i++) {
4214
rule = this.rules[i];
4216
if (rule.rules || (rule instanceof tree.Media)) {
4217
rulesets.push(rule.toCSS(env));
4218
} else if (rule instanceof tree.Directive) {
4219
var cssValue = rule.toCSS(env);
4220
// Output only the first @charset definition as such - convert the others
4221
// to comments in case debug is enabled
4222
if (rule.name === "@charset") {
4223
// Only output the debug info together with subsequent @charset definitions
4224
// a comment (or @media statement) before the actual @charset directive would
4225
// be considered illegal css as it has to be on the first line
4227
if (rule.debugInfo) {
4228
rulesets.push(tree.debugInfo(env, rule));
4229
rulesets.push(new tree.Comment("/* "+cssValue.replace(/\n/g, "")+" */\n").toCSS(env));
4235
rulesets.push(cssValue);
4236
} else if (rule instanceof tree.Comment) {
4239
rulesets.push(rule.toCSS(env));
4241
rules.push(rule.toCSS(env));
4245
if (rule.toCSS && !rule.variable) {
4246
if (this.firstRoot && rule instanceof tree.Rule) {
4247
throw { message: "properties must be inside selector blocks, they cannot be in the root.",
4248
index: rule.index, filename: rule.currentFileInfo ? rule.currentFileInfo.filename : null};
4250
rules.push(rule.toCSS(env));
4251
} else if (rule.value && !rule.variable) {
4252
rules.push(rule.value.toString());
4257
// Remove last semicolon
4258
if (env.compress && rules.length) {
4259
rule = rules[rules.length - 1];
4260
if (rule.charAt(rule.length - 1) === ';') {
4261
rules[rules.length - 1] = rule.substring(0, rule.length - 1);
4265
rulesets = rulesets.join('');
4267
// If this is the root node, we don't render
4268
// a selector, or {}.
4269
// Otherwise, only output if this ruleset has rules.
4271
css.push(rules.join(env.compress ? '' : '\n'));
4273
if (rules.length > 0) {
4274
debugInfo = tree.debugInfo(env, this);
4275
selector = this.paths.map(function (p) {
4276
return p.map(function (s) {
4277
return s.toCSS(env);
4279
}).join(env.compress ? ',' : ',\n');
4281
// Remove duplicates
4282
for (var i = rules.length - 1; i >= 0; i--) {
4283
if (rules[i].slice(0, 2) === "/*" || _rules.indexOf(rules[i]) === -1) {
4284
_rules.unshift(rules[i]);
4289
css.push(debugInfo + selector +
4290
(env.compress ? '{' : ' {\n ') +
4291
rules.join(env.compress ? '' : '\n ') +
4292
(env.compress ? '}' : '\n}\n'));
4297
return css.join('') + (env.compress ? '\n' : '');
4300
joinSelectors: function (paths, context, selectors) {
4301
for (var s = 0; s < selectors.length; s++) {
4302
this.joinSelector(paths, context, selectors[s]);
4306
joinSelector: function (paths, context, selector) {
4309
hasParentSelector, newSelectors, el, sel, parentSel,
4310
newSelectorPath, afterParentJoin, newJoinedSelector,
4311
newJoinedSelectorEmpty, lastSelector, currentElements,
4312
selectorsMultiplied;
4314
for (i = 0; i < selector.elements.length; i++) {
4315
el = selector.elements[i];
4316
if (el.value === '&') {
4317
hasParentSelector = true;
4321
if (!hasParentSelector) {
4322
if (context.length > 0) {
4323
for(i = 0; i < context.length; i++) {
4324
paths.push(context[i].concat(selector));
4328
paths.push([selector]);
4333
// The paths are [[Selector]]
4334
// The first list is a list of comma seperated selectors
4335
// The inner list is a list of inheritance seperated selectors
4341
// == [[.a] [.c]] [[.b] [.c]]
4344
// the elements from the current selector so far
4345
currentElements = [];
4346
// the current list of new selectors to add to the path.
4347
// We will build it up. We initiate it with one empty selector as we "multiply" the new selectors
4349
newSelectors = [[]];
4351
for (i = 0; i < selector.elements.length; i++) {
4352
el = selector.elements[i];
4353
// non parent reference elements just get added
4354
if (el.value !== "&") {
4355
currentElements.push(el);
4357
// the new list of selectors to add
4358
selectorsMultiplied = [];
4360
// merge the current list of non parent selector elements
4361
// on to the current list of selectors to add
4362
if (currentElements.length > 0) {
4363
this.mergeElementsOnToSelectors(currentElements, newSelectors);
4366
// loop through our current selectors
4367
for(j = 0; j < newSelectors.length; j++) {
4368
sel = newSelectors[j];
4369
// if we don't have any parent paths, the & might be in a mixin so that it can be used
4370
// whether there are parents or not
4371
if (context.length == 0) {
4372
// the combinator used on el should now be applied to the next element instead so that
4374
if (sel.length > 0) {
4375
sel[0].elements = sel[0].elements.slice(0);
4376
sel[0].elements.push(new(tree.Element)(el.combinator, '', 0)); //new Element(el.Combinator, ""));
4378
selectorsMultiplied.push(sel);
4381
// and the parent selectors
4382
for(k = 0; k < context.length; k++) {
4383
parentSel = context[k];
4384
// We need to put the current selectors
4385
// then join the last selector's elements on to the parents selectors
4387
// our new selector path
4388
newSelectorPath = [];
4389
// selectors from the parent after the join
4390
afterParentJoin = [];
4391
newJoinedSelectorEmpty = true;
4393
//construct the joined selector - if & is the first thing this will be empty,
4394
// if not newJoinedSelector will be the last set of elements in the selector
4395
if (sel.length > 0) {
4396
newSelectorPath = sel.slice(0);
4397
lastSelector = newSelectorPath.pop();
4398
newJoinedSelector = new(tree.Selector)(lastSelector.elements.slice(0), selector.extendList);
4399
newJoinedSelectorEmpty = false;
4402
newJoinedSelector = new(tree.Selector)([], selector.extendList);
4405
//put together the parent selectors after the join
4406
if (parentSel.length > 1) {
4407
afterParentJoin = afterParentJoin.concat(parentSel.slice(1));
4410
if (parentSel.length > 0) {
4411
newJoinedSelectorEmpty = false;
4413
// join the elements so far with the first part of the parent
4414
newJoinedSelector.elements.push(new(tree.Element)(el.combinator, parentSel[0].elements[0].value, 0));
4415
newJoinedSelector.elements = newJoinedSelector.elements.concat(parentSel[0].elements.slice(1));
4418
if (!newJoinedSelectorEmpty) {
4419
// now add the joined selector
4420
newSelectorPath.push(newJoinedSelector);
4423
// and the rest of the parent
4424
newSelectorPath = newSelectorPath.concat(afterParentJoin);
4426
// add that to our new set of selectors
4427
selectorsMultiplied.push(newSelectorPath);
4432
// our new selectors has been multiplied, so reset the state
4433
newSelectors = selectorsMultiplied;
4434
currentElements = [];
4438
// if we have any elements left over (e.g. .a& .b == .b)
4439
// add them on to all the current selectors
4440
if (currentElements.length > 0) {
4441
this.mergeElementsOnToSelectors(currentElements, newSelectors);
4444
for(i = 0; i < newSelectors.length; i++) {
4445
if (newSelectors[i].length > 0) {
4446
paths.push(newSelectors[i]);
4451
mergeElementsOnToSelectors: function(elements, selectors) {
4452
var i, sel, extendList;
4454
if (selectors.length == 0) {
4455
selectors.push([ new(tree.Selector)(elements) ]);
4459
for(i = 0; i < selectors.length; i++) {
4462
// if the previous thing in sel is a parent this needs to join on to it
4463
if (sel.length > 0) {
4464
sel[sel.length - 1] = new(tree.Selector)(sel[sel.length - 1].elements.concat(elements), sel[sel.length - 1].extendList);
4467
sel.push(new(tree.Selector)(elements));
4472
})(require('../tree'));
4475
tree.Selector = function (elements, extendList) {
4476
this.elements = elements;
4477
this.extendList = extendList || [];
4479
tree.Selector.prototype = {
4481
accept: function (visitor) {
4482
this.elements = visitor.visit(this.elements);
4483
this.extendList = visitor.visit(this.extendList)
4485
match: function (other) {
4486
var elements = this.elements,
4487
len = elements.length,
4488
oelements, olen, max, i;
4490
oelements = other.elements.slice(
4491
(other.elements.length && other.elements[0].value === "&") ? 1 : 0);
4492
olen = oelements.length;
4493
max = Math.min(len, olen);
4495
if (olen === 0 || len < olen) {
4498
for (i = 0; i < max; i++) {
4499
if (elements[i].value !== oelements[i].value) {
4506
eval: function (env) {
4507
return new(tree.Selector)(this.elements.map(function (e) {
4509
}), this.extendList.map(function(extend) {
4510
return extend.eval(env);
4513
toCSS: function (env) {
4514
if (this._css) { return this._css }
4516
if (this.elements[0].combinator.value === "") {
4522
this._css += this.elements.map(function (e) {
4523
if (typeof(e) === 'string') {
4524
return ' ' + e.trim();
4526
return e.toCSS(env);
4534
})(require('../tree'));
4537
tree.UnicodeDescriptor = function (value) {
4540
tree.UnicodeDescriptor.prototype = {
4541
type: "UnicodeDescriptor",
4542
toCSS: function (env) {
4545
eval: function () { return this }
4548
})(require('../tree'));
4551
tree.URL = function (val, currentFileInfo) {
4553
this.currentFileInfo = currentFileInfo;
4555
tree.URL.prototype = {
4557
accept: function (visitor) {
4558
this.value = visitor.visit(this.value);
4560
toCSS: function () {
4561
return "url(" + this.value.toCSS() + ")";
4563
eval: function (ctx) {
4564
var val = this.value.eval(ctx), rootpath;
4566
// Add the base path if the URL is relative
4567
rootpath = this.currentFileInfo && this.currentFileInfo.rootpath;
4568
if (rootpath && typeof val.value === "string" && ctx.isPathRelative(val.value)) {
4570
rootpath = rootpath.replace(/[\(\)'"\s]/g, function(match) { return "\\"+match; });
4572
val.value = rootpath + val.value;
4575
return new(tree.URL)(val, null);
4579
})(require('../tree'));
4582
tree.Value = function (value) {
4585
tree.Value.prototype = {
4587
accept: function (visitor) {
4588
this.value = visitor.visit(this.value);
4590
eval: function (env) {
4591
if (this.value.length === 1) {
4592
return this.value[0].eval(env);
4594
return new(tree.Value)(this.value.map(function (v) {
4599
toCSS: function (env) {
4600
return this.value.map(function (e) {
4601
return e.toCSS(env);
4602
}).join(env.compress ? ',' : ', ');
4606
})(require('../tree'));
4609
tree.Variable = function (name, index, currentFileInfo) { this.name = name, this.index = index, this.currentFileInfo = currentFileInfo };
4610
tree.Variable.prototype = {
4612
eval: function (env) {
4613
var variable, v, name = this.name;
4615
if (name.indexOf('@@') == 0) {
4616
name = '@' + new(tree.Variable)(name.slice(1)).eval(env).value;
4619
if (this.evaluating) {
4620
throw { type: 'Name',
4621
message: "Recursive variable definition for " + name,
4622
filename: this.currentFileInfo.file,
4623
index: this.index };
4626
this.evaluating = true;
4628
if (variable = tree.find(env.frames, function (frame) {
4629
if (v = frame.variable(name)) {
4630
return v.value.eval(env);
4633
this.evaluating = false;
4637
throw { type: 'Name',
4638
message: "variable " + name + " is undefined",
4639
filename: this.currentFileInfo.filename,
4640
index: this.index };
4645
})(require('../tree'));
4648
tree.debugInfo = function(env, ctx) {
4650
if (env.dumpLineNumbers && !env.compress) {
4651
switch(env.dumpLineNumbers) {
4653
result = tree.debugInfo.asComment(ctx);
4656
result = tree.debugInfo.asMediaQuery(ctx);
4659
result = tree.debugInfo.asComment(ctx)+tree.debugInfo.asMediaQuery(ctx);
4666
tree.debugInfo.asComment = function(ctx) {
4667
return '/* line ' + ctx.debugInfo.lineNumber + ', ' + ctx.debugInfo.fileName + ' */\n';
4670
tree.debugInfo.asMediaQuery = function(ctx) {
4671
return '@media -sass-debug-info{filename{font-family:' +
4672
('file://' + ctx.debugInfo.fileName).replace(/([.:/\\])/g, function(a){if(a=='\\') a = '\/'; return '\\' + a}) +
4673
'}line{font-family:\\00003' + ctx.debugInfo.lineNumber + '}}\n';
4676
tree.find = function (obj, fun) {
4677
for (var i = 0, r; i < obj.length; i++) {
4678
if (r = fun.call(obj, obj[i])) { return r }
4682
tree.jsify = function (obj) {
4683
if (Array.isArray(obj.value) && (obj.value.length > 1)) {
4684
return '[' + obj.value.map(function (v) { return v.toCSS(false) }).join(', ') + ']';
4686
return obj.toCSS(false);
4690
})(require('./tree'));
4693
var parseCopyProperties = [
4694
'paths', // option - unmodified - paths to search for imports on
4695
'optimization', // option - optimization level (for the chunker)
4696
'files', // list of files that have been imported, used for import-once
4697
'contents', // browser-only, contents of all the files
4698
'relativeUrls', // option - whether to adjust URL's to be relative
4699
'strictImports', // option -
4700
'dumpLineNumbers', // option - whether to dump line numbers
4701
'compress', // option - whether to compress
4702
'processImports', // option - whether to process imports. if false then imports will not be imported
4703
'mime', // browser only - mime type for sheet import
4704
'currentFileInfo' // information about the current file - for error reporting and importing and making urls relative etc.
4707
//currentFileInfo = {
4708
// 'relativeUrls' - option - whether to adjust URL's to be relative
4709
// 'filename' - full resolved filename of current file
4710
// 'rootpath' - path to append to normal URLs for this node
4711
// 'currentDirectory' - path to the current file, absolute
4712
// 'rootFilename' - filename of the base file
4713
// 'entryPath' = absolute path to the entry file
4715
tree.parseEnv = function(options) {
4716
copyFromOriginal(options, this, parseCopyProperties);
4718
if (!this.contents) { this.contents = {}; }
4719
if (!this.files) { this.files = {}; }
4721
if (!this.currentFileInfo) {
4722
var filename = options.filename || "input";
4723
options.filename = null;
4724
var entryPath = filename.replace(/[^\/\\]*$/, "");
4725
this.currentFileInfo = {
4727
relativeUrls: this.relativeUrls,
4728
rootpath: options.rootpath || "",
4729
currentDirectory: entryPath,
4730
entryPath: entryPath,
4731
rootFilename: filename
4736
tree.parseEnv.prototype.toSheet = function (path) {
4737
var env = new tree.parseEnv(this);
4740
env.type = this.mime;
4744
var evalCopyProperties = [
4745
'silent', // whether to swallow errors and warnings
4746
'verbose', // whether to log more activity
4747
'compress', // whether to compress
4748
'ieCompat', // whether to enforce IE compatibility (IE8 data-uri)
4749
'strictMaths', // whether maths has to be within parenthesis
4750
'strictUnits' // whether units need to evaluate correctly
4753
tree.evalEnv = function(options, frames) {
4754
copyFromOriginal(options, this, evalCopyProperties);
4756
this.frames = frames || [];
4759
tree.evalEnv.prototype.inParenthesis = function () {
4760
if (!this.parensStack) {
4761
this.parensStack = [];
4763
this.parensStack.push(true);
4766
tree.evalEnv.prototype.outOfParenthesis = function () {
4767
this.parensStack.pop();
4770
tree.evalEnv.prototype.isMathsOn = function () {
4771
return this.strictMaths === false ? true : (this.parensStack && this.parensStack.length);
4774
tree.evalEnv.prototype.isPathRelative = function (path) {
4775
return !/^(?:[a-z-]+:|\/)/.test(path);
4778
//todo - do the same for the toCSS env
4779
//tree.toCSSEnv = function (options) {
4782
var copyFromOriginal = function(original, destination, propertiesToCopy) {
4783
if (!original) { return; }
4785
for(var i = 0; i < propertiesToCopy.length; i++) {
4786
if (original.hasOwnProperty(propertiesToCopy[i])) {
4787
destination[propertiesToCopy[i]] = original[propertiesToCopy[i]];
4791
})(require('./tree'));(function (tree) {
4793
tree.visitor = function(implementation) {
4794
this._implementation = implementation;
4797
tree.visitor.prototype = {
4798
visit: function(node) {
4800
if (node instanceof Array) {
4801
return this.visitArray(node);
4804
if (!node || !node.type) {
4808
var funcName = "visit" + node.type,
4809
func = this._implementation[funcName],
4812
visitArgs = {visitDeeper: true};
4813
newNode = func.call(this._implementation, node, visitArgs);
4814
if (this._implementation.isReplacing) {
4818
if ((!visitArgs || visitArgs.visitDeeper) && node && node.accept) {
4821
funcName = funcName + "Out";
4822
if (this._implementation[funcName]) {
4823
this._implementation[funcName](node);
4827
visitArray: function(nodes) {
4828
var i, newNodes = [];
4829
for(i = 0; i < nodes.length; i++) {
4830
var evald = this.visit(nodes[i]);
4831
if (evald instanceof Array) {
4832
newNodes = newNodes.concat(evald);
4834
newNodes.push(evald);
4837
if (this._implementation.isReplacing) {
4844
})(require('./tree'));(function (tree) {
4845
tree.importVisitor = function(importer, finish, evalEnv) {
4846
this._visitor = new tree.visitor(this);
4847
this._importer = importer;
4848
this._finish = finish;
4849
this.env = evalEnv || new tree.evalEnv();
4850
this.importCount = 0;
4853
tree.importVisitor.prototype = {
4855
run: function (root) {
4858
// process the contents
4859
this._visitor.visit(root);
4865
this.isFinished = true;
4867
if (this.importCount === 0) {
4868
this._finish(error);
4871
visitImport: function (importNode, visitArgs) {
4872
var importVisitor = this,
4875
if (!importNode.css) {
4878
evaldImportNode = importNode.evalForImport(this.env);
4880
if (!e.filename) { e.index = importNode.index; e.filename = importNode.currentFileInfo.filename; }
4881
// attempt to eval properly and treat as css
4882
importNode.css = true;
4883
// if that fails, this error will be thrown
4884
importNode.error = e;
4887
if (evaldImportNode && !evaldImportNode.css) {
4888
importNode = evaldImportNode;
4890
var env = new tree.evalEnv(this.env, this.env.frames.slice(0));
4891
this._importer.push(importNode.getPath(), importNode.currentFileInfo, function (e, root, imported) {
4892
if (e && !e.filename) { e.index = importNode.index; e.filename = importNode.currentFileInfo.filename; }
4893
if (imported && !importNode.options.multiple) { importNode.skip = imported; }
4895
var subFinish = function(e) {
4896
importVisitor.importCount--;
4898
if (importVisitor.importCount === 0 && importVisitor.isFinished) {
4899
importVisitor._finish(e);
4904
importNode.root = root;
4905
new(tree.importVisitor)(importVisitor._importer, subFinish, env)
4913
visitArgs.visitDeeper = false;
4916
visitRule: function (ruleNode, visitArgs) {
4917
visitArgs.visitDeeper = false;
4920
visitDirective: function (directiveNode, visitArgs) {
4921
this.env.frames.unshift(directiveNode);
4922
return directiveNode;
4924
visitDirectiveOut: function (directiveNode) {
4925
this.env.frames.shift();
4927
visitMixinDefinition: function (mixinDefinitionNode, visitArgs) {
4928
this.env.frames.unshift(mixinDefinitionNode);
4929
return mixinDefinitionNode;
4931
visitMixinDefinitionOut: function (mixinDefinitionNode) {
4932
this.env.frames.shift();
4934
visitRuleset: function (rulesetNode, visitArgs) {
4935
this.env.frames.unshift(rulesetNode);
4938
visitRulesetOut: function (rulesetNode) {
4939
this.env.frames.shift();
4941
visitMedia: function (mediaNode, visitArgs) {
4942
this.env.frames.unshift(mediaNode.ruleset);
4945
visitMediaOut: function (mediaNode) {
4946
this.env.frames.shift();
4950
})(require('./tree'));(function (tree) {
4951
tree.joinSelectorVisitor = function() {
4952
this.contexts = [[]];
4953
this._visitor = new tree.visitor(this);
4956
tree.joinSelectorVisitor.prototype = {
4957
run: function (root) {
4958
return this._visitor.visit(root);
4960
visitRule: function (ruleNode, visitArgs) {
4961
visitArgs.visitDeeper = false;
4963
visitMixinDefinition: function (mixinDefinitionNode, visitArgs) {
4964
visitArgs.visitDeeper = false;
4967
visitRuleset: function (rulesetNode, visitArgs) {
4968
var context = this.contexts[this.contexts.length - 1];
4970
this.contexts.push(paths);
4972
if (! rulesetNode.root) {
4973
rulesetNode.joinSelectors(paths, context, rulesetNode.selectors);
4974
rulesetNode.paths = paths;
4977
visitRulesetOut: function (rulesetNode) {
4978
this.contexts.length = this.contexts.length - 1;
4980
visitMedia: function (mediaNode, visitArgs) {
4981
var context = this.contexts[this.contexts.length - 1];
4982
mediaNode.ruleset.root = (context.length === 0 || context[0].multiMedia);
4986
})(require('./tree'));(function (tree) {
4987
tree.extendFinderVisitor = function() {
4988
this._visitor = new tree.visitor(this);
4990
this.allExtendsStack = [[]];
4993
tree.extendFinderVisitor.prototype = {
4994
run: function (root) {
4995
root = this._visitor.visit(root);
4996
root.allExtends = this.allExtendsStack[0];
4999
visitRule: function (ruleNode, visitArgs) {
5000
visitArgs.visitDeeper = false;
5002
visitMixinDefinition: function (mixinDefinitionNode, visitArgs) {
5003
visitArgs.visitDeeper = false;
5005
visitRuleset: function (rulesetNode, visitArgs) {
5007
if (rulesetNode.root) {
5011
var i, j, extend, allSelectorsExtendList = [], extendList;
5013
// get &:extend(.a); rules which apply to all selectors in this ruleset
5014
for(i = 0; i < rulesetNode.rules.length; i++) {
5015
if (rulesetNode.rules[i] instanceof tree.Extend) {
5016
allSelectorsExtendList.push(rulesetNode.rules[i]);
5020
// now find every selector and apply the extends that apply to all extends
5021
// and the ones which apply to an individual extend
5022
for(i = 0; i < rulesetNode.paths.length; i++) {
5023
var selectorPath = rulesetNode.paths[i],
5024
selector = selectorPath[selectorPath.length-1];
5025
extendList = selector.extendList.slice(0).concat(allSelectorsExtendList).map(function(allSelectorsExtend) {
5026
return allSelectorsExtend.clone();
5028
for(j = 0; j < extendList.length; j++) {
5029
this.foundExtends = true;
5030
extend = extendList[j];
5031
extend.findSelfSelectors(selectorPath);
5032
extend.ruleset = rulesetNode;
5033
if (j === 0) { extend.firstExtendOnThisSelectorPath = true; }
5034
this.allExtendsStack[this.allExtendsStack.length-1].push(extend);
5038
this.contexts.push(rulesetNode.selectors);
5040
visitRulesetOut: function (rulesetNode) {
5041
if (!rulesetNode.root) {
5042
this.contexts.length = this.contexts.length - 1;
5045
visitMedia: function (mediaNode, visitArgs) {
5046
mediaNode.allExtends = [];
5047
this.allExtendsStack.push(mediaNode.allExtends);
5049
visitMediaOut: function (mediaNode) {
5050
this.allExtendsStack.length = this.allExtendsStack.length - 1;
5052
visitDirective: function (directiveNode, visitArgs) {
5053
directiveNode.allExtends = [];
5054
this.allExtendsStack.push(directiveNode.allExtends);
5056
visitDirectiveOut: function (directiveNode) {
5057
this.allExtendsStack.length = this.allExtendsStack.length - 1;
5061
tree.processExtendsVisitor = function() {
5062
this._visitor = new tree.visitor(this);
5065
tree.processExtendsVisitor.prototype = {
5066
run: function(root) {
5067
var extendFinder = new tree.extendFinderVisitor();
5068
extendFinder.run(root);
5069
if (!extendFinder.foundExtends) { return root; }
5070
root.allExtends = root.allExtends.concat(this.doExtendChaining(root.allExtends, root.allExtends));
5071
this.allExtendsStack = [root.allExtends];
5072
return this._visitor.visit(root);
5074
doExtendChaining: function (extendsList, extendsListTarget, iterationCount) {
5076
// chaining is different from normal extension.. if we extend an extend then we are not just copying, altering and pasting
5077
// the selector we would do normally, but we are also adding an extend with the same target selector
5078
// this means this new extend can then go and alter other extends
5080
// this method deals with all the chaining work - without it, extend is flat and doesn't work on other extend selectors
5081
// this is also the most expensive.. and a match on one selector can cause an extension of a selector we had already processed if
5082
// we look at each selector at a time, as is done in visitRuleset
5084
var extendIndex, targetExtendIndex, matches, extendsToAdd = [], newSelector, extendVisitor = this, selectorPath, extend, targetExtend;
5086
iterationCount = iterationCount || 0;
5088
//loop through comparing every extend with every target extend.
5089
// a target extend is the one on the ruleset we are looking at copy/edit/pasting in place
5090
// e.g. .a:extend(.b) {} and .b:extend(.c) {} then the first extend extends the second one
5091
// and the second is the target.
5092
// the seperation into two lists allows us to process a subset of chains with a bigger set, as is the
5093
// case when processing media queries
5094
for(extendIndex = 0; extendIndex < extendsList.length; extendIndex++){
5095
for(targetExtendIndex = 0; targetExtendIndex < extendsListTarget.length; targetExtendIndex++){
5097
extend = extendsList[extendIndex];
5098
targetExtend = extendsListTarget[targetExtendIndex];
5100
// look for circular references
5101
if (this.inInheritanceChain(targetExtend, extend)) { continue; }
5103
// find a match in the target extends self selector (the bit before :extend)
5104
selectorPath = [targetExtend.selfSelectors[0]];
5105
matches = extendVisitor.findMatch(extend, selectorPath);
5107
if (matches.length) {
5109
// we found a match, so for each self selector..
5110
extend.selfSelectors.forEach(function(selfSelector) {
5112
// process the extend as usual
5113
newSelector = extendVisitor.extendSelector(matches, selectorPath, selfSelector);
5115
// but now we create a new extend from it
5116
newExtend = new(tree.Extend)(targetExtend.selector, targetExtend.option, 0);
5117
newExtend.selfSelectors = newSelector;
5119
// add the extend onto the list of extends for that selector
5120
newSelector[newSelector.length-1].extendList = [newExtend];
5122
// record that we need to add it.
5123
extendsToAdd.push(newExtend);
5124
newExtend.ruleset = targetExtend.ruleset;
5126
//remember its parents for circular references
5127
newExtend.parents = [targetExtend, extend];
5129
// only process the selector once.. if we have :extend(.a,.b) then multiple
5130
// extends will look at the same selector path, so when extending
5131
// we know that any others will be duplicates in terms of what is added to the css
5132
if (targetExtend.firstExtendOnThisSelectorPath) {
5133
newExtend.firstExtendOnThisSelectorPath = true;
5134
targetExtend.ruleset.paths.push(newSelector);
5141
if (extendsToAdd.length) {
5142
// try to detect circular references to stop a stack overflow.
5143
// may no longer be needed.
5144
this.extendChainCount++;
5145
if (iterationCount > 100) {
5146
var selectorOne = "{unable to calculate}";
5147
var selectorTwo = "{unable to calculate}";
5150
selectorOne = extendsToAdd[0].selfSelectors[0].toCSS();
5151
selectorTwo = extendsToAdd[0].selector.toCSS();
5154
throw {message: "extend circular reference detected. One of the circular extends is currently:"+selectorOne+":extend(" + selectorTwo+")"};
5157
// now process the new extends on the existing rules so that we can handle a extending b extending c ectending d extending e...
5158
return extendsToAdd.concat(extendVisitor.doExtendChaining(extendsToAdd, extendsListTarget, iterationCount+1));
5160
return extendsToAdd;
5163
inInheritanceChain: function (possibleParent, possibleChild) {
5164
if (possibleParent === possibleChild) {
5167
if (possibleChild.parents) {
5168
if (this.inInheritanceChain(possibleParent, possibleChild.parents[0])) {
5171
if (this.inInheritanceChain(possibleParent, possibleChild.parents[1])) {
5177
visitRule: function (ruleNode, visitArgs) {
5178
visitArgs.visitDeeper = false;
5180
visitMixinDefinition: function (mixinDefinitionNode, visitArgs) {
5181
visitArgs.visitDeeper = false;
5183
visitSelector: function (selectorNode, visitArgs) {
5184
visitArgs.visitDeeper = false;
5186
visitRuleset: function (rulesetNode, visitArgs) {
5187
if (rulesetNode.root) {
5190
var matches, pathIndex, extendIndex, allExtends = this.allExtendsStack[this.allExtendsStack.length-1], selectorsToAdd = [], extendVisitor = this;
5192
// look at each selector path in the ruleset, find any extend matches and then copy, find and replace
5194
for(extendIndex = 0; extendIndex < allExtends.length; extendIndex++) {
5195
for(pathIndex = 0; pathIndex < rulesetNode.paths.length; pathIndex++) {
5197
selectorPath = rulesetNode.paths[pathIndex];
5199
// extending extends happens initially, before the main pass
5200
if (selectorPath[selectorPath.length-1].extendList.length) { continue; }
5202
matches = this.findMatch(allExtends[extendIndex], selectorPath);
5204
if (matches.length) {
5206
allExtends[extendIndex].selfSelectors.forEach(function(selfSelector) {
5207
selectorsToAdd.push(extendVisitor.extendSelector(matches, selectorPath, selfSelector));
5212
rulesetNode.paths = rulesetNode.paths.concat(selectorsToAdd);
5214
findMatch: function (extend, haystackSelectorPath) {
5216
// look through the haystack selector path to try and find the needle - extend.selector
5217
// returns an array of selector matches that can then be replaced
5219
var haystackSelectorIndex, hackstackSelector, hackstackElementIndex, haystackElement,
5220
targetCombinator, i,
5221
needleElements = extend.selector.elements,
5222
potentialMatches = [], potentialMatch, matches = [];
5224
// loop through the haystack elements
5225
for(haystackSelectorIndex = 0; haystackSelectorIndex < haystackSelectorPath.length; haystackSelectorIndex++) {
5226
hackstackSelector = haystackSelectorPath[haystackSelectorIndex];
5228
for(hackstackElementIndex = 0; hackstackElementIndex < hackstackSelector.elements.length; hackstackElementIndex++) {
5230
haystackElement = hackstackSelector.elements[hackstackElementIndex];
5232
// if we allow elements before our match we can add a potential match every time. otherwise only at the first element.
5233
if (extend.allowBefore || (haystackSelectorIndex == 0 && hackstackElementIndex == 0)) {
5234
potentialMatches.push({pathIndex: haystackSelectorIndex, index: hackstackElementIndex, matched: 0, initialCombinator: haystackElement.combinator});
5237
for(i = 0; i < potentialMatches.length; i++) {
5238
potentialMatch = potentialMatches[i];
5240
// selectors add " " onto the first element. When we use & it joins the selectors together, but if we don't
5241
// then each selector in haystackSelectorPath has a space before it added in the toCSS phase. so we need to work out
5242
// what the resulting combinator will be
5243
targetCombinator = haystackElement.combinator.value;
5244
if (targetCombinator == '' && hackstackElementIndex === 0) {
5245
targetCombinator = ' ';
5248
// if we don't match, null our match to indicate failure
5249
if (needleElements[potentialMatch.matched].value !== haystackElement.value ||
5250
(potentialMatch.matched > 0 && needleElements[potentialMatch.matched].combinator.value !== targetCombinator)) {
5251
potentialMatch = null;
5253
potentialMatch.matched++;
5256
// if we are still valid and have finished, test whether we have elements after and whether these are allowed
5257
if (potentialMatch) {
5258
potentialMatch.finished = potentialMatch.matched === needleElements.length;
5259
if (potentialMatch.finished &&
5260
(!extend.allowAfter && (hackstackElementIndex+1 < hackstackSelector.elements.length || haystackSelectorIndex+1 < haystackSelectorPath.length))) {
5261
potentialMatch = null;
5264
// if null we remove, if not, we are still valid, so either push as a valid match or continue
5265
if (potentialMatch) {
5266
if (potentialMatch.finished) {
5267
potentialMatch.length = needleElements.length;
5268
potentialMatch.endPathIndex = haystackSelectorIndex;
5269
potentialMatch.endPathElementIndex = hackstackElementIndex + 1; // index after end of match
5270
potentialMatches.length = 0; // we don't allow matches to overlap, so start matching again
5271
matches.push(potentialMatch);
5274
potentialMatches.splice(i, 1);
5282
extendSelector:function (matches, selectorPath, replacementSelector) {
5284
//for a set of matches, replace each match with the replacement selector
5286
var currentSelectorPathIndex = 0,
5287
currentSelectorPathElementIndex = 0,
5293
for (matchIndex = 0; matchIndex < matches.length; matchIndex++) {
5294
match = matches[matchIndex];
5295
selector = selectorPath[match.pathIndex];
5296
firstElement = new tree.Element(
5297
match.initialCombinator,
5298
replacementSelector.elements[0].value,
5299
replacementSelector.elements[0].index
5302
if (match.pathIndex > currentSelectorPathIndex && currentSelectorPathElementIndex > 0) {
5303
path[path.length - 1].elements = path[path.length - 1].elements.concat(selectorPath[currentSelectorPathIndex].elements.slice(currentSelectorPathElementIndex));
5304
currentSelectorPathElementIndex = 0;
5305
currentSelectorPathIndex++;
5308
path = path.concat(selectorPath.slice(currentSelectorPathIndex, match.pathIndex));
5310
path.push(new tree.Selector(
5312
.slice(currentSelectorPathElementIndex, match.index)
5313
.concat([firstElement])
5314
.concat(replacementSelector.elements.slice(1))
5316
currentSelectorPathIndex = match.endPathIndex;
5317
currentSelectorPathElementIndex = match.endPathElementIndex;
5318
if (currentSelectorPathElementIndex >= selector.elements.length) {
5319
currentSelectorPathElementIndex = 0;
5320
currentSelectorPathIndex++;
5324
if (currentSelectorPathIndex < selectorPath.length && currentSelectorPathElementIndex > 0) {
5325
path[path.length - 1].elements = path[path.length - 1].elements.concat(selectorPath[currentSelectorPathIndex].elements.slice(currentSelectorPathElementIndex));
5326
currentSelectorPathElementIndex = 0;
5327
currentSelectorPathIndex++;
5330
path = path.concat(selectorPath.slice(currentSelectorPathIndex, selectorPath.length));
5334
visitRulesetOut: function (rulesetNode) {
5336
visitMedia: function (mediaNode, visitArgs) {
5337
var newAllExtends = mediaNode.allExtends.concat(this.allExtendsStack[this.allExtendsStack.length-1]);
5338
newAllExtends = newAllExtends.concat(this.doExtendChaining(newAllExtends, mediaNode.allExtends));
5339
this.allExtendsStack.push(newAllExtends);
5341
visitMediaOut: function (mediaNode) {
5342
this.allExtendsStack.length = this.allExtendsStack.length - 1;
5344
visitDirective: function (directiveNode, visitArgs) {
5345
var newAllExtends = directiveNode.allExtends.concat(this.allExtendsStack[this.allExtendsStack.length-1]);
5346
newAllExtends = newAllExtends.concat(this.doExtendChaining(newAllExtends, directiveNode.allExtends));
5347
this.allExtendsStack.push(newAllExtends);
5349
visitDirectiveOut: function (directiveNode) {
5350
this.allExtendsStack.length = this.allExtendsStack.length - 1;
5354
})(require('./tree'));//
5355
// browser.js - client-side engine
5358
var isFileProtocol = /^(file|chrome(-extension)?|resource|qrc|app):/.test(location.protocol);
5360
less.env = less.env || (location.hostname == '127.0.0.1' ||
5361
location.hostname == '0.0.0.0' ||
5362
location.hostname == 'localhost' ||
5363
location.port.length > 0 ||
5364
isFileProtocol ? 'development'
5367
// Load styles asynchronously (default: false)
5369
// This is set to `false` by default, so that the body
5370
// doesn't start loading before the stylesheets are parsed.
5371
// Setting this to `true` can result in flickering.
5373
less.async = less.async || false;
5374
less.fileAsync = less.fileAsync || false;
5376
// Interval between watch polls
5377
less.poll = less.poll || (isFileProtocol ? 1000 : 1500);
5379
//Setup user functions
5380
if (less.functions) {
5381
for(var func in less.functions) {
5382
less.tree.functions[func] = less.functions[func];
5386
var dumpLineNumbers = /!dumpLineNumbers:(comments|mediaquery|all)/.exec(location.hash);
5387
if (dumpLineNumbers) {
5388
less.dumpLineNumbers = dumpLineNumbers[1];
5394
less.watch = function () {
5395
if (!less.watchMode ){
5396
less.env = 'development';
5399
return this.watchMode = true
5402
less.unwatch = function () {clearInterval(less.watchTimer); return this.watchMode = false; };
5404
function initRunningMode(){
5405
if (less.env === 'development') {
5406
less.optimization = 0;
5407
less.watchTimer = setInterval(function () {
5408
if (less.watchMode) {
5409
loadStyleSheets(function (e, root, _, sheet, env) {
5411
error(e, sheet.href);
5413
createCSS(root.toCSS(less), sheet, env.lastModified);
5419
less.optimization = 3;
5423
if (/!watch/.test(location.hash)) {
5429
if (less.env != 'development') {
5431
cache = (typeof(window.localStorage) === 'undefined') ? null : window.localStorage;
5436
// Get all <link> tags with the 'rel' attribute set to "stylesheet/less"
5438
var links = document.getElementsByTagName('link');
5439
var typePattern = /^text\/(x-)?less$/;
5443
for (var i = 0; i < links.length; i++) {
5444
if (links[i].rel === 'stylesheet/less' || (links[i].rel.match(/stylesheet/) &&
5445
(links[i].type.match(typePattern)))) {
5446
less.sheets.push(links[i]);
5451
// With this function, it's possible to alter variables and re-render
5452
// CSS without reloading less-files
5454
var session_cache = '';
5455
less.modifyVars = function(record) {
5456
var str = session_cache;
5457
for (name in record) {
5458
str += ((name.slice(0,1) === '@')? '' : '@') + name +': '+
5459
((record[name].slice(-1) === ';')? record[name] : record[name] +';');
5461
new(less.Parser)(new less.tree.parseEnv(less)).parse(str, function (e, root) {
5463
error(e, "session_cache");
5465
createCSS(root.toCSS(less), less.sheets[less.sheets.length - 1]);
5470
less.refresh = function (reload) {
5471
var startTime, endTime;
5472
startTime = endTime = new(Date);
5474
loadStyleSheets(function (e, root, _, sheet, env) {
5476
return error(e, sheet.href);
5479
log("loading " + sheet.href + " from cache.");
5481
log("parsed " + sheet.href + " successfully.");
5482
createCSS(root.toCSS(less), sheet, env.lastModified);
5484
log("css for " + sheet.href + " generated in " + (new(Date) - endTime) + 'ms');
5485
(env.remaining === 0) && log("css generated in " + (new(Date) - startTime) + 'ms');
5486
endTime = new(Date);
5491
less.refreshStyles = loadStyles;
5493
less.refresh(less.env === 'development');
5495
function loadStyles() {
5496
var styles = document.getElementsByTagName('style');
5497
for (var i = 0; i < styles.length; i++) {
5498
if (styles[i].type.match(typePattern)) {
5499
var env = new less.tree.parseEnv(less);
5500
env.filename = document.location.href.replace(/#.*$/, '');
5502
new(less.Parser)(env).parse(styles[i].innerHTML || '', function (e, cssAST) {
5504
return error(e, "inline");
5506
var css = cssAST.toCSS(less);
5507
var style = styles[i];
5508
style.type = 'text/css';
5509
if (style.styleSheet) {
5510
style.styleSheet.cssText = css;
5512
style.innerHTML = css;
5519
function loadStyleSheets(callback, reload) {
5520
for (var i = 0; i < less.sheets.length; i++) {
5521
loadStyleSheet(less.sheets[i], callback, reload, less.sheets.length - (i + 1));
5525
function pathDiff(url, baseUrl) {
5526
// diff between two paths to create a relative path
5528
var urlParts = extractUrlParts(url),
5529
baseUrlParts = extractUrlParts(baseUrl),
5530
i, max, urlDirectories, baseUrlDirectories, diff = "";
5531
if (urlParts.hostPart !== baseUrlParts.hostPart) {
5534
max = Math.max(baseUrlParts.directories.length, urlParts.directories.length);
5535
for(i = 0; i < max; i++) {
5536
if (baseUrlParts.directories[i] !== urlParts.directories[i]) { break; }
5538
baseUrlDirectories = baseUrlParts.directories.slice(i);
5539
urlDirectories = urlParts.directories.slice(i);
5540
for(i = 0; i < baseUrlDirectories.length-1; i++) {
5543
for(i = 0; i < urlDirectories.length-1; i++) {
5544
diff += urlDirectories[i] + "/";
5549
function extractUrlParts(url, baseUrl) {
5550
// urlParts[1] = protocol&hostname || /
5551
// urlParts[2] = / if path relative to host base
5552
// urlParts[3] = directories
5553
// urlParts[4] = filename
5554
// urlParts[5] = parameters
5556
var urlPartsRegex = /^((?:[a-z-]+:)?\/+?(?:[^\/\?#]*\/)|([\/\\]))?((?:[^\/\\\?#]*[\/\\])*)([^\/\\\?#]*)([#\?].*)?$/,
5557
urlParts = url.match(urlPartsRegex),
5558
returner = {}, directories = [], i, baseUrlParts;
5561
throw new Error("Could not parse sheet href - '"+url+"'");
5564
// Stylesheets in IE don't always return the full path
5565
if (!urlParts[1] || urlParts[2]) {
5566
baseUrlParts = baseUrl.match(urlPartsRegex);
5567
if (!baseUrlParts) {
5568
throw new Error("Could not parse page url - '"+baseUrl+"'");
5570
urlParts[1] = baseUrlParts[1];
5572
urlParts[3] = baseUrlParts[3] + urlParts[3];
5577
directories = urlParts[3].replace("\\", "/").split("/");
5579
for(i = 0; i < directories.length; i++) {
5580
if (directories[i] === ".." && i > 0) {
5581
directories.splice(i-1, 2);
5587
returner.hostPart = urlParts[1];
5588
returner.directories = directories;
5589
returner.path = urlParts[1] + directories.join("/");
5590
returner.fileUrl = returner.path + (urlParts[4] || "");
5591
returner.url = returner.fileUrl + (urlParts[5] || "");
5595
function loadStyleSheet(sheet, callback, reload, remaining) {
5597
// sheet may be set to the stylesheet for the initial load or a collection of properties including
5598
// some env variables for imports
5599
var hrefParts = extractUrlParts(sheet.href, window.location.href);
5600
var href = hrefParts.url;
5601
var css = cache && cache.getItem(href);
5602
var timestamp = cache && cache.getItem(href + ':timestamp');
5603
var styles = { css: css, timestamp: timestamp };
5606
relativeUrls: less.relativeUrls,
5607
currentDirectory: hrefParts.path,
5611
if (sheet instanceof less.tree.parseEnv) {
5612
env = new less.tree.parseEnv(sheet);
5613
newFileInfo.entryPath = env.currentFileInfo.entryPath;
5614
newFileInfo.rootpath = env.currentFileInfo.rootpath;
5615
newFileInfo.rootFilename = env.currentFileInfo.rootFilename;
5617
env = new less.tree.parseEnv(less);
5618
env.mime = sheet.type;
5619
newFileInfo.entryPath = hrefParts.path;
5620
newFileInfo.rootpath = less.rootpath || hrefParts.path;
5621
newFileInfo.rootFilename = href;
5624
if (env.relativeUrls) {
5625
//todo - this relies on option being set on less object rather than being passed in as an option
5626
// - need an originalRootpath
5627
if (less.rootpath) {
5628
newFileInfo.rootpath = extractUrlParts(less.rootpath + pathDiff(hrefParts.path, newFileInfo.entryPath)).path;
5630
newFileInfo.rootpath = hrefParts.path;
5634
xhr(href, sheet.type, function (data, lastModified) {
5635
// Store data this session
5636
session_cache += data.replace(/@import .+?;/ig, '');
5638
if (!reload && styles && lastModified &&
5639
(new(Date)(lastModified).valueOf() ===
5640
new(Date)(styles.timestamp).valueOf())) {
5642
createCSS(styles.css, sheet);
5643
callback(null, null, data, sheet, { local: true, remaining: remaining }, href);
5645
// Use remote copy (re-parse)
5647
env.contents[href] = data; // Updating content cache
5648
env.paths = [hrefParts.path];
5649
env.currentFileInfo = newFileInfo;
5651
new(less.Parser)(env).parse(data, function (e, root) {
5652
if (e) { return callback(e, null, null, sheet); }
5654
callback(e, root, data, sheet, { local: false, lastModified: lastModified, remaining: remaining }, href);
5655
//TODO - there must be a better way? A generic less-to-css function that can both call error
5656
//and removeNode where appropriate
5657
//should also add tests
5658
if (env.currentFileInfo.rootFilename === href) {
5659
removeNode(document.getElementById('less-error-message:' + extractId(href)));
5662
callback(e, null, null, sheet);
5666
callback(e, null, null, sheet);
5669
}, function (status, url) {
5670
callback({ type: 'File', message: "'" + url + "' wasn't found (" + status + ")" }, null, null, sheet);
5674
function extractId(href) {
5675
return href.replace(/^[a-z-]+:\/+?[^\/]+/, '' ) // Remove protocol & domain
5676
.replace(/^\//, '' ) // Remove root /
5677
.replace(/\.[a-zA-Z]+$/, '' ) // Remove simple extension
5678
.replace(/[^\.\w-]+/g, '-') // Replace illegal characters
5679
.replace(/\./g, ':'); // Replace dots with colons(for valid id)
5682
function createCSS(styles, sheet, lastModified) {
5683
// Strip the query-string
5684
var href = sheet.href || '';
5686
// If there is no title set, use the filename, minus the extension
5687
var id = 'less:' + (sheet.title || extractId(href));
5689
// If this has already been inserted into the DOM, we may need to replace it
5690
var oldCss = document.getElementById(id);
5691
var keepOldCss = false;
5693
// Create a new stylesheet node for insertion or (if necessary) replacement
5694
var css = document.createElement('style');
5695
css.setAttribute('type', 'text/css');
5697
css.setAttribute('media', sheet.media);
5701
if (css.styleSheet) { // IE
5703
css.styleSheet.cssText = styles;
5705
throw new(Error)("Couldn't reassign styleSheet.cssText.");
5708
css.appendChild(document.createTextNode(styles));
5710
// If new contents match contents of oldCss, don't replace oldCss
5711
keepOldCss = (oldCss !== null && oldCss.childNodes.length > 0 && css.childNodes.length > 0 &&
5712
oldCss.firstChild.nodeValue === css.firstChild.nodeValue);
5715
var head = document.getElementsByTagName('head')[0];
5717
// If there is no oldCss, just append; otherwise, only append if we need
5718
// to replace oldCss with an updated stylesheet
5719
if (oldCss == null || keepOldCss === false) {
5720
var nextEl = sheet && sheet.nextSibling || null;
5721
(nextEl || document.getElementsByTagName('head')[0]).parentNode.insertBefore(css, nextEl);
5723
if (oldCss && keepOldCss === false) {
5724
head.removeChild(oldCss);
5727
// Don't update the local store if the file wasn't modified
5728
if (lastModified && cache) {
5729
log('saving ' + href + ' to cache.');
5731
cache.setItem(href, styles);
5732
cache.setItem(href + ':timestamp', lastModified);
5734
//TODO - could do with adding more robust error handling
5735
log('failed to save');
5740
function xhr(url, type, callback, errback) {
5741
var xhr = getXMLHttpRequest();
5742
var async = isFileProtocol ? less.fileAsync : less.async;
5744
if (typeof(xhr.overrideMimeType) === 'function') {
5745
xhr.overrideMimeType('text/css');
5747
xhr.open('GET', url, async);
5748
xhr.setRequestHeader('Accept', type || 'text/x-less, text/css; q=0.9, */*; q=0.5');
5751
if (isFileProtocol && !less.fileAsync) {
5752
if (xhr.status === 0 || (xhr.status >= 200 && xhr.status < 300)) {
5753
callback(xhr.responseText);
5755
errback(xhr.status, url);
5758
xhr.onreadystatechange = function () {
5759
if (xhr.readyState == 4) {
5760
handleResponse(xhr, callback, errback);
5764
handleResponse(xhr, callback, errback);
5767
function handleResponse(xhr, callback, errback) {
5768
if (xhr.status >= 200 && xhr.status < 300) {
5769
callback(xhr.responseText,
5770
xhr.getResponseHeader("Last-Modified"));
5771
} else if (typeof(errback) === 'function') {
5772
errback(xhr.status, url);
5777
function getXMLHttpRequest() {
5778
if (window.XMLHttpRequest) {
5779
return new(XMLHttpRequest);
5782
return new(ActiveXObject)("MSXML2.XMLHTTP.3.0");
5784
log("browser doesn't support AJAX.");
5790
function removeNode(node) {
5791
return node && node.parentNode.removeChild(node);
5795
if (less.env == 'development' && typeof(console) !== "undefined") { console.log('less: ' + str) }
5798
function error(e, rootHref) {
5799
var id = 'less-error-message:' + extractId(rootHref || "");
5800
var template = '<li><label>{line}</label><pre class="{class}">{content}</pre></li>';
5801
var elem = document.createElement('div'), timer, content, error = [];
5802
var filename = e.filename || rootHref;
5803
var filenameNoPath = filename.match(/([^\/]+(\?.*)?)$/)[1];
5806
elem.className = "less-error-message";
5808
content = '<h3>' + (e.type || "Syntax") + "Error: " + (e.message || 'There is an error in your .less file') +
5809
'</h3>' + '<p>in <a href="' + filename + '">' + filenameNoPath + "</a> ";
5811
var errorline = function (e, i, classname) {
5812
if (e.extract[i] != undefined) {
5813
error.push(template.replace(/\{line\}/, (parseInt(e.line) || 0) + (i - 1))
5814
.replace(/\{class\}/, classname)
5815
.replace(/\{content\}/, e.extract[i]));
5820
errorline(e, 0, '');
5821
errorline(e, 1, 'line');
5822
errorline(e, 2, '');
5823
content += 'on line ' + e.line + ', column ' + (e.column + 1) + ':</p>' +
5824
'<ul>' + error.join('') + '</ul>';
5825
} else if (e.stack) {
5826
content += '<br/>' + e.stack.split('\n').slice(1).join('<br/>');
5828
elem.innerHTML = content;
5830
// CSS for error messages
5832
'.less-error-message ul, .less-error-message li {',
5833
'list-style-type: none;',
5834
'margin-right: 15px;',
5838
'.less-error-message label {',
5840
'margin-right: 15px;',
5844
'.less-error-message pre {',
5848
'display: inline-block;',
5850
'.less-error-message pre.line {',
5853
'.less-error-message h3 {',
5855
'font-weight: bold;',
5856
'padding: 15px 0 5px 0;',
5859
'.less-error-message a {',
5862
'.less-error-message .error {',
5864
'font-weight: bold;',
5865
'padding-bottom: 2px;',
5866
'border-bottom: 1px dashed red;',
5868
].join('\n'), { title: 'error-message' });
5870
elem.style.cssText = [
5871
"font-family: Arial, sans-serif",
5872
"border: 1px solid #e00",
5873
"background-color: #eee",
5874
"border-radius: 5px",
5875
"-webkit-border-radius: 5px",
5876
"-moz-border-radius: 5px",
5879
"margin-bottom: 15px"
5882
if (less.env == 'development') {
5883
timer = setInterval(function () {
5884
if (document.body) {
5885
if (document.getElementById(id)) {
5886
document.body.replaceChild(elem, document.getElementById(id));
5888
document.body.insertBefore(elem, document.body.firstChild);
5890
clearInterval(timer);
5897
// Define Less as an AMD module.
5898
if (typeof define === "function" && define.amd) {
5899
define(function () { return less; } );