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