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,472 +1,469 @@
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
- import * as nodes from '../parser/cssNodes';
6
- import * as nls from 'vscode-nls';
7
- var localize = nls.loadMessageBundle();
8
- export var colorFunctions = [
9
- { func: 'rgb($red, $green, $blue)', desc: localize('css.builtin.rgb', 'Creates a Color from red, green, and blue values.') },
10
- { func: 'rgba($red, $green, $blue, $alpha)', desc: localize('css.builtin.rgba', 'Creates a Color from red, green, blue, and alpha values.') },
11
- { func: 'hsl($hue, $saturation, $lightness)', desc: localize('css.builtin.hsl', 'Creates a Color from hue, saturation, and lightness values.') },
12
- { func: 'hsla($hue, $saturation, $lightness, $alpha)', desc: localize('css.builtin.hsla', 'Creates a Color from hue, saturation, lightness, and alpha values.') },
13
- { func: 'hwb($hue $white $black)', desc: localize('css.builtin.hwb', 'Creates a Color from hue, white and black.') }
14
- ];
15
- export var colors = {
16
- aliceblue: '#f0f8ff',
17
- antiquewhite: '#faebd7',
18
- aqua: '#00ffff',
19
- aquamarine: '#7fffd4',
20
- azure: '#f0ffff',
21
- beige: '#f5f5dc',
22
- bisque: '#ffe4c4',
23
- black: '#000000',
24
- blanchedalmond: '#ffebcd',
25
- blue: '#0000ff',
26
- blueviolet: '#8a2be2',
27
- brown: '#a52a2a',
28
- burlywood: '#deb887',
29
- cadetblue: '#5f9ea0',
30
- chartreuse: '#7fff00',
31
- chocolate: '#d2691e',
32
- coral: '#ff7f50',
33
- cornflowerblue: '#6495ed',
34
- cornsilk: '#fff8dc',
35
- crimson: '#dc143c',
36
- cyan: '#00ffff',
37
- darkblue: '#00008b',
38
- darkcyan: '#008b8b',
39
- darkgoldenrod: '#b8860b',
40
- darkgray: '#a9a9a9',
41
- darkgrey: '#a9a9a9',
42
- darkgreen: '#006400',
43
- darkkhaki: '#bdb76b',
44
- darkmagenta: '#8b008b',
45
- darkolivegreen: '#556b2f',
46
- darkorange: '#ff8c00',
47
- darkorchid: '#9932cc',
48
- darkred: '#8b0000',
49
- darksalmon: '#e9967a',
50
- darkseagreen: '#8fbc8f',
51
- darkslateblue: '#483d8b',
52
- darkslategray: '#2f4f4f',
53
- darkslategrey: '#2f4f4f',
54
- darkturquoise: '#00ced1',
55
- darkviolet: '#9400d3',
56
- deeppink: '#ff1493',
57
- deepskyblue: '#00bfff',
58
- dimgray: '#696969',
59
- dimgrey: '#696969',
60
- dodgerblue: '#1e90ff',
61
- firebrick: '#b22222',
62
- floralwhite: '#fffaf0',
63
- forestgreen: '#228b22',
64
- fuchsia: '#ff00ff',
65
- gainsboro: '#dcdcdc',
66
- ghostwhite: '#f8f8ff',
67
- gold: '#ffd700',
68
- goldenrod: '#daa520',
69
- gray: '#808080',
70
- grey: '#808080',
71
- green: '#008000',
72
- greenyellow: '#adff2f',
73
- honeydew: '#f0fff0',
74
- hotpink: '#ff69b4',
75
- indianred: '#cd5c5c',
76
- indigo: '#4b0082',
77
- ivory: '#fffff0',
78
- khaki: '#f0e68c',
79
- lavender: '#e6e6fa',
80
- lavenderblush: '#fff0f5',
81
- lawngreen: '#7cfc00',
82
- lemonchiffon: '#fffacd',
83
- lightblue: '#add8e6',
84
- lightcoral: '#f08080',
85
- lightcyan: '#e0ffff',
86
- lightgoldenrodyellow: '#fafad2',
87
- lightgray: '#d3d3d3',
88
- lightgrey: '#d3d3d3',
89
- lightgreen: '#90ee90',
90
- lightpink: '#ffb6c1',
91
- lightsalmon: '#ffa07a',
92
- lightseagreen: '#20b2aa',
93
- lightskyblue: '#87cefa',
94
- lightslategray: '#778899',
95
- lightslategrey: '#778899',
96
- lightsteelblue: '#b0c4de',
97
- lightyellow: '#ffffe0',
98
- lime: '#00ff00',
99
- limegreen: '#32cd32',
100
- linen: '#faf0e6',
101
- magenta: '#ff00ff',
102
- maroon: '#800000',
103
- mediumaquamarine: '#66cdaa',
104
- mediumblue: '#0000cd',
105
- mediumorchid: '#ba55d3',
106
- mediumpurple: '#9370d8',
107
- mediumseagreen: '#3cb371',
108
- mediumslateblue: '#7b68ee',
109
- mediumspringgreen: '#00fa9a',
110
- mediumturquoise: '#48d1cc',
111
- mediumvioletred: '#c71585',
112
- midnightblue: '#191970',
113
- mintcream: '#f5fffa',
114
- mistyrose: '#ffe4e1',
115
- moccasin: '#ffe4b5',
116
- navajowhite: '#ffdead',
117
- navy: '#000080',
118
- oldlace: '#fdf5e6',
119
- olive: '#808000',
120
- olivedrab: '#6b8e23',
121
- orange: '#ffa500',
122
- orangered: '#ff4500',
123
- orchid: '#da70d6',
124
- palegoldenrod: '#eee8aa',
125
- palegreen: '#98fb98',
126
- paleturquoise: '#afeeee',
127
- palevioletred: '#d87093',
128
- papayawhip: '#ffefd5',
129
- peachpuff: '#ffdab9',
130
- peru: '#cd853f',
131
- pink: '#ffc0cb',
132
- plum: '#dda0dd',
133
- powderblue: '#b0e0e6',
134
- purple: '#800080',
135
- red: '#ff0000',
136
- rebeccapurple: '#663399',
137
- rosybrown: '#bc8f8f',
138
- royalblue: '#4169e1',
139
- saddlebrown: '#8b4513',
140
- salmon: '#fa8072',
141
- sandybrown: '#f4a460',
142
- seagreen: '#2e8b57',
143
- seashell: '#fff5ee',
144
- sienna: '#a0522d',
145
- silver: '#c0c0c0',
146
- skyblue: '#87ceeb',
147
- slateblue: '#6a5acd',
148
- slategray: '#708090',
149
- slategrey: '#708090',
150
- snow: '#fffafa',
151
- springgreen: '#00ff7f',
152
- steelblue: '#4682b4',
153
- tan: '#d2b48c',
154
- teal: '#008080',
155
- thistle: '#d8bfd8',
156
- tomato: '#ff6347',
157
- turquoise: '#40e0d0',
158
- violet: '#ee82ee',
159
- wheat: '#f5deb3',
160
- white: '#ffffff',
161
- whitesmoke: '#f5f5f5',
162
- yellow: '#ffff00',
163
- yellowgreen: '#9acd32'
164
- };
165
- export var colorKeywords = {
166
- 'currentColor': 'The value of the \'color\' property. The computed value of the \'currentColor\' keyword is the computed value of the \'color\' property. If the \'currentColor\' keyword is set on the \'color\' property itself, it is treated as \'color:inherit\' at parse time.',
167
- 'transparent': 'Fully transparent. This keyword can be considered a shorthand for rgba(0,0,0,0) which is its computed value.',
168
- };
169
- function getNumericValue(node, factor) {
170
- var val = node.getText();
171
- var m = val.match(/^([-+]?[0-9]*\.?[0-9]+)(%?)$/);
172
- if (m) {
173
- if (m[2]) {
174
- factor = 100.0;
175
- }
176
- var result = parseFloat(m[1]) / factor;
177
- if (result >= 0 && result <= 1) {
178
- return result;
179
- }
180
- }
181
- throw new Error();
182
- }
183
- function getAngle(node) {
184
- var val = node.getText();
185
- var m = val.match(/^([-+]?[0-9]*\.?[0-9]+)(deg|rad|grad|turn)?$/);
186
- if (m) {
187
- switch (m[2]) {
188
- case 'deg':
189
- return parseFloat(val) % 360;
190
- case 'rad':
191
- return (parseFloat(val) * 180 / Math.PI) % 360;
192
- case 'grad':
193
- return (parseFloat(val) * 0.9) % 360;
194
- case 'turn':
195
- return (parseFloat(val) * 360) % 360;
196
- default:
197
- if ('undefined' === typeof m[2]) {
198
- return parseFloat(val) % 360;
199
- }
200
- }
201
- }
202
- throw new Error();
203
- }
204
- export function isColorConstructor(node) {
205
- var name = node.getName();
206
- if (!name) {
207
- return false;
208
- }
209
- return /^(rgb|rgba|hsl|hsla|hwb)$/gi.test(name);
210
- }
211
- /**
212
- * Returns true if the node is a color value - either
213
- * defined a hex number, as rgb or rgba function, or
214
- * as color name.
215
- */
216
- export function isColorValue(node) {
217
- if (node.type === nodes.NodeType.HexColorValue) {
218
- return true;
219
- }
220
- else if (node.type === nodes.NodeType.Function) {
221
- return isColorConstructor(node);
222
- }
223
- else if (node.type === nodes.NodeType.Identifier) {
224
- if (node.parent && node.parent.type !== nodes.NodeType.Term) {
225
- return false;
226
- }
227
- var candidateColor = node.getText().toLowerCase();
228
- if (candidateColor === 'none') {
229
- return false;
230
- }
231
- if (colors[candidateColor]) {
232
- return true;
233
- }
234
- }
235
- return false;
236
- }
237
- var Digit0 = 48;
238
- var Digit9 = 57;
239
- var A = 65;
240
- var F = 70;
241
- var a = 97;
242
- var f = 102;
243
- export function hexDigit(charCode) {
244
- if (charCode < Digit0) {
245
- return 0;
246
- }
247
- if (charCode <= Digit9) {
248
- return charCode - Digit0;
249
- }
250
- if (charCode < a) {
251
- charCode += (a - A);
252
- }
253
- if (charCode >= a && charCode <= f) {
254
- return charCode - a + 10;
255
- }
256
- return 0;
257
- }
258
- export function colorFromHex(text) {
259
- if (text[0] !== '#') {
260
- return null;
261
- }
262
- switch (text.length) {
263
- case 4:
264
- return {
265
- red: (hexDigit(text.charCodeAt(1)) * 0x11) / 255.0,
266
- green: (hexDigit(text.charCodeAt(2)) * 0x11) / 255.0,
267
- blue: (hexDigit(text.charCodeAt(3)) * 0x11) / 255.0,
268
- alpha: 1
269
- };
270
- case 5:
271
- return {
272
- red: (hexDigit(text.charCodeAt(1)) * 0x11) / 255.0,
273
- green: (hexDigit(text.charCodeAt(2)) * 0x11) / 255.0,
274
- blue: (hexDigit(text.charCodeAt(3)) * 0x11) / 255.0,
275
- alpha: (hexDigit(text.charCodeAt(4)) * 0x11) / 255.0,
276
- };
277
- case 7:
278
- return {
279
- red: (hexDigit(text.charCodeAt(1)) * 0x10 + hexDigit(text.charCodeAt(2))) / 255.0,
280
- green: (hexDigit(text.charCodeAt(3)) * 0x10 + hexDigit(text.charCodeAt(4))) / 255.0,
281
- blue: (hexDigit(text.charCodeAt(5)) * 0x10 + hexDigit(text.charCodeAt(6))) / 255.0,
282
- alpha: 1
283
- };
284
- case 9:
285
- return {
286
- red: (hexDigit(text.charCodeAt(1)) * 0x10 + hexDigit(text.charCodeAt(2))) / 255.0,
287
- green: (hexDigit(text.charCodeAt(3)) * 0x10 + hexDigit(text.charCodeAt(4))) / 255.0,
288
- blue: (hexDigit(text.charCodeAt(5)) * 0x10 + hexDigit(text.charCodeAt(6))) / 255.0,
289
- alpha: (hexDigit(text.charCodeAt(7)) * 0x10 + hexDigit(text.charCodeAt(8))) / 255.0
290
- };
291
- }
292
- return null;
293
- }
294
- export function colorFrom256RGB(red, green, blue, alpha) {
295
- if (alpha === void 0) { alpha = 1.0; }
296
- return {
297
- red: red / 255.0,
298
- green: green / 255.0,
299
- blue: blue / 255.0,
300
- alpha: alpha
301
- };
302
- }
303
- export function colorFromHSL(hue, sat, light, alpha) {
304
- if (alpha === void 0) { alpha = 1.0; }
305
- hue = hue / 60.0;
306
- if (sat === 0) {
307
- return { red: light, green: light, blue: light, alpha: alpha };
308
- }
309
- else {
310
- var hueToRgb = function (t1, t2, hue) {
311
- while (hue < 0) {
312
- hue += 6;
313
- }
314
- while (hue >= 6) {
315
- hue -= 6;
316
- }
317
- if (hue < 1) {
318
- return (t2 - t1) * hue + t1;
319
- }
320
- if (hue < 3) {
321
- return t2;
322
- }
323
- if (hue < 4) {
324
- return (t2 - t1) * (4 - hue) + t1;
325
- }
326
- return t1;
327
- };
328
- var t2 = light <= 0.5 ? (light * (sat + 1)) : (light + sat - (light * sat));
329
- var t1 = light * 2 - t2;
330
- return { red: hueToRgb(t1, t2, hue + 2), green: hueToRgb(t1, t2, hue), blue: hueToRgb(t1, t2, hue - 2), alpha: alpha };
331
- }
332
- }
333
- export function hslFromColor(rgba) {
334
- var r = rgba.red;
335
- var g = rgba.green;
336
- var b = rgba.blue;
337
- var a = rgba.alpha;
338
- var max = Math.max(r, g, b);
339
- var min = Math.min(r, g, b);
340
- var h = 0;
341
- var s = 0;
342
- var l = (min + max) / 2;
343
- var chroma = max - min;
344
- if (chroma > 0) {
345
- s = Math.min((l <= 0.5 ? chroma / (2 * l) : chroma / (2 - (2 * l))), 1);
346
- switch (max) {
347
- case r:
348
- h = (g - b) / chroma + (g < b ? 6 : 0);
349
- break;
350
- case g:
351
- h = (b - r) / chroma + 2;
352
- break;
353
- case b:
354
- h = (r - g) / chroma + 4;
355
- break;
356
- }
357
- h *= 60;
358
- h = Math.round(h);
359
- }
360
- return { h: h, s: s, l: l, a: a };
361
- }
362
- export function colorFromHWB(hue, white, black, alpha) {
363
- if (alpha === void 0) { alpha = 1.0; }
364
- if (white + black >= 1) {
365
- var gray = white / (white + black);
366
- return { red: gray, green: gray, blue: gray, alpha: alpha };
367
- }
368
- var rgb = colorFromHSL(hue, 1, 0.5, alpha);
369
- var red = rgb.red;
370
- red *= (1 - white - black);
371
- red += white;
372
- var green = rgb.green;
373
- green *= (1 - white - black);
374
- green += white;
375
- var blue = rgb.blue;
376
- blue *= (1 - white - black);
377
- blue += white;
378
- return {
379
- red: red,
380
- green: green,
381
- blue: blue,
382
- alpha: alpha
383
- };
384
- }
385
- export function hwbFromColor(rgba) {
386
- var hsl = hslFromColor(rgba);
387
- var white = Math.min(rgba.red, rgba.green, rgba.blue);
388
- var black = 1 - Math.max(rgba.red, rgba.green, rgba.blue);
389
- return {
390
- h: hsl.h,
391
- w: white,
392
- b: black,
393
- a: hsl.a
394
- };
395
- }
396
- export function getColorValue(node) {
397
- if (node.type === nodes.NodeType.HexColorValue) {
398
- var text = node.getText();
399
- return colorFromHex(text);
400
- }
401
- else if (node.type === nodes.NodeType.Function) {
402
- var functionNode = node;
403
- var name = functionNode.getName();
404
- var colorValues = functionNode.getArguments().getChildren();
405
- if (colorValues.length === 1) {
406
- var functionArg = colorValues[0].getChildren();
407
- if (functionArg.length === 1 && functionArg[0].type === nodes.NodeType.Expression) {
408
- colorValues = functionArg[0].getChildren();
409
- if (colorValues.length === 3) {
410
- var lastValue = colorValues[2];
411
- if (lastValue instanceof nodes.BinaryExpression) {
412
- var left = lastValue.getLeft(), right = lastValue.getRight(), operator = lastValue.getOperator();
413
- if (left && right && operator && operator.matches('/')) {
414
- colorValues = [colorValues[0], colorValues[1], left, right];
415
- }
416
- }
417
- }
418
- }
419
- }
420
- if (!name || colorValues.length < 3 || colorValues.length > 4) {
421
- return null;
422
- }
423
- try {
424
- var alpha = colorValues.length === 4 ? getNumericValue(colorValues[3], 1) : 1;
425
- if (name === 'rgb' || name === 'rgba') {
426
- return {
427
- red: getNumericValue(colorValues[0], 255.0),
428
- green: getNumericValue(colorValues[1], 255.0),
429
- blue: getNumericValue(colorValues[2], 255.0),
430
- alpha: alpha
431
- };
432
- }
433
- else if (name === 'hsl' || name === 'hsla') {
434
- var h = getAngle(colorValues[0]);
435
- var s = getNumericValue(colorValues[1], 100.0);
436
- var l = getNumericValue(colorValues[2], 100.0);
437
- return colorFromHSL(h, s, l, alpha);
438
- }
439
- else if (name === 'hwb') {
440
- var h = getAngle(colorValues[0]);
441
- var w = getNumericValue(colorValues[1], 100.0);
442
- var b = getNumericValue(colorValues[2], 100.0);
443
- return colorFromHWB(h, w, b, alpha);
444
- }
445
- }
446
- catch (e) {
447
- // parse error on numeric value
448
- return null;
449
- }
450
- }
451
- else if (node.type === nodes.NodeType.Identifier) {
452
- if (node.parent && node.parent.type !== nodes.NodeType.Term) {
453
- return null;
454
- }
455
- var term = node.parent;
456
- if (term && term.parent && term.parent.type === nodes.NodeType.BinaryExpression) {
457
- var expression = term.parent;
458
- if (expression.parent && expression.parent.type === nodes.NodeType.ListEntry && expression.parent.key === expression) {
459
- return null;
460
- }
461
- }
462
- var candidateColor = node.getText().toLowerCase();
463
- if (candidateColor === 'none') {
464
- return null;
465
- }
466
- var colorHex = colors[candidateColor];
467
- if (colorHex) {
468
- return colorFromHex(colorHex);
469
- }
470
- }
471
- return null;
472
- }
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
+ import * as nodes from '../parser/cssNodes';
6
+ import * as nls from 'vscode-nls';
7
+ const localize = nls.loadMessageBundle();
8
+ export const colorFunctions = [
9
+ { func: 'rgb($red, $green, $blue)', desc: localize('css.builtin.rgb', 'Creates a Color from red, green, and blue values.') },
10
+ { func: 'rgba($red, $green, $blue, $alpha)', desc: localize('css.builtin.rgba', 'Creates a Color from red, green, blue, and alpha values.') },
11
+ { func: 'hsl($hue, $saturation, $lightness)', desc: localize('css.builtin.hsl', 'Creates a Color from hue, saturation, and lightness values.') },
12
+ { func: 'hsla($hue, $saturation, $lightness, $alpha)', desc: localize('css.builtin.hsla', 'Creates a Color from hue, saturation, lightness, and alpha values.') },
13
+ { func: 'hwb($hue $white $black)', desc: localize('css.builtin.hwb', 'Creates a Color from hue, white and black.') }
14
+ ];
15
+ export const colors = {
16
+ aliceblue: '#f0f8ff',
17
+ antiquewhite: '#faebd7',
18
+ aqua: '#00ffff',
19
+ aquamarine: '#7fffd4',
20
+ azure: '#f0ffff',
21
+ beige: '#f5f5dc',
22
+ bisque: '#ffe4c4',
23
+ black: '#000000',
24
+ blanchedalmond: '#ffebcd',
25
+ blue: '#0000ff',
26
+ blueviolet: '#8a2be2',
27
+ brown: '#a52a2a',
28
+ burlywood: '#deb887',
29
+ cadetblue: '#5f9ea0',
30
+ chartreuse: '#7fff00',
31
+ chocolate: '#d2691e',
32
+ coral: '#ff7f50',
33
+ cornflowerblue: '#6495ed',
34
+ cornsilk: '#fff8dc',
35
+ crimson: '#dc143c',
36
+ cyan: '#00ffff',
37
+ darkblue: '#00008b',
38
+ darkcyan: '#008b8b',
39
+ darkgoldenrod: '#b8860b',
40
+ darkgray: '#a9a9a9',
41
+ darkgrey: '#a9a9a9',
42
+ darkgreen: '#006400',
43
+ darkkhaki: '#bdb76b',
44
+ darkmagenta: '#8b008b',
45
+ darkolivegreen: '#556b2f',
46
+ darkorange: '#ff8c00',
47
+ darkorchid: '#9932cc',
48
+ darkred: '#8b0000',
49
+ darksalmon: '#e9967a',
50
+ darkseagreen: '#8fbc8f',
51
+ darkslateblue: '#483d8b',
52
+ darkslategray: '#2f4f4f',
53
+ darkslategrey: '#2f4f4f',
54
+ darkturquoise: '#00ced1',
55
+ darkviolet: '#9400d3',
56
+ deeppink: '#ff1493',
57
+ deepskyblue: '#00bfff',
58
+ dimgray: '#696969',
59
+ dimgrey: '#696969',
60
+ dodgerblue: '#1e90ff',
61
+ firebrick: '#b22222',
62
+ floralwhite: '#fffaf0',
63
+ forestgreen: '#228b22',
64
+ fuchsia: '#ff00ff',
65
+ gainsboro: '#dcdcdc',
66
+ ghostwhite: '#f8f8ff',
67
+ gold: '#ffd700',
68
+ goldenrod: '#daa520',
69
+ gray: '#808080',
70
+ grey: '#808080',
71
+ green: '#008000',
72
+ greenyellow: '#adff2f',
73
+ honeydew: '#f0fff0',
74
+ hotpink: '#ff69b4',
75
+ indianred: '#cd5c5c',
76
+ indigo: '#4b0082',
77
+ ivory: '#fffff0',
78
+ khaki: '#f0e68c',
79
+ lavender: '#e6e6fa',
80
+ lavenderblush: '#fff0f5',
81
+ lawngreen: '#7cfc00',
82
+ lemonchiffon: '#fffacd',
83
+ lightblue: '#add8e6',
84
+ lightcoral: '#f08080',
85
+ lightcyan: '#e0ffff',
86
+ lightgoldenrodyellow: '#fafad2',
87
+ lightgray: '#d3d3d3',
88
+ lightgrey: '#d3d3d3',
89
+ lightgreen: '#90ee90',
90
+ lightpink: '#ffb6c1',
91
+ lightsalmon: '#ffa07a',
92
+ lightseagreen: '#20b2aa',
93
+ lightskyblue: '#87cefa',
94
+ lightslategray: '#778899',
95
+ lightslategrey: '#778899',
96
+ lightsteelblue: '#b0c4de',
97
+ lightyellow: '#ffffe0',
98
+ lime: '#00ff00',
99
+ limegreen: '#32cd32',
100
+ linen: '#faf0e6',
101
+ magenta: '#ff00ff',
102
+ maroon: '#800000',
103
+ mediumaquamarine: '#66cdaa',
104
+ mediumblue: '#0000cd',
105
+ mediumorchid: '#ba55d3',
106
+ mediumpurple: '#9370d8',
107
+ mediumseagreen: '#3cb371',
108
+ mediumslateblue: '#7b68ee',
109
+ mediumspringgreen: '#00fa9a',
110
+ mediumturquoise: '#48d1cc',
111
+ mediumvioletred: '#c71585',
112
+ midnightblue: '#191970',
113
+ mintcream: '#f5fffa',
114
+ mistyrose: '#ffe4e1',
115
+ moccasin: '#ffe4b5',
116
+ navajowhite: '#ffdead',
117
+ navy: '#000080',
118
+ oldlace: '#fdf5e6',
119
+ olive: '#808000',
120
+ olivedrab: '#6b8e23',
121
+ orange: '#ffa500',
122
+ orangered: '#ff4500',
123
+ orchid: '#da70d6',
124
+ palegoldenrod: '#eee8aa',
125
+ palegreen: '#98fb98',
126
+ paleturquoise: '#afeeee',
127
+ palevioletred: '#d87093',
128
+ papayawhip: '#ffefd5',
129
+ peachpuff: '#ffdab9',
130
+ peru: '#cd853f',
131
+ pink: '#ffc0cb',
132
+ plum: '#dda0dd',
133
+ powderblue: '#b0e0e6',
134
+ purple: '#800080',
135
+ red: '#ff0000',
136
+ rebeccapurple: '#663399',
137
+ rosybrown: '#bc8f8f',
138
+ royalblue: '#4169e1',
139
+ saddlebrown: '#8b4513',
140
+ salmon: '#fa8072',
141
+ sandybrown: '#f4a460',
142
+ seagreen: '#2e8b57',
143
+ seashell: '#fff5ee',
144
+ sienna: '#a0522d',
145
+ silver: '#c0c0c0',
146
+ skyblue: '#87ceeb',
147
+ slateblue: '#6a5acd',
148
+ slategray: '#708090',
149
+ slategrey: '#708090',
150
+ snow: '#fffafa',
151
+ springgreen: '#00ff7f',
152
+ steelblue: '#4682b4',
153
+ tan: '#d2b48c',
154
+ teal: '#008080',
155
+ thistle: '#d8bfd8',
156
+ tomato: '#ff6347',
157
+ turquoise: '#40e0d0',
158
+ violet: '#ee82ee',
159
+ wheat: '#f5deb3',
160
+ white: '#ffffff',
161
+ whitesmoke: '#f5f5f5',
162
+ yellow: '#ffff00',
163
+ yellowgreen: '#9acd32'
164
+ };
165
+ export const colorKeywords = {
166
+ 'currentColor': 'The value of the \'color\' property. The computed value of the \'currentColor\' keyword is the computed value of the \'color\' property. If the \'currentColor\' keyword is set on the \'color\' property itself, it is treated as \'color:inherit\' at parse time.',
167
+ 'transparent': 'Fully transparent. This keyword can be considered a shorthand for rgba(0,0,0,0) which is its computed value.',
168
+ };
169
+ function getNumericValue(node, factor) {
170
+ const val = node.getText();
171
+ const m = val.match(/^([-+]?[0-9]*\.?[0-9]+)(%?)$/);
172
+ if (m) {
173
+ if (m[2]) {
174
+ factor = 100.0;
175
+ }
176
+ const result = parseFloat(m[1]) / factor;
177
+ if (result >= 0 && result <= 1) {
178
+ return result;
179
+ }
180
+ }
181
+ throw new Error();
182
+ }
183
+ function getAngle(node) {
184
+ const val = node.getText();
185
+ const m = val.match(/^([-+]?[0-9]*\.?[0-9]+)(deg|rad|grad|turn)?$/);
186
+ if (m) {
187
+ switch (m[2]) {
188
+ case 'deg':
189
+ return parseFloat(val) % 360;
190
+ case 'rad':
191
+ return (parseFloat(val) * 180 / Math.PI) % 360;
192
+ case 'grad':
193
+ return (parseFloat(val) * 0.9) % 360;
194
+ case 'turn':
195
+ return (parseFloat(val) * 360) % 360;
196
+ default:
197
+ if ('undefined' === typeof m[2]) {
198
+ return parseFloat(val) % 360;
199
+ }
200
+ }
201
+ }
202
+ throw new Error();
203
+ }
204
+ export function isColorConstructor(node) {
205
+ const name = node.getName();
206
+ if (!name) {
207
+ return false;
208
+ }
209
+ return /^(rgb|rgba|hsl|hsla|hwb)$/gi.test(name);
210
+ }
211
+ /**
212
+ * Returns true if the node is a color value - either
213
+ * defined a hex number, as rgb or rgba function, or
214
+ * as color name.
215
+ */
216
+ export function isColorValue(node) {
217
+ if (node.type === nodes.NodeType.HexColorValue) {
218
+ return true;
219
+ }
220
+ else if (node.type === nodes.NodeType.Function) {
221
+ return isColorConstructor(node);
222
+ }
223
+ else if (node.type === nodes.NodeType.Identifier) {
224
+ if (node.parent && node.parent.type !== nodes.NodeType.Term) {
225
+ return false;
226
+ }
227
+ const candidateColor = node.getText().toLowerCase();
228
+ if (candidateColor === 'none') {
229
+ return false;
230
+ }
231
+ if (colors[candidateColor]) {
232
+ return true;
233
+ }
234
+ }
235
+ return false;
236
+ }
237
+ const Digit0 = 48;
238
+ const Digit9 = 57;
239
+ const A = 65;
240
+ const F = 70;
241
+ const a = 97;
242
+ const f = 102;
243
+ export function hexDigit(charCode) {
244
+ if (charCode < Digit0) {
245
+ return 0;
246
+ }
247
+ if (charCode <= Digit9) {
248
+ return charCode - Digit0;
249
+ }
250
+ if (charCode < a) {
251
+ charCode += (a - A);
252
+ }
253
+ if (charCode >= a && charCode <= f) {
254
+ return charCode - a + 10;
255
+ }
256
+ return 0;
257
+ }
258
+ export function colorFromHex(text) {
259
+ if (text[0] !== '#') {
260
+ return null;
261
+ }
262
+ switch (text.length) {
263
+ case 4:
264
+ return {
265
+ red: (hexDigit(text.charCodeAt(1)) * 0x11) / 255.0,
266
+ green: (hexDigit(text.charCodeAt(2)) * 0x11) / 255.0,
267
+ blue: (hexDigit(text.charCodeAt(3)) * 0x11) / 255.0,
268
+ alpha: 1
269
+ };
270
+ case 5:
271
+ return {
272
+ red: (hexDigit(text.charCodeAt(1)) * 0x11) / 255.0,
273
+ green: (hexDigit(text.charCodeAt(2)) * 0x11) / 255.0,
274
+ blue: (hexDigit(text.charCodeAt(3)) * 0x11) / 255.0,
275
+ alpha: (hexDigit(text.charCodeAt(4)) * 0x11) / 255.0,
276
+ };
277
+ case 7:
278
+ return {
279
+ red: (hexDigit(text.charCodeAt(1)) * 0x10 + hexDigit(text.charCodeAt(2))) / 255.0,
280
+ green: (hexDigit(text.charCodeAt(3)) * 0x10 + hexDigit(text.charCodeAt(4))) / 255.0,
281
+ blue: (hexDigit(text.charCodeAt(5)) * 0x10 + hexDigit(text.charCodeAt(6))) / 255.0,
282
+ alpha: 1
283
+ };
284
+ case 9:
285
+ return {
286
+ red: (hexDigit(text.charCodeAt(1)) * 0x10 + hexDigit(text.charCodeAt(2))) / 255.0,
287
+ green: (hexDigit(text.charCodeAt(3)) * 0x10 + hexDigit(text.charCodeAt(4))) / 255.0,
288
+ blue: (hexDigit(text.charCodeAt(5)) * 0x10 + hexDigit(text.charCodeAt(6))) / 255.0,
289
+ alpha: (hexDigit(text.charCodeAt(7)) * 0x10 + hexDigit(text.charCodeAt(8))) / 255.0
290
+ };
291
+ }
292
+ return null;
293
+ }
294
+ export function colorFrom256RGB(red, green, blue, alpha = 1.0) {
295
+ return {
296
+ red: red / 255.0,
297
+ green: green / 255.0,
298
+ blue: blue / 255.0,
299
+ alpha
300
+ };
301
+ }
302
+ export function colorFromHSL(hue, sat, light, alpha = 1.0) {
303
+ hue = hue / 60.0;
304
+ if (sat === 0) {
305
+ return { red: light, green: light, blue: light, alpha };
306
+ }
307
+ else {
308
+ const hueToRgb = (t1, t2, hue) => {
309
+ while (hue < 0) {
310
+ hue += 6;
311
+ }
312
+ while (hue >= 6) {
313
+ hue -= 6;
314
+ }
315
+ if (hue < 1) {
316
+ return (t2 - t1) * hue + t1;
317
+ }
318
+ if (hue < 3) {
319
+ return t2;
320
+ }
321
+ if (hue < 4) {
322
+ return (t2 - t1) * (4 - hue) + t1;
323
+ }
324
+ return t1;
325
+ };
326
+ const t2 = light <= 0.5 ? (light * (sat + 1)) : (light + sat - (light * sat));
327
+ const t1 = light * 2 - t2;
328
+ return { red: hueToRgb(t1, t2, hue + 2), green: hueToRgb(t1, t2, hue), blue: hueToRgb(t1, t2, hue - 2), alpha };
329
+ }
330
+ }
331
+ export function hslFromColor(rgba) {
332
+ const r = rgba.red;
333
+ const g = rgba.green;
334
+ const b = rgba.blue;
335
+ const a = rgba.alpha;
336
+ const max = Math.max(r, g, b);
337
+ const min = Math.min(r, g, b);
338
+ let h = 0;
339
+ let s = 0;
340
+ const l = (min + max) / 2;
341
+ const chroma = max - min;
342
+ if (chroma > 0) {
343
+ s = Math.min((l <= 0.5 ? chroma / (2 * l) : chroma / (2 - (2 * l))), 1);
344
+ switch (max) {
345
+ case r:
346
+ h = (g - b) / chroma + (g < b ? 6 : 0);
347
+ break;
348
+ case g:
349
+ h = (b - r) / chroma + 2;
350
+ break;
351
+ case b:
352
+ h = (r - g) / chroma + 4;
353
+ break;
354
+ }
355
+ h *= 60;
356
+ h = Math.round(h);
357
+ }
358
+ return { h, s, l, a };
359
+ }
360
+ export function colorFromHWB(hue, white, black, alpha = 1.0) {
361
+ if (white + black >= 1) {
362
+ const gray = white / (white + black);
363
+ return { red: gray, green: gray, blue: gray, alpha };
364
+ }
365
+ const rgb = colorFromHSL(hue, 1, 0.5, alpha);
366
+ let red = rgb.red;
367
+ red *= (1 - white - black);
368
+ red += white;
369
+ let green = rgb.green;
370
+ green *= (1 - white - black);
371
+ green += white;
372
+ let blue = rgb.blue;
373
+ blue *= (1 - white - black);
374
+ blue += white;
375
+ return {
376
+ red: red,
377
+ green: green,
378
+ blue: blue,
379
+ alpha
380
+ };
381
+ }
382
+ export function hwbFromColor(rgba) {
383
+ const hsl = hslFromColor(rgba);
384
+ const white = Math.min(rgba.red, rgba.green, rgba.blue);
385
+ const black = 1 - Math.max(rgba.red, rgba.green, rgba.blue);
386
+ return {
387
+ h: hsl.h,
388
+ w: white,
389
+ b: black,
390
+ a: hsl.a
391
+ };
392
+ }
393
+ export function getColorValue(node) {
394
+ if (node.type === nodes.NodeType.HexColorValue) {
395
+ const text = node.getText();
396
+ return colorFromHex(text);
397
+ }
398
+ else if (node.type === nodes.NodeType.Function) {
399
+ const functionNode = node;
400
+ const name = functionNode.getName();
401
+ let colorValues = functionNode.getArguments().getChildren();
402
+ if (colorValues.length === 1) {
403
+ const functionArg = colorValues[0].getChildren();
404
+ if (functionArg.length === 1 && functionArg[0].type === nodes.NodeType.Expression) {
405
+ colorValues = functionArg[0].getChildren();
406
+ if (colorValues.length === 3) {
407
+ const lastValue = colorValues[2];
408
+ if (lastValue instanceof nodes.BinaryExpression) {
409
+ const left = lastValue.getLeft(), right = lastValue.getRight(), operator = lastValue.getOperator();
410
+ if (left && right && operator && operator.matches('/')) {
411
+ colorValues = [colorValues[0], colorValues[1], left, right];
412
+ }
413
+ }
414
+ }
415
+ }
416
+ }
417
+ if (!name || colorValues.length < 3 || colorValues.length > 4) {
418
+ return null;
419
+ }
420
+ try {
421
+ const alpha = colorValues.length === 4 ? getNumericValue(colorValues[3], 1) : 1;
422
+ if (name === 'rgb' || name === 'rgba') {
423
+ return {
424
+ red: getNumericValue(colorValues[0], 255.0),
425
+ green: getNumericValue(colorValues[1], 255.0),
426
+ blue: getNumericValue(colorValues[2], 255.0),
427
+ alpha
428
+ };
429
+ }
430
+ else if (name === 'hsl' || name === 'hsla') {
431
+ const h = getAngle(colorValues[0]);
432
+ const s = getNumericValue(colorValues[1], 100.0);
433
+ const l = getNumericValue(colorValues[2], 100.0);
434
+ return colorFromHSL(h, s, l, alpha);
435
+ }
436
+ else if (name === 'hwb') {
437
+ const h = getAngle(colorValues[0]);
438
+ const w = getNumericValue(colorValues[1], 100.0);
439
+ const b = getNumericValue(colorValues[2], 100.0);
440
+ return colorFromHWB(h, w, b, alpha);
441
+ }
442
+ }
443
+ catch (e) {
444
+ // parse error on numeric value
445
+ return null;
446
+ }
447
+ }
448
+ else if (node.type === nodes.NodeType.Identifier) {
449
+ if (node.parent && node.parent.type !== nodes.NodeType.Term) {
450
+ return null;
451
+ }
452
+ const term = node.parent;
453
+ if (term && term.parent && term.parent.type === nodes.NodeType.BinaryExpression) {
454
+ const expression = term.parent;
455
+ if (expression.parent && expression.parent.type === nodes.NodeType.ListEntry && expression.parent.key === expression) {
456
+ return null;
457
+ }
458
+ }
459
+ const candidateColor = node.getText().toLowerCase();
460
+ if (candidateColor === 'none') {
461
+ return null;
462
+ }
463
+ const colorHex = colors[candidateColor];
464
+ if (colorHex) {
465
+ return colorFromHex(colorHex);
466
+ }
467
+ }
468
+ return null;
469
+ }