vscode-css-languageservice 5.4.2 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/CHANGELOG.md +5 -1
  2. package/lib/esm/cssLanguageService.d.ts +37 -37
  3. package/lib/esm/cssLanguageService.js +72 -75
  4. package/lib/esm/cssLanguageTypes.d.ts +238 -238
  5. package/lib/esm/cssLanguageTypes.js +42 -42
  6. package/lib/esm/data/webCustomData.js +21965 -21965
  7. package/lib/esm/languageFacts/builtinData.js +142 -142
  8. package/lib/esm/languageFacts/colors.js +469 -472
  9. package/lib/esm/languageFacts/dataManager.js +88 -92
  10. package/lib/esm/languageFacts/dataProvider.js +73 -79
  11. package/lib/esm/languageFacts/entry.js +137 -138
  12. package/lib/esm/languageFacts/facts.js +8 -8
  13. package/lib/esm/parser/cssErrors.js +48 -50
  14. package/lib/esm/parser/cssNodes.js +1502 -2019
  15. package/lib/esm/parser/cssParser.js +1534 -1563
  16. package/lib/esm/parser/cssScanner.js +592 -599
  17. package/lib/esm/parser/cssSymbolScope.js +311 -341
  18. package/lib/esm/parser/lessParser.js +714 -740
  19. package/lib/esm/parser/lessScanner.js +57 -78
  20. package/lib/esm/parser/scssErrors.js +18 -20
  21. package/lib/esm/parser/scssParser.js +796 -818
  22. package/lib/esm/parser/scssScanner.js +95 -116
  23. package/lib/esm/services/cssCodeActions.js +77 -81
  24. package/lib/esm/services/cssCompletion.js +1054 -1149
  25. package/lib/esm/services/cssFolding.js +190 -193
  26. package/lib/esm/services/cssFormatter.js +136 -136
  27. package/lib/esm/services/cssHover.js +148 -151
  28. package/lib/esm/services/cssNavigation.js +378 -470
  29. package/lib/esm/services/cssSelectionRange.js +47 -47
  30. package/lib/esm/services/cssValidation.js +41 -44
  31. package/lib/esm/services/lessCompletion.js +378 -397
  32. package/lib/esm/services/lint.js +518 -532
  33. package/lib/esm/services/lintRules.js +76 -83
  34. package/lib/esm/services/lintUtil.js +196 -205
  35. package/lib/esm/services/pathCompletion.js +157 -231
  36. package/lib/esm/services/scssCompletion.js +354 -378
  37. package/lib/esm/services/scssNavigation.js +82 -154
  38. package/lib/esm/services/selectorPrinting.js +492 -536
  39. package/lib/esm/utils/arrays.js +40 -46
  40. package/lib/esm/utils/objects.js +11 -11
  41. package/lib/esm/utils/resources.js +11 -24
  42. package/lib/esm/utils/strings.js +102 -104
  43. package/lib/umd/cssLanguageService.d.ts +37 -37
  44. package/lib/umd/cssLanguageService.js +99 -102
  45. package/lib/umd/cssLanguageTypes.d.ts +238 -238
  46. package/lib/umd/cssLanguageTypes.js +89 -88
  47. package/lib/umd/data/webCustomData.js +21978 -21978
  48. package/lib/umd/languageFacts/builtinData.js +154 -154
  49. package/lib/umd/languageFacts/colors.js +492 -495
  50. package/lib/umd/languageFacts/dataManager.js +101 -104
  51. package/lib/umd/languageFacts/dataProvider.js +86 -91
  52. package/lib/umd/languageFacts/entry.js +152 -153
  53. package/lib/umd/languageFacts/facts.js +29 -29
  54. package/lib/umd/parser/cssErrors.js +61 -62
  55. package/lib/umd/parser/cssNodes.js +1587 -2034
  56. package/lib/umd/parser/cssParser.js +1547 -1575
  57. package/lib/umd/parser/cssScanner.js +606 -611
  58. package/lib/umd/parser/cssSymbolScope.js +328 -353
  59. package/lib/umd/parser/lessParser.js +727 -752
  60. package/lib/umd/parser/lessScanner.js +70 -90
  61. package/lib/umd/parser/scssErrors.js +31 -32
  62. package/lib/umd/parser/scssParser.js +809 -830
  63. package/lib/umd/parser/scssScanner.js +108 -128
  64. package/lib/umd/services/cssCodeActions.js +90 -93
  65. package/lib/umd/services/cssCompletion.js +1067 -1161
  66. package/lib/umd/services/cssFolding.js +203 -206
  67. package/lib/umd/services/cssFormatter.js +150 -150
  68. package/lib/umd/services/cssHover.js +161 -163
  69. package/lib/umd/services/cssNavigation.js +391 -482
  70. package/lib/umd/services/cssSelectionRange.js +60 -60
  71. package/lib/umd/services/cssValidation.js +54 -56
  72. package/lib/umd/services/lessCompletion.js +391 -409
  73. package/lib/umd/services/lint.js +531 -544
  74. package/lib/umd/services/lintRules.js +91 -95
  75. package/lib/umd/services/lintUtil.js +210 -218
  76. package/lib/umd/services/pathCompletion.js +171 -244
  77. package/lib/umd/services/scssCompletion.js +367 -390
  78. package/lib/umd/services/scssNavigation.js +95 -166
  79. package/lib/umd/services/selectorPrinting.js +510 -550
  80. package/lib/umd/utils/arrays.js +55 -61
  81. package/lib/umd/utils/objects.js +25 -25
  82. package/lib/umd/utils/resources.js +26 -39
  83. package/lib/umd/utils/strings.js +120 -122
  84. package/package.json +10 -10
