Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix false positives for hex colours in color-function-notation (#5650)
- Loading branch information
Showing
5 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use strict'; | ||
|
||
const isStandardSyntaxColorFunction = require('../isStandardSyntaxColorFunction'); | ||
const postcss = require('postcss'); | ||
const valueParser = require('postcss-value-parser'); | ||
|
||
describe('isStandardSyntaxColorFunction', () => { | ||
it('legacy syntax', () => { | ||
expect(isStandardSyntaxColorFunction(func('a { color: rgb(0, 0, 0) }'))).toBe(true); | ||
}); | ||
|
||
it('legacy syntax alpha', () => { | ||
expect(isStandardSyntaxColorFunction(func('a { color: rgba(0, 0, 0, 0) }'))).toBe(true); | ||
}); | ||
|
||
it('modern syntax', () => { | ||
expect(isStandardSyntaxColorFunction(func('a { color: rgba(0 0 0) }'))).toBe(true); | ||
}); | ||
|
||
it('modern syntax alpha', () => { | ||
expect(isStandardSyntaxColorFunction(func('a { color: rgba(0 0 0 / 50%) }'))).toBe(true); | ||
}); | ||
|
||
it('space', () => { | ||
expect(isStandardSyntaxColorFunction(func('a { color: rgba( 0 0 0 / 50% ) }'))).toBe(true); | ||
}); | ||
|
||
it('comment', () => { | ||
expect(isStandardSyntaxColorFunction(func('a { color: rgba(/* #aaa */ 0 0 0 / 50%) }'))).toBe( | ||
true, | ||
); | ||
}); | ||
|
||
it('custom property', () => { | ||
expect(isStandardSyntaxColorFunction(func('a { color: rgba(var(--red) 0 0 / 50%) }'))).toBe( | ||
true, | ||
); | ||
}); | ||
|
||
it('function', () => { | ||
expect( | ||
isStandardSyntaxColorFunction(func('a { color: rgba(calc(var(--red) + 50) 0 0 / 50%) }')), | ||
).toBe(true); | ||
}); | ||
|
||
it('scss color function conversion', () => { | ||
expect(isStandardSyntaxColorFunction(func('a { color: rbga(#aaa, 0.5) }'))).toBe(false); | ||
}); | ||
|
||
it('scss color function conversion with comment', () => { | ||
expect(isStandardSyntaxColorFunction(func('a { color: rbga(/* comment */ #aaa, 0.5) }'))).toBe( | ||
false, | ||
); | ||
}); | ||
}); | ||
|
||
function func(css) { | ||
const functions = []; | ||
|
||
postcss.parse(css).walkDecls((decl) => { | ||
valueParser(decl.value).walk((valueNode) => { | ||
if (valueNode.type === 'function') { | ||
functions.push(valueNode); | ||
} | ||
}); | ||
}); | ||
|
||
return functions[0]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
const isStandardSyntaxFunction = require('./isStandardSyntaxFunction'); | ||
|
||
/** | ||
* Check whether a function is standard syntax color function | ||
* | ||
* @param {import('postcss-value-parser').FunctionNode} node | ||
* @returns {boolean} | ||
*/ | ||
module.exports = function isStandardSyntaxColorFunction(node) { | ||
if (!isStandardSyntaxFunction(node)) return false; | ||
|
||
// scss rgba() function can accept a hex as the first param | ||
for (const fnNode of node.nodes) { | ||
if (fnNode.type === 'word' && fnNode.value.startsWith('#')) return false; | ||
} | ||
|
||
return true; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters