Skip to content

Commit b7f2fa5

Browse files
authored
use case-insensitive Set-like construct to avoid lot of toLowerCase() (#316)
1 parent 5644203 commit b7f2fa5

File tree

6 files changed

+73
-29
lines changed

6 files changed

+73
-29
lines changed

src/index.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -423,18 +423,26 @@ function analyze(css) {
423423
if (nodeName.length > 20 || nodeName.length < 3) {
424424
return this.skip
425425
}
426-
let stringified = stringifyNode(valueNode)
427-
let lowerCased = nodeName.toLowerCase()
428426

429-
if (namedColors.has(lowerCased)) {
427+
if (namedColors.has(nodeName)) {
428+
let stringified = stringifyNode(valueNode)
430429
colors.push(stringified, property)
431430
colorFormats.push('named')
432-
} else if (colorKeywords.has(lowerCased)) {
431+
return
432+
}
433+
434+
if (colorKeywords.has(nodeName)) {
435+
let stringified = stringifyNode(valueNode)
433436
colors.push(stringified, property)
434-
colorFormats.push(lowerCased)
435-
} else if (systemColors.has(lowerCased)) {
437+
colorFormats.push(nodeName.toLowerCase())
438+
return
439+
}
440+
441+
if (systemColors.has(nodeName)) {
442+
let stringified = stringifyNode(valueNode)
436443
colors.push(stringified, property)
437444
colorFormats.push('system')
445+
return
438446
}
439447
return this.skip
440448
}
@@ -443,11 +451,11 @@ function analyze(css) {
443451
if (strEquals('var', nodeName)) {
444452
return this.skip
445453
}
446-
let fnName = nodeName.toLowerCase()
447-
let stringified = stringifyNode(valueNode)
448-
if (colorFunctions.has(fnName)) {
454+
455+
if (colorFunctions.has(nodeName)) {
456+
let stringified = stringifyNode(valueNode)
449457
colors.push(stringified, property)
450-
colorFormats.push(fnName)
458+
colorFormats.push(nodeName.toLowerCase())
451459
}
452460
// No this.skip here intentionally,
453461
// otherwise we'll miss colors in linear-gradient() etc.

src/keyword-set.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { strEquals } from "./string-utils.js"
2+
3+
/**
4+
* @description A Set-like construct to search CSS keywords in a case-insensitive way
5+
*/
6+
export class KeywordSet {
7+
8+
/** @param {string[]} items */
9+
constructor(items) {
10+
/** @type {string[]} */
11+
this.set = items
12+
}
13+
14+
/** @param {string} item */
15+
has(item) {
16+
let len = this.set.length
17+
18+
for (let index = 0; index < len; index++) {
19+
if (strEquals(this.set[index], item)) {
20+
return true
21+
}
22+
}
23+
return false
24+
}
25+
}

src/values/animations.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const timingKeywords = new Set([
1+
import { KeywordSet } from "../keyword-set.js"
2+
3+
const timingKeywords = new KeywordSet([
24
'linear',
35
'ease',
46
'ease-in',

src/values/colors.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
1-
export const namedColors = new Set([
1+
import { KeywordSet } from "../keyword-set.js"
2+
3+
export const namedColors = new KeywordSet([
24
// CSS Named Colors
35
// Spec: https://drafts.csswg.org/css-color/#named-colors
6+
7+
// Heuristic: popular names first for quick finding in set.has()
8+
'white',
9+
'black',
10+
'red',
11+
'blue',
12+
'gray',
13+
'grey',
14+
'green',
15+
'rebeccapurple',
16+
'yellow',
17+
'orange',
18+
419
'aliceblue',
520
'antiquewhite',
621
'aqua',
722
'aquamarine',
823
'azure',
924
'beige',
1025
'bisque',
11-
'black',
1226
'blanchedalmond',
13-
'blue',
1427
'blueviolet',
1528
'brown',
1629
'burlywood',
@@ -54,10 +67,7 @@ export const namedColors = new Set([
5467
'ghostwhite',
5568
'gold',
5669
'goldenrod',
57-
'gray',
58-
'green',
5970
'greenyellow',
60-
'grey',
6171
'honeydew',
6272
'hotpink',
6373
'indianred',
@@ -106,7 +116,6 @@ export const namedColors = new Set([
106116
'oldlace',
107117
'olive',
108118
'olivedrab',
109-
'orange',
110119
'orangered',
111120
'orchid',
112121
'palegoldenrod',
@@ -120,8 +129,6 @@ export const namedColors = new Set([
120129
'plum',
121130
'powderblue',
122131
'purple',
123-
'rebeccapurple',
124-
'red',
125132
'rosybrown',
126133
'royalblue',
127134
'saddlebrown',
@@ -145,13 +152,11 @@ export const namedColors = new Set([
145152
'turquoise',
146153
'violet',
147154
'wheat',
148-
'white',
149155
'whitesmoke',
150-
'yellow',
151156
'yellowgreen',
152157
])
153158

154-
export const systemColors = new Set([
159+
export const systemColors = new KeywordSet([
155160
// CSS System Colors
156161
// Spec: https://drafts.csswg.org/css-color/#css-system-colors
157162
'canvas',
@@ -176,7 +181,7 @@ export const systemColors = new Set([
176181
// Spec: https://drafts.csswg.org/css-color/#deprecated-system-colors
177182
])
178183

179-
export const colorFunctions = new Set([
184+
export const colorFunctions = new KeywordSet([
180185
'rgb',
181186
'rgba',
182187
'hsl',
@@ -189,7 +194,7 @@ export const colorFunctions = new Set([
189194
'color',
190195
])
191196

192-
export const colorKeywords = new Set([
193-
'currentcolor',
197+
export const colorKeywords = new KeywordSet([
194198
'transparent',
199+
'currentcolor',
195200
])

src/values/destructure-font-shorthand.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const FONT_KEYWORDS = new Set([
1+
import { KeywordSet } from "../keyword-set.js"
2+
3+
const FONT_KEYWORDS = new KeywordSet([
24
// Global CSS keywords
35
'inherit',
46
'initial',
@@ -14,7 +16,7 @@ const FONT_KEYWORDS = new Set([
1416
'status-bar',
1517
])
1618

17-
const SIZE_KEYWORDS = new Set([
19+
const SIZE_KEYWORDS = new KeywordSet([
1820
/* <absolute-size> values */
1921
'xx-small',
2022
'x-small',

src/values/values.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const keywords = new Set([
1+
import { KeywordSet } from "../keyword-set.js"
2+
3+
const keywords = new KeywordSet([
24
'auto',
35
'inherit',
46
'initial',

0 commit comments

Comments
 (0)