less 1.2.21 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (165) hide show
  1. data/.gitignore +4 -3
  2. data/.gitmodules +6 -0
  3. data/Gemfile +3 -0
  4. data/README.md +39 -46
  5. data/Rakefile +5 -50
  6. data/bin/lessc +1 -100
  7. data/less.gemspec +21 -125
  8. data/lib/less.rb +11 -30
  9. data/{LICENSE → lib/less/js/LICENSE} +1 -1
  10. data/lib/less/js/Makefile +56 -0
  11. data/lib/less/js/README.md +20 -0
  12. data/lib/less/js/benchmark/benchmark.less +3979 -0
  13. data/lib/less/js/benchmark/less-benchmark.js +49 -0
  14. data/lib/less/js/bin/lessc +105 -0
  15. data/lib/less/js/build/ecma-5.js +120 -0
  16. data/lib/less/js/build/header.js +7 -0
  17. data/lib/less/js/build/require.js +7 -0
  18. data/lib/less/js/dist/less-1.0.44.js +2655 -0
  19. data/lib/less/js/dist/less-1.1.0.js +2695 -0
  20. data/lib/less/js/dist/less-1.1.0.min.js +16 -0
  21. data/lib/less/js/dist/less-1.1.1.js +2710 -0
  22. data/lib/less/js/dist/less-1.1.1.min.js +16 -0
  23. data/lib/less/js/dist/less-1.1.2.js +2712 -0
  24. data/lib/less/js/dist/less-1.1.2.min.js +16 -0
  25. data/lib/less/js/dist/less-1.1.3.js +2721 -0
  26. data/lib/less/js/dist/less-1.1.3.min.js +16 -0
  27. data/lib/less/js/lib/less/browser.js +369 -0
  28. data/lib/less/js/lib/less/functions.js +174 -0
  29. data/lib/less/js/lib/less/index.js +137 -0
  30. data/lib/less/js/lib/less/parser.js +1098 -0
  31. data/lib/less/js/lib/less/tree.js +13 -0
  32. data/lib/less/js/lib/less/tree/alpha.js +17 -0
  33. data/lib/less/js/lib/less/tree/anonymous.js +13 -0
  34. data/lib/less/js/lib/less/tree/call.js +45 -0
  35. data/lib/less/js/lib/less/tree/color.js +98 -0
  36. data/lib/less/js/lib/less/tree/comment.js +14 -0
  37. data/lib/less/js/lib/less/tree/dimension.js +34 -0
  38. data/lib/less/js/lib/less/tree/directive.js +33 -0
  39. data/lib/less/js/lib/less/tree/element.js +32 -0
  40. data/lib/less/js/lib/less/tree/expression.js +23 -0
  41. data/lib/less/js/lib/less/tree/import.js +77 -0
  42. data/lib/less/js/lib/less/tree/javascript.js +51 -0
  43. data/lib/less/js/lib/less/tree/keyword.js +9 -0
  44. data/lib/less/js/lib/less/tree/mixin.js +106 -0
  45. data/lib/less/js/lib/less/tree/operation.js +32 -0
  46. data/lib/less/js/lib/less/tree/quoted.js +29 -0
  47. data/lib/less/js/lib/less/tree/rule.js +38 -0
  48. data/lib/less/js/lib/less/tree/ruleset.js +179 -0
  49. data/lib/less/js/lib/less/tree/selector.js +28 -0
  50. data/lib/less/js/lib/less/tree/url.js +25 -0
  51. data/lib/less/js/lib/less/tree/value.js +24 -0
  52. data/lib/less/js/lib/less/tree/variable.js +24 -0
  53. data/lib/less/js/package.json +13 -0
  54. data/lib/less/js/test/css/colors.css +42 -0
  55. data/lib/less/js/test/css/comments.css +52 -0
  56. data/lib/less/js/test/css/css-3.css +42 -0
  57. data/lib/less/js/test/css/css-escapes.css +20 -0
  58. data/lib/less/js/test/css/css.css +82 -0
  59. data/lib/less/js/test/css/functions.css +30 -0
  60. data/{spec → lib/less/js/test}/css/import.css +4 -2
  61. data/lib/less/js/test/css/javascript.css +22 -0
  62. data/lib/less/js/test/css/lazy-eval.css +3 -0
  63. data/lib/less/js/test/css/media.css +21 -0
  64. data/lib/less/js/test/css/mixins-args.css +61 -0
  65. data/lib/less/js/test/css/mixins-closure.css +9 -0
  66. data/lib/less/js/test/css/mixins-nested.css +14 -0
  67. data/lib/less/js/test/css/mixins-pattern.css +49 -0
  68. data/lib/less/js/test/css/mixins.css +50 -0
  69. data/{spec → lib/less/js/test}/css/operations.css +20 -2
  70. data/{spec → lib/less/js/test}/css/parens.css +0 -0
  71. data/lib/less/js/test/css/rulesets.css +29 -0
  72. data/{spec → lib/less/js/test}/css/scope.css +6 -2
  73. data/lib/less/js/test/css/selectors.css +32 -0
  74. data/lib/less/js/test/css/strings.css +38 -0
  75. data/lib/less/js/test/css/variables.css +24 -0
  76. data/lib/less/js/test/css/whitespace.css +36 -0
  77. data/lib/less/js/test/less-test.js +75 -0
  78. data/{spec → lib/less/js/test}/less/colors.less +13 -2
  79. data/{spec → lib/less/js/test}/less/comments.less +19 -2
  80. data/{spec → lib/less/js/test}/less/css-3.less +4 -1
  81. data/lib/less/js/test/less/css-escapes.less +28 -0
  82. data/{spec → lib/less/js/test}/less/css.less +10 -18
  83. data/lib/less/js/test/less/functions.less +35 -0
  84. data/{spec → lib/less/js/test}/less/import.less +1 -1
  85. data/{spec → lib/less/js/test}/less/import/import-test-a.less +0 -0
  86. data/{spec → lib/less/js/test}/less/import/import-test-b.less +0 -0
  87. data/{spec → lib/less/js/test}/less/import/import-test-c.less +0 -0
  88. data/{spec → lib/less/js/test}/less/import/import-test-d.css +0 -0
  89. data/lib/less/js/test/less/javascript.less +27 -0
  90. data/{spec → lib/less/js/test}/less/lazy-eval.less +0 -0
  91. data/lib/less/js/test/less/media.less +25 -0
  92. data/lib/less/js/test/less/mixins-args.less +118 -0
  93. data/lib/less/js/test/less/mixins-closure.less +26 -0
  94. data/lib/less/js/test/less/mixins-nested.less +22 -0
  95. data/lib/less/js/test/less/mixins-pattern.less +96 -0
  96. data/{spec → lib/less/js/test}/less/mixins.less +8 -4
  97. data/{spec → lib/less/js/test}/less/operations.less +19 -0
  98. data/{spec → lib/less/js/test}/less/parens.less +0 -0
  99. data/{spec → lib/less/js/test}/less/rulesets.less +2 -2
  100. data/{spec → lib/less/js/test}/less/scope.less +1 -1
  101. data/{spec → lib/less/js/test}/less/selectors.less +1 -1
  102. data/lib/less/js/test/less/strings.less +49 -0
  103. data/lib/less/js/test/less/variables.less +50 -0
  104. data/{spec → lib/less/js/test}/less/whitespace.less +3 -0
  105. data/lib/less/loader.rb +67 -0
  106. data/lib/less/parser.rb +46 -0
  107. data/lib/less/version.rb +3 -0
  108. data/spec/less/one/one.less +1 -0
  109. data/spec/less/parser_spec.rb +30 -0
  110. data/spec/less/two/two.less +1 -0
  111. data/spec/spec_helper.rb +2 -7
  112. metadata +156 -106
  113. data/CHANGELOG +0 -62
  114. data/VERSION +0 -1
  115. data/lib/less/command.rb +0 -110
  116. data/lib/less/engine.rb +0 -52
  117. data/lib/less/engine/grammar/common.tt +0 -29
  118. data/lib/less/engine/grammar/entity.tt +0 -144
  119. data/lib/less/engine/grammar/less.tt +0 -341
  120. data/lib/less/engine/nodes.rb +0 -9
  121. data/lib/less/engine/nodes/element.rb +0 -281
  122. data/lib/less/engine/nodes/entity.rb +0 -79
  123. data/lib/less/engine/nodes/function.rb +0 -93
  124. data/lib/less/engine/nodes/literal.rb +0 -171
  125. data/lib/less/engine/nodes/property.rb +0 -232
  126. data/lib/less/engine/nodes/ruleset.rb +0 -12
  127. data/lib/less/engine/nodes/selector.rb +0 -44
  128. data/lib/less/ext.rb +0 -60
  129. data/spec/command_spec.rb +0 -102
  130. data/spec/css/accessors.css +0 -18
  131. data/spec/css/big.css +0 -3768
  132. data/spec/css/colors.css +0 -14
  133. data/spec/css/comments.css +0 -9
  134. data/spec/css/css-3.css +0 -21
  135. data/spec/css/css.css +0 -50
  136. data/spec/css/dash-prefix.css +0 -12
  137. data/spec/css/functions.css +0 -6
  138. data/spec/css/import-with-extra-paths.css +0 -8
  139. data/spec/css/import-with-partial-in-extra-path.css +0 -6
  140. data/spec/css/lazy-eval.css +0 -1
  141. data/spec/css/mixins-args.css +0 -32
  142. data/spec/css/mixins.css +0 -28
  143. data/spec/css/rulesets.css +0 -17
  144. data/spec/css/selectors.css +0 -13
  145. data/spec/css/strings.css +0 -12
  146. data/spec/css/variables.css +0 -8
  147. data/spec/css/whitespace.css +0 -7
  148. data/spec/engine_spec.rb +0 -127
  149. data/spec/less/accessors.less +0 -20
  150. data/spec/less/big.less +0 -1264
  151. data/spec/less/dash-prefix.less +0 -21
  152. data/spec/less/exceptions/mixed-units-error.less +0 -3
  153. data/spec/less/exceptions/name-error-1.0.less +0 -3
  154. data/spec/less/exceptions/syntax-error-1.0.less +0 -3
  155. data/spec/less/extra_import_path/extra.less +0 -1
  156. data/spec/less/extra_import_path/import/import-test-a.css +0 -1
  157. data/spec/less/extra_import_path/import/import-test-a.less +0 -4
  158. data/spec/less/functions.less +0 -6
  159. data/spec/less/hidden.less +0 -25
  160. data/spec/less/import-with-extra-paths.less +0 -4
  161. data/spec/less/literal-css.less +0 -11
  162. data/spec/less/mixins-args.less +0 -59
  163. data/spec/less/strings.less +0 -14
  164. data/spec/less/variables.less +0 -29
  165. data/spec/spec.css +0 -50
