Skip to content

Commit 9189daa

Browse files
feat: add hwb, lab, and lch to functions matching isColor (#111)
* Add hwb, lab, and lch to functions matching isColor * Use Regex for all function checks * Update snapshots
1 parent 11b91a0 commit 9189daa

16 files changed

+1837
-1377
lines changed

lib/nodes/Func.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,13 @@ const allFunctions = [
8484
'url',
8585
'var'
8686
];
87-
const colorFunctions = ['hsl', 'hsla', 'rgb', 'rgba'];
8887
const vendorPrefixes = ['-webkit-', '-moz-', '-ms-', '-o-'];
8988
const reFunctions = new RegExp(`^(${vendorPrefixes.join('|')})?(${allFunctions.join('|')})`, 'i');
9089
const rePunctuation = new RegExp(`^(\\${Punctuation.chars.join('|\\')})`);
91-
const reVar = /^--[^\s]+$/;
90+
91+
const colorFunctionsRegex = /^(hsla?|hwb|lab|lch|rgba?)$/i;
92+
const varFunctionRegex = /^var$/i;
93+
const varPrefixRegex = /^--[^\s]+$/;
9294

9395
class Func extends Container {
9496
constructor(options = {}) {
@@ -189,10 +191,10 @@ class Func extends Container {
189191
parser.back(rightTokens);
190192

191193
const { lastNode } = parser;
192-
const lowerName = lastNode.name.toLowerCase();
193194
const { nodes } = node;
194-
lastNode.isColor = colorFunctions.includes(lowerName);
195-
lastNode.isVar = lowerName === 'var' && nodes.length && reVar.test(nodes[0].value);
195+
lastNode.isColor = colorFunctionsRegex.test(lastNode.name);
196+
lastNode.isVar =
197+
varFunctionRegex.test(lastNode.name) && nodes.length && varPrefixRegex.test(nodes[0].value);
196198
}
197199
}
198200

test/fixtures/func.js

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ module.exports = {
2525
'rgba( 29, 439 , 29 )',
2626
'RGBA( 29, 439 , 29 )',
2727
'RgBa( 29, 439 , 29 )',
28+
'Lab( 40% 56.6 39 )',
29+
'lCH(40% 68.8 34.5 / 50%)',
30+
'hwb(90deg 0% 0% / 0.5)',
2831
'calc(-0.5 * var(foo))',
2932
'calc(1px + -2vw - 4px)',
3033
'calc(((768px - 100vw) / 2) - 15px)',

test/snapshots/atword.test.js.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The actual snapshot is saved in `atword.test.js.snap`.
44

5-
Generated by [AVA](https://ava.li).
5+
Generated by [AVA](https://avajs.dev).
66

77
## @word
88

test/snapshots/comment.test.js.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The actual snapshot is saved in `comment.test.js.snap`.
44

5-
Generated by [AVA](https://ava.li).
5+
Generated by [AVA](https://avajs.dev).
66

77
## /**/
88

0 commit comments

Comments
 (0)