vscode-css-languageservice 6.3.9 → 7.0.0-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/lib/esm/beautify/beautify-css.js +1437 -1606
  3. package/lib/esm/cssLanguageService.d.ts +2 -2
  4. package/lib/esm/cssLanguageService.js +18 -18
  5. package/lib/esm/data/webCustomData.js +13251 -2444
  6. package/lib/esm/languageFacts/builtinData.js +2 -1
  7. package/lib/esm/languageFacts/colors.js +1 -1
  8. package/lib/esm/languageFacts/dataManager.js +3 -3
  9. package/lib/esm/languageFacts/entry.js +1 -1
  10. package/lib/esm/languageFacts/facts.js +3 -3
  11. package/lib/esm/parser/cssErrors.js +1 -0
  12. package/lib/esm/parser/cssNodes.js +1 -1
  13. package/lib/esm/parser/cssParser.js +164 -17
  14. package/lib/esm/parser/cssSymbolScope.js +2 -2
  15. package/lib/esm/parser/lessParser.js +5 -5
  16. package/lib/esm/parser/lessScanner.js +1 -1
  17. package/lib/esm/parser/scssParser.js +64 -6
  18. package/lib/esm/parser/scssScanner.js +1 -1
  19. package/lib/esm/services/cssCodeActions.js +4 -4
  20. package/lib/esm/services/cssCompletion.js +7 -7
  21. package/lib/esm/services/cssFolding.js +3 -3
  22. package/lib/esm/services/cssFormatter.js +3 -3
  23. package/lib/esm/services/cssHover.js +6 -6
  24. package/lib/esm/services/cssNavigation.js +6 -6
  25. package/lib/esm/services/cssSelectionRange.js +2 -2
  26. package/lib/esm/services/cssValidation.js +4 -4
  27. package/lib/esm/services/lessCompletion.js +2 -2
  28. package/lib/esm/services/lint.js +5 -5
  29. package/lib/esm/services/lintRules.js +1 -1
  30. package/lib/esm/services/lintUtil.js +1 -1
  31. package/lib/esm/services/pathCompletion.js +3 -3
  32. package/lib/esm/services/scssCompletion.js +3 -3
  33. package/lib/esm/services/scssNavigation.js +4 -4
  34. package/lib/esm/services/selectorPrinting.js +3 -3
  35. package/package.json +20 -20
  36. package/lib/umd/beautify/beautify-css.js +0 -1695
  37. package/lib/umd/cssLanguageService.d.ts +0 -39
  38. package/lib/umd/cssLanguageService.js +0 -105
  39. package/lib/umd/cssLanguageTypes.d.ts +0 -267
  40. package/lib/umd/cssLanguageTypes.js +0 -89
  41. package/lib/umd/data/webCustomData.js +0 -33806
  42. package/lib/umd/languageFacts/builtinData.js +0 -154
  43. package/lib/umd/languageFacts/colors.js +0 -949
  44. package/lib/umd/languageFacts/dataManager.js +0 -101
  45. package/lib/umd/languageFacts/dataProvider.js +0 -86
  46. package/lib/umd/languageFacts/entry.js +0 -217
  47. package/lib/umd/languageFacts/facts.js +0 -33
  48. package/lib/umd/parser/cssErrors.js +0 -60
  49. package/lib/umd/parser/cssNodes.js +0 -1676
  50. package/lib/umd/parser/cssParser.js +0 -1888
  51. package/lib/umd/parser/cssScanner.js +0 -619
  52. package/lib/umd/parser/cssSymbolScope.js +0 -328
  53. package/lib/umd/parser/lessParser.js +0 -732
  54. package/lib/umd/parser/lessScanner.js +0 -70
  55. package/lib/umd/parser/scssErrors.js +0 -30
  56. package/lib/umd/parser/scssParser.js +0 -816
  57. package/lib/umd/parser/scssScanner.js +0 -108
  58. package/lib/umd/services/cssCodeActions.js +0 -89
  59. package/lib/umd/services/cssCompletion.js +0 -1109
  60. package/lib/umd/services/cssFolding.js +0 -202
  61. package/lib/umd/services/cssFormatter.js +0 -149
  62. package/lib/umd/services/cssHover.js +0 -174
  63. package/lib/umd/services/cssNavigation.js +0 -539
  64. package/lib/umd/services/cssSelectionRange.js +0 -59
  65. package/lib/umd/services/cssValidation.js +0 -54
  66. package/lib/umd/services/lessCompletion.js +0 -390
  67. package/lib/umd/services/lint.js +0 -577
  68. package/lib/umd/services/lintRules.js +0 -90
  69. package/lib/umd/services/lintUtil.js +0 -210
  70. package/lib/umd/services/pathCompletion.js +0 -171
  71. package/lib/umd/services/scssCompletion.js +0 -367
  72. package/lib/umd/services/scssNavigation.js +0 -169
  73. package/lib/umd/services/selectorPrinting.js +0 -575
  74. package/lib/umd/utils/arrays.js +0 -54
  75. package/lib/umd/utils/objects.js +0 -24
  76. package/lib/umd/utils/resources.js +0 -25
  77. package/lib/umd/utils/strings.js +0 -123
