Skip to content
Permalink
Browse files
fix(postcss-colormin): Strict color parsing (#1122)
* Strict color parsing

* Add more tests

* Fix cssnano-preset-default snapshots

* Fix cssnano-preset-advanced snapshots
  • Loading branch information
omgovich committed May 21, 2021
1 parent 8a31ca3 commit 32771da46ee94f07a6907ec47701189f90ad2ec0
Showing with 58 additions and 54 deletions.
  1. +1 −1 packages/cssnano-preset-advanced/src/__tests__/integrations/bootstrap-v3.4.0.css
  2. +1 −1 packages/cssnano-preset-advanced/src/__tests__/integrations/gumby-v2.6.4.css
  3. +1 −1 packages/cssnano-preset-advanced/src/__tests__/integrations/kube-v6.5.2.css
  4. +1 −1 packages/cssnano-preset-advanced/src/__tests__/integrations/picnic-v6.4.0.css
  5. +10 −10 packages/cssnano-preset-advanced/src/__tests__/integrations/semantic-ui-v2.4.1.css
  6. +1 −1 packages/cssnano-preset-advanced/src/__tests__/integrations/turret-v5.1.3.css
  7. +1 −1 packages/cssnano-preset-advanced/src/__tests__/integrations/uikit-v3.0.3.css
  8. +1 −1 packages/cssnano-preset-default/src/__tests__/integrations/basscss-v8.0.2.css
  9. +1 −1 packages/cssnano-preset-default/src/__tests__/integrations/bootstrap-v3.4.0.css
  10. +1 −1 packages/cssnano-preset-default/src/__tests__/integrations/gumby-v2.6.4.css
  11. +1 −1 packages/cssnano-preset-default/src/__tests__/integrations/kube-v6.5.2.css
  12. +1 −1 packages/cssnano-preset-default/src/__tests__/integrations/picnic-v6.4.0.css
  13. +10 −10 packages/cssnano-preset-default/src/__tests__/integrations/semantic-ui-v2.4.1.css
  14. +1 −1 packages/cssnano-preset-default/src/__tests__/integrations/turret-v5.1.3.css
  15. +1 −1 packages/cssnano-preset-default/src/__tests__/integrations/uikit-v3.0.3.css
  16. +1 −1 packages/postcss-colormin/package.json
  17. +2 −0 packages/postcss-colormin/src/__tests__/colours.js
  18. +15 −0 packages/postcss-colormin/src/__tests__/index.js
  19. +3 −16 packages/postcss-colormin/src/colours.js
  20. +4 −4 packages/postcss-colormin/yarn.lock

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.

@@ -31,7 +31,7 @@
"repository": "cssnano/cssnano",
"dependencies": {
"browserslist": "^4.16.0",
"colord": "^1.7.2",
"colord": "^2.0.0",
"postcss-value-parser": "^4.1.0"
},
"bugs": {
@@ -127,6 +127,8 @@ test(
isEqual('rgb(50%,23,54)', 'rgb(50%,23,54)')
);

test('should pass on non prefixed hexadecimal value', isEqual('999', '999'));

test('should convert darkgray to a hex', isEqual('darkgray', '#a9a9a9'));

test('should convert 8 character hex codes', isEqual('#000000FF', '#000'));
@@ -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))}')
);
@@ -1,28 +1,15 @@
import { colord, extend, getFormat } from 'colord';
import { colord, extend } from 'colord';
import namesPlugin from 'colord/plugins/names';
import toShorthand from './lib/toShorthand';

extend([namesPlugin]);

export default (colour, isLegacy = false) => {
if (getFormat(colour) === 'rgb') {
/* check that it is a valid CSS value
https://www.w3.org/TR/css-color-4/#rgb-functions */
let percentCount = 0;
for (const c of colour) {
if (c === '%') {
percentCount++;
}
}
// rgb values should either be all percentages or all numbers
if (percentCount !== 3 && percentCount !== 0) {
return colour;
}
}
const parsed = colord(colour);

const parsed = colord(colour.toLowerCase());
if (parsed.isValid()) {
const alpha = parsed.alpha();

if (alpha === 1) {
const toHex = toShorthand(parsed.toHex());
const toName = parsed.toName();
@@ -18,10 +18,10 @@ caniuse-lite@^1.0.30001165:
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001170.tgz#0088bfecc6a14694969e391cc29d7eb6362ca6a7"
integrity sha512-Dd4d/+0tsK0UNLrZs3CvNukqalnVTRrxb5mcQm8rHL49t7V5ZaTygwXkrq+FB+dVDf++4ri8eJnFEJAB8332PA==

colord@^1.7.2:
version "1.7.2"
resolved "https://registry.yarnpkg.com/colord/-/colord-1.7.2.tgz#474f0e46333cb5c7a191dbd571edeee19a5f97b6"
integrity sha512-/sQCxy6PEhZbrAn1+NVRRefy3k4jkWQGxk7mo2o0CoNA24jq4ujDc2jXzJ5uXphm/TwfdGOP0w8U+H+9ys4Peg==
colord@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/colord/-/colord-2.0.0.tgz#f8c19f2526b7dc5b22d6e57ef102f03a2a43a3d8"
integrity sha512-WMDFJfoY3wqPZNpKUFdse3HhD5BHCbE9JCdxRzoVH+ywRITGOeWAHNkGEmyxLlErEpN9OLMWgdM9dWQtDk5dog==

colorette@^1.2.1, colorette@^1.2.2:
version "1.2.2"

0 comments on commit 32771da

Please sign in to comment.