@@ -1,599 +1,592 @@
1
- /*---------------------------------------------------------------------------------------------
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the MIT License. See License.txt in the project root for license information.
4
- *--------------------------------------------------------------------------------------------*/
5
- 'use strict';
6
- export var TokenType;
7
- (function (TokenType) {
8
- TokenType[TokenType["Ident"] = 0] = "Ident";
9
- TokenType[TokenType["AtKeyword"] = 1] = "AtKeyword";
10
- TokenType[TokenType["String"] = 2] = "String";
11
- TokenType[TokenType["BadString"] = 3] = "BadString";
12
- TokenType[TokenType["UnquotedString"] = 4] = "UnquotedString";
13
- TokenType[TokenType["Hash"] = 5] = "Hash";
14
- TokenType[TokenType["Num"] = 6] = "Num";
15
- TokenType[TokenType["Percentage"] = 7] = "Percentage";
16
- TokenType[TokenType["Dimension"] = 8] = "Dimension";
17
- TokenType[TokenType["UnicodeRange"] = 9] = "UnicodeRange";
18
- TokenType[TokenType["CDO"] = 10] = "CDO";
19
- TokenType[TokenType["CDC"] = 11] = "CDC";
20
- TokenType[TokenType["Colon"] = 12] = "Colon";
21
- TokenType[TokenType["SemiColon"] = 13] = "SemiColon";
22
- TokenType[TokenType["CurlyL"] = 14] = "CurlyL";
23
- TokenType[TokenType["CurlyR"] = 15] = "CurlyR";
24
- TokenType[TokenType["ParenthesisL"] = 16] = "ParenthesisL";
25
- TokenType[TokenType["ParenthesisR"] = 17] = "ParenthesisR";
26
- TokenType[TokenType["BracketL"] = 18] = "BracketL";
27
- TokenType[TokenType["BracketR"] = 19] = "BracketR";
28
- TokenType[TokenType["Whitespace"] = 20] = "Whitespace";
29
- TokenType[TokenType["Includes"] = 21] = "Includes";
30
- TokenType[TokenType["Dashmatch"] = 22] = "Dashmatch";
31
- TokenType[TokenType["SubstringOperator"] = 23] = "SubstringOperator";
32
- TokenType[TokenType["PrefixOperator"] = 24] = "PrefixOperator";
33
- TokenType[TokenType["SuffixOperator"] = 25] = "SuffixOperator";
34
- TokenType[TokenType["Delim"] = 26] = "Delim";
35
- TokenType[TokenType["EMS"] = 27] = "EMS";
36
- TokenType[TokenType["EXS"] = 28] = "EXS";
37
- TokenType[TokenType["Length"] = 29] = "Length";
38
- TokenType[TokenType["Angle"] = 30] = "Angle";
39
- TokenType[TokenType["Time"] = 31] = "Time";
40
- TokenType[TokenType["Freq"] = 32] = "Freq";
41
- TokenType[TokenType["Exclamation"] = 33] = "Exclamation";
42
- TokenType[TokenType["Resolution"] = 34] = "Resolution";
43
- TokenType[TokenType["Comma"] = 35] = "Comma";
44
- TokenType[TokenType["Charset"] = 36] = "Charset";
45
- TokenType[TokenType["EscapedJavaScript"] = 37] = "EscapedJavaScript";
46
- TokenType[TokenType["BadEscapedJavaScript"] = 38] = "BadEscapedJavaScript";
47
- TokenType[TokenType["Comment"] = 39] = "Comment";
48
- TokenType[TokenType["SingleLineComment"] = 40] = "SingleLineComment";
49
- TokenType[TokenType["EOF"] = 41] = "EOF";
50
- TokenType[TokenType["CustomToken"] = 42] = "CustomToken";
51
- })(TokenType || (TokenType = {}));
52
- var MultiLineStream = /** @class */ (function () {
53
- function MultiLineStream(source) {
54
- this.source = source;
55
- this.len = source.length;
56
- this.position = 0;
57
- }
58
- MultiLineStream.prototype.substring = function (from, to) {
59
- if (to === void 0) { to = this.position; }
60
- return this.source.substring(from, to);
61
- };
62
- MultiLineStream.prototype.eos = function () {
63
- return this.len <= this.position;
64
- };
65
- MultiLineStream.prototype.pos = function () {
66
- return this.position;
67
- };
68
- MultiLineStream.prototype.goBackTo = function (pos) {
69
- this.position = pos;
70
- };
71
- MultiLineStream.prototype.goBack = function (n) {
72
- this.position -= n;
73
- };
74
- MultiLineStream.prototype.advance = function (n) {
75
- this.position += n;
76
- };
77
- MultiLineStream.prototype.nextChar = function () {
78
- return this.source.charCodeAt(this.position++) || 0;
79
- };
80
- MultiLineStream.prototype.peekChar = function (n) {
81
- if (n === void 0) { n = 0; }
82
- return this.source.charCodeAt(this.position + n) || 0;
83
- };
84
- MultiLineStream.prototype.lookbackChar = function (n) {
85
- if (n === void 0) { n = 0; }
86
- return this.source.charCodeAt(this.position - n) || 0;
87
- };
88
- MultiLineStream.prototype.advanceIfChar = function (ch) {
89
- if (ch === this.source.charCodeAt(this.position)) {
90
- this.position++;
91
- return true;
92
- }
93
- return false;
94
- };
95
- MultiLineStream.prototype.advanceIfChars = function (ch) {
96
- if (this.position + ch.length > this.source.length) {
97
- return false;
98
- }
99
- var i = 0;
100
- for (; i < ch.length; i++) {
101
- if (this.source.charCodeAt(this.position + i) !== ch[i]) {
102
- return false;
103
- }
104
- }
105
- this.advance(i);
106
- return true;
107
- };
108
- MultiLineStream.prototype.advanceWhileChar = function (condition) {
109
- var posNow = this.position;
110
- while (this.position < this.len && condition(this.source.charCodeAt(this.position))) {
111
- this.position++;
112
- }
113
- return this.position - posNow;
114
- };
115
- return MultiLineStream;
116
- }());
117
- export { MultiLineStream };
118
- var _a = 'a'.charCodeAt(0);
119
- var _f = 'f'.charCodeAt(0);
120
- var _z = 'z'.charCodeAt(0);
121
- var _u = 'u'.charCodeAt(0);
122
- var _A = 'A'.charCodeAt(0);
123
- var _F = 'F'.charCodeAt(0);
124
- var _Z = 'Z'.charCodeAt(0);
125
- var _0 = '0'.charCodeAt(0);
126
- var _9 = '9'.charCodeAt(0);
127
- var _TLD = '~'.charCodeAt(0);
128
- var _HAT = '^'.charCodeAt(0);
129
- var _EQS = '='.charCodeAt(0);
130
- var _PIP = '|'.charCodeAt(0);
131
- var _MIN = '-'.charCodeAt(0);
132
- var _USC = '_'.charCodeAt(0);
133
- var _PRC = '%'.charCodeAt(0);
134
- var _MUL = '*'.charCodeAt(0);
135
- var _LPA = '('.charCodeAt(0);
136
- var _RPA = ')'.charCodeAt(0);
137
- var _LAN = '<'.charCodeAt(0);
138
- var _RAN = '>'.charCodeAt(0);
139
- var _ATS = '@'.charCodeAt(0);
140
- var _HSH = '#'.charCodeAt(0);
141
- var _DLR = '$'.charCodeAt(0);
142
- var _BSL = '\\'.charCodeAt(0);
143
- var _FSL = '/'.charCodeAt(0);
144
- var _NWL = '\n'.charCodeAt(0);
145
- var _CAR = '\r'.charCodeAt(0);
146
- var _LFD = '\f'.charCodeAt(0);
147
- var _DQO = '"'.charCodeAt(0);
148
- var _SQO = '\''.charCodeAt(0);
149
- var _WSP = ' '.charCodeAt(0);
150
- var _TAB = '\t'.charCodeAt(0);
151
- var _SEM = ';'.charCodeAt(0);
152
- var _COL = ':'.charCodeAt(0);
153
- var _CUL = '{'.charCodeAt(0);
154
- var _CUR = '}'.charCodeAt(0);
155
- var _BRL = '['.charCodeAt(0);
156
- var _BRR = ']'.charCodeAt(0);
157
- var _CMA = ','.charCodeAt(0);
158
- var _DOT = '.'.charCodeAt(0);
159
- var _BNG = '!'.charCodeAt(0);
160
- var _QSM = '?'.charCodeAt(0);
161
- var _PLS = '+'.charCodeAt(0);
162
- var staticTokenTable = {};
163
- staticTokenTable[_SEM] = TokenType.SemiColon;
164
- staticTokenTable[_COL] = TokenType.Colon;
165
- staticTokenTable[_CUL] = TokenType.CurlyL;
166
- staticTokenTable[_CUR] = TokenType.CurlyR;
167
- staticTokenTable[_BRR] = TokenType.BracketR;
168
- staticTokenTable[_BRL] = TokenType.BracketL;
169
- staticTokenTable[_LPA] = TokenType.ParenthesisL;
170
- staticTokenTable[_RPA] = TokenType.ParenthesisR;
171
- staticTokenTable[_CMA] = TokenType.Comma;
172
- var staticUnitTable = {};
173
- staticUnitTable['em'] = TokenType.EMS;
174
- staticUnitTable['ex'] = TokenType.EXS;
175
- staticUnitTable['px'] = TokenType.Length;
176
- staticUnitTable['cm'] = TokenType.Length;
177
- staticUnitTable['mm'] = TokenType.Length;
178
- staticUnitTable['in'] = TokenType.Length;
179
- staticUnitTable['pt'] = TokenType.Length;
180
- staticUnitTable['pc'] = TokenType.Length;
181
- staticUnitTable['deg'] = TokenType.Angle;
182
- staticUnitTable['rad'] = TokenType.Angle;
183
- staticUnitTable['grad'] = TokenType.Angle;
184
- staticUnitTable['ms'] = TokenType.Time;
185
- staticUnitTable['s'] = TokenType.Time;
186
- staticUnitTable['hz'] = TokenType.Freq;
187
- staticUnitTable['khz'] = TokenType.Freq;
188
- staticUnitTable['%'] = TokenType.Percentage;
189
- staticUnitTable['fr'] = TokenType.Percentage;
190
- staticUnitTable['dpi'] = TokenType.Resolution;
191
- staticUnitTable['dpcm'] = TokenType.Resolution;
192
- var Scanner = /** @class */ (function () {
193
- function Scanner() {
194
- this.stream = new MultiLineStream('');
195
- this.ignoreComment = true;
196
- this.ignoreWhitespace = true;
197
- this.inURL = false;
198
- }
199
- Scanner.prototype.setSource = function (input) {
200
- this.stream = new MultiLineStream(input);
201
- };
202
- Scanner.prototype.finishToken = function (offset, type, text) {
203
- return {
204
- offset: offset,
205
- len: this.stream.pos() - offset,
206
- type: type,
207
- text: text || this.stream.substring(offset)
208
- };
209
- };
210
- Scanner.prototype.substring = function (offset, len) {
211
- return this.stream.substring(offset, offset + len);
212
- };
213
- Scanner.prototype.pos = function () {
214
- return this.stream.pos();
215
- };
216
- Scanner.prototype.goBackTo = function (pos) {
217
- this.stream.goBackTo(pos);
218
- };
219
- Scanner.prototype.scanUnquotedString = function () {
220
- var offset = this.stream.pos();
221
- var content = [];
222
- if (this._unquotedString(content)) {
223
- return this.finishToken(offset, TokenType.UnquotedString, content.join(''));
224
- }
225
- return null;
226
- };
227
- Scanner.prototype.scan = function () {
228
- // processes all whitespaces and comments
229
- var triviaToken = this.trivia();
230
- if (triviaToken !== null) {
231
- return triviaToken;
232
- }
233
- var offset = this.stream.pos();
234
- // End of file/input
235
- if (this.stream.eos()) {
236
- return this.finishToken(offset, TokenType.EOF);
237
- }
238
- return this.scanNext(offset);
239
- };
240
- /**
241
- * Read the range as described in https://www.w3.org/TR/CSS21/syndata.html#tokenization
242
- * Assume the `u` has aleady been consumed
243
- * @returns if reading the unicode was successful
244
- */
245
- Scanner.prototype.tryScanUnicode = function () {
246
- var offset = this.stream.pos();
247
- if (!this.stream.eos() && this._unicodeRange()) {
248
- return this.finishToken(offset, TokenType.UnicodeRange);
249
- }
250
- this.stream.goBackTo(offset);
251
- return undefined;
252
- };
253
- Scanner.prototype.scanNext = function (offset) {
254
- // CDO <!--
255
- if (this.stream.advanceIfChars([_LAN, _BNG, _MIN, _MIN])) {
256
- return this.finishToken(offset, TokenType.CDO);
257
- }
258
- // CDC -->
259
- if (this.stream.advanceIfChars([_MIN, _MIN, _RAN])) {
260
- return this.finishToken(offset, TokenType.CDC);
261
- }
262
- var content = [];
263
- if (this.ident(content)) {
264
- return this.finishToken(offset, TokenType.Ident, content.join(''));
265
- }
266
- // at-keyword
267
- if (this.stream.advanceIfChar(_ATS)) {
268
- content = ['@'];
269
- if (this._name(content)) {
270
- var keywordText = content.join('');
271
- if (keywordText === '@charset') {
272
- return this.finishToken(offset, TokenType.Charset, keywordText);
273
- }
274
- return this.finishToken(offset, TokenType.AtKeyword, keywordText);
275
- }
276
- else {
277
- return this.finishToken(offset, TokenType.Delim);
278
- }
279
- }
280
- // hash
281
- if (this.stream.advanceIfChar(_HSH)) {
282
- content = ['#'];
283
- if (this._name(content)) {
284
- return this.finishToken(offset, TokenType.Hash, content.join(''));
285
- }
286
- else {
287
- return this.finishToken(offset, TokenType.Delim);
288
- }
289
- }
290
- // Important
291
- if (this.stream.advanceIfChar(_BNG)) {
292
- return this.finishToken(offset, TokenType.Exclamation);
293
- }
294
- // Numbers
295
- if (this._number()) {
296
- var pos = this.stream.pos();
297
- content = [this.stream.substring(offset, pos)];
298
- if (this.stream.advanceIfChar(_PRC)) {
299
- // Percentage 43%
300
- return this.finishToken(offset, TokenType.Percentage);
301
- }
302
- else if (this.ident(content)) {
303
- var dim = this.stream.substring(pos).toLowerCase();
304
- var tokenType_1 = staticUnitTable[dim];
305
- if (typeof tokenType_1 !== 'undefined') {
306
- // Known dimension 43px
307
- return this.finishToken(offset, tokenType_1, content.join(''));
308
- }
309
- else {
310
- // Unknown dimension 43ft
311
- return this.finishToken(offset, TokenType.Dimension, content.join(''));
312
- }
313
- }
314
- return this.finishToken(offset, TokenType.Num);
315
- }
316
- // String, BadString
317
- content = [];
318
- var tokenType = this._string(content);
319
- if (tokenType !== null) {
320
- return this.finishToken(offset, tokenType, content.join(''));
321
- }
322
- // single character tokens
323
- tokenType = staticTokenTable[this.stream.peekChar()];
324
- if (typeof tokenType !== 'undefined') {
325
- this.stream.advance(1);
326
- return this.finishToken(offset, tokenType);
327
- }
328
- // includes ~=
329
- if (this.stream.peekChar(0) === _TLD && this.stream.peekChar(1) === _EQS) {
330
- this.stream.advance(2);
331
- return this.finishToken(offset, TokenType.Includes);
332
- }
333
- // DashMatch |=
334
- if (this.stream.peekChar(0) === _PIP && this.stream.peekChar(1) === _EQS) {
335
- this.stream.advance(2);
336
- return this.finishToken(offset, TokenType.Dashmatch);
337
- }
338
- // Substring operator *=
339
- if (this.stream.peekChar(0) === _MUL && this.stream.peekChar(1) === _EQS) {
340
- this.stream.advance(2);
341
- return this.finishToken(offset, TokenType.SubstringOperator);
342
- }
343
- // Substring operator ^=
344
- if (this.stream.peekChar(0) === _HAT && this.stream.peekChar(1) === _EQS) {
345
- this.stream.advance(2);
346
- return this.finishToken(offset, TokenType.PrefixOperator);
347
- }
348
- // Substring operator $=
349
- if (this.stream.peekChar(0) === _DLR && this.stream.peekChar(1) === _EQS) {
350
- this.stream.advance(2);
351
- return this.finishToken(offset, TokenType.SuffixOperator);
352
- }
353
- // Delim
354
- this.stream.nextChar();
355
- return this.finishToken(offset, TokenType.Delim);
356
- };
357
- Scanner.prototype.trivia = function () {
358
- while (true) {
359
- var offset = this.stream.pos();
360
- if (this._whitespace()) {
361
- if (!this.ignoreWhitespace) {
362
- return this.finishToken(offset, TokenType.Whitespace);
363
- }
364
- }
365
- else if (this.comment()) {
366
- if (!this.ignoreComment) {
367
- return this.finishToken(offset, TokenType.Comment);
368
- }
369
- }
370
- else {
371
- return null;
372
- }
373
- }
374
- };
375
- Scanner.prototype.comment = function () {
376
- if (this.stream.advanceIfChars([_FSL, _MUL])) {
377
- var success_1 = false, hot_1 = false;
378
- this.stream.advanceWhileChar(function (ch) {
379
- if (hot_1 && ch === _FSL) {
380
- success_1 = true;
381
- return false;
382
- }
383
- hot_1 = ch === _MUL;
384
- return true;
385
- });
386
- if (success_1) {
387
- this.stream.advance(1);
388
- }
389
- return true;
390
- }
391
- return false;
392
- };
393
- Scanner.prototype._number = function () {
394
- var npeek = 0, ch;
395
- if (this.stream.peekChar() === _DOT) {
396
- npeek = 1;
397
- }
398
- ch = this.stream.peekChar(npeek);
399
- if (ch >= _0 && ch <= _9) {
400
- this.stream.advance(npeek + 1);
401
- this.stream.advanceWhileChar(function (ch) {
402
- return ch >= _0 && ch <= _9 || npeek === 0 && ch === _DOT;
403
- });
404
- return true;
405
- }
406
- return false;
407
- };
408
- Scanner.prototype._newline = function (result) {
409
- var ch = this.stream.peekChar();
410
- switch (ch) {
411
- case _CAR:
412
- case _LFD:
413
- case _NWL:
414
- this.stream.advance(1);
415
- result.push(String.fromCharCode(ch));
416
- if (ch === _CAR && this.stream.advanceIfChar(_NWL)) {
417
- result.push('\n');
418
- }
419
- return true;
420
- }
421
- return false;
422
- };
423
- Scanner.prototype._escape = function (result, includeNewLines) {
424
- var ch = this.stream.peekChar();
425
- if (ch === _BSL) {
426
- this.stream.advance(1);
427
- ch = this.stream.peekChar();
428
- var hexNumCount = 0;
429
- while (hexNumCount < 6 && (ch >= _0 && ch <= _9 || ch >= _a && ch <= _f || ch >= _A && ch <= _F)) {
430
- this.stream.advance(1);
431
- ch = this.stream.peekChar();
432
- hexNumCount++;
433
- }
434
- if (hexNumCount > 0) {
435
- try {
436
- var hexVal = parseInt(this.stream.substring(this.stream.pos() - hexNumCount), 16);
437
- if (hexVal) {
438
- result.push(String.fromCharCode(hexVal));
439
- }
440
- }
441
- catch (e) {
442
- // ignore
443
- }
444
- // optional whitespace or new line, not part of result text
445
- if (ch === _WSP || ch === _TAB) {
446
- this.stream.advance(1);
447
- }
448
- else {
449
- this._newline([]);
450
- }
451
- return true;
452
- }
453
- if (ch !== _CAR && ch !== _LFD && ch !== _NWL) {
454
- this.stream.advance(1);
455
- result.push(String.fromCharCode(ch));
456
- return true;
457
- }
458
- else if (includeNewLines) {
459
- return this._newline(result);
460
- }
461
- }
462
- return false;
463
- };
464
- Scanner.prototype._stringChar = function (closeQuote, result) {
465
- // not closeQuote, not backslash, not newline
466
- var ch = this.stream.peekChar();
467
- if (ch !== 0 && ch !== closeQuote && ch !== _BSL && ch !== _CAR && ch !== _LFD && ch !== _NWL) {
468
- this.stream.advance(1);
469
- result.push(String.fromCharCode(ch));
470
- return true;
471
- }
472
- return false;
473
- };
474
- Scanner.prototype._string = function (result) {
475
- if (this.stream.peekChar() === _SQO || this.stream.peekChar() === _DQO) {
476
- var closeQuote = this.stream.nextChar();
477
- result.push(String.fromCharCode(closeQuote));
478
- while (this._stringChar(closeQuote, result) || this._escape(result, true)) {
479
- // loop
480
- }
481
- if (this.stream.peekChar() === closeQuote) {
482
- this.stream.nextChar();
483
- result.push(String.fromCharCode(closeQuote));
484
- return TokenType.String;
485
- }
486
- else {
487
- return TokenType.BadString;
488
- }
489
- }
490
- return null;
491
- };
492
- Scanner.prototype._unquotedChar = function (result) {
493
- // not closeQuote, not backslash, not newline
494
- var ch = this.stream.peekChar();
495
- if (ch !== 0 && ch !== _BSL && ch !== _SQO && ch !== _DQO && ch !== _LPA && ch !== _RPA && ch !== _WSP && ch !== _TAB && ch !== _NWL && ch !== _LFD && ch !== _CAR) {
496
- this.stream.advance(1);
497
- result.push(String.fromCharCode(ch));
498
- return true;
499
- }
500
- return false;
501
- };
502
- Scanner.prototype._unquotedString = function (result) {
503
- var hasContent = false;
504
- while (this._unquotedChar(result) || this._escape(result)) {
505
- hasContent = true;
506
- }
507
- return hasContent;
508
- };
509
- Scanner.prototype._whitespace = function () {
510
- var n = this.stream.advanceWhileChar(function (ch) {
511
- return ch === _WSP || ch === _TAB || ch === _NWL || ch === _LFD || ch === _CAR;
512
- });
513
- return n > 0;
514
- };
515
- Scanner.prototype._name = function (result) {
516
- var matched = false;
517
- while (this._identChar(result) || this._escape(result)) {
518
- matched = true;
519
- }
520
- return matched;
521
- };
522
- Scanner.prototype.ident = function (result) {
523
- var pos = this.stream.pos();
524
- var hasMinus = this._minus(result);
525
- if (hasMinus) {
526
- if (this._minus(result) /* -- */ || this._identFirstChar(result) || this._escape(result)) {
527
- while (this._identChar(result) || this._escape(result)) {
528
- // loop
529
- }
530
- return true;
531
- }
532
- }
533
- else if (this._identFirstChar(result) || this._escape(result)) {
534
- while (this._identChar(result) || this._escape(result)) {
535
- // loop
536
- }
537
- return true;
538
- }
539
- this.stream.goBackTo(pos);
540
- return false;
541
- };
542
- Scanner.prototype._identFirstChar = function (result) {
543
- var ch = this.stream.peekChar();
544
- if (ch === _USC || // _
545
- ch >= _a && ch <= _z || // a-z
546
- ch >= _A && ch <= _Z || // A-Z
547
- ch >= 0x80 && ch <= 0xFFFF) { // nonascii
548
- this.stream.advance(1);
549
- result.push(String.fromCharCode(ch));
550
- return true;
551
- }
552
- return false;
553
- };
554
- Scanner.prototype._minus = function (result) {
555
- var ch = this.stream.peekChar();
556
- if (ch === _MIN) {
557
- this.stream.advance(1);
558
- result.push(String.fromCharCode(ch));
559
- return true;
560
- }
561
- return false;
562
- };
563
- Scanner.prototype._identChar = function (result) {
564
- var ch = this.stream.peekChar();
565
- if (ch === _USC || // _
566
- ch === _MIN || // -
567
- ch >= _a && ch <= _z || // a-z
568
- ch >= _A && ch <= _Z || // A-Z
569
- ch >= _0 && ch <= _9 || // 0/9
570
- ch >= 0x80 && ch <= 0xFFFF) { // nonascii
571
- this.stream.advance(1);
572
- result.push(String.fromCharCode(ch));
573
- return true;
574
- }
575
- return false;
576
- };
577
- Scanner.prototype._unicodeRange = function () {
578
- // follow https://www.w3.org/TR/CSS21/syndata.html#tokenization and https://www.w3.org/TR/css-syntax-3/#urange-syntax
579
- // assume u has already been parsed
580
- if (this.stream.advanceIfChar(_PLS)) {
581
- var isHexDigit = function (ch) { return (ch >= _0 && ch <= _9 || ch >= _a && ch <= _f || ch >= _A && ch <= _F); };
582
- var codePoints = this.stream.advanceWhileChar(isHexDigit) + this.stream.advanceWhileChar(function (ch) { return ch === _QSM; });
583
- if (codePoints >= 1 && codePoints <= 6) {
584
- if (this.stream.advanceIfChar(_MIN)) {
585
- var digits = this.stream.advanceWhileChar(isHexDigit);
586
- if (digits >= 1 && digits <= 6) {
587
- return true;
588
- }
589
- }
590
- else {
591
- return true;
592
- }
593
- }
594
- }
595
- return false;
596
- };
597
- return Scanner;
598
- }());
599
- export { Scanner };
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License. See License.txt in the project root for license information.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ 'use strict';
6
+ export var TokenType;
7
+ (function (TokenType) {
8
+ TokenType[TokenType["Ident"] = 0] = "Ident";
9
+ TokenType[TokenType["AtKeyword"] = 1] = "AtKeyword";
10
+ TokenType[TokenType["String"] = 2] = "String";
11
+ TokenType[TokenType["BadString"] = 3] = "BadString";
12
+ TokenType[TokenType["UnquotedString"] = 4] = "UnquotedString";
13
+ TokenType[TokenType["Hash"] = 5] = "Hash";
14
+ TokenType[TokenType["Num"] = 6] = "Num";
15
+ TokenType[TokenType["Percentage"] = 7] = "Percentage";
16
+ TokenType[TokenType["Dimension"] = 8] = "Dimension";
17
+ TokenType[TokenType["UnicodeRange"] = 9] = "UnicodeRange";
18
+ TokenType[TokenType["CDO"] = 10] = "CDO";
19
+ TokenType[TokenType["CDC"] = 11] = "CDC";
20
+ TokenType[TokenType["Colon"] = 12] = "Colon";
21
+ TokenType[TokenType["SemiColon"] = 13] = "SemiColon";
22
+ TokenType[TokenType["CurlyL"] = 14] = "CurlyL";
23
+ TokenType[TokenType["CurlyR"] = 15] = "CurlyR";
24
+ TokenType[TokenType["ParenthesisL"] = 16] = "ParenthesisL";
25
+ TokenType[TokenType["ParenthesisR"] = 17] = "ParenthesisR";
26
+ TokenType[TokenType["BracketL"] = 18] = "BracketL";
27
+ TokenType[TokenType["BracketR"] = 19] = "BracketR";
28
+ TokenType[TokenType["Whitespace"] = 20] = "Whitespace";
29
+ TokenType[TokenType["Includes"] = 21] = "Includes";
30
+ TokenType[TokenType["Dashmatch"] = 22] = "Dashmatch";
31
+ TokenType[TokenType["SubstringOperator"] = 23] = "SubstringOperator";
32
+ TokenType[TokenType["PrefixOperator"] = 24] = "PrefixOperator";
33
+ TokenType[TokenType["SuffixOperator"] = 25] = "SuffixOperator";
34
+ TokenType[TokenType["Delim"] = 26] = "Delim";
35
+ TokenType[TokenType["EMS"] = 27] = "EMS";
36
+ TokenType[TokenType["EXS"] = 28] = "EXS";
37
+ TokenType[TokenType["Length"] = 29] = "Length";
38
+ TokenType[TokenType["Angle"] = 30] = "Angle";
39
+ TokenType[TokenType["Time"] = 31] = "Time";
40
+ TokenType[TokenType["Freq"] = 32] = "Freq";
41
+ TokenType[TokenType["Exclamation"] = 33] = "Exclamation";
42
+ TokenType[TokenType["Resolution"] = 34] = "Resolution";
43
+ TokenType[TokenType["Comma"] = 35] = "Comma";
44
+ TokenType[TokenType["Charset"] = 36] = "Charset";
45
+ TokenType[TokenType["EscapedJavaScript"] = 37] = "EscapedJavaScript";
46
+ TokenType[TokenType["BadEscapedJavaScript"] = 38] = "BadEscapedJavaScript";
47
+ TokenType[TokenType["Comment"] = 39] = "Comment";
48
+ TokenType[TokenType["SingleLineComment"] = 40] = "SingleLineComment";
49
+ TokenType[TokenType["EOF"] = 41] = "EOF";
50
+ TokenType[TokenType["CustomToken"] = 42] = "CustomToken";
51
+ })(TokenType || (TokenType = {}));
52
+ export class MultiLineStream {
53
+ constructor(source) {
54
+ this.source = source;
55
+ this.len = source.length;
56
+ this.position = 0;
57
+ }
58
+ substring(from, to = this.position) {
59
+ return this.source.substring(from, to);
60
+ }
61
+ eos() {
62
+ return this.len <= this.position;
63
+ }
64
+ pos() {
65
+ return this.position;
66
+ }
67
+ goBackTo(pos) {
68
+ this.position = pos;
69
+ }
70
+ goBack(n) {
71
+ this.position -= n;
72
+ }
73
+ advance(n) {
74
+ this.position += n;
75
+ }
76
+ nextChar() {
77
+ return this.source.charCodeAt(this.position++) || 0;
78
+ }
79
+ peekChar(n = 0) {
80
+ return this.source.charCodeAt(this.position + n) || 0;
81
+ }
82
+ lookbackChar(n = 0) {
83
+ return this.source.charCodeAt(this.position - n) || 0;
84
+ }
85
+ advanceIfChar(ch) {
86
+ if (ch === this.source.charCodeAt(this.position)) {
87
+ this.position++;
88
+ return true;
89
+ }
90
+ return false;
91
+ }
92
+ advanceIfChars(ch) {
93
+ if (this.position + ch.length > this.source.length) {
94
+ return false;
95
+ }
96
+ let i = 0;
97
+ for (; i < ch.length; i++) {
98
+ if (this.source.charCodeAt(this.position + i) !== ch[i]) {
99
+ return false;
100
+ }
101
+ }
102
+ this.advance(i);
103
+ return true;
104
+ }
105
+ advanceWhileChar(condition) {
106
+ const posNow = this.position;
107
+ while (this.position < this.len && condition(this.source.charCodeAt(this.position))) {
108
+ this.position++;
109
+ }
110
+ return this.position - posNow;
111
+ }
112
+ }
113
+ const _a = 'a'.charCodeAt(0);
114
+ const _f = 'f'.charCodeAt(0);
115
+ const _z = 'z'.charCodeAt(0);
116
+ const _u = 'u'.charCodeAt(0);
117
+ const _A = 'A'.charCodeAt(0);
118
+ const _F = 'F'.charCodeAt(0);
119
+ const _Z = 'Z'.charCodeAt(0);
120
+ const _0 = '0'.charCodeAt(0);
121
+ const _9 = '9'.charCodeAt(0);
122
+ const _TLD = '~'.charCodeAt(0);
123
+ const _HAT = '^'.charCodeAt(0);
124
+ const _EQS = '='.charCodeAt(0);
125
+ const _PIP = '|'.charCodeAt(0);
126
+ const _MIN = '-'.charCodeAt(0);
127
+ const _USC = '_'.charCodeAt(0);
128
+ const _PRC = '%'.charCodeAt(0);
129
+ const _MUL = '*'.charCodeAt(0);
130
+ const _LPA = '('.charCodeAt(0);
131
+ const _RPA = ')'.charCodeAt(0);
132
+ const _LAN = '<'.charCodeAt(0);
133
+ const _RAN = '>'.charCodeAt(0);
134
+ const _ATS = '@'.charCodeAt(0);
135
+ const _HSH = '#'.charCodeAt(0);
136
+ const _DLR = '$'.charCodeAt(0);
137
+ const _BSL = '\\'.charCodeAt(0);
138
+ const _FSL = '/'.charCodeAt(0);
139
+ const _NWL = '\n'.charCodeAt(0);
140
+ const _CAR = '\r'.charCodeAt(0);
141
+ const _LFD = '\f'.charCodeAt(0);
142
+ const _DQO = '"'.charCodeAt(0);
143
+ const _SQO = '\''.charCodeAt(0);
144
+ const _WSP = ' '.charCodeAt(0);
145
+ const _TAB = '\t'.charCodeAt(0);
146
+ const _SEM = ';'.charCodeAt(0);
147
+ const _COL = ':'.charCodeAt(0);
148
+ const _CUL = '{'.charCodeAt(0);
149
+ const _CUR = '}'.charCodeAt(0);
150
+ const _BRL = '['.charCodeAt(0);
151
+ const _BRR = ']'.charCodeAt(0);
152
+ const _CMA = ','.charCodeAt(0);
153
+ const _DOT = '.'.charCodeAt(0);
154
+ const _BNG = '!'.charCodeAt(0);
155
+ const _QSM = '?'.charCodeAt(0);
156
+ const _PLS = '+'.charCodeAt(0);
157
+ const staticTokenTable = {};
158
+ staticTokenTable[_SEM] = TokenType.SemiColon;
159
+ staticTokenTable[_COL] = TokenType.Colon;
160
+ staticTokenTable[_CUL] = TokenType.CurlyL;
161
+ staticTokenTable[_CUR] = TokenType.CurlyR;
162
+ staticTokenTable[_BRR] = TokenType.BracketR;
163
+ staticTokenTable[_BRL] = TokenType.BracketL;
164
+ staticTokenTable[_LPA] = TokenType.ParenthesisL;
165
+ staticTokenTable[_RPA] = TokenType.ParenthesisR;
166
+ staticTokenTable[_CMA] = TokenType.Comma;
167
+ const staticUnitTable = {};
168
+ staticUnitTable['em'] = TokenType.EMS;
169
+ staticUnitTable['ex'] = TokenType.EXS;
170
+ staticUnitTable['px'] = TokenType.Length;
171
+ staticUnitTable['cm'] = TokenType.Length;
172
+ staticUnitTable['mm'] = TokenType.Length;
173
+ staticUnitTable['in'] = TokenType.Length;
174
+ staticUnitTable['pt'] = TokenType.Length;
175
+ staticUnitTable['pc'] = TokenType.Length;
176
+ staticUnitTable['deg'] = TokenType.Angle;
177
+ staticUnitTable['rad'] = TokenType.Angle;
178
+ staticUnitTable['grad'] = TokenType.Angle;
179
+ staticUnitTable['ms'] = TokenType.Time;
180
+ staticUnitTable['s'] = TokenType.Time;
181
+ staticUnitTable['hz'] = TokenType.Freq;
182
+ staticUnitTable['khz'] = TokenType.Freq;
183
+ staticUnitTable['%'] = TokenType.Percentage;
184
+ staticUnitTable['fr'] = TokenType.Percentage;
185
+ staticUnitTable['dpi'] = TokenType.Resolution;
186
+ staticUnitTable['dpcm'] = TokenType.Resolution;
187
+ export class Scanner {
188
+ constructor() {
189
+ this.stream = new MultiLineStream('');
190
+ this.ignoreComment = true;
191
+ this.ignoreWhitespace = true;
192
+ this.inURL = false;
193
+ }
194
+ setSource(input) {
195
+ this.stream = new MultiLineStream(input);
196
+ }
197
+ finishToken(offset, type, text) {
198
+ return {
199
+ offset: offset,
200
+ len: this.stream.pos() - offset,
201
+ type: type,
202
+ text: text || this.stream.substring(offset)
203
+ };
204
+ }
205
+ substring(offset, len) {
206
+ return this.stream.substring(offset, offset + len);
207
+ }
208
+ pos() {
209
+ return this.stream.pos();
210
+ }
211
+ goBackTo(pos) {
212
+ this.stream.goBackTo(pos);
213
+ }
214
+ scanUnquotedString() {
215
+ const offset = this.stream.pos();
216
+ const content = [];
217
+ if (this._unquotedString(content)) {
218
+ return this.finishToken(offset, TokenType.UnquotedString, content.join(''));
219
+ }
220
+ return null;
221
+ }
222
+ scan() {
223
+ // processes all whitespaces and comments
224
+ const triviaToken = this.trivia();
225
+ if (triviaToken !== null) {
226
+ return triviaToken;
227
+ }
228
+ const offset = this.stream.pos();
229
+ // End of file/input
230
+ if (this.stream.eos()) {
231
+ return this.finishToken(offset, TokenType.EOF);
232
+ }
233
+ return this.scanNext(offset);
234
+ }
235
+ /**
236
+ * Read the range as described in https://www.w3.org/TR/CSS21/syndata.html#tokenization
237
+ * Assume the `u` has aleady been consumed
238
+ * @returns if reading the unicode was successful
239
+ */
240
+ tryScanUnicode() {
241
+ const offset = this.stream.pos();
242
+ if (!this.stream.eos() && this._unicodeRange()) {
243
+ return this.finishToken(offset, TokenType.UnicodeRange);
244
+ }
245
+ this.stream.goBackTo(offset);
246
+ return undefined;
247
+ }
248
+ scanNext(offset) {
249
+ // CDO <!--
250
+ if (this.stream.advanceIfChars([_LAN, _BNG, _MIN, _MIN])) {
251
+ return this.finishToken(offset, TokenType.CDO);
252
+ }
253
+ // CDC -->
254
+ if (this.stream.advanceIfChars([_MIN, _MIN, _RAN])) {
255
+ return this.finishToken(offset, TokenType.CDC);
256
+ }
257
+ let content = [];
258
+ if (this.ident(content)) {
259
+ return this.finishToken(offset, TokenType.Ident, content.join(''));
260
+ }
261
+ // at-keyword
262
+ if (this.stream.advanceIfChar(_ATS)) {
263
+ content = ['@'];
264
+ if (this._name(content)) {
265
+ const keywordText = content.join('');
266
+ if (keywordText === '@charset') {
267
+ return this.finishToken(offset, TokenType.Charset, keywordText);
268
+ }
269
+ return this.finishToken(offset, TokenType.AtKeyword, keywordText);
270
+ }
271
+ else {
272
+ return this.finishToken(offset, TokenType.Delim);
273
+ }
274
+ }
275
+ // hash
276
+ if (this.stream.advanceIfChar(_HSH)) {
277
+ content = ['#'];
278
+ if (this._name(content)) {
279
+ return this.finishToken(offset, TokenType.Hash, content.join(''));
280
+ }
281
+ else {
282
+ return this.finishToken(offset, TokenType.Delim);
283
+ }
284
+ }
285
+ // Important
286
+ if (this.stream.advanceIfChar(_BNG)) {
287
+ return this.finishToken(offset, TokenType.Exclamation);
288
+ }
289
+ // Numbers
290
+ if (this._number()) {
291
+ const pos = this.stream.pos();
292
+ content = [this.stream.substring(offset, pos)];
293
+ if (this.stream.advanceIfChar(_PRC)) {
294
+ // Percentage 43%
295
+ return this.finishToken(offset, TokenType.Percentage);
296
+ }
297
+ else if (this.ident(content)) {
298
+ const dim = this.stream.substring(pos).toLowerCase();
299
+ const tokenType = staticUnitTable[dim];
300
+ if (typeof tokenType !== 'undefined') {
301
+ // Known dimension 43px
302
+ return this.finishToken(offset, tokenType, content.join(''));
303
+ }
304
+ else {
305
+ // Unknown dimension 43ft
306
+ return this.finishToken(offset, TokenType.Dimension, content.join(''));
307
+ }
308
+ }
309
+ return this.finishToken(offset, TokenType.Num);
310
+ }
311
+ // String, BadString
312
+ content = [];
313
+ let tokenType = this._string(content);
314
+ if (tokenType !== null) {
315
+ return this.finishToken(offset, tokenType, content.join(''));
316
+ }
317
+ // single character tokens
318
+ tokenType = staticTokenTable[this.stream.peekChar()];
319
+ if (typeof tokenType !== 'undefined') {
320
+ this.stream.advance(1);
321
+ return this.finishToken(offset, tokenType);
322
+ }
323
+ // includes ~=
324
+ if (this.stream.peekChar(0) === _TLD && this.stream.peekChar(1) === _EQS) {
325
+ this.stream.advance(2);
326
+ return this.finishToken(offset, TokenType.Includes);
327
+ }
328
+ // DashMatch |=
329
+ if (this.stream.peekChar(0) === _PIP && this.stream.peekChar(1) === _EQS) {
330
+ this.stream.advance(2);
331
+ return this.finishToken(offset, TokenType.Dashmatch);
332
+ }
333
+ // Substring operator *=
334
+ if (this.stream.peekChar(0) === _MUL && this.stream.peekChar(1) === _EQS) {
335
+ this.stream.advance(2);
336
+ return this.finishToken(offset, TokenType.SubstringOperator);
337
+ }
338
+ // Substring operator ^=
339
+ if (this.stream.peekChar(0) === _HAT && this.stream.peekChar(1) === _EQS) {
340
+ this.stream.advance(2);
341
+ return this.finishToken(offset, TokenType.PrefixOperator);
342
+ }
343
+ // Substring operator $=
344
+ if (this.stream.peekChar(0) === _DLR && this.stream.peekChar(1) === _EQS) {
345
+ this.stream.advance(2);
346
+ return this.finishToken(offset, TokenType.SuffixOperator);
347
+ }
348
+ // Delim
349
+ this.stream.nextChar();
350
+ return this.finishToken(offset, TokenType.Delim);
351
+ }
352
+ trivia() {
353
+ while (true) {
354
+ const offset = this.stream.pos();
355
+ if (this._whitespace()) {
356
+ if (!this.ignoreWhitespace) {
357
+ return this.finishToken(offset, TokenType.Whitespace);
358
+ }
359
+ }
360
+ else if (this.comment()) {
361
+ if (!this.ignoreComment) {
362
+ return this.finishToken(offset, TokenType.Comment);
363
+ }
364
+ }
365
+ else {
366
+ return null;
367
+ }
368
+ }
369
+ }
370
+ comment() {
371
+ if (this.stream.advanceIfChars([_FSL, _MUL])) {
372
+ let success = false, hot = false;
373
+ this.stream.advanceWhileChar((ch) => {
374
+ if (hot && ch === _FSL) {
375
+ success = true;
376
+ return false;
377
+ }
378
+ hot = ch === _MUL;
379
+ return true;
380
+ });
381
+ if (success) {
382
+ this.stream.advance(1);
383
+ }
384
+ return true;
385
+ }
386
+ return false;
387
+ }
388
+ _number() {
389
+ let npeek = 0, ch;
390
+ if (this.stream.peekChar() === _DOT) {
391
+ npeek = 1;
392
+ }
393
+ ch = this.stream.peekChar(npeek);
394
+ if (ch >= _0 && ch <= _9) {
395
+ this.stream.advance(npeek + 1);
396
+ this.stream.advanceWhileChar((ch) => {
397
+ return ch >= _0 && ch <= _9 || npeek === 0 && ch === _DOT;
398
+ });
399
+ return true;
400
+ }
401
+ return false;
402
+ }
403
+ _newline(result) {
404
+ const ch = this.stream.peekChar();
405
+ switch (ch) {
406
+ case _CAR:
407
+ case _LFD:
408
+ case _NWL:
409
+ this.stream.advance(1);
410
+ result.push(String.fromCharCode(ch));
411
+ if (ch === _CAR && this.stream.advanceIfChar(_NWL)) {
412
+ result.push('\n');
413
+ }
414
+ return true;
415
+ }
416
+ return false;
417
+ }
418
+ _escape(result, includeNewLines) {
419
+ let ch = this.stream.peekChar();
420
+ if (ch === _BSL) {
421
+ this.stream.advance(1);
422
+ ch = this.stream.peekChar();
423
+ let hexNumCount = 0;
424
+ while (hexNumCount < 6 && (ch >= _0 && ch <= _9 || ch >= _a && ch <= _f || ch >= _A && ch <= _F)) {
425
+ this.stream.advance(1);
426
+ ch = this.stream.peekChar();
427
+ hexNumCount++;
428
+ }
429
+ if (hexNumCount > 0) {
430
+ try {
431
+ const hexVal = parseInt(this.stream.substring(this.stream.pos() - hexNumCount), 16);
432
+ if (hexVal) {
433
+ result.push(String.fromCharCode(hexVal));
434
+ }
435
+ }
436
+ catch (e) {
437
+ // ignore
438
+ }
439
+ // optional whitespace or new line, not part of result text
440
+ if (ch === _WSP || ch === _TAB) {
441
+ this.stream.advance(1);
442
+ }
443
+ else {
444
+ this._newline([]);
445
+ }
446
+ return true;
447
+ }
448
+ if (ch !== _CAR && ch !== _LFD && ch !== _NWL) {
449
+ this.stream.advance(1);
450
+ result.push(String.fromCharCode(ch));
451
+ return true;
452
+ }
453
+ else if (includeNewLines) {
454
+ return this._newline(result);
455
+ }
456
+ }
457
+ return false;
458
+ }
459
+ _stringChar(closeQuote, result) {
460
+ // not closeQuote, not backslash, not newline
461
+ const ch = this.stream.peekChar();
462
+ if (ch !== 0 && ch !== closeQuote && ch !== _BSL && ch !== _CAR && ch !== _LFD && ch !== _NWL) {
463
+ this.stream.advance(1);
464
+ result.push(String.fromCharCode(ch));
465
+ return true;
466
+ }
467
+ return false;
468
+ }
469
+ _string(result) {
470
+ if (this.stream.peekChar() === _SQO || this.stream.peekChar() === _DQO) {
471
+ const closeQuote = this.stream.nextChar();
472
+ result.push(String.fromCharCode(closeQuote));
473
+ while (this._stringChar(closeQuote, result) || this._escape(result, true)) {
474
+ // loop
475
+ }
476
+ if (this.stream.peekChar() === closeQuote) {
477
+ this.stream.nextChar();
478
+ result.push(String.fromCharCode(closeQuote));
479
+ return TokenType.String;
480
+ }
481
+ else {
482
+ return TokenType.BadString;
483
+ }
484
+ }
485
+ return null;
486
+ }
487
+ _unquotedChar(result) {
488
+ // not closeQuote, not backslash, not newline
489
+ const ch = this.stream.peekChar();
490
+ if (ch !== 0 && ch !== _BSL && ch !== _SQO && ch !== _DQO && ch !== _LPA && ch !== _RPA && ch !== _WSP && ch !== _TAB && ch !== _NWL && ch !== _LFD && ch !== _CAR) {
491
+ this.stream.advance(1);
492
+ result.push(String.fromCharCode(ch));
493
+ return true;
494
+ }
495
+ return false;
496
+ }
497
+ _unquotedString(result) {
498
+ let hasContent = false;
499
+ while (this._unquotedChar(result) || this._escape(result)) {
500
+ hasContent = true;
501
+ }
502
+ return hasContent;
503
+ }
504
+ _whitespace() {
505
+ const n = this.stream.advanceWhileChar((ch) => {
506
+ return ch === _WSP || ch === _TAB || ch === _NWL || ch === _LFD || ch === _CAR;
507
+ });
508
+ return n > 0;
509
+ }
510
+ _name(result) {
511
+ let matched = false;
512
+ while (this._identChar(result) || this._escape(result)) {
513
+ matched = true;
514
+ }
515
+ return matched;
516
+ }
517
+ ident(result) {
518
+ const pos = this.stream.pos();
519
+ const hasMinus = this._minus(result);
520
+ if (hasMinus) {
521
+ if (this._minus(result) /* -- */ || this._identFirstChar(result) || this._escape(result)) {
522
+ while (this._identChar(result) || this._escape(result)) {
523
+ // loop
524
+ }
525
+ return true;
526
+ }
527
+ }
528
+ else if (this._identFirstChar(result) || this._escape(result)) {
529
+ while (this._identChar(result) || this._escape(result)) {
530
+ // loop
531
+ }
532
+ return true;
533
+ }
534
+ this.stream.goBackTo(pos);
535
+ return false;
536
+ }
537
+ _identFirstChar(result) {
538
+ const ch = this.stream.peekChar();
539
+ if (ch === _USC || // _
540
+ ch >= _a && ch <= _z || // a-z
541
+ ch >= _A && ch <= _Z || // A-Z
542
+ ch >= 0x80 && ch <= 0xFFFF) { // nonascii
543
+ this.stream.advance(1);
544
+ result.push(String.fromCharCode(ch));
545
+ return true;
546
+ }
547
+ return false;
548
+ }
549
+ _minus(result) {
550
+ const ch = this.stream.peekChar();
551
+ if (ch === _MIN) {
552
+ this.stream.advance(1);
553
+ result.push(String.fromCharCode(ch));
554
+ return true;
555
+ }
556
+ return false;
557
+ }
558
+ _identChar(result) {
559
+ const ch = this.stream.peekChar();
560
+ if (ch === _USC || // _
561
+ ch === _MIN || // -
562
+ ch >= _a && ch <= _z || // a-z
563
+ ch >= _A && ch <= _Z || // A-Z
564
+ ch >= _0 && ch <= _9 || // 0/9
565
+ ch >= 0x80 && ch <= 0xFFFF) { // nonascii
566
+ this.stream.advance(1);
567
+ result.push(String.fromCharCode(ch));
568
+ return true;
569
+ }
570
+ return false;
571
+ }
572
+ _unicodeRange() {
573
+ // follow https://www.w3.org/TR/CSS21/syndata.html#tokenization and https://www.w3.org/TR/css-syntax-3/#urange-syntax
574
+ // assume u has already been parsed
575
+ if (this.stream.advanceIfChar(_PLS)) {
576
+ const isHexDigit = (ch) => (ch >= _0 && ch <= _9 || ch >= _a && ch <= _f || ch >= _A && ch <= _F);
577
+ const codePoints = this.stream.advanceWhileChar(isHexDigit) + this.stream.advanceWhileChar(ch => ch === _QSM);
578
+ if (codePoints >= 1 && codePoints <= 6) {
579
+ if (this.stream.advanceIfChar(_MIN)) {
580
+ const digits = this.stream.advanceWhileChar(isHexDigit);
581
+ if (digits >= 1 && digits <= 6) {
582
+ return true;
583
+ }
584
+ }
585
+ else {
586
+ return true;
587
+ }
588
+ }
589
+ }
590
+ return false;
591
+ }
592
+ }