vscode-css-languageservice 5.4.2 → 6.0.0

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.
Files changed (84) hide show
  1. package/CHANGELOG.md +5 -1
  2. package/lib/esm/cssLanguageService.d.ts +37 -37
  3. package/lib/esm/cssLanguageService.js +72 -75
  4. package/lib/esm/cssLanguageTypes.d.ts +238 -238
  5. package/lib/esm/cssLanguageTypes.js +42 -42
  6. package/lib/esm/data/webCustomData.js +21965 -21965
  7. package/lib/esm/languageFacts/builtinData.js +142 -142
  8. package/lib/esm/languageFacts/colors.js +469 -472
  9. package/lib/esm/languageFacts/dataManager.js +88 -92
  10. package/lib/esm/languageFacts/dataProvider.js +73 -79
  11. package/lib/esm/languageFacts/entry.js +137 -138
  12. package/lib/esm/languageFacts/facts.js +8 -8
  13. package/lib/esm/parser/cssErrors.js +48 -50
  14. package/lib/esm/parser/cssNodes.js +1502 -2019
  15. package/lib/esm/parser/cssParser.js +1534 -1563
  16. package/lib/esm/parser/cssScanner.js +592 -599
  17. package/lib/esm/parser/cssSymbolScope.js +311 -341
  18. package/lib/esm/parser/lessParser.js +714 -740
  19. package/lib/esm/parser/lessScanner.js +57 -78
  20. package/lib/esm/parser/scssErrors.js +18 -20
  21. package/lib/esm/parser/scssParser.js +796 -818
  22. package/lib/esm/parser/scssScanner.js +95 -116
  23. package/lib/esm/services/cssCodeActions.js +77 -81
  24. package/lib/esm/services/cssCompletion.js +1054 -1149
  25. package/lib/esm/services/cssFolding.js +190 -193
  26. package/lib/esm/services/cssFormatter.js +136 -136
  27. package/lib/esm/services/cssHover.js +148 -151
  28. package/lib/esm/services/cssNavigation.js +378 -470
  29. package/lib/esm/services/cssSelectionRange.js +47 -47
  30. package/lib/esm/services/cssValidation.js +41 -44
  31. package/lib/esm/services/lessCompletion.js +378 -397
  32. package/lib/esm/services/lint.js +518 -532
  33. package/lib/esm/services/lintRules.js +76 -83
  34. package/lib/esm/services/lintUtil.js +196 -205
  35. package/lib/esm/services/pathCompletion.js +157 -231
  36. package/lib/esm/services/scssCompletion.js +354 -378
  37. package/lib/esm/services/scssNavigation.js +82 -154
  38. package/lib/esm/services/selectorPrinting.js +492 -536
  39. package/lib/esm/utils/arrays.js +40 -46
  40. package/lib/esm/utils/objects.js +11 -11
  41. package/lib/esm/utils/resources.js +11 -24
  42. package/lib/esm/utils/strings.js +102 -104
  43. package/lib/umd/cssLanguageService.d.ts +37 -37
  44. package/lib/umd/cssLanguageService.js +99 -102
  45. package/lib/umd/cssLanguageTypes.d.ts +238 -238
  46. package/lib/umd/cssLanguageTypes.js +89 -88
  47. package/lib/umd/data/webCustomData.js +21978 -21978
  48. package/lib/umd/languageFacts/builtinData.js +154 -154
  49. package/lib/umd/languageFacts/colors.js +492 -495
  50. package/lib/umd/languageFacts/dataManager.js +101 -104
  51. package/lib/umd/languageFacts/dataProvider.js +86 -91
  52. package/lib/umd/languageFacts/entry.js +152 -153
  53. package/lib/umd/languageFacts/facts.js +29 -29
  54. package/lib/umd/parser/cssErrors.js +61 -62
  55. package/lib/umd/parser/cssNodes.js +1587 -2034
  56. package/lib/umd/parser/cssParser.js +1547 -1575
  57. package/lib/umd/parser/cssScanner.js +606 -611
  58. package/lib/umd/parser/cssSymbolScope.js +328 -353
  59. package/lib/umd/parser/lessParser.js +727 -752
  60. package/lib/umd/parser/lessScanner.js +70 -90
  61. package/lib/umd/parser/scssErrors.js +31 -32
  62. package/lib/umd/parser/scssParser.js +809 -830
  63. package/lib/umd/parser/scssScanner.js +108 -128
  64. package/lib/umd/services/cssCodeActions.js +90 -93
  65. package/lib/umd/services/cssCompletion.js +1067 -1161
  66. package/lib/umd/services/cssFolding.js +203 -206
  67. package/lib/umd/services/cssFormatter.js +150 -150
  68. package/lib/umd/services/cssHover.js +161 -163
  69. package/lib/umd/services/cssNavigation.js +391 -482
  70. package/lib/umd/services/cssSelectionRange.js +60 -60
  71. package/lib/umd/services/cssValidation.js +54 -56
  72. package/lib/umd/services/lessCompletion.js +391 -409
  73. package/lib/umd/services/lint.js +531 -544
  74. package/lib/umd/services/lintRules.js +91 -95
  75. package/lib/umd/services/lintUtil.js +210 -218
  76. package/lib/umd/services/pathCompletion.js +171 -244
  77. package/lib/umd/services/scssCompletion.js +367 -390
  78. package/lib/umd/services/scssNavigation.js +95 -166
  79. package/lib/umd/services/selectorPrinting.js +510 -550
  80. package/lib/umd/utils/arrays.js +55 -61
  81. package/lib/umd/utils/objects.js +25 -25
  82. package/lib/umd/utils/resources.js +26 -39
  83. package/lib/umd/utils/strings.js +120 -122
  84. package/package.json +10 -10
