less 1.7.0 → 1.7.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.grunt/grunt-contrib-jasmine/es5-shim.js +1360 -0
- package/.idea/jsLibraryMappings.xml +0 -1
- package/.idea/workspace.xml +182 -183
- package/.travis.yml +0 -1
- package/CHANGELOG.md +60 -2
- package/CONTRIBUTING.md +1 -1
- package/Gruntfile.js +12 -24
- package/LICENSE +0 -2
- package/README.md +5 -3
- package/benchmark/less-benchmark.js +1 -2
- package/bin/lessc +13 -11
- package/bower.json +2 -2
- package/build/rhino-modules.js +1 -1
- package/dist/less-1.7.0.js +59 -59
- package/dist/less-1.7.0.min.js +12 -12
- package/dist/less-1.7.1.js +7936 -0
- package/dist/less-1.7.1.min.js +16 -0
- package/dist/less-1.7.2.js +7938 -0
- package/dist/less-1.7.2.min.js +16 -0
- package/dist/less-1.7.3.js +7942 -0
- package/dist/less-1.7.3.min.js +16 -0
- package/dist/less-1.7.4.js +7979 -0
- package/dist/less-1.7.4.min.js +16 -0
- package/dist/less-1.7.5.js +8008 -0
- package/dist/less-1.7.5.min.js +16 -0
- package/dist/less-rhino-1.7.0.js +48 -48
- package/dist/less-rhino-1.7.1.js +9311 -0
- package/dist/less-rhino-1.7.2.js +9313 -0
- package/dist/less-rhino-1.7.3.js +9317 -0
- package/dist/less-rhino-1.7.4.js +9354 -0
- package/dist/less-rhino-1.7.5.js +9383 -0
- package/dist/lessc-rhino-1.7.0.js +2 -2
- package/dist/lessc-rhino-1.7.1.js +449 -0
- package/dist/lessc-rhino-1.7.2.js +449 -0
- package/dist/lessc-rhino-1.7.3.js +449 -0
- package/dist/lessc-rhino-1.7.4.js +449 -0
- package/dist/lessc-rhino-1.7.5.js +449 -0
- package/lib/less/browser.js +14 -9
- package/lib/less/env.js +2 -0
- package/lib/less/fs.js +10 -0
- package/lib/less/functions.js +41 -38
- package/lib/less/import-visitor.js +9 -7
- package/lib/less/index.js +30 -18
- package/lib/less/lessc_helper.js +1 -0
- package/lib/less/parser/parser.js.orig +1747 -0
- package/lib/less/parser.js +81 -40
- package/lib/less/rhino.js +3 -3
- package/lib/less/source-map-output.js +1 -1
- package/lib/less/to-css-visitor.js +3 -0
- package/lib/less/tree/anonymous.js +7 -3
- package/lib/less/tree/directive.js +6 -0
- package/lib/less/tree/element.js +6 -22
- package/lib/less/tree/import.js +1 -1
- package/lib/less/tree/mixin.js +4 -3
- package/lib/less/tree/quoted.js +12 -4
- package/lib/less/tree/rule.js +7 -4
- package/lib/less/tree/rule.js.orig +164 -0
- package/lib/less/tree/ruleset.js +27 -2
- package/lib/less/tree/selector.js +1 -1
- package/lib/less/tree.js +2 -2
- package/package.json +13 -12
- package/test/browser/less.js +7317 -6931
- package/test/css/comments.css +8 -0
- package/test/css/css-3.css +9 -0
- package/test/css/functions.css +8 -8
- package/test/css/import-inline.css +4 -1
- package/test/css/import.css +6 -0
- package/test/css/javascript.css +5 -0
- package/test/css/mixins-guards.css +14 -3
- package/test/css/mixins.css +3 -0
- package/test/css/property-name-interp.css +1 -0
- package/test/css/urls.css +6 -0
- package/test/index.js +1 -1
- package/test/less/comments.less +14 -1
- package/test/less/css-3.less +12 -1
- package/test/less/errors/color-invalid-hex-code.less +3 -0
- package/test/less/errors/color-invalid-hex-code.txt +4 -0
- package/test/less/errors/color-invalid-hex-code2.less +3 -0
- package/test/less/errors/color-invalid-hex-code2.txt +4 -0
- package/test/less/errors/import-malformed.less +1 -0
- package/test/less/errors/import-malformed.txt +3 -0
- package/test/less/errors/import-no-semi.txt +1 -1
- package/test/less/errors/javascript-error.less +1 -1
- package/test/less/errors/javascript-error.txt +2 -2
- package/test/less/import-inline.less +1 -0
- package/test/less/import.less +8 -1
- package/test/less/javascript.less +9 -0
- package/test/less/mixins-guards.less +17 -3
- package/test/less/mixins.less +4 -0
- package/test/less/property-name-interp.less +4 -1
- package/test/less/property-name-interp.less.orig +59 -0
- package/test/less/urls.less +12 -0
- package/test/less-test.js +7 -8
- package/test/modify-vars.js +17 -0
- package/test/sourcemaps/basic.json +1 -1
- package/tmp/browser/test-runner-main.html +4 -0
- package/tmp/less.js +8295 -0
- package/build/README.md +0 -51
- package/tmp/browser/test-postProcessor.html +0 -59
- package/tmp/browser/test-runner-browser.html +0 -59
- package/tmp/browser/test-runner-errors.html +0 -267
- package/tmp/browser/test-runner-global-vars.html +0 -59
- package/tmp/browser/test-runner-legacy.html +0 -59
- package/tmp/browser/test-runner-modify-vars.html +0 -59
- package/tmp/browser/test-runner-no-js-errors.html +0 -59
- package/tmp/browser/test-runner-production.html +0 -55
- package/tmp/browser/test-runner-relative-urls.html +0 -59
- package/tmp/browser/test-runner-rootpath-relative.html +0 -59
- package/tmp/browser/test-runner-rootpath.html +0 -59
|
@@ -0,0 +1,1747 @@
|
|
|
1
|
+
var LessError = require('../less-error.js'),
|
|
2
|
+
tree = require("../tree/index.js"),
|
|
3
|
+
visitor = require("../visitor/index.js"),
|
|
4
|
+
contexts = require("../contexts.js"),
|
|
5
|
+
getImportManager = require("./imports.js"),
|
|
6
|
+
getParserInput = require("./parser-input.js");
|
|
7
|
+
|
|
8
|
+
module.exports = function(environment) {
|
|
9
|
+
var SourceMapOutput = require("../source-map-output")(environment);
|
|
10
|
+
//
|
|
11
|
+
// less.js - parser
|
|
12
|
+
//
|
|
13
|
+
// A relatively straight-forward predictive parser.
|
|
14
|
+
// There is no tokenization/lexing stage, the input is parsed
|
|
15
|
+
// in one sweep.
|
|
16
|
+
//
|
|
17
|
+
// To make the parser fast enough to run in the browser, several
|
|
18
|
+
// optimization had to be made:
|
|
19
|
+
//
|
|
20
|
+
// - Matching and slicing on a huge input is often cause of slowdowns.
|
|
21
|
+
// The solution is to chunkify the input into smaller strings.
|
|
22
|
+
// The chunks are stored in the `chunks` var,
|
|
23
|
+
// `j` holds the current chunk index, and `currentPos` holds
|
|
24
|
+
// the index of the current chunk in relation to `input`.
|
|
25
|
+
// This gives us an almost 4x speed-up.
|
|
26
|
+
//
|
|
27
|
+
// - In many cases, we don't need to match individual tokens;
|
|
28
|
+
// for example, if a value doesn't hold any variables, operations
|
|
29
|
+
// or dynamic references, the parser can effectively 'skip' it,
|
|
30
|
+
// treating it as a literal.
|
|
31
|
+
// An example would be '1px solid #000' - which evaluates to itself,
|
|
32
|
+
// we don't need to know what the individual components are.
|
|
33
|
+
// The drawback, of course is that you don't get the benefits of
|
|
34
|
+
// syntax-checking on the CSS. This gives us a 50% speed-up in the parser,
|
|
35
|
+
// and a smaller speed-up in the code-gen.
|
|
36
|
+
//
|
|
37
|
+
//
|
|
38
|
+
// Token matching is done with the `$` function, which either takes
|
|
39
|
+
// a terminal string or regexp, or a non-terminal function to call.
|
|
40
|
+
// It also takes care of moving all the indices forwards.
|
|
41
|
+
//
|
|
42
|
+
//
|
|
43
|
+
var Parser = function Parser(env) {
|
|
44
|
+
var parser,
|
|
45
|
+
parsers,
|
|
46
|
+
parserInput = getParserInput();
|
|
47
|
+
|
|
48
|
+
// Top parser on an import tree must be sure there is one "env"
|
|
49
|
+
// which will then be passed around by reference.
|
|
50
|
+
if (!(env instanceof contexts.parseEnv)) {
|
|
51
|
+
env = new contexts.parseEnv(env);
|
|
52
|
+
}
|
|
53
|
+
this.env = env;
|
|
54
|
+
|
|
55
|
+
var imports = this.imports = getImportManager(environment, env, Parser);
|
|
56
|
+
|
|
57
|
+
function expect(arg, msg, index) {
|
|
58
|
+
// some older browsers return typeof 'function' for RegExp
|
|
59
|
+
var result = (Object.prototype.toString.call(arg) === '[object Function]') ? arg.call(parsers) : parserInput.$(arg);
|
|
60
|
+
if (result) {
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
error(msg || (typeof(arg) === 'string' ? "expected '" + arg + "' got '" + parserInput.currentChar() + "'"
|
|
64
|
+
: "unexpected token"));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Specialization of expect()
|
|
68
|
+
function expectChar(arg, msg) {
|
|
69
|
+
if (parserInput.$char(arg)) {
|
|
70
|
+
return arg;
|
|
71
|
+
}
|
|
72
|
+
error(msg || "expected '" + arg + "' got '" + parserInput.currentChar() + "'");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function error(msg, type) {
|
|
76
|
+
var e = new Error(msg);
|
|
77
|
+
e.index = parserInput.i;
|
|
78
|
+
e.type = type || 'Syntax';
|
|
79
|
+
throw e;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function getInput(e, env) {
|
|
83
|
+
if (e.filename && env.currentFileInfo.filename && (e.filename !== env.currentFileInfo.filename)) {
|
|
84
|
+
return parser.imports.contents[e.filename];
|
|
85
|
+
} else {
|
|
86
|
+
return parserInput.getInput();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function getDebugInfo(index) {
|
|
91
|
+
var filename = env.currentFileInfo.filename;
|
|
92
|
+
filename = environment.getAbsolutePath(env, filename);
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
lineNumber: parserInput.getLocation(index).line + 1,
|
|
96
|
+
fileName: filename
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
//
|
|
101
|
+
// The Parser
|
|
102
|
+
//
|
|
103
|
+
parser = {
|
|
104
|
+
|
|
105
|
+
imports: imports,
|
|
106
|
+
//
|
|
107
|
+
// Parse an input string into an abstract syntax tree,
|
|
108
|
+
// @param str A string containing 'less' markup
|
|
109
|
+
// @param callback call `callback` when done.
|
|
110
|
+
// @param [additionalData] An optional map which can contains vars - a map (key, value) of variables to apply
|
|
111
|
+
//
|
|
112
|
+
parse: function (str, callback, additionalData) {
|
|
113
|
+
var root, error = null, globalVars, modifyVars, preText = "";
|
|
114
|
+
|
|
115
|
+
globalVars = (additionalData && additionalData.globalVars) ? Parser.serializeVars(additionalData.globalVars) + '\n' : '';
|
|
116
|
+
modifyVars = (additionalData && additionalData.modifyVars) ? '\n' + Parser.serializeVars(additionalData.modifyVars) : '';
|
|
117
|
+
|
|
118
|
+
if (globalVars || (additionalData && additionalData.banner)) {
|
|
119
|
+
preText = ((additionalData && additionalData.banner) ? additionalData.banner : "") + globalVars;
|
|
120
|
+
parser.imports.contentsIgnoredChars[env.currentFileInfo.filename] = preText.length;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
str = str.replace(/\r\n/g, '\n');
|
|
124
|
+
// Remove potential UTF Byte Order Mark
|
|
125
|
+
str = preText + str.replace(/^\uFEFF/, '') + modifyVars;
|
|
126
|
+
parser.imports.contents[env.currentFileInfo.filename] = str;
|
|
127
|
+
|
|
128
|
+
// Start with the primary rule.
|
|
129
|
+
// The whole syntax tree is held under a Ruleset node,
|
|
130
|
+
// with the `root` property set to true, so no `{}` are
|
|
131
|
+
// output. The callback is called when the input is parsed.
|
|
132
|
+
try {
|
|
133
|
+
parserInput.start(str, env.chunkInput, parser, env);
|
|
134
|
+
|
|
135
|
+
root = new(tree.Ruleset)(null, this.parsers.primary());
|
|
136
|
+
root.root = true;
|
|
137
|
+
root.firstRoot = true;
|
|
138
|
+
} catch (e) {
|
|
139
|
+
return callback(new LessError(parser, e, env));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
root.toCSS = (function (evaluate) {
|
|
143
|
+
return function (options, variables) {
|
|
144
|
+
options = options || {};
|
|
145
|
+
var evaldRoot,
|
|
146
|
+
css,
|
|
147
|
+
evalEnv = new contexts.evalEnv(options);
|
|
148
|
+
|
|
149
|
+
//
|
|
150
|
+
// Allows setting variables with a hash, so:
|
|
151
|
+
//
|
|
152
|
+
// `{ color: new(tree.Color)('#f01') }` will become:
|
|
153
|
+
//
|
|
154
|
+
// new(tree.Rule)('@color',
|
|
155
|
+
// new(tree.Value)([
|
|
156
|
+
// new(tree.Expression)([
|
|
157
|
+
// new(tree.Color)('#f01')
|
|
158
|
+
// ])
|
|
159
|
+
// ])
|
|
160
|
+
// )
|
|
161
|
+
//
|
|
162
|
+
if (typeof(variables) === 'object' && !Array.isArray(variables)) {
|
|
163
|
+
variables = Object.keys(variables).map(function (k) {
|
|
164
|
+
var value = variables[k];
|
|
165
|
+
|
|
166
|
+
if (! (value instanceof tree.Value)) {
|
|
167
|
+
if (! (value instanceof tree.Expression)) {
|
|
168
|
+
value = new(tree.Expression)([value]);
|
|
169
|
+
}
|
|
170
|
+
value = new(tree.Value)([value]);
|
|
171
|
+
}
|
|
172
|
+
return new(tree.Rule)('@' + k, value, false, null, 0);
|
|
173
|
+
});
|
|
174
|
+
evalEnv.frames = [new(tree.Ruleset)(null, variables)];
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
try {
|
|
178
|
+
var preEvalVisitors = [],
|
|
179
|
+
visitors = [
|
|
180
|
+
new(visitor.JoinSelectorVisitor)(),
|
|
181
|
+
new(visitor.ExtendVisitor)(),
|
|
182
|
+
new(visitor.ToCSSVisitor)({compress: Boolean(options.compress)})
|
|
183
|
+
], i, root = this;
|
|
184
|
+
|
|
185
|
+
if (options.plugins) {
|
|
186
|
+
for(i =0; i < options.plugins.length; i++) {
|
|
187
|
+
if (options.plugins[i].isPreEvalVisitor) {
|
|
188
|
+
preEvalVisitors.push(options.plugins[i]);
|
|
189
|
+
} else {
|
|
190
|
+
if (options.plugins[i].isPreVisitor) {
|
|
191
|
+
visitors.splice(0, 0, options.plugins[i]);
|
|
192
|
+
} else {
|
|
193
|
+
visitors.push(options.plugins[i]);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
for(i = 0; i < preEvalVisitors.length; i++) {
|
|
200
|
+
preEvalVisitors[i].run(root);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
evaldRoot = evaluate.call(root, evalEnv);
|
|
204
|
+
|
|
205
|
+
for(i = 0; i < visitors.length; i++) {
|
|
206
|
+
visitors[i].run(evaldRoot);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (options.sourceMap) {
|
|
210
|
+
evaldRoot = new SourceMapOutput(
|
|
211
|
+
{
|
|
212
|
+
contentsIgnoredCharsMap: parser.imports.contentsIgnoredChars,
|
|
213
|
+
writeSourceMap: options.writeSourceMap,
|
|
214
|
+
rootNode: evaldRoot,
|
|
215
|
+
contentsMap: parser.imports.contents,
|
|
216
|
+
sourceMapFilename: options.sourceMapFilename,
|
|
217
|
+
sourceMapURL: options.sourceMapURL,
|
|
218
|
+
outputFilename: options.sourceMapOutputFilename,
|
|
219
|
+
sourceMapBasepath: options.sourceMapBasepath,
|
|
220
|
+
sourceMapRootpath: options.sourceMapRootpath,
|
|
221
|
+
outputSourceFiles: options.outputSourceFiles,
|
|
222
|
+
sourceMapGenerator: options.sourceMapGenerator
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
css = evaldRoot.toCSS({
|
|
227
|
+
compress: Boolean(options.compress),
|
|
228
|
+
dumpLineNumbers: env.dumpLineNumbers,
|
|
229
|
+
strictUnits: Boolean(options.strictUnits),
|
|
230
|
+
numPrecision: 8});
|
|
231
|
+
} catch (e) {
|
|
232
|
+
throw new LessError(parser, e, env);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
var CleanCSS = environment.getCleanCSS();
|
|
236
|
+
if (options.cleancss && CleanCSS) {
|
|
237
|
+
var cleancssOptions = options.cleancssOptions || {};
|
|
238
|
+
|
|
239
|
+
if (cleancssOptions.keepSpecialComments === undefined) {
|
|
240
|
+
cleancssOptions.keepSpecialComments = "*";
|
|
241
|
+
}
|
|
242
|
+
cleancssOptions.processImport = false;
|
|
243
|
+
cleancssOptions.noRebase = true;
|
|
244
|
+
if (cleancssOptions.noAdvanced === undefined) {
|
|
245
|
+
cleancssOptions.noAdvanced = true;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return new CleanCSS(cleancssOptions).minify(css);
|
|
249
|
+
} else if (options.compress) {
|
|
250
|
+
return css.replace(/(^(\s)+)|((\s)+$)/g, "");
|
|
251
|
+
} else {
|
|
252
|
+
return css;
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
})(root.eval);
|
|
256
|
+
|
|
257
|
+
// If `i` is smaller than the `input.length - 1`,
|
|
258
|
+
// it means the parser wasn't able to parse the whole
|
|
259
|
+
// string, so we've got a parsing error.
|
|
260
|
+
//
|
|
261
|
+
// We try to extract a \n delimited string,
|
|
262
|
+
// showing the line where the parse error occurred.
|
|
263
|
+
// We split it up into two parts (the part which parsed,
|
|
264
|
+
// and the part which didn't), so we can color them differently.
|
|
265
|
+
var endInfo = parserInput.end();
|
|
266
|
+
if (!endInfo.isFinished) {
|
|
267
|
+
|
|
268
|
+
var message = endInfo.furthestPossibleErrorMessage;
|
|
269
|
+
|
|
270
|
+
if (!message) {
|
|
271
|
+
message = "Unrecognised input";
|
|
272
|
+
if (endInfo.furthestChar === '}') {
|
|
273
|
+
message += ". Possibly missing opening '{'";
|
|
274
|
+
} else if (endInfo.furthestChar === ')') {
|
|
275
|
+
message += ". Possibly missing opening '('";
|
|
276
|
+
} else if (endInfo.furthestReachedEnd) {
|
|
277
|
+
message += ". Possibly missing something";
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
error = new LessError(parser, {
|
|
282
|
+
type: "Parse",
|
|
283
|
+
message: message,
|
|
284
|
+
index: endInfo.furthest,
|
|
285
|
+
filename: env.currentFileInfo.filename
|
|
286
|
+
}, env);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
var finish = function (e) {
|
|
290
|
+
e = error || e || parser.imports.error;
|
|
291
|
+
|
|
292
|
+
if (e) {
|
|
293
|
+
if (!(e instanceof LessError)) {
|
|
294
|
+
e = new LessError(parser, e, env);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return callback(e);
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
return callback(null, root);
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
if (env.processImports !== false) {
|
|
305
|
+
new visitor.ImportVisitor(this.imports, finish)
|
|
306
|
+
.run(root);
|
|
307
|
+
} else {
|
|
308
|
+
return finish();
|
|
309
|
+
}
|
|
310
|
+
},
|
|
311
|
+
|
|
312
|
+
//
|
|
313
|
+
// Here in, the parsing rules/functions
|
|
314
|
+
//
|
|
315
|
+
// The basic structure of the syntax tree generated is as follows:
|
|
316
|
+
//
|
|
317
|
+
// Ruleset -> Rule -> Value -> Expression -> Entity
|
|
318
|
+
//
|
|
319
|
+
// Here's some Less code:
|
|
320
|
+
//
|
|
321
|
+
// .class {
|
|
322
|
+
// color: #fff;
|
|
323
|
+
// border: 1px solid #000;
|
|
324
|
+
// width: @w + 4px;
|
|
325
|
+
// > .child {...}
|
|
326
|
+
// }
|
|
327
|
+
//
|
|
328
|
+
// And here's what the parse tree might look like:
|
|
329
|
+
//
|
|
330
|
+
// Ruleset (Selector '.class', [
|
|
331
|
+
// Rule ("color", Value ([Expression [Color #fff]]))
|
|
332
|
+
// Rule ("border", Value ([Expression [Dimension 1px][Keyword "solid"][Color #000]]))
|
|
333
|
+
// Rule ("width", Value ([Expression [Operation "+" [Variable "@w"][Dimension 4px]]]))
|
|
334
|
+
// Ruleset (Selector [Element '>', '.child'], [...])
|
|
335
|
+
// ])
|
|
336
|
+
//
|
|
337
|
+
// In general, most rules will try to parse a token with the `$()` function, and if the return
|
|
338
|
+
// value is truly, will return a new node, of the relevant type. Sometimes, we need to check
|
|
339
|
+
// first, before parsing, that's when we use `peek()`.
|
|
340
|
+
//
|
|
341
|
+
parsers: parsers = {
|
|
342
|
+
//
|
|
343
|
+
// The `primary` rule is the *entry* and *exit* point of the parser.
|
|
344
|
+
// The rules here can appear at any level of the parse tree.
|
|
345
|
+
//
|
|
346
|
+
// The recursive nature of the grammar is an interplay between the `block`
|
|
347
|
+
// rule, which represents `{ ... }`, the `ruleset` rule, and this `primary` rule,
|
|
348
|
+
// as represented by this simplified grammar:
|
|
349
|
+
//
|
|
350
|
+
// primary → (ruleset | rule)+
|
|
351
|
+
// ruleset → selector+ block
|
|
352
|
+
// block → '{' primary '}'
|
|
353
|
+
//
|
|
354
|
+
// Only at one point is the primary rule not called from the
|
|
355
|
+
// block rule: at the root level.
|
|
356
|
+
//
|
|
357
|
+
primary: function () {
|
|
358
|
+
var mixin = this.mixin, root = [], node;
|
|
359
|
+
|
|
360
|
+
while (!parserInput.finished)
|
|
361
|
+
{
|
|
362
|
+
while(true) {
|
|
363
|
+
node = this.comment();
|
|
364
|
+
if (!node) { break; }
|
|
365
|
+
root.push(node);
|
|
366
|
+
}
|
|
367
|
+
if (parserInput.peek('}')) {
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
node = this.extendRule() || mixin.definition() || this.rule() || this.ruleset() ||
|
|
371
|
+
mixin.call() || this.rulesetCall() || this.directive();
|
|
372
|
+
if (node) {
|
|
373
|
+
root.push(node);
|
|
374
|
+
} else {
|
|
375
|
+
if (!(parserInput.$re(/^[\s\n]+/) || parserInput.$re(/^;+/))) {
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return root;
|
|
382
|
+
},
|
|
383
|
+
|
|
384
|
+
// comments are collected by the main parsing mechanism and then assigned to nodes
|
|
385
|
+
// where the current structure allows it
|
|
386
|
+
comment: function () {
|
|
387
|
+
if (parserInput.commentStore.length) {
|
|
388
|
+
var comment = parserInput.commentStore.shift();
|
|
389
|
+
return new(tree.Comment)(comment.text, comment.isLineComment, comment.index, env.currentFileInfo);
|
|
390
|
+
}
|
|
391
|
+
},
|
|
392
|
+
|
|
393
|
+
//
|
|
394
|
+
// Entities are tokens which can be found inside an Expression
|
|
395
|
+
//
|
|
396
|
+
entities: {
|
|
397
|
+
//
|
|
398
|
+
// A string, which supports escaping " and '
|
|
399
|
+
//
|
|
400
|
+
// "milky way" 'he\'s the one!'
|
|
401
|
+
//
|
|
402
|
+
quoted: function () {
|
|
403
|
+
var str, index = parserInput.i;
|
|
404
|
+
|
|
405
|
+
str = parserInput.$re(/^(~)?("((?:[^"\\\r\n]|\\.)*)"|'((?:[^'\\\r\n]|\\.)*)')/);
|
|
406
|
+
if (str) {
|
|
407
|
+
return new(tree.Quoted)(str[2], str[3] || str[4], Boolean(str[1]), index, env.currentFileInfo);
|
|
408
|
+
}
|
|
409
|
+
},
|
|
410
|
+
|
|
411
|
+
//
|
|
412
|
+
// A catch-all word, such as:
|
|
413
|
+
//
|
|
414
|
+
// black border-collapse
|
|
415
|
+
//
|
|
416
|
+
keyword: function () {
|
|
417
|
+
var k = parserInput.$re(/^%|^[_A-Za-z-][_A-Za-z0-9-]*/);
|
|
418
|
+
if (k) {
|
|
419
|
+
return tree.Color.fromKeyword(k) || new(tree.Keyword)(k);
|
|
420
|
+
}
|
|
421
|
+
},
|
|
422
|
+
|
|
423
|
+
//
|
|
424
|
+
// A function call
|
|
425
|
+
//
|
|
426
|
+
// rgb(255, 0, 255)
|
|
427
|
+
//
|
|
428
|
+
// We also try to catch IE's `alpha()`, but let the `alpha` parser
|
|
429
|
+
// deal with the details.
|
|
430
|
+
//
|
|
431
|
+
// The arguments are parsed with the `entities.arguments` parser.
|
|
432
|
+
//
|
|
433
|
+
call: function () {
|
|
434
|
+
var name, nameLC, args, alpha, index = parserInput.i;
|
|
435
|
+
|
|
436
|
+
if (parserInput.peek(/^url\(/i)) {
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
parserInput.save();
|
|
441
|
+
|
|
442
|
+
name = parserInput.$re(/^([\w-]+|%|progid:[\w\.]+)\(/);
|
|
443
|
+
if (!name) { parserInput.forget(); return; }
|
|
444
|
+
|
|
445
|
+
name = name[1];
|
|
446
|
+
nameLC = name.toLowerCase();
|
|
447
|
+
|
|
448
|
+
if (nameLC === 'alpha') {
|
|
449
|
+
alpha = parsers.alpha();
|
|
450
|
+
if(alpha) {
|
|
451
|
+
return alpha;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
args = this.arguments();
|
|
456
|
+
|
|
457
|
+
if (! parserInput.$char(')')) {
|
|
458
|
+
parserInput.restore("Could not parse call arguments or missing ')'");
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
parserInput.forget();
|
|
463
|
+
return new(tree.Call)(name, args, index, env.currentFileInfo);
|
|
464
|
+
},
|
|
465
|
+
arguments: function () {
|
|
466
|
+
var args = [], arg;
|
|
467
|
+
|
|
468
|
+
while (true) {
|
|
469
|
+
arg = this.assignment() || parsers.expression();
|
|
470
|
+
if (!arg) {
|
|
471
|
+
break;
|
|
472
|
+
}
|
|
473
|
+
args.push(arg);
|
|
474
|
+
if (! parserInput.$char(',')) {
|
|
475
|
+
break;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
return args;
|
|
479
|
+
},
|
|
480
|
+
literal: function () {
|
|
481
|
+
return this.dimension() ||
|
|
482
|
+
this.color() ||
|
|
483
|
+
this.quoted() ||
|
|
484
|
+
this.unicodeDescriptor();
|
|
485
|
+
},
|
|
486
|
+
|
|
487
|
+
// Assignments are argument entities for calls.
|
|
488
|
+
// They are present in ie filter properties as shown below.
|
|
489
|
+
//
|
|
490
|
+
// filter: progid:DXImageTransform.Microsoft.Alpha( *opacity=50* )
|
|
491
|
+
//
|
|
492
|
+
|
|
493
|
+
assignment: function () {
|
|
494
|
+
var key, value;
|
|
495
|
+
key = parserInput.$re(/^\w+(?=\s?=)/i);
|
|
496
|
+
if (!key) {
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
if (!parserInput.$char('=')) {
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
value = parsers.entity();
|
|
503
|
+
if (value) {
|
|
504
|
+
return new(tree.Assignment)(key, value);
|
|
505
|
+
}
|
|
506
|
+
},
|
|
507
|
+
|
|
508
|
+
//
|
|
509
|
+
// Parse url() tokens
|
|
510
|
+
//
|
|
511
|
+
// We use a specific rule for urls, because they don't really behave like
|
|
512
|
+
// standard function calls. The difference is that the argument doesn't have
|
|
513
|
+
// to be enclosed within a string, so it can't be parsed as an Expression.
|
|
514
|
+
//
|
|
515
|
+
url: function () {
|
|
516
|
+
var value;
|
|
517
|
+
|
|
518
|
+
if (parserInput.currentChar() !== 'u' || !parserInput.$re(/^url\(/)) {
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
parserInput.autoCommentAbsorb = false;
|
|
523
|
+
|
|
524
|
+
value = this.quoted() || this.variable() ||
|
|
525
|
+
parserInput.$re(/^(?:(?:\\[\(\)'"])|[^\(\)'"])+/) || "";
|
|
526
|
+
|
|
527
|
+
parserInput.autoCommentAbsorb = true;
|
|
528
|
+
|
|
529
|
+
expectChar(')');
|
|
530
|
+
|
|
531
|
+
return new(tree.URL)((value.value != null || value instanceof tree.Variable)
|
|
532
|
+
? value : new(tree.Anonymous)(value), env.currentFileInfo);
|
|
533
|
+
},
|
|
534
|
+
|
|
535
|
+
//
|
|
536
|
+
// A Variable entity, such as `@fink`, in
|
|
537
|
+
//
|
|
538
|
+
// width: @fink + 2px
|
|
539
|
+
//
|
|
540
|
+
// We use a different parser for variable definitions,
|
|
541
|
+
// see `parsers.variable`.
|
|
542
|
+
//
|
|
543
|
+
variable: function () {
|
|
544
|
+
var name, index = parserInput.i;
|
|
545
|
+
|
|
546
|
+
if (parserInput.currentChar() === '@' && (name = parserInput.$re(/^@@?[\w-]+/))) {
|
|
547
|
+
return new(tree.Variable)(name, index, env.currentFileInfo);
|
|
548
|
+
}
|
|
549
|
+
},
|
|
550
|
+
|
|
551
|
+
// A variable entity useing the protective {} e.g. @{var}
|
|
552
|
+
variableCurly: function () {
|
|
553
|
+
var curly, index = parserInput.i;
|
|
554
|
+
|
|
555
|
+
if (parserInput.currentChar() === '@' && (curly = parserInput.$re(/^@\{([\w-]+)\}/))) {
|
|
556
|
+
return new(tree.Variable)("@" + curly[1], index, env.currentFileInfo);
|
|
557
|
+
}
|
|
558
|
+
},
|
|
559
|
+
|
|
560
|
+
//
|
|
561
|
+
// A Hexadecimal color
|
|
562
|
+
//
|
|
563
|
+
// #4F3C2F
|
|
564
|
+
//
|
|
565
|
+
// `rgb` and `hsl` colors are parsed through the `entities.call` parser.
|
|
566
|
+
//
|
|
567
|
+
color: function () {
|
|
568
|
+
var rgb;
|
|
569
|
+
|
|
570
|
+
if (parserInput.currentChar() === '#' && (rgb = parserInput.$re(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})/))) {
|
|
571
|
+
var colorCandidateString = rgb.input.match(/^#([\w]+).*/); // strip colons, brackets, whitespaces and other characters that should not definitely be part of color string
|
|
572
|
+
colorCandidateString = colorCandidateString[1];
|
|
573
|
+
if (!colorCandidateString.match(/^[A-Fa-f0-9]+$/)) { // verify if candidate consists only of allowed HEX characters
|
|
574
|
+
error("Invalid HEX color code");
|
|
575
|
+
}
|
|
576
|
+
return new(tree.Color)(rgb[1]);
|
|
577
|
+
}
|
|
578
|
+
},
|
|
579
|
+
|
|
580
|
+
//
|
|
581
|
+
// A Dimension, that is, a number and a unit
|
|
582
|
+
//
|
|
583
|
+
// 0.5em 95%
|
|
584
|
+
//
|
|
585
|
+
dimension: function () {
|
|
586
|
+
if (parserInput.peekNotNumeric()) {
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
var value = parserInput.$re(/^([+-]?\d*\.?\d+)(%|[a-z]+)?/);
|
|
591
|
+
if (value) {
|
|
592
|
+
return new(tree.Dimension)(value[1], value[2]);
|
|
593
|
+
}
|
|
594
|
+
},
|
|
595
|
+
|
|
596
|
+
//
|
|
597
|
+
// A unicode descriptor, as is used in unicode-range
|
|
598
|
+
//
|
|
599
|
+
// U+0?? or U+00A1-00A9
|
|
600
|
+
//
|
|
601
|
+
unicodeDescriptor: function () {
|
|
602
|
+
var ud;
|
|
603
|
+
|
|
604
|
+
ud = parserInput.$re(/^U\+[0-9a-fA-F?]+(\-[0-9a-fA-F?]+)?/);
|
|
605
|
+
if (ud) {
|
|
606
|
+
return new(tree.UnicodeDescriptor)(ud[0]);
|
|
607
|
+
}
|
|
608
|
+
},
|
|
609
|
+
|
|
610
|
+
//
|
|
611
|
+
// JavaScript code to be evaluated
|
|
612
|
+
//
|
|
613
|
+
// `window.location.href`
|
|
614
|
+
//
|
|
615
|
+
javascript: function () {
|
|
616
|
+
var js, index = parserInput.i;
|
|
617
|
+
|
|
618
|
+
js = parserInput.$re(/^(~)?`([^`]*)`/);
|
|
619
|
+
if (js) {
|
|
620
|
+
return new(tree.JavaScript)(js[2], index, Boolean(js[1]));
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
},
|
|
624
|
+
|
|
625
|
+
//
|
|
626
|
+
// The variable part of a variable definition. Used in the `rule` parser
|
|
627
|
+
//
|
|
628
|
+
// @fink:
|
|
629
|
+
//
|
|
630
|
+
variable: function () {
|
|
631
|
+
var name;
|
|
632
|
+
|
|
633
|
+
if (parserInput.currentChar() === '@' && (name = parserInput.$re(/^(@[\w-]+)\s*:/))) { return name[1]; }
|
|
634
|
+
},
|
|
635
|
+
|
|
636
|
+
//
|
|
637
|
+
// The variable part of a variable definition. Used in the `rule` parser
|
|
638
|
+
//
|
|
639
|
+
// @fink();
|
|
640
|
+
//
|
|
641
|
+
rulesetCall: function () {
|
|
642
|
+
var name;
|
|
643
|
+
|
|
644
|
+
if (parserInput.currentChar() === '@' && (name = parserInput.$re(/^(@[\w-]+)\s*\(\s*\)\s*;/))) {
|
|
645
|
+
return new tree.RulesetCall(name[1]);
|
|
646
|
+
}
|
|
647
|
+
},
|
|
648
|
+
|
|
649
|
+
//
|
|
650
|
+
// extend syntax - used to extend selectors
|
|
651
|
+
//
|
|
652
|
+
extend: function(isRule) {
|
|
653
|
+
var elements, e, index = parserInput.i, option, extendList, extend;
|
|
654
|
+
|
|
655
|
+
if (!(isRule ? parserInput.$re(/^&:extend\(/) : parserInput.$re(/^:extend\(/))) { return; }
|
|
656
|
+
|
|
657
|
+
do {
|
|
658
|
+
option = null;
|
|
659
|
+
elements = null;
|
|
660
|
+
while (! (option = parserInput.$re(/^(all)(?=\s*(\)|,))/))) {
|
|
661
|
+
e = this.element();
|
|
662
|
+
if (!e) { break; }
|
|
663
|
+
if (elements) { elements.push(e); } else { elements = [ e ]; }
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
option = option && option[1];
|
|
667
|
+
if (!elements)
|
|
668
|
+
error("Missing target selector for :extend().");
|
|
669
|
+
extend = new(tree.Extend)(new(tree.Selector)(elements), option, index);
|
|
670
|
+
if (extendList) { extendList.push(extend); } else { extendList = [ extend ]; }
|
|
671
|
+
|
|
672
|
+
} while(parserInput.$char(","));
|
|
673
|
+
|
|
674
|
+
expect(/^\)/);
|
|
675
|
+
|
|
676
|
+
if (isRule) {
|
|
677
|
+
expect(/^;/);
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
return extendList;
|
|
681
|
+
},
|
|
682
|
+
|
|
683
|
+
//
|
|
684
|
+
// extendRule - used in a rule to extend all the parent selectors
|
|
685
|
+
//
|
|
686
|
+
extendRule: function() {
|
|
687
|
+
return this.extend(true);
|
|
688
|
+
},
|
|
689
|
+
|
|
690
|
+
//
|
|
691
|
+
// Mixins
|
|
692
|
+
//
|
|
693
|
+
mixin: {
|
|
694
|
+
//
|
|
695
|
+
// A Mixin call, with an optional argument list
|
|
696
|
+
//
|
|
697
|
+
// #mixins > .square(#fff);
|
|
698
|
+
// .rounded(4px, black);
|
|
699
|
+
// .button;
|
|
700
|
+
//
|
|
701
|
+
// The `while` loop is there because mixins can be
|
|
702
|
+
// namespaced, but we only support the child and descendant
|
|
703
|
+
// selector for now.
|
|
704
|
+
//
|
|
705
|
+
call: function () {
|
|
706
|
+
var s = parserInput.currentChar(), important = false, index = parserInput.i, elemIndex,
|
|
707
|
+
elements, elem, e, c, args;
|
|
708
|
+
|
|
709
|
+
if (s !== '.' && s !== '#') { return; }
|
|
710
|
+
|
|
711
|
+
parserInput.save(); // stop us absorbing part of an invalid selector
|
|
712
|
+
|
|
713
|
+
while (true) {
|
|
714
|
+
elemIndex = parserInput.i;
|
|
715
|
+
e = parserInput.$re(/^[#.](?:[\w-]|\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+/);
|
|
716
|
+
if (!e) {
|
|
717
|
+
break;
|
|
718
|
+
}
|
|
719
|
+
elem = new(tree.Element)(c, e, elemIndex, env.currentFileInfo);
|
|
720
|
+
if (elements) { elements.push(elem); } else { elements = [ elem ]; }
|
|
721
|
+
c = parserInput.$char('>');
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
if (elements) {
|
|
725
|
+
if (parserInput.$char('(')) {
|
|
726
|
+
args = this.args(true).args;
|
|
727
|
+
expectChar(')');
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
if (parsers.important()) {
|
|
731
|
+
important = true;
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
if (parsers.end()) {
|
|
735
|
+
parserInput.forget();
|
|
736
|
+
return new(tree.mixin.Call)(elements, args, index, env.currentFileInfo, important);
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
parserInput.restore();
|
|
741
|
+
},
|
|
742
|
+
args: function (isCall) {
|
|
743
|
+
var parsers = parser.parsers, entities = parsers.entities,
|
|
744
|
+
returner = { args:null, variadic: false },
|
|
745
|
+
expressions = [], argsSemiColon = [], argsComma = [],
|
|
746
|
+
isSemiColonSeperated, expressionContainsNamed, name, nameLoop, value, arg;
|
|
747
|
+
|
|
748
|
+
parserInput.save();
|
|
749
|
+
|
|
750
|
+
while (true) {
|
|
751
|
+
if (isCall) {
|
|
752
|
+
arg = parsers.detachedRuleset() || parsers.expression();
|
|
753
|
+
} else {
|
|
754
|
+
parserInput.commentStore.length = 0;
|
|
755
|
+
if (parserInput.currentChar() === '.' && parserInput.$re(/^\.{3}/)) {
|
|
756
|
+
returner.variadic = true;
|
|
757
|
+
if (parserInput.$char(";") && !isSemiColonSeperated) {
|
|
758
|
+
isSemiColonSeperated = true;
|
|
759
|
+
}
|
|
760
|
+
(isSemiColonSeperated ? argsSemiColon : argsComma)
|
|
761
|
+
.push({ variadic: true });
|
|
762
|
+
break;
|
|
763
|
+
}
|
|
764
|
+
arg = entities.variable() || entities.literal() || entities.keyword();
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
if (!arg) {
|
|
768
|
+
break;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
nameLoop = null;
|
|
772
|
+
if (arg.throwAwayComments) {
|
|
773
|
+
arg.throwAwayComments();
|
|
774
|
+
}
|
|
775
|
+
value = arg;
|
|
776
|
+
var val = null;
|
|
777
|
+
|
|
778
|
+
if (isCall) {
|
|
779
|
+
// Variable
|
|
780
|
+
if (arg.value && arg.value.length == 1) {
|
|
781
|
+
val = arg.value[0];
|
|
782
|
+
}
|
|
783
|
+
} else {
|
|
784
|
+
val = arg;
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
if (val && val instanceof tree.Variable) {
|
|
788
|
+
if (parserInput.$char(':')) {
|
|
789
|
+
if (expressions.length > 0) {
|
|
790
|
+
if (isSemiColonSeperated) {
|
|
791
|
+
error("Cannot mix ; and , as delimiter types");
|
|
792
|
+
}
|
|
793
|
+
expressionContainsNamed = true;
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
// we do not support setting a ruleset as a default variable - it doesn't make sense
|
|
797
|
+
// However if we do want to add it, there is nothing blocking it, just don't error
|
|
798
|
+
// and remove isCall dependency below
|
|
799
|
+
value = (isCall && parsers.detachedRuleset()) || parsers.expression();
|
|
800
|
+
|
|
801
|
+
if (!value) {
|
|
802
|
+
if (isCall) {
|
|
803
|
+
error("could not understand value for named argument");
|
|
804
|
+
} else {
|
|
805
|
+
parserInput.restore();
|
|
806
|
+
returner.args = [];
|
|
807
|
+
return returner;
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
nameLoop = (name = val.name);
|
|
811
|
+
} else if (!isCall && parserInput.$re(/^\.{3}/)) {
|
|
812
|
+
returner.variadic = true;
|
|
813
|
+
if (parserInput.$char(";") && !isSemiColonSeperated) {
|
|
814
|
+
isSemiColonSeperated = true;
|
|
815
|
+
}
|
|
816
|
+
(isSemiColonSeperated ? argsSemiColon : argsComma)
|
|
817
|
+
.push({ name: arg.name, variadic: true });
|
|
818
|
+
break;
|
|
819
|
+
} else if (!isCall) {
|
|
820
|
+
name = nameLoop = val.name;
|
|
821
|
+
value = null;
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
if (value) {
|
|
826
|
+
expressions.push(value);
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
argsComma.push({ name:nameLoop, value:value });
|
|
830
|
+
|
|
831
|
+
if (parserInput.$char(',')) {
|
|
832
|
+
continue;
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
if (parserInput.$char(';') || isSemiColonSeperated) {
|
|
836
|
+
|
|
837
|
+
if (expressionContainsNamed) {
|
|
838
|
+
error("Cannot mix ; and , as delimiter types");
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
isSemiColonSeperated = true;
|
|
842
|
+
|
|
843
|
+
if (expressions.length > 1) {
|
|
844
|
+
value = new(tree.Value)(expressions);
|
|
845
|
+
}
|
|
846
|
+
argsSemiColon.push({ name:name, value:value });
|
|
847
|
+
|
|
848
|
+
name = null;
|
|
849
|
+
expressions = [];
|
|
850
|
+
expressionContainsNamed = false;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
parserInput.forget();
|
|
855
|
+
returner.args = isSemiColonSeperated ? argsSemiColon : argsComma;
|
|
856
|
+
return returner;
|
|
857
|
+
},
|
|
858
|
+
//
|
|
859
|
+
// A Mixin definition, with a list of parameters
|
|
860
|
+
//
|
|
861
|
+
// .rounded (@radius: 2px, @color) {
|
|
862
|
+
// ...
|
|
863
|
+
// }
|
|
864
|
+
//
|
|
865
|
+
// Until we have a finer grained state-machine, we have to
|
|
866
|
+
// do a look-ahead, to make sure we don't have a mixin call.
|
|
867
|
+
// See the `rule` function for more information.
|
|
868
|
+
//
|
|
869
|
+
// We start by matching `.rounded (`, and then proceed on to
|
|
870
|
+
// the argument list, which has optional default values.
|
|
871
|
+
// We store the parameters in `params`, with a `value` key,
|
|
872
|
+
// if there is a value, such as in the case of `@radius`.
|
|
873
|
+
//
|
|
874
|
+
// Once we've got our params list, and a closing `)`, we parse
|
|
875
|
+
// the `{...}` block.
|
|
876
|
+
//
|
|
877
|
+
definition: function () {
|
|
878
|
+
var name, params = [], match, ruleset, cond, variadic = false;
|
|
879
|
+
if ((parserInput.currentChar() !== '.' && parserInput.currentChar() !== '#') ||
|
|
880
|
+
parserInput.peek(/^[^{]*\}/)) {
|
|
881
|
+
return;
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
parserInput.save();
|
|
885
|
+
|
|
886
|
+
match = parserInput.$re(/^([#.](?:[\w-]|\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+)\s*\(/);
|
|
887
|
+
if (match) {
|
|
888
|
+
name = match[1];
|
|
889
|
+
|
|
890
|
+
var argInfo = this.args(false);
|
|
891
|
+
params = argInfo.args;
|
|
892
|
+
variadic = argInfo.variadic;
|
|
893
|
+
|
|
894
|
+
// .mixincall("@{a}");
|
|
895
|
+
// looks a bit like a mixin definition..
|
|
896
|
+
// also
|
|
897
|
+
// .mixincall(@a: {rule: set;});
|
|
898
|
+
// so we have to be nice and restore
|
|
899
|
+
if (!parserInput.$char(')')) {
|
|
900
|
+
parserInput.restore("Missing closing ')'");
|
|
901
|
+
return;
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
parserInput.commentStore.length = 0;
|
|
905
|
+
|
|
906
|
+
if (parserInput.$re(/^when/)) { // Guard
|
|
907
|
+
cond = expect(parsers.conditions, 'expected condition');
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
ruleset = parsers.block();
|
|
911
|
+
|
|
912
|
+
if (ruleset) {
|
|
913
|
+
parserInput.forget();
|
|
914
|
+
return new(tree.mixin.Definition)(name, params, ruleset, cond, variadic);
|
|
915
|
+
} else {
|
|
916
|
+
parserInput.restore();
|
|
917
|
+
}
|
|
918
|
+
} else {
|
|
919
|
+
parserInput.forget();
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
},
|
|
923
|
+
|
|
924
|
+
//
|
|
925
|
+
// Entities are the smallest recognized token,
|
|
926
|
+
// and can be found inside a rule's value.
|
|
927
|
+
//
|
|
928
|
+
entity: function () {
|
|
929
|
+
var entities = this.entities;
|
|
930
|
+
|
|
931
|
+
return this.comment() || entities.literal() || entities.variable() || entities.url() ||
|
|
932
|
+
entities.call() || entities.keyword() || entities.javascript();
|
|
933
|
+
},
|
|
934
|
+
|
|
935
|
+
//
|
|
936
|
+
// A Rule terminator. Note that we use `peek()` to check for '}',
|
|
937
|
+
// because the `block` rule will be expecting it, but we still need to make sure
|
|
938
|
+
// it's there, if ';' was ommitted.
|
|
939
|
+
//
|
|
940
|
+
end: function () {
|
|
941
|
+
return parserInput.$char(';') || parserInput.peek('}');
|
|
942
|
+
},
|
|
943
|
+
|
|
944
|
+
//
|
|
945
|
+
// IE's alpha function
|
|
946
|
+
//
|
|
947
|
+
// alpha(opacity=88)
|
|
948
|
+
//
|
|
949
|
+
alpha: function () {
|
|
950
|
+
var value;
|
|
951
|
+
|
|
952
|
+
if (! parserInput.$re(/^opacity=/i)) { return; }
|
|
953
|
+
value = parserInput.$re(/^\d+/);
|
|
954
|
+
if (!value) {
|
|
955
|
+
value = expect(this.entities.variable, "Could not parse alpha");
|
|
956
|
+
}
|
|
957
|
+
expectChar(')');
|
|
958
|
+
return new(tree.Alpha)(value);
|
|
959
|
+
},
|
|
960
|
+
|
|
961
|
+
//
|
|
962
|
+
// A Selector Element
|
|
963
|
+
//
|
|
964
|
+
// div
|
|
965
|
+
// + h1
|
|
966
|
+
// #socks
|
|
967
|
+
// input[type="text"]
|
|
968
|
+
//
|
|
969
|
+
// Elements are the building blocks for Selectors,
|
|
970
|
+
// they are made out of a `Combinator` (see combinator rule),
|
|
971
|
+
// and an element name, such as a tag a class, or `*`.
|
|
972
|
+
//
|
|
973
|
+
element: function () {
|
|
974
|
+
var e, c, v, index = parserInput.i;
|
|
975
|
+
|
|
976
|
+
c = this.combinator();
|
|
977
|
+
|
|
978
|
+
e = parserInput.$re(/^(?:\d+\.\d+|\d+)%/) || parserInput.$re(/^(?:[.#]?|:*)(?:[\w-]|[^\x00-\x9f]|\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+/) ||
|
|
979
|
+
parserInput.$char('*') || parserInput.$char('&') || this.attribute() || parserInput.$re(/^\([^()@]+\)/) || parserInput.$re(/^[\.#](?=@)/) ||
|
|
980
|
+
this.entities.variableCurly();
|
|
981
|
+
|
|
982
|
+
if (! e) {
|
|
983
|
+
parserInput.save();
|
|
984
|
+
if (parserInput.$char('(')) {
|
|
985
|
+
if ((v = this.selector()) && parserInput.$char(')')) {
|
|
986
|
+
e = new(tree.Paren)(v);
|
|
987
|
+
parserInput.forget();
|
|
988
|
+
} else {
|
|
989
|
+
parserInput.restore("Missing closing ')'");
|
|
990
|
+
}
|
|
991
|
+
} else {
|
|
992
|
+
parserInput.forget();
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
if (e) { return new(tree.Element)(c, e, index, env.currentFileInfo); }
|
|
997
|
+
},
|
|
998
|
+
|
|
999
|
+
//
|
|
1000
|
+
// Combinators combine elements together, in a Selector.
|
|
1001
|
+
//
|
|
1002
|
+
// Because our parser isn't white-space sensitive, special care
|
|
1003
|
+
// has to be taken, when parsing the descendant combinator, ` `,
|
|
1004
|
+
// as it's an empty space. We have to check the previous character
|
|
1005
|
+
// in the input, to see if it's a ` ` character. More info on how
|
|
1006
|
+
// we deal with this in *combinator.js*.
|
|
1007
|
+
//
|
|
1008
|
+
combinator: function () {
|
|
1009
|
+
var c = parserInput.currentChar();
|
|
1010
|
+
|
|
1011
|
+
if (c === '/') {
|
|
1012
|
+
parserInput.save();
|
|
1013
|
+
var slashedCombinator = parserInput.$re(/^\/[a-z]+\//i);
|
|
1014
|
+
if (slashedCombinator) {
|
|
1015
|
+
parserInput.forget();
|
|
1016
|
+
return new(tree.Combinator)(slashedCombinator);
|
|
1017
|
+
}
|
|
1018
|
+
parserInput.restore();
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
if (c === '>' || c === '+' || c === '~' || c === '|' || c === '^') {
|
|
1022
|
+
parserInput.i++;
|
|
1023
|
+
if (c === '^' && parserInput.currentChar() === '^') {
|
|
1024
|
+
c = '^^';
|
|
1025
|
+
parserInput.i++;
|
|
1026
|
+
}
|
|
1027
|
+
while (parserInput.isWhitespace()) { parserInput.i++; }
|
|
1028
|
+
return new(tree.Combinator)(c);
|
|
1029
|
+
} else if (parserInput.isWhitespace(-1)) {
|
|
1030
|
+
return new(tree.Combinator)(" ");
|
|
1031
|
+
} else {
|
|
1032
|
+
return new(tree.Combinator)(null);
|
|
1033
|
+
}
|
|
1034
|
+
},
|
|
1035
|
+
//
|
|
1036
|
+
// A CSS selector (see selector below)
|
|
1037
|
+
// with less extensions e.g. the ability to extend and guard
|
|
1038
|
+
//
|
|
1039
|
+
lessSelector: function () {
|
|
1040
|
+
return this.selector(true);
|
|
1041
|
+
},
|
|
1042
|
+
//
|
|
1043
|
+
// A CSS Selector
|
|
1044
|
+
//
|
|
1045
|
+
// .class > div + h1
|
|
1046
|
+
// li a:hover
|
|
1047
|
+
//
|
|
1048
|
+
// Selectors are made out of one or more Elements, see above.
|
|
1049
|
+
//
|
|
1050
|
+
selector: function (isLess) {
|
|
1051
|
+
var index = parserInput.i, elements, extendList, c, e, extend, when, condition;
|
|
1052
|
+
|
|
1053
|
+
while ((isLess && (extend = this.extend())) || (isLess && (when = parserInput.$re(/^when/))) || (e = this.element())) {
|
|
1054
|
+
if (when) {
|
|
1055
|
+
condition = expect(this.conditions, 'expected condition');
|
|
1056
|
+
} else if (condition) {
|
|
1057
|
+
error("CSS guard can only be used at the end of selector");
|
|
1058
|
+
} else if (extend) {
|
|
1059
|
+
if (extendList) { extendList.push(extend); } else { extendList = [ extend ]; }
|
|
1060
|
+
} else {
|
|
1061
|
+
if (extendList) { error("Extend can only be used at the end of selector"); }
|
|
1062
|
+
c = parserInput.currentChar();
|
|
1063
|
+
if (elements) { elements.push(e); } else { elements = [ e ]; }
|
|
1064
|
+
e = null;
|
|
1065
|
+
}
|
|
1066
|
+
if (c === '{' || c === '}' || c === ';' || c === ',' || c === ')') {
|
|
1067
|
+
break;
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
if (elements) { return new(tree.Selector)(elements, extendList, condition, index, env.currentFileInfo); }
|
|
1072
|
+
if (extendList) { error("Extend must be used to extend a selector, it cannot be used on its own"); }
|
|
1073
|
+
},
|
|
1074
|
+
attribute: function () {
|
|
1075
|
+
if (! parserInput.$char('[')) { return; }
|
|
1076
|
+
|
|
1077
|
+
var entities = this.entities,
|
|
1078
|
+
key, val, op;
|
|
1079
|
+
|
|
1080
|
+
if (!(key = entities.variableCurly())) {
|
|
1081
|
+
key = expect(/^(?:[_A-Za-z0-9-\*]*\|)?(?:[_A-Za-z0-9-]|\\.)+/);
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
op = parserInput.$re(/^[|~*$^]?=/);
|
|
1085
|
+
if (op) {
|
|
1086
|
+
val = entities.quoted() || parserInput.$re(/^[0-9]+%/) || parserInput.$re(/^[\w-]+/) || entities.variableCurly();
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
expectChar(']');
|
|
1090
|
+
|
|
1091
|
+
return new(tree.Attribute)(key, op, val);
|
|
1092
|
+
},
|
|
1093
|
+
|
|
1094
|
+
//
|
|
1095
|
+
// The `block` rule is used by `ruleset` and `mixin.definition`.
|
|
1096
|
+
// It's a wrapper around the `primary` rule, with added `{}`.
|
|
1097
|
+
//
|
|
1098
|
+
block: function () {
|
|
1099
|
+
var content;
|
|
1100
|
+
if (parserInput.$char('{') && (content = this.primary()) && parserInput.$char('}')) {
|
|
1101
|
+
return content;
|
|
1102
|
+
}
|
|
1103
|
+
},
|
|
1104
|
+
|
|
1105
|
+
blockRuleset: function() {
|
|
1106
|
+
var block = this.block();
|
|
1107
|
+
|
|
1108
|
+
if (block) {
|
|
1109
|
+
block = new tree.Ruleset(null, block);
|
|
1110
|
+
}
|
|
1111
|
+
return block;
|
|
1112
|
+
},
|
|
1113
|
+
|
|
1114
|
+
detachedRuleset: function() {
|
|
1115
|
+
var blockRuleset = this.blockRuleset();
|
|
1116
|
+
if (blockRuleset) {
|
|
1117
|
+
return new tree.DetachedRuleset(blockRuleset);
|
|
1118
|
+
}
|
|
1119
|
+
},
|
|
1120
|
+
|
|
1121
|
+
//
|
|
1122
|
+
// div, .class, body > p {...}
|
|
1123
|
+
//
|
|
1124
|
+
ruleset: function () {
|
|
1125
|
+
var selectors, s, rules, debugInfo;
|
|
1126
|
+
|
|
1127
|
+
parserInput.save();
|
|
1128
|
+
|
|
1129
|
+
if (env.dumpLineNumbers) {
|
|
1130
|
+
debugInfo = getDebugInfo(parserInput.i);
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
while (true) {
|
|
1134
|
+
s = this.lessSelector();
|
|
1135
|
+
if (!s) {
|
|
1136
|
+
break;
|
|
1137
|
+
}
|
|
1138
|
+
if (selectors) { selectors.push(s); } else { selectors = [ s ]; }
|
|
1139
|
+
parserInput.commentStore.length = 0;
|
|
1140
|
+
if (s.condition && selectors.length > 1) {
|
|
1141
|
+
error("Guards are only currently allowed on a single selector.");
|
|
1142
|
+
}
|
|
1143
|
+
if (! parserInput.$char(',')) { break; }
|
|
1144
|
+
if (s.condition) {
|
|
1145
|
+
error("Guards are only currently allowed on a single selector.");
|
|
1146
|
+
}
|
|
1147
|
+
parserInput.commentStore.length = 0;
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
if (selectors && (rules = this.block())) {
|
|
1151
|
+
parserInput.forget();
|
|
1152
|
+
var ruleset = new(tree.Ruleset)(selectors, rules, env.strictImports);
|
|
1153
|
+
if (env.dumpLineNumbers) {
|
|
1154
|
+
ruleset.debugInfo = debugInfo;
|
|
1155
|
+
}
|
|
1156
|
+
return ruleset;
|
|
1157
|
+
} else {
|
|
1158
|
+
parserInput.restore();
|
|
1159
|
+
}
|
|
1160
|
+
},
|
|
1161
|
+
rule: function (tryAnonymous) {
|
|
1162
|
+
var name, value, startOfRule = parserInput.i, c = parserInput.currentChar(), important, merge, isVariable;
|
|
1163
|
+
|
|
1164
|
+
if (c === '.' || c === '#' || c === '&') { return; }
|
|
1165
|
+
|
|
1166
|
+
parserInput.save();
|
|
1167
|
+
|
|
1168
|
+
name = this.variable() || this.ruleProperty();
|
|
1169
|
+
if (name) {
|
|
1170
|
+
isVariable = typeof name === "string";
|
|
1171
|
+
|
|
1172
|
+
if (isVariable) {
|
|
1173
|
+
value = this.detachedRuleset();
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
this.comments();
|
|
1177
|
+
if (!value) {
|
|
1178
|
+
// a name returned by this.ruleProperty() is always an array of the form:
|
|
1179
|
+
// [string-1, ..., string-n, ""] or [string-1, ..., string-n, "+"]
|
|
1180
|
+
// where each item is a tree.Keyword or tree.Variable
|
|
1181
|
+
merge = !isVariable && name.pop().value;
|
|
1182
|
+
|
|
1183
|
+
// prefer to try to parse first if its a variable or we are compressing
|
|
1184
|
+
// but always fallback on the other one
|
|
1185
|
+
var tryValueFirst = !tryAnonymous && (env.compress || isVariable);
|
|
1186
|
+
|
|
1187
|
+
if (tryValueFirst) {
|
|
1188
|
+
value = this.value();
|
|
1189
|
+
}
|
|
1190
|
+
if (!value) {
|
|
1191
|
+
value = this.anonymousValue();
|
|
1192
|
+
if (value) {
|
|
1193
|
+
parserInput.forget();
|
|
1194
|
+
// anonymous values absorb the end ';' which is reequired for them to work
|
|
1195
|
+
return new (tree.Rule)(name, value, false, merge, startOfRule, env.currentFileInfo);
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
if (!tryValueFirst && !value) {
|
|
1199
|
+
value = this.value();
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
important = this.important();
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
if (value && this.end()) {
|
|
1206
|
+
parserInput.forget();
|
|
1207
|
+
return new (tree.Rule)(name, value, important, merge, startOfRule, env.currentFileInfo);
|
|
1208
|
+
} else {
|
|
1209
|
+
parserInput.restore();
|
|
1210
|
+
if (value && !tryAnonymous) {
|
|
1211
|
+
return this.rule(true);
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
} else {
|
|
1215
|
+
parserInput.forget();
|
|
1216
|
+
}
|
|
1217
|
+
},
|
|
1218
|
+
anonymousValue: function () {
|
|
1219
|
+
var match = parserInput.$re(/^([^@+\/'"*`(;{}-]*);/);
|
|
1220
|
+
if (match) {
|
|
1221
|
+
return new(tree.Anonymous)(match[1]);
|
|
1222
|
+
}
|
|
1223
|
+
},
|
|
1224
|
+
|
|
1225
|
+
//
|
|
1226
|
+
// An @import directive
|
|
1227
|
+
//
|
|
1228
|
+
// @import "lib";
|
|
1229
|
+
//
|
|
1230
|
+
// Depending on our environment, importing is done differently:
|
|
1231
|
+
// In the browser, it's an XHR request, in Node, it would be a
|
|
1232
|
+
// file-system operation. The function used for importing is
|
|
1233
|
+
// stored in `import`, which we pass to the Import constructor.
|
|
1234
|
+
//
|
|
1235
|
+
"import": function () {
|
|
1236
|
+
var path, features, index = parserInput.i;
|
|
1237
|
+
|
|
1238
|
+
var dir = parserInput.$re(/^@import?\s+/);
|
|
1239
|
+
|
|
1240
|
+
if (dir) {
|
|
1241
|
+
var options = (dir ? this.importOptions() : null) || {};
|
|
1242
|
+
|
|
1243
|
+
if ((path = this.entities.quoted() || this.entities.url())) {
|
|
1244
|
+
features = this.mediaFeatures();
|
|
1245
|
+
|
|
1246
|
+
if (!parserInput.$(';')) {
|
|
1247
|
+
parserInput.i = index;
|
|
1248
|
+
error("missing semi-colon or unrecognised media features on import");
|
|
1249
|
+
}
|
|
1250
|
+
features = features && new(tree.Value)(features);
|
|
1251
|
+
return new(tree.Import)(path, features, options, index, env.currentFileInfo);
|
|
1252
|
+
}
|
|
1253
|
+
else
|
|
1254
|
+
{
|
|
1255
|
+
parserInput.i = index;
|
|
1256
|
+
error("malformed import statement");
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
},
|
|
1260
|
+
|
|
1261
|
+
importOptions: function() {
|
|
1262
|
+
var o, options = {}, optionName, value;
|
|
1263
|
+
|
|
1264
|
+
// list of options, surrounded by parens
|
|
1265
|
+
if (! parserInput.$char('(')) { return null; }
|
|
1266
|
+
do {
|
|
1267
|
+
o = this.importOption();
|
|
1268
|
+
if (o) {
|
|
1269
|
+
optionName = o;
|
|
1270
|
+
value = true;
|
|
1271
|
+
switch(optionName) {
|
|
1272
|
+
case "css":
|
|
1273
|
+
optionName = "less";
|
|
1274
|
+
value = false;
|
|
1275
|
+
break;
|
|
1276
|
+
case "once":
|
|
1277
|
+
optionName = "multiple";
|
|
1278
|
+
value = false;
|
|
1279
|
+
break;
|
|
1280
|
+
}
|
|
1281
|
+
options[optionName] = value;
|
|
1282
|
+
if (! parserInput.$char(',')) { break; }
|
|
1283
|
+
}
|
|
1284
|
+
} while (o);
|
|
1285
|
+
expectChar(')');
|
|
1286
|
+
return options;
|
|
1287
|
+
},
|
|
1288
|
+
|
|
1289
|
+
importOption: function() {
|
|
1290
|
+
var opt = parserInput.$re(/^(less|css|multiple|once|inline|reference)/);
|
|
1291
|
+
if (opt) {
|
|
1292
|
+
return opt[1];
|
|
1293
|
+
}
|
|
1294
|
+
},
|
|
1295
|
+
|
|
1296
|
+
mediaFeature: function () {
|
|
1297
|
+
var entities = this.entities, nodes = [], e, p;
|
|
1298
|
+
parserInput.save();
|
|
1299
|
+
do {
|
|
1300
|
+
e = entities.keyword() || entities.variable();
|
|
1301
|
+
if (e) {
|
|
1302
|
+
nodes.push(e);
|
|
1303
|
+
} else if (parserInput.$char('(')) {
|
|
1304
|
+
p = this.property();
|
|
1305
|
+
e = this.value();
|
|
1306
|
+
if (parserInput.$char(')')) {
|
|
1307
|
+
if (p && e) {
|
|
1308
|
+
nodes.push(new(tree.Paren)(new(tree.Rule)(p, e, null, null, parserInput.i, env.currentFileInfo, true)));
|
|
1309
|
+
} else if (e) {
|
|
1310
|
+
nodes.push(new(tree.Paren)(e));
|
|
1311
|
+
} else {
|
|
1312
|
+
parserInput.restore("badly formed media feature definition");
|
|
1313
|
+
return null;
|
|
1314
|
+
}
|
|
1315
|
+
} else {
|
|
1316
|
+
parserInput.restore("Missing closing ')'");
|
|
1317
|
+
return null;
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
} while (e);
|
|
1321
|
+
|
|
1322
|
+
parserInput.forget();
|
|
1323
|
+
if (nodes.length > 0) {
|
|
1324
|
+
return new(tree.Expression)(nodes);
|
|
1325
|
+
}
|
|
1326
|
+
},
|
|
1327
|
+
|
|
1328
|
+
mediaFeatures: function () {
|
|
1329
|
+
var entities = this.entities, features = [], e;
|
|
1330
|
+
do {
|
|
1331
|
+
e = this.mediaFeature();
|
|
1332
|
+
if (e) {
|
|
1333
|
+
features.push(e);
|
|
1334
|
+
if (! parserInput.$char(',')) { break; }
|
|
1335
|
+
} else {
|
|
1336
|
+
e = entities.variable();
|
|
1337
|
+
if (e) {
|
|
1338
|
+
features.push(e);
|
|
1339
|
+
if (! parserInput.$char(',')) { break; }
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
} while (e);
|
|
1343
|
+
|
|
1344
|
+
return features.length > 0 ? features : null;
|
|
1345
|
+
},
|
|
1346
|
+
|
|
1347
|
+
media: function () {
|
|
1348
|
+
var features, rules, media, debugInfo;
|
|
1349
|
+
|
|
1350
|
+
if (env.dumpLineNumbers) {
|
|
1351
|
+
debugInfo = getDebugInfo(parserInput.i);
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
if (parserInput.$re(/^@media/)) {
|
|
1355
|
+
features = this.mediaFeatures();
|
|
1356
|
+
|
|
1357
|
+
rules = this.block();
|
|
1358
|
+
if (rules) {
|
|
1359
|
+
media = new(tree.Media)(rules, features, parserInput.i, env.currentFileInfo);
|
|
1360
|
+
if (env.dumpLineNumbers) {
|
|
1361
|
+
media.debugInfo = debugInfo;
|
|
1362
|
+
}
|
|
1363
|
+
return media;
|
|
1364
|
+
}
|
|
1365
|
+
}
|
|
1366
|
+
},
|
|
1367
|
+
|
|
1368
|
+
//
|
|
1369
|
+
// A CSS Directive
|
|
1370
|
+
//
|
|
1371
|
+
// @charset "utf-8";
|
|
1372
|
+
//
|
|
1373
|
+
directive: function () {
|
|
1374
|
+
var index = parserInput.i, name, value, rules, nonVendorSpecificName,
|
|
1375
|
+
hasIdentifier, hasExpression, hasUnknown, hasBlock = true;
|
|
1376
|
+
|
|
1377
|
+
if (parserInput.currentChar() !== '@') { return; }
|
|
1378
|
+
|
|
1379
|
+
value = this['import']() || this.media();
|
|
1380
|
+
if (value) {
|
|
1381
|
+
return value;
|
|
1382
|
+
}
|
|
1383
|
+
|
|
1384
|
+
parserInput.save();
|
|
1385
|
+
|
|
1386
|
+
name = parserInput.$re(/^@[a-z-]+/);
|
|
1387
|
+
|
|
1388
|
+
if (!name) { return; }
|
|
1389
|
+
|
|
1390
|
+
nonVendorSpecificName = name;
|
|
1391
|
+
if (name.charAt(1) == '-' && name.indexOf('-', 2) > 0) {
|
|
1392
|
+
nonVendorSpecificName = "@" + name.slice(name.indexOf('-', 2) + 1);
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
switch(nonVendorSpecificName) {
|
|
1396
|
+
/*
|
|
1397
|
+
case "@font-face":
|
|
1398
|
+
case "@viewport":
|
|
1399
|
+
case "@top-left":
|
|
1400
|
+
case "@top-left-corner":
|
|
1401
|
+
case "@top-center":
|
|
1402
|
+
case "@top-right":
|
|
1403
|
+
case "@top-right-corner":
|
|
1404
|
+
case "@bottom-left":
|
|
1405
|
+
case "@bottom-left-corner":
|
|
1406
|
+
case "@bottom-center":
|
|
1407
|
+
case "@bottom-right":
|
|
1408
|
+
case "@bottom-right-corner":
|
|
1409
|
+
case "@left-top":
|
|
1410
|
+
case "@left-middle":
|
|
1411
|
+
case "@left-bottom":
|
|
1412
|
+
case "@right-top":
|
|
1413
|
+
case "@right-middle":
|
|
1414
|
+
case "@right-bottom":
|
|
1415
|
+
hasBlock = true;
|
|
1416
|
+
break;
|
|
1417
|
+
*/
|
|
1418
|
+
case "@charset":
|
|
1419
|
+
hasIdentifier = true;
|
|
1420
|
+
hasBlock = false;
|
|
1421
|
+
break;
|
|
1422
|
+
case "@namespace":
|
|
1423
|
+
hasExpression = true;
|
|
1424
|
+
hasBlock = false;
|
|
1425
|
+
break;
|
|
1426
|
+
case "@keyframes":
|
|
1427
|
+
hasIdentifier = true;
|
|
1428
|
+
break;
|
|
1429
|
+
case "@host":
|
|
1430
|
+
case "@page":
|
|
1431
|
+
case "@document":
|
|
1432
|
+
case "@supports":
|
|
1433
|
+
hasUnknown = true;
|
|
1434
|
+
break;
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
this.comments();
|
|
1438
|
+
|
|
1439
|
+
if (hasIdentifier) {
|
|
1440
|
+
value = this.entity();
|
|
1441
|
+
if (!value) {
|
|
1442
|
+
error("expected " + name + " identifier");
|
|
1443
|
+
}
|
|
1444
|
+
} else if (hasExpression) {
|
|
1445
|
+
value = this.expression();
|
|
1446
|
+
if (!value) {
|
|
1447
|
+
error("expected " + name + " expression");
|
|
1448
|
+
}
|
|
1449
|
+
} else if (hasUnknown) {
|
|
1450
|
+
value = (parserInput.$re(/^[^{;]+/) || '').trim();
|
|
1451
|
+
if (value) {
|
|
1452
|
+
value = new(tree.Anonymous)(value);
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
this.comments();
|
|
1457
|
+
|
|
1458
|
+
if (hasBlock) {
|
|
1459
|
+
rules = this.blockRuleset();
|
|
1460
|
+
}
|
|
1461
|
+
|
|
1462
|
+
if (rules || (!hasBlock && value && parserInput.$char(';'))) {
|
|
1463
|
+
parserInput.forget();
|
|
1464
|
+
return new(tree.Directive)(name, value, rules, index, env.currentFileInfo,
|
|
1465
|
+
env.dumpLineNumbers ? getDebugInfo(index) : null);
|
|
1466
|
+
}
|
|
1467
|
+
|
|
1468
|
+
parserInput.restore("directive options not recognised");
|
|
1469
|
+
},
|
|
1470
|
+
|
|
1471
|
+
//
|
|
1472
|
+
// A Value is a comma-delimited list of Expressions
|
|
1473
|
+
//
|
|
1474
|
+
// font-family: Baskerville, Georgia, serif;
|
|
1475
|
+
//
|
|
1476
|
+
// In a Rule, a Value represents everything after the `:`,
|
|
1477
|
+
// and before the `;`.
|
|
1478
|
+
//
|
|
1479
|
+
value: function () {
|
|
1480
|
+
var e, expressions = [];
|
|
1481
|
+
|
|
1482
|
+
do {
|
|
1483
|
+
e = this.expression();
|
|
1484
|
+
if (e) {
|
|
1485
|
+
expressions.push(e);
|
|
1486
|
+
if (! parserInput.$char(',')) { break; }
|
|
1487
|
+
}
|
|
1488
|
+
} while(e);
|
|
1489
|
+
|
|
1490
|
+
if (expressions.length > 0) {
|
|
1491
|
+
return new(tree.Value)(expressions);
|
|
1492
|
+
}
|
|
1493
|
+
},
|
|
1494
|
+
important: function () {
|
|
1495
|
+
if (parserInput.currentChar() === '!') {
|
|
1496
|
+
return parserInput.$re(/^! *important/);
|
|
1497
|
+
}
|
|
1498
|
+
},
|
|
1499
|
+
sub: function () {
|
|
1500
|
+
var a, e;
|
|
1501
|
+
|
|
1502
|
+
if (parserInput.$char('(')) {
|
|
1503
|
+
a = this.addition();
|
|
1504
|
+
if (a) {
|
|
1505
|
+
e = new(tree.Expression)([a]);
|
|
1506
|
+
expectChar(')');
|
|
1507
|
+
e.parens = true;
|
|
1508
|
+
return e;
|
|
1509
|
+
}
|
|
1510
|
+
}
|
|
1511
|
+
},
|
|
1512
|
+
multiplication: function () {
|
|
1513
|
+
var m, a, op, operation, isSpaced;
|
|
1514
|
+
m = this.operand();
|
|
1515
|
+
if (m) {
|
|
1516
|
+
isSpaced = parserInput.isWhitespace(-1);
|
|
1517
|
+
while (true) {
|
|
1518
|
+
if (parserInput.peek(/^\/[*\/]/)) {
|
|
1519
|
+
break;
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1522
|
+
parserInput.save();
|
|
1523
|
+
|
|
1524
|
+
op = parserInput.$char('/') || parserInput.$char('*');
|
|
1525
|
+
|
|
1526
|
+
if (!op) { parserInput.forget(); break; }
|
|
1527
|
+
|
|
1528
|
+
a = this.operand();
|
|
1529
|
+
|
|
1530
|
+
if (!a) { parserInput.restore(); break; }
|
|
1531
|
+
parserInput.forget();
|
|
1532
|
+
|
|
1533
|
+
m.parensInOp = true;
|
|
1534
|
+
a.parensInOp = true;
|
|
1535
|
+
operation = new(tree.Operation)(op, [operation || m, a], isSpaced);
|
|
1536
|
+
isSpaced = parserInput.isWhitespace(-1);
|
|
1537
|
+
}
|
|
1538
|
+
return operation || m;
|
|
1539
|
+
}
|
|
1540
|
+
},
|
|
1541
|
+
addition: function () {
|
|
1542
|
+
var m, a, op, operation, isSpaced;
|
|
1543
|
+
m = this.multiplication();
|
|
1544
|
+
if (m) {
|
|
1545
|
+
isSpaced = parserInput.isWhitespace(-1);
|
|
1546
|
+
while (true) {
|
|
1547
|
+
op = parserInput.$re(/^[-+]\s+/) || (!isSpaced && (parserInput.$char('+') || parserInput.$char('-')));
|
|
1548
|
+
if (!op) {
|
|
1549
|
+
break;
|
|
1550
|
+
}
|
|
1551
|
+
a = this.multiplication();
|
|
1552
|
+
if (!a) {
|
|
1553
|
+
break;
|
|
1554
|
+
}
|
|
1555
|
+
|
|
1556
|
+
m.parensInOp = true;
|
|
1557
|
+
a.parensInOp = true;
|
|
1558
|
+
operation = new(tree.Operation)(op, [operation || m, a], isSpaced);
|
|
1559
|
+
isSpaced = parserInput.isWhitespace(-1);
|
|
1560
|
+
}
|
|
1561
|
+
return operation || m;
|
|
1562
|
+
}
|
|
1563
|
+
},
|
|
1564
|
+
conditions: function () {
|
|
1565
|
+
var a, b, index = parserInput.i, condition;
|
|
1566
|
+
|
|
1567
|
+
a = this.condition();
|
|
1568
|
+
if (a) {
|
|
1569
|
+
while (true) {
|
|
1570
|
+
if (!parserInput.peek(/^,\s*(not\s*)?\(/) || !parserInput.$char(',')) {
|
|
1571
|
+
break;
|
|
1572
|
+
}
|
|
1573
|
+
b = this.condition();
|
|
1574
|
+
if (!b) {
|
|
1575
|
+
break;
|
|
1576
|
+
}
|
|
1577
|
+
condition = new(tree.Condition)('or', condition || a, b, index);
|
|
1578
|
+
}
|
|
1579
|
+
return condition || a;
|
|
1580
|
+
}
|
|
1581
|
+
},
|
|
1582
|
+
condition: function () {
|
|
1583
|
+
var entities = this.entities, index = parserInput.i, negate = false,
|
|
1584
|
+
a, b, c, op;
|
|
1585
|
+
|
|
1586
|
+
if (parserInput.$re(/^not/)) { negate = true; }
|
|
1587
|
+
expectChar('(');
|
|
1588
|
+
a = this.addition() || entities.keyword() || entities.quoted();
|
|
1589
|
+
if (a) {
|
|
1590
|
+
op = parserInput.$re(/^(?:>=|<=|=<|[<=>])/);
|
|
1591
|
+
if (op) {
|
|
1592
|
+
b = this.addition() || entities.keyword() || entities.quoted();
|
|
1593
|
+
if (b) {
|
|
1594
|
+
c = new(tree.Condition)(op, a, b, index, negate);
|
|
1595
|
+
} else {
|
|
1596
|
+
error('expected expression');
|
|
1597
|
+
}
|
|
1598
|
+
} else {
|
|
1599
|
+
c = new(tree.Condition)('=', a, new(tree.Keyword)('true'), index, negate);
|
|
1600
|
+
}
|
|
1601
|
+
expectChar(')');
|
|
1602
|
+
return parserInput.$re(/^and/) ? new(tree.Condition)('and', c, this.condition()) : c;
|
|
1603
|
+
}
|
|
1604
|
+
},
|
|
1605
|
+
|
|
1606
|
+
//
|
|
1607
|
+
// An operand is anything that can be part of an operation,
|
|
1608
|
+
// such as a Color, or a Variable
|
|
1609
|
+
//
|
|
1610
|
+
operand: function () {
|
|
1611
|
+
var entities = this.entities, negate;
|
|
1612
|
+
|
|
1613
|
+
if (parserInput.peek(/^-[@\(]/)) {
|
|
1614
|
+
negate = parserInput.$char('-');
|
|
1615
|
+
}
|
|
1616
|
+
|
|
1617
|
+
var o = this.sub() || entities.dimension() ||
|
|
1618
|
+
entities.color() || entities.variable() ||
|
|
1619
|
+
entities.call();
|
|
1620
|
+
|
|
1621
|
+
if (negate) {
|
|
1622
|
+
o.parensInOp = true;
|
|
1623
|
+
o = new(tree.Negative)(o);
|
|
1624
|
+
}
|
|
1625
|
+
|
|
1626
|
+
return o;
|
|
1627
|
+
},
|
|
1628
|
+
|
|
1629
|
+
//
|
|
1630
|
+
// Expressions either represent mathematical operations,
|
|
1631
|
+
// or white-space delimited Entities.
|
|
1632
|
+
//
|
|
1633
|
+
// 1px solid black
|
|
1634
|
+
// @var * 2
|
|
1635
|
+
//
|
|
1636
|
+
expression: function () {
|
|
1637
|
+
var entities = [], e, delim;
|
|
1638
|
+
|
|
1639
|
+
do {
|
|
1640
|
+
e = this.comment();
|
|
1641
|
+
if (e) {
|
|
1642
|
+
entities.push(e);
|
|
1643
|
+
continue;
|
|
1644
|
+
}
|
|
1645
|
+
e = this.addition() || this.entity();
|
|
1646
|
+
if (e) {
|
|
1647
|
+
entities.push(e);
|
|
1648
|
+
// operations do not allow keyword "/" dimension (e.g. small/20px) so we support that here
|
|
1649
|
+
if (!parserInput.peek(/^\/[\/*]/)) {
|
|
1650
|
+
delim = parserInput.$char('/');
|
|
1651
|
+
if (delim) {
|
|
1652
|
+
entities.push(new(tree.Anonymous)(delim));
|
|
1653
|
+
}
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
} while (e);
|
|
1657
|
+
if (entities.length > 0) {
|
|
1658
|
+
return new(tree.Expression)(entities);
|
|
1659
|
+
}
|
|
1660
|
+
},
|
|
1661
|
+
property: function () {
|
|
1662
|
+
var name = parserInput.$re(/^(\*?-?[_a-zA-Z0-9-]+)\s*:/);
|
|
1663
|
+
if (name) {
|
|
1664
|
+
return name[1];
|
|
1665
|
+
}
|
|
1666
|
+
},
|
|
1667
|
+
ruleProperty: function () {
|
|
1668
|
+
var name = [], index = [], s, k;
|
|
1669
|
+
|
|
1670
|
+
parserInput.save();
|
|
1671
|
+
|
|
1672
|
+
function match(re) {
|
|
1673
|
+
var i = parserInput.i,
|
|
1674
|
+
chunk = parserInput.$re(re);
|
|
1675
|
+
if (chunk) {
|
|
1676
|
+
index.push(i);
|
|
1677
|
+
return name.push(chunk[1]);
|
|
1678
|
+
}
|
|
1679
|
+
}
|
|
1680
|
+
function cutOutBlockComments() {
|
|
1681
|
+
//match block comments
|
|
1682
|
+
var a = /^\s*\/\*(?:[^*]|\*+[^\/*])*\*+\//.exec(c);
|
|
1683
|
+
if (a) {
|
|
1684
|
+
length += a[0].length;
|
|
1685
|
+
c = c.slice(a[0].length);
|
|
1686
|
+
return true;
|
|
1687
|
+
}
|
|
1688
|
+
return false;
|
|
1689
|
+
}
|
|
1690
|
+
|
|
1691
|
+
match(/^(\*?)/);
|
|
1692
|
+
<<<<<<< HEAD:lib/less/parser/parser.js
|
|
1693
|
+
while (true) {
|
|
1694
|
+
if (!match(/^((?:[\w-]+)|(?:@\{[\w-]+\}))/)) {
|
|
1695
|
+
break;
|
|
1696
|
+
}
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
if ((name.length > 1) && match(/^((?:\+_|\+)?)\s*:/)) {
|
|
1700
|
+
parserInput.forget();
|
|
1701
|
+
|
|
1702
|
+
=======
|
|
1703
|
+
while (match(/^((?:[\w-]+)|(?:@\{[\w-]+\}))/)); // !
|
|
1704
|
+
while (cutOutBlockComments());
|
|
1705
|
+
if ((name.length > 1) && match(/^\s*((?:\+_|\+)?)\s*:/)) {
|
|
1706
|
+
>>>>>>> origin/master:lib/less/parser.js
|
|
1707
|
+
// at last, we have the complete match now. move forward,
|
|
1708
|
+
// convert name particles to tree objects and return:
|
|
1709
|
+
if (name[0] === '') {
|
|
1710
|
+
name.shift();
|
|
1711
|
+
index.shift();
|
|
1712
|
+
}
|
|
1713
|
+
for (k = 0; k < name.length; k++) {
|
|
1714
|
+
s = name[k];
|
|
1715
|
+
name[k] = (s.charAt(0) !== '@') ?
|
|
1716
|
+
new(tree.Keyword)(s) :
|
|
1717
|
+
new(tree.Variable)('@' + s.slice(2, -1),
|
|
1718
|
+
index[k], env.currentFileInfo);
|
|
1719
|
+
}
|
|
1720
|
+
return name;
|
|
1721
|
+
}
|
|
1722
|
+
parserInput.restore();
|
|
1723
|
+
}
|
|
1724
|
+
}
|
|
1725
|
+
};
|
|
1726
|
+
|
|
1727
|
+
parser.getInput = getInput;
|
|
1728
|
+
parser.getLocation = parserInput.getLocation;
|
|
1729
|
+
|
|
1730
|
+
return parser;
|
|
1731
|
+
};
|
|
1732
|
+
Parser.serializeVars = function(vars) {
|
|
1733
|
+
var s = '';
|
|
1734
|
+
|
|
1735
|
+
for (var name in vars) {
|
|
1736
|
+
if (Object.hasOwnProperty.call(vars, name)) {
|
|
1737
|
+
var value = vars[name];
|
|
1738
|
+
s += ((name[0] === '@') ? '' : '@') + name +': '+ value +
|
|
1739
|
+
((('' + value).slice(-1) === ';') ? '' : ';');
|
|
1740
|
+
}
|
|
1741
|
+
}
|
|
1742
|
+
|
|
1743
|
+
return s;
|
|
1744
|
+
};
|
|
1745
|
+
|
|
1746
|
+
return Parser;
|
|
1747
|
+
};
|