postcss-colormin 9.0.4 → 9.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-colormin",
3
- "version": "9.0.4",
3
+ "version": "9.0.6",
4
4
  "description": "Minify colors in your CSS files with PostCSS.",
5
5
  "exports": {
6
6
  ".": {
@@ -45,9 +45,9 @@
45
45
  "dependencies": {
46
46
  "@colordx/core": "^7.2.0",
47
47
  "@csstools/css-tokenizer": "^4.0.2",
48
- "browserslist": "^4.29.1",
48
+ "browserslist": "^4.29.3",
49
49
  "caniuse-api": "^4.0.0",
50
- "cssnano-utils": "^8.0.0"
50
+ "cssnano-utils": "^8.0.2"
51
51
  },
52
52
  "bugs": {
53
53
  "url": "https://github.com/cssnano/cssnano/issues"
@@ -65,6 +65,6 @@
65
65
  "type": "module",
66
66
  "scripts": {
67
67
  "test": "node --test",
68
- "acquire": "node ./script/acquire.mjs"
68
+ "acquire": "node ./script/acquire.js"
69
69
  }
70
70
  }
package/src/index.js CHANGED
@@ -1,9 +1,8 @@
1
1
  import getBrowsersList from '#getBrowsersList';
2
2
  import caniuseApi from 'caniuse-api';
3
- import { colordx as colord } from '@colordx/core';
3
+ import { tryMinifyColor } from './minifyColor.js';
4
4
  import cssnanoUtils from 'cssnano-utils';
5
5
  import colorPropertiesData from './data/colorProperties.json' with { type: 'json' };
6
- import minifyColor from './minifyColor.js';
7
6
 
8
7
  /** @import {CSSToken} from '@csstools/css-tokenizer' */
9
8
  const { isSupported } = caniuseApi;
@@ -39,6 +38,28 @@ const tokensRequiringSeparator = new Set([
39
38
  TokenType.Percentage,
40
39
  ]);
41
40
 
41
+ /**
42
+ * Fast-path check for valid CSS hex colors (CSS Color 4 § 5.2):
43
+ * Must be 3, 4, 6, or 8 hexadecimal digits.
44
+ * @param {string} value
45
+ * @return {boolean}
46
+ */
47
+ function isHexColor(value) {
48
+ const len = value.length;
49
+ if (len !== 3 && len !== 4 && len !== 6 && len !== 8) return false;
50
+ for (let i = 0; i < len; i++) {
51
+ const code = value.charCodeAt(i);
52
+ if (
53
+ !(code >= 48 && code <= 57) &&
54
+ !(code >= 65 && code <= 70) &&
55
+ !(code >= 97 && code <= 102)
56
+ ) {
57
+ return false;
58
+ }
59
+ }
60
+ return true;
61
+ }
62
+
42
63
  /** @param {string} value @param {Options} options @return {string} */
43
64
  function transform(value, options) {
44
65
  const structure = balancedTokens(value);
@@ -76,43 +97,29 @@ function transform(value, options) {
76
97
  const raw = value.slice(tokenStart(t), tokenEnd(closeToken));
77
98
  const inputForMinify =
78
99
  name + '(' + value.slice(tokenEnd(t), tokenEnd(closeToken));
79
- if (colord(inputForMinify).isValid()) {
80
- const out = minifyColor(inputForMinify, options);
81
- if (out !== raw) {
82
- replacements.push({
83
- start: tokenStart(t),
84
- end: tokenEnd(closeToken),
85
- text: out + separator(tokenEnd(closeToken), closeIndex),
86
- });
87
- }
88
- }
89
- i = closeIndex;
90
- continue;
91
- }
92
- } else if (t[0] === TokenType.Ident) {
93
- const dec = decoded(t);
94
- if (!dec.startsWith('#') && colord(dec).isValid()) {
95
- const out = minifyColor(dec, options);
96
- if (out !== t[1] && out.length <= t[1].length) {
100
+ const out = tryMinifyColor(inputForMinify, options);
101
+ if (out !== undefined && out !== raw) {
97
102
  replacements.push({
98
103
  start: tokenStart(t),
99
- end: tokenEnd(t),
100
- text: out + separator(tokenEnd(t), i),
104
+ end: tokenEnd(closeToken),
105
+ text: out + separator(tokenEnd(closeToken), closeIndex),
101
106
  });
102
107
  }
108
+ i = closeIndex;
109
+ continue;
103
110
  }
104
- } else if (t[0] === TokenType.Hash) {
111
+ } else if (t[0] === TokenType.Ident || t[0] === TokenType.Hash) {
105
112
  const dec = decoded(t);
106
- const hexCandidate = '#' + dec;
107
- if (colord(hexCandidate).isValid()) {
108
- const out = minifyColor(hexCandidate, options);
109
- if (out !== t[1] && out.length <= t[1].length) {
110
- replacements.push({
111
- start: tokenStart(t),
112
- end: tokenEnd(t),
113
- text: out + separator(tokenEnd(t), i),
114
- });
115
- }
113
+ const isHash = t[0] === TokenType.Hash;
114
+ if (isHash ? !isHexColor(dec) : dec.startsWith('#')) continue;
115
+ const candidate = isHash ? '#' + dec : dec;
116
+ const out = tryMinifyColor(candidate, options);
117
+ if (out !== undefined && out !== t[1] && out.length <= t[1].length) {
118
+ replacements.push({
119
+ start: tokenStart(t),
120
+ end: tokenEnd(t),
121
+ text: out + separator(tokenEnd(t), i),
122
+ });
116
123
  }
117
124
  }
118
125
  }
@@ -6,13 +6,13 @@ import minifierPlugin from '@colordx/core/plugins/minify';
6
6
  extend(/** @type {any[]} */ ([hwbPlugin, namesPlugin, minifierPlugin]));
7
7
 
8
8
  /**
9
- * Performs color value minification
9
+ * Performs color value minification if input is a valid CSS color
10
10
  *
11
11
  * @param {string} input - CSS value
12
- * @param {import('./index.js').MinifyColorOptions} options - object with colordx.minify() options
13
- * @return {string}
12
+ * @param {import('./index.js').MinifyColorOptions} [options] - object with colordx.minify() options
13
+ * @return {string | undefined}
14
14
  */
15
- function minifyColor(input, options = {}) {
15
+ export function tryMinifyColor(input, options = {}) {
16
16
  const instance = colord(input);
17
17
 
18
18
  if (instance.isValid()) {
@@ -21,10 +21,18 @@ function minifyColor(input, options = {}) {
21
21
 
22
22
  // Fall back to the original input if it's smaller or has equal length
23
23
  return minified.length < input.length ? minified : input.toLowerCase();
24
- } else {
25
- // Possibly malformed, so pass through
26
- return input;
27
24
  }
28
25
  }
29
26
 
27
+ /**
28
+ * Performs color value minification
29
+ *
30
+ * @param {string} input - CSS value
31
+ * @param {import('./index.js').MinifyColorOptions} [options] - object with colordx.minify() options
32
+ * @return {string}
33
+ */
34
+ function minifyColor(input, options = {}) {
35
+ return tryMinifyColor(input, options) ?? input;
36
+ }
37
+
30
38
  export default minifyColor;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"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G,AAAxB,CACA,EADW,OAAO,CAClB;IAAqB,IAAI,AAAzB,CACA,EADW,OAAO,CAClB;IAAqB,WAAW,AAAhC,CACA,EADW,OAAO,CAClB;;;;IAAqB,yBAAyB,AAA9C,CACF,EADa,OAAO,CACpB;CAAA,CAAA;AAGE,YAAwD,mBAAmB,GAAjE;IAAE,oBAAoB,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;CAAE,CAAqB;AAC3E,YAAgE,mBAAmB,GAAzE,IAAI,CAAC,YAAY,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,GAAG,KAAK,CAAC,CAAqB;AACnF,YAA0E,OAAO,GAAvE,kBAAkB,GAAG,mBAAmB,GAAG,mBAAmB,CAAS;AAdpF;;;;;;;;;GASG;AAEH;;;;GAIG;AAEH;;;GAGG;AACH,iBAAS,aAAa,CAAC,MAAM,GAHlB,OAGuB,GAFtB,OAAO,SAAS,EAAE,MAAM,CAsEnC;kBApEQ,aAAa;iBAqEX,IAAI;;AAEf,QAAA,MAAM,aAAa,sBAAgB,CAAC;AAEpC,OAAO,EAAE,aAAa,IAAI,OAAO,EAAE,aAAa,IAAI,gBAAgB,EAAE,CAAC"}
@@ -1,8 +1,16 @@
1
+ /**
2
+ * Performs color value minification if input is a valid CSS color
3
+ *
4
+ * @param {string} input - CSS value
5
+ * @param {import('./index.js').MinifyColorOptions} [options] - object with colordx.minify() options
6
+ * @return {string | undefined}
7
+ */
8
+ export declare function tryMinifyColor(input: string, options?: import('./index.js').MinifyColorOptions): string | undefined;
1
9
  /**
2
10
  * Performs color value minification
3
11
  *
4
12
  * @param {string} input - CSS value
5
- * @param {import('./index.js').MinifyColorOptions} options - object with colordx.minify() options
13
+ * @param {import('./index.js').MinifyColorOptions} [options] - object with colordx.minify() options
6
14
  * @return {string}
7
15
  */
8
16
  declare function minifyColor(input: string, options?: import('./index.js').MinifyColorOptions): string;
@@ -1 +1 @@
1
- {"version":3,"file":"minifyColor.d.ts","sourceRoot":"","sources":["../src/minifyColor.js"],"names":[],"mappings":"AAOA;;;;;;GAMG;AACH,iBAAS,WAAW,CAAC,KAAK,EAJf,MAIe,EAAE,OAAO,GAHxB,OAAO,YAAY,EAAE,kBAGQ,GAF5B,MAAM,CAejB;eAEc,WAAW"}
1
+ {"version":3,"file":"minifyColor.d.ts","sourceRoot":"","sources":["../src/minifyColor.js"],"names":[],"mappings":"AAOA;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAJzB,MAIyB,EAAE,OAAO,AAH1C,CACA,EADQ,OAAO,YAAY,EAAE,kBAGkB,GAFtC,MAAM,GAAG,SAAS,CAY7B;AAED;;;;;;GAMG;AACH,iBAAS,WAAW,CAAC,KAAK,EAJf,MAIe,EAAE,OAAO,AAHhC,CACA,EADQ,OAAO,YAAY,EAAE,kBAGQ,GAF5B,MAAM,CAIjB;eAEc,WAAW"}