Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(postcss-colormin): prevent interpreting all numbers as hex colors. #1118

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions packages/postcss-colormin/src/__tests__/index.js
Expand Up @@ -308,3 +308,18 @@ test(
'should not mangle colours in the content property',
passthroughCSS('h2:before{content:"black"}')
);

test(
'should not attempt to convert z-index',
passthroughCSS('h1{z-index:999}')
);

test(
'should not attempt to convert variables',
passthroughCSS(':root{--some-color: 200 255 0}')
);

test(
'should respect CSS variables',
passthroughCSS('div{background-color:rgba(51,153,255,var(--tw-bg-opacity))}')
);
12 changes: 10 additions & 2 deletions packages/postcss-colormin/src/colours.js
Expand Up @@ -5,9 +5,12 @@ import toShorthand from './lib/toShorthand';
extend([namesPlugin]);

export default (colour, isLegacy = false) => {
if (getFormat(colour) === 'rgb') {
/* check that it is a valid CSS value
if (colour.includes('var(')) {
return colour;
}
/* check that it is a valid CSS value
https://www.w3.org/TR/css-color-4/#rgb-functions */
if (getFormat(colour) === 'rgb') {
let percentCount = 0;
for (const c of colour) {
if (c === '%') {
Expand All @@ -20,6 +23,11 @@ export default (colour, isLegacy = false) => {
}
}

if (getFormat(colour) === 'hex') {
if (colour.charAt(0) !== '#') {
return colour;
}
}
const parsed = colord(colour.toLowerCase());
if (parsed.isValid()) {
const alpha = parsed.alpha();
Expand Down