vscode-css-languageservice 6.0.1 → 6.1.0

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