2
// LESS - Leaner CSS v1.3.0
5
// Copyright (c) 2009-2011, Alexis Sellier
6
// Licensed under the Apache 2.0 License.
8
(function (window, undefined) {
10
// Stub out `require` in the browser
12
function require(arg) {
13
return window.less[arg.split('/')[1]];
18
// Define Less as an AMD module.
19
if (typeof define === "function" && define.amd) {
20
define("less", [], function () { return less; } );
25
// -- kriskowal Kris Kowal Copyright (C) 2009-2010 MIT License
26
// -- tlrobinson Tom Robinson
27
// dantman Daniel Friesen
33
Array.isArray = function(obj) {
34
return Object.prototype.toString.call(obj) === "[object Array]" ||
35
(obj instanceof Array);
38
if (!Array.prototype.forEach) {
39
Array.prototype.forEach = function(block, thisObject) {
40
var len = this.length >>> 0;
41
for (var i = 0; i < len; i++) {
43
block.call(thisObject, this[i], i, this);
48
if (!Array.prototype.map) {
49
Array.prototype.map = function(fun /*, thisp*/) {
50
var len = this.length >>> 0;
51
var res = new Array(len);
52
var thisp = arguments[1];
54
for (var i = 0; i < len; i++) {
56
res[i] = fun.call(thisp, this[i], i, this);
62
if (!Array.prototype.filter) {
63
Array.prototype.filter = function (block /*, thisp */) {
65
var thisp = arguments[1];
66
for (var i = 0; i < this.length; i++) {
67
if (block.call(thisp, this[i])) {
74
if (!Array.prototype.reduce) {
75
Array.prototype.reduce = function(fun /*, initial*/) {
76
var len = this.length >>> 0;
79
// no value to return if no initial value and an empty array
80
if (len === 0 && arguments.length === 1) throw new TypeError();
82
if (arguments.length >= 2) {
83
var rv = arguments[1];
90
// if array contains no values, no initial value to return
91
if (++i >= len) throw new TypeError();
94
for (; i < len; i++) {
96
rv = fun.call(null, rv, this[i], i, this);
102
if (!Array.prototype.indexOf) {
103
Array.prototype.indexOf = function (value /*, fromIndex */ ) {
104
var length = this.length;
105
var i = arguments[1] || 0;
107
if (!length) return -1;
108
if (i >= length) return -1;
109
if (i < 0) i += length;
111
for (; i < length; i++) {
112
if (!Object.prototype.hasOwnProperty.call(this, i)) { continue }
113
if (value === this[i]) return i;
123
Object.keys = function (object) {
125
for (var name in object) {
126
if (Object.prototype.hasOwnProperty.call(object, name)) {
137
if (!String.prototype.trim) {
138
String.prototype.trim = function () {
139
return String(this).replace(/^\s\s*/, '').replace(/\s\s*$/, '');
144
if (typeof environment === "object" && ({}).toString.call(environment) === "[object Environment]") {
146
// Details on how to detect Rhino: https://github.com/ringo/ringojs/issues/88
147
if (typeof(window) === 'undefined') { less = {} }
148
else { less = window.less = {} }
149
tree = less.tree = {};
151
} else if (typeof(window) === 'undefined') {
154
tree = require('./tree');
158
if (typeof(window.less) === 'undefined') { window.less = {} }
160
tree = window.less.tree = {};
161
less.mode = 'browser';
166
// A relatively straight-forward predictive parser.
167
// There is no tokenization/lexing stage, the input is parsed
170
// To make the parser fast enough to run in the browser, several
171
// optimization had to be made:
173
// - Matching and slicing on a huge input is often cause of slowdowns.
174
// The solution is to chunkify the input into smaller strings.
175
// The chunks are stored in the `chunks` var,
176
// `j` holds the current chunk index, and `current` holds
177
// the index of the current chunk in relation to `input`.
178
// This gives us an almost 4x speed-up.
180
// - In many cases, we don't need to match individual tokens;
181
// for example, if a value doesn't hold any variables, operations
182
// or dynamic references, the parser can effectively 'skip' it,
183
// treating it as a literal.
184
// An example would be '1px solid #000' - which evaluates to itself,
185
// we don't need to know what the individual components are.
186
// The drawback, of course is that you don't get the benefits of
187
// syntax-checking on the CSS. This gives us a 50% speed-up in the parser,
188
// and a smaller speed-up in the code-gen.
191
// Token matching is done with the `$` function, which either takes
192
// a terminal string or regexp, or a non-terminal function to call.
193
// It also takes care of moving all the indices forwards.
196
less.Parser = function Parser(env) {
197
var input, // LeSS input string
198
i, // current index in `input`
200
temp, // temporarily holds a chunk's state, for backtracking
201
memo, // temporarily holds `i`, when backtracking
202
furthest, // furthest index the parser has gone to
203
chunks, // chunkified input
204
current, // index of current chunk, in `input`
209
// This function is called after all files
210
// have been imported through `@import`.
211
var finish = function () {};
213
var imports = this.imports = {
214
paths: env && env.paths || [], // Search paths, when importing
215
queue: [], // Files which haven't been imported yet
216
files: {}, // Holds the imported parse trees
217
contents: {}, // Holds the imported file contents
218
mime: env && env.mime, // MIME type of .less files
219
error: null, // Error in parsing/evaluating an import
220
push: function (path, callback) {
222
this.queue.push(path);
225
// Import a file asynchronously
227
less.Parser.importer(path, this.paths, function (e, root, contents) {
228
that.queue.splice(that.queue.indexOf(path), 1); // Remove the path from the queue
229
that.files[path] = root; // Store the root
230
that.contents[path] = contents;
232
if (e && !that.error) { that.error = e }
235
if (that.queue.length === 0) { finish() } // Call `finish` if we're done importing
240
function save() { temp = chunks[j], memo = i, current = i }
241
function restore() { chunks[j] = temp, i = memo, current = i }
245
chunks[j] = chunks[j].slice(i - current);
250
// Parse from a token, regexp or string, and move forward if match
253
var match, args, length, c, index, endIndex, k, mem;
258
if (tok instanceof Function) {
259
return tok.call(parser.parsers);
263
// Either match a single character in the input,
264
// or match a regexp in the current chunk (chunk[j]).
266
} else if (typeof(tok) === 'string') {
267
match = input.charAt(i) === tok ? tok : null;
273
if (match = tok.exec(chunks[j])) {
274
length = match[0].length;
280
// The match is confirmed, add the match length to `i`,
281
// and consume any extra white-space characters (' ' || '\n')
282
// which come after that. The reason for this is that LeSS's
283
// grammar is mostly white-space insensitive.
287
endIndex = i + chunks[j].length - length;
289
while (i < endIndex) {
290
c = input.charCodeAt(i);
291
if (! (c === 32 || c === 10 || c === 9)) { break }
294
chunks[j] = chunks[j].slice(length + (i - mem));
297
if (chunks[j].length === 0 && j < chunks.length - 1) { j++ }
299
if(typeof(match) === 'string') {
302
return match.length === 1 ? match[0] : match;
307
function expect(arg, msg) {
310
error(msg || (typeof(arg) === 'string' ? "expected '" + arg + "' got '" + input.charAt(i) + "'"
311
: "unexpected token"));
317
function error(msg, type) {
318
throw { index: i, type: type || 'Syntax', message: msg };
321
// Same as $(), but don't change the state of the parser,
322
// just return the match.
324
if (typeof(tok) === 'string') {
325
return input.charAt(i) === tok;
327
if (tok.test(chunks[j])) {
335
function basename(pathname) {
336
if (less.mode === 'node') {
337
return require('path').basename(pathname);
339
return pathname.match(/[^\/]+$/)[0];
343
function getInput(e, env) {
344
if (e.filename && env.filename && (e.filename !== env.filename)) {
345
return parser.imports.contents[basename(e.filename)];
351
function getLocation(index, input) {
352
for (var n = index, column = -1;
353
n >= 0 && input.charAt(n) !== '\n';
356
return { line: typeof(index) === 'number' ? (input.slice(0, index).match(/\n/g) || "").length : null,
360
function LessError(e, env) {
361
var input = getInput(e, env),
362
loc = getLocation(e.index, input),
365
lines = input.split('\n');
367
this.type = e.type || 'Syntax';
368
this.message = e.message;
369
this.filename = e.filename || env.filename;
370
this.index = e.index;
371
this.line = typeof(line) === 'number' ? line + 1 : null;
372
this.callLine = e.call && (getLocation(e.call, input).line + 1);
373
this.callExtract = lines[getLocation(e.call, input).line];
374
this.stack = e.stack;
383
this.env = env = env || {};
385
// The optimization level dictates the thoroughness of the parser,
386
// the lower the number, the less nodes it will create in the tree.
387
// This could matter for debugging, or if you want to access
388
// the individual nodes in the tree.
389
this.optimization = ('optimization' in this.env) ? this.env.optimization : 1;
391
this.env.filename = this.env.filename || null;
400
// Parse an input string into an abstract syntax tree,
401
// call `callback` when done.
403
parse: function (str, callback) {
404
var root, start, end, zone, line, lines, buff = [], c, error = null;
406
i = j = current = furthest = 0;
407
input = str.replace(/\r\n/g, '\n');
409
// Split the input into chunks.
410
chunks = (function (chunks) {
412
skip = /[^"'`\{\}\/\(\)\\]+/g,
413
comment = /\/\*(?:[^*]|\*+[^\/*])*\*+\/|\/\/.*/g,
414
string = /"((?:[^"\\\r\n]|\\.)*)"|'((?:[^'\\\r\n]|\\.)*)'|`((?:[^`\\\r\n]|\\.)*)`/g,
420
for (var i = 0, c, cc; i < input.length; i++) {
422
if (match = skip.exec(input)) {
423
if (match.index === i) {
424
i += match[0].length;
425
chunk.push(match[0]);
429
comment.lastIndex = string.lastIndex = i;
431
if (match = string.exec(input)) {
432
if (match.index === i) {
433
i += match[0].length;
434
chunk.push(match[0]);
439
if (!inParam && c === '/') {
440
cc = input.charAt(i + 1);
441
if (cc === '/' || cc === '*') {
442
if (match = comment.exec(input)) {
443
if (match.index === i) {
444
i += match[0].length;
445
chunk.push(match[0]);
453
case '{': if (! inParam) { level ++; chunk.push(c); break }
454
case '}': if (! inParam) { level --; chunk.push(c); chunks[++j] = chunk = []; break }
455
case '(': if (! inParam) { inParam = true; chunk.push(c); break }
456
case ')': if ( inParam) { inParam = false; chunk.push(c); break }
457
default: chunk.push(c);
461
error = new(LessError)({
464
message: "missing closing `}`",
465
filename: env.filename
469
return chunks.map(function (c) { return c.join('') });;
473
return callback(error);
476
// Start with the primary rule.
477
// The whole syntax tree is held under a Ruleset node,
478
// with the `root` property set to true, so no `{}` are
479
// output. The callback is called when the input is parsed.
481
root = new(tree.Ruleset)([], $(this.parsers.primary));
484
return callback(new(LessError)(e, env));
487
root.toCSS = (function (evaluate) {
488
var line, lines, column;
490
return function (options, variables) {
491
var frames = [], importError;
493
options = options || {};
495
// Allows setting variables with a hash, so:
497
// `{ color: new(tree.Color)('#f01') }` will become:
499
// new(tree.Rule)('@color',
501
// new(tree.Expression)([
502
// new(tree.Color)('#f01')
507
if (typeof(variables) === 'object' && !Array.isArray(variables)) {
508
variables = Object.keys(variables).map(function (k) {
509
var value = variables[k];
511
if (! (value instanceof tree.Value)) {
512
if (! (value instanceof tree.Expression)) {
513
value = new(tree.Expression)([value]);
515
value = new(tree.Value)([value]);
517
return new(tree.Rule)('@' + k, value, false, 0);
519
frames = [new(tree.Ruleset)(null, variables)];
523
var css = evaluate.call(this, { frames: frames })
524
.toCSS([], { compress: options.compress || false });
526
throw new(LessError)(e, env);
529
if ((importError = parser.imports.error)) { // Check if there was an error during importing
530
if (importError instanceof LessError) throw importError;
531
else throw new(LessError)(importError, env);
534
if (options.yuicompress && less.mode === 'node') {
535
return require('./cssmin').compressor.cssmin(css);
536
} else if (options.compress) {
537
return css.replace(/(\s)+/g, "$1");
544
// If `i` is smaller than the `input.length - 1`,
545
// it means the parser wasn't able to parse the whole
546
// string, so we've got a parsing error.
548
// We try to extract a \n delimited string,
549
// showing the line where the parse error occured.
550
// We split it up into two parts (the part which parsed,
551
// and the part which didn't), so we can color them differently.
552
if (i < input.length - 1) {
554
lines = input.split('\n');
555
line = (input.slice(0, i).match(/\n/g) || "").length + 1;
557
for (var n = i, column = -1; n >= 0 && input.charAt(n) !== '\n'; n--) { column++ }
561
message: "Syntax Error on line " + line,
563
filename: env.filename,
574
if (this.imports.queue.length > 0) {
575
finish = function () { callback(error, root) };
577
callback(error, root);
582
// Here in, the parsing rules/functions
584
// The basic structure of the syntax tree generated is as follows:
586
// Ruleset -> Rule -> Value -> Expression -> Entity
588
// Here's some LESS code:
592
// border: 1px solid #000;
597
// And here's what the parse tree might look like:
599
// Ruleset (Selector '.class', [
600
// Rule ("color", Value ([Expression [Color #fff]]))
601
// Rule ("border", Value ([Expression [Dimension 1px][Keyword "solid"][Color #000]]))
602
// Rule ("width", Value ([Expression [Operation "+" [Variable "@w"][Dimension 4px]]]))
603
// Ruleset (Selector [Element '>', '.child'], [...])
606
// In general, most rules will try to parse a token with the `$()` function, and if the return
607
// value is truly, will return a new node, of the relevant type. Sometimes, we need to check
608
// first, before parsing, that's when we use `peek()`.
612
// The `primary` rule is the *entry* and *exit* point of the parser.
613
// The rules here can appear at any level of the parse tree.
615
// The recursive nature of the grammar is an interplay between the `block`
616
// rule, which represents `{ ... }`, the `ruleset` rule, and this `primary` rule,
617
// as represented by this simplified grammar:
619
// primary → (ruleset | rule)+
620
// ruleset → selector+ block
621
// block → '{' primary '}'
623
// Only at one point is the primary rule not called from the
624
// block rule: at the root level.
626
primary: function () {
629
while ((node = $(this.mixin.definition) || $(this.rule) || $(this.ruleset) ||
630
$(this.mixin.call) || $(this.comment) || $(this.directive))
632
node && root.push(node);
637
// We create a Comment node for CSS comments `/* */`,
638
// but keep the LeSS comments `//` silent, by just skipping
640
comment: function () {
643
if (input.charAt(i) !== '/') return;
645
if (input.charAt(i + 1) === '/') {
646
return new(tree.Comment)($(/^\/\/.*/), true);
647
} else if (comment = $(/^\/\*(?:[^*]|\*+[^\/*])*\*+\/\n?/)) {
648
return new(tree.Comment)(comment);
653
// Entities are tokens which can be found inside an Expression
657
// A string, which supports escaping " and '
659
// "milky way" 'he\'s the one!'
661
quoted: function () {
664
if (input.charAt(j) === '~') { j++, e = true } // Escaped strings
665
if (input.charAt(j) !== '"' && input.charAt(j) !== "'") return;
669
if (str = $(/^"((?:[^"\\\r\n]|\\.)*)"|'((?:[^'\\\r\n]|\\.)*)'/)) {
670
return new(tree.Quoted)(str[0], str[1] || str[2], e);
675
// A catch-all word, such as:
677
// black border-collapse
679
keyword: function () {
682
if (k = $(/^[_A-Za-z-][_A-Za-z0-9-]*/)) {
683
if (tree.colors.hasOwnProperty(k)) {
684
// detect named color
685
return new(tree.Color)(tree.colors[k].slice(1));
687
return new(tree.Keyword)(k);
697
// We also try to catch IE's `alpha()`, but let the `alpha` parser
698
// deal with the details.
700
// The arguments are parsed with the `entities.arguments` parser.
703
var name, args, index = i;
705
if (! (name = /^([\w-]+|%|progid:[\w\.]+)\(/.exec(chunks[j]))) return;
707
name = name[1].toLowerCase();
709
if (name === 'url') { return null }
710
else { i += name.length }
712
if (name === 'alpha') { return $(this.alpha) }
714
$('('); // Parse the '(' and consume whitespace.
716
args = $(this.entities.arguments);
718
if (! $(')')) return;
720
if (name) { return new(tree.Call)(name, args, index, env.filename) }
722
arguments: function () {
725
while (arg = $(this.entities.assignment) || $(this.expression)) {
727
if (! $(',')) { break }
731
literal: function () {
732
return $(this.entities.dimension) ||
733
$(this.entities.color) ||
734
$(this.entities.quoted);
737
// Assignments are argument entities for calls.
738
// They are present in ie filter properties as shown below.
740
// filter: progid:DXImageTransform.Microsoft.Alpha( *opacity=50* )
743
assignment: function () {
745
if ((key = $(/^\w+(?=\s?=)/i)) && $('=') && (value = $(this.entity))) {
746
return new(tree.Assignment)(key, value);
751
// Parse url() tokens
753
// We use a specific rule for urls, because they don't really behave like
754
// standard function calls. The difference is that the argument doesn't have
755
// to be enclosed within a string, so it can't be parsed as an Expression.
760
if (input.charAt(i) !== 'u' || !$(/^url\(/)) return;
761
value = $(this.entities.quoted) || $(this.entities.variable) ||
762
$(this.entities.dataURI) || $(/^[-\w%@$\/.&=:;#+?~]+/) || "";
766
return new(tree.URL)((value.value || value.data || value instanceof tree.Variable)
767
? value : new(tree.Anonymous)(value), imports.paths);
770
dataURI: function () {
775
obj.mime = $(/^[^\/]+\/[^,;)]+/) || '';
776
obj.charset = $(/^;\s*charset=[^,;)]+/) || '';
777
obj.base64 = $(/^;\s*base64/) || '';
778
obj.data = $(/^,\s*[^)]+/);
780
if (obj.data) { return obj }
785
// A Variable entity, such as `@fink`, in
787
// width: @fink + 2px
789
// We use a different parser for variable definitions,
790
// see `parsers.variable`.
792
variable: function () {
795
if (input.charAt(i) === '@' && (name = $(/^@@?[\w-]+/))) {
796
return new(tree.Variable)(name, index, env.filename);
801
// A Hexadecimal color
805
// `rgb` and `hsl` colors are parsed through the `entities.call` parser.
810
if (input.charAt(i) === '#' && (rgb = $(/^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})/))) {
811
return new(tree.Color)(rgb[1]);
816
// A Dimension, that is, a number and a unit
820
dimension: function () {
821
var value, c = input.charCodeAt(i);
822
if ((c > 57 || c < 45) || c === 47) return;
824
if (value = $(/^(-?\d*\.?\d+)(px|%|em|rem|pc|ex|in|deg|s|ms|pt|cm|mm|rad|grad|turn)?/)) {
825
return new(tree.Dimension)(value[1], value[2]);
830
// JavaScript code to be evaluated
832
// `window.location.href`
834
javascript: function () {
837
if (input.charAt(j) === '~') { j++, e = true } // Escaped strings
838
if (input.charAt(j) !== '`') { return }
842
if (str = $(/^`([^`]*)`/)) {
843
return new(tree.JavaScript)(str[1], i, e);
849
// The variable part of a variable definition. Used in the `rule` parser
853
variable: function () {
856
if (input.charAt(i) === '@' && (name = $(/^(@[\w-]+)\s*:/))) { return name[1] }
860
// A font size/line-height shorthand
864
// We need to peek first, or we'll match on keywords and dimensions
866
shorthand: function () {
869
if (! peek(/^[@\w.%-]+\/[@\w.-]+/)) return;
871
if ((a = $(this.entity)) && $('/') && (b = $(this.entity))) {
872
return new(tree.Shorthand)(a, b);
881
// A Mixin call, with an optional argument list
883
// #mixins > .square(#fff);
884
// .rounded(4px, black);
887
// The `while` loop is there because mixins can be
888
// namespaced, but we only support the child and descendant
892
var elements = [], e, c, args, index = i, s = input.charAt(i), important = false;
894
if (s !== '.' && s !== '#') { return }
896
while (e = $(/^[#.](?:[\w-]|\\(?:[a-fA-F0-9]{1,6} ?|[^a-fA-F0-9]))+/)) {
897
elements.push(new(tree.Element)(c, e, i));
900
$('(') && (args = $(this.entities.arguments)) && $(')');
902
if ($(this.important)) {
906
if (elements.length > 0 && ($(';') || peek('}'))) {
907
return new(tree.mixin.Call)(elements, args || [], index, env.filename, important);
912
// A Mixin definition, with a list of parameters
914
// .rounded (@radius: 2px, @color) {
918
// Until we have a finer grained state-machine, we have to
919
// do a look-ahead, to make sure we don't have a mixin call.
920
// See the `rule` function for more information.
922
// We start by matching `.rounded (`, and then proceed on to
923
// the argument list, which has optional default values.
924
// We store the parameters in `params`, with a `value` key,
925
// if there is a value, such as in the case of `@radius`.
927
// Once we've got our params list, and a closing `)`, we parse
928
// the `{...}` block.
930
definition: function () {
931
var name, params = [], match, ruleset, param, value, cond, variadic = false;
932
if ((input.charAt(i) !== '.' && input.charAt(i) !== '#') ||
933
peek(/^[^{]*(;|})/)) return;
937
if (match = $(/^([#.](?:[\w-]|\\(?:[a-fA-F0-9]{1,6} ?|[^a-fA-F0-9]))+)\s*\(/)) {
941
if (input.charAt(i) === '.' && $(/^\.{3}/)) {
944
} else if (param = $(this.entities.variable) || $(this.entities.literal)
945
|| $(this.entities.keyword)) {
947
if (param instanceof tree.Variable) {
949
value = expect(this.expression, 'expected expression');
950
params.push({ name: param.name, value: value });
951
} else if ($(/^\.{3}/)) {
952
params.push({ name: param.name, variadic: true });
956
params.push({ name: param.name });
959
params.push({ value: param });
968
if ($(/^when/)) { // Guard
969
cond = expect(this.conditions, 'expected condition');
972
ruleset = $(this.block);
975
return new(tree.mixin.Definition)(name, params, ruleset, cond, variadic);
984
// Entities are the smallest recognized token,
985
// and can be found inside a rule's value.
987
entity: function () {
988
return $(this.entities.literal) || $(this.entities.variable) || $(this.entities.url) ||
989
$(this.entities.call) || $(this.entities.keyword) || $(this.entities.javascript) ||
994
// A Rule terminator. Note that we use `peek()` to check for '}',
995
// because the `block` rule will be expecting it, but we still need to make sure
996
// it's there, if ';' was ommitted.
999
return $(';') || peek('}');
1003
// IE's alpha function
1005
// alpha(opacity=88)
1007
alpha: function () {
1010
if (! $(/^\(opacity=/i)) return;
1011
if (value = $(/^\d+/) || $(this.entities.variable)) {
1013
return new(tree.Alpha)(value);
1018
// A Selector Element
1023
// input[type="text"]
1025
// Elements are the building blocks for Selectors,
1026
// they are made out of a `Combinator` (see combinator rule),
1027
// and an element name, such as a tag a class, or `*`.
1029
element: function () {
1032
c = $(this.combinator);
1033
e = $(/^(?:\d+\.\d+|\d+)%/) || $(/^(?:[.#]?|:*)(?:[\w-]|\\(?:[a-fA-F0-9]{1,6} ?|[^a-fA-F0-9]))+/) ||
1034
$('*') || $(this.attribute) || $(/^\([^)@]+\)/);
1037
$('(') && (v = $(this.entities.variable)) && $(')') && (e = new(tree.Paren)(v));
1040
if (e) { return new(tree.Element)(c, e, i) }
1042
if (c.value && c.value.charAt(0) === '&') {
1043
return new(tree.Element)(c, null, i);
1048
// Combinators combine elements together, in a Selector.
1050
// Because our parser isn't white-space sensitive, special care
1051
// has to be taken, when parsing the descendant combinator, ` `,
1052
// as it's an empty space. We have to check the previous character
1053
// in the input, to see if it's a ` ` character. More info on how
1054
// we deal with this in *combinator.js*.
1056
combinator: function () {
1057
var match, c = input.charAt(i);
1059
if (c === '>' || c === '+' || c === '~') {
1061
while (input.charAt(i) === ' ') { i++ }
1062
return new(tree.Combinator)(c);
1063
} else if (c === '&') {
1066
if(input.charAt(i) === ' ') {
1069
while (input.charAt(i) === ' ') { i++ }
1070
return new(tree.Combinator)(match);
1071
} else if (input.charAt(i - 1) === ' ') {
1072
return new(tree.Combinator)(" ");
1074
return new(tree.Combinator)(null);
1081
// .class > div + h1
1084
// Selectors are made out of one or more Elements, see above.
1086
selector: function () {
1087
var sel, e, elements = [], c, match;
1090
sel = $(this.entity);
1092
return new(tree.Selector)([new(tree.Element)('', sel, i)]);
1095
while (e = $(this.element)) {
1096
c = input.charAt(i);
1098
if (c === '{' || c === '}' || c === ';' || c === ',') { break }
1101
if (elements.length > 0) { return new(tree.Selector)(elements) }
1104
return $(/^[a-zA-Z][a-zA-Z-]*[0-9]?/) || $('*');
1106
attribute: function () {
1107
var attr = '', key, val, op;
1109
if (! $('[')) return;
1111
if (key = $(/^[a-zA-Z-]+/) || $(this.entities.quoted)) {
1112
if ((op = $(/^[|~*$^]?=/)) &&
1113
(val = $(this.entities.quoted) || $(/^[\w-]+/))) {
1114
attr = [key, op, val.toCSS ? val.toCSS() : val].join('');
1115
} else { attr = key }
1118
if (! $(']')) return;
1120
if (attr) { return "[" + attr + "]" }
1124
// The `block` rule is used by `ruleset` and `mixin.definition`.
1125
// It's a wrapper around the `primary` rule, with added `{}`.
1127
block: function () {
1130
if ($('{') && (content = $(this.primary)) && $('}')) {
1136
// div, .class, body > p {...}
1138
ruleset: function () {
1139
var selectors = [], s, rules, match;
1142
while (s = $(this.selector)) {
1145
if (! $(',')) { break }
1149
if (selectors.length > 0 && (rules = $(this.block))) {
1150
return new(tree.Ruleset)(selectors, rules, env.strictImports);
1158
var name, value, c = input.charAt(i), important, match;
1161
if (c === '.' || c === '#' || c === '&') { return }
1163
if (name = $(this.variable) || $(this.property)) {
1164
if ((name.charAt(0) != '@') && (match = /^([^@+\/'"*`(;{}-]*);/.exec(chunks[j]))) {
1165
i += match[0].length - 1;
1166
value = new(tree.Anonymous)(match[1]);
1167
} else if (name === "font") {
1168
value = $(this.font);
1170
value = $(this.value);
1172
important = $(this.important);
1174
if (value && $(this.end)) {
1175
return new(tree.Rule)(name, value, important, memo);
1184
// An @import directive
1188
// Depending on our environemnt, importing is done differently:
1189
// In the browser, it's an XHR request, in Node, it would be a
1190
// file-system operation. The function used for importing is
1191
// stored in `import`, which we pass to the Import constructor.
1193
"import": function () {
1194
var path, features, index = i;
1195
if ($(/^@import\s+/) &&
1196
(path = $(this.entities.quoted) || $(this.entities.url))) {
1197
features = $(this.mediaFeatures);
1199
return new(tree.Import)(path, imports, features, index);
1204
mediaFeature: function () {
1205
var e, p, nodes = [];
1208
if (e = $(this.entities.keyword)) {
1210
} else if ($('(')) {
1211
p = $(this.property);
1215
nodes.push(new(tree.Paren)(new(tree.Rule)(p, e, null, i, true)));
1217
nodes.push(new(tree.Paren)(e));
1221
} else { return null }
1225
if (nodes.length > 0) {
1226
return new(tree.Expression)(nodes);
1230
mediaFeatures: function () {
1231
var e, features = [];
1234
if (e = $(this.mediaFeature)) {
1236
if (! $(',')) { break }
1237
} else if (e = $(this.entities.variable)) {
1239
if (! $(',')) { break }
1243
return features.length > 0 ? features : null;
1246
media: function () {
1247
var features, rules;
1250
features = $(this.mediaFeatures);
1252
if (rules = $(this.block)) {
1253
return new(tree.Media)(rules, features);
1261
// @charset "utf-8";
1263
directive: function () {
1264
var name, value, rules, types, e, nodes;
1266
if (input.charAt(i) !== '@') return;
1268
if (value = $(this['import']) || $(this.media)) {
1270
} else if (name = $(/^@page|@keyframes/) || $(/^@(?:-webkit-|-moz-|-o-|-ms-)[a-z0-9-]+/)) {
1271
types = ($(/^[^{]+/) || '').trim();
1272
if (rules = $(this.block)) {
1273
return new(tree.Directive)(name + " " + types, rules);
1275
} else if (name = $(/^@[-a-z]+/)) {
1276
if (name === '@font-face') {
1277
if (rules = $(this.block)) {
1278
return new(tree.Directive)(name, rules);
1280
} else if ((value = $(this.entity)) && $(';')) {
1281
return new(tree.Directive)(name, value);
1286
var value = [], expression = [], weight, shorthand, font, e;
1288
while (e = $(this.shorthand) || $(this.entity)) {
1291
value.push(new(tree.Expression)(expression));
1294
while (e = $(this.expression)) {
1296
if (! $(',')) { break }
1299
return new(tree.Value)(value);
1303
// A Value is a comma-delimited list of Expressions
1305
// font-family: Baskerville, Georgia, serif;
1307
// In a Rule, a Value represents everything after the `:`,
1308
// and before the `;`.
1310
value: function () {
1311
var e, expressions = [], important;
1313
while (e = $(this.expression)) {
1314
expressions.push(e);
1315
if (! $(',')) { break }
1318
if (expressions.length > 0) {
1319
return new(tree.Value)(expressions);
1322
important: function () {
1323
if (input.charAt(i) === '!') {
1324
return $(/^! *important/);
1330
if ($('(') && (e = $(this.expression)) && $(')')) {
1334
multiplication: function () {
1335
var m, a, op, operation;
1336
if (m = $(this.operand)) {
1337
while (!peek(/^\/\*/) && (op = ($('/') || $('*'))) && (a = $(this.operand))) {
1338
operation = new(tree.Operation)(op, [operation || m, a]);
1340
return operation || m;
1343
addition: function () {
1344
var m, a, op, operation;
1345
if (m = $(this.multiplication)) {
1346
while ((op = $(/^[-+]\s+/) || (input.charAt(i - 1) != ' ' && ($('+') || $('-')))) &&
1347
(a = $(this.multiplication))) {
1348
operation = new(tree.Operation)(op, [operation || m, a]);
1350
return operation || m;
1353
conditions: function () {
1354
var a, b, index = i, condition;
1356
if (a = $(this.condition)) {
1357
while ($(',') && (b = $(this.condition))) {
1358
condition = new(tree.Condition)('or', condition || a, b, index);
1360
return condition || a;
1363
condition: function () {
1364
var a, b, c, op, index = i, negate = false;
1366
if ($(/^not/)) { negate = true }
1368
if (a = $(this.addition) || $(this.entities.keyword) || $(this.entities.quoted)) {
1369
if (op = $(/^(?:>=|=<|[<=>])/)) {
1370
if (b = $(this.addition) || $(this.entities.keyword) || $(this.entities.quoted)) {
1371
c = new(tree.Condition)(op, a, b, index, negate);
1373
error('expected expression');
1376
c = new(tree.Condition)('=', a, new(tree.Keyword)('true'), index, negate);
1379
return $(/^and/) ? new(tree.Condition)('and', c, $(this.condition)) : c;
1384
// An operand is anything that can be part of an operation,
1385
// such as a Color, or a Variable
1387
operand: function () {
1388
var negate, p = input.charAt(i + 1);
1390
if (input.charAt(i) === '-' && (p === '@' || p === '(')) { negate = $('-') }
1391
var o = $(this.sub) || $(this.entities.dimension) ||
1392
$(this.entities.color) || $(this.entities.variable) ||
1393
$(this.entities.call);
1394
return negate ? new(tree.Operation)('*', [new(tree.Dimension)(-1), o])
1399
// Expressions either represent mathematical operations,
1400
// or white-space delimited Entities.
1405
expression: function () {
1406
var e, delim, entities = [], d;
1408
while (e = $(this.addition) || $(this.entity)) {
1411
if (entities.length > 0) {
1412
return new(tree.Expression)(entities);
1415
property: function () {
1418
if (name = $(/^(\*?-?[-a-z_0-9]+)\s*:/)) {
1426
if (less.mode === 'browser' || less.mode === 'rhino') {
1428
// Used by `@import` directives
1430
less.Parser.importer = function (path, paths, callback, env) {
1431
if (!/^([a-z]+:)?\//.test(path) && paths.length > 0) {
1432
path = paths[0] + path;
1434
// We pass `true` as 3rd argument, to force the reload of the import.
1435
// This is so we can get the syntax tree as opposed to just the CSS output,
1436
// as we need this to evaluate the current stylesheet.
1437
loadStyleSheet({ href: path, title: path, type: env.mime }, function (e) {
1438
if (e && typeof(env.errback) === "function") {
1439
env.errback.call(null, path, paths, callback, env);
1441
callback.apply(null, arguments);
1450
rgb: function (r, g, b) {
1451
return this.rgba(r, g, b, 1.0);
1453
rgba: function (r, g, b, a) {
1454
var rgb = [r, g, b].map(function (c) { return number(c) }),
1456
return new(tree.Color)(rgb, a);
1458
hsl: function (h, s, l) {
1459
return this.hsla(h, s, l, 1.0);
1461
hsla: function (h, s, l, a) {
1462
h = (number(h) % 360) / 360;
1463
s = number(s); l = number(l); a = number(a);
1465
var m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s;
1466
var m1 = l * 2 - m2;
1468
return this.rgba(hue(h + 1/3) * 255,
1474
h = h < 0 ? h + 1 : (h > 1 ? h - 1 : h);
1475
if (h * 6 < 1) return m1 + (m2 - m1) * h * 6;
1476
else if (h * 2 < 1) return m2;
1477
else if (h * 3 < 2) return m1 + (m2 - m1) * (2/3 - h) * 6;
1481
hue: function (color) {
1482
return new(tree.Dimension)(Math.round(color.toHSL().h));
1484
saturation: function (color) {
1485
return new(tree.Dimension)(Math.round(color.toHSL().s * 100), '%');
1487
lightness: function (color) {
1488
return new(tree.Dimension)(Math.round(color.toHSL().l * 100), '%');
1490
alpha: function (color) {
1491
return new(tree.Dimension)(color.toHSL().a);
1493
saturate: function (color, amount) {
1494
var hsl = color.toHSL();
1496
hsl.s += amount.value / 100;
1497
hsl.s = clamp(hsl.s);
1500
desaturate: function (color, amount) {
1501
var hsl = color.toHSL();
1503
hsl.s -= amount.value / 100;
1504
hsl.s = clamp(hsl.s);
1507
lighten: function (color, amount) {
1508
var hsl = color.toHSL();
1510
hsl.l += amount.value / 100;
1511
hsl.l = clamp(hsl.l);
1514
darken: function (color, amount) {
1515
var hsl = color.toHSL();
1517
hsl.l -= amount.value / 100;
1518
hsl.l = clamp(hsl.l);
1521
fadein: function (color, amount) {
1522
var hsl = color.toHSL();
1524
hsl.a += amount.value / 100;
1525
hsl.a = clamp(hsl.a);
1528
fadeout: function (color, amount) {
1529
var hsl = color.toHSL();
1531
hsl.a -= amount.value / 100;
1532
hsl.a = clamp(hsl.a);
1535
fade: function (color, amount) {
1536
var hsl = color.toHSL();
1538
hsl.a = amount.value / 100;
1539
hsl.a = clamp(hsl.a);
1542
spin: function (color, amount) {
1543
var hsl = color.toHSL();
1544
var hue = (hsl.h + amount.value) % 360;
1546
hsl.h = hue < 0 ? 360 + hue : hue;
1551
// Copyright (c) 2006-2009 Hampton Catlin, Nathan Weizenbaum, and Chris Eppstein
1552
// http://sass-lang.com
1554
mix: function (color1, color2, weight) {
1555
var p = weight.value / 100.0;
1557
var a = color1.toHSL().a - color2.toHSL().a;
1559
var w1 = (((w * a == -1) ? w : (w + a) / (1 + w * a)) + 1) / 2.0;
1562
var rgb = [color1.rgb[0] * w1 + color2.rgb[0] * w2,
1563
color1.rgb[1] * w1 + color2.rgb[1] * w2,
1564
color1.rgb[2] * w1 + color2.rgb[2] * w2];
1566
var alpha = color1.alpha * p + color2.alpha * (1 - p);
1568
return new(tree.Color)(rgb, alpha);
1570
greyscale: function (color) {
1571
return this.desaturate(color, new(tree.Dimension)(100));
1574
return new(tree.Anonymous)(str instanceof tree.JavaScript ? str.evaluated : str);
1576
escape: function (str) {
1577
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"));
1579
'%': function (quoted /* arg, arg, ...*/) {
1580
var args = Array.prototype.slice.call(arguments, 1),
1583
for (var i = 0; i < args.length; i++) {
1584
str = str.replace(/%[sda]/i, function(token) {
1585
var value = token.match(/s/i) ? args[i].value : args[i].toCSS();
1586
return token.match(/[A-Z]$/) ? encodeURIComponent(value) : value;
1589
str = str.replace(/%%/g, '%');
1590
return new(tree.Quoted)('"' + str + '"', str);
1592
round: function (n) {
1593
return this._math('round', n);
1595
ceil: function (n) {
1596
return this._math('ceil', n);
1598
floor: function (n) {
1599
return this._math('floor', n);
1601
_math: function (fn, n) {
1602
if (n instanceof tree.Dimension) {
1603
return new(tree.Dimension)(Math[fn](number(n)), n.unit);
1604
} else if (typeof(n) === 'number') {
1607
throw { type: "Argument", message: "argument must be a number" };
1610
argb: function (color) {
1611
return new(tree.Anonymous)(color.toARGB());
1614
percentage: function (n) {
1615
return new(tree.Dimension)(n.value * 100, '%');
1617
color: function (n) {
1618
if (n instanceof tree.Quoted) {
1619
return new(tree.Color)(n.value.slice(1));
1621
throw { type: "Argument", message: "argument must be a string" };
1624
iscolor: function (n) {
1625
return this._isa(n, tree.Color);
1627
isnumber: function (n) {
1628
return this._isa(n, tree.Dimension);
1630
isstring: function (n) {
1631
return this._isa(n, tree.Quoted);
1633
iskeyword: function (n) {
1634
return this._isa(n, tree.Keyword);
1636
isurl: function (n) {
1637
return this._isa(n, tree.URL);
1639
ispixel: function (n) {
1640
return (n instanceof tree.Dimension) && n.unit === 'px' ? tree.True : tree.False;
1642
ispercentage: function (n) {
1643
return (n instanceof tree.Dimension) && n.unit === '%' ? tree.True : tree.False;
1645
isem: function (n) {
1646
return (n instanceof tree.Dimension) && n.unit === 'em' ? tree.True : tree.False;
1648
_isa: function (n, Type) {
1649
return (n instanceof Type) ? tree.True : tree.False;
1653
function hsla(hsla) {
1654
return tree.functions.hsla(hsla.h, hsla.s, hsla.l, hsla.a);
1657
function number(n) {
1658
if (n instanceof tree.Dimension) {
1659
return parseFloat(n.unit == '%' ? n.value / 100 : n.value);
1660
} else if (typeof(n) === 'number') {
1664
error: "RuntimeError",
1665
message: "color functions take numbers as parameters"
1670
function clamp(val) {
1671
return Math.min(1, Math.max(0, val));
1674
})(require('./tree'));
1677
'aliceblue':'#f0f8ff',
1678
'antiquewhite':'#faebd7',
1680
'aquamarine':'#7fffd4',
1685
'blanchedalmond':'#ffebcd',
1687
'blueviolet':'#8a2be2',
1689
'burlywood':'#deb887',
1690
'cadetblue':'#5f9ea0',
1691
'chartreuse':'#7fff00',
1692
'chocolate':'#d2691e',
1694
'cornflowerblue':'#6495ed',
1695
'cornsilk':'#fff8dc',
1696
'crimson':'#dc143c',
1698
'darkblue':'#00008b',
1699
'darkcyan':'#008b8b',
1700
'darkgoldenrod':'#b8860b',
1701
'darkgray':'#a9a9a9',
1702
'darkgrey':'#a9a9a9',
1703
'darkgreen':'#006400',
1704
'darkkhaki':'#bdb76b',
1705
'darkmagenta':'#8b008b',
1706
'darkolivegreen':'#556b2f',
1707
'darkorange':'#ff8c00',
1708
'darkorchid':'#9932cc',
1709
'darkred':'#8b0000',
1710
'darksalmon':'#e9967a',
1711
'darkseagreen':'#8fbc8f',
1712
'darkslateblue':'#483d8b',
1713
'darkslategray':'#2f4f4f',
1714
'darkslategrey':'#2f4f4f',
1715
'darkturquoise':'#00ced1',
1716
'darkviolet':'#9400d3',
1717
'deeppink':'#ff1493',
1718
'deepskyblue':'#00bfff',
1719
'dimgray':'#696969',
1720
'dimgrey':'#696969',
1721
'dodgerblue':'#1e90ff',
1722
'firebrick':'#b22222',
1723
'floralwhite':'#fffaf0',
1724
'forestgreen':'#228b22',
1725
'fuchsia':'#ff00ff',
1726
'gainsboro':'#dcdcdc',
1727
'ghostwhite':'#f8f8ff',
1729
'goldenrod':'#daa520',
1733
'greenyellow':'#adff2f',
1734
'honeydew':'#f0fff0',
1735
'hotpink':'#ff69b4',
1736
'indianred':'#cd5c5c',
1740
'lavender':'#e6e6fa',
1741
'lavenderblush':'#fff0f5',
1742
'lawngreen':'#7cfc00',
1743
'lemonchiffon':'#fffacd',
1744
'lightblue':'#add8e6',
1745
'lightcoral':'#f08080',
1746
'lightcyan':'#e0ffff',
1747
'lightgoldenrodyellow':'#fafad2',
1748
'lightgray':'#d3d3d3',
1749
'lightgrey':'#d3d3d3',
1750
'lightgreen':'#90ee90',
1751
'lightpink':'#ffb6c1',
1752
'lightsalmon':'#ffa07a',
1753
'lightseagreen':'#20b2aa',
1754
'lightskyblue':'#87cefa',
1755
'lightslategray':'#778899',
1756
'lightslategrey':'#778899',
1757
'lightsteelblue':'#b0c4de',
1758
'lightyellow':'#ffffe0',
1760
'limegreen':'#32cd32',
1762
'magenta':'#ff00ff',
1764
'mediumaquamarine':'#66cdaa',
1765
'mediumblue':'#0000cd',
1766
'mediumorchid':'#ba55d3',
1767
'mediumpurple':'#9370d8',
1768
'mediumseagreen':'#3cb371',
1769
'mediumslateblue':'#7b68ee',
1770
'mediumspringgreen':'#00fa9a',
1771
'mediumturquoise':'#48d1cc',
1772
'mediumvioletred':'#c71585',
1773
'midnightblue':'#191970',
1774
'mintcream':'#f5fffa',
1775
'mistyrose':'#ffe4e1',
1776
'moccasin':'#ffe4b5',
1777
'navajowhite':'#ffdead',
1779
'oldlace':'#fdf5e6',
1781
'olivedrab':'#6b8e23',
1783
'orangered':'#ff4500',
1785
'palegoldenrod':'#eee8aa',
1786
'palegreen':'#98fb98',
1787
'paleturquoise':'#afeeee',
1788
'palevioletred':'#d87093',
1789
'papayawhip':'#ffefd5',
1790
'peachpuff':'#ffdab9',
1794
'powderblue':'#b0e0e6',
1797
'rosybrown':'#bc8f8f',
1798
'royalblue':'#4169e1',
1799
'saddlebrown':'#8b4513',
1801
'sandybrown':'#f4a460',
1802
'seagreen':'#2e8b57',
1803
'seashell':'#fff5ee',
1806
'skyblue':'#87ceeb',
1807
'slateblue':'#6a5acd',
1808
'slategray':'#708090',
1809
'slategrey':'#708090',
1811
'springgreen':'#00ff7f',
1812
'steelblue':'#4682b4',
1815
'thistle':'#d8bfd8',
1817
'turquoise':'#40e0d0',
1821
'whitesmoke':'#f5f5f5',
1823
'yellowgreen':'#9acd32'
1825
})(require('./tree'));
1828
tree.Alpha = function (val) {
1831
tree.Alpha.prototype = {
1832
toCSS: function () {
1833
return "alpha(opacity=" +
1834
(this.value.toCSS ? this.value.toCSS() : this.value) + ")";
1836
eval: function (env) {
1837
if (this.value.eval) { this.value = this.value.eval(env) }
1842
})(require('../tree'));
1845
tree.Anonymous = function (string) {
1846
this.value = string.value || string;
1848
tree.Anonymous.prototype = {
1849
toCSS: function () {
1852
eval: function () { return this }
1855
})(require('../tree'));
1858
tree.Assignment = function (key, val) {
1862
tree.Assignment.prototype = {
1863
toCSS: function () {
1864
return this.key + '=' + (this.value.toCSS ? this.value.toCSS() : this.value);
1866
eval: function (env) {
1867
if (this.value.eval) { this.value = this.value.eval(env) }
1872
})(require('../tree'));(function (tree) {
1875
// A function call node.
1877
tree.Call = function (name, args, index, filename) {
1881
this.filename = filename;
1883
tree.Call.prototype = {
1885
// When evaluating a function call,
1886
// we either find the function in `tree.functions` [1],
1887
// in which case we call it, passing the evaluated arguments,
1888
// or we simply print it out as it appeared originally [2].
1890
// The *functions.js* file contains the built-in functions.
1892
// The reason why we evaluate the arguments, is in the case where
1893
// we try to pass a variable to a function, like: `saturate(@color)`.
1894
// The function should receive the value, not the variable.
1896
eval: function (env) {
1897
var args = this.args.map(function (a) { return a.eval(env) });
1899
if (this.name in tree.functions) { // 1.
1901
return tree.functions[this.name].apply(tree.functions, args);
1903
throw { type: e.type || "Runtime",
1904
message: "error evaluating function `" + this.name + "`" +
1905
(e.message ? ': ' + e.message : ''),
1906
index: this.index, filename: this.filename };
1909
return new(tree.Anonymous)(this.name +
1910
"(" + args.map(function (a) { return a.toCSS() }).join(', ') + ")");
1914
toCSS: function (env) {
1915
return this.eval(env).toCSS();
1919
})(require('../tree'));
1922
// RGB Colors - #ff0014, #eee
1924
tree.Color = function (rgb, a) {
1926
// The end goal here, is to parse the arguments
1927
// into an integer triplet, such as `128, 255, 0`
1929
// This facilitates operations and conversions.
1931
if (Array.isArray(rgb)) {
1933
} else if (rgb.length == 6) {
1934
this.rgb = rgb.match(/.{2}/g).map(function (c) {
1935
return parseInt(c, 16);
1938
this.rgb = rgb.split('').map(function (c) {
1939
return parseInt(c + c, 16);
1942
this.alpha = typeof(a) === 'number' ? a : 1;
1944
tree.Color.prototype = {
1945
eval: function () { return this },
1948
// If we have some transparency, the only way to represent it
1949
// is via `rgba`. Otherwise, we use the hex representation,
1950
// which has better compatibility with older browsers.
1951
// Values are capped between `0` and `255`, rounded and zero-padded.
1953
toCSS: function () {
1954
if (this.alpha < 1.0) {
1955
return "rgba(" + this.rgb.map(function (c) {
1956
return Math.round(c);
1957
}).concat(this.alpha).join(', ') + ")";
1959
return '#' + this.rgb.map(function (i) {
1961
i = (i > 255 ? 255 : (i < 0 ? 0 : i)).toString(16);
1962
return i.length === 1 ? '0' + i : i;
1968
// Operations have to be done per-channel, if not,
1969
// channels will spill onto each other. Once we have
1970
// our result, in the form of an integer triplet,
1971
// we create a new Color node to hold the result.
1973
operate: function (op, other) {
1976
if (! (other instanceof tree.Color)) {
1977
other = other.toColor();
1980
for (var c = 0; c < 3; c++) {
1981
result[c] = tree.operate(op, this.rgb[c], other.rgb[c]);
1983
return new(tree.Color)(result, this.alpha + other.alpha);
1986
toHSL: function () {
1987
var r = this.rgb[0] / 255,
1988
g = this.rgb[1] / 255,
1989
b = this.rgb[2] / 255,
1992
var max = Math.max(r, g, b), min = Math.min(r, g, b);
1993
var h, s, l = (max + min) / 2, d = max - min;
1998
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
2001
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
2002
case g: h = (b - r) / d + 2; break;
2003
case b: h = (r - g) / d + 4; break;
2007
return { h: h * 360, s: s, l: l, a: a };
2009
toARGB: function () {
2010
var argb = [Math.round(this.alpha * 255)].concat(this.rgb);
2011
return '#' + argb.map(function (i) {
2013
i = (i > 255 ? 255 : (i < 0 ? 0 : i)).toString(16);
2014
return i.length === 1 ? '0' + i : i;
2020
})(require('../tree'));
2023
tree.Comment = function (value, silent) {
2025
this.silent = !!silent;
2027
tree.Comment.prototype = {
2028
toCSS: function (env) {
2029
return env.compress ? '' : this.value;
2031
eval: function () { return this }
2034
})(require('../tree'));
2037
tree.Condition = function (op, l, r, i, negate) {
2038
this.op = op.trim();
2042
this.negate = negate;
2044
tree.Condition.prototype.eval = function (env) {
2045
var a = this.lvalue.eval(env),
2046
b = this.rvalue.eval(env);
2048
var i = this.index, result;
2050
var result = (function (op) {
2058
result = a.compare(b);
2059
} else if (b.compare) {
2060
result = b.compare(a);
2062
throw { type: "Type",
2063
message: "Unable to perform comparison",
2067
case -1: return op === '<' || op === '=<';
2068
case 0: return op === '=' || op === '>=' || op === '=<';
2069
case 1: return op === '>' || op === '>=';
2073
return this.negate ? !result : result;
2076
})(require('../tree'));
2080
// A number with a unit
2082
tree.Dimension = function (value, unit) {
2083
this.value = parseFloat(value);
2084
this.unit = unit || null;
2087
tree.Dimension.prototype = {
2088
eval: function () { return this },
2089
toColor: function () {
2090
return new(tree.Color)([this.value, this.value, this.value]);
2092
toCSS: function () {
2093
var css = this.value + this.unit;
2097
// In an operation between two Dimensions,
2098
// we default to the first Dimension's unit,
2099
// so `1px + 2em` will yield `3px`.
2100
// In the future, we could implement some unit
2101
// conversions such that `100cm + 10mm` would yield
2103
operate: function (op, other) {
2104
return new(tree.Dimension)
2105
(tree.operate(op, this.value, other.value),
2106
this.unit || other.unit);
2109
// TODO: Perform unit conversion before comparing
2110
compare: function (other) {
2111
if (other instanceof tree.Dimension) {
2112
if (other.value > this.value) {
2114
} else if (other.value < this.value) {
2125
})(require('../tree'));
2128
tree.Directive = function (name, value, features) {
2131
if (Array.isArray(value)) {
2132
this.ruleset = new(tree.Ruleset)([], value);
2133
this.ruleset.allowImports = true;
2138
tree.Directive.prototype = {
2139
toCSS: function (ctx, env) {
2141
this.ruleset.root = true;
2142
return this.name + (env.compress ? '{' : ' {\n ') +
2143
this.ruleset.toCSS(ctx, env).trim().replace(/\n/g, '\n ') +
2144
(env.compress ? '}': '\n}\n');
2146
return this.name + ' ' + this.value.toCSS() + ';\n';
2149
eval: function (env) {
2150
env.frames.unshift(this);
2151
this.ruleset = this.ruleset && this.ruleset.eval(env);
2155
variable: function (name) { return tree.Ruleset.prototype.variable.call(this.ruleset, name) },
2156
find: function () { return tree.Ruleset.prototype.find.apply(this.ruleset, arguments) },
2157
rulesets: function () { return tree.Ruleset.prototype.rulesets.apply(this.ruleset) }
2160
})(require('../tree'));
2163
tree.Element = function (combinator, value, index) {
2164
this.combinator = combinator instanceof tree.Combinator ?
2165
combinator : new(tree.Combinator)(combinator);
2167
if (typeof(value) === 'string') {
2168
this.value = value.trim();
2176
tree.Element.prototype.eval = function (env) {
2177
return new(tree.Element)(this.combinator,
2178
this.value.eval ? this.value.eval(env) : this.value,
2181
tree.Element.prototype.toCSS = function (env) {
2182
return this.combinator.toCSS(env || {}) + (this.value.toCSS ? this.value.toCSS(env) : this.value);
2185
tree.Combinator = function (value) {
2186
if (value === ' ') {
2188
} else if (value === '& ') {
2191
this.value = value ? value.trim() : "";
2194
tree.Combinator.prototype.toCSS = function (env) {
2201
'+' : env.compress ? '+' : ' + ',
2202
'~' : env.compress ? '~' : ' ~ ',
2203
'>' : env.compress ? '>' : ' > '
2207
})(require('../tree'));
2210
tree.Expression = function (value) { this.value = value };
2211
tree.Expression.prototype = {
2212
eval: function (env) {
2213
if (this.value.length > 1) {
2214
return new(tree.Expression)(this.value.map(function (e) {
2217
} else if (this.value.length === 1) {
2218
return this.value[0].eval(env);
2223
toCSS: function (env) {
2224
return this.value.map(function (e) {
2225
return e.toCSS ? e.toCSS(env) : '';
2230
})(require('../tree'));
2235
// The general strategy here is that we don't want to wait
2236
// for the parsing to be completed, before we start importing
2237
// the file. That's because in the context of a browser,
2238
// most of the time will be spent waiting for the server to respond.
2240
// On creation, we push the import path to our import queue, though
2241
// `import,push`, we also pass it a callback, which it'll call once
2242
// the file has been fetched, and parsed.
2244
tree.Import = function (path, imports, features, index) {
2249
this.features = features && new(tree.Value)(features);
2251
// The '.less' extension is optional
2252
if (path instanceof tree.Quoted) {
2253
this.path = /\.(le?|c)ss(\?.*)?$/.test(path.value) ? path.value : path.value + '.less';
2255
this.path = path.value.value || path.value;
2258
this.css = /css(\?.*)?$/.test(this.path);
2260
// Only pre-compile .less files
2262
imports.push(this.path, function (e, root) {
2263
if (e) { e.index = index }
2264
that.root = root || new(tree.Ruleset)([], []);
2270
// The actual import node doesn't return anything, when converted to CSS.
2271
// The reason is that it's used at the evaluation stage, so that the rules
2272
// it imports can be treated like any other rules.
2274
// In `eval`, we make sure all Import nodes get evaluated, recursively, so
2275
// we end up with a flat structure, which can easily be imported in the parent
2278
tree.Import.prototype = {
2279
toCSS: function (env) {
2280
var features = this.features ? ' ' + this.features.toCSS(env) : '';
2283
return "@import " + this._path.toCSS() + features + ';\n';
2288
eval: function (env) {
2289
var ruleset, features = this.features && this.features.eval(env);
2294
ruleset = new(tree.Ruleset)([], this.root.rules.slice(0));
2296
for (var i = 0; i < ruleset.rules.length; i++) {
2297
if (ruleset.rules[i] instanceof tree.Import) {
2300
.apply(ruleset.rules,
2301
[i, 1].concat(ruleset.rules[i].eval(env)));
2304
return this.features ? new(tree.Media)(ruleset.rules, this.features.value) : ruleset.rules;
2309
})(require('../tree'));
2312
tree.JavaScript = function (string, index, escaped) {
2313
this.escaped = escaped;
2314
this.expression = string;
2317
tree.JavaScript.prototype = {
2318
eval: function (env) {
2323
var expression = this.expression.replace(/@\{([\w-]+)\}/g, function (_, name) {
2324
return tree.jsify(new(tree.Variable)('@' + name, that.index).eval(env));
2328
expression = new(Function)('return (' + expression + ')');
2330
throw { message: "JavaScript evaluation error: `" + expression + "`" ,
2331
index: this.index };
2334
for (var k in env.frames[0].variables()) {
2335
context[k.slice(1)] = {
2336
value: env.frames[0].variables()[k].value,
2338
return this.value.eval(env).toCSS();
2344
result = expression.call(context);
2346
throw { message: "JavaScript evaluation error: '" + e.name + ': ' + e.message + "'" ,
2347
index: this.index };
2349
if (typeof(result) === 'string') {
2350
return new(tree.Quoted)('"' + result + '"', result, this.escaped, this.index);
2351
} else if (Array.isArray(result)) {
2352
return new(tree.Anonymous)(result.join(', '));
2354
return new(tree.Anonymous)(result);
2359
})(require('../tree'));
2363
tree.Keyword = function (value) { this.value = value };
2364
tree.Keyword.prototype = {
2365
eval: function () { return this },
2366
toCSS: function () { return this.value },
2367
compare: function (other) {
2368
if (other instanceof tree.Keyword) {
2369
return other.value === this.value ? 0 : 1;
2376
tree.True = new(tree.Keyword)('true');
2377
tree.False = new(tree.Keyword)('false');
2379
})(require('../tree'));
2382
tree.Media = function (value, features) {
2383
var el = new(tree.Element)('&', null, 0),
2384
selectors = [new(tree.Selector)([el])];
2386
this.features = new(tree.Value)(features);
2387
this.ruleset = new(tree.Ruleset)(selectors, value);
2388
this.ruleset.allowImports = true;
2390
tree.Media.prototype = {
2391
toCSS: function (ctx, env) {
2392
var features = this.features.toCSS(env);
2394
this.ruleset.root = (ctx.length === 0 || ctx[0].multiMedia);
2395
return '@media ' + features + (env.compress ? '{' : ' {\n ') +
2396
this.ruleset.toCSS(ctx, env).trim().replace(/\n/g, '\n ') +
2397
(env.compress ? '}': '\n}\n');
2399
eval: function (env) {
2400
if (!env.mediaBlocks) {
2401
env.mediaBlocks = [];
2405
var blockIndex = env.mediaBlocks.length;
2406
env.mediaPath.push(this);
2407
env.mediaBlocks.push(this);
2409
var media = new(tree.Media)([], []);
2410
media.features = this.features.eval(env);
2412
env.frames.unshift(this.ruleset);
2413
media.ruleset = this.ruleset.eval(env);
2416
env.mediaBlocks[blockIndex] = media;
2417
env.mediaPath.pop();
2419
return env.mediaPath.length === 0 ? media.evalTop(env) :
2420
media.evalNested(env)
2422
variable: function (name) { return tree.Ruleset.prototype.variable.call(this.ruleset, name) },
2423
find: function () { return tree.Ruleset.prototype.find.apply(this.ruleset, arguments) },
2424
rulesets: function () { return tree.Ruleset.prototype.rulesets.apply(this.ruleset) },
2426
evalTop: function (env) {
2429
// Render all dependent Media blocks.
2430
if (env.mediaBlocks.length > 1) {
2431
var el = new(tree.Element)('&', null, 0);
2432
var selectors = [new(tree.Selector)([el])];
2433
result = new(tree.Ruleset)(selectors, env.mediaBlocks);
2434
result.multiMedia = true;
2437
delete env.mediaBlocks;
2438
delete env.mediaPath;
2442
evalNested: function (env) {
2444
path = env.mediaPath.concat([this]);
2446
// Extract the media-query conditions separated with `,` (OR).
2447
for (i = 0; i < path.length; i++) {
2448
value = path[i].features instanceof tree.Value ?
2449
path[i].features.value : path[i].features;
2450
path[i] = Array.isArray(value) ? value : [value];
2453
// Trace all permutations to generate the resulting media-query.
2455
// (a, b and c) with nested (d, e) ->
2460
this.features = new(tree.Value)(this.permute(path).map(function (path) {
2461
path = path.map(function (fragment) {
2462
return fragment.toCSS ? fragment : new(tree.Anonymous)(fragment);
2465
for(i = path.length - 1; i > 0; i--) {
2466
path.splice(i, 0, new(tree.Anonymous)("and"));
2469
return new(tree.Expression)(path);
2472
// Fake a tree-node that doesn't output anything.
2473
return new(tree.Ruleset)([], []);
2475
permute: function (arr) {
2476
if (arr.length === 0) {
2478
} else if (arr.length === 1) {
2482
var rest = this.permute(arr.slice(1));
2483
for (var i = 0; i < rest.length; i++) {
2484
for (var j = 0; j < arr[0].length; j++) {
2485
result.push([arr[0][j]].concat(rest[i]));
2493
})(require('../tree'));
2497
tree.mixin.Call = function (elements, args, index, filename, important) {
2498
this.selector = new(tree.Selector)(elements);
2499
this.arguments = args;
2501
this.filename = filename;
2502
this.important = important;
2504
tree.mixin.Call.prototype = {
2505
eval: function (env) {
2506
var mixins, args, rules = [], match = false;
2508
for (var i = 0; i < env.frames.length; i++) {
2509
if ((mixins = env.frames[i].find(this.selector)).length > 0) {
2510
args = this.arguments && this.arguments.map(function (a) { return a.eval(env) });
2511
for (var m = 0; m < mixins.length; m++) {
2512
if (mixins[m].match(args, env)) {
2514
Array.prototype.push.apply(
2515
rules, mixins[m].eval(env, this.arguments, this.important).rules);
2518
throw { message: e.message, index: this.index, filename: this.filename, stack: e.stack };
2525
throw { type: 'Runtime',
2526
message: 'No matching definition was found for `' +
2527
this.selector.toCSS().trim() + '(' +
2528
this.arguments.map(function (a) {
2530
}).join(', ') + ")`",
2531
index: this.index, filename: this.filename };
2535
throw { type: 'Name',
2536
message: this.selector.toCSS().trim() + " is undefined",
2537
index: this.index, filename: this.filename };
2541
tree.mixin.Definition = function (name, params, rules, condition, variadic) {
2543
this.selectors = [new(tree.Selector)([new(tree.Element)(null, name)])];
2544
this.params = params;
2545
this.condition = condition;
2546
this.variadic = variadic;
2547
this.arity = params.length;
2550
this.required = params.reduce(function (count, p) {
2551
if (!p.name || (p.name && !p.value)) { return count + 1 }
2552
else { return count }
2554
this.parent = tree.Ruleset.prototype;
2557
tree.mixin.Definition.prototype = {
2558
toCSS: function () { return "" },
2559
variable: function (name) { return this.parent.variable.call(this, name) },
2560
variables: function () { return this.parent.variables.call(this) },
2561
find: function () { return this.parent.find.apply(this, arguments) },
2562
rulesets: function () { return this.parent.rulesets.apply(this) },
2564
evalParams: function (env, args) {
2565
var frame = new(tree.Ruleset)(null, []), varargs;
2567
for (var i = 0, val, name; i < this.params.length; i++) {
2568
if (name = this.params[i].name) {
2569
if (this.params[i].variadic && args) {
2571
for (var j = i; j < args.length; j++) {
2572
varargs.push(args[j].eval(env));
2574
frame.rules.unshift(new(tree.Rule)(name, new(tree.Expression)(varargs).eval(env)));
2575
} else if (val = (args && args[i]) || this.params[i].value) {
2576
frame.rules.unshift(new(tree.Rule)(name, val.eval(env)));
2578
throw { type: 'Runtime', message: "wrong number of arguments for " + this.name +
2579
' (' + args.length + ' for ' + this.arity + ')' };
2585
eval: function (env, args, important) {
2586
var frame = this.evalParams(env, args), context, _arguments = [], rules, start;
2588
for (var i = 0; i < Math.max(this.params.length, args && args.length); i++) {
2589
_arguments.push(args[i] || this.params[i].value);
2591
frame.rules.unshift(new(tree.Rule)('@arguments', new(tree.Expression)(_arguments).eval(env)));
2594
this.rules.map(function (r) {
2595
return new(tree.Rule)(r.name, r.value, '!important', r.index);
2596
}) : this.rules.slice(0);
2598
return new(tree.Ruleset)(null, rules).eval({
2599
frames: [this, frame].concat(this.frames, env.frames)
2602
match: function (args, env) {
2603
var argsLength = (args && args.length) || 0, len, frame;
2605
if (! this.variadic) {
2606
if (argsLength < this.required) { return false }
2607
if (argsLength > this.params.length) { return false }
2608
if ((this.required > 0) && (argsLength > this.params.length)) { return false }
2611
if (this.condition && !this.condition.eval({
2612
frames: [this.evalParams(env, args)].concat(env.frames)
2613
})) { return false }
2615
len = Math.min(argsLength, this.arity);
2617
for (var i = 0; i < len; i++) {
2618
if (!this.params[i].name) {
2619
if (args[i].eval(env).toCSS() != this.params[i].value.eval(env).toCSS()) {
2628
})(require('../tree'));
2631
tree.Operation = function (op, operands) {
2632
this.op = op.trim();
2633
this.operands = operands;
2635
tree.Operation.prototype.eval = function (env) {
2636
var a = this.operands[0].eval(env),
2637
b = this.operands[1].eval(env),
2640
if (a instanceof tree.Dimension && b instanceof tree.Color) {
2641
if (this.op === '*' || this.op === '+') {
2642
temp = b, b = a, a = temp;
2644
throw { name: "OperationError",
2645
message: "Can't substract or divide a color from a number" };
2648
return a.operate(this.op, b);
2651
tree.operate = function (op, a, b) {
2653
case '+': return a + b;
2654
case '-': return a - b;
2655
case '*': return a * b;
2656
case '/': return a / b;
2660
})(require('../tree'));
2664
tree.Paren = function (node) {
2667
tree.Paren.prototype = {
2668
toCSS: function (env) {
2669
return '(' + this.value.toCSS(env) + ')';
2671
eval: function (env) {
2672
return new(tree.Paren)(this.value.eval(env));
2676
})(require('../tree'));
2679
tree.Quoted = function (str, content, escaped, i) {
2680
this.escaped = escaped;
2681
this.value = content || '';
2682
this.quote = str.charAt(0);
2685
tree.Quoted.prototype = {
2686
toCSS: function () {
2690
return this.quote + this.value + this.quote;
2693
eval: function (env) {
2695
var value = this.value.replace(/`([^`]+)`/g, function (_, exp) {
2696
return new(tree.JavaScript)(exp, that.index, true).eval(env).value;
2697
}).replace(/@\{([\w-]+)\}/g, function (_, name) {
2698
var v = new(tree.Variable)('@' + name, that.index).eval(env);
2699
return ('value' in v) ? v.value : v.toCSS();
2701
return new(tree.Quoted)(this.quote + value + this.quote, value, this.escaped, this.index);
2705
})(require('../tree'));
2708
tree.Rule = function (name, value, important, index, inline) {
2710
this.value = (value instanceof tree.Value) ? value : new(tree.Value)([value]);
2711
this.important = important ? ' ' + important.trim() : '';
2713
this.inline = inline || false;
2715
if (name.charAt(0) === '@') {
2716
this.variable = true;
2717
} else { this.variable = false }
2719
tree.Rule.prototype.toCSS = function (env) {
2720
if (this.variable) { return "" }
2722
return this.name + (env.compress ? ':' : ': ') +
2723
this.value.toCSS(env) +
2724
this.important + (this.inline ? "" : ";");
2728
tree.Rule.prototype.eval = function (context) {
2729
return new(tree.Rule)(this.name,
2730
this.value.eval(context),
2732
this.index, this.inline);
2735
tree.Shorthand = function (a, b) {
2740
tree.Shorthand.prototype = {
2741
toCSS: function (env) {
2742
return this.a.toCSS(env) + "/" + this.b.toCSS(env);
2744
eval: function () { return this }
2747
})(require('../tree'));
2750
tree.Ruleset = function (selectors, rules, strictImports) {
2751
this.selectors = selectors;
2754
this.strictImports = strictImports;
2756
tree.Ruleset.prototype = {
2757
eval: function (env) {
2758
var selectors = this.selectors && this.selectors.map(function (s) { return s.eval(env) });
2759
var ruleset = new(tree.Ruleset)(selectors, this.rules.slice(0), this.strictImports);
2761
ruleset.root = this.root;
2762
ruleset.allowImports = this.allowImports;
2764
// push the current ruleset to the frames stack
2765
env.frames.unshift(ruleset);
2768
if (ruleset.root || ruleset.allowImports || !ruleset.strictImports) {
2769
for (var i = 0; i < ruleset.rules.length; i++) {
2770
if (ruleset.rules[i] instanceof tree.Import) {
2771
Array.prototype.splice
2772
.apply(ruleset.rules, [i, 1].concat(ruleset.rules[i].eval(env)));
2777
// Store the frames around mixin definitions,
2778
// so they can be evaluated like closures when the time comes.
2779
for (var i = 0; i < ruleset.rules.length; i++) {
2780
if (ruleset.rules[i] instanceof tree.mixin.Definition) {
2781
ruleset.rules[i].frames = env.frames.slice(0);
2785
// Evaluate mixin calls.
2786
for (var i = 0; i < ruleset.rules.length; i++) {
2787
if (ruleset.rules[i] instanceof tree.mixin.Call) {
2788
Array.prototype.splice
2789
.apply(ruleset.rules, [i, 1].concat(ruleset.rules[i].eval(env)));
2793
// Evaluate everything else
2794
for (var i = 0, rule; i < ruleset.rules.length; i++) {
2795
rule = ruleset.rules[i];
2797
if (! (rule instanceof tree.mixin.Definition)) {
2798
ruleset.rules[i] = rule.eval ? rule.eval(env) : rule;
2807
match: function (args) {
2808
return !args || args.length === 0;
2810
variables: function () {
2811
if (this._variables) { return this._variables }
2813
return this._variables = this.rules.reduce(function (hash, r) {
2814
if (r instanceof tree.Rule && r.variable === true) {
2821
variable: function (name) {
2822
return this.variables()[name];
2824
rulesets: function () {
2825
if (this._rulesets) { return this._rulesets }
2827
return this._rulesets = this.rules.filter(function (r) {
2828
return (r instanceof tree.Ruleset) || (r instanceof tree.mixin.Definition);
2832
find: function (selector, self) {
2833
self = self || this;
2834
var rules = [], rule, match,
2835
key = selector.toCSS();
2837
if (key in this._lookups) { return this._lookups[key] }
2839
this.rulesets().forEach(function (rule) {
2840
if (rule !== self) {
2841
for (var j = 0; j < rule.selectors.length; j++) {
2842
if (match = selector.match(rule.selectors[j])) {
2843
if (selector.elements.length > rule.selectors[j].elements.length) {
2844
Array.prototype.push.apply(rules, rule.find(
2845
new(tree.Selector)(selector.elements.slice(1)), self));
2854
return this._lookups[key] = rules;
2857
// Entry point for code generation
2859
// `context` holds an array of arrays.
2861
toCSS: function (context, env) {
2862
var css = [], // The CSS output
2863
rules = [], // node.Rule instances
2864
rulesets = [], // node.Ruleset instances
2865
paths = [], // Current selectors
2866
selector, // The fully rendered selector
2870
if (context.length === 0) {
2871
paths = this.selectors.map(function (s) { return [s] });
2873
this.joinSelectors(paths, context, this.selectors);
2877
// Compile rules and rulesets
2878
for (var i = 0; i < this.rules.length; i++) {
2879
rule = this.rules[i];
2881
if (rule.rules || (rule instanceof tree.Directive) || (rule instanceof tree.Media)) {
2882
rulesets.push(rule.toCSS(paths, env));
2883
} else if (rule instanceof tree.Comment) {
2886
rulesets.push(rule.toCSS(env));
2888
rules.push(rule.toCSS(env));
2892
if (rule.toCSS && !rule.variable) {
2893
rules.push(rule.toCSS(env));
2894
} else if (rule.value && !rule.variable) {
2895
rules.push(rule.value.toString());
2900
rulesets = rulesets.join('');
2902
// If this is the root node, we don't render
2903
// a selector, or {}.
2904
// Otherwise, only output if this ruleset has rules.
2906
css.push(rules.join(env.compress ? '' : '\n'));
2908
if (rules.length > 0) {
2909
selector = paths.map(function (p) {
2910
return p.map(function (s) {
2911
return s.toCSS(env);
2913
}).join( env.compress ? ',' : ',\n');
2916
(env.compress ? '{' : ' {\n ') +
2917
rules.join(env.compress ? '' : '\n ') +
2918
(env.compress ? '}' : '\n}\n'));
2923
return css.join('') + (env.compress ? '\n' : '');
2926
joinSelectors: function (paths, context, selectors) {
2927
for (var s = 0; s < selectors.length; s++) {
2928
this.joinSelector(paths, context, selectors[s]);
2932
joinSelector: function (paths, context, selector) {
2933
var before = [], after = [], beforeElements = [],
2934
afterElements = [], hasParentSelector = false, el;
2936
for (var i = 0; i < selector.elements.length; i++) {
2937
el = selector.elements[i];
2938
if (el.combinator.value.charAt(0) === '&') {
2939
hasParentSelector = true;
2941
if (hasParentSelector) afterElements.push(el);
2942
else beforeElements.push(el);
2945
if (! hasParentSelector) {
2946
afterElements = beforeElements;
2947
beforeElements = [];
2950
if (beforeElements.length > 0) {
2951
before.push(new(tree.Selector)(beforeElements));
2954
if (afterElements.length > 0) {
2955
after.push(new(tree.Selector)(afterElements));
2958
for (var c = 0; c < context.length; c++) {
2959
paths.push(before.concat(context[c]).concat(after));
2963
})(require('../tree'));
2966
tree.Selector = function (elements) {
2967
this.elements = elements;
2968
if (this.elements[0].combinator.value === "") {
2969
this.elements[0].combinator.value = ' ';
2972
tree.Selector.prototype.match = function (other) {
2973
var len = this.elements.length,
2974
olen = other.elements.length,
2975
max = Math.min(len, olen);
2980
for (var i = 0; i < max; i++) {
2981
if (this.elements[i].value !== other.elements[i].value) {
2988
tree.Selector.prototype.eval = function (env) {
2989
return new(tree.Selector)(this.elements.map(function (e) {
2993
tree.Selector.prototype.toCSS = function (env) {
2994
if (this._css) { return this._css }
2996
return this._css = this.elements.map(function (e) {
2997
if (typeof(e) === 'string') {
2998
return ' ' + e.trim();
3000
return e.toCSS(env);
3005
})(require('../tree'));
3008
tree.URL = function (val, paths) {
3012
// Add the base path if the URL is relative and we are in the browser
3013
if (typeof(window) !== 'undefined' && !/^(?:https?:\/\/|file:\/\/|data:|\/)/.test(val.value) && paths.length > 0) {
3014
val.value = paths[0] + (val.value.charAt(0) === '/' ? val.value.slice(1) : val.value);
3020
tree.URL.prototype = {
3021
toCSS: function () {
3022
return "url(" + (this.attrs ? 'data:' + this.attrs.mime + this.attrs.charset + this.attrs.base64 + this.attrs.data
3023
: this.value.toCSS()) + ")";
3025
eval: function (ctx) {
3026
return this.attrs ? this : new(tree.URL)(this.value.eval(ctx), this.paths);
3030
})(require('../tree'));
3033
tree.Value = function (value) {
3037
tree.Value.prototype = {
3038
eval: function (env) {
3039
if (this.value.length === 1) {
3040
return this.value[0].eval(env);
3042
return new(tree.Value)(this.value.map(function (v) {
3047
toCSS: function (env) {
3048
return this.value.map(function (e) {
3049
return e.toCSS(env);
3050
}).join(env.compress ? ',' : ', ');
3054
})(require('../tree'));
3057
tree.Variable = function (name, index, file) { this.name = name, this.index = index, this.file = file };
3058
tree.Variable.prototype = {
3059
eval: function (env) {
3060
var variable, v, name = this.name;
3062
if (name.indexOf('@@') == 0) {
3063
name = '@' + new(tree.Variable)(name.slice(1)).eval(env).value;
3066
if (variable = tree.find(env.frames, function (frame) {
3067
if (v = frame.variable(name)) {
3068
return v.value.eval(env);
3070
})) { return variable }
3072
throw { type: 'Name',
3073
message: "variable " + name + " is undefined",
3074
filename: this.file,
3075
index: this.index };
3080
})(require('../tree'));
3083
tree.find = function (obj, fun) {
3084
for (var i = 0, r; i < obj.length; i++) {
3085
if (r = fun.call(obj, obj[i])) { return r }
3089
tree.jsify = function (obj) {
3090
if (Array.isArray(obj.value) && (obj.value.length > 1)) {
3091
return '[' + obj.value.map(function (v) { return v.toCSS(false) }).join(', ') + ']';
3093
return obj.toCSS(false);
3097
})(require('./tree'));
3099
// browser.js - client-side engine
3102
var isFileProtocol = (location.protocol === 'file:' ||
3103
location.protocol === 'chrome:' ||
3104
location.protocol === 'chrome-extension:' ||
3105
location.protocol === 'resource:');
3107
less.env = less.env || (location.hostname == '127.0.0.1' ||
3108
location.hostname == '0.0.0.0' ||
3109
location.hostname == 'localhost' ||
3110
location.port.length > 0 ||
3111
isFileProtocol ? 'development'
3114
// Load styles asynchronously (default: false)
3116
// This is set to `false` by default, so that the body
3117
// doesn't start loading before the stylesheets are parsed.
3118
// Setting this to `true` can result in flickering.
3122
// Interval between watch polls
3123
less.poll = less.poll || (isFileProtocol ? 1000 : 1500);
3128
less.watch = function () { return this.watchMode = true };
3129
less.unwatch = function () { return this.watchMode = false };
3131
if (less.env === 'development') {
3132
less.optimization = 0;
3134
if (/!watch/.test(location.hash)) {
3137
less.watchTimer = setInterval(function () {
3138
if (less.watchMode) {
3139
loadStyleSheets(function (e, root, _, sheet, env) {
3141
createCSS(root.toCSS(), sheet, env.lastModified);
3147
less.optimization = 3;
3153
cache = (typeof(window.localStorage) === 'undefined') ? null : window.localStorage;
3159
// Get all <link> tags with the 'rel' attribute set to "stylesheet/less"
3161
var links = document.getElementsByTagName('link');
3162
var typePattern = /^text\/(x-)?less$/;
3166
for (var i = 0; i < links.length; i++) {
3167
if (links[i].rel === 'stylesheet/less' || (links[i].rel.match(/stylesheet/) &&
3168
(links[i].type.match(typePattern)))) {
3169
less.sheets.push(links[i]);
3174
less.refresh = function (reload) {
3175
var startTime, endTime;
3176
startTime = endTime = new(Date);
3178
loadStyleSheets(function (e, root, _, sheet, env) {
3180
log("loading " + sheet.href + " from cache.");
3182
log("parsed " + sheet.href + " successfully.");
3183
createCSS(root.toCSS(), sheet, env.lastModified);
3185
log("css for " + sheet.href + " generated in " + (new(Date) - endTime) + 'ms');
3186
(env.remaining === 0) && log("css generated in " + (new(Date) - startTime) + 'ms');
3187
endTime = new(Date);
3192
less.refreshStyles = loadStyles;
3194
less.refresh(less.env === 'development');
3196
function loadStyles() {
3197
var styles = document.getElementsByTagName('style');
3198
for (var i = 0; i < styles.length; i++) {
3199
if (styles[i].type.match(typePattern)) {
3200
new(less.Parser)().parse(styles[i].innerHTML || '', function (e, tree) {
3201
var css = tree.toCSS();
3202
var style = styles[i];
3203
style.type = 'text/css';
3204
if (style.styleSheet) {
3205
style.styleSheet.cssText = css;
3207
style.innerHTML = css;
3214
function loadStyleSheets(callback, reload) {
3215
for (var i = 0; i < less.sheets.length; i++) {
3216
loadStyleSheet(less.sheets[i], callback, reload, less.sheets.length - (i + 1));
3220
function loadStyleSheet(sheet, callback, reload, remaining) {
3221
var url = window.location.href.replace(/[#?].*$/, '');
3222
var href = sheet.href.replace(/\?.*$/, '');
3223
var css = cache && cache.getItem(href);
3224
var timestamp = cache && cache.getItem(href + ':timestamp');
3225
var styles = { css: css, timestamp: timestamp };
3227
// Stylesheets in IE don't always return the full path
3228
if (! /^(https?|file):/.test(href)) {
3229
if (href.charAt(0) == "/") {
3230
href = window.location.protocol + "//" + window.location.host + href;
3232
href = url.slice(0, url.lastIndexOf('/') + 1) + href;
3235
var filename = href.match(/([^\/]+)$/)[1];
3237
xhr(sheet.href, sheet.type, function (data, lastModified) {
3238
if (!reload && styles && lastModified &&
3239
(new(Date)(lastModified).valueOf() ===
3240
new(Date)(styles.timestamp).valueOf())) {
3242
createCSS(styles.css, sheet);
3243
callback(null, null, data, sheet, { local: true, remaining: remaining });
3245
// Use remote copy (re-parse)
3248
optimization: less.optimization,
3249
paths: [href.replace(/[\w\.-]+$/, '')],
3252
}).parse(data, function (e, root) {
3253
if (e) { return error(e, href) }
3255
callback(e, root, data, sheet, { local: false, lastModified: lastModified, remaining: remaining });
3256
removeNode(document.getElementById('less-error-message:' + extractId(href)));
3265
}, function (status, url) {
3266
throw new(Error)("Couldn't load " + url + " (" + status + ")");
3270
function extractId(href) {
3271
return href.replace(/^[a-z]+:\/\/?[^\/]+/, '' ) // Remove protocol & domain
3272
.replace(/^\//, '' ) // Remove root /
3273
.replace(/\?.*$/, '' ) // Remove query
3274
.replace(/\.[^\.\/]+$/, '' ) // Remove file extension
3275
.replace(/[^\.\w-]+/g, '-') // Replace illegal characters
3276
.replace(/\./g, ':'); // Replace dots with colons(for valid id)
3279
function createCSS(styles, sheet, lastModified) {
3282
// Strip the query-string
3283
var href = sheet.href ? sheet.href.replace(/\?.*$/, '') : '';
3285
// If there is no title set, use the filename, minus the extension
3286
var id = 'less:' + (sheet.title || extractId(href));
3288
// If the stylesheet doesn't exist, create a new node
3289
if ((css = document.getElementById(id)) === null) {
3290
css = document.createElement('style');
3291
css.type = 'text/css';
3292
css.media = sheet.media || 'screen';
3294
document.getElementsByTagName('head')[0].appendChild(css);
3297
if (css.styleSheet) { // IE
3299
css.styleSheet.cssText = styles;
3301
throw new(Error)("Couldn't reassign styleSheet.cssText.");
3305
if (css.childNodes.length > 0) {
3306
if (css.firstChild.nodeValue !== node.nodeValue) {
3307
css.replaceChild(node, css.firstChild);
3310
css.appendChild(node);
3312
})(document.createTextNode(styles));
3315
// Don't update the local store if the file wasn't modified
3316
if (lastModified && cache) {
3317
log('saving ' + href + ' to cache.');
3318
cache.setItem(href, styles);
3319
cache.setItem(href + ':timestamp', lastModified);
3323
function xhr(url, type, callback, errback) {
3324
var xhr = getXMLHttpRequest();
3325
var async = isFileProtocol ? false : less.async;
3327
if (typeof(xhr.overrideMimeType) === 'function') {
3328
xhr.overrideMimeType('text/css');
3330
xhr.open('GET', url, async);
3331
xhr.setRequestHeader('Accept', type || 'text/x-less, text/css; q=0.9, */*; q=0.5');
3334
if (isFileProtocol) {
3335
if (xhr.status === 0 || (xhr.status >= 200 && xhr.status < 300)) {
3336
callback(xhr.responseText);
3338
errback(xhr.status, url);
3341
xhr.onreadystatechange = function () {
3342
if (xhr.readyState == 4) {
3343
handleResponse(xhr, callback, errback);
3347
handleResponse(xhr, callback, errback);
3350
function handleResponse(xhr, callback, errback) {
3351
if (xhr.status >= 200 && xhr.status < 300) {
3352
callback(xhr.responseText,
3353
xhr.getResponseHeader("Last-Modified"));
3354
} else if (typeof(errback) === 'function') {
3355
errback(xhr.status, url);
3360
function getXMLHttpRequest() {
3361
if (window.XMLHttpRequest) {
3362
return new(XMLHttpRequest);
3365
return new(ActiveXObject)("MSXML2.XMLHTTP.3.0");
3367
log("browser doesn't support AJAX.");
3373
function removeNode(node) {
3374
return node && node.parentNode.removeChild(node);
3378
if (less.env == 'development' && typeof(console) !== "undefined") { console.log('less: ' + str) }
3381
function error(e, href) {
3382
var id = 'less-error-message:' + extractId(href);
3383
var template = '<li><label>{line}</label><pre class="{class}">{content}</pre></li>';
3384
var elem = document.createElement('div'), timer, content, error = [];
3385
var filename = e.filename || href;
3388
elem.className = "less-error-message";
3390
content = '<h3>' + (e.message || 'There is an error in your .less file') +
3391
'</h3>' + '<p>in <a href="' + filename + '">' + filename + "</a> ";
3393
var errorline = function (e, i, classname) {
3395
error.push(template.replace(/\{line\}/, parseInt(e.line) + (i - 1))
3396
.replace(/\{class\}/, classname)
3397
.replace(/\{content\}/, e.extract[i]));
3402
content += '<br/>' + e.stack.split('\n').slice(1).join('<br/>');
3403
} else if (e.extract) {
3404
errorline(e, 0, '');
3405
errorline(e, 1, 'line');
3406
errorline(e, 2, '');
3407
content += 'on line ' + e.line + ', column ' + (e.column + 1) + ':</p>' +
3408
'<ul>' + error.join('') + '</ul>';
3410
elem.innerHTML = content;
3412
// CSS for error messages
3414
'.less-error-message ul, .less-error-message li {',
3415
'list-style-type: none;',
3416
'margin-right: 15px;',
3420
'.less-error-message label {',
3422
'margin-right: 15px;',
3426
'.less-error-message pre {',
3430
'display: inline-block;',
3432
'.less-error-message pre.line {',
3435
'.less-error-message h3 {',
3437
'font-weight: bold;',
3438
'padding: 15px 0 5px 0;',
3441
'.less-error-message a {',
3444
'.less-error-message .error {',
3446
'font-weight: bold;',
3447
'padding-bottom: 2px;',
3448
'border-bottom: 1px dashed red;',
3450
].join('\n'), { title: 'error-message' });
3452
elem.style.cssText = [
3453
"font-family: Arial, sans-serif",
3454
"border: 1px solid #e00",
3455
"background-color: #eee",
3456
"border-radius: 5px",
3457
"-webkit-border-radius: 5px",
3458
"-moz-border-radius: 5px",
3461
"margin-bottom: 15px"
3464
if (less.env == 'development') {
3465
timer = setInterval(function () {
3466
if (document.body) {
3467
if (document.getElementById(id)) {
3468
document.body.replaceChild(elem, document.getElementById(id));
3470
document.body.insertBefore(elem, document.body.firstChild);
3472
clearInterval(timer);