Skip to content

Commit

Permalink
feat: add hwb, lab, and lch to functions matching isColor (#111)
Browse files Browse the repository at this point in the history
* Add hwb, lab, and lch to functions matching isColor

* Use Regex for all function checks

* Update snapshots
  • Loading branch information
jonathantneal authored Apr 18, 2020
1 parent 11b91a0 commit 9189daa
Show file tree
Hide file tree
Showing 16 changed files with 1,837 additions and 1,377 deletions.
12 changes: 7 additions & 5 deletions lib/nodes/Func.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ const allFunctions = [
'url',
'var'
];
const colorFunctions = ['hsl', 'hsla', 'rgb', 'rgba'];
const vendorPrefixes = ['-webkit-', '-moz-', '-ms-', '-o-'];
const reFunctions = new RegExp(`^(${vendorPrefixes.join('|')})?(${allFunctions.join('|')})`, 'i');
const rePunctuation = new RegExp(`^(\\${Punctuation.chars.join('|\\')})`);
const reVar = /^--[^\s]+$/;

const colorFunctionsRegex = /^(hsla?|hwb|lab|lch|rgba?)$/i;
const varFunctionRegex = /^var$/i;
const varPrefixRegex = /^--[^\s]+$/;

class Func extends Container {
constructor(options = {}) {
Expand Down Expand Up @@ -189,10 +191,10 @@ class Func extends Container {
parser.back(rightTokens);

const { lastNode } = parser;
const lowerName = lastNode.name.toLowerCase();
const { nodes } = node;
lastNode.isColor = colorFunctions.includes(lowerName);
lastNode.isVar = lowerName === 'var' && nodes.length && reVar.test(nodes[0].value);
lastNode.isColor = colorFunctionsRegex.test(lastNode.name);
lastNode.isVar =
varFunctionRegex.test(lastNode.name) && nodes.length && varPrefixRegex.test(nodes[0].value);
}
}

Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/func.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ module.exports = {
'rgba( 29, 439 , 29 )',
'RGBA( 29, 439 , 29 )',
'RgBa( 29, 439 , 29 )',
'Lab( 40% 56.6 39 )',
'lCH(40% 68.8 34.5 / 50%)',
'hwb(90deg 0% 0% / 0.5)',
'calc(-0.5 * var(foo))',
'calc(1px + -2vw - 4px)',
'calc(((768px - 100vw) / 2) - 15px)',
Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/atword.test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Generated by [AVA](https://ava.li).
Generated by [AVA](https://avajs.dev).

## @word

Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/comment.test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Generated by [AVA](https://ava.li).
Generated by [AVA](https://avajs.dev).

## /**/

Expand Down
Loading

0 comments on commit 9189daa

Please sign in to comment.