vscode-css-languageservice 5.4.1 → 6.0.1

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