@@ -1,1575 +1,1547 @@
1
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
2
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
3
- if (ar || !(i in from)) {
4
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
5
- ar[i] = from[i];
6
- }
7
- }
8
- return to.concat(ar || Array.prototype.slice.call(from));
9
- };
10
- (function (factory) {
11
- if (typeof module === "object" && typeof module.exports === "object") {
12
- var v = factory(require, exports);
13
- if (v !== undefined) module.exports = v;
14
- }
15
- else if (typeof define === "function" && define.amd) {
16
- define(["require", "exports", "./cssScanner", "./cssNodes", "./cssErrors", "../languageFacts/facts", "../utils/objects"], factory);
17
- }
18
- })(function (require, exports) {
19
- /*---------------------------------------------------------------------------------------------
20
- * Copyright (c) Microsoft Corporation. All rights reserved.
21
- * Licensed under the MIT License. See License.txt in the project root for license information.
22
- *--------------------------------------------------------------------------------------------*/
23
- 'use strict';
24
- Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.Parser = void 0;
26
- var cssScanner_1 = require("./cssScanner");
27
- var nodes = require("./cssNodes");
28
- var cssErrors_1 = require("./cssErrors");
29
- var languageFacts = require("../languageFacts/facts");
30
- var objects_1 = require("../utils/objects");
31
- /// <summary>
32
- /// A parser for the css core specification. See for reference:
33
- /// https://www.w3.org/TR/CSS21/grammar.html
34
- /// http://www.w3.org/TR/CSS21/syndata.html#tokenization
35
- /// </summary>
36
- var Parser = /** @class */ (function () {
37
- function Parser(scnr) {
38
- if (scnr === void 0) { scnr = new cssScanner_1.Scanner(); }
39
- this.keyframeRegex = /^@(\-(webkit|ms|moz|o)\-)?keyframes$/i;
40
- this.scanner = scnr;
41
- this.token = { type: cssScanner_1.TokenType.EOF, offset: -1, len: 0, text: '' };
42
- this.prevToken = undefined;
43
- }
44
- Parser.prototype.peekIdent = function (text) {
45
- return cssScanner_1.TokenType.Ident === this.token.type && text.length === this.token.text.length && text === this.token.text.toLowerCase();
46
- };
47
- Parser.prototype.peekKeyword = function (text) {
48
- return cssScanner_1.TokenType.AtKeyword === this.token.type && text.length === this.token.text.length && text === this.token.text.toLowerCase();
49
- };
50
- Parser.prototype.peekDelim = function (text) {
51
- return cssScanner_1.TokenType.Delim === this.token.type && text === this.token.text;
52
- };
53
- Parser.prototype.peek = function (type) {
54
- return type === this.token.type;
55
- };
56
- Parser.prototype.peekOne = function () {
57
- var types = [];
58
- for (var _i = 0; _i < arguments.length; _i++) {
59
- types[_i] = arguments[_i];
60
- }
61
- return types.indexOf(this.token.type) !== -1;
62
- };
63
- Parser.prototype.peekRegExp = function (type, regEx) {
64
- if (type !== this.token.type) {
65
- return false;
66
- }
67
- return regEx.test(this.token.text);
68
- };
69
- Parser.prototype.hasWhitespace = function () {
70
- return !!this.prevToken && (this.prevToken.offset + this.prevToken.len !== this.token.offset);
71
- };
72
- Parser.prototype.consumeToken = function () {
73
- this.prevToken = this.token;
74
- this.token = this.scanner.scan();
75
- };
76
- Parser.prototype.acceptUnicodeRange = function () {
77
- var token = this.scanner.tryScanUnicode();
78
- if (token) {
79
- this.prevToken = token;
80
- this.token = this.scanner.scan();
81
- return true;
82
- }
83
- return false;
84
- };
85
- Parser.prototype.mark = function () {
86
- return {
87
- prev: this.prevToken,
88
- curr: this.token,
89
- pos: this.scanner.pos()
90
- };
91
- };
92
- Parser.prototype.restoreAtMark = function (mark) {
93
- this.prevToken = mark.prev;
94
- this.token = mark.curr;
95
- this.scanner.goBackTo(mark.pos);
96
- };
97
- Parser.prototype.try = function (func) {
98
- var pos = this.mark();
99
- var node = func();
100
- if (!node) {
101
- this.restoreAtMark(pos);
102
- return null;
103
- }
104
- return node;
105
- };
106
- Parser.prototype.acceptOneKeyword = function (keywords) {
107
- if (cssScanner_1.TokenType.AtKeyword === this.token.type) {
108
- for (var _i = 0, keywords_1 = keywords; _i < keywords_1.length; _i++) {
109
- var keyword = keywords_1[_i];
110
- if (keyword.length === this.token.text.length && keyword === this.token.text.toLowerCase()) {
111
- this.consumeToken();
112
- return true;
113
- }
114
- }
115
- }
116
- return false;
117
- };
118
- Parser.prototype.accept = function (type) {
119
- if (type === this.token.type) {
120
- this.consumeToken();
121
- return true;
122
- }
123
- return false;
124
- };
125
- Parser.prototype.acceptIdent = function (text) {
126
- if (this.peekIdent(text)) {
127
- this.consumeToken();
128
- return true;
129
- }
130
- return false;
131
- };
132
- Parser.prototype.acceptKeyword = function (text) {
133
- if (this.peekKeyword(text)) {
134
- this.consumeToken();
135
- return true;
136
- }
137
- return false;
138
- };
139
- Parser.prototype.acceptDelim = function (text) {
140
- if (this.peekDelim(text)) {
141
- this.consumeToken();
142
- return true;
143
- }
144
- return false;
145
- };
146
- Parser.prototype.acceptRegexp = function (regEx) {
147
- if (regEx.test(this.token.text)) {
148
- this.consumeToken();
149
- return true;
150
- }
151
- return false;
152
- };
153
- Parser.prototype._parseRegexp = function (regEx) {
154
- var node = this.createNode(nodes.NodeType.Identifier);
155
- do { } while (this.acceptRegexp(regEx));
156
- return this.finish(node);
157
- };
158
- Parser.prototype.acceptUnquotedString = function () {
159
- var pos = this.scanner.pos();
160
- this.scanner.goBackTo(this.token.offset);
161
- var unquoted = this.scanner.scanUnquotedString();
162
- if (unquoted) {
163
- this.token = unquoted;
164
- this.consumeToken();
165
- return true;
166
- }
167
- this.scanner.goBackTo(pos);
168
- return false;
169
- };
170
- Parser.prototype.resync = function (resyncTokens, resyncStopTokens) {
171
- while (true) {
172
- if (resyncTokens && resyncTokens.indexOf(this.token.type) !== -1) {
173
- this.consumeToken();
174
- return true;
175
- }
176
- else if (resyncStopTokens && resyncStopTokens.indexOf(this.token.type) !== -1) {
177
- return true;
178
- }
179
- else {
180
- if (this.token.type === cssScanner_1.TokenType.EOF) {
181
- return false;
182
- }
183
- this.token = this.scanner.scan();
184
- }
185
- }
186
- };
187
- Parser.prototype.createNode = function (nodeType) {
188
- return new nodes.Node(this.token.offset, this.token.len, nodeType);
189
- };
190
- Parser.prototype.create = function (ctor) {
191
- return new ctor(this.token.offset, this.token.len);
192
- };
193
- Parser.prototype.finish = function (node, error, resyncTokens, resyncStopTokens) {
194
- // parseNumeric misuses error for boolean flagging (however the real error mustn't be a false)
195
- // + nodelist offsets mustn't be modified, because there is a offset hack in rulesets for smartselection
196
- if (!(node instanceof nodes.Nodelist)) {
197
- if (error) {
198
- this.markError(node, error, resyncTokens, resyncStopTokens);
199
- }
200
- // set the node end position
201
- if (this.prevToken) {
202
- // length with more elements belonging together
203
- var prevEnd = this.prevToken.offset + this.prevToken.len;
204
- node.length = prevEnd > node.offset ? prevEnd - node.offset : 0; // offset is taken from current token, end from previous: Use 0 for empty nodes
205
- }
206
- }
207
- return node;
208
- };
209
- Parser.prototype.markError = function (node, error, resyncTokens, resyncStopTokens) {
210
- if (this.token !== this.lastErrorToken) { // do not report twice on the same token
211
- node.addIssue(new nodes.Marker(node, error, nodes.Level.Error, undefined, this.token.offset, this.token.len));
212
- this.lastErrorToken = this.token;
213
- }
214
- if (resyncTokens || resyncStopTokens) {
215
- this.resync(resyncTokens, resyncStopTokens);
216
- }
217
- };
218
- Parser.prototype.parseStylesheet = function (textDocument) {
219
- var versionId = textDocument.version;
220
- var text = textDocument.getText();
221
- var textProvider = function (offset, length) {
222
- if (textDocument.version !== versionId) {
223
- throw new Error('Underlying model has changed, AST is no longer valid');
224
- }
225
- return text.substr(offset, length);
226
- };
227
- return this.internalParse(text, this._parseStylesheet, textProvider);
228
- };
229
- Parser.prototype.internalParse = function (input, parseFunc, textProvider) {
230
- this.scanner.setSource(input);
231
- this.token = this.scanner.scan();
232
- var node = parseFunc.bind(this)();
233
- if (node) {
234
- if (textProvider) {
235
- node.textProvider = textProvider;
236
- }
237
- else {
238
- node.textProvider = function (offset, length) { return input.substr(offset, length); };
239
- }
240
- }
241
- return node;
242
- };
243
- Parser.prototype._parseStylesheet = function () {
244
- var node = this.create(nodes.Stylesheet);
245
- while (node.addChild(this._parseStylesheetStart())) {
246
- // Parse statements only valid at the beginning of stylesheets.
247
- }
248
- var inRecovery = false;
249
- do {
250
- var hasMatch = false;
251
- do {
252
- hasMatch = false;
253
- var statement = this._parseStylesheetStatement();
254
- if (statement) {
255
- node.addChild(statement);
256
- hasMatch = true;
257
- inRecovery = false;
258
- if (!this.peek(cssScanner_1.TokenType.EOF) && this._needsSemicolonAfter(statement) && !this.accept(cssScanner_1.TokenType.SemiColon)) {
259
- this.markError(node, cssErrors_1.ParseError.SemiColonExpected);
260
- }
261
- }
262
- while (this.accept(cssScanner_1.TokenType.SemiColon) || this.accept(cssScanner_1.TokenType.CDO) || this.accept(cssScanner_1.TokenType.CDC)) {
263
- // accept empty statements
264
- hasMatch = true;
265
- inRecovery = false;
266
- }
267
- } while (hasMatch);
268
- if (this.peek(cssScanner_1.TokenType.EOF)) {
269
- break;
270
- }
271
- if (!inRecovery) {
272
- if (this.peek(cssScanner_1.TokenType.AtKeyword)) {
273
- this.markError(node, cssErrors_1.ParseError.UnknownAtRule);
274
- }
275
- else {
276
- this.markError(node, cssErrors_1.ParseError.RuleOrSelectorExpected);
277
- }
278
- inRecovery = true;
279
- }
280
- this.consumeToken();
281
- } while (!this.peek(cssScanner_1.TokenType.EOF));
282
- return this.finish(node);
283
- };
284
- Parser.prototype._parseStylesheetStart = function () {
285
- return this._parseCharset();
286
- };
287
- Parser.prototype._parseStylesheetStatement = function (isNested) {
288
- if (isNested === void 0) { isNested = false; }
289
- if (this.peek(cssScanner_1.TokenType.AtKeyword)) {
290
- return this._parseStylesheetAtStatement(isNested);
291
- }
292
- return this._parseRuleset(isNested);
293
- };
294
- Parser.prototype._parseStylesheetAtStatement = function (isNested) {
295
- if (isNested === void 0) { isNested = false; }
296
- return this._parseImport()
297
- || this._parseMedia(isNested)
298
- || this._parsePage()
299
- || this._parseFontFace()
300
- || this._parseKeyframe()
301
- || this._parseSupports(isNested)
302
- || this._parseViewPort()
303
- || this._parseNamespace()
304
- || this._parseDocument()
305
- || this._parseUnknownAtRule();
306
- };
307
- Parser.prototype._tryParseRuleset = function (isNested) {
308
- var mark = this.mark();
309
- if (this._parseSelector(isNested)) {
310
- while (this.accept(cssScanner_1.TokenType.Comma) && this._parseSelector(isNested)) {
311
- // loop
312
- }
313
- if (this.accept(cssScanner_1.TokenType.CurlyL)) {
314
- this.restoreAtMark(mark);
315
- return this._parseRuleset(isNested);
316
- }
317
- }
318
- this.restoreAtMark(mark);
319
- return null;
320
- };
321
- Parser.prototype._parseRuleset = function (isNested) {
322
- if (isNested === void 0) { isNested = false; }
323
- var node = this.create(nodes.RuleSet);
324
- var selectors = node.getSelectors();
325
- if (!selectors.addChild(this._parseSelector(isNested))) {
326
- return null;
327
- }
328
- while (this.accept(cssScanner_1.TokenType.Comma)) {
329
- if (!selectors.addChild(this._parseSelector(isNested))) {
330
- return this.finish(node, cssErrors_1.ParseError.SelectorExpected);
331
- }
332
- }
333
- return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
334
- };
335
- Parser.prototype._parseRuleSetDeclarationAtStatement = function () {
336
- return this._parseUnknownAtRule();
337
- };
338
- Parser.prototype._parseRuleSetDeclaration = function () {
339
- // https://www.w3.org/TR/css-syntax-3/#consume-a-list-of-declarations
340
- if (this.peek(cssScanner_1.TokenType.AtKeyword)) {
341
- return this._parseRuleSetDeclarationAtStatement();
342
- }
343
- return this._parseDeclaration();
344
- };
345
- Parser.prototype._needsSemicolonAfter = function (node) {
346
- switch (node.type) {
347
- case nodes.NodeType.Keyframe:
348
- case nodes.NodeType.ViewPort:
349
- case nodes.NodeType.Media:
350
- case nodes.NodeType.Ruleset:
351
- case nodes.NodeType.Namespace:
352
- case nodes.NodeType.If:
353
- case nodes.NodeType.For:
354
- case nodes.NodeType.Each:
355
- case nodes.NodeType.While:
356
- case nodes.NodeType.MixinDeclaration:
357
- case nodes.NodeType.FunctionDeclaration:
358
- case nodes.NodeType.MixinContentDeclaration:
359
- return false;
360
- case nodes.NodeType.ExtendsReference:
361
- case nodes.NodeType.MixinContentReference:
362
- case nodes.NodeType.ReturnStatement:
363
- case nodes.NodeType.MediaQuery:
364
- case nodes.NodeType.Debug:
365
- case nodes.NodeType.Import:
366
- case nodes.NodeType.AtApplyRule:
367
- case nodes.NodeType.CustomPropertyDeclaration:
368
- return true;
369
- case nodes.NodeType.VariableDeclaration:
370
- return node.needsSemicolon;
371
- case nodes.NodeType.MixinReference:
372
- return !node.getContent();
373
- case nodes.NodeType.Declaration:
374
- return !node.getNestedProperties();
375
- }
376
- return false;
377
- };
378
- Parser.prototype._parseDeclarations = function (parseDeclaration) {
379
- var node = this.create(nodes.Declarations);
380
- if (!this.accept(cssScanner_1.TokenType.CurlyL)) {
381
- return null;
382
- }
383
- var decl = parseDeclaration();
384
- while (node.addChild(decl)) {
385
- if (this.peek(cssScanner_1.TokenType.CurlyR)) {
386
- break;
387
- }
388
- if (this._needsSemicolonAfter(decl) && !this.accept(cssScanner_1.TokenType.SemiColon)) {
389
- return this.finish(node, cssErrors_1.ParseError.SemiColonExpected, [cssScanner_1.TokenType.SemiColon, cssScanner_1.TokenType.CurlyR]);
390
- }
391
- // We accepted semicolon token. Link it to declaration.
392
- if (decl && this.prevToken && this.prevToken.type === cssScanner_1.TokenType.SemiColon) {
393
- decl.semicolonPosition = this.prevToken.offset;
394
- }
395
- while (this.accept(cssScanner_1.TokenType.SemiColon)) {
396
- // accept empty statements
397
- }
398
- decl = parseDeclaration();
399
- }
400
- if (!this.accept(cssScanner_1.TokenType.CurlyR)) {
401
- return this.finish(node, cssErrors_1.ParseError.RightCurlyExpected, [cssScanner_1.TokenType.CurlyR, cssScanner_1.TokenType.SemiColon]);
402
- }
403
- return this.finish(node);
404
- };
405
- Parser.prototype._parseBody = function (node, parseDeclaration) {
406
- if (!node.setDeclarations(this._parseDeclarations(parseDeclaration))) {
407
- return this.finish(node, cssErrors_1.ParseError.LeftCurlyExpected, [cssScanner_1.TokenType.CurlyR, cssScanner_1.TokenType.SemiColon]);
408
- }
409
- return this.finish(node);
410
- };
411
- Parser.prototype._parseSelector = function (isNested) {
412
- var node = this.create(nodes.Selector);
413
- var hasContent = false;
414
- if (isNested) {
415
- // nested selectors can start with a combinator
416
- hasContent = node.addChild(this._parseCombinator());
417
- }
418
- while (node.addChild(this._parseSimpleSelector())) {
419
- hasContent = true;
420
- node.addChild(this._parseCombinator()); // optional
421
- }
422
- return hasContent ? this.finish(node) : null;
423
- };
424
- Parser.prototype._parseDeclaration = function (stopTokens) {
425
- var custonProperty = this._tryParseCustomPropertyDeclaration(stopTokens);
426
- if (custonProperty) {
427
- return custonProperty;
428
- }
429
- var node = this.create(nodes.Declaration);
430
- if (!node.setProperty(this._parseProperty())) {
431
- return null;
432
- }
433
- if (!this.accept(cssScanner_1.TokenType.Colon)) {
434
- return this.finish(node, cssErrors_1.ParseError.ColonExpected, [cssScanner_1.TokenType.Colon], stopTokens || [cssScanner_1.TokenType.SemiColon]);
435
- }
436
- if (this.prevToken) {
437
- node.colonPosition = this.prevToken.offset;
438
- }
439
- if (!node.setValue(this._parseExpr())) {
440
- return this.finish(node, cssErrors_1.ParseError.PropertyValueExpected);
441
- }
442
- node.addChild(this._parsePrio());
443
- if (this.peek(cssScanner_1.TokenType.SemiColon)) {
444
- node.semicolonPosition = this.token.offset; // not part of the declaration, but useful information for code assist
445
- }
446
- return this.finish(node);
447
- };
448
- Parser.prototype._tryParseCustomPropertyDeclaration = function (stopTokens) {
449
- if (!this.peekRegExp(cssScanner_1.TokenType.Ident, /^--/)) {
450
- return null;
451
- }
452
- var node = this.create(nodes.CustomPropertyDeclaration);
453
- if (!node.setProperty(this._parseProperty())) {
454
- return null;
455
- }
456
- if (!this.accept(cssScanner_1.TokenType.Colon)) {
457
- return this.finish(node, cssErrors_1.ParseError.ColonExpected, [cssScanner_1.TokenType.Colon]);
458
- }
459
- if (this.prevToken) {
460
- node.colonPosition = this.prevToken.offset;
461
- }
462
- var mark = this.mark();
463
- if (this.peek(cssScanner_1.TokenType.CurlyL)) {
464
- // try to parse it as nested declaration
465
- var propertySet = this.create(nodes.CustomPropertySet);
466
- var declarations = this._parseDeclarations(this._parseRuleSetDeclaration.bind(this));
467
- if (propertySet.setDeclarations(declarations) && !declarations.isErroneous(true)) {
468
- propertySet.addChild(this._parsePrio());
469
- if (this.peek(cssScanner_1.TokenType.SemiColon)) {
470
- this.finish(propertySet);
471
- node.setPropertySet(propertySet);
472
- node.semicolonPosition = this.token.offset; // not part of the declaration, but useful information for code assist
473
- return this.finish(node);
474
- }
475
- }
476
- this.restoreAtMark(mark);
477
- }
478
- // try to parse as expression
479
- var expression = this._parseExpr();
480
- if (expression && !expression.isErroneous(true)) {
481
- this._parsePrio();
482
- if (this.peekOne.apply(this, __spreadArray(__spreadArray([], (stopTokens || []), false), [cssScanner_1.TokenType.SemiColon, cssScanner_1.TokenType.EOF], false))) {
483
- node.setValue(expression);
484
- if (this.peek(cssScanner_1.TokenType.SemiColon)) {
485
- node.semicolonPosition = this.token.offset; // not part of the declaration, but useful information for code assist
486
- }
487
- return this.finish(node);
488
- }
489
- }
490
- this.restoreAtMark(mark);
491
- node.addChild(this._parseCustomPropertyValue(stopTokens));
492
- node.addChild(this._parsePrio());
493
- if ((0, objects_1.isDefined)(node.colonPosition) && this.token.offset === node.colonPosition + 1) {
494
- return this.finish(node, cssErrors_1.ParseError.PropertyValueExpected);
495
- }
496
- return this.finish(node);
497
- };
498
- /**
499
- * Parse custom property values.
500
- *
501
- * Based on https://www.w3.org/TR/css-variables/#syntax
502
- *
503
- * This code is somewhat unusual, as the allowed syntax is incredibly broad,
504
- * parsing almost any sequence of tokens, save for a small set of exceptions.
505
- * Unbalanced delimitors, invalid tokens, and declaration
506
- * terminators like semicolons and !important directives (when not inside
507
- * of delimitors).
508
- */
509
- Parser.prototype._parseCustomPropertyValue = function (stopTokens) {
510
- var _this = this;
511
- if (stopTokens === void 0) { stopTokens = [cssScanner_1.TokenType.CurlyR]; }
512
- var node = this.create(nodes.Node);
513
- var isTopLevel = function () { return curlyDepth === 0 && parensDepth === 0 && bracketsDepth === 0; };
514
- var onStopToken = function () { return stopTokens.indexOf(_this.token.type) !== -1; };
515
- var curlyDepth = 0;
516
- var parensDepth = 0;
517
- var bracketsDepth = 0;
518
- done: while (true) {
519
- switch (this.token.type) {
520
- case cssScanner_1.TokenType.SemiColon:
521
- // A semicolon only ends things if we're not inside a delimitor.
522
- if (isTopLevel()) {
523
- break done;
524
- }
525
- break;
526
- case cssScanner_1.TokenType.Exclamation:
527
- // An exclamation ends the value if we're not inside delims.
528
- if (isTopLevel()) {
529
- break done;
530
- }
531
- break;
532
- case cssScanner_1.TokenType.CurlyL:
533
- curlyDepth++;
534
- break;
535
- case cssScanner_1.TokenType.CurlyR:
536
- curlyDepth--;
537
- if (curlyDepth < 0) {
538
- // The property value has been terminated without a semicolon, and
539
- // this is the last declaration in the ruleset.
540
- if (onStopToken() && parensDepth === 0 && bracketsDepth === 0) {
541
- break done;
542
- }
543
- return this.finish(node, cssErrors_1.ParseError.LeftCurlyExpected);
544
- }
545
- break;
546
- case cssScanner_1.TokenType.ParenthesisL:
547
- parensDepth++;
548
- break;
549
- case cssScanner_1.TokenType.ParenthesisR:
550
- parensDepth--;
551
- if (parensDepth < 0) {
552
- if (onStopToken() && bracketsDepth === 0 && curlyDepth === 0) {
553
- break done;
554
- }
555
- return this.finish(node, cssErrors_1.ParseError.LeftParenthesisExpected);
556
- }
557
- break;
558
- case cssScanner_1.TokenType.BracketL:
559
- bracketsDepth++;
560
- break;
561
- case cssScanner_1.TokenType.BracketR:
562
- bracketsDepth--;
563
- if (bracketsDepth < 0) {
564
- return this.finish(node, cssErrors_1.ParseError.LeftSquareBracketExpected);
565
- }
566
- break;
567
- case cssScanner_1.TokenType.BadString: // fall through
568
- break done;
569
- case cssScanner_1.TokenType.EOF:
570
- // We shouldn't have reached the end of input, something is
571
- // unterminated.
572
- var error = cssErrors_1.ParseError.RightCurlyExpected;
573
- if (bracketsDepth > 0) {
574
- error = cssErrors_1.ParseError.RightSquareBracketExpected;
575
- }
576
- else if (parensDepth > 0) {
577
- error = cssErrors_1.ParseError.RightParenthesisExpected;
578
- }
579
- return this.finish(node, error);
580
- }
581
- this.consumeToken();
582
- }
583
- return this.finish(node);
584
- };
585
- Parser.prototype._tryToParseDeclaration = function (stopTokens) {
586
- var mark = this.mark();
587
- if (this._parseProperty() && this.accept(cssScanner_1.TokenType.Colon)) {
588
- // looks like a declaration, go ahead
589
- this.restoreAtMark(mark);
590
- return this._parseDeclaration(stopTokens);
591
- }
592
- this.restoreAtMark(mark);
593
- return null;
594
- };
595
- Parser.prototype._parseProperty = function () {
596
- var node = this.create(nodes.Property);
597
- var mark = this.mark();
598
- if (this.acceptDelim('*') || this.acceptDelim('_')) {
599
- // support for IE 5.x, 6 and 7 star hack: see http://en.wikipedia.org/wiki/CSS_filter#Star_hack
600
- if (this.hasWhitespace()) {
601
- this.restoreAtMark(mark);
602
- return null;
603
- }
604
- }
605
- if (node.setIdentifier(this._parsePropertyIdentifier())) {
606
- return this.finish(node);
607
- }
608
- return null;
609
- };
610
- Parser.prototype._parsePropertyIdentifier = function () {
611
- return this._parseIdent();
612
- };
613
- Parser.prototype._parseCharset = function () {
614
- if (!this.peek(cssScanner_1.TokenType.Charset)) {
615
- return null;
616
- }
617
- var node = this.create(nodes.Node);
618
- this.consumeToken(); // charset
619
- if (!this.accept(cssScanner_1.TokenType.String)) {
620
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
621
- }
622
- if (!this.accept(cssScanner_1.TokenType.SemiColon)) {
623
- return this.finish(node, cssErrors_1.ParseError.SemiColonExpected);
624
- }
625
- return this.finish(node);
626
- };
627
- Parser.prototype._parseImport = function () {
628
- if (!this.peekKeyword('@import')) {
629
- return null;
630
- }
631
- var node = this.create(nodes.Import);
632
- this.consumeToken(); // @import
633
- if (!node.addChild(this._parseURILiteral()) && !node.addChild(this._parseStringLiteral())) {
634
- return this.finish(node, cssErrors_1.ParseError.URIOrStringExpected);
635
- }
636
- if (!this.peek(cssScanner_1.TokenType.SemiColon) && !this.peek(cssScanner_1.TokenType.EOF)) {
637
- node.setMedialist(this._parseMediaQueryList());
638
- }
639
- return this.finish(node);
640
- };
641
- Parser.prototype._parseNamespace = function () {
642
- // http://www.w3.org/TR/css3-namespace/
643
- // namespace : NAMESPACE_SYM S* [IDENT S*]? [STRING|URI] S* ';' S*
644
- if (!this.peekKeyword('@namespace')) {
645
- return null;
646
- }
647
- var node = this.create(nodes.Namespace);
648
- this.consumeToken(); // @namespace
649
- if (!node.addChild(this._parseURILiteral())) { // url literal also starts with ident
650
- node.addChild(this._parseIdent()); // optional prefix
651
- if (!node.addChild(this._parseURILiteral()) && !node.addChild(this._parseStringLiteral())) {
652
- return this.finish(node, cssErrors_1.ParseError.URIExpected, [cssScanner_1.TokenType.SemiColon]);
653
- }
654
- }
655
- if (!this.accept(cssScanner_1.TokenType.SemiColon)) {
656
- return this.finish(node, cssErrors_1.ParseError.SemiColonExpected);
657
- }
658
- return this.finish(node);
659
- };
660
- Parser.prototype._parseFontFace = function () {
661
- if (!this.peekKeyword('@font-face')) {
662
- return null;
663
- }
664
- var node = this.create(nodes.FontFace);
665
- this.consumeToken(); // @font-face
666
- return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
667
- };
668
- Parser.prototype._parseViewPort = function () {
669
- if (!this.peekKeyword('@-ms-viewport') &&
670
- !this.peekKeyword('@-o-viewport') &&
671
- !this.peekKeyword('@viewport')) {
672
- return null;
673
- }
674
- var node = this.create(nodes.ViewPort);
675
- this.consumeToken(); // @-ms-viewport
676
- return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
677
- };
678
- Parser.prototype._parseKeyframe = function () {
679
- if (!this.peekRegExp(cssScanner_1.TokenType.AtKeyword, this.keyframeRegex)) {
680
- return null;
681
- }
682
- var node = this.create(nodes.Keyframe);
683
- var atNode = this.create(nodes.Node);
684
- this.consumeToken(); // atkeyword
685
- node.setKeyword(this.finish(atNode));
686
- if (atNode.matches('@-ms-keyframes')) { // -ms-keyframes never existed
687
- this.markError(atNode, cssErrors_1.ParseError.UnknownKeyword);
688
- }
689
- if (!node.setIdentifier(this._parseKeyframeIdent())) {
690
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected, [cssScanner_1.TokenType.CurlyR]);
691
- }
692
- return this._parseBody(node, this._parseKeyframeSelector.bind(this));
693
- };
694
- Parser.prototype._parseKeyframeIdent = function () {
695
- return this._parseIdent([nodes.ReferenceType.Keyframe]);
696
- };
697
- Parser.prototype._parseKeyframeSelector = function () {
698
- var node = this.create(nodes.KeyframeSelector);
699
- if (!node.addChild(this._parseIdent()) && !this.accept(cssScanner_1.TokenType.Percentage)) {
700
- return null;
701
- }
702
- while (this.accept(cssScanner_1.TokenType.Comma)) {
703
- if (!node.addChild(this._parseIdent()) && !this.accept(cssScanner_1.TokenType.Percentage)) {
704
- return this.finish(node, cssErrors_1.ParseError.PercentageExpected);
705
- }
706
- }
707
- return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
708
- };
709
- Parser.prototype._tryParseKeyframeSelector = function () {
710
- var node = this.create(nodes.KeyframeSelector);
711
- var pos = this.mark();
712
- if (!node.addChild(this._parseIdent()) && !this.accept(cssScanner_1.TokenType.Percentage)) {
713
- return null;
714
- }
715
- while (this.accept(cssScanner_1.TokenType.Comma)) {
716
- if (!node.addChild(this._parseIdent()) && !this.accept(cssScanner_1.TokenType.Percentage)) {
717
- this.restoreAtMark(pos);
718
- return null;
719
- }
720
- }
721
- if (!this.peek(cssScanner_1.TokenType.CurlyL)) {
722
- this.restoreAtMark(pos);
723
- return null;
724
- }
725
- return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
726
- };
727
- Parser.prototype._parseSupports = function (isNested) {
728
- if (isNested === void 0) { isNested = false; }
729
- // SUPPORTS_SYM S* supports_condition '{' S* ruleset* '}' S*
730
- if (!this.peekKeyword('@supports')) {
731
- return null;
732
- }
733
- var node = this.create(nodes.Supports);
734
- this.consumeToken(); // @supports
735
- node.addChild(this._parseSupportsCondition());
736
- return this._parseBody(node, this._parseSupportsDeclaration.bind(this, isNested));
737
- };
738
- Parser.prototype._parseSupportsDeclaration = function (isNested) {
739
- if (isNested === void 0) { isNested = false; }
740
- if (isNested) {
741
- // if nested, the body can contain rulesets, but also declarations
742
- return this._tryParseRuleset(true)
743
- || this._tryToParseDeclaration()
744
- || this._parseStylesheetStatement(true);
745
- }
746
- return this._parseStylesheetStatement(false);
747
- };
748
- Parser.prototype._parseSupportsCondition = function () {
749
- // supports_condition : supports_negation | supports_conjunction | supports_disjunction | supports_condition_in_parens ;
750
- // supports_condition_in_parens: ( '(' S* supports_condition S* ')' ) | supports_declaration_condition | general_enclosed ;
751
- // supports_negation: NOT S+ supports_condition_in_parens ;
752
- // supports_conjunction: supports_condition_in_parens ( S+ AND S+ supports_condition_in_parens )+;
753
- // supports_disjunction: supports_condition_in_parens ( S+ OR S+ supports_condition_in_parens )+;
754
- // supports_declaration_condition: '(' S* declaration ')';
755
- // general_enclosed: ( FUNCTION | '(' ) ( any | unused )* ')' ;
756
- var node = this.create(nodes.SupportsCondition);
757
- if (this.acceptIdent('not')) {
758
- node.addChild(this._parseSupportsConditionInParens());
759
- }
760
- else {
761
- node.addChild(this._parseSupportsConditionInParens());
762
- if (this.peekRegExp(cssScanner_1.TokenType.Ident, /^(and|or)$/i)) {
763
- var text = this.token.text.toLowerCase();
764
- while (this.acceptIdent(text)) {
765
- node.addChild(this._parseSupportsConditionInParens());
766
- }
767
- }
768
- }
769
- return this.finish(node);
770
- };
771
- Parser.prototype._parseSupportsConditionInParens = function () {
772
- var node = this.create(nodes.SupportsCondition);
773
- if (this.accept(cssScanner_1.TokenType.ParenthesisL)) {
774
- if (this.prevToken) {
775
- node.lParent = this.prevToken.offset;
776
- }
777
- if (!node.addChild(this._tryToParseDeclaration([cssScanner_1.TokenType.ParenthesisR]))) {
778
- if (!this._parseSupportsCondition()) {
779
- return this.finish(node, cssErrors_1.ParseError.ConditionExpected);
780
- }
781
- }
782
- if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
783
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected, [cssScanner_1.TokenType.ParenthesisR], []);
784
- }
785
- if (this.prevToken) {
786
- node.rParent = this.prevToken.offset;
787
- }
788
- return this.finish(node);
789
- }
790
- else if (this.peek(cssScanner_1.TokenType.Ident)) {
791
- var pos = this.mark();
792
- this.consumeToken();
793
- if (!this.hasWhitespace() && this.accept(cssScanner_1.TokenType.ParenthesisL)) {
794
- var openParentCount = 1;
795
- while (this.token.type !== cssScanner_1.TokenType.EOF && openParentCount !== 0) {
796
- if (this.token.type === cssScanner_1.TokenType.ParenthesisL) {
797
- openParentCount++;
798
- }
799
- else if (this.token.type === cssScanner_1.TokenType.ParenthesisR) {
800
- openParentCount--;
801
- }
802
- this.consumeToken();
803
- }
804
- return this.finish(node);
805
- }
806
- else {
807
- this.restoreAtMark(pos);
808
- }
809
- }
810
- return this.finish(node, cssErrors_1.ParseError.LeftParenthesisExpected, [], [cssScanner_1.TokenType.ParenthesisL]);
811
- };
812
- Parser.prototype._parseMediaDeclaration = function (isNested) {
813
- if (isNested === void 0) { isNested = false; }
814
- if (isNested) {
815
- // if nested, the body can contain rulesets, but also declarations
816
- return this._tryParseRuleset(true)
817
- || this._tryToParseDeclaration()
818
- || this._parseStylesheetStatement(true);
819
- }
820
- return this._parseStylesheetStatement(false);
821
- };
822
- Parser.prototype._parseMedia = function (isNested) {
823
- if (isNested === void 0) { isNested = false; }
824
- // MEDIA_SYM S* media_query_list '{' S* ruleset* '}' S*
825
- // media_query_list : S* [media_query [ ',' S* media_query ]* ]?
826
- if (!this.peekKeyword('@media')) {
827
- return null;
828
- }
829
- var node = this.create(nodes.Media);
830
- this.consumeToken(); // @media
831
- if (!node.addChild(this._parseMediaQueryList())) {
832
- return this.finish(node, cssErrors_1.ParseError.MediaQueryExpected);
833
- }
834
- return this._parseBody(node, this._parseMediaDeclaration.bind(this, isNested));
835
- };
836
- Parser.prototype._parseMediaQueryList = function () {
837
- var node = this.create(nodes.Medialist);
838
- if (!node.addChild(this._parseMediaQuery())) {
839
- return this.finish(node, cssErrors_1.ParseError.MediaQueryExpected);
840
- }
841
- while (this.accept(cssScanner_1.TokenType.Comma)) {
842
- if (!node.addChild(this._parseMediaQuery())) {
843
- return this.finish(node, cssErrors_1.ParseError.MediaQueryExpected);
844
- }
845
- }
846
- return this.finish(node);
847
- };
848
- Parser.prototype._parseMediaQuery = function () {
849
- // <media-query> = <media-condition> | [ not | only ]? <media-type> [ and <media-condition-without-or> ]?
850
- var node = this.create(nodes.MediaQuery);
851
- var pos = this.mark();
852
- this.acceptIdent('not');
853
- if (!this.peek(cssScanner_1.TokenType.ParenthesisL)) {
854
- if (this.acceptIdent('only')) {
855
- // optional
856
- }
857
- if (!node.addChild(this._parseIdent())) {
858
- return null;
859
- }
860
- if (this.acceptIdent('and')) {
861
- node.addChild(this._parseMediaCondition());
862
- }
863
- }
864
- else {
865
- this.restoreAtMark(pos); // 'not' is part of the MediaCondition
866
- node.addChild(this._parseMediaCondition());
867
- }
868
- return this.finish(node);
869
- };
870
- Parser.prototype._parseRatio = function () {
871
- var pos = this.mark();
872
- var node = this.create(nodes.RatioValue);
873
- if (!this._parseNumeric()) {
874
- return null;
875
- }
876
- if (!this.acceptDelim('/')) {
877
- this.restoreAtMark(pos);
878
- return null;
879
- }
880
- if (!this._parseNumeric()) {
881
- return this.finish(node, cssErrors_1.ParseError.NumberExpected);
882
- }
883
- return this.finish(node);
884
- };
885
- Parser.prototype._parseMediaCondition = function () {
886
- // <media-condition> = <media-not> | <media-and> | <media-or> | <media-in-parens>
887
- // <media-not> = not <media-in-parens>
888
- // <media-and> = <media-in-parens> [ and <media-in-parens> ]+
889
- // <media-or> = <media-in-parens> [ or <media-in-parens> ]+
890
- // <media-in-parens> = ( <media-condition> ) | <media-feature> | <general-enclosed>
891
- var node = this.create(nodes.MediaCondition);
892
- this.acceptIdent('not');
893
- var parseExpression = true;
894
- while (parseExpression) {
895
- if (!this.accept(cssScanner_1.TokenType.ParenthesisL)) {
896
- return this.finish(node, cssErrors_1.ParseError.LeftParenthesisExpected, [], [cssScanner_1.TokenType.CurlyL]);
897
- }
898
- if (this.peek(cssScanner_1.TokenType.ParenthesisL) || this.peekIdent('not')) {
899
- // <media-condition>
900
- node.addChild(this._parseMediaCondition());
901
- }
902
- else {
903
- node.addChild(this._parseMediaFeature());
904
- }
905
- // not yet implemented: general enclosed
906
- if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
907
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected, [], [cssScanner_1.TokenType.CurlyL]);
908
- }
909
- parseExpression = this.acceptIdent('and') || this.acceptIdent('or');
910
- }
911
- return this.finish(node);
912
- };
913
- Parser.prototype._parseMediaFeature = function () {
914
- var _this = this;
915
- var resyncStopToken = [cssScanner_1.TokenType.ParenthesisR];
916
- var node = this.create(nodes.MediaFeature);
917
- // <media-feature> = ( [ <mf-plain> | <mf-boolean> | <mf-range> ] )
918
- // <mf-plain> = <mf-name> : <mf-value>
919
- // <mf-boolean> = <mf-name>
920
- // <mf-range> = <mf-name> [ '<' | '>' ]? '='? <mf-value> | <mf-value> [ '<' | '>' ]? '='? <mf-name> | <mf-value> '<' '='? <mf-name> '<' '='? <mf-value> | <mf-value> '>' '='? <mf-name> '>' '='? <mf-value>
921
- var parseRangeOperator = function () {
922
- if (_this.acceptDelim('<') || _this.acceptDelim('>')) {
923
- if (!_this.hasWhitespace()) {
924
- _this.acceptDelim('=');
925
- }
926
- return true;
927
- }
928
- else if (_this.acceptDelim('=')) {
929
- return true;
930
- }
931
- return false;
932
- };
933
- if (node.addChild(this._parseMediaFeatureName())) {
934
- if (this.accept(cssScanner_1.TokenType.Colon)) {
935
- if (!node.addChild(this._parseMediaFeatureValue())) {
936
- return this.finish(node, cssErrors_1.ParseError.TermExpected, [], resyncStopToken);
937
- }
938
- }
939
- else if (parseRangeOperator()) {
940
- if (!node.addChild(this._parseMediaFeatureValue())) {
941
- return this.finish(node, cssErrors_1.ParseError.TermExpected, [], resyncStopToken);
942
- }
943
- if (parseRangeOperator()) {
944
- if (!node.addChild(this._parseMediaFeatureValue())) {
945
- return this.finish(node, cssErrors_1.ParseError.TermExpected, [], resyncStopToken);
946
- }
947
- }
948
- }
949
- else {
950
- // <mf-boolean> = <mf-name>
951
- }
952
- }
953
- else if (node.addChild(this._parseMediaFeatureValue())) {
954
- if (!parseRangeOperator()) {
955
- return this.finish(node, cssErrors_1.ParseError.OperatorExpected, [], resyncStopToken);
956
- }
957
- if (!node.addChild(this._parseMediaFeatureName())) {
958
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected, [], resyncStopToken);
959
- }
960
- if (parseRangeOperator()) {
961
- if (!node.addChild(this._parseMediaFeatureValue())) {
962
- return this.finish(node, cssErrors_1.ParseError.TermExpected, [], resyncStopToken);
963
- }
964
- }
965
- }
966
- else {
967
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected, [], resyncStopToken);
968
- }
969
- return this.finish(node);
970
- };
971
- Parser.prototype._parseMediaFeatureName = function () {
972
- return this._parseIdent();
973
- };
974
- Parser.prototype._parseMediaFeatureValue = function () {
975
- return this._parseRatio() || this._parseTermExpression();
976
- };
977
- Parser.prototype._parseMedium = function () {
978
- var node = this.create(nodes.Node);
979
- if (node.addChild(this._parseIdent())) {
980
- return this.finish(node);
981
- }
982
- else {
983
- return null;
984
- }
985
- };
986
- Parser.prototype._parsePageDeclaration = function () {
987
- return this._parsePageMarginBox() || this._parseRuleSetDeclaration();
988
- };
989
- Parser.prototype._parsePage = function () {
990
- // http://www.w3.org/TR/css3-page/
991
- // page_rule : PAGE_SYM S* page_selector_list '{' S* page_body '}' S*
992
- // page_body : /* Can be empty */ declaration? [ ';' S* page_body ]? | page_margin_box page_body
993
- if (!this.peekKeyword('@page')) {
994
- return null;
995
- }
996
- var node = this.create(nodes.Page);
997
- this.consumeToken();
998
- if (node.addChild(this._parsePageSelector())) {
999
- while (this.accept(cssScanner_1.TokenType.Comma)) {
1000
- if (!node.addChild(this._parsePageSelector())) {
1001
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
1002
- }
1003
- }
1004
- }
1005
- return this._parseBody(node, this._parsePageDeclaration.bind(this));
1006
- };
1007
- Parser.prototype._parsePageMarginBox = function () {
1008
- // page_margin_box : margin_sym S* '{' S* declaration? [ ';' S* declaration? ]* '}' S*
1009
- if (!this.peek(cssScanner_1.TokenType.AtKeyword)) {
1010
- return null;
1011
- }
1012
- var node = this.create(nodes.PageBoxMarginBox);
1013
- if (!this.acceptOneKeyword(languageFacts.pageBoxDirectives)) {
1014
- this.markError(node, cssErrors_1.ParseError.UnknownAtRule, [], [cssScanner_1.TokenType.CurlyL]);
1015
- }
1016
- return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
1017
- };
1018
- Parser.prototype._parsePageSelector = function () {
1019
- // page_selector : pseudo_page+ | IDENT pseudo_page*
1020
- // pseudo_page : ':' [ "left" | "right" | "first" | "blank" ];
1021
- if (!this.peek(cssScanner_1.TokenType.Ident) && !this.peek(cssScanner_1.TokenType.Colon)) {
1022
- return null;
1023
- }
1024
- var node = this.create(nodes.Node);
1025
- node.addChild(this._parseIdent()); // optional ident
1026
- if (this.accept(cssScanner_1.TokenType.Colon)) {
1027
- if (!node.addChild(this._parseIdent())) { // optional ident
1028
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
1029
- }
1030
- }
1031
- return this.finish(node);
1032
- };
1033
- Parser.prototype._parseDocument = function () {
1034
- // -moz-document is experimental but has been pushed to css4
1035
- if (!this.peekKeyword('@-moz-document')) {
1036
- return null;
1037
- }
1038
- var node = this.create(nodes.Document);
1039
- this.consumeToken(); // @-moz-document
1040
- this.resync([], [cssScanner_1.TokenType.CurlyL]); // ignore all the rules
1041
- return this._parseBody(node, this._parseStylesheetStatement.bind(this));
1042
- };
1043
- // https://www.w3.org/TR/css-syntax-3/#consume-an-at-rule
1044
- Parser.prototype._parseUnknownAtRule = function () {
1045
- if (!this.peek(cssScanner_1.TokenType.AtKeyword)) {
1046
- return null;
1047
- }
1048
- var node = this.create(nodes.UnknownAtRule);
1049
- node.addChild(this._parseUnknownAtRuleName());
1050
- var isTopLevel = function () { return curlyDepth === 0 && parensDepth === 0 && bracketsDepth === 0; };
1051
- var curlyLCount = 0;
1052
- var curlyDepth = 0;
1053
- var parensDepth = 0;
1054
- var bracketsDepth = 0;
1055
- done: while (true) {
1056
- switch (this.token.type) {
1057
- case cssScanner_1.TokenType.SemiColon:
1058
- if (isTopLevel()) {
1059
- break done;
1060
- }
1061
- break;
1062
- case cssScanner_1.TokenType.EOF:
1063
- if (curlyDepth > 0) {
1064
- return this.finish(node, cssErrors_1.ParseError.RightCurlyExpected);
1065
- }
1066
- else if (bracketsDepth > 0) {
1067
- return this.finish(node, cssErrors_1.ParseError.RightSquareBracketExpected);
1068
- }
1069
- else if (parensDepth > 0) {
1070
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
1071
- }
1072
- else {
1073
- return this.finish(node);
1074
- }
1075
- case cssScanner_1.TokenType.CurlyL:
1076
- curlyLCount++;
1077
- curlyDepth++;
1078
- break;
1079
- case cssScanner_1.TokenType.CurlyR:
1080
- curlyDepth--;
1081
- // End of at-rule, consume CurlyR and return node
1082
- if (curlyLCount > 0 && curlyDepth === 0) {
1083
- this.consumeToken();
1084
- if (bracketsDepth > 0) {
1085
- return this.finish(node, cssErrors_1.ParseError.RightSquareBracketExpected);
1086
- }
1087
- else if (parensDepth > 0) {
1088
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
1089
- }
1090
- break done;
1091
- }
1092
- if (curlyDepth < 0) {
1093
- // The property value has been terminated without a semicolon, and
1094
- // this is the last declaration in the ruleset.
1095
- if (parensDepth === 0 && bracketsDepth === 0) {
1096
- break done;
1097
- }
1098
- return this.finish(node, cssErrors_1.ParseError.LeftCurlyExpected);
1099
- }
1100
- break;
1101
- case cssScanner_1.TokenType.ParenthesisL:
1102
- parensDepth++;
1103
- break;
1104
- case cssScanner_1.TokenType.ParenthesisR:
1105
- parensDepth--;
1106
- if (parensDepth < 0) {
1107
- return this.finish(node, cssErrors_1.ParseError.LeftParenthesisExpected);
1108
- }
1109
- break;
1110
- case cssScanner_1.TokenType.BracketL:
1111
- bracketsDepth++;
1112
- break;
1113
- case cssScanner_1.TokenType.BracketR:
1114
- bracketsDepth--;
1115
- if (bracketsDepth < 0) {
1116
- return this.finish(node, cssErrors_1.ParseError.LeftSquareBracketExpected);
1117
- }
1118
- break;
1119
- }
1120
- this.consumeToken();
1121
- }
1122
- return node;
1123
- };
1124
- Parser.prototype._parseUnknownAtRuleName = function () {
1125
- var node = this.create(nodes.Node);
1126
- if (this.accept(cssScanner_1.TokenType.AtKeyword)) {
1127
- return this.finish(node);
1128
- }
1129
- return node;
1130
- };
1131
- Parser.prototype._parseOperator = function () {
1132
- // these are operators for binary expressions
1133
- if (this.peekDelim('/') ||
1134
- this.peekDelim('*') ||
1135
- this.peekDelim('+') ||
1136
- this.peekDelim('-') ||
1137
- this.peek(cssScanner_1.TokenType.Dashmatch) ||
1138
- this.peek(cssScanner_1.TokenType.Includes) ||
1139
- this.peek(cssScanner_1.TokenType.SubstringOperator) ||
1140
- this.peek(cssScanner_1.TokenType.PrefixOperator) ||
1141
- this.peek(cssScanner_1.TokenType.SuffixOperator) ||
1142
- this.peekDelim('=')) { // doesn't stick to the standard here
1143
- var node = this.createNode(nodes.NodeType.Operator);
1144
- this.consumeToken();
1145
- return this.finish(node);
1146
- }
1147
- else {
1148
- return null;
1149
- }
1150
- };
1151
- Parser.prototype._parseUnaryOperator = function () {
1152
- if (!this.peekDelim('+') && !this.peekDelim('-')) {
1153
- return null;
1154
- }
1155
- var node = this.create(nodes.Node);
1156
- this.consumeToken();
1157
- return this.finish(node);
1158
- };
1159
- Parser.prototype._parseCombinator = function () {
1160
- if (this.peekDelim('>')) {
1161
- var node = this.create(nodes.Node);
1162
- this.consumeToken();
1163
- var mark = this.mark();
1164
- if (!this.hasWhitespace() && this.acceptDelim('>')) {
1165
- if (!this.hasWhitespace() && this.acceptDelim('>')) {
1166
- node.type = nodes.NodeType.SelectorCombinatorShadowPiercingDescendant;
1167
- return this.finish(node);
1168
- }
1169
- this.restoreAtMark(mark);
1170
- }
1171
- node.type = nodes.NodeType.SelectorCombinatorParent;
1172
- return this.finish(node);
1173
- }
1174
- else if (this.peekDelim('+')) {
1175
- var node = this.create(nodes.Node);
1176
- this.consumeToken();
1177
- node.type = nodes.NodeType.SelectorCombinatorSibling;
1178
- return this.finish(node);
1179
- }
1180
- else if (this.peekDelim('~')) {
1181
- var node = this.create(nodes.Node);
1182
- this.consumeToken();
1183
- node.type = nodes.NodeType.SelectorCombinatorAllSiblings;
1184
- return this.finish(node);
1185
- }
1186
- else if (this.peekDelim('/')) {
1187
- var node = this.create(nodes.Node);
1188
- this.consumeToken();
1189
- var mark = this.mark();
1190
- if (!this.hasWhitespace() && this.acceptIdent('deep') && !this.hasWhitespace() && this.acceptDelim('/')) {
1191
- node.type = nodes.NodeType.SelectorCombinatorShadowPiercingDescendant;
1192
- return this.finish(node);
1193
- }
1194
- this.restoreAtMark(mark);
1195
- }
1196
- return null;
1197
- };
1198
- Parser.prototype._parseSimpleSelector = function () {
1199
- // simple_selector
1200
- // : element_name [ HASH | class | attrib | pseudo ]* | [ HASH | class | attrib | pseudo ]+ ;
1201
- var node = this.create(nodes.SimpleSelector);
1202
- var c = 0;
1203
- if (node.addChild(this._parseElementName())) {
1204
- c++;
1205
- }
1206
- while ((c === 0 || !this.hasWhitespace()) && node.addChild(this._parseSimpleSelectorBody())) {
1207
- c++;
1208
- }
1209
- return c > 0 ? this.finish(node) : null;
1210
- };
1211
- Parser.prototype._parseSimpleSelectorBody = function () {
1212
- return this._parsePseudo() || this._parseHash() || this._parseClass() || this._parseAttrib();
1213
- };
1214
- Parser.prototype._parseSelectorIdent = function () {
1215
- return this._parseIdent();
1216
- };
1217
- Parser.prototype._parseHash = function () {
1218
- if (!this.peek(cssScanner_1.TokenType.Hash) && !this.peekDelim('#')) {
1219
- return null;
1220
- }
1221
- var node = this.createNode(nodes.NodeType.IdentifierSelector);
1222
- if (this.acceptDelim('#')) {
1223
- if (this.hasWhitespace() || !node.addChild(this._parseSelectorIdent())) {
1224
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
1225
- }
1226
- }
1227
- else {
1228
- this.consumeToken(); // TokenType.Hash
1229
- }
1230
- return this.finish(node);
1231
- };
1232
- Parser.prototype._parseClass = function () {
1233
- // class: '.' IDENT ;
1234
- if (!this.peekDelim('.')) {
1235
- return null;
1236
- }
1237
- var node = this.createNode(nodes.NodeType.ClassSelector);
1238
- this.consumeToken(); // '.'
1239
- if (this.hasWhitespace() || !node.addChild(this._parseSelectorIdent())) {
1240
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
1241
- }
1242
- return this.finish(node);
1243
- };
1244
- Parser.prototype._parseElementName = function () {
1245
- // element_name: (ns? '|')? IDENT | '*';
1246
- var pos = this.mark();
1247
- var node = this.createNode(nodes.NodeType.ElementNameSelector);
1248
- node.addChild(this._parseNamespacePrefix());
1249
- if (!node.addChild(this._parseSelectorIdent()) && !this.acceptDelim('*')) {
1250
- this.restoreAtMark(pos);
1251
- return null;
1252
- }
1253
- return this.finish(node);
1254
- };
1255
- Parser.prototype._parseNamespacePrefix = function () {
1256
- var pos = this.mark();
1257
- var node = this.createNode(nodes.NodeType.NamespacePrefix);
1258
- if (!node.addChild(this._parseIdent()) && !this.acceptDelim('*')) {
1259
- // ns is optional
1260
- }
1261
- if (!this.acceptDelim('|')) {
1262
- this.restoreAtMark(pos);
1263
- return null;
1264
- }
1265
- return this.finish(node);
1266
- };
1267
- Parser.prototype._parseAttrib = function () {
1268
- // attrib : '[' S* IDENT S* [ [ '=' | INCLUDES | DASHMATCH ] S* [ IDENT | STRING ] S* ]? ']'
1269
- if (!this.peek(cssScanner_1.TokenType.BracketL)) {
1270
- return null;
1271
- }
1272
- var node = this.create(nodes.AttributeSelector);
1273
- this.consumeToken(); // BracketL
1274
- // Optional attrib namespace
1275
- node.setNamespacePrefix(this._parseNamespacePrefix());
1276
- if (!node.setIdentifier(this._parseIdent())) {
1277
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
1278
- }
1279
- if (node.setOperator(this._parseOperator())) {
1280
- node.setValue(this._parseBinaryExpr());
1281
- this.acceptIdent('i'); // case insensitive matching
1282
- this.acceptIdent('s'); // case sensitive matching
1283
- }
1284
- if (!this.accept(cssScanner_1.TokenType.BracketR)) {
1285
- return this.finish(node, cssErrors_1.ParseError.RightSquareBracketExpected);
1286
- }
1287
- return this.finish(node);
1288
- };
1289
- Parser.prototype._parsePseudo = function () {
1290
- var _this = this;
1291
- // pseudo: ':' [ IDENT | FUNCTION S* [IDENT S*]? ')' ]
1292
- var node = this._tryParsePseudoIdentifier();
1293
- if (node) {
1294
- if (!this.hasWhitespace() && this.accept(cssScanner_1.TokenType.ParenthesisL)) {
1295
- var tryAsSelector = function () {
1296
- var selectors = _this.create(nodes.Node);
1297
- if (!selectors.addChild(_this._parseSelector(false))) {
1298
- return null;
1299
- }
1300
- while (_this.accept(cssScanner_1.TokenType.Comma) && selectors.addChild(_this._parseSelector(false))) {
1301
- // loop
1302
- }
1303
- if (_this.peek(cssScanner_1.TokenType.ParenthesisR)) {
1304
- return _this.finish(selectors);
1305
- }
1306
- return null;
1307
- };
1308
- node.addChild(this.try(tryAsSelector) || this._parseBinaryExpr());
1309
- if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
1310
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
1311
- }
1312
- }
1313
- return this.finish(node);
1314
- }
1315
- return null;
1316
- };
1317
- Parser.prototype._tryParsePseudoIdentifier = function () {
1318
- if (!this.peek(cssScanner_1.TokenType.Colon)) {
1319
- return null;
1320
- }
1321
- var pos = this.mark();
1322
- var node = this.createNode(nodes.NodeType.PseudoSelector);
1323
- this.consumeToken(); // Colon
1324
- if (this.hasWhitespace()) {
1325
- this.restoreAtMark(pos);
1326
- return null;
1327
- }
1328
- // optional, support ::
1329
- this.accept(cssScanner_1.TokenType.Colon);
1330
- if (this.hasWhitespace() || !node.addChild(this._parseIdent())) {
1331
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
1332
- }
1333
- return this.finish(node);
1334
- };
1335
- Parser.prototype._tryParsePrio = function () {
1336
- var mark = this.mark();
1337
- var prio = this._parsePrio();
1338
- if (prio) {
1339
- return prio;
1340
- }
1341
- this.restoreAtMark(mark);
1342
- return null;
1343
- };
1344
- Parser.prototype._parsePrio = function () {
1345
- if (!this.peek(cssScanner_1.TokenType.Exclamation)) {
1346
- return null;
1347
- }
1348
- var node = this.createNode(nodes.NodeType.Prio);
1349
- if (this.accept(cssScanner_1.TokenType.Exclamation) && this.acceptIdent('important')) {
1350
- return this.finish(node);
1351
- }
1352
- return null;
1353
- };
1354
- Parser.prototype._parseExpr = function (stopOnComma) {
1355
- if (stopOnComma === void 0) { stopOnComma = false; }
1356
- var node = this.create(nodes.Expression);
1357
- if (!node.addChild(this._parseBinaryExpr())) {
1358
- return null;
1359
- }
1360
- while (true) {
1361
- if (this.peek(cssScanner_1.TokenType.Comma)) { // optional
1362
- if (stopOnComma) {
1363
- return this.finish(node);
1364
- }
1365
- this.consumeToken();
1366
- }
1367
- if (!node.addChild(this._parseBinaryExpr())) {
1368
- break;
1369
- }
1370
- }
1371
- return this.finish(node);
1372
- };
1373
- Parser.prototype._parseUnicodeRange = function () {
1374
- if (!this.peekIdent('u')) {
1375
- return null;
1376
- }
1377
- var node = this.create(nodes.UnicodeRange);
1378
- if (!this.acceptUnicodeRange()) {
1379
- return null;
1380
- }
1381
- return this.finish(node);
1382
- };
1383
- Parser.prototype._parseNamedLine = function () {
1384
- // https://www.w3.org/TR/css-grid-1/#named-lines
1385
- if (!this.peek(cssScanner_1.TokenType.BracketL)) {
1386
- return null;
1387
- }
1388
- var node = this.createNode(nodes.NodeType.GridLine);
1389
- this.consumeToken();
1390
- while (node.addChild(this._parseIdent())) {
1391
- // repeat
1392
- }
1393
- if (!this.accept(cssScanner_1.TokenType.BracketR)) {
1394
- return this.finish(node, cssErrors_1.ParseError.RightSquareBracketExpected);
1395
- }
1396
- return this.finish(node);
1397
- };
1398
- Parser.prototype._parseBinaryExpr = function (preparsedLeft, preparsedOper) {
1399
- var node = this.create(nodes.BinaryExpression);
1400
- if (!node.setLeft((preparsedLeft || this._parseTerm()))) {
1401
- return null;
1402
- }
1403
- if (!node.setOperator(preparsedOper || this._parseOperator())) {
1404
- return this.finish(node);
1405
- }
1406
- if (!node.setRight(this._parseTerm())) {
1407
- return this.finish(node, cssErrors_1.ParseError.TermExpected);
1408
- }
1409
- // things needed for multiple binary expressions
1410
- node = this.finish(node);
1411
- var operator = this._parseOperator();
1412
- if (operator) {
1413
- node = this._parseBinaryExpr(node, operator);
1414
- }
1415
- return this.finish(node);
1416
- };
1417
- Parser.prototype._parseTerm = function () {
1418
- var node = this.create(nodes.Term);
1419
- node.setOperator(this._parseUnaryOperator()); // optional
1420
- if (node.setExpression(this._parseTermExpression())) {
1421
- return this.finish(node);
1422
- }
1423
- return null;
1424
- };
1425
- Parser.prototype._parseTermExpression = function () {
1426
- return this._parseURILiteral() || // url before function
1427
- this._parseUnicodeRange() ||
1428
- this._parseFunction() || // function before ident
1429
- this._parseIdent() ||
1430
- this._parseStringLiteral() ||
1431
- this._parseNumeric() ||
1432
- this._parseHexColor() ||
1433
- this._parseOperation() ||
1434
- this._parseNamedLine();
1435
- };
1436
- Parser.prototype._parseOperation = function () {
1437
- if (!this.peek(cssScanner_1.TokenType.ParenthesisL)) {
1438
- return null;
1439
- }
1440
- var node = this.create(nodes.Node);
1441
- this.consumeToken(); // ParenthesisL
1442
- node.addChild(this._parseExpr());
1443
- if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
1444
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
1445
- }
1446
- return this.finish(node);
1447
- };
1448
- Parser.prototype._parseNumeric = function () {
1449
- if (this.peek(cssScanner_1.TokenType.Num) ||
1450
- this.peek(cssScanner_1.TokenType.Percentage) ||
1451
- this.peek(cssScanner_1.TokenType.Resolution) ||
1452
- this.peek(cssScanner_1.TokenType.Length) ||
1453
- this.peek(cssScanner_1.TokenType.EMS) ||
1454
- this.peek(cssScanner_1.TokenType.EXS) ||
1455
- this.peek(cssScanner_1.TokenType.Angle) ||
1456
- this.peek(cssScanner_1.TokenType.Time) ||
1457
- this.peek(cssScanner_1.TokenType.Dimension) ||
1458
- this.peek(cssScanner_1.TokenType.Freq)) {
1459
- var node = this.create(nodes.NumericValue);
1460
- this.consumeToken();
1461
- return this.finish(node);
1462
- }
1463
- return null;
1464
- };
1465
- Parser.prototype._parseStringLiteral = function () {
1466
- if (!this.peek(cssScanner_1.TokenType.String) && !this.peek(cssScanner_1.TokenType.BadString)) {
1467
- return null;
1468
- }
1469
- var node = this.createNode(nodes.NodeType.StringLiteral);
1470
- this.consumeToken();
1471
- return this.finish(node);
1472
- };
1473
- Parser.prototype._parseURILiteral = function () {
1474
- if (!this.peekRegExp(cssScanner_1.TokenType.Ident, /^url(-prefix)?$/i)) {
1475
- return null;
1476
- }
1477
- var pos = this.mark();
1478
- var node = this.createNode(nodes.NodeType.URILiteral);
1479
- this.accept(cssScanner_1.TokenType.Ident);
1480
- if (this.hasWhitespace() || !this.peek(cssScanner_1.TokenType.ParenthesisL)) {
1481
- this.restoreAtMark(pos);
1482
- return null;
1483
- }
1484
- this.scanner.inURL = true;
1485
- this.consumeToken(); // consume ()
1486
- node.addChild(this._parseURLArgument()); // argument is optional
1487
- this.scanner.inURL = false;
1488
- if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
1489
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
1490
- }
1491
- return this.finish(node);
1492
- };
1493
- Parser.prototype._parseURLArgument = function () {
1494
- var node = this.create(nodes.Node);
1495
- if (!this.accept(cssScanner_1.TokenType.String) && !this.accept(cssScanner_1.TokenType.BadString) && !this.acceptUnquotedString()) {
1496
- return null;
1497
- }
1498
- return this.finish(node);
1499
- };
1500
- Parser.prototype._parseIdent = function (referenceTypes) {
1501
- if (!this.peek(cssScanner_1.TokenType.Ident)) {
1502
- return null;
1503
- }
1504
- var node = this.create(nodes.Identifier);
1505
- if (referenceTypes) {
1506
- node.referenceTypes = referenceTypes;
1507
- }
1508
- node.isCustomProperty = this.peekRegExp(cssScanner_1.TokenType.Ident, /^--/);
1509
- this.consumeToken();
1510
- return this.finish(node);
1511
- };
1512
- Parser.prototype._parseFunction = function () {
1513
- var pos = this.mark();
1514
- var node = this.create(nodes.Function);
1515
- if (!node.setIdentifier(this._parseFunctionIdentifier())) {
1516
- return null;
1517
- }
1518
- if (this.hasWhitespace() || !this.accept(cssScanner_1.TokenType.ParenthesisL)) {
1519
- this.restoreAtMark(pos);
1520
- return null;
1521
- }
1522
- if (node.getArguments().addChild(this._parseFunctionArgument())) {
1523
- while (this.accept(cssScanner_1.TokenType.Comma)) {
1524
- if (this.peek(cssScanner_1.TokenType.ParenthesisR)) {
1525
- break;
1526
- }
1527
- if (!node.getArguments().addChild(this._parseFunctionArgument())) {
1528
- this.markError(node, cssErrors_1.ParseError.ExpressionExpected);
1529
- }
1530
- }
1531
- }
1532
- if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
1533
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
1534
- }
1535
- return this.finish(node);
1536
- };
1537
- Parser.prototype._parseFunctionIdentifier = function () {
1538
- if (!this.peek(cssScanner_1.TokenType.Ident)) {
1539
- return null;
1540
- }
1541
- var node = this.create(nodes.Identifier);
1542
- node.referenceTypes = [nodes.ReferenceType.Function];
1543
- if (this.acceptIdent('progid')) {
1544
- // support for IE7 specific filters: 'progid:DXImageTransform.Microsoft.MotionBlur(strength=13, direction=310)'
1545
- if (this.accept(cssScanner_1.TokenType.Colon)) {
1546
- while (this.accept(cssScanner_1.TokenType.Ident) && this.acceptDelim('.')) {
1547
- // loop
1548
- }
1549
- }
1550
- return this.finish(node);
1551
- }
1552
- this.consumeToken();
1553
- return this.finish(node);
1554
- };
1555
- Parser.prototype._parseFunctionArgument = function () {
1556
- var node = this.create(nodes.FunctionArgument);
1557
- if (node.setValue(this._parseExpr(true))) {
1558
- return this.finish(node);
1559
- }
1560
- return null;
1561
- };
1562
- Parser.prototype._parseHexColor = function () {
1563
- if (this.peekRegExp(cssScanner_1.TokenType.Hash, /^#([A-Fa-f0-9]{3}|[A-Fa-f0-9]{4}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$/g)) {
1564
- var node = this.create(nodes.HexColorValue);
1565
- this.consumeToken();
1566
- return this.finish(node);
1567
- }
1568
- else {
1569
- return null;
1570
- }
1571
- };
1572
- return Parser;
1573
- }());
1574
- exports.Parser = Parser;
1575
- });
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports", "./cssScanner", "./cssNodes", "./cssErrors", "../languageFacts/facts", "../utils/objects"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ /*---------------------------------------------------------------------------------------------
11
+ * Copyright (c) Microsoft Corporation. All rights reserved.
12
+ * Licensed under the MIT License. See License.txt in the project root for license information.
13
+ *--------------------------------------------------------------------------------------------*/
14
+ 'use strict';
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.Parser = void 0;
17
+ const cssScanner_1 = require("./cssScanner");
18
+ const nodes = require("./cssNodes");
19
+ const cssErrors_1 = require("./cssErrors");
20
+ const languageFacts = require("../languageFacts/facts");
21
+ const objects_1 = require("../utils/objects");
22
+ /// <summary>
23
+ /// A parser for the css core specification. See for reference:
24
+ /// https://www.w3.org/TR/CSS21/grammar.html
25
+ /// http://www.w3.org/TR/CSS21/syndata.html#tokenization
26
+ /// </summary>
27
+ class Parser {
28
+ constructor(scnr = new cssScanner_1.Scanner()) {
29
+ this.keyframeRegex = /^@(\-(webkit|ms|moz|o)\-)?keyframes$/i;
30
+ this.scanner = scnr;
31
+ this.token = { type: cssScanner_1.TokenType.EOF, offset: -1, len: 0, text: '' };
32
+ this.prevToken = undefined;
33
+ }
34
+ peekIdent(text) {
35
+ return cssScanner_1.TokenType.Ident === this.token.type && text.length === this.token.text.length && text === this.token.text.toLowerCase();
36
+ }
37
+ peekKeyword(text) {
38
+ return cssScanner_1.TokenType.AtKeyword === this.token.type && text.length === this.token.text.length && text === this.token.text.toLowerCase();
39
+ }
40
+ peekDelim(text) {
41
+ return cssScanner_1.TokenType.Delim === this.token.type && text === this.token.text;
42
+ }
43
+ peek(type) {
44
+ return type === this.token.type;
45
+ }
46
+ peekOne(...types) {
47
+ return types.indexOf(this.token.type) !== -1;
48
+ }
49
+ peekRegExp(type, regEx) {
50
+ if (type !== this.token.type) {
51
+ return false;
52
+ }
53
+ return regEx.test(this.token.text);
54
+ }
55
+ hasWhitespace() {
56
+ return !!this.prevToken && (this.prevToken.offset + this.prevToken.len !== this.token.offset);
57
+ }
58
+ consumeToken() {
59
+ this.prevToken = this.token;
60
+ this.token = this.scanner.scan();
61
+ }
62
+ acceptUnicodeRange() {
63
+ const token = this.scanner.tryScanUnicode();
64
+ if (token) {
65
+ this.prevToken = token;
66
+ this.token = this.scanner.scan();
67
+ return true;
68
+ }
69
+ return false;
70
+ }
71
+ mark() {
72
+ return {
73
+ prev: this.prevToken,
74
+ curr: this.token,
75
+ pos: this.scanner.pos()
76
+ };
77
+ }
78
+ restoreAtMark(mark) {
79
+ this.prevToken = mark.prev;
80
+ this.token = mark.curr;
81
+ this.scanner.goBackTo(mark.pos);
82
+ }
83
+ try(func) {
84
+ const pos = this.mark();
85
+ const node = func();
86
+ if (!node) {
87
+ this.restoreAtMark(pos);
88
+ return null;
89
+ }
90
+ return node;
91
+ }
92
+ acceptOneKeyword(keywords) {
93
+ if (cssScanner_1.TokenType.AtKeyword === this.token.type) {
94
+ for (const keyword of keywords) {
95
+ if (keyword.length === this.token.text.length && keyword === this.token.text.toLowerCase()) {
96
+ this.consumeToken();
97
+ return true;
98
+ }
99
+ }
100
+ }
101
+ return false;
102
+ }
103
+ accept(type) {
104
+ if (type === this.token.type) {
105
+ this.consumeToken();
106
+ return true;
107
+ }
108
+ return false;
109
+ }
110
+ acceptIdent(text) {
111
+ if (this.peekIdent(text)) {
112
+ this.consumeToken();
113
+ return true;
114
+ }
115
+ return false;
116
+ }
117
+ acceptKeyword(text) {
118
+ if (this.peekKeyword(text)) {
119
+ this.consumeToken();
120
+ return true;
121
+ }
122
+ return false;
123
+ }
124
+ acceptDelim(text) {
125
+ if (this.peekDelim(text)) {
126
+ this.consumeToken();
127
+ return true;
128
+ }
129
+ return false;
130
+ }
131
+ acceptRegexp(regEx) {
132
+ if (regEx.test(this.token.text)) {
133
+ this.consumeToken();
134
+ return true;
135
+ }
136
+ return false;
137
+ }
138
+ _parseRegexp(regEx) {
139
+ let node = this.createNode(nodes.NodeType.Identifier);
140
+ do { } while (this.acceptRegexp(regEx));
141
+ return this.finish(node);
142
+ }
143
+ acceptUnquotedString() {
144
+ const pos = this.scanner.pos();
145
+ this.scanner.goBackTo(this.token.offset);
146
+ const unquoted = this.scanner.scanUnquotedString();
147
+ if (unquoted) {
148
+ this.token = unquoted;
149
+ this.consumeToken();
150
+ return true;
151
+ }
152
+ this.scanner.goBackTo(pos);
153
+ return false;
154
+ }
155
+ resync(resyncTokens, resyncStopTokens) {
156
+ while (true) {
157
+ if (resyncTokens && resyncTokens.indexOf(this.token.type) !== -1) {
158
+ this.consumeToken();
159
+ return true;
160
+ }
161
+ else if (resyncStopTokens && resyncStopTokens.indexOf(this.token.type) !== -1) {
162
+ return true;
163
+ }
164
+ else {
165
+ if (this.token.type === cssScanner_1.TokenType.EOF) {
166
+ return false;
167
+ }
168
+ this.token = this.scanner.scan();
169
+ }
170
+ }
171
+ }
172
+ createNode(nodeType) {
173
+ return new nodes.Node(this.token.offset, this.token.len, nodeType);
174
+ }
175
+ create(ctor) {
176
+ return new ctor(this.token.offset, this.token.len);
177
+ }
178
+ finish(node, error, resyncTokens, resyncStopTokens) {
179
+ // parseNumeric misuses error for boolean flagging (however the real error mustn't be a false)
180
+ // + nodelist offsets mustn't be modified, because there is a offset hack in rulesets for smartselection
181
+ if (!(node instanceof nodes.Nodelist)) {
182
+ if (error) {
183
+ this.markError(node, error, resyncTokens, resyncStopTokens);
184
+ }
185
+ // set the node end position
186
+ if (this.prevToken) {
187
+ // length with more elements belonging together
188
+ const prevEnd = this.prevToken.offset + this.prevToken.len;
189
+ node.length = prevEnd > node.offset ? prevEnd - node.offset : 0; // offset is taken from current token, end from previous: Use 0 for empty nodes
190
+ }
191
+ }
192
+ return node;
193
+ }
194
+ markError(node, error, resyncTokens, resyncStopTokens) {
195
+ if (this.token !== this.lastErrorToken) { // do not report twice on the same token
196
+ node.addIssue(new nodes.Marker(node, error, nodes.Level.Error, undefined, this.token.offset, this.token.len));
197
+ this.lastErrorToken = this.token;
198
+ }
199
+ if (resyncTokens || resyncStopTokens) {
200
+ this.resync(resyncTokens, resyncStopTokens);
201
+ }
202
+ }
203
+ parseStylesheet(textDocument) {
204
+ const versionId = textDocument.version;
205
+ const text = textDocument.getText();
206
+ const textProvider = (offset, length) => {
207
+ if (textDocument.version !== versionId) {
208
+ throw new Error('Underlying model has changed, AST is no longer valid');
209
+ }
210
+ return text.substr(offset, length);
211
+ };
212
+ return this.internalParse(text, this._parseStylesheet, textProvider);
213
+ }
214
+ internalParse(input, parseFunc, textProvider) {
215
+ this.scanner.setSource(input);
216
+ this.token = this.scanner.scan();
217
+ const node = parseFunc.bind(this)();
218
+ if (node) {
219
+ if (textProvider) {
220
+ node.textProvider = textProvider;
221
+ }
222
+ else {
223
+ node.textProvider = (offset, length) => { return input.substr(offset, length); };
224
+ }
225
+ }
226
+ return node;
227
+ }
228
+ _parseStylesheet() {
229
+ const node = this.create(nodes.Stylesheet);
230
+ while (node.addChild(this._parseStylesheetStart())) {
231
+ // Parse statements only valid at the beginning of stylesheets.
232
+ }
233
+ let inRecovery = false;
234
+ do {
235
+ let hasMatch = false;
236
+ do {
237
+ hasMatch = false;
238
+ const statement = this._parseStylesheetStatement();
239
+ if (statement) {
240
+ node.addChild(statement);
241
+ hasMatch = true;
242
+ inRecovery = false;
243
+ if (!this.peek(cssScanner_1.TokenType.EOF) && this._needsSemicolonAfter(statement) && !this.accept(cssScanner_1.TokenType.SemiColon)) {
244
+ this.markError(node, cssErrors_1.ParseError.SemiColonExpected);
245
+ }
246
+ }
247
+ while (this.accept(cssScanner_1.TokenType.SemiColon) || this.accept(cssScanner_1.TokenType.CDO) || this.accept(cssScanner_1.TokenType.CDC)) {
248
+ // accept empty statements
249
+ hasMatch = true;
250
+ inRecovery = false;
251
+ }
252
+ } while (hasMatch);
253
+ if (this.peek(cssScanner_1.TokenType.EOF)) {
254
+ break;
255
+ }
256
+ if (!inRecovery) {
257
+ if (this.peek(cssScanner_1.TokenType.AtKeyword)) {
258
+ this.markError(node, cssErrors_1.ParseError.UnknownAtRule);
259
+ }
260
+ else {
261
+ this.markError(node, cssErrors_1.ParseError.RuleOrSelectorExpected);
262
+ }
263
+ inRecovery = true;
264
+ }
265
+ this.consumeToken();
266
+ } while (!this.peek(cssScanner_1.TokenType.EOF));
267
+ return this.finish(node);
268
+ }
269
+ _parseStylesheetStart() {
270
+ return this._parseCharset();
271
+ }
272
+ _parseStylesheetStatement(isNested = false) {
273
+ if (this.peek(cssScanner_1.TokenType.AtKeyword)) {
274
+ return this._parseStylesheetAtStatement(isNested);
275
+ }
276
+ return this._parseRuleset(isNested);
277
+ }
278
+ _parseStylesheetAtStatement(isNested = false) {
279
+ return this._parseImport()
280
+ || this._parseMedia(isNested)
281
+ || this._parsePage()
282
+ || this._parseFontFace()
283
+ || this._parseKeyframe()
284
+ || this._parseSupports(isNested)
285
+ || this._parseViewPort()
286
+ || this._parseNamespace()
287
+ || this._parseDocument()
288
+ || this._parseUnknownAtRule();
289
+ }
290
+ _tryParseRuleset(isNested) {
291
+ const mark = this.mark();
292
+ if (this._parseSelector(isNested)) {
293
+ while (this.accept(cssScanner_1.TokenType.Comma) && this._parseSelector(isNested)) {
294
+ // loop
295
+ }
296
+ if (this.accept(cssScanner_1.TokenType.CurlyL)) {
297
+ this.restoreAtMark(mark);
298
+ return this._parseRuleset(isNested);
299
+ }
300
+ }
301
+ this.restoreAtMark(mark);
302
+ return null;
303
+ }
304
+ _parseRuleset(isNested = false) {
305
+ const node = this.create(nodes.RuleSet);
306
+ const selectors = node.getSelectors();
307
+ if (!selectors.addChild(this._parseSelector(isNested))) {
308
+ return null;
309
+ }
310
+ while (this.accept(cssScanner_1.TokenType.Comma)) {
311
+ if (!selectors.addChild(this._parseSelector(isNested))) {
312
+ return this.finish(node, cssErrors_1.ParseError.SelectorExpected);
313
+ }
314
+ }
315
+ return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
316
+ }
317
+ _parseRuleSetDeclarationAtStatement() {
318
+ return this._parseUnknownAtRule();
319
+ }
320
+ _parseRuleSetDeclaration() {
321
+ // https://www.w3.org/TR/css-syntax-3/#consume-a-list-of-declarations
322
+ if (this.peek(cssScanner_1.TokenType.AtKeyword)) {
323
+ return this._parseRuleSetDeclarationAtStatement();
324
+ }
325
+ return this._parseDeclaration();
326
+ }
327
+ _needsSemicolonAfter(node) {
328
+ switch (node.type) {
329
+ case nodes.NodeType.Keyframe:
330
+ case nodes.NodeType.ViewPort:
331
+ case nodes.NodeType.Media:
332
+ case nodes.NodeType.Ruleset:
333
+ case nodes.NodeType.Namespace:
334
+ case nodes.NodeType.If:
335
+ case nodes.NodeType.For:
336
+ case nodes.NodeType.Each:
337
+ case nodes.NodeType.While:
338
+ case nodes.NodeType.MixinDeclaration:
339
+ case nodes.NodeType.FunctionDeclaration:
340
+ case nodes.NodeType.MixinContentDeclaration:
341
+ return false;
342
+ case nodes.NodeType.ExtendsReference:
343
+ case nodes.NodeType.MixinContentReference:
344
+ case nodes.NodeType.ReturnStatement:
345
+ case nodes.NodeType.MediaQuery:
346
+ case nodes.NodeType.Debug:
347
+ case nodes.NodeType.Import:
348
+ case nodes.NodeType.AtApplyRule:
349
+ case nodes.NodeType.CustomPropertyDeclaration:
350
+ return true;
351
+ case nodes.NodeType.VariableDeclaration:
352
+ return node.needsSemicolon;
353
+ case nodes.NodeType.MixinReference:
354
+ return !node.getContent();
355
+ case nodes.NodeType.Declaration:
356
+ return !node.getNestedProperties();
357
+ }
358
+ return false;
359
+ }
360
+ _parseDeclarations(parseDeclaration) {
361
+ const node = this.create(nodes.Declarations);
362
+ if (!this.accept(cssScanner_1.TokenType.CurlyL)) {
363
+ return null;
364
+ }
365
+ let decl = parseDeclaration();
366
+ while (node.addChild(decl)) {
367
+ if (this.peek(cssScanner_1.TokenType.CurlyR)) {
368
+ break;
369
+ }
370
+ if (this._needsSemicolonAfter(decl) && !this.accept(cssScanner_1.TokenType.SemiColon)) {
371
+ return this.finish(node, cssErrors_1.ParseError.SemiColonExpected, [cssScanner_1.TokenType.SemiColon, cssScanner_1.TokenType.CurlyR]);
372
+ }
373
+ // We accepted semicolon token. Link it to declaration.
374
+ if (decl && this.prevToken && this.prevToken.type === cssScanner_1.TokenType.SemiColon) {
375
+ decl.semicolonPosition = this.prevToken.offset;
376
+ }
377
+ while (this.accept(cssScanner_1.TokenType.SemiColon)) {
378
+ // accept empty statements
379
+ }
380
+ decl = parseDeclaration();
381
+ }
382
+ if (!this.accept(cssScanner_1.TokenType.CurlyR)) {
383
+ return this.finish(node, cssErrors_1.ParseError.RightCurlyExpected, [cssScanner_1.TokenType.CurlyR, cssScanner_1.TokenType.SemiColon]);
384
+ }
385
+ return this.finish(node);
386
+ }
387
+ _parseBody(node, parseDeclaration) {
388
+ if (!node.setDeclarations(this._parseDeclarations(parseDeclaration))) {
389
+ return this.finish(node, cssErrors_1.ParseError.LeftCurlyExpected, [cssScanner_1.TokenType.CurlyR, cssScanner_1.TokenType.SemiColon]);
390
+ }
391
+ return this.finish(node);
392
+ }
393
+ _parseSelector(isNested) {
394
+ const node = this.create(nodes.Selector);
395
+ let hasContent = false;
396
+ if (isNested) {
397
+ // nested selectors can start with a combinator
398
+ hasContent = node.addChild(this._parseCombinator());
399
+ }
400
+ while (node.addChild(this._parseSimpleSelector())) {
401
+ hasContent = true;
402
+ node.addChild(this._parseCombinator()); // optional
403
+ }
404
+ return hasContent ? this.finish(node) : null;
405
+ }
406
+ _parseDeclaration(stopTokens) {
407
+ const custonProperty = this._tryParseCustomPropertyDeclaration(stopTokens);
408
+ if (custonProperty) {
409
+ return custonProperty;
410
+ }
411
+ const node = this.create(nodes.Declaration);
412
+ if (!node.setProperty(this._parseProperty())) {
413
+ return null;
414
+ }
415
+ if (!this.accept(cssScanner_1.TokenType.Colon)) {
416
+ return this.finish(node, cssErrors_1.ParseError.ColonExpected, [cssScanner_1.TokenType.Colon], stopTokens || [cssScanner_1.TokenType.SemiColon]);
417
+ }
418
+ if (this.prevToken) {
419
+ node.colonPosition = this.prevToken.offset;
420
+ }
421
+ if (!node.setValue(this._parseExpr())) {
422
+ return this.finish(node, cssErrors_1.ParseError.PropertyValueExpected);
423
+ }
424
+ node.addChild(this._parsePrio());
425
+ if (this.peek(cssScanner_1.TokenType.SemiColon)) {
426
+ node.semicolonPosition = this.token.offset; // not part of the declaration, but useful information for code assist
427
+ }
428
+ return this.finish(node);
429
+ }
430
+ _tryParseCustomPropertyDeclaration(stopTokens) {
431
+ if (!this.peekRegExp(cssScanner_1.TokenType.Ident, /^--/)) {
432
+ return null;
433
+ }
434
+ const node = this.create(nodes.CustomPropertyDeclaration);
435
+ if (!node.setProperty(this._parseProperty())) {
436
+ return null;
437
+ }
438
+ if (!this.accept(cssScanner_1.TokenType.Colon)) {
439
+ return this.finish(node, cssErrors_1.ParseError.ColonExpected, [cssScanner_1.TokenType.Colon]);
440
+ }
441
+ if (this.prevToken) {
442
+ node.colonPosition = this.prevToken.offset;
443
+ }
444
+ const mark = this.mark();
445
+ if (this.peek(cssScanner_1.TokenType.CurlyL)) {
446
+ // try to parse it as nested declaration
447
+ const propertySet = this.create(nodes.CustomPropertySet);
448
+ const declarations = this._parseDeclarations(this._parseRuleSetDeclaration.bind(this));
449
+ if (propertySet.setDeclarations(declarations) && !declarations.isErroneous(true)) {
450
+ propertySet.addChild(this._parsePrio());
451
+ if (this.peek(cssScanner_1.TokenType.SemiColon)) {
452
+ this.finish(propertySet);
453
+ node.setPropertySet(propertySet);
454
+ node.semicolonPosition = this.token.offset; // not part of the declaration, but useful information for code assist
455
+ return this.finish(node);
456
+ }
457
+ }
458
+ this.restoreAtMark(mark);
459
+ }
460
+ // try to parse as expression
461
+ const expression = this._parseExpr();
462
+ if (expression && !expression.isErroneous(true)) {
463
+ this._parsePrio();
464
+ if (this.peekOne(...(stopTokens || []), cssScanner_1.TokenType.SemiColon, cssScanner_1.TokenType.EOF)) {
465
+ node.setValue(expression);
466
+ if (this.peek(cssScanner_1.TokenType.SemiColon)) {
467
+ node.semicolonPosition = this.token.offset; // not part of the declaration, but useful information for code assist
468
+ }
469
+ return this.finish(node);
470
+ }
471
+ }
472
+ this.restoreAtMark(mark);
473
+ node.addChild(this._parseCustomPropertyValue(stopTokens));
474
+ node.addChild(this._parsePrio());
475
+ if ((0, objects_1.isDefined)(node.colonPosition) && this.token.offset === node.colonPosition + 1) {
476
+ return this.finish(node, cssErrors_1.ParseError.PropertyValueExpected);
477
+ }
478
+ return this.finish(node);
479
+ }
480
+ /**
481
+ * Parse custom property values.
482
+ *
483
+ * Based on https://www.w3.org/TR/css-variables/#syntax
484
+ *
485
+ * This code is somewhat unusual, as the allowed syntax is incredibly broad,
486
+ * parsing almost any sequence of tokens, save for a small set of exceptions.
487
+ * Unbalanced delimitors, invalid tokens, and declaration
488
+ * terminators like semicolons and !important directives (when not inside
489
+ * of delimitors).
490
+ */
491
+ _parseCustomPropertyValue(stopTokens = [cssScanner_1.TokenType.CurlyR]) {
492
+ const node = this.create(nodes.Node);
493
+ const isTopLevel = () => curlyDepth === 0 && parensDepth === 0 && bracketsDepth === 0;
494
+ const onStopToken = () => stopTokens.indexOf(this.token.type) !== -1;
495
+ let curlyDepth = 0;
496
+ let parensDepth = 0;
497
+ let bracketsDepth = 0;
498
+ done: while (true) {
499
+ switch (this.token.type) {
500
+ case cssScanner_1.TokenType.SemiColon:
501
+ // A semicolon only ends things if we're not inside a delimitor.
502
+ if (isTopLevel()) {
503
+ break done;
504
+ }
505
+ break;
506
+ case cssScanner_1.TokenType.Exclamation:
507
+ // An exclamation ends the value if we're not inside delims.
508
+ if (isTopLevel()) {
509
+ break done;
510
+ }
511
+ break;
512
+ case cssScanner_1.TokenType.CurlyL:
513
+ curlyDepth++;
514
+ break;
515
+ case cssScanner_1.TokenType.CurlyR:
516
+ curlyDepth--;
517
+ if (curlyDepth < 0) {
518
+ // The property value has been terminated without a semicolon, and
519
+ // this is the last declaration in the ruleset.
520
+ if (onStopToken() && parensDepth === 0 && bracketsDepth === 0) {
521
+ break done;
522
+ }
523
+ return this.finish(node, cssErrors_1.ParseError.LeftCurlyExpected);
524
+ }
525
+ break;
526
+ case cssScanner_1.TokenType.ParenthesisL:
527
+ parensDepth++;
528
+ break;
529
+ case cssScanner_1.TokenType.ParenthesisR:
530
+ parensDepth--;
531
+ if (parensDepth < 0) {
532
+ if (onStopToken() && bracketsDepth === 0 && curlyDepth === 0) {
533
+ break done;
534
+ }
535
+ return this.finish(node, cssErrors_1.ParseError.LeftParenthesisExpected);
536
+ }
537
+ break;
538
+ case cssScanner_1.TokenType.BracketL:
539
+ bracketsDepth++;
540
+ break;
541
+ case cssScanner_1.TokenType.BracketR:
542
+ bracketsDepth--;
543
+ if (bracketsDepth < 0) {
544
+ return this.finish(node, cssErrors_1.ParseError.LeftSquareBracketExpected);
545
+ }
546
+ break;
547
+ case cssScanner_1.TokenType.BadString: // fall through
548
+ break done;
549
+ case cssScanner_1.TokenType.EOF:
550
+ // We shouldn't have reached the end of input, something is
551
+ // unterminated.
552
+ let error = cssErrors_1.ParseError.RightCurlyExpected;
553
+ if (bracketsDepth > 0) {
554
+ error = cssErrors_1.ParseError.RightSquareBracketExpected;
555
+ }
556
+ else if (parensDepth > 0) {
557
+ error = cssErrors_1.ParseError.RightParenthesisExpected;
558
+ }
559
+ return this.finish(node, error);
560
+ }
561
+ this.consumeToken();
562
+ }
563
+ return this.finish(node);
564
+ }
565
+ _tryToParseDeclaration(stopTokens) {
566
+ const mark = this.mark();
567
+ if (this._parseProperty() && this.accept(cssScanner_1.TokenType.Colon)) {
568
+ // looks like a declaration, go ahead
569
+ this.restoreAtMark(mark);
570
+ return this._parseDeclaration(stopTokens);
571
+ }
572
+ this.restoreAtMark(mark);
573
+ return null;
574
+ }
575
+ _parseProperty() {
576
+ const node = this.create(nodes.Property);
577
+ const mark = this.mark();
578
+ if (this.acceptDelim('*') || this.acceptDelim('_')) {
579
+ // support for IE 5.x, 6 and 7 star hack: see http://en.wikipedia.org/wiki/CSS_filter#Star_hack
580
+ if (this.hasWhitespace()) {
581
+ this.restoreAtMark(mark);
582
+ return null;
583
+ }
584
+ }
585
+ if (node.setIdentifier(this._parsePropertyIdentifier())) {
586
+ return this.finish(node);
587
+ }
588
+ return null;
589
+ }
590
+ _parsePropertyIdentifier() {
591
+ return this._parseIdent();
592
+ }
593
+ _parseCharset() {
594
+ if (!this.peek(cssScanner_1.TokenType.Charset)) {
595
+ return null;
596
+ }
597
+ const node = this.create(nodes.Node);
598
+ this.consumeToken(); // charset
599
+ if (!this.accept(cssScanner_1.TokenType.String)) {
600
+ return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
601
+ }
602
+ if (!this.accept(cssScanner_1.TokenType.SemiColon)) {
603
+ return this.finish(node, cssErrors_1.ParseError.SemiColonExpected);
604
+ }
605
+ return this.finish(node);
606
+ }
607
+ _parseImport() {
608
+ if (!this.peekKeyword('@import')) {
609
+ return null;
610
+ }
611
+ const node = this.create(nodes.Import);
612
+ this.consumeToken(); // @import
613
+ if (!node.addChild(this._parseURILiteral()) && !node.addChild(this._parseStringLiteral())) {
614
+ return this.finish(node, cssErrors_1.ParseError.URIOrStringExpected);
615
+ }
616
+ if (!this.peek(cssScanner_1.TokenType.SemiColon) && !this.peek(cssScanner_1.TokenType.EOF)) {
617
+ node.setMedialist(this._parseMediaQueryList());
618
+ }
619
+ return this.finish(node);
620
+ }
621
+ _parseNamespace() {
622
+ // http://www.w3.org/TR/css3-namespace/
623
+ // namespace : NAMESPACE_SYM S* [IDENT S*]? [STRING|URI] S* ';' S*
624
+ if (!this.peekKeyword('@namespace')) {
625
+ return null;
626
+ }
627
+ const node = this.create(nodes.Namespace);
628
+ this.consumeToken(); // @namespace
629
+ if (!node.addChild(this._parseURILiteral())) { // url literal also starts with ident
630
+ node.addChild(this._parseIdent()); // optional prefix
631
+ if (!node.addChild(this._parseURILiteral()) && !node.addChild(this._parseStringLiteral())) {
632
+ return this.finish(node, cssErrors_1.ParseError.URIExpected, [cssScanner_1.TokenType.SemiColon]);
633
+ }
634
+ }
635
+ if (!this.accept(cssScanner_1.TokenType.SemiColon)) {
636
+ return this.finish(node, cssErrors_1.ParseError.SemiColonExpected);
637
+ }
638
+ return this.finish(node);
639
+ }
640
+ _parseFontFace() {
641
+ if (!this.peekKeyword('@font-face')) {
642
+ return null;
643
+ }
644
+ const node = this.create(nodes.FontFace);
645
+ this.consumeToken(); // @font-face
646
+ return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
647
+ }
648
+ _parseViewPort() {
649
+ if (!this.peekKeyword('@-ms-viewport') &&
650
+ !this.peekKeyword('@-o-viewport') &&
651
+ !this.peekKeyword('@viewport')) {
652
+ return null;
653
+ }
654
+ const node = this.create(nodes.ViewPort);
655
+ this.consumeToken(); // @-ms-viewport
656
+ return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
657
+ }
658
+ _parseKeyframe() {
659
+ if (!this.peekRegExp(cssScanner_1.TokenType.AtKeyword, this.keyframeRegex)) {
660
+ return null;
661
+ }
662
+ const node = this.create(nodes.Keyframe);
663
+ const atNode = this.create(nodes.Node);
664
+ this.consumeToken(); // atkeyword
665
+ node.setKeyword(this.finish(atNode));
666
+ if (atNode.matches('@-ms-keyframes')) { // -ms-keyframes never existed
667
+ this.markError(atNode, cssErrors_1.ParseError.UnknownKeyword);
668
+ }
669
+ if (!node.setIdentifier(this._parseKeyframeIdent())) {
670
+ return this.finish(node, cssErrors_1.ParseError.IdentifierExpected, [cssScanner_1.TokenType.CurlyR]);
671
+ }
672
+ return this._parseBody(node, this._parseKeyframeSelector.bind(this));
673
+ }
674
+ _parseKeyframeIdent() {
675
+ return this._parseIdent([nodes.ReferenceType.Keyframe]);
676
+ }
677
+ _parseKeyframeSelector() {
678
+ const node = this.create(nodes.KeyframeSelector);
679
+ if (!node.addChild(this._parseIdent()) && !this.accept(cssScanner_1.TokenType.Percentage)) {
680
+ return null;
681
+ }
682
+ while (this.accept(cssScanner_1.TokenType.Comma)) {
683
+ if (!node.addChild(this._parseIdent()) && !this.accept(cssScanner_1.TokenType.Percentage)) {
684
+ return this.finish(node, cssErrors_1.ParseError.PercentageExpected);
685
+ }
686
+ }
687
+ return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
688
+ }
689
+ _tryParseKeyframeSelector() {
690
+ const node = this.create(nodes.KeyframeSelector);
691
+ const pos = this.mark();
692
+ if (!node.addChild(this._parseIdent()) && !this.accept(cssScanner_1.TokenType.Percentage)) {
693
+ return null;
694
+ }
695
+ while (this.accept(cssScanner_1.TokenType.Comma)) {
696
+ if (!node.addChild(this._parseIdent()) && !this.accept(cssScanner_1.TokenType.Percentage)) {
697
+ this.restoreAtMark(pos);
698
+ return null;
699
+ }
700
+ }
701
+ if (!this.peek(cssScanner_1.TokenType.CurlyL)) {
702
+ this.restoreAtMark(pos);
703
+ return null;
704
+ }
705
+ return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
706
+ }
707
+ _parseSupports(isNested = false) {
708
+ // SUPPORTS_SYM S* supports_condition '{' S* ruleset* '}' S*
709
+ if (!this.peekKeyword('@supports')) {
710
+ return null;
711
+ }
712
+ const node = this.create(nodes.Supports);
713
+ this.consumeToken(); // @supports
714
+ node.addChild(this._parseSupportsCondition());
715
+ return this._parseBody(node, this._parseSupportsDeclaration.bind(this, isNested));
716
+ }
717
+ _parseSupportsDeclaration(isNested = false) {
718
+ if (isNested) {
719
+ // if nested, the body can contain rulesets, but also declarations
720
+ return this._tryParseRuleset(true)
721
+ || this._tryToParseDeclaration()
722
+ || this._parseStylesheetStatement(true);
723
+ }
724
+ return this._parseStylesheetStatement(false);
725
+ }
726
+ _parseSupportsCondition() {
727
+ // supports_condition : supports_negation | supports_conjunction | supports_disjunction | supports_condition_in_parens ;
728
+ // supports_condition_in_parens: ( '(' S* supports_condition S* ')' ) | supports_declaration_condition | general_enclosed ;
729
+ // supports_negation: NOT S+ supports_condition_in_parens ;
730
+ // supports_conjunction: supports_condition_in_parens ( S+ AND S+ supports_condition_in_parens )+;
731
+ // supports_disjunction: supports_condition_in_parens ( S+ OR S+ supports_condition_in_parens )+;
732
+ // supports_declaration_condition: '(' S* declaration ')';
733
+ // general_enclosed: ( FUNCTION | '(' ) ( any | unused )* ')' ;
734
+ const node = this.create(nodes.SupportsCondition);
735
+ if (this.acceptIdent('not')) {
736
+ node.addChild(this._parseSupportsConditionInParens());
737
+ }
738
+ else {
739
+ node.addChild(this._parseSupportsConditionInParens());
740
+ if (this.peekRegExp(cssScanner_1.TokenType.Ident, /^(and|or)$/i)) {
741
+ const text = this.token.text.toLowerCase();
742
+ while (this.acceptIdent(text)) {
743
+ node.addChild(this._parseSupportsConditionInParens());
744
+ }
745
+ }
746
+ }
747
+ return this.finish(node);
748
+ }
749
+ _parseSupportsConditionInParens() {
750
+ const node = this.create(nodes.SupportsCondition);
751
+ if (this.accept(cssScanner_1.TokenType.ParenthesisL)) {
752
+ if (this.prevToken) {
753
+ node.lParent = this.prevToken.offset;
754
+ }
755
+ if (!node.addChild(this._tryToParseDeclaration([cssScanner_1.TokenType.ParenthesisR]))) {
756
+ if (!this._parseSupportsCondition()) {
757
+ return this.finish(node, cssErrors_1.ParseError.ConditionExpected);
758
+ }
759
+ }
760
+ if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
761
+ return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected, [cssScanner_1.TokenType.ParenthesisR], []);
762
+ }
763
+ if (this.prevToken) {
764
+ node.rParent = this.prevToken.offset;
765
+ }
766
+ return this.finish(node);
767
+ }
768
+ else if (this.peek(cssScanner_1.TokenType.Ident)) {
769
+ const pos = this.mark();
770
+ this.consumeToken();
771
+ if (!this.hasWhitespace() && this.accept(cssScanner_1.TokenType.ParenthesisL)) {
772
+ let openParentCount = 1;
773
+ while (this.token.type !== cssScanner_1.TokenType.EOF && openParentCount !== 0) {
774
+ if (this.token.type === cssScanner_1.TokenType.ParenthesisL) {
775
+ openParentCount++;
776
+ }
777
+ else if (this.token.type === cssScanner_1.TokenType.ParenthesisR) {
778
+ openParentCount--;
779
+ }
780
+ this.consumeToken();
781
+ }
782
+ return this.finish(node);
783
+ }
784
+ else {
785
+ this.restoreAtMark(pos);
786
+ }
787
+ }
788
+ return this.finish(node, cssErrors_1.ParseError.LeftParenthesisExpected, [], [cssScanner_1.TokenType.ParenthesisL]);
789
+ }
790
+ _parseMediaDeclaration(isNested = false) {
791
+ if (isNested) {
792
+ // if nested, the body can contain rulesets, but also declarations
793
+ return this._tryParseRuleset(true)
794
+ || this._tryToParseDeclaration()
795
+ || this._parseStylesheetStatement(true);
796
+ }
797
+ return this._parseStylesheetStatement(false);
798
+ }
799
+ _parseMedia(isNested = false) {
800
+ // MEDIA_SYM S* media_query_list '{' S* ruleset* '}' S*
801
+ // media_query_list : S* [media_query [ ',' S* media_query ]* ]?
802
+ if (!this.peekKeyword('@media')) {
803
+ return null;
804
+ }
805
+ const node = this.create(nodes.Media);
806
+ this.consumeToken(); // @media
807
+ if (!node.addChild(this._parseMediaQueryList())) {
808
+ return this.finish(node, cssErrors_1.ParseError.MediaQueryExpected);
809
+ }
810
+ return this._parseBody(node, this._parseMediaDeclaration.bind(this, isNested));
811
+ }
812
+ _parseMediaQueryList() {
813
+ const node = this.create(nodes.Medialist);
814
+ if (!node.addChild(this._parseMediaQuery())) {
815
+ return this.finish(node, cssErrors_1.ParseError.MediaQueryExpected);
816
+ }
817
+ while (this.accept(cssScanner_1.TokenType.Comma)) {
818
+ if (!node.addChild(this._parseMediaQuery())) {
819
+ return this.finish(node, cssErrors_1.ParseError.MediaQueryExpected);
820
+ }
821
+ }
822
+ return this.finish(node);
823
+ }
824
+ _parseMediaQuery() {
825
+ // <media-query> = <media-condition> | [ not | only ]? <media-type> [ and <media-condition-without-or> ]?
826
+ const node = this.create(nodes.MediaQuery);
827
+ const pos = this.mark();
828
+ this.acceptIdent('not');
829
+ if (!this.peek(cssScanner_1.TokenType.ParenthesisL)) {
830
+ if (this.acceptIdent('only')) {
831
+ // optional
832
+ }
833
+ if (!node.addChild(this._parseIdent())) {
834
+ return null;
835
+ }
836
+ if (this.acceptIdent('and')) {
837
+ node.addChild(this._parseMediaCondition());
838
+ }
839
+ }
840
+ else {
841
+ this.restoreAtMark(pos); // 'not' is part of the MediaCondition
842
+ node.addChild(this._parseMediaCondition());
843
+ }
844
+ return this.finish(node);
845
+ }
846
+ _parseRatio() {
847
+ const pos = this.mark();
848
+ const node = this.create(nodes.RatioValue);
849
+ if (!this._parseNumeric()) {
850
+ return null;
851
+ }
852
+ if (!this.acceptDelim('/')) {
853
+ this.restoreAtMark(pos);
854
+ return null;
855
+ }
856
+ if (!this._parseNumeric()) {
857
+ return this.finish(node, cssErrors_1.ParseError.NumberExpected);
858
+ }
859
+ return this.finish(node);
860
+ }
861
+ _parseMediaCondition() {
862
+ // <media-condition> = <media-not> | <media-and> | <media-or> | <media-in-parens>
863
+ // <media-not> = not <media-in-parens>
864
+ // <media-and> = <media-in-parens> [ and <media-in-parens> ]+
865
+ // <media-or> = <media-in-parens> [ or <media-in-parens> ]+
866
+ // <media-in-parens> = ( <media-condition> ) | <media-feature> | <general-enclosed>
867
+ const node = this.create(nodes.MediaCondition);
868
+ this.acceptIdent('not');
869
+ let parseExpression = true;
870
+ while (parseExpression) {
871
+ if (!this.accept(cssScanner_1.TokenType.ParenthesisL)) {
872
+ return this.finish(node, cssErrors_1.ParseError.LeftParenthesisExpected, [], [cssScanner_1.TokenType.CurlyL]);
873
+ }
874
+ if (this.peek(cssScanner_1.TokenType.ParenthesisL) || this.peekIdent('not')) {
875
+ // <media-condition>
876
+ node.addChild(this._parseMediaCondition());
877
+ }
878
+ else {
879
+ node.addChild(this._parseMediaFeature());
880
+ }
881
+ // not yet implemented: general enclosed
882
+ if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
883
+ return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected, [], [cssScanner_1.TokenType.CurlyL]);
884
+ }
885
+ parseExpression = this.acceptIdent('and') || this.acceptIdent('or');
886
+ }
887
+ return this.finish(node);
888
+ }
889
+ _parseMediaFeature() {
890
+ const resyncStopToken = [cssScanner_1.TokenType.ParenthesisR];
891
+ const node = this.create(nodes.MediaFeature);
892
+ // <media-feature> = ( [ <mf-plain> | <mf-boolean> | <mf-range> ] )
893
+ // <mf-plain> = <mf-name> : <mf-value>
894
+ // <mf-boolean> = <mf-name>
895
+ // <mf-range> = <mf-name> [ '<' | '>' ]? '='? <mf-value> | <mf-value> [ '<' | '>' ]? '='? <mf-name> | <mf-value> '<' '='? <mf-name> '<' '='? <mf-value> | <mf-value> '>' '='? <mf-name> '>' '='? <mf-value>
896
+ const parseRangeOperator = () => {
897
+ if (this.acceptDelim('<') || this.acceptDelim('>')) {
898
+ if (!this.hasWhitespace()) {
899
+ this.acceptDelim('=');
900
+ }
901
+ return true;
902
+ }
903
+ else if (this.acceptDelim('=')) {
904
+ return true;
905
+ }
906
+ return false;
907
+ };
908
+ if (node.addChild(this._parseMediaFeatureName())) {
909
+ if (this.accept(cssScanner_1.TokenType.Colon)) {
910
+ if (!node.addChild(this._parseMediaFeatureValue())) {
911
+ return this.finish(node, cssErrors_1.ParseError.TermExpected, [], resyncStopToken);
912
+ }
913
+ }
914
+ else if (parseRangeOperator()) {
915
+ if (!node.addChild(this._parseMediaFeatureValue())) {
916
+ return this.finish(node, cssErrors_1.ParseError.TermExpected, [], resyncStopToken);
917
+ }
918
+ if (parseRangeOperator()) {
919
+ if (!node.addChild(this._parseMediaFeatureValue())) {
920
+ return this.finish(node, cssErrors_1.ParseError.TermExpected, [], resyncStopToken);
921
+ }
922
+ }
923
+ }
924
+ else {
925
+ // <mf-boolean> = <mf-name>
926
+ }
927
+ }
928
+ else if (node.addChild(this._parseMediaFeatureValue())) {
929
+ if (!parseRangeOperator()) {
930
+ return this.finish(node, cssErrors_1.ParseError.OperatorExpected, [], resyncStopToken);
931
+ }
932
+ if (!node.addChild(this._parseMediaFeatureName())) {
933
+ return this.finish(node, cssErrors_1.ParseError.IdentifierExpected, [], resyncStopToken);
934
+ }
935
+ if (parseRangeOperator()) {
936
+ if (!node.addChild(this._parseMediaFeatureValue())) {
937
+ return this.finish(node, cssErrors_1.ParseError.TermExpected, [], resyncStopToken);
938
+ }
939
+ }
940
+ }
941
+ else {
942
+ return this.finish(node, cssErrors_1.ParseError.IdentifierExpected, [], resyncStopToken);
943
+ }
944
+ return this.finish(node);
945
+ }
946
+ _parseMediaFeatureName() {
947
+ return this._parseIdent();
948
+ }
949
+ _parseMediaFeatureValue() {
950
+ return this._parseRatio() || this._parseTermExpression();
951
+ }
952
+ _parseMedium() {
953
+ const node = this.create(nodes.Node);
954
+ if (node.addChild(this._parseIdent())) {
955
+ return this.finish(node);
956
+ }
957
+ else {
958
+ return null;
959
+ }
960
+ }
961
+ _parsePageDeclaration() {
962
+ return this._parsePageMarginBox() || this._parseRuleSetDeclaration();
963
+ }
964
+ _parsePage() {
965
+ // http://www.w3.org/TR/css3-page/
966
+ // page_rule : PAGE_SYM S* page_selector_list '{' S* page_body '}' S*
967
+ // page_body : /* Can be empty */ declaration? [ ';' S* page_body ]? | page_margin_box page_body
968
+ if (!this.peekKeyword('@page')) {
969
+ return null;
970
+ }
971
+ const node = this.create(nodes.Page);
972
+ this.consumeToken();
973
+ if (node.addChild(this._parsePageSelector())) {
974
+ while (this.accept(cssScanner_1.TokenType.Comma)) {
975
+ if (!node.addChild(this._parsePageSelector())) {
976
+ return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
977
+ }
978
+ }
979
+ }
980
+ return this._parseBody(node, this._parsePageDeclaration.bind(this));
981
+ }
982
+ _parsePageMarginBox() {
983
+ // page_margin_box : margin_sym S* '{' S* declaration? [ ';' S* declaration? ]* '}' S*
984
+ if (!this.peek(cssScanner_1.TokenType.AtKeyword)) {
985
+ return null;
986
+ }
987
+ const node = this.create(nodes.PageBoxMarginBox);
988
+ if (!this.acceptOneKeyword(languageFacts.pageBoxDirectives)) {
989
+ this.markError(node, cssErrors_1.ParseError.UnknownAtRule, [], [cssScanner_1.TokenType.CurlyL]);
990
+ }
991
+ return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
992
+ }
993
+ _parsePageSelector() {
994
+ // page_selector : pseudo_page+ | IDENT pseudo_page*
995
+ // pseudo_page : ':' [ "left" | "right" | "first" | "blank" ];
996
+ if (!this.peek(cssScanner_1.TokenType.Ident) && !this.peek(cssScanner_1.TokenType.Colon)) {
997
+ return null;
998
+ }
999
+ const node = this.create(nodes.Node);
1000
+ node.addChild(this._parseIdent()); // optional ident
1001
+ if (this.accept(cssScanner_1.TokenType.Colon)) {
1002
+ if (!node.addChild(this._parseIdent())) { // optional ident
1003
+ return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
1004
+ }
1005
+ }
1006
+ return this.finish(node);
1007
+ }
1008
+ _parseDocument() {
1009
+ // -moz-document is experimental but has been pushed to css4
1010
+ if (!this.peekKeyword('@-moz-document')) {
1011
+ return null;
1012
+ }
1013
+ const node = this.create(nodes.Document);
1014
+ this.consumeToken(); // @-moz-document
1015
+ this.resync([], [cssScanner_1.TokenType.CurlyL]); // ignore all the rules
1016
+ return this._parseBody(node, this._parseStylesheetStatement.bind(this));
1017
+ }
1018
+ // https://www.w3.org/TR/css-syntax-3/#consume-an-at-rule
1019
+ _parseUnknownAtRule() {
1020
+ if (!this.peek(cssScanner_1.TokenType.AtKeyword)) {
1021
+ return null;
1022
+ }
1023
+ const node = this.create(nodes.UnknownAtRule);
1024
+ node.addChild(this._parseUnknownAtRuleName());
1025
+ const isTopLevel = () => curlyDepth === 0 && parensDepth === 0 && bracketsDepth === 0;
1026
+ let curlyLCount = 0;
1027
+ let curlyDepth = 0;
1028
+ let parensDepth = 0;
1029
+ let bracketsDepth = 0;
1030
+ done: while (true) {
1031
+ switch (this.token.type) {
1032
+ case cssScanner_1.TokenType.SemiColon:
1033
+ if (isTopLevel()) {
1034
+ break done;
1035
+ }
1036
+ break;
1037
+ case cssScanner_1.TokenType.EOF:
1038
+ if (curlyDepth > 0) {
1039
+ return this.finish(node, cssErrors_1.ParseError.RightCurlyExpected);
1040
+ }
1041
+ else if (bracketsDepth > 0) {
1042
+ return this.finish(node, cssErrors_1.ParseError.RightSquareBracketExpected);
1043
+ }
1044
+ else if (parensDepth > 0) {
1045
+ return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
1046
+ }
1047
+ else {
1048
+ return this.finish(node);
1049
+ }
1050
+ case cssScanner_1.TokenType.CurlyL:
1051
+ curlyLCount++;
1052
+ curlyDepth++;
1053
+ break;
1054
+ case cssScanner_1.TokenType.CurlyR:
1055
+ curlyDepth--;
1056
+ // End of at-rule, consume CurlyR and return node
1057
+ if (curlyLCount > 0 && curlyDepth === 0) {
1058
+ this.consumeToken();
1059
+ if (bracketsDepth > 0) {
1060
+ return this.finish(node, cssErrors_1.ParseError.RightSquareBracketExpected);
1061
+ }
1062
+ else if (parensDepth > 0) {
1063
+ return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
1064
+ }
1065
+ break done;
1066
+ }
1067
+ if (curlyDepth < 0) {
1068
+ // The property value has been terminated without a semicolon, and
1069
+ // this is the last declaration in the ruleset.
1070
+ if (parensDepth === 0 && bracketsDepth === 0) {
1071
+ break done;
1072
+ }
1073
+ return this.finish(node, cssErrors_1.ParseError.LeftCurlyExpected);
1074
+ }
1075
+ break;
1076
+ case cssScanner_1.TokenType.ParenthesisL:
1077
+ parensDepth++;
1078
+ break;
1079
+ case cssScanner_1.TokenType.ParenthesisR:
1080
+ parensDepth--;
1081
+ if (parensDepth < 0) {
1082
+ return this.finish(node, cssErrors_1.ParseError.LeftParenthesisExpected);
1083
+ }
1084
+ break;
1085
+ case cssScanner_1.TokenType.BracketL:
1086
+ bracketsDepth++;
1087
+ break;
1088
+ case cssScanner_1.TokenType.BracketR:
1089
+ bracketsDepth--;
1090
+ if (bracketsDepth < 0) {
1091
+ return this.finish(node, cssErrors_1.ParseError.LeftSquareBracketExpected);
1092
+ }
1093
+ break;
1094
+ }
1095
+ this.consumeToken();
1096
+ }
1097
+ return node;
1098
+ }
1099
+ _parseUnknownAtRuleName() {
1100
+ const node = this.create(nodes.Node);
1101
+ if (this.accept(cssScanner_1.TokenType.AtKeyword)) {
1102
+ return this.finish(node);
1103
+ }
1104
+ return node;
1105
+ }
1106
+ _parseOperator() {
1107
+ // these are operators for binary expressions
1108
+ if (this.peekDelim('/') ||
1109
+ this.peekDelim('*') ||
1110
+ this.peekDelim('+') ||
1111
+ this.peekDelim('-') ||
1112
+ this.peek(cssScanner_1.TokenType.Dashmatch) ||
1113
+ this.peek(cssScanner_1.TokenType.Includes) ||
1114
+ this.peek(cssScanner_1.TokenType.SubstringOperator) ||
1115
+ this.peek(cssScanner_1.TokenType.PrefixOperator) ||
1116
+ this.peek(cssScanner_1.TokenType.SuffixOperator) ||
1117
+ this.peekDelim('=')) { // doesn't stick to the standard here
1118
+ const node = this.createNode(nodes.NodeType.Operator);
1119
+ this.consumeToken();
1120
+ return this.finish(node);
1121
+ }
1122
+ else {
1123
+ return null;
1124
+ }
1125
+ }
1126
+ _parseUnaryOperator() {
1127
+ if (!this.peekDelim('+') && !this.peekDelim('-')) {
1128
+ return null;
1129
+ }
1130
+ const node = this.create(nodes.Node);
1131
+ this.consumeToken();
1132
+ return this.finish(node);
1133
+ }
1134
+ _parseCombinator() {
1135
+ if (this.peekDelim('>')) {
1136
+ const node = this.create(nodes.Node);
1137
+ this.consumeToken();
1138
+ const mark = this.mark();
1139
+ if (!this.hasWhitespace() && this.acceptDelim('>')) {
1140
+ if (!this.hasWhitespace() && this.acceptDelim('>')) {
1141
+ node.type = nodes.NodeType.SelectorCombinatorShadowPiercingDescendant;
1142
+ return this.finish(node);
1143
+ }
1144
+ this.restoreAtMark(mark);
1145
+ }
1146
+ node.type = nodes.NodeType.SelectorCombinatorParent;
1147
+ return this.finish(node);
1148
+ }
1149
+ else if (this.peekDelim('+')) {
1150
+ const node = this.create(nodes.Node);
1151
+ this.consumeToken();
1152
+ node.type = nodes.NodeType.SelectorCombinatorSibling;
1153
+ return this.finish(node);
1154
+ }
1155
+ else if (this.peekDelim('~')) {
1156
+ const node = this.create(nodes.Node);
1157
+ this.consumeToken();
1158
+ node.type = nodes.NodeType.SelectorCombinatorAllSiblings;
1159
+ return this.finish(node);
1160
+ }
1161
+ else if (this.peekDelim('/')) {
1162
+ const node = this.create(nodes.Node);
1163
+ this.consumeToken();
1164
+ const mark = this.mark();
1165
+ if (!this.hasWhitespace() && this.acceptIdent('deep') && !this.hasWhitespace() && this.acceptDelim('/')) {
1166
+ node.type = nodes.NodeType.SelectorCombinatorShadowPiercingDescendant;
1167
+ return this.finish(node);
1168
+ }
1169
+ this.restoreAtMark(mark);
1170
+ }
1171
+ return null;
1172
+ }
1173
+ _parseSimpleSelector() {
1174
+ // simple_selector
1175
+ // : element_name [ HASH | class | attrib | pseudo ]* | [ HASH | class | attrib | pseudo ]+ ;
1176
+ const node = this.create(nodes.SimpleSelector);
1177
+ let c = 0;
1178
+ if (node.addChild(this._parseElementName())) {
1179
+ c++;
1180
+ }
1181
+ while ((c === 0 || !this.hasWhitespace()) && node.addChild(this._parseSimpleSelectorBody())) {
1182
+ c++;
1183
+ }
1184
+ return c > 0 ? this.finish(node) : null;
1185
+ }
1186
+ _parseSimpleSelectorBody() {
1187
+ return this._parsePseudo() || this._parseHash() || this._parseClass() || this._parseAttrib();
1188
+ }
1189
+ _parseSelectorIdent() {
1190
+ return this._parseIdent();
1191
+ }
1192
+ _parseHash() {
1193
+ if (!this.peek(cssScanner_1.TokenType.Hash) && !this.peekDelim('#')) {
1194
+ return null;
1195
+ }
1196
+ const node = this.createNode(nodes.NodeType.IdentifierSelector);
1197
+ if (this.acceptDelim('#')) {
1198
+ if (this.hasWhitespace() || !node.addChild(this._parseSelectorIdent())) {
1199
+ return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
1200
+ }
1201
+ }
1202
+ else {
1203
+ this.consumeToken(); // TokenType.Hash
1204
+ }
1205
+ return this.finish(node);
1206
+ }
1207
+ _parseClass() {
1208
+ // class: '.' IDENT ;
1209
+ if (!this.peekDelim('.')) {
1210
+ return null;
1211
+ }
1212
+ const node = this.createNode(nodes.NodeType.ClassSelector);
1213
+ this.consumeToken(); // '.'
1214
+ if (this.hasWhitespace() || !node.addChild(this._parseSelectorIdent())) {
1215
+ return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
1216
+ }
1217
+ return this.finish(node);
1218
+ }
1219
+ _parseElementName() {
1220
+ // element_name: (ns? '|')? IDENT | '*';
1221
+ const pos = this.mark();
1222
+ const node = this.createNode(nodes.NodeType.ElementNameSelector);
1223
+ node.addChild(this._parseNamespacePrefix());
1224
+ if (!node.addChild(this._parseSelectorIdent()) && !this.acceptDelim('*')) {
1225
+ this.restoreAtMark(pos);
1226
+ return null;
1227
+ }
1228
+ return this.finish(node);
1229
+ }
1230
+ _parseNamespacePrefix() {
1231
+ const pos = this.mark();
1232
+ const node = this.createNode(nodes.NodeType.NamespacePrefix);
1233
+ if (!node.addChild(this._parseIdent()) && !this.acceptDelim('*')) {
1234
+ // ns is optional
1235
+ }
1236
+ if (!this.acceptDelim('|')) {
1237
+ this.restoreAtMark(pos);
1238
+ return null;
1239
+ }
1240
+ return this.finish(node);
1241
+ }
1242
+ _parseAttrib() {
1243
+ // attrib : '[' S* IDENT S* [ [ '=' | INCLUDES | DASHMATCH ] S* [ IDENT | STRING ] S* ]? ']'
1244
+ if (!this.peek(cssScanner_1.TokenType.BracketL)) {
1245
+ return null;
1246
+ }
1247
+ const node = this.create(nodes.AttributeSelector);
1248
+ this.consumeToken(); // BracketL
1249
+ // Optional attrib namespace
1250
+ node.setNamespacePrefix(this._parseNamespacePrefix());
1251
+ if (!node.setIdentifier(this._parseIdent())) {
1252
+ return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
1253
+ }
1254
+ if (node.setOperator(this._parseOperator())) {
1255
+ node.setValue(this._parseBinaryExpr());
1256
+ this.acceptIdent('i'); // case insensitive matching
1257
+ this.acceptIdent('s'); // case sensitive matching
1258
+ }
1259
+ if (!this.accept(cssScanner_1.TokenType.BracketR)) {
1260
+ return this.finish(node, cssErrors_1.ParseError.RightSquareBracketExpected);
1261
+ }
1262
+ return this.finish(node);
1263
+ }
1264
+ _parsePseudo() {
1265
+ // pseudo: ':' [ IDENT | FUNCTION S* [IDENT S*]? ')' ]
1266
+ const node = this._tryParsePseudoIdentifier();
1267
+ if (node) {
1268
+ if (!this.hasWhitespace() && this.accept(cssScanner_1.TokenType.ParenthesisL)) {
1269
+ const tryAsSelector = () => {
1270
+ const selectors = this.create(nodes.Node);
1271
+ if (!selectors.addChild(this._parseSelector(false))) {
1272
+ return null;
1273
+ }
1274
+ while (this.accept(cssScanner_1.TokenType.Comma) && selectors.addChild(this._parseSelector(false))) {
1275
+ // loop
1276
+ }
1277
+ if (this.peek(cssScanner_1.TokenType.ParenthesisR)) {
1278
+ return this.finish(selectors);
1279
+ }
1280
+ return null;
1281
+ };
1282
+ node.addChild(this.try(tryAsSelector) || this._parseBinaryExpr());
1283
+ if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
1284
+ return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
1285
+ }
1286
+ }
1287
+ return this.finish(node);
1288
+ }
1289
+ return null;
1290
+ }
1291
+ _tryParsePseudoIdentifier() {
1292
+ if (!this.peek(cssScanner_1.TokenType.Colon)) {
1293
+ return null;
1294
+ }
1295
+ const pos = this.mark();
1296
+ const node = this.createNode(nodes.NodeType.PseudoSelector);
1297
+ this.consumeToken(); // Colon
1298
+ if (this.hasWhitespace()) {
1299
+ this.restoreAtMark(pos);
1300
+ return null;
1301
+ }
1302
+ // optional, support ::
1303
+ this.accept(cssScanner_1.TokenType.Colon);
1304
+ if (this.hasWhitespace() || !node.addChild(this._parseIdent())) {
1305
+ return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
1306
+ }
1307
+ return this.finish(node);
1308
+ }
1309
+ _tryParsePrio() {
1310
+ const mark = this.mark();
1311
+ const prio = this._parsePrio();
1312
+ if (prio) {
1313
+ return prio;
1314
+ }
1315
+ this.restoreAtMark(mark);
1316
+ return null;
1317
+ }
1318
+ _parsePrio() {
1319
+ if (!this.peek(cssScanner_1.TokenType.Exclamation)) {
1320
+ return null;
1321
+ }
1322
+ const node = this.createNode(nodes.NodeType.Prio);
1323
+ if (this.accept(cssScanner_1.TokenType.Exclamation) && this.acceptIdent('important')) {
1324
+ return this.finish(node);
1325
+ }
1326
+ return null;
1327
+ }
1328
+ _parseExpr(stopOnComma = false) {
1329
+ const node = this.create(nodes.Expression);
1330
+ if (!node.addChild(this._parseBinaryExpr())) {
1331
+ return null;
1332
+ }
1333
+ while (true) {
1334
+ if (this.peek(cssScanner_1.TokenType.Comma)) { // optional
1335
+ if (stopOnComma) {
1336
+ return this.finish(node);
1337
+ }
1338
+ this.consumeToken();
1339
+ }
1340
+ if (!node.addChild(this._parseBinaryExpr())) {
1341
+ break;
1342
+ }
1343
+ }
1344
+ return this.finish(node);
1345
+ }
1346
+ _parseUnicodeRange() {
1347
+ if (!this.peekIdent('u')) {
1348
+ return null;
1349
+ }
1350
+ const node = this.create(nodes.UnicodeRange);
1351
+ if (!this.acceptUnicodeRange()) {
1352
+ return null;
1353
+ }
1354
+ return this.finish(node);
1355
+ }
1356
+ _parseNamedLine() {
1357
+ // https://www.w3.org/TR/css-grid-1/#named-lines
1358
+ if (!this.peek(cssScanner_1.TokenType.BracketL)) {
1359
+ return null;
1360
+ }
1361
+ const node = this.createNode(nodes.NodeType.GridLine);
1362
+ this.consumeToken();
1363
+ while (node.addChild(this._parseIdent())) {
1364
+ // repeat
1365
+ }
1366
+ if (!this.accept(cssScanner_1.TokenType.BracketR)) {
1367
+ return this.finish(node, cssErrors_1.ParseError.RightSquareBracketExpected);
1368
+ }
1369
+ return this.finish(node);
1370
+ }
1371
+ _parseBinaryExpr(preparsedLeft, preparsedOper) {
1372
+ let node = this.create(nodes.BinaryExpression);
1373
+ if (!node.setLeft((preparsedLeft || this._parseTerm()))) {
1374
+ return null;
1375
+ }
1376
+ if (!node.setOperator(preparsedOper || this._parseOperator())) {
1377
+ return this.finish(node);
1378
+ }
1379
+ if (!node.setRight(this._parseTerm())) {
1380
+ return this.finish(node, cssErrors_1.ParseError.TermExpected);
1381
+ }
1382
+ // things needed for multiple binary expressions
1383
+ node = this.finish(node);
1384
+ const operator = this._parseOperator();
1385
+ if (operator) {
1386
+ node = this._parseBinaryExpr(node, operator);
1387
+ }
1388
+ return this.finish(node);
1389
+ }
1390
+ _parseTerm() {
1391
+ let node = this.create(nodes.Term);
1392
+ node.setOperator(this._parseUnaryOperator()); // optional
1393
+ if (node.setExpression(this._parseTermExpression())) {
1394
+ return this.finish(node);
1395
+ }
1396
+ return null;
1397
+ }
1398
+ _parseTermExpression() {
1399
+ return this._parseURILiteral() || // url before function
1400
+ this._parseUnicodeRange() ||
1401
+ this._parseFunction() || // function before ident
1402
+ this._parseIdent() ||
1403
+ this._parseStringLiteral() ||
1404
+ this._parseNumeric() ||
1405
+ this._parseHexColor() ||
1406
+ this._parseOperation() ||
1407
+ this._parseNamedLine();
1408
+ }
1409
+ _parseOperation() {
1410
+ if (!this.peek(cssScanner_1.TokenType.ParenthesisL)) {
1411
+ return null;
1412
+ }
1413
+ const node = this.create(nodes.Node);
1414
+ this.consumeToken(); // ParenthesisL
1415
+ node.addChild(this._parseExpr());
1416
+ if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
1417
+ return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
1418
+ }
1419
+ return this.finish(node);
1420
+ }
1421
+ _parseNumeric() {
1422
+ if (this.peek(cssScanner_1.TokenType.Num) ||
1423
+ this.peek(cssScanner_1.TokenType.Percentage) ||
1424
+ this.peek(cssScanner_1.TokenType.Resolution) ||
1425
+ this.peek(cssScanner_1.TokenType.Length) ||
1426
+ this.peek(cssScanner_1.TokenType.EMS) ||
1427
+ this.peek(cssScanner_1.TokenType.EXS) ||
1428
+ this.peek(cssScanner_1.TokenType.Angle) ||
1429
+ this.peek(cssScanner_1.TokenType.Time) ||
1430
+ this.peek(cssScanner_1.TokenType.Dimension) ||
1431
+ this.peek(cssScanner_1.TokenType.Freq)) {
1432
+ const node = this.create(nodes.NumericValue);
1433
+ this.consumeToken();
1434
+ return this.finish(node);
1435
+ }
1436
+ return null;
1437
+ }
1438
+ _parseStringLiteral() {
1439
+ if (!this.peek(cssScanner_1.TokenType.String) && !this.peek(cssScanner_1.TokenType.BadString)) {
1440
+ return null;
1441
+ }
1442
+ const node = this.createNode(nodes.NodeType.StringLiteral);
1443
+ this.consumeToken();
1444
+ return this.finish(node);
1445
+ }
1446
+ _parseURILiteral() {
1447
+ if (!this.peekRegExp(cssScanner_1.TokenType.Ident, /^url(-prefix)?$/i)) {
1448
+ return null;
1449
+ }
1450
+ const pos = this.mark();
1451
+ const node = this.createNode(nodes.NodeType.URILiteral);
1452
+ this.accept(cssScanner_1.TokenType.Ident);
1453
+ if (this.hasWhitespace() || !this.peek(cssScanner_1.TokenType.ParenthesisL)) {
1454
+ this.restoreAtMark(pos);
1455
+ return null;
1456
+ }
1457
+ this.scanner.inURL = true;
1458
+ this.consumeToken(); // consume ()
1459
+ node.addChild(this._parseURLArgument()); // argument is optional
1460
+ this.scanner.inURL = false;
1461
+ if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
1462
+ return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
1463
+ }
1464
+ return this.finish(node);
1465
+ }
1466
+ _parseURLArgument() {
1467
+ const node = this.create(nodes.Node);
1468
+ if (!this.accept(cssScanner_1.TokenType.String) && !this.accept(cssScanner_1.TokenType.BadString) && !this.acceptUnquotedString()) {
1469
+ return null;
1470
+ }
1471
+ return this.finish(node);
1472
+ }
1473
+ _parseIdent(referenceTypes) {
1474
+ if (!this.peek(cssScanner_1.TokenType.Ident)) {
1475
+ return null;
1476
+ }
1477
+ const node = this.create(nodes.Identifier);
1478
+ if (referenceTypes) {
1479
+ node.referenceTypes = referenceTypes;
1480
+ }
1481
+ node.isCustomProperty = this.peekRegExp(cssScanner_1.TokenType.Ident, /^--/);
1482
+ this.consumeToken();
1483
+ return this.finish(node);
1484
+ }
1485
+ _parseFunction() {
1486
+ const pos = this.mark();
1487
+ const node = this.create(nodes.Function);
1488
+ if (!node.setIdentifier(this._parseFunctionIdentifier())) {
1489
+ return null;
1490
+ }
1491
+ if (this.hasWhitespace() || !this.accept(cssScanner_1.TokenType.ParenthesisL)) {
1492
+ this.restoreAtMark(pos);
1493
+ return null;
1494
+ }
1495
+ if (node.getArguments().addChild(this._parseFunctionArgument())) {
1496
+ while (this.accept(cssScanner_1.TokenType.Comma)) {
1497
+ if (this.peek(cssScanner_1.TokenType.ParenthesisR)) {
1498
+ break;
1499
+ }
1500
+ if (!node.getArguments().addChild(this._parseFunctionArgument())) {
1501
+ this.markError(node, cssErrors_1.ParseError.ExpressionExpected);
1502
+ }
1503
+ }
1504
+ }
1505
+ if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
1506
+ return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
1507
+ }
1508
+ return this.finish(node);
1509
+ }
1510
+ _parseFunctionIdentifier() {
1511
+ if (!this.peek(cssScanner_1.TokenType.Ident)) {
1512
+ return null;
1513
+ }
1514
+ const node = this.create(nodes.Identifier);
1515
+ node.referenceTypes = [nodes.ReferenceType.Function];
1516
+ if (this.acceptIdent('progid')) {
1517
+ // support for IE7 specific filters: 'progid:DXImageTransform.Microsoft.MotionBlur(strength=13, direction=310)'
1518
+ if (this.accept(cssScanner_1.TokenType.Colon)) {
1519
+ while (this.accept(cssScanner_1.TokenType.Ident) && this.acceptDelim('.')) {
1520
+ // loop
1521
+ }
1522
+ }
1523
+ return this.finish(node);
1524
+ }
1525
+ this.consumeToken();
1526
+ return this.finish(node);
1527
+ }
1528
+ _parseFunctionArgument() {
1529
+ const node = this.create(nodes.FunctionArgument);
1530
+ if (node.setValue(this._parseExpr(true))) {
1531
+ return this.finish(node);
1532
+ }
1533
+ return null;
1534
+ }
1535
+ _parseHexColor() {
1536
+ if (this.peekRegExp(cssScanner_1.TokenType.Hash, /^#([A-Fa-f0-9]{3}|[A-Fa-f0-9]{4}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$/g)) {
1537
+ const node = this.create(nodes.HexColorValue);
1538
+ this.consumeToken();
1539
+ return this.finish(node);
1540
+ }
1541
+ else {
1542
+ return null;
1543
+ }
1544
+ }
1545
+ }
1546
+ exports.Parser = Parser;
1547
+ });