@@ -0,0 +1,49 @@
1
+ var path = require('path'),
2
+ fs = require('fs'),
3
+ sys = require('sys');
4
+
5
+ require.paths.unshift(__dirname, path.join(__dirname, '..'));
6
+
7
+ var less = require('lib/less');
8
+ var file = path.join(__dirname, 'benchmark.less');
9
+
10
+ if (process.argv[2]) { file = path.join(process.cwd(), process.argv[2]) }
11
+
12
+ fs.readFile(file, 'utf8', function (e, data) {
13
+ var tree, css, start, end, total;
14
+
15
+ sys.puts("Bechmarking...\n", path.basename(file) + " (" +
16
+ parseInt(data.length / 1024) + " KB)", "");
17
+
18
+ start = new(Date);
19
+
20
+ new(less.Parser)({ optimization: 2 }).parse(data, function (err, tree) {
21
+ end = new(Date);
22
+
23
+ total = end - start;
24
+
25
+ sys.puts("Parsing: " +
26
+ total + " ms (" +
27
+ parseInt(1000 / total *
28
+ data.length / 1024) + " KB\/s)");
29
+
30
+ start = new(Date);
31
+ css = tree.toCSS();
32
+ end = new(Date);
33
+
34
+ sys.puts("Generation: " + (end - start) + " ms (" +
35
+ parseInt(1000 / (end - start) *
36
+ data.length / 1024) + " KB\/s)");
37
+
38
+ total += end - start;
39
+
40
+ sys.puts("Total: " + total + "ms (" +
41
+ parseInt(1000 / total * data.length / 1024) + " KB/s)");
42
+
43
+ if (err) {
44
+ less.writeError(err);
45
+ process.exit(3);
46
+ }
47
+ });
48
+ });
49
+
@@ -0,0 +1,105 @@
1
+ #!/usr/bin/env node
2
+
3
+ var path = require('path'),
4
+ fs = require('fs'),
5
+ sys = require('sys');
6
+
7
+ require.paths.unshift(path.join(__dirname, '..', 'lib'));
8
+
9
+ var less = require('less');
10
+ var args = process.argv.slice(1);
11
+ var options = {
12
+ compress: false,
13
+ optimization: 1,
14
+ silent: false,
15
+ paths: []
16
+ };
17
+
18
+ args = args.filter(function (arg) {
19
+ var match;
20
+
21
+ if (match = arg.match(/^--?([a-z][0-9a-z-]*)(?:=([^\s]+))?$/i)) { arg = match[1] }
22
+ else { return arg }
23
+
24
+ switch (arg) {
25
+ case 'v':
26
+ case 'version':
27
+ sys.puts("lessc " + less.version.join('.') + " (LESS Compiler) [JavaScript]");
28
+ process.exit(0);
29
+ case 'verbose':
30
+ options.verbose = true;
31
+ break;
32
+ case 's':
33
+ case 'silent':
34
+ options.silent = true;
35
+ break;
36
+ case 'h':
37
+ case 'help':
38
+ sys.puts("usage: lessc source [destination]");
39
+ process.exit(0);
40
+ case 'x':
41
+ case 'compress':
42
+ options.compress = true;
43
+ break;
44
+ case 'include-path':
45
+ options.paths = match[2].split(':')
46
+ .map(function(p) {
47
+ if (p && p[0] == '/') {
48
+ return path.join(path.dirname(input), p);
49
+ } else if (p) {
50
+ return path.join(process.cwd(), p);
51
+ }
52
+ });
53
+ break;
54
+ case 'O0': options.optimization = 0; break;
55
+ case 'O1': options.optimization = 1; break;
56
+ case 'O2': options.optimization = 2; break;
57
+ }
58
+ });
59
+
60
+ var input = args[1];
61
+ if (input && input[0] != '/') {
62
+ input = path.join(process.cwd(), input);
63
+ }
64
+ var output = args[2];
65
+ if (output && output[0] != '/') {
66
+ output = path.join(process.cwd(), output);
67
+ }
68
+
69
+ var css, fd, tree;
70
+
71
+ if (! input) {
72
+ sys.puts("lessc: no input files");
73
+ process.exit(1);
74
+ }
75
+
76
+ fs.readFile(input, 'utf-8', function (e, data) {
77
+ if (e) {
78
+ sys.puts("lessc: " + e.message);
79
+ process.exit(1);
80
+ }
81
+
82
+ new(less.Parser)({
83
+ paths: [path.dirname(input)].concat(options.paths),
84
+ optimization: options.optimization,
85
+ filename: input
86
+ }).parse(data, function (err, tree) {
87
+ if (err) {
88
+ less.writeError(err, options);
89
+ process.exit(1);
90
+ } else {
91
+ try {
92
+ css = tree.toCSS({ compress: options.compress });
93
+ if (output) {
94
+ fd = fs.openSync(output, "w");
95
+ fs.writeSync(fd, css, 0, "utf8");
96
+ } else {
97
+ sys.print(css);
98
+ }
99
+ } catch (e) {
100
+ less.writeError(e, options);
101
+ process.exit(2);
102
+ }
103
+ }
104
+ });
105
+ });
@@ -0,0 +1,120 @@
1
+
2
+ // ecma-5.js
3
+ //
4
+ // -- kriskowal Kris Kowal Copyright (C) 2009-2010 MIT License
5
+ // -- tlrobinson Tom Robinson
6
+ // dantman Daniel Friesen
7
+
8
+ //
9
+ // Array
10
+ //
11
+ if (!Array.isArray) {
12
+ Array.isArray = function(obj) {
13
+ return Object.prototype.toString.call(obj) === "[object Array]" ||
14
+ (obj instanceof Array);
15
+ };
16
+ }
17
+ if (!Array.prototype.forEach) {
18
+ Array.prototype.forEach = function(block, thisObject) {
19
+ var len = this.length >>> 0;
20
+ for (var i = 0; i < len; i++) {
21
+ if (i in this) {
22
+ block.call(thisObject, this[i], i, this);
23
+ }
24
+ }
25
+ };
26
+ }
27
+ if (!Array.prototype.map) {
28
+ Array.prototype.map = function(fun /*, thisp*/) {
29
+ var len = this.length >>> 0;
30
+ var res = new Array(len);
31
+ var thisp = arguments[1];
32
+
33
+ for (var i = 0; i < len; i++) {
34
+ if (i in this) {
35
+ res[i] = fun.call(thisp, this[i], i, this);
36
+ }
37
+ }
38
+ return res;
39
+ };
40
+ }
41
+ if (!Array.prototype.filter) {
42
+ Array.prototype.filter = function (block /*, thisp */) {
43
+ var values = [];
44
+ var thisp = arguments[1];
45
+ for (var i = 0; i < this.length; i++) {
46
+ if (block.call(thisp, this[i])) {
47
+ values.push(this[i]);
48
+ }
49
+ }
50
+ return values;
51
+ };
52
+ }
53
+ if (!Array.prototype.reduce) {
54
+ Array.prototype.reduce = function(fun /*, initial*/) {
55
+ var len = this.length >>> 0;
56
+ var i = 0;
57
+
58
+ // no value to return if no initial value and an empty array
59
+ if (len === 0 && arguments.length === 1) throw new TypeError();
60
+
61
+ if (arguments.length >= 2) {
62
+ var rv = arguments[1];
63
+ } else {
64
+ do {
65
+ if (i in this) {
66
+ rv = this[i++];
67
+ break;
68
+ }
69
+ // if array contains no values, no initial value to return
70
+ if (++i >= len) throw new TypeError();
71
+ } while (true);
72
+ }
73
+ for (; i < len; i++) {
74
+ if (i in this) {
75
+ rv = fun.call(null, rv, this[i], i, this);
76
+ }
77
+ }
78
+ return rv;
79
+ };
80
+ }
81
+ if (!Array.prototype.indexOf) {
82
+ Array.prototype.indexOf = function (value /*, fromIndex */ ) {
83
+ var length = this.length;
84
+ var i = arguments[1] || 0;
85
+
86
+ if (!length) return -1;
87
+ if (i >= length) return -1;
88
+ if (i < 0) i += length;
89
+
90
+ for (; i < length; i++) {
91
+ if (!Object.prototype.hasOwnProperty.call(this, i)) { continue }
92
+ if (value === this[i]) return i;
93
+ }
94
+ return -1;
95
+ };
96
+ }
97
+
98
+ //
99
+ // Object
100
+ //
101
+ if (!Object.keys) {
102
+ Object.keys = function (object) {
103
+ var keys = [];
104
+ for (var name in object) {
105
+ if (Object.prototype.hasOwnProperty.call(object, name)) {
106
+ keys.push(name);
107
+ }
108
+ }
109
+ return keys;
110
+ };
111
+ }
112
+
113
+ //
114
+ // String
115
+ //
116
+ if (!String.prototype.trim) {
117
+ String.prototype.trim = function () {
118
+ return String(this).replace(/^\s\s*/, '').replace(/\s\s*$/, '');
119
+ };
120
+ }
@@ -0,0 +1,7 @@
1
+ //
2
+ // LESS - Leaner CSS v@VERSION
3
+ // http://lesscss.org
4
+ //
5
+ // Copyright (c) 2009-2011, Alexis Sellier
6
+ // Licensed under the Apache 2.0 License.
7
+ //
@@ -0,0 +1,7 @@
1
+ //
2
+ // Stub out `require` in the browser
3
+ //
4
+ function require(arg) {
5
+ return window.less[arg.split('/')[1]];
6
+ };
7
+
@@ -0,0 +1,2655 @@
1
+ //
2
+ // LESS - Leaner CSS v1.0.44
3
+ // http://lesscss.org
4
+ //
5
+ // Copyright (c) 2010, Alexis Sellier
6
+ // Licensed under the Apache 2.0 License.
7
+ //
8
+ (function (window, undefined) {
9
+ //
10
+ // Stub out `require` in the browser
11
+ //
12
+ function require(arg) {
13
+ return window.less[arg.split('/')[1]];
14
+ };
15
+
16
+
17
+ // ecma-5.js
18
+ //
19
+ // -- kriskowal Kris Kowal Copyright (C) 2009-2010 MIT License
20
+ // -- tlrobinson Tom Robinson
21
+ // dantman Daniel Friesen
22
+
23
+ //
24
+ // Array
25
+ //
26
+ if (!Array.isArray) {
27
+ Array.isArray = function(obj) {
28
+ return Object.prototype.toString.call(obj) === "[object Array]" ||
29
+ (obj instanceof Array);
30
+ };
31
+ }
32
+ if (!Array.prototype.forEach) {
33
+ Array.prototype.forEach = function(block, thisObject) {
34
+ var len = this.length >>> 0;
35
+ for (var i = 0; i < len; i++) {
36
+ if (i in this) {
37
+ block.call(thisObject, this[i], i, this);
38
+ }
39
+ }
40
+ };
41
+ }
42
+ if (!Array.prototype.map) {
43
+ Array.prototype.map = function(fun /*, thisp*/) {
44
+ var len = this.length >>> 0;
45
+ var res = new Array(len);
46
+ var thisp = arguments[1];
47
+
48
+ for (var i = 0; i < len; i++) {
49
+ if (i in this) {
50
+ res[i] = fun.call(thisp, this[i], i, this);
51
+ }
52
+ }
53
+ return res;
54
+ };
55
+ }
56
+ if (!Array.prototype.filter) {
57
+ Array.prototype.filter = function (block /*, thisp */) {
58
+ var values = [];
59
+ var thisp = arguments[1];
60
+ for (var i = 0; i < this.length; i++) {
61
+ if (block.call(thisp, this[i])) {
62
+ values.push(this[i]);
63
+ }
64
+ }
65
+ return values;
66
+ };
67
+ }
68
+ if (!Array.prototype.reduce) {
69
+ Array.prototype.reduce = function(fun /*, initial*/) {
70
+ var len = this.length >>> 0;
71
+ var i = 0;
72
+
73
+ // no value to return if no initial value and an empty array
74
+ if (len === 0 && arguments.length === 1) throw new TypeError();
75
+
76
+ if (arguments.length >= 2) {
77
+ var rv = arguments[1];
78
+ } else {
79
+ do {
80
+ if (i in this) {
81
+ rv = this[i++];
82
+ break;
83
+ }
84
+ // if array contains no values, no initial value to return
85
+ if (++i >= len) throw new TypeError();
86
+ } while (true);
87
+ }
88
+ for (; i < len; i++) {
89
+ if (i in this) {
90
+ rv = fun.call(null, rv, this[i], i, this);
91
+ }
92
+ }
93
+ return rv;
94
+ };
95
+ }
96
+ if (!Array.prototype.indexOf) {
97
+ Array.prototype.indexOf = function (value /*, fromIndex */ ) {
98
+ var length = this.length;
99
+ var i = arguments[1] || 0;
100
+
101
+ if (!length) return -1;
102
+ if (i >= length) return -1;
103
+ if (i < 0) i += length;
104
+
105
+ for (; i < length; i++) {
106
+ if (!Object.prototype.hasOwnProperty.call(this, i)) { continue }
107
+ if (value === this[i]) return i;
108
+ }
109
+ return -1;
110
+ };
111
+ }
112
+
113
+ //
114
+ // Object
115
+ //
116
+ if (!Object.keys) {
117
+ Object.keys = function (object) {
118
+ var keys = [];
119
+ for (var name in object) {
120
+ if (Object.prototype.hasOwnProperty.call(object, name)) {
121
+ keys.push(name);
122
+ }
123
+ }
124
+ return keys;
125
+ };
126
+ }
127
+
128
+ //
129
+ // String
130
+ //
131
+ if (!String.prototype.trim) {
132
+ String.prototype.trim = function () {
133
+ return String(this).replace(/^\s\s*/, '').replace(/\s\s*$/, '');
134
+ };
135
+ }
136
+ var less, tree;
137
+
138
+ if (typeof(window) === 'undefined') {
139
+ less = exports,
140
+ tree = require('less/tree');
141
+ } else {
142
+ if (typeof(window.less) === 'undefined') { window.less = {} }
143
+ less = window.less,
144
+ tree = window.less.tree = {};
145
+ }
146
+ //
147
+ // less.js - parser
148
+ //
149
+ // A relatively straight-forward predictive parser.
150
+ // There is no tokenization/lexing stage, the input is parsed
151
+ // in one sweep.
152
+ //
153
+ // To make the parser fast enough to run in the browser, several
154
+ // optimization had to be made:
155
+ //
156
+ // - Matching and slicing on a huge input is often cause of slowdowns.
157
+ // The solution is to chunkify the input into smaller strings.
158
+ // The chunks are stored in the `chunks` var,
159
+ // `j` holds the current chunk index, and `current` holds
160
+ // the index of the current chunk in relation to `input`.
161
+ // This gives us an almost 4x speed-up.
162
+ //
163
+ // - In many cases, we don't need to match individual tokens;
164
+ // for example, if a value doesn't hold any variables, operations
165
+ // or dynamic references, the parser can effectively 'skip' it,
166
+ // treating it as a literal.
167
+ // An example would be '1px solid #000' - which evaluates to itself,
168
+ // we don't need to know what the individual components are.
169
+ // The drawback, of course is that you don't get the benefits of
170
+ // syntax-checking on the CSS. This gives us a 50% speed-up in the parser,
171
+ // and a smaller speed-up in the code-gen.
172
+ //
173
+ //
174
+ // Token matching is done with the `$` function, which either takes
175
+ // a terminal string or regexp, or a non-terminal function to call.
176
+ // It also takes care of moving all the indices forwards.
177
+ //
178
+ //
179
+ less.Parser = function Parser(env) {
180
+ var input, // LeSS input string
181
+ i, // current index in `input`
182
+ j, // current chunk
183
+ temp, // temporarily holds a chunk's state, for backtracking
184
+ memo, // temporarily holds `i`, when backtracking
185
+ furthest, // furthest index the parser has gone to
186
+ chunks, // chunkified input
187
+ current, // index of current chunk, in `input`
188
+ parser;
189
+
190
+ var that = this;
191
+
192
+ // This function is called after all files
193
+ // have been imported through `@import`.
194
+ var finish = function () {};
195
+
196
+ var imports = this.imports = {
197
+ paths: env && env.paths || [], // Search paths, when importing
198
+ queue: [], // Files which haven't been imported yet
199
+ files: {}, // Holds the imported parse trees
200
+ mime: env && env.mime, // MIME type of .less files
201
+ push: function (path, callback) {
202
+ var that = this;
203
+ this.queue.push(path);
204
+
205
+ //
206
+ // Import a file asynchronously
207
+ //
208
+ less.Parser.importer(path, this.paths, function (root) {
209
+ that.queue.splice(that.queue.indexOf(path), 1); // Remove the path from the queue
210
+ that.files[path] = root; // Store the root
211
+
212
+ callback(root);
213
+
214
+ if (that.queue.length === 0) { finish() } // Call `finish` if we're done importing
215
+ }, env);
216
+ }
217
+ };
218
+
219
+ function save() { temp = chunks[j], memo = i, current = i }
220
+ function restore() { chunks[j] = temp, i = memo, current = i }
221
+
222
+ function sync() {
223
+ if (i > current) {
224
+ chunks[j] = chunks[j].slice(i - current);
225
+ current = i;
226
+ }
227
+ }
228
+ //
229
+ // Parse from a token, regexp or string, and move forward if match
230
+ //
231
+ function $(tok) {
232
+ var match, args, length, c, index, endIndex, k, mem;
233
+
234
+ //
235
+ // Non-terminal
236
+ //
237
+ if (tok instanceof Function) {
238
+ return tok.call(parser.parsers);
239
+ //
240
+ // Terminal
241
+ //
242
+ // Either match a single character in the input,
243
+ // or match a regexp in the current chunk (chunk[j]).
244
+ //
245
+ } else if (typeof(tok) === 'string') {
246
+ match = input.charAt(i) === tok ? tok : null;
247
+ length = 1;
248
+ sync ();
249
+ } else {
250
+ sync ();
251
+
252
+ if (match = tok.exec(chunks[j])) {
253
+ length = match[0].length;
254
+ } else {
255
+ return null;
256
+ }
257
+ }
258
+
259
+ // The match is confirmed, add the match length to `i`,
260
+ // and consume any extra white-space characters (' ' || '\n')
261
+ // which come after that. The reason for this is that LeSS's
262
+ // grammar is mostly white-space insensitive.
263
+ //
264
+ if (match) {
265
+ mem = i += length;
266
+ endIndex = i + chunks[j].length - length;
267
+
268
+ while (i < endIndex) {
269
+ c = input.charCodeAt(i);
270
+ if (! (c === 32 || c === 10 || c === 9)) { break }
271
+ i++;
272
+ }
273
+ chunks[j] = chunks[j].slice(length + (i - mem));
274
+ current = i;
275
+
276
+ if (chunks[j].length === 0 && j < chunks.length - 1) { j++ }
277
+
278
+ if(typeof(match) === 'string') {
279
+ return match;
280
+ } else {
281
+ return match.length === 1 ? match[0] : match;
282
+ }
283
+ }
284
+ }
285
+
286
+ // Same as $(), but don't change the state of the parser,
287
+ // just return the match.
288
+ function peek(tok) {
289
+ if (typeof(tok) === 'string') {
290
+ return input.charAt(i) === tok;
291
+ } else {
292
+ if (tok.test(chunks[j])) {
293
+ return true;
294
+ } else {
295
+ return false;
296
+ }
297
+ }
298
+ }
299
+
300
+ this.env = env = env || {};
301
+
302
+ // The optimization level dictates the thoroughness of the parser,
303
+ // the lower the number, the less nodes it will create in the tree.
304
+ // This could matter for debugging, or if you want to access
305
+ // the individual nodes in the tree.
306
+ this.optimization = ('optimization' in this.env) ? this.env.optimization : 1;
307
+
308
+ this.env.filename = this.env.filename || null;
309
+
310
+ //
311
+ // The Parser
312
+ //
313
+ return parser = {
314
+
315
+ imports: imports,
316
+ //
317
+ // Parse an input string into an abstract syntax tree,
318
+ // call `callback` when done.
319
+ //
320
+ parse: function (str, callback) {
321
+ var root, start, end, zone, line, lines, buff = [], c, error = null;
322
+
323
+ i = j = current = furthest = 0;
324
+ chunks = [];
325
+ input = str.replace(/\r\n/g, '\n');
326
+
327
+ // Split the input into chunks.
328
+ chunks = (function (chunks) {
329
+ var j = 0,
330
+ skip = /[^"'`\{\}\/\(\)]+/g,
331
+ comment = /\/\*(?:[^*]|\*+[^\/*])*\*+\/|\/\/.*/g,
332
+ level = 0,
333
+ match,
334
+ chunk = chunks[0],
335
+ inParam,
336
+ inString;
337
+
338
+ for (var i = 0, c, cc; i < input.length; i++) {
339
+ skip.lastIndex = i;
340
+ if (match = skip.exec(input)) {
341
+ if (match.index === i) {
342
+ i += match[0].length;
343
+ chunk.push(match[0]);
344
+ }
345
+ }
346
+ c = input.charAt(i);
347
+ comment.lastIndex = i;
348
+
349
+ if (!inString && !inParam && c === '/') {
350
+ cc = input.charAt(i + 1);
351
+ if (cc === '/' || cc === '*') {
352
+ if (match = comment.exec(input)) {
353
+ if (match.index === i) {
354
+ i += match[0].length;
355
+ chunk.push(match[0]);
356
+ c = input.charAt(i);
357
+ }
358
+ }
359
+ }
360
+ }
361
+
362
+ if (c === '{' && !inString && !inParam) { level ++;
363
+ chunk.push(c);
364
+ } else if (c === '}' && !inString && !inParam) { level --;
365
+ chunk.push(c);
366
+ chunks[++j] = chunk = [];
367
+ } else if (c === '(' && !inString && !inParam) {
368
+ chunk.push(c);
369
+ inParam = true;
370
+ } else if (c === ')' && !inString && inParam) {
371
+ chunk.push(c);
372
+ inParam = false;
373
+ } else {
374
+ if (c === '"' || c === "'" || c === '`') {
375
+ if (! inString) {
376
+ inString = c;
377
+ } else {
378
+ inString = inString === c ? false : inString;
379
+ }
380
+ }
381
+ chunk.push(c);
382
+ }
383
+ }
384
+ if (level > 0) {
385
+ throw {
386
+ type: 'Syntax',
387
+ message: "Missing closing `}`",
388
+ filename: env.filename
389
+ };
390
+ }
391
+
392
+ return chunks.map(function (c) { return c.join('') });;
393
+ })([[]]);
394
+
395
+ // Start with the primary rule.
396
+ // The whole syntax tree is held under a Ruleset node,
397
+ // with the `root` property set to true, so no `{}` are
398
+ // output. The callback is called when the input is parsed.
399
+ root = new(tree.Ruleset)([], $(this.parsers.primary));
400
+ root.root = true;
401
+
402
+ root.toCSS = (function (evaluate) {
403
+ var line, lines, column;
404
+
405
+ return function (options, variables) {
406
+ var frames = [];
407
+
408
+ options = options || {};
409
+ //
410
+ // Allows setting variables with a hash, so:
411
+ //
412
+ // `{ color: new(tree.Color)('#f01') }` will become:
413
+ //
414
+ // new(tree.Rule)('@color',
415
+ // new(tree.Value)([
416
+ // new(tree.Expression)([
417
+ // new(tree.Color)('#f01')
418
+ // ])
419
+ // ])
420
+ // )
421
+ //
422
+ if (typeof(variables) === 'object' && !Array.isArray(variables)) {
423
+ variables = Object.keys(variables).map(function (k) {
424
+ var value = variables[k];
425
+
426
+ if (! (value instanceof tree.Value)) {
427
+ if (! (value instanceof tree.Expression)) {
428
+ value = new(tree.Expression)([value]);
429
+ }
430
+ value = new(tree.Value)([value]);
431
+ }
432
+ return new(tree.Rule)('@' + k, value, false, 0);
433
+ });
434
+ frames = [new(tree.Ruleset)(null, variables)];
435
+ }
436
+
437
+ try {
438
+ var css = evaluate.call(this, { frames: frames })
439
+ .toCSS([], { compress: options.compress || false });
440
+ } catch (e) {
441
+ lines = input.split('\n');
442
+ line = getLine(e.index);
443
+
444
+ for (var n = e.index, column = -1;
445
+ n >= 0 && input.charAt(n) !== '\n';
446
+ n--) { column++ }
447
+
448
+ throw {
449
+ type: e.type,
450
+ message: e.message,
451
+ filename: env.filename,
452
+ index: e.index,
453
+ line: typeof(line) === 'number' ? line + 1 : null,
454
+ callLine: e.call && (getLine(e.call) + 1),
455
+ callExtract: lines[getLine(e.call)],
456
+ stack: e.stack,
457
+ column: column,
458
+ extract: [
459
+ lines[line - 1],
460
+ lines[line],
461
+ lines[line + 1]
462
+ ]
463
+ };
464
+ }
465
+ if (options.compress) {
466
+ return css.replace(/(\s)+/g, "$1");
467
+ } else {
468
+ return css;
469
+ }
470
+
471
+ function getLine(index) {
472
+ return index ? (input.slice(0, index).match(/\n/g) || "").length : null;
473
+ }
474
+ };
475
+ })(root.eval);
476
+
477
+ // If `i` is smaller than the `input.length - 1`,
478
+ // it means the parser wasn't able to parse the whole
479
+ // string, so we've got a parsing error.
480
+ //
481
+ // We try to extract a \n delimited string,
482
+ // showing the line where the parse error occured.
483
+ // We split it up into two parts (the part which parsed,
484
+ // and the part which didn't), so we can color them differently.
485
+ if (i < input.length - 1) {
486
+ i = furthest;
487
+ lines = input.split('\n');
488
+ line = (input.slice(0, i).match(/\n/g) || "").length + 1;
489
+
490
+ for (var n = i, column = -1; n >= 0 && input.charAt(n) !== '\n'; n--) { column++ }
491
+
492
+ error = {
493
+ name: "ParseError",
494
+ message: "Syntax Error on line " + line,
495
+ index: i,
496
+ filename: env.filename,
497
+ line: line,
498
+ column: column,
499
+ extract: [
500
+ lines[line - 2],
501
+ lines[line - 1],
502
+ lines[line]
503
+ ]
504
+ };
505
+ }
506
+
507
+ if (this.imports.queue.length > 0) {
508
+ finish = function () { callback(error, root) };
509
+ } else {
510
+ callback(error, root);
511
+ }
512
+ },
513
+
514
+ //
515
+ // Here in, the parsing rules/functions
516
+ //
517
+ // The basic structure of the syntax tree generated is as follows:
518
+ //
519
+ // Ruleset -> Rule -> Value -> Expression -> Entity
520
+ //
521
+ // Here's some LESS code:
522
+ //
523
+ // .class {
524
+ // color: #fff;
525
+ // border: 1px solid #000;
526
+ // width: @w + 4px;
527
+ // > .child {...}
528
+ // }
529
+ //
530
+ // And here's what the parse tree might look like:
531
+ //
532
+ // Ruleset (Selector '.class', [
533
+ // Rule ("color", Value ([Expression [Color #fff]]))
534
+ // Rule ("border", Value ([Expression [Dimension 1px][Keyword "solid"][Color #000]]))
535
+ // Rule ("width", Value ([Expression [Operation "+" [Variable "@w"][Dimension 4px]]]))
536
+ // Ruleset (Selector [Element '>', '.child'], [...])
537
+ // ])
538
+ //
539
+ // In general, most rules will try to parse a token with the `$()` function, and if the return
540
+ // value is truly, will return a new node, of the relevant type. Sometimes, we need to check
541
+ // first, before parsing, that's when we use `peek()`.
542
+ //
543
+ parsers: {
544
+ //
545
+ // The `primary` rule is the *entry* and *exit* point of the parser.
546
+ // The rules here can appear at any level of the parse tree.
547
+ //
548
+ // The recursive nature of the grammar is an interplay between the `block`
549
+ // rule, which represents `{ ... }`, the `ruleset` rule, and this `primary` rule,
550
+ // as represented by this simplified grammar:
551
+ //
552
+ // primary → (ruleset | rule)+
553
+ // ruleset → selector+ block
554
+ // block → '{' primary '}'
555
+ //
556
+ // Only at one point is the primary rule not called from the
557
+ // block rule: at the root level.
558
+ //
559
+ primary: function () {
560
+ var node, root = [];
561
+
562
+ while ((node = $(this.mixin.definition) || $(this.rule) || $(this.ruleset) ||
563
+ $(this.mixin.call) || $(this.comment) || $(this.directive))
564
+ || $(/^[\s\n]+/)) {
565
+ node && root.push(node);
566
+ }
567
+ return root;
568
+ },
569
+
570
+ // We create a Comment node for CSS comments `/* */`,
571
+ // but keep the LeSS comments `//` silent, by just skipping
572
+ // over them.
573
+ comment: function () {
574
+ var comment;
575
+
576
+ if (input.charAt(i) !== '/') return;
577
+
578
+ if (input.charAt(i + 1) === '/') {
579
+ return new(tree.Comment)($(/^\/\/.*/), true);
580
+ } else if (comment = $(/^\/\*(?:[^*]|\*+[^\/*])*\*+\/\n?/)) {
581
+ return new(tree.Comment)(comment);
582
+ }
583
+ },
584
+
585
+ //
586
+ // Entities are tokens which can be found inside an Expression
587
+ //
588
+ entities: {
589
+ //
590
+ // A string, which supports escaping " and '
591
+ //
592
+ // "milky way" 'he\'s the one!'
593
+ //
594
+ quoted: function () {
595
+ var str;
596
+ if (input.charAt(i) !== '"' && input.charAt(i) !== "'") return;
597
+
598
+ if (str = $(/^"((?:[^"\\\r\n]|\\.)*)"|'((?:[^'\\\r\n]|\\.)*)'/)) {
599
+ return new(tree.Quoted)(str[0], str[1] || str[2]);
600
+ }
601
+ },
602
+
603
+ //
604
+ // A catch-all word, such as:
605
+ //
606
+ // black border-collapse
607
+ //
608
+ keyword: function () {
609
+ var k;
610
+ if (k = $(/^[A-Za-z-]+/)) { return new(tree.Keyword)(k) }
611
+ },
612
+
613
+ //
614
+ // A function call
615
+ //
616
+ // rgb(255, 0, 255)
617
+ //
618
+ // We also try to catch IE's `alpha()`, but let the `alpha` parser
619
+ // deal with the details.
620
+ //
621
+ // The arguments are parsed with the `entities.arguments` parser.
622
+ //
623
+ call: function () {
624
+ var name, args;
625
+
626
+ if (! (name = /^([\w-]+|%)\(/.exec(chunks[j]))) return;
627
+
628
+ name = name[1].toLowerCase();
629
+
630
+ if (name === 'url') { return null }
631
+ else { i += name.length }
632
+
633
+ if (name === 'alpha') { return $(this.alpha) }
634
+
635
+ $('('); // Parse the '(' and consume whitespace.
636
+
637
+ args = $(this.entities.arguments);
638
+
639
+ if (! $(')')) return;
640
+
641
+ if (name) { return new(tree.Call)(name, args) }
642
+ },
643
+ arguments: function () {
644
+ var args = [], arg;
645
+
646
+ while (arg = $(this.expression)) {
647
+ args.push(arg);
648
+ if (! $(',')) { break }
649
+ }
650
+ return args;
651
+ },
652
+ literal: function () {
653
+ return $(this.entities.dimension) ||
654
+ $(this.entities.color) ||
655
+ $(this.entities.quoted);
656
+ },
657
+
658
+ //
659
+ // Parse url() tokens
660
+ //
661
+ // We use a specific rule for urls, because they don't really behave like
662
+ // standard function calls. The difference is that the argument doesn't have
663
+ // to be enclosed within a string, so it can't be parsed as an Expression.
664
+ //
665
+ url: function () {
666
+ var value;
667
+
668
+ if (input.charAt(i) !== 'u' || !$(/^url\(/)) return;
669
+ value = $(this.entities.quoted) || $(this.entities.variable) ||
670
+ $(this.entities.dataURI) || $(/^[-\w%@$\/.&=:;#+?]+/) || "";
671
+ if (! $(')')) throw new(Error)("missing closing ) for url()");
672
+
673
+ return new(tree.URL)((value.value || value.data || value instanceof tree.Variable)
674
+ ? value : new(tree.Anonymous)(value), imports.paths);
675
+ },
676
+
677
+ dataURI: function () {
678
+ var obj;
679
+
680
+ if ($(/^data:/)) {
681
+ obj = {};
682
+ obj.mime = $(/^[^\/]+\/[^,;)]+/) || '';
683
+ obj.charset = $(/^;\s*charset=[^,;)]+/) || '';
684
+ obj.base64 = $(/^;\s*base64/) || '';
685
+ obj.data = $(/^,\s*[^)]+/);
686
+
687
+ if (obj.data) { return obj }
688
+ }
689
+ },
690
+
691
+ //
692
+ // A Variable entity, such as `@fink`, in
693
+ //
694
+ // width: @fink + 2px
695
+ //
696
+ // We use a different parser for variable definitions,
697
+ // see `parsers.variable`.
698
+ //
699
+ variable: function () {
700
+ var name, index = i;
701
+
702
+ if (input.charAt(i) === '@' && (name = $(/^@[\w-]+/))) {
703
+ return new(tree.Variable)(name, index);
704
+ }
705
+ },
706
+
707
+ //
708
+ // A Hexadecimal color
709
+ //
710
+ // #4F3C2F
711
+ //
712
+ // `rgb` and `hsl` colors are parsed through the `entities.call` parser.
713
+ //
714
+ color: function () {
715
+ var rgb;
716
+
717
+ if (input.charAt(i) === '#' && (rgb = $(/^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})/))) {
718
+ return new(tree.Color)(rgb[1]);
719
+ }
720
+ },
721
+
722
+ //
723
+ // A Dimension, that is, a number and a unit
724
+ //
725
+ // 0.5em 95%
726
+ //
727
+ dimension: function () {
728
+ var value, c = input.charCodeAt(i);
729
+ if ((c > 57 || c < 45) || c === 47) return;
730
+
731
+ if (value = $(/^(-?\d*\.?\d+)(px|%|em|pc|ex|in|deg|s|ms|pt|cm|mm|rad|grad|turn)?/)) {
732
+ return new(tree.Dimension)(value[1], value[2]);
733
+ }
734
+ },
735
+
736
+ //
737
+ // JavaScript code to be evaluated
738
+ //
739
+ // `window.location.href`
740
+ //
741
+ javascript: function () {
742
+ var str;
743
+
744
+ if (input.charAt(i) !== '`') { return }
745
+
746
+ if (str = $(/^`([^`]*)`/)) {
747
+ return new(tree.JavaScript)(str[1], i);
748
+ }
749
+ }
750
+ },
751
+
752
+ //
753
+ // The variable part of a variable definition. Used in the `rule` parser
754
+ //
755
+ // @fink:
756
+ //
757
+ variable: function () {
758
+ var name;
759
+
760
+ if (input.charAt(i) === '@' && (name = $(/^(@[\w-]+)\s*:/))) { return name[1] }
761
+ },
762
+
763
+ //
764
+ // A font size/line-height shorthand
765
+ //
766
+ // small/12px
767
+ //
768
+ // We need to peek first, or we'll match on keywords and dimensions
769
+ //
770
+ shorthand: function () {
771
+ var a, b;
772
+
773
+ if (! peek(/^[@\w.%-]+\/[@\w.-]+/)) return;
774
+
775
+ if ((a = $(this.entity)) && $('/') && (b = $(this.entity))) {
776
+ return new(tree.Shorthand)(a, b);
777
+ }
778
+ },
779
+
780
+ //
781
+ // Mixins
782
+ //
783
+ mixin: {
784
+ //
785
+ // A Mixin call, with an optional argument list
786
+ //
787
+ // #mixins > .square(#fff);
788
+ // .rounded(4px, black);
789
+ // .button;
790
+ //
791
+ // The `while` loop is there because mixins can be
792
+ // namespaced, but we only support the child and descendant
793
+ // selector for now.
794
+ //
795
+ call: function () {
796
+ var elements = [], e, c, args, index = i, s = input.charAt(i);
797
+
798
+ if (s !== '.' && s !== '#') { return }
799
+
800
+ while (e = $(/^[#.](?:[\w-]|\\(?:[a-fA-F0-9]{1,6} ?|[^a-fA-F0-9]))+/)) {
801
+ elements.push(new(tree.Element)(c, e));
802
+ c = $('>');
803
+ }
804
+ $('(') && (args = $(this.entities.arguments)) && $(')');
805
+
806
+ if (elements.length > 0 && ($(';') || peek('}'))) {
807
+ return new(tree.mixin.Call)(elements, args, index);
808
+ }
809
+ },
810
+
811
+ //
812
+ // A Mixin definition, with a list of parameters
813
+ //
814
+ // .rounded (@radius: 2px, @color) {
815
+ // ...
816
+ // }
817
+ //
818
+ // Until we have a finer grained state-machine, we have to
819
+ // do a look-ahead, to make sure we don't have a mixin call.
820
+ // See the `rule` function for more information.
821
+ //
822
+ // We start by matching `.rounded (`, and then proceed on to
823
+ // the argument list, which has optional default values.
824
+ // We store the parameters in `params`, with a `value` key,
825
+ // if there is a value, such as in the case of `@radius`.
826
+ //
827
+ // Once we've got our params list, and a closing `)`, we parse
828
+ // the `{...}` block.
829
+ //
830
+ definition: function () {
831
+ var name, params = [], match, ruleset, param, value;
832
+
833
+ if ((input.charAt(i) !== '.' && input.charAt(i) !== '#') ||
834
+ peek(/^[^{]*(;|})/)) return;
835
+
836
+ if (match = $(/^([#.](?:[\w-]|\\(?:[a-fA-F0-9]{1,6} ?|[^a-fA-F0-9]))+)\s*\(/)) {
837
+ name = match[1];
838
+
839
+ while (param = $(this.entities.variable) || $(this.entities.literal)
840
+ || $(this.entities.keyword)) {
841
+ // Variable
842
+ if (param instanceof tree.Variable) {
843
+ if ($(':')) {
844
+ if (value = $(this.expression)) {
845
+ params.push({ name: param.name, value: value });
846
+ } else {
847
+ throw new(Error)("Expected value");
848
+ }
849
+ } else {
850
+ params.push({ name: param.name });
851
+ }
852
+ } else {
853
+ params.push({ value: param });
854
+ }
855
+ if (! $(',')) { break }
856
+ }
857
+ if (! $(')')) throw new(Error)("Expected )");
858
+
859
+ ruleset = $(this.block);
860
+
861
+ if (ruleset) {
862
+ return new(tree.mixin.Definition)(name, params, ruleset);
863
+ }
864
+ }
865
+ }
866
+ },
867
+
868
+ //
869
+ // Entities are the smallest recognized token,
870
+ // and can be found inside a rule's value.
871
+ //
872
+ entity: function () {
873
+ return $(this.entities.literal) || $(this.entities.variable) || $(this.entities.url) ||
874
+ $(this.entities.call) || $(this.entities.keyword) || $(this.entities.javascript) ||
875
+ $(this.comment);
876
+ },
877
+
878
+ //
879
+ // A Rule terminator. Note that we use `peek()` to check for '}',
880
+ // because the `block` rule will be expecting it, but we still need to make sure
881
+ // it's there, if ';' was ommitted.
882
+ //
883
+ end: function () {
884
+ return $(';') || peek('}');
885
+ },
886
+
887
+ //
888
+ // IE's alpha function
889
+ //
890
+ // alpha(opacity=88)
891
+ //
892
+ alpha: function () {
893
+ var value;
894
+
895
+ if (! $(/^opacity=/i)) return;
896
+ if (value = $(/^\d+/) || $(this.entities.variable)) {
897
+ if (! $(')')) throw new(Error)("missing closing ) for alpha()");
898
+ return new(tree.Alpha)(value);
899
+ }
900
+ },
901
+
902
+ //
903
+ // A Selector Element
904
+ //
905
+ // div
906
+ // + h1
907
+ // #socks
908
+ // input[type="text"]
909
+ //
910
+ // Elements are the building blocks for Selectors,
911
+ // they are made out of a `Combinator` (see combinator rule),
912
+ // and an element name, such as a tag a class, or `*`.
913
+ //
914
+ element: function () {
915
+ var e, t, c;
916
+
917
+ c = $(this.combinator);
918
+ e = $(/^(?:[.#]?|:*)(?:[\w-]|\\(?:[a-fA-F0-9]{1,6} ?|[^a-fA-F0-9]))+/) || $('*') || $(this.attribute) || $(/^\([^)@]+\)/);
919
+
920
+ if (e) { return new(tree.Element)(c, e) }
921
+ },
922
+
923
+ //
924
+ // Combinators combine elements together, in a Selector.
925
+ //
926
+ // Because our parser isn't white-space sensitive, special care
927
+ // has to be taken, when parsing the descendant combinator, ` `,
928
+ // as it's an empty space. We have to check the previous character
929
+ // in the input, to see if it's a ` ` character. More info on how
930
+ // we deal with this in *combinator.js*.
931
+ //
932
+ combinator: function () {
933
+ var match, c = input.charAt(i);
934
+
935
+ if (c === '>' || c === '&' || c === '+' || c === '~') {
936
+ i++;
937
+ while (input.charAt(i) === ' ') { i++ }
938
+ return new(tree.Combinator)(c);
939
+ } else if (c === ':' && input.charAt(i + 1) === ':') {
940
+ i += 2;
941
+ while (input.charAt(i) === ' ') { i++ }
942
+ return new(tree.Combinator)('::');
943
+ } else if (input.charAt(i - 1) === ' ') {
944
+ return new(tree.Combinator)(" ");
945
+ } else {
946
+ return new(tree.Combinator)(null);
947
+ }
948
+ },
949
+
950
+ //
951
+ // A CSS Selector
952
+ //
953
+ // .class > div + h1
954
+ // li a:hover
955
+ //
956
+ // Selectors are made out of one or more Elements, see above.
957
+ //
958
+ selector: function () {
959
+ var sel, e, elements = [], c, match;
960
+
961
+ while (e = $(this.element)) {
962
+ c = input.charAt(i);
963
+ elements.push(e)
964
+ if (c === '{' || c === '}' || c === ';' || c === ',') { break }
965
+ }
966
+
967
+ if (elements.length > 0) { return new(tree.Selector)(elements) }
968
+ },
969
+ tag: function () {
970
+ return $(/^[a-zA-Z][a-zA-Z-]*[0-9]?/) || $('*');
971
+ },
972
+ attribute: function () {
973
+ var attr = '', key, val, op;
974
+
975
+ if (! $('[')) return;
976
+
977
+ if (key = $(/^[a-zA-Z-]+/) || $(this.entities.quoted)) {
978
+ if ((op = $(/^[|~*$^]?=/)) &&
979
+ (val = $(this.entities.quoted) || $(/^[\w-]+/))) {
980
+ attr = [key, op, val.toCSS ? val.toCSS() : val].join('');
981
+ } else { attr = key }
982
+ }
983
+
984
+ if (! $(']')) return;
985
+
986
+ if (attr) { return "[" + attr + "]" }
987
+ },
988
+
989
+ //
990
+ // The `block` rule is used by `ruleset` and `mixin.definition`.
991
+ // It's a wrapper around the `primary` rule, with added `{}`.
992
+ //
993
+ block: function () {
994
+ var content;
995
+
996
+ if ($('{') && (content = $(this.primary)) && $('}')) {
997
+ return content;
998
+ }
999
+ },
1000
+
1001
+ //
1002
+ // div, .class, body > p {...}
1003
+ //
1004
+ ruleset: function () {
1005
+ var selectors = [], s, rules, match;
1006
+ save();
1007
+
1008
+ if (match = /^([.#: \w-]+)[\s\n]*\{/.exec(chunks[j])) {
1009
+ i += match[0].length - 1;
1010
+ selectors = [new(tree.Selector)([new(tree.Element)(null, match[1])])];
1011
+ } else {
1012
+ while (s = $(this.selector)) {
1013
+ selectors.push(s);
1014
+ $(this.comment);
1015
+ if (! $(',')) { break }
1016
+ $(this.comment);
1017
+ }
1018
+ }
1019
+
1020
+ if (selectors.length > 0 && (rules = $(this.block))) {
1021
+ return new(tree.Ruleset)(selectors, rules);
1022
+ } else {
1023
+ // Backtrack
1024
+ furthest = i;
1025
+ restore();
1026
+ }
1027
+ },
1028
+ rule: function () {
1029
+ var name, value, c = input.charAt(i), important, match;
1030
+ save();
1031
+
1032
+ if (c === '.' || c === '#' || c === '&') { return }
1033
+
1034
+ if (name = $(this.variable) || $(this.property)) {
1035
+ if ((name.charAt(0) != '@') && (match = /^([^@+\/'"*`(;{}-]*);/.exec(chunks[j]))) {
1036
+ i += match[0].length - 1;
1037
+ value = new(tree.Anonymous)(match[1]);
1038
+ } else if (name === "font") {
1039
+ value = $(this.font);
1040
+ } else {
1041
+ value = $(this.value);
1042
+ }
1043
+ important = $(this.important);
1044
+
1045
+ if (value && $(this.end)) {
1046
+ return new(tree.Rule)(name, value, important, memo);
1047
+ } else {
1048
+ furthest = i;
1049
+ restore();
1050
+ }
1051
+ }
1052
+ },
1053
+
1054
+ //
1055
+ // An @import directive
1056
+ //
1057
+ // @import "lib";
1058
+ //
1059
+ // Depending on our environemnt, importing is done differently:
1060
+ // In the browser, it's an XHR request, in Node, it would be a
1061
+ // file-system operation. The function used for importing is
1062
+ // stored in `import`, which we pass to the Import constructor.
1063
+ //
1064
+ "import": function () {
1065
+ var path;
1066
+ if ($(/^@import\s+/) &&
1067
+ (path = $(this.entities.quoted) || $(this.entities.url)) &&
1068
+ $(';')) {
1069
+ return new(tree.Import)(path, imports);
1070
+ }
1071
+ },
1072
+
1073
+ //
1074
+ // A CSS Directive
1075
+ //
1076
+ // @charset "utf-8";
1077
+ //
1078
+ directive: function () {
1079
+ var name, value, rules, types;
1080
+
1081
+ if (input.charAt(i) !== '@') return;
1082
+
1083
+ if (value = $(this['import'])) {
1084
+ return value;
1085
+ } else if (name = $(/^@media|@page|@-[-a-z]+/)) {
1086
+ types = ($(/^[^{]+/) || '').trim();
1087
+ if (rules = $(this.block)) {
1088
+ return new(tree.Directive)(name + " " + types, rules);
1089
+ }
1090
+ } else if (name = $(/^@[-a-z]+/)) {
1091
+ if (name === '@font-face') {
1092
+ if (rules = $(this.block)) {
1093
+ return new(tree.Directive)(name, rules);
1094
+ }
1095
+ } else if ((value = $(this.entity)) && $(';')) {
1096
+ return new(tree.Directive)(name, value);
1097
+ }
1098
+ }
1099
+ },
1100
+ font: function () {
1101
+ var value = [], expression = [], weight, shorthand, font, e;
1102
+
1103
+ while (e = $(this.shorthand) || $(this.entity)) {
1104
+ expression.push(e);
1105
+ }
1106
+ value.push(new(tree.Expression)(expression));
1107
+
1108
+ if ($(',')) {
1109
+ while (e = $(this.expression)) {
1110
+ value.push(e);
1111
+ if (! $(',')) { break }
1112
+ }
1113
+ }
1114
+ return new(tree.Value)(value);
1115
+ },
1116
+
1117
+ //
1118
+ // A Value is a comma-delimited list of Expressions
1119
+ //
1120
+ // font-family: Baskerville, Georgia, serif;
1121
+ //
1122
+ // In a Rule, a Value represents everything after the `:`,
1123
+ // and before the `;`.
1124
+ //
1125
+ value: function () {
1126
+ var e, expressions = [], important;
1127
+
1128
+ while (e = $(this.expression)) {
1129
+ expressions.push(e);
1130
+ if (! $(',')) { break }
1131
+ }
1132
+
1133
+ if (expressions.length > 0) {
1134
+ return new(tree.Value)(expressions);
1135
+ }
1136
+ },
1137
+ important: function () {
1138
+ if (input.charAt(i) === '!') {
1139
+ return $(/^! *important/);
1140
+ }
1141
+ },
1142
+ sub: function () {
1143
+ var e;
1144
+
1145
+ if ($('(') && (e = $(this.expression)) && $(')')) {
1146
+ return e;
1147
+ }
1148
+ },
1149
+ multiplication: function () {
1150
+ var m, a, op, operation;
1151
+ if (m = $(this.operand)) {
1152
+ while ((op = ($('/') || $('*'))) && (a = $(this.operand))) {
1153
+ operation = new(tree.Operation)(op, [operation || m, a]);
1154
+ }
1155
+ return operation || m;
1156
+ }
1157
+ },
1158
+ addition: function () {
1159
+ var m, a, op, operation;
1160
+ if (m = $(this.multiplication)) {
1161
+ while ((op = $(/^[-+]\s+/) || (input.charAt(i - 1) != ' ' && ($('+') || $('-')))) &&
1162
+ (a = $(this.multiplication))) {
1163
+ operation = new(tree.Operation)(op, [operation || m, a]);
1164
+ }
1165
+ return operation || m;
1166
+ }
1167
+ },
1168
+
1169
+ //
1170
+ // An operand is anything that can be part of an operation,
1171
+ // such as a Color, or a Variable
1172
+ //
1173
+ operand: function () {
1174
+ var negate, p = input.charAt(i + 1);
1175
+
1176
+ if (input.charAt(i) === '-' && (p === '@' || p === '(')) { negate = $('-') }
1177
+ var o = $(this.sub) || $(this.entities.dimension) ||
1178
+ $(this.entities.color) || $(this.entities.variable) ||
1179
+ $(this.entities.call);
1180
+ return negate ? new(tree.Operation)('*', [new(tree.Dimension)(-1), o])
1181
+ : o;
1182
+ },
1183
+
1184
+ //
1185
+ // Expressions either represent mathematical operations,
1186
+ // or white-space delimited Entities.
1187
+ //
1188
+ // 1px solid black
1189
+ // @var * 2
1190
+ //
1191
+ expression: function () {
1192
+ var e, delim, entities = [], d;
1193
+
1194
+ while (e = $(this.addition) || $(this.entity)) {
1195
+ entities.push(e);
1196
+ }
1197
+ if (entities.length > 0) {
1198
+ return new(tree.Expression)(entities);
1199
+ }
1200
+ },
1201
+ property: function () {
1202
+ var name;
1203
+
1204
+ if (name = $(/^(\*?-?[-a-z_0-9]+)\s*:/)) {
1205
+ return name[1];
1206
+ }
1207
+ }
1208
+ }
1209
+ };
1210
+ };
1211
+
1212
+ if (typeof(window) !== 'undefined') {
1213
+ //
1214
+ // Used by `@import` directives
1215
+ //
1216
+ less.Parser.importer = function (path, paths, callback, env) {
1217
+ if (path.charAt(0) !== '/' && paths.length > 0) {
1218
+ path = paths[0] + path;
1219
+ }
1220
+ // We pass `true` as 3rd argument, to force the reload of the import.
1221
+ // This is so we can get the syntax tree as opposed to just the CSS output,
1222
+ // as we need this to evaluate the current stylesheet.
1223
+ loadStyleSheet({ href: path, title: path, type: env.mime }, callback, true);
1224
+ };
1225
+ }
1226
+
1227
+ (function (tree) {
1228
+
1229
+ tree.functions = {
1230
+ rgb: function (r, g, b) {
1231
+ return this.rgba(r, g, b, 1.0);
1232
+ },
1233
+ rgba: function (r, g, b, a) {
1234
+ var rgb = [r, g, b].map(function (c) { return number(c) }),
1235
+ a = number(a);
1236
+ return new(tree.Color)(rgb, a);
1237
+ },
1238
+ hsl: function (h, s, l) {
1239
+ return this.hsla(h, s, l, 1.0);
1240
+ },
1241
+ hsla: function (h, s, l, a) {
1242
+ h = (number(h) % 360) / 360;
1243
+ s = number(s); l = number(l); a = number(a);
1244
+
1245
+ var m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s;
1246
+ var m1 = l * 2 - m2;
1247
+
1248
+ return this.rgba(hue(h + 1/3) * 255,
1249
+ hue(h) * 255,
1250
+ hue(h - 1/3) * 255,
1251
+ a);
1252
+
1253
+ function hue(h) {
1254
+ h = h < 0 ? h + 1 : (h > 1 ? h - 1 : h);
1255
+ if (h * 6 < 1) return m1 + (m2 - m1) * h * 6;
1256
+ else if (h * 2 < 1) return m2;
1257
+ else if (h * 3 < 2) return m1 + (m2 - m1) * (2/3 - h) * 6;
1258
+ else return m1;
1259
+ }
1260
+ },
1261
+ hue: function (color) {
1262
+ return new(tree.Dimension)(Math.round(color.toHSL().h));
1263
+ },
1264
+ saturation: function (color) {
1265
+ return new(tree.Dimension)(Math.round(color.toHSL().s * 100), '%');
1266
+ },
1267
+ lightness: function (color) {
1268
+ return new(tree.Dimension)(Math.round(color.toHSL().l * 100), '%');
1269
+ },
1270
+ alpha: function (color) {
1271
+ return new(tree.Dimension)(color.toHSL().a);
1272
+ },
1273
+ saturate: function (color, amount) {
1274
+ var hsl = color.toHSL();
1275
+
1276
+ hsl.s += amount.value / 100;
1277
+ hsl.s = clamp(hsl.s);
1278
+ return hsla(hsl);
1279
+ },
1280
+ desaturate: function (color, amount) {
1281
+ var hsl = color.toHSL();
1282
+
1283
+ hsl.s -= amount.value / 100;
1284
+ hsl.s = clamp(hsl.s);
1285
+ return hsla(hsl);
1286
+ },
1287
+ lighten: function (color, amount) {
1288
+ var hsl = color.toHSL();
1289
+
1290
+ hsl.l += amount.value / 100;
1291
+ hsl.l = clamp(hsl.l);
1292
+ return hsla(hsl);
1293
+ },
1294
+ darken: function (color, amount) {
1295
+ var hsl = color.toHSL();
1296
+
1297
+ hsl.l -= amount.value / 100;
1298
+ hsl.l = clamp(hsl.l);
1299
+ return hsla(hsl);
1300
+ },
1301
+ fadein: function (color, amount) {
1302
+ var hsl = color.toHSL();
1303
+
1304
+ hsl.a += amount.value / 100;
1305
+ hsl.a = clamp(hsl.a);
1306
+ return hsla(hsl);
1307
+ },
1308
+ fadeout: function (color, amount) {
1309
+ var hsl = color.toHSL();
1310
+
1311
+ hsl.a -= amount.value / 100;
1312
+ hsl.a = clamp(hsl.a);
1313
+ return hsla(hsl);
1314
+ },
1315
+ spin: function (color, amount) {
1316
+ var hsl = color.toHSL();
1317
+ var hue = (hsl.h + amount.value) % 360;
1318
+
1319
+ hsl.h = hue < 0 ? 360 + hue : hue;
1320
+
1321
+ return hsla(hsl);
1322
+ },
1323
+ //
1324
+ // Copyright (c) 2006-2009 Hampton Catlin, Nathan Weizenbaum, and Chris Eppstein
1325
+ // http://sass-lang.com
1326
+ //
1327
+ mix: function (color1, color2, weight) {
1328
+ var p = weight.value / 100.0;
1329
+ var w = p * 2 - 1;
1330
+ var a = color1.toHSL().a - color2.toHSL().a;
1331
+
1332
+ var w1 = (((w * a == -1) ? w : (w + a) / (1 + w * a)) + 1) / 2.0;
1333
+ var w2 = 1 - w1;
1334
+
1335
+ var rgb = [color1.rgb[0] * w1 + color2.rgb[0] * w2,
1336
+ color1.rgb[1] * w1 + color2.rgb[1] * w2,
1337
+ color1.rgb[2] * w1 + color2.rgb[2] * w2];
1338
+
1339
+ var alpha = color1.alpha * p + color2.alpha * (1 - p);
1340
+
1341
+ return new(tree.Color)(rgb, alpha);
1342
+ },
1343
+ greyscale: function (color) {
1344
+ return this.desaturate(color, new(tree.Dimension)(100));
1345
+ },
1346
+ e: function (str) {
1347
+ return new(tree.Anonymous)(str instanceof tree.JavaScript ? str.evaluated : str);
1348
+ },
1349
+ escape: function (str) {
1350
+ 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"));
1351
+ },
1352
+ '%': function (quoted /* arg, arg, ...*/) {
1353
+ var args = Array.prototype.slice.call(arguments, 1),
1354
+ str = quoted.value;
1355
+
1356
+ for (var i = 0; i < args.length; i++) {
1357
+ str = str.replace(/%[sda]/i, function(token) {
1358
+ var value = token.match(/s/i) ? args[i].value : args[i].toCSS();
1359
+ return token.match(/[A-Z]$/) ? encodeURIComponent(value) : value;
1360
+ });
1361
+ }
1362
+ str = str.replace(/%%/g, '%');
1363
+ return new(tree.Quoted)('"' + str + '"', str);
1364
+ },
1365
+ round: function (n) {
1366
+ if (n instanceof tree.Dimension) {
1367
+ return new(tree.Dimension)(Math.round(number(n)), n.unit);
1368
+ } else if (typeof(n) === 'number') {
1369
+ return Math.round(n);
1370
+ } else {
1371
+ throw {
1372
+ error: "RuntimeError",
1373
+ message: "math functions take numbers as parameters"
1374
+ };
1375
+ }
1376
+ }
1377
+ };
1378
+
1379
+ function hsla(hsla) {
1380
+ return tree.functions.hsla(hsla.h, hsla.s, hsla.l, hsla.a);
1381
+ }
1382
+
1383
+ function number(n) {
1384
+ if (n instanceof tree.Dimension) {
1385
+ return parseFloat(n.unit == '%' ? n.value / 100 : n.value);
1386
+ } else if (typeof(n) === 'number') {
1387
+ return n;
1388
+ } else {
1389
+ throw {
1390
+ error: "RuntimeError",
1391
+ message: "color functions take numbers as parameters"
1392
+ };
1393
+ }
1394
+ }
1395
+
1396
+ function clamp(val) {
1397
+ return Math.min(1, Math.max(0, val));
1398
+ }
1399
+
1400
+ })(require('less/tree'));
1401
+ (function (tree) {
1402
+
1403
+ tree.Alpha = function (val) {
1404
+ this.value = val;
1405
+ };
1406
+ tree.Alpha.prototype = {
1407
+ toCSS: function () {
1408
+ return "alpha(opacity=" +
1409
+ (this.value.toCSS ? this.value.toCSS() : this.value) + ")";
1410
+ },
1411
+ eval: function () { return this }
1412
+ };
1413
+
1414
+ })(require('less/tree'));
1415
+ (function (tree) {
1416
+
1417
+ tree.Anonymous = function (string) {
1418
+ this.value = string.value || string;
1419
+ };
1420
+ tree.Anonymous.prototype = {
1421
+ toCSS: function () {
1422
+ return this.value;
1423
+ },
1424
+ eval: function () { return this }
1425
+ };
1426
+
1427
+ })(require('less/tree'));
1428
+ (function (tree) {
1429
+
1430
+ //
1431
+ // A function call node.
1432
+ //
1433
+ tree.Call = function (name, args) {
1434
+ this.name = name;
1435
+ this.args = args;
1436
+ };
1437
+ tree.Call.prototype = {
1438
+ //
1439
+ // When evaluating a function call,
1440
+ // we either find the function in `tree.functions` [1],
1441
+ // in which case we call it, passing the evaluated arguments,
1442
+ // or we simply print it out as it appeared originally [2].
1443
+ //
1444
+ // The *functions.js* file contains the built-in functions.
1445
+ //
1446
+ // The reason why we evaluate the arguments, is in the case where
1447
+ // we try to pass a variable to a function, like: `saturate(@color)`.
1448
+ // The function should receive the value, not the variable.
1449
+ //
1450
+ eval: function (env) {
1451
+ var args = this.args.map(function (a) { return a.eval(env) });
1452
+
1453
+ if (this.name in tree.functions) { // 1.
1454
+ return tree.functions[this.name].apply(tree.functions, args);
1455
+ } else { // 2.
1456
+ return new(tree.Anonymous)(this.name +
1457
+ "(" + args.map(function (a) { return a.toCSS() }).join(', ') + ")");
1458
+ }
1459
+ },
1460
+
1461
+ toCSS: function (env) {
1462
+ return this.eval(env).toCSS();
1463
+ }
1464
+ };
1465
+
1466
+ })(require('less/tree'));
1467
+ (function (tree) {
1468
+ //
1469
+ // RGB Colors - #ff0014, #eee
1470
+ //
1471
+ tree.Color = function (rgb, a) {
1472
+ //
1473
+ // The end goal here, is to parse the arguments
1474
+ // into an integer triplet, such as `128, 255, 0`
1475
+ //
1476
+ // This facilitates operations and conversions.
1477
+ //
1478
+ if (Array.isArray(rgb)) {
1479
+ this.rgb = rgb;
1480
+ } else if (rgb.length == 6) {
1481
+ this.rgb = rgb.match(/.{2}/g).map(function (c) {
1482
+ return parseInt(c, 16);
1483
+ });
1484
+ } else if (rgb.length == 8) {
1485
+ this.alpha = parseInt(rgb.substring(0,2), 16) / 255.0;
1486
+ this.rgb = rgb.substr(2).match(/.{2}/g).map(function (c) {
1487
+ return parseInt(c, 16);
1488
+ });
1489
+ } else {
1490
+ this.rgb = rgb.split('').map(function (c) {
1491
+ return parseInt(c + c, 16);
1492
+ });
1493
+ }
1494
+ this.alpha = typeof(a) === 'number' ? a : 1;
1495
+ };
1496
+ tree.Color.prototype = {
1497
+ eval: function () { return this },
1498
+
1499
+ //
1500
+ // If we have some transparency, the only way to represent it
1501
+ // is via `rgba`. Otherwise, we use the hex representation,
1502
+ // which has better compatibility with older browsers.
1503
+ // Values are capped between `0` and `255`, rounded and zero-padded.
1504
+ //
1505
+ toCSS: function () {
1506
+ if (this.alpha < 1.0) {
1507
+ return "rgba(" + this.rgb.map(function (c) {
1508
+ return Math.round(c);
1509
+ }).concat(this.alpha).join(', ') + ")";
1510
+ } else {
1511
+ return '#' + this.rgb.map(function (i) {
1512
+ i = Math.round(i);
1513
+ i = (i > 255 ? 255 : (i < 0 ? 0 : i)).toString(16);
1514
+ return i.length === 1 ? '0' + i : i;
1515
+ }).join('');
1516
+ }
1517
+ },
1518
+
1519
+ //
1520
+ // Operations have to be done per-channel, if not,
1521
+ // channels will spill onto each other. Once we have
1522
+ // our result, in the form of an integer triplet,
1523
+ // we create a new Color node to hold the result.
1524
+ //
1525
+ operate: function (op, other) {
1526
+ var result = [];
1527
+
1528
+ if (! (other instanceof tree.Color)) {
1529
+ other = other.toColor();
1530
+ }
1531
+
1532
+ for (var c = 0; c < 3; c++) {
1533
+ result[c] = tree.operate(op, this.rgb[c], other.rgb[c]);
1534
+ }
1535
+ return new(tree.Color)(result, this.alpha + other.alpha);
1536
+ },
1537
+
1538
+ toHSL: function () {
1539
+ var r = this.rgb[0] / 255,
1540
+ g = this.rgb[1] / 255,
1541
+ b = this.rgb[2] / 255,
1542
+ a = this.alpha;
1543
+
1544
+ var max = Math.max(r, g, b), min = Math.min(r, g, b);
1545
+ var h, s, l = (max + min) / 2, d = max - min;
1546
+
1547
+ if (max === min) {
1548
+ h = s = 0;
1549
+ } else {
1550
+ s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
1551
+
1552
+ switch (max) {
1553
+ case r: h = (g - b) / d + (g < b ? 6 : 0); break;
1554
+ case g: h = (b - r) / d + 2; break;
1555
+ case b: h = (r - g) / d + 4; break;
1556
+ }
1557
+ h /= 6;
1558
+ }
1559
+ return { h: h * 360, s: s, l: l, a: a };
1560
+ }
1561
+ };
1562
+
1563
+
1564
+ })(require('less/tree'));
1565
+ (function (tree) {
1566
+
1567
+ tree.Comment = function (value, silent) {
1568
+ this.value = value;
1569
+ this.silent = !!silent;
1570
+ };
1571
+ tree.Comment.prototype = {
1572
+ toCSS: function (env) {
1573
+ return env.compress ? '' : this.value;
1574
+ },
1575
+ eval: function () { return this }
1576
+ };
1577
+
1578
+ })(require('less/tree'));
1579
+ (function (tree) {
1580
+
1581
+ //
1582
+ // A number with a unit
1583
+ //
1584
+ tree.Dimension = function (value, unit) {
1585
+ this.value = parseFloat(value);
1586
+ this.unit = unit || null;
1587
+ };
1588
+
1589
+ tree.Dimension.prototype = {
1590
+ eval: function () { return this },
1591
+ toColor: function () {
1592
+ return new(tree.Color)([this.value, this.value, this.value]);
1593
+ },
1594
+ toCSS: function () {
1595
+ var css = this.value + this.unit;
1596
+ return css;
1597
+ },
1598
+
1599
+ // In an operation between two Dimensions,
1600
+ // we default to the first Dimension's unit,
1601
+ // so `1px + 2em` will yield `3px`.
1602
+ // In the future, we could implement some unit
1603
+ // conversions such that `100cm + 10mm` would yield
1604
+ // `101cm`.
1605
+ operate: function (op, other) {
1606
+ return new(tree.Dimension)
1607
+ (tree.operate(op, this.value, other.value),
1608
+ this.unit || other.unit);
1609
+ }
1610
+ };
1611
+
1612
+ })(require('less/tree'));
1613
+ (function (tree) {
1614
+
1615
+ tree.Directive = function (name, value) {
1616
+ this.name = name;
1617
+ if (Array.isArray(value)) {
1618
+ this.ruleset = new(tree.Ruleset)([], value);
1619
+ } else {
1620
+ this.value = value;
1621
+ }
1622
+ };
1623
+ tree.Directive.prototype = {
1624
+ toCSS: function (ctx, env) {
1625
+ if (this.ruleset) {
1626
+ this.ruleset.root = true;
1627
+ return this.name + (env.compress ? '{' : ' {\n ') +
1628
+ this.ruleset.toCSS(ctx, env).trim().replace(/\n/g, '\n ') +
1629
+ (env.compress ? '}': '\n}\n');
1630
+ } else {
1631
+ return this.name + ' ' + this.value.toCSS() + ';\n';
1632
+ }
1633
+ },
1634
+ eval: function (env) {
1635
+ env.frames.unshift(this);
1636
+ this.ruleset = this.ruleset && this.ruleset.eval(env);
1637
+ env.frames.shift();
1638
+ return this;
1639
+ },
1640
+ variable: function (name) { return tree.Ruleset.prototype.variable.call(this.ruleset, name) },
1641
+ find: function () { return tree.Ruleset.prototype.find.apply(this.ruleset, arguments) },
1642
+ rulesets: function () { return tree.Ruleset.prototype.rulesets.apply(this.ruleset) }
1643
+ };
1644
+
1645
+ })(require('less/tree'));
1646
+ (function (tree) {
1647
+
1648
+ tree.Element = function (combinator, value) {
1649
+ this.combinator = combinator instanceof tree.Combinator ?
1650
+ combinator : new(tree.Combinator)(combinator);
1651
+ this.value = value.trim();
1652
+ };
1653
+ tree.Element.prototype.toCSS = function (env) {
1654
+ return this.combinator.toCSS(env || {}) + this.value;
1655
+ };
1656
+
1657
+ tree.Combinator = function (value) {
1658
+ if (value === ' ') {
1659
+ this.value = ' ';
1660
+ } else {
1661
+ this.value = value ? value.trim() : "";
1662
+ }
1663
+ };
1664
+ tree.Combinator.prototype.toCSS = function (env) {
1665
+ return {
1666
+ '' : '',
1667
+ ' ' : ' ',
1668
+ '&' : '',
1669
+ ':' : ' :',
1670
+ '::': '::',
1671
+ '+' : env.compress ? '+' : ' + ',
1672
+ '~' : env.compress ? '~' : ' ~ ',
1673
+ '>' : env.compress ? '>' : ' > '
1674
+ }[this.value];
1675
+ };
1676
+
1677
+ })(require('less/tree'));
1678
+ (function (tree) {
1679
+
1680
+ tree.Expression = function (value) { this.value = value };
1681
+ tree.Expression.prototype = {
1682
+ eval: function (env) {
1683
+ if (this.value.length > 1) {
1684
+ return new(tree.Expression)(this.value.map(function (e) {
1685
+ return e.eval(env);
1686
+ }));
1687
+ } else {
1688
+ return this.value[0].eval(env);
1689
+ }
1690
+ },
1691
+ toCSS: function (env) {
1692
+ return this.value.map(function (e) {
1693
+ return e.toCSS(env);
1694
+ }).join(' ');
1695
+ }
1696
+ };
1697
+
1698
+ })(require('less/tree'));
1699
+ (function (tree) {
1700
+ //
1701
+ // CSS @import node
1702
+ //
1703
+ // The general strategy here is that we don't want to wait
1704
+ // for the parsing to be completed, before we start importing
1705
+ // the file. That's because in the context of a browser,
1706
+ // most of the time will be spent waiting for the server to respond.
1707
+ //
1708
+ // On creation, we push the import path to our import queue, though
1709
+ // `import,push`, we also pass it a callback, which it'll call once
1710
+ // the file has been fetched, and parsed.
1711
+ //
1712
+ tree.Import = function (path, imports) {
1713
+ var that = this;
1714
+
1715
+ this._path = path;
1716
+
1717
+ // The '.less' extension is optional
1718
+ if (path instanceof tree.Quoted) {
1719
+ this.path = /\.(le?|c)ss$/.test(path.value) ? path.value : path.value + '.less';
1720
+ } else {
1721
+ this.path = path.value.value || path.value;
1722
+ }
1723
+
1724
+ this.css = /css$/.test(this.path);
1725
+
1726
+ // Only pre-compile .less files
1727
+ if (! this.css) {
1728
+ imports.push(this.path, function (root) {
1729
+ if (! root) {
1730
+ throw new(Error)("Error parsing " + that.path);
1731
+ }
1732
+ that.root = root;
1733
+ });
1734
+ }
1735
+ };
1736
+
1737
+ //
1738
+ // The actual import node doesn't return anything, when converted to CSS.
1739
+ // The reason is that it's used at the evaluation stage, so that the rules
1740
+ // it imports can be treated like any other rules.
1741
+ //
1742
+ // In `eval`, we make sure all Import nodes get evaluated, recursively, so
1743
+ // we end up with a flat structure, which can easily be imported in the parent
1744
+ // ruleset.
1745
+ //
1746
+ tree.Import.prototype = {
1747
+ toCSS: function () {
1748
+ if (this.css) {
1749
+ return "@import " + this._path.toCSS() + ';\n';
1750
+ } else {
1751
+ return "";
1752
+ }
1753
+ },
1754
+ eval: function (env) {
1755
+ var ruleset;
1756
+
1757
+ if (this.css) {
1758
+ return this;
1759
+ } else {
1760
+ ruleset = new(tree.Ruleset)(null, this.root.rules.slice(0));
1761
+
1762
+ for (var i = 0; i < ruleset.rules.length; i++) {
1763
+ if (ruleset.rules[i] instanceof tree.Import) {
1764
+ Array.prototype
1765
+ .splice
1766
+ .apply(ruleset.rules,
1767
+ [i, 1].concat(ruleset.rules[i].eval(env)));
1768
+ }
1769
+ }
1770
+ return ruleset.rules;
1771
+ }
1772
+ }
1773
+ };
1774
+
1775
+ })(require('less/tree'));
1776
+ (function (tree) {
1777
+
1778
+ tree.JavaScript = function (string, index) {
1779
+ this.expression = string;
1780
+ this.index = index;
1781
+ };
1782
+ tree.JavaScript.prototype = {
1783
+ toCSS: function () {
1784
+ return JSON.stringify(this.evaluated);
1785
+ },
1786
+ eval: function (env) {
1787
+ var result,
1788
+ expression = new(Function)('return (' + this.expression + ')'),
1789
+ context = {};
1790
+
1791
+ for (var k in env.frames[0].variables()) {
1792
+ context[k.slice(1)] = {
1793
+ value: env.frames[0].variables()[k].value,
1794
+ toJS: function () {
1795
+ return this.value.eval(env).toCSS();
1796
+ }
1797
+ };
1798
+ }
1799
+
1800
+ try {
1801
+ this.evaluated = expression.call(context);
1802
+ } catch (e) {
1803
+ throw { message: "JavaScript evaluation error: '" + e.name + ': ' + e.message + "'" ,
1804
+ index: this.index };
1805
+ }
1806
+ return this;
1807
+ }
1808
+ };
1809
+
1810
+ })(require('less/tree'));
1811
+
1812
+ (function (tree) {
1813
+
1814
+ tree.Keyword = function (value) { this.value = value };
1815
+ tree.Keyword.prototype = {
1816
+ eval: function () { return this },
1817
+ toCSS: function () { return this.value }
1818
+ };
1819
+
1820
+ })(require('less/tree'));
1821
+ (function (tree) {
1822
+
1823
+ tree.mixin = {};
1824
+ tree.mixin.Call = function (elements, args, index) {
1825
+ this.selector = new(tree.Selector)(elements);
1826
+ this.arguments = args;
1827
+ this.index = index;
1828
+ };
1829
+ tree.mixin.Call.prototype = {
1830
+ eval: function (env) {
1831
+ var mixins, rules = [], match = false;
1832
+
1833
+ for (var i = 0; i < env.frames.length; i++) {
1834
+ if ((mixins = env.frames[i].find(this.selector)).length > 0) {
1835
+ for (var m = 0; m < mixins.length; m++) {
1836
+ if (mixins[m].match(this.arguments, env)) {
1837
+ try {
1838
+ Array.prototype.push.apply(
1839
+ rules, mixins[m].eval(env, this.arguments).rules);
1840
+ match = true;
1841
+ } catch (e) {
1842
+ throw { message: e.message, index: e.index, stack: e.stack, call: this.index };
1843
+ }
1844
+ }
1845
+ }
1846
+ if (match) {
1847
+ return rules;
1848
+ } else {
1849
+ throw { message: 'No matching definition was found for `' +
1850
+ this.selector.toCSS().trim() + '(' +
1851
+ this.arguments.map(function (a) {
1852
+ return a.toCSS();
1853
+ }).join(', ') + ")`",
1854
+ index: this.index };
1855
+ }
1856
+ }
1857
+ }
1858
+ throw { message: this.selector.toCSS().trim() + " is undefined",
1859
+ index: this.index };
1860
+ }
1861
+ };
1862
+
1863
+ tree.mixin.Definition = function (name, params, rules) {
1864
+ this.name = name;
1865
+ this.selectors = [new(tree.Selector)([new(tree.Element)(null, name)])];
1866
+ this.params = params;
1867
+ this.arity = params.length;
1868
+ this.rules = rules;
1869
+ this._lookups = {};
1870
+ this.required = params.reduce(function (count, p) {
1871
+ if (!p.name || (p.name && !p.value)) { return count + 1 }
1872
+ else { return count }
1873
+ }, 0);
1874
+ this.parent = tree.Ruleset.prototype;
1875
+ this.frames = [];
1876
+ };
1877
+ tree.mixin.Definition.prototype = {
1878
+ toCSS: function () { return "" },
1879
+ variable: function (name) { return this.parent.variable.call(this, name) },
1880
+ variables: function () { return this.parent.variables.call(this) },
1881
+ find: function () { return this.parent.find.apply(this, arguments) },
1882
+ rulesets: function () { return this.parent.rulesets.apply(this) },
1883
+
1884
+ eval: function (env, args) {
1885
+ var frame = new(tree.Ruleset)(null, []), context;
1886
+
1887
+ for (var i = 0, val; i < this.params.length; i++) {
1888
+ if (this.params[i].name) {
1889
+ if (val = (args && args[i]) || this.params[i].value) {
1890
+ frame.rules.unshift(new(tree.Rule)(this.params[i].name, val.eval(env)));
1891
+ } else {
1892
+ throw { message: "wrong number of arguments for " + this.name +
1893
+ ' (' + args.length + ' for ' + this.arity + ')' };
1894
+ }
1895
+ }
1896
+ }
1897
+ frame.rules.unshift(new(tree.Rule)('@arguments', new(tree.Expression)(args)));
1898
+
1899
+ return new(tree.Ruleset)(null, this.rules.slice(0)).eval({
1900
+ frames: [this, frame].concat(this.frames, env.frames)
1901
+ });
1902
+ },
1903
+ match: function (args, env) {
1904
+ var argsLength = (args && args.length) || 0, len;
1905
+
1906
+ if (argsLength < this.required) { return false }
1907
+ if ((this.required > 0) && (argsLength > this.params.length)) { return false }
1908
+
1909
+ len = Math.min(argsLength, this.arity);
1910
+
1911
+ for (var i = 0; i < len; i++) {
1912
+ if (!this.params[i].name) {
1913
+ if (args[i].eval(env).toCSS() != this.params[i].value.eval(env).toCSS()) {
1914
+ return false;
1915
+ }
1916
+ }
1917
+ }
1918
+ return true;
1919
+ }
1920
+ };
1921
+
1922
+ })(require('less/tree'));
1923
+ (function (tree) {
1924
+
1925
+ tree.Operation = function (op, operands) {
1926
+ this.op = op.trim();
1927
+ this.operands = operands;
1928
+ };
1929
+ tree.Operation.prototype.eval = function (env) {
1930
+ var a = this.operands[0].eval(env),
1931
+ b = this.operands[1].eval(env),
1932
+ temp;
1933
+
1934
+ if (a instanceof tree.Dimension && b instanceof tree.Color) {
1935
+ if (this.op === '*' || this.op === '+') {
1936
+ temp = b, b = a, a = temp;
1937
+ } else {
1938
+ throw { name: "OperationError",
1939
+ message: "Can't substract or divide a color from a number" };
1940
+ }
1941
+ }
1942
+ return a.operate(this.op, b);
1943
+ };
1944
+
1945
+ tree.operate = function (op, a, b) {
1946
+ switch (op) {
1947
+ case '+': return a + b;
1948
+ case '-': return a - b;
1949
+ case '*': return a * b;
1950
+ case '/': return a / b;
1951
+ }
1952
+ };
1953
+
1954
+ })(require('less/tree'));
1955
+ (function (tree) {
1956
+
1957
+ tree.Quoted = function (str, content) {
1958
+ this.value = content || '';
1959
+ this.quote = str.charAt(0);
1960
+ };
1961
+ tree.Quoted.prototype = {
1962
+ toCSS: function () {
1963
+ return this.quote + this.value + this.quote;
1964
+ },
1965
+ eval: function () {
1966
+ return this;
1967
+ }
1968
+ };
1969
+
1970
+ })(require('less/tree'));
1971
+ (function (tree) {
1972
+
1973
+ tree.Rule = function (name, value, important, index) {
1974
+ this.name = name;
1975
+ this.value = (value instanceof tree.Value) ? value : new(tree.Value)([value]);
1976
+ this.important = important ? ' ' + important.trim() : '';
1977
+ this.index = index;
1978
+
1979
+ if (name.charAt(0) === '@') {
1980
+ this.variable = true;
1981
+ } else { this.variable = false }
1982
+ };
1983
+ tree.Rule.prototype.toCSS = function (env) {
1984
+ if (this.variable) { return "" }
1985
+ else {
1986
+ return this.name + (env.compress ? ':' : ': ') +
1987
+ this.value.toCSS(env) +
1988
+ this.important + ";";
1989
+ }
1990
+ };
1991
+
1992
+ tree.Rule.prototype.eval = function (context) {
1993
+ return new(tree.Rule)(this.name, this.value.eval(context), this.important, this.index);
1994
+ };
1995
+
1996
+ tree.Shorthand = function (a, b) {
1997
+ this.a = a;
1998
+ this.b = b;
1999
+ };
2000
+
2001
+ tree.Shorthand.prototype = {
2002
+ toCSS: function (env) {
2003
+ return this.a.toCSS(env) + "/" + this.b.toCSS(env);
2004
+ },
2005
+ eval: function () { return this }
2006
+ };
2007
+
2008
+ })(require('less/tree'));
2009
+ (function (tree) {
2010
+
2011
+ tree.Ruleset = function (selectors, rules) {
2012
+ this.selectors = selectors;
2013
+ this.rules = rules;
2014
+ this._lookups = {};
2015
+ };
2016
+ tree.Ruleset.prototype = {
2017
+ eval: function (env) {
2018
+ var ruleset = new(tree.Ruleset)(this.selectors, this.rules.slice(0));
2019
+
2020
+ ruleset.root = this.root;
2021
+
2022
+ // push the current ruleset to the frames stack
2023
+ env.frames.unshift(ruleset);
2024
+
2025
+ // Evaluate imports
2026
+ if (ruleset.root) {
2027
+ for (var i = 0; i < ruleset.rules.length; i++) {
2028
+ if (ruleset.rules[i] instanceof tree.Import) {
2029
+ Array.prototype.splice
2030
+ .apply(ruleset.rules, [i, 1].concat(ruleset.rules[i].eval(env)));
2031
+ }
2032
+ }
2033
+ }
2034
+
2035
+ // Store the frames around mixin definitions,
2036
+ // so they can be evaluated like closures when the time comes.
2037
+ for (var i = 0; i < ruleset.rules.length; i++) {
2038
+ if (ruleset.rules[i] instanceof tree.mixin.Definition) {
2039
+ ruleset.rules[i].frames = env.frames.slice(0);
2040
+ }
2041
+ }
2042
+
2043
+ // Evaluate mixin calls.
2044
+ for (var i = 0; i < ruleset.rules.length; i++) {
2045
+ if (ruleset.rules[i] instanceof tree.mixin.Call) {
2046
+ Array.prototype.splice
2047
+ .apply(ruleset.rules, [i, 1].concat(ruleset.rules[i].eval(env)));
2048
+ }
2049
+ }
2050
+
2051
+ // Evaluate everything else
2052
+ for (var i = 0, rule; i < ruleset.rules.length; i++) {
2053
+ rule = ruleset.rules[i];
2054
+
2055
+ if (! (rule instanceof tree.mixin.Definition)) {
2056
+ ruleset.rules[i] = rule.eval ? rule.eval(env) : rule;
2057
+ }
2058
+ }
2059
+
2060
+ // Pop the stack
2061
+ env.frames.shift();
2062
+
2063
+ return ruleset;
2064
+ },
2065
+ match: function (args) {
2066
+ return !args || args.length === 0;
2067
+ },
2068
+ variables: function () {
2069
+ if (this._variables) { return this._variables }
2070
+ else {
2071
+ return this._variables = this.rules.reduce(function (hash, r) {
2072
+ if (r instanceof tree.Rule && r.variable === true) {
2073
+ hash[r.name] = r;
2074
+ }
2075
+ return hash;
2076
+ }, {});
2077
+ }
2078
+ },
2079
+ variable: function (name) {
2080
+ return this.variables()[name];
2081
+ },
2082
+ rulesets: function () {
2083
+ if (this._rulesets) { return this._rulesets }
2084
+ else {
2085
+ return this._rulesets = this.rules.filter(function (r) {
2086
+ return (r instanceof tree.Ruleset) || (r instanceof tree.mixin.Definition);
2087
+ });
2088
+ }
2089
+ },
2090
+ find: function (selector, self) {
2091
+ self = self || this;
2092
+ var rules = [], rule, match,
2093
+ key = selector.toCSS();
2094
+
2095
+ if (key in this._lookups) { return this._lookups[key] }
2096
+
2097
+ this.rulesets().forEach(function (rule) {
2098
+ if (rule !== self) {
2099
+ for (var j = 0; j < rule.selectors.length; j++) {
2100
+ if (match = selector.match(rule.selectors[j])) {
2101
+ if (selector.elements.length > 1) {
2102
+ Array.prototype.push.apply(rules, rule.find(
2103
+ new(tree.Selector)(selector.elements.slice(1)), self));
2104
+ } else {
2105
+ rules.push(rule);
2106
+ }
2107
+ break;
2108
+ }
2109
+ }
2110
+ }
2111
+ });
2112
+ return this._lookups[key] = rules;
2113
+ },
2114
+ //
2115
+ // Entry point for code generation
2116
+ //
2117
+ // `context` holds an array of arrays.
2118
+ //
2119
+ toCSS: function (context, env) {
2120
+ var css = [], // The CSS output
2121
+ rules = [], // node.Rule instances
2122
+ rulesets = [], // node.Ruleset instances
2123
+ paths = [], // Current selectors
2124
+ selector, // The fully rendered selector
2125
+ rule;
2126
+
2127
+ if (! this.root) {
2128
+ if (context.length === 0) {
2129
+ paths = this.selectors.map(function (s) { return [s] });
2130
+ } else {
2131
+ for (var s = 0; s < this.selectors.length; s++) {
2132
+ for (var c = 0; c < context.length; c++) {
2133
+ paths.push(context[c].concat([this.selectors[s]]));
2134
+ }
2135
+ }
2136
+ }
2137
+ }
2138
+
2139
+ // Compile rules and rulesets
2140
+ for (var i = 0; i < this.rules.length; i++) {
2141
+ rule = this.rules[i];
2142
+
2143
+ if (rule.rules || (rule instanceof tree.Directive)) {
2144
+ rulesets.push(rule.toCSS(paths, env));
2145
+ } else if (rule instanceof tree.Comment) {
2146
+ if (!rule.silent) {
2147
+ if (this.root) {
2148
+ rulesets.push(rule.toCSS(env));
2149
+ } else {
2150
+ rules.push(rule.toCSS(env));
2151
+ }
2152
+ }
2153
+ } else {
2154
+ if (rule.toCSS && !rule.variable) {
2155
+ rules.push(rule.toCSS(env));
2156
+ } else if (rule.value && !rule.variable) {
2157
+ rules.push(rule.value.toString());
2158
+ }
2159
+ }
2160
+ }
2161
+
2162
+ rulesets = rulesets.join('');
2163
+
2164
+ // If this is the root node, we don't render
2165
+ // a selector, or {}.
2166
+ // Otherwise, only output if this ruleset has rules.
2167
+ if (this.root) {
2168
+ css.push(rules.join(env.compress ? '' : '\n'));
2169
+ } else {
2170
+ if (rules.length > 0) {
2171
+ selector = paths.map(function (p) {
2172
+ return p.map(function (s) {
2173
+ return s.toCSS(env);
2174
+ }).join('').trim();
2175
+ }).join(env.compress ? ',' : (paths.length > 3 ? ',\n' : ', '));
2176
+ css.push(selector,
2177
+ (env.compress ? '{' : ' {\n ') +
2178
+ rules.join(env.compress ? '' : '\n ') +
2179
+ (env.compress ? '}' : '\n}\n'));
2180
+ }
2181
+ }
2182
+ css.push(rulesets);
2183
+
2184
+ return css.join('') + (env.compress ? '\n' : '');
2185
+ }
2186
+ };
2187
+ })(require('less/tree'));
2188
+ (function (tree) {
2189
+
2190
+ tree.Selector = function (elements) {
2191
+ this.elements = elements;
2192
+ if (this.elements[0].combinator.value === "") {
2193
+ this.elements[0].combinator.value = ' ';
2194
+ }
2195
+ };
2196
+ tree.Selector.prototype.match = function (other) {
2197
+ if (this.elements[0].value === other.elements[0].value) {
2198
+ return true;
2199
+ } else {
2200
+ return false;
2201
+ }
2202
+ };
2203
+ tree.Selector.prototype.toCSS = function (env) {
2204
+ if (this._css) { return this._css }
2205
+
2206
+ return this._css = this.elements.map(function (e) {
2207
+ if (typeof(e) === 'string') {
2208
+ return ' ' + e.trim();
2209
+ } else {
2210
+ return e.toCSS(env);
2211
+ }
2212
+ }).join('');
2213
+ };
2214
+
2215
+ })(require('less/tree'));
2216
+ (function (tree) {
2217
+
2218
+ tree.URL = function (val, paths) {
2219
+ if (val.data) {
2220
+ this.attrs = val;
2221
+ } else {
2222
+ // Add the base path if the URL is relative and we are in the browser
2223
+ if (!/^(?:https?:\/|file:\/)?\//.test(val.value) && paths.length > 0 && typeof(window) !== 'undefined') {
2224
+ val.value = paths[0] + (val.value.charAt(0) === '/' ? val.value.slice(1) : val.value);
2225
+ }
2226
+ this.value = val;
2227
+ this.paths = paths;
2228
+ }
2229
+ };
2230
+ tree.URL.prototype = {
2231
+ toCSS: function () {
2232
+ return "url(" + (this.attrs ? 'data:' + this.attrs.mime + this.attrs.charset + this.attrs.base64 + this.attrs.data
2233
+ : this.value.toCSS()) + ")";
2234
+ },
2235
+ eval: function (ctx) {
2236
+ return this.attrs ? this : new(tree.URL)(this.value.eval(ctx), this.paths);
2237
+ }
2238
+ };
2239
+
2240
+ })(require('less/tree'));
2241
+ (function (tree) {
2242
+
2243
+ tree.Value = function (value) {
2244
+ this.value = value;
2245
+ this.is = 'value';
2246
+ };
2247
+ tree.Value.prototype = {
2248
+ eval: function (env) {
2249
+ if (this.value.length === 1) {
2250
+ return this.value[0].eval(env);
2251
+ } else {
2252
+ return new(tree.Value)(this.value.map(function (v) {
2253
+ return v.eval(env);
2254
+ }));
2255
+ }
2256
+ },
2257
+ toCSS: function (env) {
2258
+ return this.value.map(function (e) {
2259
+ return e.toCSS(env);
2260
+ }).join(env.compress ? ',' : ', ');
2261
+ }
2262
+ };
2263
+
2264
+ })(require('less/tree'));
2265
+ (function (tree) {
2266
+
2267
+ tree.Variable = function (name, index) { this.name = name, this.index = index };
2268
+ tree.Variable.prototype = {
2269
+ eval: function (env) {
2270
+ var variable, v, name = this.name;
2271
+
2272
+ if (variable = tree.find(env.frames, function (frame) {
2273
+ if (v = frame.variable(name)) {
2274
+ return v.value.eval(env);
2275
+ }
2276
+ })) { return variable }
2277
+ else {
2278
+ throw { message: "variable " + this.name + " is undefined",
2279
+ index: this.index };
2280
+ }
2281
+ }
2282
+ };
2283
+
2284
+ })(require('less/tree'));
2285
+ require('less/tree').find = function (obj, fun) {
2286
+ for (var i = 0, r; i < obj.length; i++) {
2287
+ if (r = fun.call(obj, obj[i])) { return r }
2288
+ }
2289
+ return null;
2290
+ };
2291
+ //
2292
+ // browser.js - client-side engine
2293
+ //
2294
+
2295
+ var isFileProtocol = (location.protocol === 'file:' ||
2296
+ location.protocol === 'chrome:' ||
2297
+ location.protocol === 'resource:');
2298
+
2299
+ less.env = less.env || (location.hostname == '127.0.0.1' ||
2300
+ location.hostname == '0.0.0.0' ||
2301
+ location.hostname == 'localhost' ||
2302
+ location.port.length > 0 ||
2303
+ isFileProtocol ? 'development'
2304
+ : 'production');
2305
+
2306
+ // Load styles asynchronously (default: false)
2307
+ //
2308
+ // This is set to `false` by default, so that the body
2309
+ // doesn't start loading before the stylesheets are parsed.
2310
+ // Setting this to `true` can result in flickering.
2311
+ //
2312
+ less.async = false;
2313
+
2314
+ // Interval between watch polls
2315
+ less.poll = less.poll || (isFileProtocol ? 1000 : 1500);
2316
+
2317
+ //
2318
+ // Watch mode
2319
+ //
2320
+ less.watch = function () { return this.watchMode = true };
2321
+ less.unwatch = function () { return this.watchMode = false };
2322
+
2323
+ if (less.env === 'development') {
2324
+ less.optimization = 0;
2325
+
2326
+ if (/!watch/.test(location.hash)) {
2327
+ less.watch();
2328
+ }
2329
+ less.watchTimer = setInterval(function () {
2330
+ if (less.watchMode) {
2331
+ loadStyleSheets(function (root, sheet, env) {
2332
+ if (root) {
2333
+ createCSS(root.toCSS(), sheet, env.lastModified);
2334
+ }
2335
+ });
2336
+ }
2337
+ }, less.poll);
2338
+ } else {
2339
+ less.optimization = 3;
2340
+ }
2341
+
2342
+ var cache;
2343
+
2344
+ try {
2345
+ cache = (typeof(window.localStorage) === 'undefined') ? null : window.localStorage;
2346
+ } catch (_) {
2347
+ cache = null;
2348
+ }
2349
+
2350
+ //
2351
+ // Get all <link> tags with the 'rel' attribute set to "stylesheet/less"
2352
+ //
2353
+ var links = document.getElementsByTagName('link');
2354
+ var typePattern = /^text\/(x-)?less$/;
2355
+
2356
+ less.sheets = [];
2357
+
2358
+ for (var i = 0; i < links.length; i++) {
2359
+ if (links[i].rel === 'stylesheet/less' || (links[i].rel.match(/stylesheet/) &&
2360
+ (links[i].type.match(typePattern)))) {
2361
+ less.sheets.push(links[i]);
2362
+ }
2363
+ }
2364
+
2365
+
2366
+ less.refresh = function (reload) {
2367
+ var startTime, endTime;
2368
+ startTime = endTime = new(Date);
2369
+
2370
+ loadStyleSheets(function (root, sheet, env) {
2371
+ if (env.local) {
2372
+ log("loading " + sheet.href + " from cache.");
2373
+ } else {
2374
+ log("parsed " + sheet.href + " successfully.");
2375
+ createCSS(root.toCSS(), sheet, env.lastModified);
2376
+ }
2377
+ log("css for " + sheet.href + " generated in " + (new(Date) - endTime) + 'ms');
2378
+ (env.remaining === 0) && log("css generated in " + (new(Date) - startTime) + 'ms');
2379
+ endTime = new(Date);
2380
+ }, reload);
2381
+
2382
+ loadStyles();
2383
+ };
2384
+ less.refreshStyles = loadStyles;
2385
+
2386
+ less.refresh(less.env === 'development');
2387
+
2388
+ function loadStyles() {
2389
+ var styles = document.getElementsByTagName('style');
2390
+ for (var i = 0; i < styles.length; i++) {
2391
+ if (styles[i].type.match(typePattern)) {
2392
+ new(less.Parser)().parse(styles[i].innerHTML || '', function (e, tree) {
2393
+ styles[i].type = 'text/css';
2394
+ styles[i].innerHTML = tree.toCSS();
2395
+ });
2396
+ }
2397
+ }
2398
+ }
2399
+
2400
+ function loadStyleSheets(callback, reload) {
2401
+ for (var i = 0; i < less.sheets.length; i++) {
2402
+ loadStyleSheet(less.sheets[i], callback, reload, less.sheets.length - (i + 1));
2403
+ }
2404
+ }
2405
+
2406
+ function loadStyleSheet(sheet, callback, reload, remaining) {
2407
+ var url = window.location.href.replace(/[#?].*$/, '');
2408
+ var href = sheet.href.replace(/\?.*$/, '');
2409
+ var css = cache && cache.getItem(href);
2410
+ var timestamp = cache && cache.getItem(href + ':timestamp');
2411
+ var styles = { css: css, timestamp: timestamp };
2412
+
2413
+ // Stylesheets in IE don't always return the full path
2414
+ if (! /^(https?|file):/.test(href)) {
2415
+ href = url.slice(0, url.lastIndexOf('/') + 1) + href;
2416
+ }
2417
+
2418
+ xhr(sheet.href, sheet.type, function (data, lastModified) {
2419
+ if (!reload && styles && lastModified &&
2420
+ (new(Date)(lastModified).valueOf() ===
2421
+ new(Date)(styles.timestamp).valueOf())) {
2422
+ // Use local copy
2423
+ createCSS(styles.css, sheet);
2424
+ callback(null, sheet, { local: true, remaining: remaining });
2425
+ } else {
2426
+ // Use remote copy (re-parse)
2427
+ try {
2428
+ new(less.Parser)({
2429
+ optimization: less.optimization,
2430
+ paths: [href.replace(/[\w\.-]+$/, '')],
2431
+ mime: sheet.type
2432
+ }).parse(data, function (e, root) {
2433
+ if (e) { return error(e, href) }
2434
+ try {
2435
+ callback(root, sheet, { local: false, lastModified: lastModified, remaining: remaining });
2436
+ removeNode(document.getElementById('less-error-message:' + extractId(href)));
2437
+ } catch (e) {
2438
+ error(e, href);
2439
+ }
2440
+ });
2441
+ } catch (e) {
2442
+ error(e, href);
2443
+ }
2444
+ }
2445
+ }, function (status, url) {
2446
+ throw new(Error)("Couldn't load " + url + " (" + status + ")");
2447
+ });
2448
+ }
2449
+
2450
+ function extractId(href) {
2451
+ return href.replace(/^[a-z]+:\/\/?[^\/]+/, '' ) // Remove protocol & domain
2452
+ .replace(/^\//, '' ) // Remove root /
2453
+ .replace(/\?.*$/, '' ) // Remove query
2454
+ .replace(/\.[^\.\/]+$/, '' ) // Remove file extension
2455
+ .replace(/[^\.\w-]+/g, '-') // Replace illegal characters
2456
+ .replace(/\./g, ':'); // Replace dots with colons(for valid id)
2457
+ }
2458
+
2459
+ function createCSS(styles, sheet, lastModified) {
2460
+ var css;
2461
+
2462
+ // Strip the query-string
2463
+ var href = sheet.href ? sheet.href.replace(/\?.*$/, '') : '';
2464
+
2465
+ // If there is no title set, use the filename, minus the extension
2466
+ var id = 'less:' + (sheet.title || extractId(href));
2467
+
2468
+ // If the stylesheet doesn't exist, create a new node
2469
+ if ((css = document.getElementById(id)) === null) {
2470
+ css = document.createElement('style');
2471
+ css.type = 'text/css';
2472
+ css.media = sheet.media || 'screen';
2473
+ css.id = id;
2474
+ document.getElementsByTagName('head')[0].appendChild(css);
2475
+ }
2476
+
2477
+ if (css.styleSheet) { // IE
2478
+ try {
2479
+ css.styleSheet.cssText = styles;
2480
+ } catch (e) {
2481
+ throw new(Error)("Couldn't reassign styleSheet.cssText.");
2482
+ }
2483
+ } else {
2484
+ (function (node) {
2485
+ if (css.childNodes.length > 0) {
2486
+ if (css.firstChild.nodeValue !== node.nodeValue) {
2487
+ css.replaceChild(node, css.firstChild);
2488
+ }
2489
+ } else {
2490
+ css.appendChild(node);
2491
+ }
2492
+ })(document.createTextNode(styles));
2493
+ }
2494
+
2495
+ // Don't update the local store if the file wasn't modified
2496
+ if (lastModified && cache) {
2497
+ log('saving ' + href + ' to cache.');
2498
+ cache.setItem(href, styles);
2499
+ cache.setItem(href + ':timestamp', lastModified);
2500
+ }
2501
+ }
2502
+
2503
+ function xhr(url, type, callback, errback) {
2504
+ var xhr = getXMLHttpRequest();
2505
+ var async = isFileProtocol ? false : less.async;
2506
+
2507
+ if (typeof(xhr.overrideMimeType) === 'function') {
2508
+ xhr.overrideMimeType('text/css');
2509
+ }
2510
+ xhr.open('GET', url, async);
2511
+ xhr.setRequestHeader('Accept', type || 'text/x-less, text/css; q=0.9, */*; q=0.5');
2512
+ xhr.send(null);
2513
+
2514
+ if (isFileProtocol) {
2515
+ if (xhr.status === 0) {
2516
+ callback(xhr.responseText);
2517
+ } else {
2518
+ errback(xhr.status, url);
2519
+ }
2520
+ } else if (async) {
2521
+ xhr.onreadystatechange = function () {
2522
+ if (xhr.readyState == 4) {
2523
+ handleResponse(xhr, callback, errback);
2524
+ }
2525
+ };
2526
+ } else {
2527
+ handleResponse(xhr, callback, errback);
2528
+ }
2529
+
2530
+ function handleResponse(xhr, callback, errback) {
2531
+ if (xhr.status >= 200 && xhr.status < 300) {
2532
+ callback(xhr.responseText,
2533
+ xhr.getResponseHeader("Last-Modified"));
2534
+ } else if (typeof(errback) === 'function') {
2535
+ errback(xhr.status, url);
2536
+ }
2537
+ }
2538
+ }
2539
+
2540
+ function getXMLHttpRequest() {
2541
+ if (window.XMLHttpRequest) {
2542
+ return new(XMLHttpRequest);
2543
+ } else {
2544
+ try {
2545
+ return new(ActiveXObject)("MSXML2.XMLHTTP.3.0");
2546
+ } catch (e) {
2547
+ log("browser doesn't support AJAX.");
2548
+ return null;
2549
+ }
2550
+ }
2551
+ }
2552
+
2553
+ function removeNode(node) {
2554
+ return node && node.parentNode.removeChild(node);
2555
+ }
2556
+
2557
+ function log(str) {
2558
+ if (less.env == 'development' && typeof(console) !== "undefined") { console.log('less: ' + str) }
2559
+ }
2560
+
2561
+ function error(e, href) {
2562
+ var id = 'less-error-message:' + extractId(href);
2563
+
2564
+ var template = ['<ul>',
2565
+ '<li><label>[-1]</label><pre class="ctx">{0}</pre></li>',
2566
+ '<li><label>[0]</label><pre>{current}</pre></li>',
2567
+ '<li><label>[1]</label><pre class="ctx">{2}</pre></li>',
2568
+ '</ul>'].join('\n');
2569
+
2570
+ var elem = document.createElement('div'), timer, content;
2571
+
2572
+ elem.id = id;
2573
+ elem.className = "less-error-message";
2574
+
2575
+ content = '<h3>' + (e.message || 'There is an error in your .less file') +
2576
+ '</h3>' + '<p><a href="' + href + '">' + href + "</a> ";
2577
+
2578
+ if (e.extract) {
2579
+ content += 'on line ' + e.line + ', column ' + (e.column + 1) + ':</p>' +
2580
+ template.replace(/\[(-?\d)\]/g, function (_, i) {
2581
+ return (parseInt(e.line) + parseInt(i)) || '';
2582
+ }).replace(/\{(\d)\}/g, function (_, i) {
2583
+ return e.extract[parseInt(i)] || '';
2584
+ }).replace(/\{current\}/, e.extract[1].slice(0, e.column) + '<span class="error">' +
2585
+ e.extract[1].slice(e.column) + '</span>');
2586
+ }
2587
+ elem.innerHTML = content;
2588
+
2589
+ // CSS for error messages
2590
+ createCSS([
2591
+ '.less-error-message ul, .less-error-message li {',
2592
+ 'list-style-type: none;',
2593
+ 'margin-right: 15px;',
2594
+ 'padding: 4px 0;',
2595
+ 'margin: 0;',
2596
+ '}',
2597
+ '.less-error-message label {',
2598
+ 'font-size: 12px;',
2599
+ 'margin-right: 15px;',
2600
+ 'padding: 4px 0;',
2601
+ 'color: #cc7777;',
2602
+ '}',
2603
+ '.less-error-message pre {',
2604
+ 'color: #ee4444;',
2605
+ 'padding: 4px 0;',
2606
+ 'margin: 0;',
2607
+ 'display: inline-block;',
2608
+ '}',
2609
+ '.less-error-message pre.ctx {',
2610
+ 'color: #dd4444;',
2611
+ '}',
2612
+ '.less-error-message h3 {',
2613
+ 'font-size: 20px;',
2614
+ 'font-weight: bold;',
2615
+ 'padding: 15px 0 5px 0;',
2616
+ 'margin: 0;',
2617
+ '}',
2618
+ '.less-error-message a {',
2619
+ 'color: #10a',
2620
+ '}',
2621
+ '.less-error-message .error {',
2622
+ 'color: red;',
2623
+ 'font-weight: bold;',
2624
+ 'padding-bottom: 2px;',
2625
+ 'border-bottom: 1px dashed red;',
2626
+ '}'
2627
+ ].join('\n'), { title: 'error-message' });
2628
+
2629
+ elem.style.cssText = [
2630
+ "font-family: Arial, sans-serif",
2631
+ "border: 1px solid #e00",
2632
+ "background-color: #eee",
2633
+ "border-radius: 5px",
2634
+ "-webkit-border-radius: 5px",
2635
+ "-moz-border-radius: 5px",
2636
+ "color: #e00",
2637
+ "padding: 15px",
2638
+ "margin-bottom: 15px"
2639
+ ].join(';');
2640
+
2641
+ if (less.env == 'development') {
2642
+ timer = setInterval(function () {
2643
+ if (document.body) {
2644
+ if (document.getElementById(id)) {
2645
+ document.body.replaceChild(elem, document.getElementById(id));
2646
+ } else {
2647
+ document.body.insertBefore(elem, document.body.firstChild);
2648
+ }
2649
+ clearInterval(timer);
2650
+ }
2651
+ }, 10);
2652
+ }
2653
+ }
2654
+
2655
+ })(window);