@tbela99/css-parser 1.6.5 → 1.6.7

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.
@@ -1,8 +1,7 @@
1
- import { hsl2hsv } from './hsv.js';
2
1
  import { getColorComponents } from './utils/components.js';
3
- import { color2srgbvalues, getAngle, getNumber } from './color.js';
2
+ import { color2srgbvalues, getNumber, getAngle } from './color.js';
4
3
  import { EnumToken, ColorType } from '../../ast/types.js';
5
- import { cmyk2srgbvalues, lch2srgbvalues, lab2srgbvalues, oklch2srgbvalues, oklab2srgbvalues } from './srgb.js';
4
+ import { cmyk2srgbvalues, lch2srgbvalues, lab2srgbvalues, oklch2srgbvalues, oklab2srgbvalues, hslvalues } from './srgb.js';
6
5
 
7
6
  function rgb2hwbToken(token) {
8
7
  const values = rgb2hwbvalues(token);
@@ -16,6 +15,7 @@ function hsl2hwbToken(token) {
16
15
  if (values == null) {
17
16
  return null;
18
17
  }
18
+ // console.error(new Error("hsl2hwbToken"));
19
19
  return hwbToken(values);
20
20
  }
21
21
  function cmyk2hwbToken(token) {
@@ -61,11 +61,10 @@ function color2hwbToken(token) {
61
61
  return hwbToken(values);
62
62
  }
63
63
  function hwbToken(values) {
64
- values[0] = values[0] * 360;
65
64
  const chi = [
66
65
  { typ: EnumToken.NumberTokenType, val: values[0] },
67
- { typ: EnumToken.PercentageTokenType, val: values[1] * 100 },
68
- { typ: EnumToken.PercentageTokenType, val: values[2] * 100 },
66
+ { typ: EnumToken.PercentageTokenType, val: values[1] },
67
+ { typ: EnumToken.PercentageTokenType, val: values[2] },
69
68
  ];
70
69
  if (values.length == 4) {
71
70
  chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
@@ -95,17 +94,12 @@ function cmyk2hwbvalues(token) {
95
94
  return values == null ? null : srgb2hwb(values[0], values[1], values[2], values[3]);
96
95
  }
97
96
  function hsl2hwbvalues(token) {
98
- const values = getColorComponents(token).map((t, index) => {
99
- if (index == 3 && t.typ == EnumToken.IdenTokenType && t.val == "none") {
100
- return 1;
101
- }
102
- if (index == 0) {
103
- return getAngle(t);
104
- }
105
- return getNumber(t);
106
- });
97
+ const values = hslvalues(token);
98
+ if (values == null) {
99
+ return null;
100
+ }
107
101
  // @ts-ignore
108
- return hslvalues2hwbvalues(values[0], values[1], values[2], values[3]);
102
+ return hslvalues2hwbvalues(values.h, values.s, values.l, values.a);
109
103
  }
110
104
  function lab2hwbvalues(token) {
111
105
  const values = lab2srgbvalues(token);
@@ -134,26 +128,6 @@ function oklch2hwbvalues(token) {
134
128
  // @ts-ignore
135
129
  return values == null ? null : srgb2hwb(values[0], values[1], values[2], values[3]);
136
130
  }
137
- function rgb2hue(r, g, b, fallback = 0) {
138
- let value = rgb2value(r, g, b);
139
- let whiteness = rgb2whiteness(r, g, b);
140
- let delta = value - whiteness;
141
- if (delta > 0) {
142
- // calculate segment
143
- let segment = value === r ? (g - b) / delta : value === g ? (b - r) / delta : (r - g) / delta;
144
- // calculate shift
145
- let shift = value === r ? (segment < 0 ? 360 / 60 : 0 / 60) : value === g ? 120 / 60 : 240 / 60;
146
- // calculate hue
147
- return (segment + shift) * 60;
148
- }
149
- return fallback;
150
- }
151
- function rgb2value(r, g, b) {
152
- return Math.max(r, g, b);
153
- }
154
- function rgb2whiteness(r, g, b) {
155
- return Math.min(r, g, b);
156
- }
157
131
  function color2hwbvalues(token) {
158
132
  const values = color2srgbvalues(token);
159
133
  if (values == null) {
@@ -162,31 +136,87 @@ function color2hwbvalues(token) {
162
136
  // @ts-ignore
163
137
  return srgb2hwb(values[0], values[1], values[2], values[3]);
164
138
  }
165
- function srgb2hwb(r, g, b, a = null, fallback = 0) {
166
- r *= 100;
167
- g *= 100;
139
+ function hwbvalues(token) {
140
+ const components = getColorComponents(token);
141
+ if (components == null) {
142
+ return null;
143
+ }
144
+ let t;
145
+ // @ts-ignore
146
+ let h = getAngle(components[0]) * 360;
147
+ // @ts-ignore
148
+ t = components[1];
149
+ // @ts-ignore
150
+ let w = getNumber(t);
151
+ // @ts-ignore
152
+ t = components[2];
153
+ // @ts-ignore
154
+ let b = getNumber(t);
155
+ // if (components[1].typ == EnumToken.NumberTokenType) {
156
+ w *= 100;
157
+ // }
158
+ // if (components[2].typ == EnumToken.NumberTokenType) {
168
159
  b *= 100;
169
- let hue = rgb2hue(r, g, b, fallback);
170
- let whiteness = rgb2whiteness(r, g, b);
171
- let value = Math.round(rgb2value(r, g, b));
172
- let blackness = 100 - value;
173
- const result = [hue / 360, whiteness / 100, blackness / 100];
174
- if (a != null) {
175
- result.push(a);
160
+ // }
161
+ let a = null;
162
+ if (components.length == 4) {
163
+ // @ts-ignore
164
+ t = components[3];
165
+ // @ts-ignore
166
+ a = getNumber(t);
176
167
  }
177
- return result;
168
+ return a == null ? { h, w, b } : { h, w, b, a };
178
169
  }
179
- function hsv2hwb(h, s, v, a = null) {
180
- const result = [h, (1 - s) * v, 1 - v];
170
+ function srgb2hwb(r, g, b, a = null, fallback = 0) {
171
+ // 1. Clamp inputs to the standard web-safe sRGB gamut [0.0, 1.0]
172
+ const rClamp = Math.min(1, Math.max(0, r));
173
+ const gClamp = Math.min(1, Math.max(0, g));
174
+ const bClamp = Math.min(1, Math.max(0, b));
175
+ const cMax = Math.max(rClamp, gClamp, bClamp);
176
+ const cMin = Math.min(rClamp, gClamp, bClamp);
177
+ const delta = cMax - cMin;
178
+ // 2. Whiteness and Blackness calculations
179
+ const w = cMin;
180
+ const bl = 1 - cMax;
181
+ // 3. Calculate Hue
182
+ let h = 0;
183
+ if (delta !== 0) {
184
+ if (cMax === rClamp) {
185
+ h = 60 * (((gClamp - bClamp) / delta) % 6);
186
+ }
187
+ else if (cMax === gClamp) {
188
+ h = 60 * ((bClamp - rClamp) / delta + 2);
189
+ }
190
+ else if (cMax === bClamp) {
191
+ h = 60 * ((rClamp - gClamp) / delta + 4);
192
+ }
193
+ if (h < 0) {
194
+ h += 360;
195
+ }
196
+ }
197
+ const result = [h, w * 100, bl * 100];
181
198
  if (a != null) {
182
199
  result.push(a);
183
200
  }
184
201
  return result;
185
202
  }
186
203
  function hslvalues2hwbvalues(h, s, l, a = null) {
187
- let values = hsl2hsv(h, s, l);
188
- // @ts-ignore
189
- return hsv2hwb(values[0], values[1], values[2], a);
204
+ const sNorm = s / 100;
205
+ const lNorm = l / 100;
206
+ // Keep Hue cleanly bound to standard circle loops
207
+ const hue = ((h % 360) + 360) % 360;
208
+ // 1. Compute the intermediate HSV Value component (brightness peak)
209
+ const v = lNorm + sNorm * Math.min(lNorm, 1 - lNorm);
210
+ // 2. Compute Chroma
211
+ const chroma = 2 * (v - lNorm);
212
+ // 3. Extract Whiteness and Blackness
213
+ const w = v - chroma;
214
+ const b = 1 - v;
215
+ const hwb = [hue, w * 100, b * 100];
216
+ if (a != null && a != 1) {
217
+ hwb.push(a);
218
+ }
219
+ return hwb;
190
220
  }
191
221
 
192
- export { cmyk2hwbToken, cmyk2hwbvalues, color2hwbToken, color2hwbvalues, hsl2hwbToken, hsl2hwbvalues, hslvalues2hwbvalues, hsv2hwb, hwbToken, lab2hwbToken, lab2hwbvalues, lch2hwbToken, lch2hwbvalues, oklab2hwbToken, oklab2hwbvalues, oklch2hwbToken, oklch2hwbvalues, rgb2hwbToken, rgb2hwbvalues, srgb2hwb };
222
+ export { cmyk2hwbToken, cmyk2hwbvalues, color2hwbToken, color2hwbvalues, hsl2hwbToken, hsl2hwbvalues, hslvalues2hwbvalues, hwbToken, hwbvalues, lab2hwbToken, lab2hwbvalues, lch2hwbToken, lch2hwbvalues, oklab2hwbToken, oklab2hwbvalues, oklch2hwbToken, oklch2hwbvalues, rgb2hwbToken, rgb2hwbvalues, srgb2hwb };
@@ -75,7 +75,7 @@ function oklabToken(values) {
75
75
  { typ: EnumToken.NumberTokenType, val: values[1] },
76
76
  { typ: EnumToken.NumberTokenType, val: values[2] },
77
77
  ];
78
- if (values.length == 4) {
78
+ if (values.length == 4 && values[3] != 1) {
79
79
  chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
80
80
  typ: EnumToken.PercentageTokenType,
81
81
  val: values[3] * 100,
@@ -151,7 +151,36 @@ function parseRelativeColorComponents(relativeKeys, original, rExp, gExp, bExp,
151
151
  : aExp),
152
152
  };
153
153
  const result = computeComponentValue(keys, values);
154
+ if (result != null) {
155
+ if ("hsl" == relativeKeys) {
156
+ Object.assign(result.s, {
157
+ typ: EnumToken.PercentageTokenType,
158
+ // @ts-ignore
159
+ val: result.s.val * 100,
160
+ });
161
+ Object.assign(result.l, {
162
+ typ: EnumToken.PercentageTokenType,
163
+ // @ts-ignore
164
+ val: result.l.val * 100,
165
+ });
166
+ }
167
+ else if ("hwb" == relativeKeys) {
168
+ // @ts
169
+ Object.assign(result.w, {
170
+ typ: EnumToken.PercentageTokenType,
171
+ // @ts-ignore
172
+ val: result.w.val * 100,
173
+ });
174
+ Object.assign(result.b, {
175
+ typ: EnumToken.PercentageTokenType,
176
+ // @ts-ignore
177
+ val: result.b.val * 100,
178
+ });
179
+ }
180
+ }
181
+ // @ts-ignore
154
182
  if (result?.alpha?.typ == EnumToken.NumberTokenType && result.alpha.val === 1) {
183
+ // @ts-ignore
155
184
  const { alpha, ...components } = result;
156
185
  return components;
157
186
  }
@@ -9,6 +9,7 @@ import { getOKLCHComponents } from './oklch.js';
9
9
  import { XYZ_to_lin_sRGB } from './xyz.js';
10
10
  import { COLORS_NAMES } from '../constants.js';
11
11
  import { parseColor } from '../syntax.js';
12
+ import { hwbvalues } from './hwb.js';
12
13
 
13
14
  // from https://www.w3.org/TR/css-color-4/#color-conversion-code
14
15
  // srgb-linear -> srgb
@@ -83,25 +84,59 @@ function xyz2srgb(x, y, z, alpha = null) {
83
84
  return lsrgb2srgbvalues(values[0], values[1], values[2], alpha);
84
85
  }
85
86
  function hwb2srgbvalues(token) {
86
- const { h: hue, s: white, l: black, a: alpha } = hslvalues(token) ?? {};
87
- if (hue == null || white == null || black == null) {
87
+ const { h, w, b, a } = hwbvalues(token) ?? {};
88
+ if (h == null || w == null || b == null) {
88
89
  return [];
89
90
  }
90
- const rgb = hslvalues2srgbvalues(hue, 1, 0.5);
91
- for (let i = 0; i < 3; i++) {
92
- rgb[i] *= 1 - white - black;
93
- rgb[i] = rgb[i] + white;
94
- }
95
- if (alpha != null && alpha != 1) {
96
- rgb.push(alpha);
91
+ const rgb = hwb2srgb(h, w, b);
92
+ if (a != null && a != 1) {
93
+ rgb.push(a);
97
94
  }
98
95
  return rgb;
99
96
  }
97
+ /**
98
+ * convert hwb to sRGB
99
+ */
100
+ function hwb2srgb(h, w, b, alpha) {
101
+ const wNorm = w / 100;
102
+ const bNorm = b / 100;
103
+ if (wNorm + bNorm >= 1) {
104
+ const gray = wNorm / (wNorm + bNorm);
105
+ return [Math.round(gray * 10000) / 10000, Math.round(gray * 10000) / 10000, Math.round(gray * 10000) / 10000];
106
+ }
107
+ const hPrime = (h % 360) / 60;
108
+ const x = 1 - Math.abs((hPrime % 2) - 1);
109
+ let rChroma = 0;
110
+ let gChroma = 0;
111
+ let bChroma = 0;
112
+ if (hPrime >= 0 && hPrime < 1) {
113
+ [rChroma, gChroma, bChroma] = [1, x, 0];
114
+ }
115
+ else if (hPrime >= 1 && hPrime < 2) {
116
+ [rChroma, gChroma, bChroma] = [x, 1, 0];
117
+ }
118
+ else if (hPrime >= 2 && hPrime < 3) {
119
+ [rChroma, gChroma, bChroma] = [0, 1, x];
120
+ }
121
+ else if (hPrime >= 3 && hPrime < 4) {
122
+ [rChroma, gChroma, bChroma] = [0, x, 1];
123
+ }
124
+ else if (hPrime >= 4 && hPrime < 5) {
125
+ [rChroma, gChroma, bChroma] = [x, 0, 1];
126
+ }
127
+ else if (hPrime >= 5 && hPrime <= 6) {
128
+ [rChroma, gChroma, bChroma] = [1, 0, x];
129
+ }
130
+ const factor = 1 - wNorm - bNorm;
131
+ const srgb = [rChroma * factor + wNorm, gChroma * factor + wNorm, bChroma * factor + wNorm];
132
+ return srgb;
133
+ }
100
134
  function hsl2srgb(token) {
101
135
  let { h, s, l, a } = hslvalues(token) ?? {};
102
136
  if (h == null || s == null || l == null) {
103
137
  return null;
104
138
  }
139
+ // hue in degrees
105
140
  return hslvalues2srgbvalues(h, s, l, a);
106
141
  }
107
142
  function cmyk2srgbvalues(token) {
@@ -162,7 +197,7 @@ function hslvalues(token) {
162
197
  }
163
198
  let t;
164
199
  // @ts-ignore
165
- let h = getAngle(components[0]);
200
+ let h = getAngle(components[0]) * 360;
166
201
  // @ts-ignore
167
202
  t = components[1];
168
203
  // @ts-ignore
@@ -171,6 +206,12 @@ function hslvalues(token) {
171
206
  t = components[2];
172
207
  // @ts-ignore
173
208
  let l = getNumber(t);
209
+ if (components[1].typ == EnumToken.PercentageTokenType) {
210
+ s *= 100;
211
+ }
212
+ if (components[2].typ == EnumToken.PercentageTokenType) {
213
+ l *= 100;
214
+ }
174
215
  let a = null;
175
216
  if (components.length == 4) {
176
217
  // @ts-ignore
@@ -181,56 +222,38 @@ function hslvalues(token) {
181
222
  return a == null ? { h, s, l } : { h, s, l, a };
182
223
  }
183
224
  function hslvalues2srgbvalues(h, s, l, a = null) {
184
- let v = l <= 0.5 ? l * (1.0 + s) : l + s - l * s;
185
- let r = l;
186
- let g = l;
187
- let b = l;
188
- if (v > 0) {
189
- let m = l + l - v;
190
- let sv = (v - m) / v;
191
- h *= 6.0;
192
- let sextant = Math.floor(h);
193
- let fract = h - sextant;
194
- let vsf = v * sv * fract;
195
- let mid1 = m + vsf;
196
- let mid2 = v - vsf;
197
- switch (sextant) {
198
- case 0:
199
- r = v;
200
- g = mid1;
201
- b = m;
202
- break;
203
- case 1:
204
- r = mid2;
205
- g = v;
206
- b = m;
207
- break;
208
- case 2:
209
- r = m;
210
- g = v;
211
- b = mid1;
212
- break;
213
- case 3:
214
- r = m;
215
- g = mid2;
216
- b = v;
217
- break;
218
- case 4:
219
- r = mid1;
220
- g = m;
221
- b = v;
222
- break;
223
- case 5:
224
- r = v;
225
- g = m;
226
- b = mid2;
227
- break;
228
- }
225
+ const sNorm = s / 100;
226
+ const lNorm = l / 100;
227
+ const chroma = (1 - Math.abs(2 * lNorm - 1)) * sNorm;
228
+ const hPrime = h / 60;
229
+ const x = chroma * (1 - Math.abs((hPrime % 2) - 1));
230
+ let rTmp = 0;
231
+ let gTmp = 0;
232
+ let bTmp = 0;
233
+ if (hPrime >= 0 && hPrime < 1) {
234
+ [rTmp, gTmp, bTmp] = [chroma, x, 0];
229
235
  }
230
- const values = [r, g, b];
231
- if (a != null && a != 1) {
236
+ else if (hPrime >= 1 && hPrime < 2) {
237
+ [rTmp, gTmp, bTmp] = [x, chroma, 0];
238
+ }
239
+ else if (hPrime >= 2 && hPrime < 3) {
240
+ [rTmp, gTmp, bTmp] = [0, chroma, x];
241
+ }
242
+ else if (hPrime >= 3 && hPrime < 4) {
243
+ [rTmp, gTmp, bTmp] = [0, x, chroma];
244
+ }
245
+ else if (hPrime >= 4 && hPrime < 5) {
246
+ [rTmp, gTmp, bTmp] = [x, 0, chroma];
247
+ }
248
+ else if (hPrime >= 5 && hPrime <= 6) {
249
+ [rTmp, gTmp, bTmp] = [chroma, 0, x];
250
+ }
251
+ const m = lNorm - chroma / 2;
252
+ const values = [rTmp + m, gTmp + m, bTmp + m];
253
+ if (a != null) {
232
254
  values.push(a);
233
255
  }
256
+ // Return exact float decimals instead of multiplying by 255
234
257
  return values;
235
258
  }
236
259
  function lab2srgbvalues(token) {
@@ -301,4 +324,4 @@ function lsrgb2srgbvalues(r, g, b, alpha = null) {
301
324
  return rgb;
302
325
  }
303
326
 
304
- export { cmyk2srgbvalues, hex2srgbvalues, hsl2srgb, hslvalues, hslvalues2srgbvalues, hwb2srgbvalues, lab2srgbvalues, lch2srgbvalues, lsrgb2srgbvalues, oklab2srgbvalues, oklch2srgbvalues, rgb2srgb, rgb2srgbvalues, srgb2lsrgbvalues, srgbvalues, xyz2srgb };
327
+ export { cmyk2srgbvalues, hex2srgbvalues, hsl2srgb, hslvalues, hslvalues2srgbvalues, hwb2srgb, hwb2srgbvalues, lab2srgbvalues, lch2srgbvalues, lsrgb2srgbvalues, oklab2srgbvalues, oklch2srgbvalues, rgb2srgb, rgb2srgbvalues, srgb2lsrgbvalues, srgbvalues, xyz2srgb };
@@ -82,7 +82,8 @@ function isMFValue(featureName, tokens, isMFRange) {
82
82
  return { valid: true, success: false, isValueAllowed: false };
83
83
  }
84
84
  featureName = featureName.toLowerCase();
85
- if (!(featureName in config.mediaFeatures)) {
85
+ // @ts-expect-error
86
+ if (config.mediaFeatures[featureName] == null) {
86
87
  return { valid: false, success: false };
87
88
  }
88
89
  // @ts-expect-error
@@ -126,8 +127,7 @@ function isMFValue(featureName, tokens, isMFRange) {
126
127
  return {
127
128
  valid: true,
128
129
  success: (tokens.length == 1 &&
129
- tokens[0].typ == EnumToken.NumberTokenType &&
130
- tokens[0].val.typ == EnumToken.FractionTokenType) ||
130
+ tokens[0].typ == EnumToken.NumberTokenType) ||
131
131
  (tokens.length === 3 &&
132
132
  tokens[0].typ == EnumToken.NumberTokenType &&
133
133
  typeof tokens[0].val === "number" &&
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tbela99/css-parser",
3
3
  "description": "CSS parser, minifier and validator for node and the browser",
4
- "version": "1.6.5",
4
+ "version": "1.6.7",
5
5
  "exports": {
6
6
  ".": "./dist/node.js",
7
7
  "./node": "./dist/node.js",
@@ -68,17 +68,17 @@
68
68
  "@types/chai": "^5.2.3",
69
69
  "@types/chai-as-promised": "^8.0.2",
70
70
  "@types/mocha": "^10.0.10",
71
- "@types/node": "^26.4.1",
72
- "@types/web": "^0.0.354",
71
+ "@types/node": "^26.6.5",
72
+ "@types/web": "^0.0.358",
73
73
  "@web/test-runner": "^1.0.0",
74
74
  "@web/test-runner-playwright": "^1.0.0",
75
- "chai": "^6.2.2",
75
+ "chai": "^6.3.0",
76
76
  "chai-as-promised": "^8.0.2",
77
77
  "esno": "^4.8.0",
78
78
  "markdown-it-task-lists": "^2.1.1",
79
- "mocha": "^12.0.0",
80
- "playwright": "^1.62.1",
81
- "rollup": "^4.63.1",
79
+ "mocha": "^12.0.3",
80
+ "playwright": "^1.64.0",
81
+ "rollup": "^4.63.6",
82
82
  "rollup-plugin-dts": "^6.5.1",
83
83
  "superc8": "^12.7.0",
84
84
  "tslib": "^2.8.1",
@@ -89,4 +89,4 @@
89
89
  "typedoc-unhoax-theme": "^0.5.3"
90
90
  },
91
91
  "sideEffects": false
92
- }
92
+ }