@@ -1,1888 +0,0 @@
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._parseScope()
282
- || this._parsePage()
283
- || this._parseFontFace()
284
- || this._parseKeyframe()
285
- || this._parseSupports(isNested)
286
- || this._parseLayer(isNested)
287
- || this._parsePropertyAtRule()
288
- || this._parseViewPort()
289
- || this._parseNamespace()
290
- || this._parseDocument()
291
- || this._parseContainer(isNested)
292
- || this._parseStartingStyleAtRule(isNested)
293
- || this._parseUnknownAtRule();
294
- }
295
- _tryParseRuleset(isNested) {
296
- const mark = this.mark();
297
- if (this._parseSelector(isNested)) {
298
- while (this.accept(cssScanner_1.TokenType.Comma) && this._parseSelector(isNested)) {
299
- // loop
300
- }
301
- if (this.accept(cssScanner_1.TokenType.CurlyL)) {
302
- this.restoreAtMark(mark);
303
- return this._parseRuleset(isNested);
304
- }
305
- }
306
- this.restoreAtMark(mark);
307
- return null;
308
- }
309
- _parseRuleset(isNested = false) {
310
- const node = this.create(nodes.RuleSet);
311
- const selectors = node.getSelectors();
312
- if (!selectors.addChild(this._parseSelector(isNested))) {
313
- return null;
314
- }
315
- while (this.accept(cssScanner_1.TokenType.Comma)) {
316
- if (!selectors.addChild(this._parseSelector(isNested))) {
317
- return this.finish(node, cssErrors_1.ParseError.SelectorExpected);
318
- }
319
- }
320
- return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
321
- }
322
- _parseRuleSetDeclarationAtStatement() {
323
- return this._parseMedia(true)
324
- || this._parseScope()
325
- || this._parseSupports(true)
326
- || this._parseLayer(true)
327
- || this._parseContainer(true)
328
- || this._parseStartingStyleAtRule(true)
329
- || this._parseUnknownAtRule();
330
- }
331
- _parseRuleSetDeclaration() {
332
- // https://www.w3.org/TR/css-syntax-3/#consume-a-list-of-declarations
333
- if (this.peek(cssScanner_1.TokenType.AtKeyword)) {
334
- return this._parseRuleSetDeclarationAtStatement();
335
- }
336
- if (!this.peek(cssScanner_1.TokenType.Ident)) {
337
- return this._parseRuleset(true);
338
- }
339
- return this._tryParseRuleset(true) || this._parseDeclaration();
340
- }
341
- _needsSemicolonAfter(node) {
342
- switch (node.type) {
343
- case nodes.NodeType.Keyframe:
344
- case nodes.NodeType.ViewPort:
345
- case nodes.NodeType.Media:
346
- case nodes.NodeType.Ruleset:
347
- case nodes.NodeType.Namespace:
348
- case nodes.NodeType.If:
349
- case nodes.NodeType.For:
350
- case nodes.NodeType.Each:
351
- case nodes.NodeType.While:
352
- case nodes.NodeType.MixinDeclaration:
353
- case nodes.NodeType.FunctionDeclaration:
354
- case nodes.NodeType.MixinContentDeclaration:
355
- case nodes.NodeType.Scope:
356
- return false;
357
- case nodes.NodeType.ExtendsReference:
358
- case nodes.NodeType.MixinContentReference:
359
- case nodes.NodeType.ReturnStatement:
360
- case nodes.NodeType.MediaQuery:
361
- case nodes.NodeType.Debug:
362
- case nodes.NodeType.Import:
363
- case nodes.NodeType.AtApplyRule:
364
- case nodes.NodeType.CustomPropertyDeclaration:
365
- return true;
366
- case nodes.NodeType.VariableDeclaration:
367
- return node.needsSemicolon;
368
- case nodes.NodeType.MixinReference:
369
- return !node.getContent();
370
- case nodes.NodeType.Declaration:
371
- return !node.getNestedProperties();
372
- }
373
- return false;
374
- }
375
- _parseDeclarations(parseDeclaration) {
376
- const node = this.create(nodes.Declarations);
377
- if (!this.accept(cssScanner_1.TokenType.CurlyL)) {
378
- return null;
379
- }
380
- let decl = parseDeclaration();
381
- while (node.addChild(decl)) {
382
- if (this.peek(cssScanner_1.TokenType.CurlyR)) {
383
- break;
384
- }
385
- if (this._needsSemicolonAfter(decl) && !this.accept(cssScanner_1.TokenType.SemiColon)) {
386
- return this.finish(node, cssErrors_1.ParseError.SemiColonExpected, [cssScanner_1.TokenType.SemiColon, cssScanner_1.TokenType.CurlyR]);
387
- }
388
- // We accepted semicolon token. Link it to declaration.
389
- if (decl && this.prevToken && this.prevToken.type === cssScanner_1.TokenType.SemiColon) {
390
- decl.semicolonPosition = this.prevToken.offset;
391
- }
392
- while (this.accept(cssScanner_1.TokenType.SemiColon)) {
393
- // accept empty statements
394
- }
395
- decl = parseDeclaration();
396
- }
397
- if (!this.accept(cssScanner_1.TokenType.CurlyR)) {
398
- return this.finish(node, cssErrors_1.ParseError.RightCurlyExpected, [cssScanner_1.TokenType.CurlyR, cssScanner_1.TokenType.SemiColon]);
399
- }
400
- return this.finish(node);
401
- }
402
- _parseBody(node, parseDeclaration) {
403
- if (!node.setDeclarations(this._parseDeclarations(parseDeclaration))) {
404
- return this.finish(node, cssErrors_1.ParseError.LeftCurlyExpected, [cssScanner_1.TokenType.CurlyR, cssScanner_1.TokenType.SemiColon]);
405
- }
406
- return this.finish(node);
407
- }
408
- _parseSelector(isNested) {
409
- const node = this.create(nodes.Selector);
410
- let hasContent = false;
411
- if (isNested) {
412
- // nested selectors can start with a combinator
413
- hasContent = node.addChild(this._parseCombinator());
414
- }
415
- while (node.addChild(this._parseSimpleSelector())) {
416
- hasContent = true;
417
- node.addChild(this._parseCombinator()); // optional
418
- }
419
- return hasContent ? this.finish(node) : null;
420
- }
421
- _parseDeclaration(stopTokens) {
422
- const customProperty = this._tryParseCustomPropertyDeclaration(stopTokens);
423
- if (customProperty) {
424
- return customProperty;
425
- }
426
- const node = this.create(nodes.Declaration);
427
- if (!node.setProperty(this._parseProperty())) {
428
- return null;
429
- }
430
- if (!this.accept(cssScanner_1.TokenType.Colon)) {
431
- return this.finish(node, cssErrors_1.ParseError.ColonExpected, [cssScanner_1.TokenType.Colon], stopTokens || [cssScanner_1.TokenType.SemiColon]);
432
- }
433
- if (this.prevToken) {
434
- node.colonPosition = this.prevToken.offset;
435
- }
436
- if (!node.setValue(this._parseExpr())) {
437
- return this.finish(node, cssErrors_1.ParseError.PropertyValueExpected);
438
- }
439
- node.addChild(this._parsePrio());
440
- if (this.peek(cssScanner_1.TokenType.SemiColon)) {
441
- node.semicolonPosition = this.token.offset; // not part of the declaration, but useful information for code assist
442
- }
443
- return this.finish(node);
444
- }
445
- _tryParseCustomPropertyDeclaration(stopTokens) {
446
- if (!this.peekRegExp(cssScanner_1.TokenType.Ident, /^--/)) {
447
- return null;
448
- }
449
- const node = this.create(nodes.CustomPropertyDeclaration);
450
- if (!node.setProperty(this._parseProperty())) {
451
- return null;
452
- }
453
- if (!this.accept(cssScanner_1.TokenType.Colon)) {
454
- return this.finish(node, cssErrors_1.ParseError.ColonExpected, [cssScanner_1.TokenType.Colon]);
455
- }
456
- if (this.prevToken) {
457
- node.colonPosition = this.prevToken.offset;
458
- }
459
- const mark = this.mark();
460
- if (this.peek(cssScanner_1.TokenType.CurlyL)) {
461
- // try to parse it as nested declaration
462
- const propertySet = this.create(nodes.CustomPropertySet);
463
- const declarations = this._parseDeclarations(this._parseRuleSetDeclaration.bind(this));
464
- if (propertySet.setDeclarations(declarations) && !declarations.isErroneous(true)) {
465
- propertySet.addChild(this._parsePrio());
466
- if (this.peek(cssScanner_1.TokenType.SemiColon)) {
467
- this.finish(propertySet);
468
- node.setPropertySet(propertySet);
469
- node.semicolonPosition = this.token.offset; // not part of the declaration, but useful information for code assist
470
- return this.finish(node);
471
- }
472
- }
473
- this.restoreAtMark(mark);
474
- }
475
- // try to parse as expression
476
- const expression = this._parseExpr();
477
- if (expression && !expression.isErroneous(true)) {
478
- this._parsePrio();
479
- if (this.peekOne(...(stopTokens || []), cssScanner_1.TokenType.SemiColon, cssScanner_1.TokenType.EOF)) {
480
- node.setValue(expression);
481
- if (this.peek(cssScanner_1.TokenType.SemiColon)) {
482
- node.semicolonPosition = this.token.offset; // not part of the declaration, but useful information for code assist
483
- }
484
- return this.finish(node);
485
- }
486
- }
487
- this.restoreAtMark(mark);
488
- node.addChild(this._parseCustomPropertyValue(stopTokens));
489
- node.addChild(this._parsePrio());
490
- if ((0, objects_1.isDefined)(node.colonPosition) && this.token.offset === node.colonPosition + 1) {
491
- return this.finish(node, cssErrors_1.ParseError.PropertyValueExpected);
492
- }
493
- return this.finish(node);
494
- }
495
- /**
496
- * Parse custom property values.
497
- *
498
- * Based on https://www.w3.org/TR/css-variables/#syntax
499
- *
500
- * This code is somewhat unusual, as the allowed syntax is incredibly broad,
501
- * parsing almost any sequence of tokens, save for a small set of exceptions.
502
- * Unbalanced delimitors, invalid tokens, and declaration
503
- * terminators like semicolons and !important directives (when not inside
504
- * of delimitors).
505
- */
506
- _parseCustomPropertyValue(stopTokens = [cssScanner_1.TokenType.CurlyR]) {
507
- const node = this.create(nodes.Node);
508
- const isTopLevel = () => curlyDepth === 0 && parensDepth === 0 && bracketsDepth === 0;
509
- const onStopToken = () => stopTokens.indexOf(this.token.type) !== -1;
510
- let curlyDepth = 0;
511
- let parensDepth = 0;
512
- let bracketsDepth = 0;
513
- done: while (true) {
514
- switch (this.token.type) {
515
- case cssScanner_1.TokenType.SemiColon:
516
- // A semicolon only ends things if we're not inside a delimitor.
517
- if (isTopLevel()) {
518
- break done;
519
- }
520
- break;
521
- case cssScanner_1.TokenType.Exclamation:
522
- // An exclamation ends the value if we're not inside delims.
523
- if (isTopLevel()) {
524
- break done;
525
- }
526
- break;
527
- case cssScanner_1.TokenType.CurlyL:
528
- curlyDepth++;
529
- break;
530
- case cssScanner_1.TokenType.CurlyR:
531
- curlyDepth--;
532
- if (curlyDepth < 0) {
533
- // The property value has been terminated without a semicolon, and
534
- // this is the last declaration in the ruleset.
535
- if (onStopToken() && parensDepth === 0 && bracketsDepth === 0) {
536
- break done;
537
- }
538
- return this.finish(node, cssErrors_1.ParseError.LeftCurlyExpected);
539
- }
540
- break;
541
- case cssScanner_1.TokenType.ParenthesisL:
542
- parensDepth++;
543
- break;
544
- case cssScanner_1.TokenType.ParenthesisR:
545
- parensDepth--;
546
- if (parensDepth < 0) {
547
- if (onStopToken() && bracketsDepth === 0 && curlyDepth === 0) {
548
- break done;
549
- }
550
- return this.finish(node, cssErrors_1.ParseError.LeftParenthesisExpected);
551
- }
552
- break;
553
- case cssScanner_1.TokenType.BracketL:
554
- bracketsDepth++;
555
- break;
556
- case cssScanner_1.TokenType.BracketR:
557
- bracketsDepth--;
558
- if (bracketsDepth < 0) {
559
- return this.finish(node, cssErrors_1.ParseError.LeftSquareBracketExpected);
560
- }
561
- break;
562
- case cssScanner_1.TokenType.BadString: // fall through
563
- break done;
564
- case cssScanner_1.TokenType.EOF:
565
- // We shouldn't have reached the end of input, something is
566
- // unterminated.
567
- let error = cssErrors_1.ParseError.RightCurlyExpected;
568
- if (bracketsDepth > 0) {
569
- error = cssErrors_1.ParseError.RightSquareBracketExpected;
570
- }
571
- else if (parensDepth > 0) {
572
- error = cssErrors_1.ParseError.RightParenthesisExpected;
573
- }
574
- return this.finish(node, error);
575
- }
576
- this.consumeToken();
577
- }
578
- return this.finish(node);
579
- }
580
- _tryToParseDeclaration(stopTokens) {
581
- const mark = this.mark();
582
- if (this._parseProperty() && this.accept(cssScanner_1.TokenType.Colon)) {
583
- // looks like a declaration, go ahead
584
- this.restoreAtMark(mark);
585
- return this._parseDeclaration(stopTokens);
586
- }
587
- this.restoreAtMark(mark);
588
- return null;
589
- }
590
- _parseProperty() {
591
- const node = this.create(nodes.Property);
592
- const mark = this.mark();
593
- if (this.acceptDelim('*') || this.acceptDelim('_')) {
594
- // support for IE 5.x, 6 and 7 star hack: see http://en.wikipedia.org/wiki/CSS_filter#Star_hack
595
- if (this.hasWhitespace()) {
596
- this.restoreAtMark(mark);
597
- return null;
598
- }
599
- }
600
- if (node.setIdentifier(this._parsePropertyIdentifier())) {
601
- return this.finish(node);
602
- }
603
- return null;
604
- }
605
- _parsePropertyIdentifier() {
606
- return this._parseIdent();
607
- }
608
- _parseCharset() {
609
- if (!this.peek(cssScanner_1.TokenType.Charset)) {
610
- return null;
611
- }
612
- const node = this.create(nodes.Node);
613
- this.consumeToken(); // charset
614
- if (!this.accept(cssScanner_1.TokenType.String)) {
615
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
616
- }
617
- if (!this.accept(cssScanner_1.TokenType.SemiColon)) {
618
- return this.finish(node, cssErrors_1.ParseError.SemiColonExpected);
619
- }
620
- return this.finish(node);
621
- }
622
- _parseImport() {
623
- // @import [ <url> | <string> ]
624
- // [ layer | layer(<layer-name>) ]?
625
- // <import-condition> ;
626
- // <import-conditions> = [ supports( [ <supports-condition> | <declaration> ] ) ]?
627
- // <media-query-list>?
628
- if (!this.peekKeyword('@import')) {
629
- return null;
630
- }
631
- const node = this.create(nodes.Import);
632
- this.consumeToken(); // @import
633
- if (!node.addChild(this._parseURILiteral()) && !node.addChild(this._parseStringLiteral())) {
634
- return this.finish(node, cssErrors_1.ParseError.URIOrStringExpected);
635
- }
636
- return this._completeParseImport(node);
637
- }
638
- _completeParseImport(node) {
639
- if (this.acceptIdent('layer')) {
640
- if (this.accept(cssScanner_1.TokenType.ParenthesisL)) {
641
- if (!node.addChild(this._parseLayerName())) {
642
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected, [cssScanner_1.TokenType.SemiColon]);
643
- }
644
- if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
645
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected, [cssScanner_1.TokenType.ParenthesisR], []);
646
- }
647
- }
648
- }
649
- if (this.acceptIdent('supports')) {
650
- if (this.accept(cssScanner_1.TokenType.ParenthesisL)) {
651
- node.addChild(this._tryToParseDeclaration() || this._parseSupportsCondition());
652
- if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
653
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected, [cssScanner_1.TokenType.ParenthesisR], []);
654
- }
655
- }
656
- }
657
- if (!this.peek(cssScanner_1.TokenType.SemiColon) && !this.peek(cssScanner_1.TokenType.EOF)) {
658
- node.setMedialist(this._parseMediaQueryList());
659
- }
660
- return this.finish(node);
661
- }
662
- _parseNamespace() {
663
- // http://www.w3.org/TR/css3-namespace/
664
- // namespace : NAMESPACE_SYM S* [IDENT S*]? [STRING|URI] S* ';' S*
665
- if (!this.peekKeyword('@namespace')) {
666
- return null;
667
- }
668
- const node = this.create(nodes.Namespace);
669
- this.consumeToken(); // @namespace
670
- if (!node.addChild(this._parseURILiteral())) { // url literal also starts with ident
671
- node.addChild(this._parseIdent()); // optional prefix
672
- if (!node.addChild(this._parseURILiteral()) && !node.addChild(this._parseStringLiteral())) {
673
- return this.finish(node, cssErrors_1.ParseError.URIExpected, [cssScanner_1.TokenType.SemiColon]);
674
- }
675
- }
676
- if (!this.accept(cssScanner_1.TokenType.SemiColon)) {
677
- return this.finish(node, cssErrors_1.ParseError.SemiColonExpected);
678
- }
679
- return this.finish(node);
680
- }
681
- _parseFontFace() {
682
- if (!this.peekKeyword('@font-face')) {
683
- return null;
684
- }
685
- const node = this.create(nodes.FontFace);
686
- this.consumeToken(); // @font-face
687
- return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
688
- }
689
- _parseViewPort() {
690
- if (!this.peekKeyword('@-ms-viewport') &&
691
- !this.peekKeyword('@-o-viewport') &&
692
- !this.peekKeyword('@viewport')) {
693
- return null;
694
- }
695
- const node = this.create(nodes.ViewPort);
696
- this.consumeToken(); // @-ms-viewport
697
- return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
698
- }
699
- _parseKeyframe() {
700
- if (!this.peekRegExp(cssScanner_1.TokenType.AtKeyword, this.keyframeRegex)) {
701
- return null;
702
- }
703
- const node = this.create(nodes.Keyframe);
704
- const atNode = this.create(nodes.Node);
705
- this.consumeToken(); // atkeyword
706
- node.setKeyword(this.finish(atNode));
707
- if (atNode.matches('@-ms-keyframes')) { // -ms-keyframes never existed
708
- this.markError(atNode, cssErrors_1.ParseError.UnknownKeyword);
709
- }
710
- if (!node.setIdentifier(this._parseKeyframeIdent())) {
711
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected, [cssScanner_1.TokenType.CurlyR]);
712
- }
713
- return this._parseBody(node, this._parseKeyframeSelector.bind(this));
714
- }
715
- _parseKeyframeIdent() {
716
- return this._parseIdent([nodes.ReferenceType.Keyframe]);
717
- }
718
- _parseKeyframeSelector() {
719
- const node = this.create(nodes.KeyframeSelector);
720
- let hasContent = false;
721
- if (node.addChild(this._parseIdent())) {
722
- hasContent = true;
723
- }
724
- if (this.accept(cssScanner_1.TokenType.Percentage)) {
725
- hasContent = true;
726
- }
727
- if (!hasContent) {
728
- return null;
729
- }
730
- while (this.accept(cssScanner_1.TokenType.Comma)) {
731
- hasContent = false;
732
- if (node.addChild(this._parseIdent())) {
733
- hasContent = true;
734
- }
735
- if (this.accept(cssScanner_1.TokenType.Percentage)) {
736
- hasContent = true;
737
- }
738
- if (!hasContent) {
739
- return this.finish(node, cssErrors_1.ParseError.PercentageExpected);
740
- }
741
- }
742
- return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
743
- }
744
- _tryParseKeyframeSelector() {
745
- const node = this.create(nodes.KeyframeSelector);
746
- const pos = this.mark();
747
- let hasContent = false;
748
- if (node.addChild(this._parseIdent())) {
749
- hasContent = true;
750
- }
751
- if (this.accept(cssScanner_1.TokenType.Percentage)) {
752
- hasContent = true;
753
- }
754
- if (!hasContent) {
755
- return null;
756
- }
757
- while (this.accept(cssScanner_1.TokenType.Comma)) {
758
- hasContent = false;
759
- if (node.addChild(this._parseIdent())) {
760
- hasContent = true;
761
- }
762
- if (this.accept(cssScanner_1.TokenType.Percentage)) {
763
- hasContent = true;
764
- }
765
- if (!hasContent) {
766
- this.restoreAtMark(pos);
767
- return null;
768
- }
769
- }
770
- if (!this.peek(cssScanner_1.TokenType.CurlyL)) {
771
- this.restoreAtMark(pos);
772
- return null;
773
- }
774
- return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
775
- }
776
- _parsePropertyAtRule() {
777
- // @property <custom-property-name> {
778
- // <declaration-list>
779
- // }
780
- if (!this.peekKeyword('@property')) {
781
- return null;
782
- }
783
- const node = this.create(nodes.PropertyAtRule);
784
- this.consumeToken(); // @layer
785
- if (!this.peekRegExp(cssScanner_1.TokenType.Ident, /^--/) || !node.setName(this._parseIdent([nodes.ReferenceType.Property]))) {
786
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
787
- }
788
- return this._parseBody(node, this._parseDeclaration.bind(this));
789
- }
790
- _parseStartingStyleAtRule(isNested = false) {
791
- if (!this.peekKeyword("@starting-style")) {
792
- return null;
793
- }
794
- const node = this.create(nodes.StartingStyleAtRule);
795
- this.consumeToken(); // @starting-style
796
- return this._parseBody(node, this._parseStartingStyleDeclaration.bind(this, isNested));
797
- }
798
- // this method is the same as ._parseContainerDeclaration()
799
- // which is the same as ._parseMediaDeclaration(),
800
- // _parseSupportsDeclaration, and ._parseLayerDeclaration()
801
- _parseStartingStyleDeclaration(isNested = false) {
802
- if (isNested) {
803
- // if nested, the body can contain rulesets, but also declarations
804
- return this._tryParseRuleset(true)
805
- || this._tryToParseDeclaration()
806
- || this._parseStylesheetStatement(true);
807
- }
808
- return this._parseStylesheetStatement(false);
809
- }
810
- _parseLayer(isNested = false) {
811
- // @layer layer-name {rules}
812
- // @layer layer-name;
813
- // @layer layer-name, layer-name, layer-name;
814
- // @layer {rules}
815
- if (!this.peekKeyword('@layer')) {
816
- return null;
817
- }
818
- const node = this.create(nodes.Layer);
819
- this.consumeToken(); // @layer
820
- const names = this._parseLayerNameList();
821
- if (names) {
822
- node.setNames(names);
823
- }
824
- if ((!names || names.getChildren().length === 1) && this.peek(cssScanner_1.TokenType.CurlyL)) {
825
- return this._parseBody(node, this._parseLayerDeclaration.bind(this, isNested));
826
- }
827
- if (!this.accept(cssScanner_1.TokenType.SemiColon)) {
828
- return this.finish(node, cssErrors_1.ParseError.SemiColonExpected);
829
- }
830
- return this.finish(node);
831
- }
832
- _parseLayerDeclaration(isNested = false) {
833
- if (isNested) {
834
- // if nested, the body can contain rulesets, but also declarations
835
- return this._tryParseRuleset(true)
836
- || this._tryToParseDeclaration()
837
- || this._parseStylesheetStatement(true);
838
- }
839
- return this._parseStylesheetStatement(false);
840
- }
841
- _parseLayerNameList() {
842
- const node = this.createNode(nodes.NodeType.LayerNameList);
843
- if (!node.addChild(this._parseLayerName())) {
844
- return null;
845
- }
846
- while (this.accept(cssScanner_1.TokenType.Comma)) {
847
- if (!node.addChild(this._parseLayerName())) {
848
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
849
- }
850
- }
851
- return this.finish(node);
852
- }
853
- _parseLayerName() {
854
- // <layer-name> = <ident> [ '.' <ident> ]*
855
- const node = this.createNode(nodes.NodeType.LayerName);
856
- if (!node.addChild(this._parseIdent())) {
857
- return null;
858
- }
859
- while (!this.hasWhitespace() && this.acceptDelim('.')) {
860
- if (this.hasWhitespace() || !node.addChild(this._parseIdent())) {
861
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
862
- }
863
- }
864
- return this.finish(node);
865
- }
866
- _parseSupports(isNested = false) {
867
- // SUPPORTS_SYM S* supports_condition '{' S* ruleset* '}' S*
868
- if (!this.peekKeyword('@supports')) {
869
- return null;
870
- }
871
- const node = this.create(nodes.Supports);
872
- this.consumeToken(); // @supports
873
- node.addChild(this._parseSupportsCondition());
874
- return this._parseBody(node, this._parseSupportsDeclaration.bind(this, isNested));
875
- }
876
- _parseSupportsDeclaration(isNested = false) {
877
- if (isNested) {
878
- // if nested, the body can contain rulesets, but also declarations
879
- return this._tryParseRuleset(true)
880
- || this._tryToParseDeclaration()
881
- || this._parseStylesheetStatement(true);
882
- }
883
- return this._parseStylesheetStatement(false);
884
- }
885
- _parseSupportsCondition() {
886
- // supports_condition : supports_negation | supports_conjunction | supports_disjunction | supports_condition_in_parens ;
887
- // supports_condition_in_parens: ( '(' S* supports_condition S* ')' ) | supports_declaration_condition | general_enclosed ;
888
- // supports_negation: NOT S+ supports_condition_in_parens ;
889
- // supports_conjunction: supports_condition_in_parens ( S+ AND S+ supports_condition_in_parens )+;
890
- // supports_disjunction: supports_condition_in_parens ( S+ OR S+ supports_condition_in_parens )+;
891
- // supports_declaration_condition: '(' S* declaration ')';
892
- // general_enclosed: ( FUNCTION | '(' ) ( any | unused )* ')' ;
893
- const node = this.create(nodes.SupportsCondition);
894
- if (this.acceptIdent('not')) {
895
- node.addChild(this._parseSupportsConditionInParens());
896
- }
897
- else {
898
- node.addChild(this._parseSupportsConditionInParens());
899
- if (this.peekRegExp(cssScanner_1.TokenType.Ident, /^(and|or)$/i)) {
900
- const text = this.token.text.toLowerCase();
901
- while (this.acceptIdent(text)) {
902
- node.addChild(this._parseSupportsConditionInParens());
903
- }
904
- }
905
- }
906
- return this.finish(node);
907
- }
908
- _parseSupportsConditionInParens() {
909
- const node = this.create(nodes.SupportsCondition);
910
- if (this.accept(cssScanner_1.TokenType.ParenthesisL)) {
911
- if (this.prevToken) {
912
- node.lParent = this.prevToken.offset;
913
- }
914
- if (!node.addChild(this._tryToParseDeclaration([cssScanner_1.TokenType.ParenthesisR]))) {
915
- if (!this._parseSupportsCondition()) {
916
- return this.finish(node, cssErrors_1.ParseError.ConditionExpected);
917
- }
918
- }
919
- if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
920
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected, [cssScanner_1.TokenType.ParenthesisR], []);
921
- }
922
- if (this.prevToken) {
923
- node.rParent = this.prevToken.offset;
924
- }
925
- return this.finish(node);
926
- }
927
- else if (this.peek(cssScanner_1.TokenType.Ident)) {
928
- const pos = this.mark();
929
- this.consumeToken();
930
- if (!this.hasWhitespace() && this.accept(cssScanner_1.TokenType.ParenthesisL)) {
931
- let openParentCount = 1;
932
- while (this.token.type !== cssScanner_1.TokenType.EOF && openParentCount !== 0) {
933
- if (this.token.type === cssScanner_1.TokenType.ParenthesisL) {
934
- openParentCount++;
935
- }
936
- else if (this.token.type === cssScanner_1.TokenType.ParenthesisR) {
937
- openParentCount--;
938
- }
939
- this.consumeToken();
940
- }
941
- return this.finish(node);
942
- }
943
- else {
944
- this.restoreAtMark(pos);
945
- }
946
- }
947
- return this.finish(node, cssErrors_1.ParseError.LeftParenthesisExpected, [], [cssScanner_1.TokenType.ParenthesisL]);
948
- }
949
- _parseMediaDeclaration(isNested = false) {
950
- if (isNested) {
951
- // if nested, the body can contain rulesets, but also declarations
952
- return this._tryParseRuleset(true)
953
- || this._tryToParseDeclaration()
954
- || this._parseStylesheetStatement(true);
955
- }
956
- return this._parseStylesheetStatement(false);
957
- }
958
- _parseMedia(isNested = false) {
959
- // MEDIA_SYM S* media_query_list '{' S* ruleset* '}' S*
960
- // media_query_list : S* [media_query [ ',' S* media_query ]* ]?
961
- if (!this.peekKeyword('@media')) {
962
- return null;
963
- }
964
- const node = this.create(nodes.Media);
965
- this.consumeToken(); // @media
966
- if (!node.addChild(this._parseMediaQueryList())) {
967
- return this.finish(node, cssErrors_1.ParseError.MediaQueryExpected);
968
- }
969
- return this._parseBody(node, this._parseMediaDeclaration.bind(this, isNested));
970
- }
971
- _parseMediaQueryList() {
972
- const node = this.create(nodes.Medialist);
973
- if (!node.addChild(this._parseMediaQuery())) {
974
- return this.finish(node, cssErrors_1.ParseError.MediaQueryExpected);
975
- }
976
- while (this.accept(cssScanner_1.TokenType.Comma)) {
977
- if (!node.addChild(this._parseMediaQuery())) {
978
- return this.finish(node, cssErrors_1.ParseError.MediaQueryExpected);
979
- }
980
- }
981
- return this.finish(node);
982
- }
983
- _parseMediaQuery() {
984
- // <media-query> = <media-condition> | [ not | only ]? <media-type> [ and <media-condition-without-or> ]?
985
- const node = this.create(nodes.MediaQuery);
986
- const pos = this.mark();
987
- this.acceptIdent('not');
988
- if (!this.peek(cssScanner_1.TokenType.ParenthesisL)) {
989
- if (this.acceptIdent('only')) {
990
- // optional
991
- }
992
- if (!node.addChild(this._parseIdent())) {
993
- return null;
994
- }
995
- if (this.acceptIdent('and')) {
996
- node.addChild(this._parseMediaCondition());
997
- }
998
- }
999
- else {
1000
- this.restoreAtMark(pos); // 'not' is part of the MediaCondition
1001
- node.addChild(this._parseMediaCondition());
1002
- }
1003
- return this.finish(node);
1004
- }
1005
- _parseRatio() {
1006
- const pos = this.mark();
1007
- const node = this.create(nodes.RatioValue);
1008
- if (!this._parseNumeric()) {
1009
- return null;
1010
- }
1011
- if (!this.acceptDelim('/')) {
1012
- this.restoreAtMark(pos);
1013
- return null;
1014
- }
1015
- if (!this._parseNumeric()) {
1016
- return this.finish(node, cssErrors_1.ParseError.NumberExpected);
1017
- }
1018
- return this.finish(node);
1019
- }
1020
- _parseMediaCondition() {
1021
- // <media-condition> = <media-not> | <media-and> | <media-or> | <media-in-parens>
1022
- // <media-not> = not <media-in-parens>
1023
- // <media-and> = <media-in-parens> [ and <media-in-parens> ]+
1024
- // <media-or> = <media-in-parens> [ or <media-in-parens> ]+
1025
- // <media-in-parens> = ( <media-condition> ) | <media-feature> | <general-enclosed>
1026
- const node = this.create(nodes.MediaCondition);
1027
- this.acceptIdent('not');
1028
- let parseExpression = true;
1029
- while (parseExpression) {
1030
- if (!this.accept(cssScanner_1.TokenType.ParenthesisL)) {
1031
- return this.finish(node, cssErrors_1.ParseError.LeftParenthesisExpected, [], [cssScanner_1.TokenType.CurlyL]);
1032
- }
1033
- if (this.peek(cssScanner_1.TokenType.ParenthesisL) || this.peekIdent('not')) {
1034
- // <media-condition>
1035
- node.addChild(this._parseMediaCondition());
1036
- }
1037
- else {
1038
- node.addChild(this._parseMediaFeature());
1039
- }
1040
- // not yet implemented: general enclosed
1041
- if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
1042
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected, [], [cssScanner_1.TokenType.CurlyL]);
1043
- }
1044
- parseExpression = this.acceptIdent('and') || this.acceptIdent('or');
1045
- }
1046
- return this.finish(node);
1047
- }
1048
- _parseMediaFeature() {
1049
- const resyncStopToken = [cssScanner_1.TokenType.ParenthesisR];
1050
- const node = this.create(nodes.MediaFeature);
1051
- // <media-feature> = ( [ <mf-plain> | <mf-boolean> | <mf-range> ] )
1052
- // <mf-plain> = <mf-name> : <mf-value>
1053
- // <mf-boolean> = <mf-name>
1054
- // <mf-range> = <mf-name> [ '<' | '>' ]? '='? <mf-value> | <mf-value> [ '<' | '>' ]? '='? <mf-name> | <mf-value> '<' '='? <mf-name> '<' '='? <mf-value> | <mf-value> '>' '='? <mf-name> '>' '='? <mf-value>
1055
- if (node.addChild(this._parseMediaFeatureName())) {
1056
- if (this.accept(cssScanner_1.TokenType.Colon)) {
1057
- if (!node.addChild(this._parseMediaFeatureValue())) {
1058
- return this.finish(node, cssErrors_1.ParseError.TermExpected, [], resyncStopToken);
1059
- }
1060
- }
1061
- else if (this._parseMediaFeatureRangeOperator()) {
1062
- if (!node.addChild(this._parseMediaFeatureValue())) {
1063
- return this.finish(node, cssErrors_1.ParseError.TermExpected, [], resyncStopToken);
1064
- }
1065
- if (this._parseMediaFeatureRangeOperator()) {
1066
- if (!node.addChild(this._parseMediaFeatureValue())) {
1067
- return this.finish(node, cssErrors_1.ParseError.TermExpected, [], resyncStopToken);
1068
- }
1069
- }
1070
- }
1071
- else {
1072
- // <mf-boolean> = <mf-name>
1073
- }
1074
- }
1075
- else if (node.addChild(this._parseMediaFeatureValue())) {
1076
- if (!this._parseMediaFeatureRangeOperator()) {
1077
- return this.finish(node, cssErrors_1.ParseError.OperatorExpected, [], resyncStopToken);
1078
- }
1079
- if (!node.addChild(this._parseMediaFeatureName())) {
1080
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected, [], resyncStopToken);
1081
- }
1082
- if (this._parseMediaFeatureRangeOperator()) {
1083
- if (!node.addChild(this._parseMediaFeatureValue())) {
1084
- return this.finish(node, cssErrors_1.ParseError.TermExpected, [], resyncStopToken);
1085
- }
1086
- }
1087
- }
1088
- else {
1089
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected, [], resyncStopToken);
1090
- }
1091
- return this.finish(node);
1092
- }
1093
- _parseMediaFeatureRangeOperator() {
1094
- if (this.acceptDelim('<') || this.acceptDelim('>')) {
1095
- if (!this.hasWhitespace()) {
1096
- this.acceptDelim('=');
1097
- }
1098
- return true;
1099
- }
1100
- else if (this.acceptDelim('=')) {
1101
- return true;
1102
- }
1103
- return false;
1104
- }
1105
- _parseMediaFeatureName() {
1106
- return this._parseIdent();
1107
- }
1108
- _parseMediaFeatureValue() {
1109
- return this._parseRatio() || this._parseTermExpression();
1110
- }
1111
- _parseScope() {
1112
- // @scope [<scope-limits>]? { <block-contents> }
1113
- if (!this.peekKeyword('@scope')) {
1114
- return null;
1115
- }
1116
- const node = this.create(nodes.Scope);
1117
- // @scope
1118
- this.consumeToken();
1119
- node.addChild(this._parseScopeLimits());
1120
- return this._parseBody(node, this._parseScopeDeclaration.bind(this));
1121
- }
1122
- _parseScopeDeclaration() {
1123
- // Treat as nested as regular declarations are implicity wrapped with :where(:scope)
1124
- // https://github.com/w3c/csswg-drafts/issues/10389
1125
- // pseudo-selectors implicitly target :scope
1126
- // https://drafts.csswg.org/css-cascade-6/#scoped-rules
1127
- const isNested = true;
1128
- return this._tryParseRuleset(isNested)
1129
- || this._tryToParseDeclaration()
1130
- || this._parseStylesheetStatement(isNested);
1131
- }
1132
- _parseScopeLimits() {
1133
- // [(<scope-start>)]? [to (<scope-end>)]?
1134
- const node = this.create(nodes.ScopeLimits);
1135
- // [(<scope-start>)]?
1136
- if (this.accept(cssScanner_1.TokenType.ParenthesisL)) {
1137
- // scope-start selector can start with a combinator as it defaults to :scope
1138
- // Treat as nested
1139
- if (!node.setScopeStart(this._parseSelector(true))) {
1140
- return this.finish(node, cssErrors_1.ParseError.SelectorExpected, [], [cssScanner_1.TokenType.ParenthesisR]);
1141
- }
1142
- if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
1143
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected, [], [cssScanner_1.TokenType.CurlyL]);
1144
- }
1145
- }
1146
- // [to (<scope-end>)]?
1147
- if (this.acceptIdent('to')) {
1148
- if (!this.accept(cssScanner_1.TokenType.ParenthesisL)) {
1149
- return this.finish(node, cssErrors_1.ParseError.LeftParenthesisExpected, [], [cssScanner_1.TokenType.CurlyL]);
1150
- }
1151
- // 'to' selector can start with a combinator as it defaults to :scope
1152
- // Treat as nested
1153
- if (!node.setScopeEnd(this._parseSelector(true))) {
1154
- return this.finish(node, cssErrors_1.ParseError.SelectorExpected, [], [cssScanner_1.TokenType.ParenthesisR]);
1155
- }
1156
- if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
1157
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected, [], [cssScanner_1.TokenType.CurlyL]);
1158
- }
1159
- }
1160
- return this.finish(node);
1161
- }
1162
- _parseMedium() {
1163
- const node = this.create(nodes.Node);
1164
- if (node.addChild(this._parseIdent())) {
1165
- return this.finish(node);
1166
- }
1167
- else {
1168
- return null;
1169
- }
1170
- }
1171
- _parsePageDeclaration() {
1172
- return this._parsePageMarginBox() || this._parseRuleSetDeclaration();
1173
- }
1174
- _parsePage() {
1175
- // http://www.w3.org/TR/css3-page/
1176
- // page_rule : PAGE_SYM S* page_selector_list '{' S* page_body '}' S*
1177
- // page_body : /* Can be empty */ declaration? [ ';' S* page_body ]? | page_margin_box page_body
1178
- if (!this.peekKeyword('@page')) {
1179
- return null;
1180
- }
1181
- const node = this.create(nodes.Page);
1182
- this.consumeToken();
1183
- if (node.addChild(this._parsePageSelector())) {
1184
- while (this.accept(cssScanner_1.TokenType.Comma)) {
1185
- if (!node.addChild(this._parsePageSelector())) {
1186
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
1187
- }
1188
- }
1189
- }
1190
- return this._parseBody(node, this._parsePageDeclaration.bind(this));
1191
- }
1192
- _parsePageMarginBox() {
1193
- // page_margin_box : margin_sym S* '{' S* declaration? [ ';' S* declaration? ]* '}' S*
1194
- if (!this.peek(cssScanner_1.TokenType.AtKeyword)) {
1195
- return null;
1196
- }
1197
- const node = this.create(nodes.PageBoxMarginBox);
1198
- if (!this.acceptOneKeyword(languageFacts.pageBoxDirectives)) {
1199
- this.markError(node, cssErrors_1.ParseError.UnknownAtRule, [], [cssScanner_1.TokenType.CurlyL]);
1200
- }
1201
- return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
1202
- }
1203
- _parsePageSelector() {
1204
- // page_selector : pseudo_page+ | IDENT pseudo_page*
1205
- // pseudo_page : ':' [ "left" | "right" | "first" | "blank" ];
1206
- if (!this.peek(cssScanner_1.TokenType.Ident) && !this.peek(cssScanner_1.TokenType.Colon)) {
1207
- return null;
1208
- }
1209
- const node = this.create(nodes.Node);
1210
- node.addChild(this._parseIdent()); // optional ident
1211
- if (this.accept(cssScanner_1.TokenType.Colon)) {
1212
- if (!node.addChild(this._parseIdent())) { // optional ident
1213
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
1214
- }
1215
- }
1216
- return this.finish(node);
1217
- }
1218
- _parseDocument() {
1219
- // -moz-document is experimental but has been pushed to css4
1220
- if (!this.peekKeyword('@-moz-document')) {
1221
- return null;
1222
- }
1223
- const node = this.create(nodes.Document);
1224
- this.consumeToken(); // @-moz-document
1225
- this.resync([], [cssScanner_1.TokenType.CurlyL]); // ignore all the rules
1226
- return this._parseBody(node, this._parseStylesheetStatement.bind(this));
1227
- }
1228
- _parseContainerDeclaration(isNested = false) {
1229
- if (isNested) {
1230
- // if nested, the body can contain rulesets, but also declarations
1231
- return this._tryParseRuleset(true) || this._tryToParseDeclaration() || this._parseStylesheetStatement(true);
1232
- }
1233
- return this._parseStylesheetStatement(false);
1234
- }
1235
- _parseContainer(isNested = false) {
1236
- if (!this.peekKeyword('@container')) {
1237
- return null;
1238
- }
1239
- const node = this.create(nodes.Container);
1240
- this.consumeToken(); // @container
1241
- node.addChild(this._parseIdent()); // optional container name
1242
- node.addChild(this._parseContainerQuery());
1243
- return this._parseBody(node, this._parseContainerDeclaration.bind(this, isNested));
1244
- }
1245
- _parseContainerQuery() {
1246
- // <container-query> = not <query-in-parens>
1247
- // | <query-in-parens> [ [ and <query-in-parens> ]* | [ or <query-in-parens> ]* ]
1248
- const node = this.create(nodes.Node);
1249
- if (this.acceptIdent('not')) {
1250
- node.addChild(this._parseContainerQueryInParens());
1251
- }
1252
- else {
1253
- node.addChild(this._parseContainerQueryInParens());
1254
- if (this.peekIdent('and')) {
1255
- while (this.acceptIdent('and')) {
1256
- node.addChild(this._parseContainerQueryInParens());
1257
- }
1258
- }
1259
- else if (this.peekIdent('or')) {
1260
- while (this.acceptIdent('or')) {
1261
- node.addChild(this._parseContainerQueryInParens());
1262
- }
1263
- }
1264
- }
1265
- return this.finish(node);
1266
- }
1267
- _parseContainerQueryInParens() {
1268
- // <query-in-parens> = ( <container-query> )
1269
- // | ( <size-feature> )
1270
- // | style( <style-query> )
1271
- // | <general-enclosed>
1272
- const node = this.create(nodes.Node);
1273
- if (this.accept(cssScanner_1.TokenType.ParenthesisL)) {
1274
- if (this.peekIdent('not') || this.peek(cssScanner_1.TokenType.ParenthesisL)) {
1275
- node.addChild(this._parseContainerQuery());
1276
- }
1277
- else {
1278
- node.addChild(this._parseMediaFeature());
1279
- }
1280
- if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
1281
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected, [], [cssScanner_1.TokenType.CurlyL]);
1282
- }
1283
- }
1284
- else if (this.acceptIdent('style')) {
1285
- if (this.hasWhitespace() || !this.accept(cssScanner_1.TokenType.ParenthesisL)) {
1286
- return this.finish(node, cssErrors_1.ParseError.LeftParenthesisExpected, [], [cssScanner_1.TokenType.CurlyL]);
1287
- }
1288
- node.addChild(this._parseStyleQuery());
1289
- if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
1290
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected, [], [cssScanner_1.TokenType.CurlyL]);
1291
- }
1292
- }
1293
- else {
1294
- return this.finish(node, cssErrors_1.ParseError.LeftParenthesisExpected, [], [cssScanner_1.TokenType.CurlyL]);
1295
- }
1296
- return this.finish(node);
1297
- }
1298
- _parseStyleQuery() {
1299
- // <style-query> = not <style-in-parens>
1300
- // | <style-in-parens> [ [ and <style-in-parens> ]* | [ or <style-in-parens> ]* ]
1301
- // | <style-feature>
1302
- // <style-in-parens> = ( <style-query> )
1303
- // | ( <style-feature> )
1304
- // | <general-enclosed>
1305
- const node = this.create(nodes.Node);
1306
- if (this.acceptIdent('not')) {
1307
- node.addChild(this._parseStyleInParens());
1308
- }
1309
- else if (this.peek(cssScanner_1.TokenType.ParenthesisL)) {
1310
- node.addChild(this._parseStyleInParens());
1311
- if (this.peekIdent('and')) {
1312
- while (this.acceptIdent('and')) {
1313
- node.addChild(this._parseStyleInParens());
1314
- }
1315
- }
1316
- else if (this.peekIdent('or')) {
1317
- while (this.acceptIdent('or')) {
1318
- node.addChild(this._parseStyleInParens());
1319
- }
1320
- }
1321
- }
1322
- else {
1323
- node.addChild(this._parseDeclaration([cssScanner_1.TokenType.ParenthesisR]));
1324
- }
1325
- return this.finish(node);
1326
- }
1327
- _parseStyleInParens() {
1328
- const node = this.create(nodes.Node);
1329
- if (this.accept(cssScanner_1.TokenType.ParenthesisL)) {
1330
- node.addChild(this._parseStyleQuery());
1331
- if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
1332
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected, [], [cssScanner_1.TokenType.CurlyL]);
1333
- }
1334
- }
1335
- else {
1336
- return this.finish(node, cssErrors_1.ParseError.LeftParenthesisExpected, [], [cssScanner_1.TokenType.CurlyL]);
1337
- }
1338
- return this.finish(node);
1339
- }
1340
- // https://www.w3.org/TR/css-syntax-3/#consume-an-at-rule
1341
- _parseUnknownAtRule() {
1342
- if (!this.peek(cssScanner_1.TokenType.AtKeyword)) {
1343
- return null;
1344
- }
1345
- const node = this.create(nodes.UnknownAtRule);
1346
- node.addChild(this._parseUnknownAtRuleName());
1347
- const isTopLevel = () => curlyDepth === 0 && parensDepth === 0 && bracketsDepth === 0;
1348
- let curlyLCount = 0;
1349
- let curlyDepth = 0;
1350
- let parensDepth = 0;
1351
- let bracketsDepth = 0;
1352
- done: while (true) {
1353
- switch (this.token.type) {
1354
- case cssScanner_1.TokenType.SemiColon:
1355
- if (isTopLevel()) {
1356
- break done;
1357
- }
1358
- break;
1359
- case cssScanner_1.TokenType.EOF:
1360
- if (curlyDepth > 0) {
1361
- return this.finish(node, cssErrors_1.ParseError.RightCurlyExpected);
1362
- }
1363
- else if (bracketsDepth > 0) {
1364
- return this.finish(node, cssErrors_1.ParseError.RightSquareBracketExpected);
1365
- }
1366
- else if (parensDepth > 0) {
1367
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
1368
- }
1369
- else {
1370
- return this.finish(node);
1371
- }
1372
- case cssScanner_1.TokenType.CurlyL:
1373
- curlyLCount++;
1374
- curlyDepth++;
1375
- break;
1376
- case cssScanner_1.TokenType.CurlyR:
1377
- curlyDepth--;
1378
- // End of at-rule, consume CurlyR and return node
1379
- if (curlyLCount > 0 && curlyDepth === 0) {
1380
- this.consumeToken();
1381
- if (bracketsDepth > 0) {
1382
- return this.finish(node, cssErrors_1.ParseError.RightSquareBracketExpected);
1383
- }
1384
- else if (parensDepth > 0) {
1385
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
1386
- }
1387
- break done;
1388
- }
1389
- if (curlyDepth < 0) {
1390
- // The property value has been terminated without a semicolon, and
1391
- // this is the last declaration in the ruleset.
1392
- if (parensDepth === 0 && bracketsDepth === 0) {
1393
- break done;
1394
- }
1395
- return this.finish(node, cssErrors_1.ParseError.LeftCurlyExpected);
1396
- }
1397
- break;
1398
- case cssScanner_1.TokenType.ParenthesisL:
1399
- parensDepth++;
1400
- break;
1401
- case cssScanner_1.TokenType.ParenthesisR:
1402
- parensDepth--;
1403
- if (parensDepth < 0) {
1404
- return this.finish(node, cssErrors_1.ParseError.LeftParenthesisExpected);
1405
- }
1406
- break;
1407
- case cssScanner_1.TokenType.BracketL:
1408
- bracketsDepth++;
1409
- break;
1410
- case cssScanner_1.TokenType.BracketR:
1411
- bracketsDepth--;
1412
- if (bracketsDepth < 0) {
1413
- return this.finish(node, cssErrors_1.ParseError.LeftSquareBracketExpected);
1414
- }
1415
- break;
1416
- }
1417
- this.consumeToken();
1418
- }
1419
- return node;
1420
- }
1421
- _parseUnknownAtRuleName() {
1422
- const node = this.create(nodes.Node);
1423
- if (this.accept(cssScanner_1.TokenType.AtKeyword)) {
1424
- return this.finish(node);
1425
- }
1426
- return node;
1427
- }
1428
- _parseOperator() {
1429
- // these are operators for binary expressions
1430
- if (this.peekDelim('/') ||
1431
- this.peekDelim('*') ||
1432
- this.peekDelim('+') ||
1433
- this.peekDelim('-') ||
1434
- this.peek(cssScanner_1.TokenType.Dashmatch) ||
1435
- this.peek(cssScanner_1.TokenType.Includes) ||
1436
- this.peek(cssScanner_1.TokenType.SubstringOperator) ||
1437
- this.peek(cssScanner_1.TokenType.PrefixOperator) ||
1438
- this.peek(cssScanner_1.TokenType.SuffixOperator) ||
1439
- this.peekDelim('=')) { // doesn't stick to the standard here
1440
- const node = this.createNode(nodes.NodeType.Operator);
1441
- this.consumeToken();
1442
- return this.finish(node);
1443
- }
1444
- else {
1445
- return null;
1446
- }
1447
- }
1448
- _parseUnaryOperator() {
1449
- if (!this.peekDelim('+') && !this.peekDelim('-')) {
1450
- return null;
1451
- }
1452
- const node = this.create(nodes.Node);
1453
- this.consumeToken();
1454
- return this.finish(node);
1455
- }
1456
- _parseCombinator() {
1457
- if (this.peekDelim('>')) {
1458
- const node = this.create(nodes.Node);
1459
- this.consumeToken();
1460
- const mark = this.mark();
1461
- if (!this.hasWhitespace() && this.acceptDelim('>')) {
1462
- if (!this.hasWhitespace() && this.acceptDelim('>')) {
1463
- node.type = nodes.NodeType.SelectorCombinatorShadowPiercingDescendant;
1464
- return this.finish(node);
1465
- }
1466
- this.restoreAtMark(mark);
1467
- }
1468
- node.type = nodes.NodeType.SelectorCombinatorParent;
1469
- return this.finish(node);
1470
- }
1471
- else if (this.peekDelim('+')) {
1472
- const node = this.create(nodes.Node);
1473
- this.consumeToken();
1474
- node.type = nodes.NodeType.SelectorCombinatorSibling;
1475
- return this.finish(node);
1476
- }
1477
- else if (this.peekDelim('~')) {
1478
- const node = this.create(nodes.Node);
1479
- this.consumeToken();
1480
- node.type = nodes.NodeType.SelectorCombinatorAllSiblings;
1481
- return this.finish(node);
1482
- }
1483
- else if (this.peekDelim('/')) {
1484
- const node = this.create(nodes.Node);
1485
- this.consumeToken();
1486
- const mark = this.mark();
1487
- if (!this.hasWhitespace() && this.acceptIdent('deep') && !this.hasWhitespace() && this.acceptDelim('/')) {
1488
- node.type = nodes.NodeType.SelectorCombinatorShadowPiercingDescendant;
1489
- return this.finish(node);
1490
- }
1491
- this.restoreAtMark(mark);
1492
- }
1493
- return null;
1494
- }
1495
- _parseSimpleSelector() {
1496
- // simple_selector
1497
- // : element_name [ HASH | class | attrib | pseudo ]* | [ HASH | class | attrib | pseudo ]+ ;
1498
- const node = this.create(nodes.SimpleSelector);
1499
- let c = 0;
1500
- if (node.addChild(this._parseElementName() || this._parseNestingSelector())) {
1501
- c++;
1502
- }
1503
- while ((c === 0 || !this.hasWhitespace()) && node.addChild(this._parseSimpleSelectorBody())) {
1504
- c++;
1505
- }
1506
- return c > 0 ? this.finish(node) : null;
1507
- }
1508
- _parseNestingSelector() {
1509
- if (this.peekDelim('&')) {
1510
- const node = this.createNode(nodes.NodeType.SelectorCombinator);
1511
- this.consumeToken();
1512
- return this.finish(node);
1513
- }
1514
- return null;
1515
- }
1516
- _parseSimpleSelectorBody() {
1517
- return this._parsePseudo() || this._parseHash() || this._parseClass() || this._parseAttrib();
1518
- }
1519
- _parseSelectorIdent() {
1520
- return this._parseIdent();
1521
- }
1522
- _parseHash() {
1523
- if (!this.peek(cssScanner_1.TokenType.Hash) && !this.peekDelim('#')) {
1524
- return null;
1525
- }
1526
- const node = this.createNode(nodes.NodeType.IdentifierSelector);
1527
- if (this.acceptDelim('#')) {
1528
- if (this.hasWhitespace() || !node.addChild(this._parseSelectorIdent())) {
1529
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
1530
- }
1531
- }
1532
- else {
1533
- this.consumeToken(); // TokenType.Hash
1534
- }
1535
- return this.finish(node);
1536
- }
1537
- _parseClass() {
1538
- // class: '.' IDENT ;
1539
- if (!this.peekDelim('.')) {
1540
- return null;
1541
- }
1542
- const node = this.createNode(nodes.NodeType.ClassSelector);
1543
- this.consumeToken(); // '.'
1544
- if (this.hasWhitespace() || !node.addChild(this._parseSelectorIdent())) {
1545
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
1546
- }
1547
- return this.finish(node);
1548
- }
1549
- _parseElementName() {
1550
- // element_name: (ns? '|')? IDENT | '*';
1551
- const pos = this.mark();
1552
- const node = this.createNode(nodes.NodeType.ElementNameSelector);
1553
- node.addChild(this._parseNamespacePrefix());
1554
- if (!node.addChild(this._parseSelectorIdent()) && !this.acceptDelim('*')) {
1555
- this.restoreAtMark(pos);
1556
- return null;
1557
- }
1558
- return this.finish(node);
1559
- }
1560
- _parseNamespacePrefix() {
1561
- const pos = this.mark();
1562
- const node = this.createNode(nodes.NodeType.NamespacePrefix);
1563
- if (!node.addChild(this._parseIdent()) && !this.acceptDelim('*')) {
1564
- // ns is optional
1565
- }
1566
- if (!this.acceptDelim('|')) {
1567
- this.restoreAtMark(pos);
1568
- return null;
1569
- }
1570
- return this.finish(node);
1571
- }
1572
- _parseAttrib() {
1573
- // attrib : '[' S* IDENT S* [ [ '=' | INCLUDES | DASHMATCH ] S* [ IDENT | STRING ] S* ]? ']'
1574
- if (!this.peek(cssScanner_1.TokenType.BracketL)) {
1575
- return null;
1576
- }
1577
- const node = this.create(nodes.AttributeSelector);
1578
- this.consumeToken(); // BracketL
1579
- // Optional attrib namespace
1580
- node.setNamespacePrefix(this._parseNamespacePrefix());
1581
- if (!node.setIdentifier(this._parseIdent())) {
1582
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
1583
- }
1584
- if (node.setOperator(this._parseOperator())) {
1585
- node.setValue(this._parseBinaryExpr());
1586
- this.acceptIdent('i'); // case insensitive matching
1587
- this.acceptIdent('s'); // case sensitive matching
1588
- }
1589
- if (!this.accept(cssScanner_1.TokenType.BracketR)) {
1590
- return this.finish(node, cssErrors_1.ParseError.RightSquareBracketExpected);
1591
- }
1592
- return this.finish(node);
1593
- }
1594
- _parsePseudo() {
1595
- // pseudo: ':' [ IDENT | FUNCTION S* [IDENT S*]? ')' ]
1596
- const node = this._tryParsePseudoIdentifier();
1597
- if (node) {
1598
- if (!this.hasWhitespace() && this.accept(cssScanner_1.TokenType.ParenthesisL)) {
1599
- const tryAsSelector = () => {
1600
- const selectors = this.createNode(nodes.NodeType.SelectorList);
1601
- if (!selectors.addChild(this._parseSelector(true))) {
1602
- return null;
1603
- }
1604
- while (this.accept(cssScanner_1.TokenType.Comma) && selectors.addChild(this._parseSelector(true))) {
1605
- // loop
1606
- }
1607
- if (this.peek(cssScanner_1.TokenType.ParenthesisR)) {
1608
- return this.finish(selectors);
1609
- }
1610
- return null;
1611
- };
1612
- let hasSelector = node.addChild(this.try(tryAsSelector));
1613
- if (!hasSelector) {
1614
- // accept the <an+b> syntax (not a proper expression) https://drafts.csswg.org/css-syntax/#anb
1615
- while (!this.peekIdent('of') && (node.addChild(this._parseTerm()) || node.addChild(this._parseOperator()))) {
1616
- // loop
1617
- }
1618
- if (this.acceptIdent('of') &&
1619
- !node.addChild(this.try(tryAsSelector))) {
1620
- return this.finish(node, cssErrors_1.ParseError.SelectorExpected);
1621
- }
1622
- }
1623
- if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
1624
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
1625
- }
1626
- }
1627
- return this.finish(node);
1628
- }
1629
- return null;
1630
- }
1631
- _tryParsePseudoIdentifier() {
1632
- if (!this.peek(cssScanner_1.TokenType.Colon)) {
1633
- return null;
1634
- }
1635
- const pos = this.mark();
1636
- const node = this.createNode(nodes.NodeType.PseudoSelector);
1637
- this.consumeToken(); // Colon
1638
- if (this.hasWhitespace()) {
1639
- this.restoreAtMark(pos);
1640
- return null;
1641
- }
1642
- // optional, support ::
1643
- this.accept(cssScanner_1.TokenType.Colon);
1644
- if (this.hasWhitespace() || !node.addChild(this._parseIdent())) {
1645
- return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
1646
- }
1647
- return this.finish(node);
1648
- }
1649
- _tryParsePrio() {
1650
- const mark = this.mark();
1651
- const prio = this._parsePrio();
1652
- if (prio) {
1653
- return prio;
1654
- }
1655
- this.restoreAtMark(mark);
1656
- return null;
1657
- }
1658
- _parsePrio() {
1659
- if (!this.peek(cssScanner_1.TokenType.Exclamation)) {
1660
- return null;
1661
- }
1662
- const node = this.createNode(nodes.NodeType.Prio);
1663
- if (this.accept(cssScanner_1.TokenType.Exclamation) && this.acceptIdent('important')) {
1664
- return this.finish(node);
1665
- }
1666
- return null;
1667
- }
1668
- _parseExpr(stopOnComma = false) {
1669
- const node = this.create(nodes.Expression);
1670
- if (!node.addChild(this._parseBinaryExpr())) {
1671
- return null;
1672
- }
1673
- while (true) {
1674
- if (this.peek(cssScanner_1.TokenType.Comma)) { // optional
1675
- if (stopOnComma) {
1676
- return this.finish(node);
1677
- }
1678
- this.consumeToken();
1679
- }
1680
- if (!node.addChild(this._parseBinaryExpr())) {
1681
- break;
1682
- }
1683
- }
1684
- return this.finish(node);
1685
- }
1686
- _parseUnicodeRange() {
1687
- if (!this.peekIdent('u')) {
1688
- return null;
1689
- }
1690
- const node = this.create(nodes.UnicodeRange);
1691
- if (!this.acceptUnicodeRange()) {
1692
- return null;
1693
- }
1694
- return this.finish(node);
1695
- }
1696
- _parseNamedLine() {
1697
- // https://www.w3.org/TR/css-grid-1/#named-lines
1698
- if (!this.peek(cssScanner_1.TokenType.BracketL)) {
1699
- return null;
1700
- }
1701
- const node = this.createNode(nodes.NodeType.GridLine);
1702
- this.consumeToken();
1703
- while (node.addChild(this._parseIdent())) {
1704
- // repeat
1705
- }
1706
- if (!this.accept(cssScanner_1.TokenType.BracketR)) {
1707
- return this.finish(node, cssErrors_1.ParseError.RightSquareBracketExpected);
1708
- }
1709
- return this.finish(node);
1710
- }
1711
- _parseBinaryExpr(preparsedLeft, preparsedOper) {
1712
- let node = this.create(nodes.BinaryExpression);
1713
- if (!node.setLeft((preparsedLeft || this._parseTerm()))) {
1714
- return null;
1715
- }
1716
- if (!node.setOperator(preparsedOper || this._parseOperator())) {
1717
- return this.finish(node);
1718
- }
1719
- if (!node.setRight(this._parseTerm())) {
1720
- return this.finish(node, cssErrors_1.ParseError.TermExpected);
1721
- }
1722
- // things needed for multiple binary expressions
1723
- node = this.finish(node);
1724
- const operator = this._parseOperator();
1725
- if (operator) {
1726
- node = this._parseBinaryExpr(node, operator);
1727
- }
1728
- return this.finish(node);
1729
- }
1730
- _parseTerm() {
1731
- let node = this.create(nodes.Term);
1732
- node.setOperator(this._parseUnaryOperator()); // optional
1733
- if (node.setExpression(this._parseTermExpression())) {
1734
- return this.finish(node);
1735
- }
1736
- return null;
1737
- }
1738
- _parseTermExpression() {
1739
- return this._parseURILiteral() || // url before function
1740
- this._parseUnicodeRange() ||
1741
- this._parseFunction() || // function before ident
1742
- this._parseIdent() ||
1743
- this._parseStringLiteral() ||
1744
- this._parseNumeric() ||
1745
- this._parseHexColor() ||
1746
- this._parseOperation() ||
1747
- this._parseNamedLine();
1748
- }
1749
- _parseOperation() {
1750
- if (!this.peek(cssScanner_1.TokenType.ParenthesisL)) {
1751
- return null;
1752
- }
1753
- const node = this.create(nodes.Node);
1754
- this.consumeToken(); // ParenthesisL
1755
- node.addChild(this._parseExpr());
1756
- if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
1757
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
1758
- }
1759
- return this.finish(node);
1760
- }
1761
- _parseNumeric() {
1762
- if (this.peek(cssScanner_1.TokenType.Num) ||
1763
- this.peek(cssScanner_1.TokenType.Percentage) ||
1764
- this.peek(cssScanner_1.TokenType.Resolution) ||
1765
- this.peek(cssScanner_1.TokenType.Length) ||
1766
- this.peek(cssScanner_1.TokenType.EMS) ||
1767
- this.peek(cssScanner_1.TokenType.EXS) ||
1768
- this.peek(cssScanner_1.TokenType.Angle) ||
1769
- this.peek(cssScanner_1.TokenType.Time) ||
1770
- this.peek(cssScanner_1.TokenType.Dimension) ||
1771
- this.peek(cssScanner_1.TokenType.ContainerQueryLength) ||
1772
- this.peek(cssScanner_1.TokenType.Freq)) {
1773
- const node = this.create(nodes.NumericValue);
1774
- this.consumeToken();
1775
- return this.finish(node);
1776
- }
1777
- return null;
1778
- }
1779
- _parseStringLiteral() {
1780
- if (!this.peek(cssScanner_1.TokenType.String) && !this.peek(cssScanner_1.TokenType.BadString)) {
1781
- return null;
1782
- }
1783
- const node = this.createNode(nodes.NodeType.StringLiteral);
1784
- this.consumeToken();
1785
- return this.finish(node);
1786
- }
1787
- _parseURILiteral() {
1788
- if (!this.peekRegExp(cssScanner_1.TokenType.Ident, /^url(-prefix)?$/i)) {
1789
- return null;
1790
- }
1791
- const pos = this.mark();
1792
- const node = this.createNode(nodes.NodeType.URILiteral);
1793
- this.accept(cssScanner_1.TokenType.Ident);
1794
- if (this.hasWhitespace() || !this.peek(cssScanner_1.TokenType.ParenthesisL)) {
1795
- this.restoreAtMark(pos);
1796
- return null;
1797
- }
1798
- this.scanner.inURL = true;
1799
- this.consumeToken(); // consume ()
1800
- node.addChild(this._parseURLArgument()); // argument is optional
1801
- this.scanner.inURL = false;
1802
- if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
1803
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
1804
- }
1805
- return this.finish(node);
1806
- }
1807
- _parseURLArgument() {
1808
- const node = this.create(nodes.Node);
1809
- if (!this.accept(cssScanner_1.TokenType.String) && !this.accept(cssScanner_1.TokenType.BadString) && !this.acceptUnquotedString()) {
1810
- return null;
1811
- }
1812
- return this.finish(node);
1813
- }
1814
- _parseIdent(referenceTypes) {
1815
- if (!this.peek(cssScanner_1.TokenType.Ident)) {
1816
- return null;
1817
- }
1818
- const node = this.create(nodes.Identifier);
1819
- if (referenceTypes) {
1820
- node.referenceTypes = referenceTypes;
1821
- }
1822
- node.isCustomProperty = this.peekRegExp(cssScanner_1.TokenType.Ident, /^--/);
1823
- this.consumeToken();
1824
- return this.finish(node);
1825
- }
1826
- _parseFunction() {
1827
- const pos = this.mark();
1828
- const node = this.create(nodes.Function);
1829
- if (!node.setIdentifier(this._parseFunctionIdentifier())) {
1830
- return null;
1831
- }
1832
- if (this.hasWhitespace() || !this.accept(cssScanner_1.TokenType.ParenthesisL)) {
1833
- this.restoreAtMark(pos);
1834
- return null;
1835
- }
1836
- if (node.getArguments().addChild(this._parseFunctionArgument())) {
1837
- while (this.accept(cssScanner_1.TokenType.Comma)) {
1838
- if (this.peek(cssScanner_1.TokenType.ParenthesisR)) {
1839
- break;
1840
- }
1841
- if (!node.getArguments().addChild(this._parseFunctionArgument())) {
1842
- this.markError(node, cssErrors_1.ParseError.ExpressionExpected);
1843
- }
1844
- }
1845
- }
1846
- if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
1847
- return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
1848
- }
1849
- return this.finish(node);
1850
- }
1851
- _parseFunctionIdentifier() {
1852
- if (!this.peek(cssScanner_1.TokenType.Ident)) {
1853
- return null;
1854
- }
1855
- const node = this.create(nodes.Identifier);
1856
- node.referenceTypes = [nodes.ReferenceType.Function];
1857
- if (this.acceptIdent('progid')) {
1858
- // support for IE7 specific filters: 'progid:DXImageTransform.Microsoft.MotionBlur(strength=13, direction=310)'
1859
- if (this.accept(cssScanner_1.TokenType.Colon)) {
1860
- while (this.accept(cssScanner_1.TokenType.Ident) && this.acceptDelim('.')) {
1861
- // loop
1862
- }
1863
- }
1864
- return this.finish(node);
1865
- }
1866
- this.consumeToken();
1867
- return this.finish(node);
1868
- }
1869
- _parseFunctionArgument() {
1870
- const node = this.create(nodes.FunctionArgument);
1871
- if (node.setValue(this._parseExpr(true))) {
1872
- return this.finish(node);
1873
- }
1874
- return null;
1875
- }
1876
- _parseHexColor() {
1877
- 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)) {
1878
- const node = this.create(nodes.HexColorValue);
1879
- this.consumeToken();
1880
- return this.finish(node);
1881
- }
1882
- else {
1883
- return null;
1884
- }
1885
- }
1886
- }
1887
- exports.Parser = Parser;
1888
- });