@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.
@@ -24,35 +24,43 @@
24
24
  /**
25
25
  * Node passed validation
26
26
  */
27
- EnumAstNodeStatus[EnumAstNodeStatus["Validated"] = 0] = "Validated";
27
+ EnumAstNodeStatus[EnumAstNodeStatus["Validated"] = 1] = "Validated";
28
28
  /**
29
29
  * Node is invalid
30
30
  */
31
- EnumAstNodeStatus[EnumAstNodeStatus["Invalid"] = 1] = "Invalid";
31
+ EnumAstNodeStatus[EnumAstNodeStatus["Invalid"] = 2] = "Invalid";
32
32
  /**
33
33
  * node is not validated
34
34
  */
35
- EnumAstNodeStatus[EnumAstNodeStatus["Unvalidated"] = 2] = "Unvalidated";
35
+ EnumAstNodeStatus[EnumAstNodeStatus["Unvalidated"] = 4] = "Unvalidated";
36
36
  /**
37
37
  * Node did not pass validation, but is preserved.
38
38
  */
39
- EnumAstNodeStatus[EnumAstNodeStatus["ValidationFailed"] = 3] = "ValidationFailed";
39
+ EnumAstNodeStatus[EnumAstNodeStatus["ValidationFailed"] = 8] = "ValidationFailed";
40
40
  /**
41
41
  * Parsing the node is not supported yet or the node syntax definition does not exist.
42
42
  */
43
- EnumAstNodeStatus[EnumAstNodeStatus["Unknown"] = 4] = "Unknown";
43
+ EnumAstNodeStatus[EnumAstNodeStatus["Unknown"] = 16] = "Unknown";
44
44
  /**
45
45
  * Failed to parse the node.
46
46
  */
47
- EnumAstNodeStatus[EnumAstNodeStatus["Unparsed"] = 5] = "Unparsed";
47
+ EnumAstNodeStatus[EnumAstNodeStatus["Unparsed"] = 32] = "Unparsed";
48
48
  /**
49
49
  * Node is disallowed in the context
50
50
  */
51
- EnumAstNodeStatus[EnumAstNodeStatus["Disallowed"] = 6] = "Disallowed";
51
+ EnumAstNodeStatus[EnumAstNodeStatus["Disallowed"] = 64] = "Disallowed";
52
52
  /**
53
53
  * Node is malformed
54
54
  */
55
- EnumAstNodeStatus[EnumAstNodeStatus["Malformed"] = 7] = "Malformed";
55
+ EnumAstNodeStatus[EnumAstNodeStatus["Malformed"] = 128] = "Malformed";
56
+ /**
57
+ * Important declaration
58
+ */
59
+ EnumAstNodeStatus[EnumAstNodeStatus["ImportantDeclaration"] = 256] = "ImportantDeclaration";
60
+ /**
61
+ * Color component without unit
62
+ */
63
+ EnumAstNodeStatus[EnumAstNodeStatus["PowerlessColorComponent"] = 512] = "PowerlessColorComponent";
56
64
  })(exports.EnumAstNodeStatus || (exports.EnumAstNodeStatus = {}));
57
65
  /**
58
66
  * Enum of validation levels
@@ -3515,7 +3523,7 @@
3515
3523
  syntax: "<number> | <percentage> | none"
3516
3524
  },
3517
3525
  "an+b": {
3518
- syntax: "odd | even | <integer> | <n-dimension> | '+'?† n | -n | <ndashdigit-dimension> | '+'?† <ndashdigit-ident> | <dashndashdigit-ident> | <n-dimension> <signed-integer> | '+'?† n <signed-integer> | -n <signed-integer> | <ndash-dimension> <signless-integer> | '+'?† n- <signless-integer> | -n- <signless-integer> | <n-dimension> ['+' | '-'] <signless-integer> | '+'?† n ['+' | '-'] <signless-integer> | -n ['+' | '-'] <signless-integer>"
3526
+ syntax: "odd | even | <integer> | <n-dimension> | '+'? n | -n | <ndashdigit-dimension> | '+'? <ndashdigit-ident> | <dashndashdigit-ident> | <n-dimension> <signed-integer> | '+'? n <signed-integer> | -n <signed-integer> | <ndash-dimension> <signless-integer> | '+'? n- <signless-integer> | -n- <signless-integer> | <n-dimension> ['+' | '-'] <signless-integer> | '+'? n ['+' | '-'] <signless-integer> | -n ['+' | '-'] <signless-integer>"
3519
3527
  },
3520
3528
  "anchor()": {
3521
3529
  syntax: "anchor( <anchor-name>? && <anchor-side>, <length-percentage>? )"
@@ -5548,6 +5556,9 @@
5548
5556
  marks: {
5549
5557
  syntax: "none | [ crop || cross ]"
5550
5558
  },
5559
+ "page-margin-safety": {
5560
+ syntax: "none | clamp | add"
5561
+ },
5551
5562
  "page-orientation": {
5552
5563
  syntax: "upright | rotate-left | rotate-right"
5553
5564
  },
@@ -7482,7 +7493,7 @@
7482
7493
  { typ: exports.EnumToken.NumberTokenType, val: values[1] },
7483
7494
  { typ: exports.EnumToken.NumberTokenType, val: values[2] },
7484
7495
  ];
7485
- if (values.length == 4) {
7496
+ if (values.length == 4 && values[3] != 1) {
7486
7497
  chi.push({ typ: exports.EnumToken.LiteralTokenType, val: "/" }, {
7487
7498
  typ: exports.EnumToken.PercentageTokenType,
7488
7499
  val: values[3] * 100,
@@ -7871,6 +7882,222 @@
7871
7882
  return xyz.map((value, i) => value * D50[i]);
7872
7883
  }
7873
7884
 
7885
+ function rgb2hwbToken(token) {
7886
+ const values = rgb2hwbvalues(token);
7887
+ if (values == null) {
7888
+ return null;
7889
+ }
7890
+ return hwbToken(values);
7891
+ }
7892
+ function hsl2hwbToken(token) {
7893
+ const values = hsl2hwbvalues(token);
7894
+ if (values == null) {
7895
+ return null;
7896
+ }
7897
+ // console.error(new Error("hsl2hwbToken"));
7898
+ return hwbToken(values);
7899
+ }
7900
+ function cmyk2hwbToken(token) {
7901
+ const values = cmyk2hwbvalues(token);
7902
+ if (values == null) {
7903
+ return null;
7904
+ }
7905
+ return hwbToken(values);
7906
+ }
7907
+ function oklab2hwbToken(token) {
7908
+ const values = oklab2hwbvalues(token);
7909
+ if (values == null) {
7910
+ return null;
7911
+ }
7912
+ return hwbToken(values);
7913
+ }
7914
+ function oklch2hwbToken(token) {
7915
+ const values = oklch2hwbvalues(token);
7916
+ if (values == null) {
7917
+ return null;
7918
+ }
7919
+ return hwbToken(values);
7920
+ }
7921
+ function lab2hwbToken(token) {
7922
+ const values = lab2hwbvalues(token);
7923
+ if (values == null) {
7924
+ return null;
7925
+ }
7926
+ return hwbToken(values);
7927
+ }
7928
+ function lch2hwbToken(token) {
7929
+ const values = lch2hwbvalues(token);
7930
+ if (values == null) {
7931
+ return null;
7932
+ }
7933
+ return hwbToken(values);
7934
+ }
7935
+ function color2hwbToken(token) {
7936
+ const values = color2hwbvalues(token);
7937
+ if (values == null) {
7938
+ return null;
7939
+ }
7940
+ return hwbToken(values);
7941
+ }
7942
+ function hwbToken(values) {
7943
+ const chi = [
7944
+ { typ: exports.EnumToken.NumberTokenType, val: values[0] },
7945
+ { typ: exports.EnumToken.PercentageTokenType, val: values[1] },
7946
+ { typ: exports.EnumToken.PercentageTokenType, val: values[2] },
7947
+ ];
7948
+ if (values.length == 4) {
7949
+ chi.push({ typ: exports.EnumToken.LiteralTokenType, val: "/" }, {
7950
+ typ: exports.EnumToken.PercentageTokenType,
7951
+ val: values[3] * 100,
7952
+ });
7953
+ }
7954
+ return {
7955
+ typ: exports.EnumToken.ColorTokenType,
7956
+ val: "hwb",
7957
+ chi,
7958
+ kin: exports.ColorType.HWB,
7959
+ };
7960
+ }
7961
+ function rgb2hwbvalues(token) {
7962
+ const values = getColorComponents(token).map((t, index) => {
7963
+ if (index == 3) {
7964
+ return getNumber(t);
7965
+ }
7966
+ return getNumber(t) / 255;
7967
+ });
7968
+ // @ts-ignore
7969
+ return srgb2hwb(values[0], values[1], values[2], values[3]);
7970
+ }
7971
+ function cmyk2hwbvalues(token) {
7972
+ const values = cmyk2srgbvalues(token);
7973
+ return values == null ? null : srgb2hwb(values[0], values[1], values[2], values[3]);
7974
+ }
7975
+ function hsl2hwbvalues(token) {
7976
+ const values = hslvalues(token);
7977
+ if (values == null) {
7978
+ return null;
7979
+ }
7980
+ // @ts-ignore
7981
+ return hslvalues2hwbvalues(values.h, values.s, values.l, values.a);
7982
+ }
7983
+ function lab2hwbvalues(token) {
7984
+ const values = lab2srgbvalues(token);
7985
+ if (values == null) {
7986
+ return null;
7987
+ }
7988
+ return srgb2hwb(values[0], values[1], values[2], values[3]);
7989
+ }
7990
+ function lch2hwbvalues(token) {
7991
+ const values = lch2srgbvalues(token);
7992
+ if (values == null) {
7993
+ return null;
7994
+ }
7995
+ return srgb2hwb(values[0], values[1], values[2], values[3]);
7996
+ }
7997
+ function oklab2hwbvalues(token) {
7998
+ const values = oklab2srgbvalues(token);
7999
+ if (values == null) {
8000
+ return null;
8001
+ }
8002
+ // @ts-ignore
8003
+ return srgb2hwb(values[0], values[1], values[2], values[3]);
8004
+ }
8005
+ function oklch2hwbvalues(token) {
8006
+ const values = oklch2srgbvalues(token);
8007
+ // @ts-ignore
8008
+ return values == null ? null : srgb2hwb(values[0], values[1], values[2], values[3]);
8009
+ }
8010
+ function color2hwbvalues(token) {
8011
+ const values = color2srgbvalues(token);
8012
+ if (values == null) {
8013
+ return null;
8014
+ }
8015
+ // @ts-ignore
8016
+ return srgb2hwb(values[0], values[1], values[2], values[3]);
8017
+ }
8018
+ function hwbvalues(token) {
8019
+ const components = getColorComponents(token);
8020
+ if (components == null) {
8021
+ return null;
8022
+ }
8023
+ let t;
8024
+ // @ts-ignore
8025
+ let h = getAngle(components[0]) * 360;
8026
+ // @ts-ignore
8027
+ t = components[1];
8028
+ // @ts-ignore
8029
+ let w = getNumber(t);
8030
+ // @ts-ignore
8031
+ t = components[2];
8032
+ // @ts-ignore
8033
+ let b = getNumber(t);
8034
+ // if (components[1].typ == EnumToken.NumberTokenType) {
8035
+ w *= 100;
8036
+ // }
8037
+ // if (components[2].typ == EnumToken.NumberTokenType) {
8038
+ b *= 100;
8039
+ // }
8040
+ let a = null;
8041
+ if (components.length == 4) {
8042
+ // @ts-ignore
8043
+ t = components[3];
8044
+ // @ts-ignore
8045
+ a = getNumber(t);
8046
+ }
8047
+ return a == null ? { h, w, b } : { h, w, b, a };
8048
+ }
8049
+ function srgb2hwb(r, g, b, a = null, fallback = 0) {
8050
+ // 1. Clamp inputs to the standard web-safe sRGB gamut [0.0, 1.0]
8051
+ const rClamp = Math.min(1, Math.max(0, r));
8052
+ const gClamp = Math.min(1, Math.max(0, g));
8053
+ const bClamp = Math.min(1, Math.max(0, b));
8054
+ const cMax = Math.max(rClamp, gClamp, bClamp);
8055
+ const cMin = Math.min(rClamp, gClamp, bClamp);
8056
+ const delta = cMax - cMin;
8057
+ // 2. Whiteness and Blackness calculations
8058
+ const w = cMin;
8059
+ const bl = 1 - cMax;
8060
+ // 3. Calculate Hue
8061
+ let h = 0;
8062
+ if (delta !== 0) {
8063
+ if (cMax === rClamp) {
8064
+ h = 60 * (((gClamp - bClamp) / delta) % 6);
8065
+ }
8066
+ else if (cMax === gClamp) {
8067
+ h = 60 * ((bClamp - rClamp) / delta + 2);
8068
+ }
8069
+ else if (cMax === bClamp) {
8070
+ h = 60 * ((rClamp - gClamp) / delta + 4);
8071
+ }
8072
+ if (h < 0) {
8073
+ h += 360;
8074
+ }
8075
+ }
8076
+ const result = [h, w * 100, bl * 100];
8077
+ if (a != null) {
8078
+ result.push(a);
8079
+ }
8080
+ return result;
8081
+ }
8082
+ function hslvalues2hwbvalues(h, s, l, a = null) {
8083
+ const sNorm = s / 100;
8084
+ const lNorm = l / 100;
8085
+ // Keep Hue cleanly bound to standard circle loops
8086
+ const hue = ((h % 360) + 360) % 360;
8087
+ // 1. Compute the intermediate HSV Value component (brightness peak)
8088
+ const v = lNorm + sNorm * Math.min(lNorm, 1 - lNorm);
8089
+ // 2. Compute Chroma
8090
+ const chroma = 2 * (v - lNorm);
8091
+ // 3. Extract Whiteness and Blackness
8092
+ const w = v - chroma;
8093
+ const b = 1 - v;
8094
+ const hwb = [hue, w * 100, b * 100];
8095
+ if (a != null && a != 1) {
8096
+ hwb.push(a);
8097
+ }
8098
+ return hwb;
8099
+ }
8100
+
7874
8101
  // from https://www.w3.org/TR/css-color-4/#color-conversion-code
7875
8102
  // srgb-linear -> srgb
7876
8103
  // 0 <= r, g, b <= 1
@@ -7944,25 +8171,59 @@
7944
8171
  return lsrgb2srgbvalues(values[0], values[1], values[2], alpha);
7945
8172
  }
7946
8173
  function hwb2srgbvalues(token) {
7947
- const { h: hue, s: white, l: black, a: alpha } = hslvalues(token) ?? {};
7948
- if (hue == null || white == null || black == null) {
8174
+ const { h, w, b, a } = hwbvalues(token) ?? {};
8175
+ if (h == null || w == null || b == null) {
7949
8176
  return [];
7950
8177
  }
7951
- const rgb = hslvalues2srgbvalues(hue, 1, 0.5);
7952
- for (let i = 0; i < 3; i++) {
7953
- rgb[i] *= 1 - white - black;
7954
- rgb[i] = rgb[i] + white;
7955
- }
7956
- if (alpha != null && alpha != 1) {
7957
- rgb.push(alpha);
8178
+ const rgb = hwb2srgb(h, w, b);
8179
+ if (a != null && a != 1) {
8180
+ rgb.push(a);
7958
8181
  }
7959
8182
  return rgb;
7960
8183
  }
8184
+ /**
8185
+ * convert hwb to sRGB
8186
+ */
8187
+ function hwb2srgb(h, w, b, alpha) {
8188
+ const wNorm = w / 100;
8189
+ const bNorm = b / 100;
8190
+ if (wNorm + bNorm >= 1) {
8191
+ const gray = wNorm / (wNorm + bNorm);
8192
+ return [Math.round(gray * 10000) / 10000, Math.round(gray * 10000) / 10000, Math.round(gray * 10000) / 10000];
8193
+ }
8194
+ const hPrime = (h % 360) / 60;
8195
+ const x = 1 - Math.abs((hPrime % 2) - 1);
8196
+ let rChroma = 0;
8197
+ let gChroma = 0;
8198
+ let bChroma = 0;
8199
+ if (hPrime >= 0 && hPrime < 1) {
8200
+ [rChroma, gChroma, bChroma] = [1, x, 0];
8201
+ }
8202
+ else if (hPrime >= 1 && hPrime < 2) {
8203
+ [rChroma, gChroma, bChroma] = [x, 1, 0];
8204
+ }
8205
+ else if (hPrime >= 2 && hPrime < 3) {
8206
+ [rChroma, gChroma, bChroma] = [0, 1, x];
8207
+ }
8208
+ else if (hPrime >= 3 && hPrime < 4) {
8209
+ [rChroma, gChroma, bChroma] = [0, x, 1];
8210
+ }
8211
+ else if (hPrime >= 4 && hPrime < 5) {
8212
+ [rChroma, gChroma, bChroma] = [x, 0, 1];
8213
+ }
8214
+ else if (hPrime >= 5 && hPrime <= 6) {
8215
+ [rChroma, gChroma, bChroma] = [1, 0, x];
8216
+ }
8217
+ const factor = 1 - wNorm - bNorm;
8218
+ const srgb = [rChroma * factor + wNorm, gChroma * factor + wNorm, bChroma * factor + wNorm];
8219
+ return srgb;
8220
+ }
7961
8221
  function hsl2srgb(token) {
7962
8222
  let { h, s, l, a } = hslvalues(token) ?? {};
7963
8223
  if (h == null || s == null || l == null) {
7964
8224
  return null;
7965
8225
  }
8226
+ // hue in degrees
7966
8227
  return hslvalues2srgbvalues(h, s, l, a);
7967
8228
  }
7968
8229
  function cmyk2srgbvalues(token) {
@@ -8023,7 +8284,7 @@
8023
8284
  }
8024
8285
  let t;
8025
8286
  // @ts-ignore
8026
- let h = getAngle(components[0]);
8287
+ let h = getAngle(components[0]) * 360;
8027
8288
  // @ts-ignore
8028
8289
  t = components[1];
8029
8290
  // @ts-ignore
@@ -8032,6 +8293,12 @@
8032
8293
  t = components[2];
8033
8294
  // @ts-ignore
8034
8295
  let l = getNumber(t);
8296
+ if (components[1].typ == exports.EnumToken.PercentageTokenType) {
8297
+ s *= 100;
8298
+ }
8299
+ if (components[2].typ == exports.EnumToken.PercentageTokenType) {
8300
+ l *= 100;
8301
+ }
8035
8302
  let a = null;
8036
8303
  if (components.length == 4) {
8037
8304
  // @ts-ignore
@@ -8042,56 +8309,38 @@
8042
8309
  return a == null ? { h, s, l } : { h, s, l, a };
8043
8310
  }
8044
8311
  function hslvalues2srgbvalues(h, s, l, a = null) {
8045
- let v = l <= 0.5 ? l * (1.0 + s) : l + s - l * s;
8046
- let r = l;
8047
- let g = l;
8048
- let b = l;
8049
- if (v > 0) {
8050
- let m = l + l - v;
8051
- let sv = (v - m) / v;
8052
- h *= 6.0;
8053
- let sextant = Math.floor(h);
8054
- let fract = h - sextant;
8055
- let vsf = v * sv * fract;
8056
- let mid1 = m + vsf;
8057
- let mid2 = v - vsf;
8058
- switch (sextant) {
8059
- case 0:
8060
- r = v;
8061
- g = mid1;
8062
- b = m;
8063
- break;
8064
- case 1:
8065
- r = mid2;
8066
- g = v;
8067
- b = m;
8068
- break;
8069
- case 2:
8070
- r = m;
8071
- g = v;
8072
- b = mid1;
8073
- break;
8074
- case 3:
8075
- r = m;
8076
- g = mid2;
8077
- b = v;
8078
- break;
8079
- case 4:
8080
- r = mid1;
8081
- g = m;
8082
- b = v;
8083
- break;
8084
- case 5:
8085
- r = v;
8086
- g = m;
8087
- b = mid2;
8088
- break;
8089
- }
8090
- }
8091
- const values = [r, g, b];
8092
- if (a != null && a != 1) {
8312
+ const sNorm = s / 100;
8313
+ const lNorm = l / 100;
8314
+ const chroma = (1 - Math.abs(2 * lNorm - 1)) * sNorm;
8315
+ const hPrime = h / 60;
8316
+ const x = chroma * (1 - Math.abs((hPrime % 2) - 1));
8317
+ let rTmp = 0;
8318
+ let gTmp = 0;
8319
+ let bTmp = 0;
8320
+ if (hPrime >= 0 && hPrime < 1) {
8321
+ [rTmp, gTmp, bTmp] = [chroma, x, 0];
8322
+ }
8323
+ else if (hPrime >= 1 && hPrime < 2) {
8324
+ [rTmp, gTmp, bTmp] = [x, chroma, 0];
8325
+ }
8326
+ else if (hPrime >= 2 && hPrime < 3) {
8327
+ [rTmp, gTmp, bTmp] = [0, chroma, x];
8328
+ }
8329
+ else if (hPrime >= 3 && hPrime < 4) {
8330
+ [rTmp, gTmp, bTmp] = [0, x, chroma];
8331
+ }
8332
+ else if (hPrime >= 4 && hPrime < 5) {
8333
+ [rTmp, gTmp, bTmp] = [x, 0, chroma];
8334
+ }
8335
+ else if (hPrime >= 5 && hPrime <= 6) {
8336
+ [rTmp, gTmp, bTmp] = [chroma, 0, x];
8337
+ }
8338
+ const m = lNorm - chroma / 2;
8339
+ const values = [rTmp + m, gTmp + m, bTmp + m];
8340
+ if (a != null) {
8093
8341
  values.push(a);
8094
8342
  }
8343
+ // Return exact float decimals instead of multiplying by 255
8095
8344
  return values;
8096
8345
  }
8097
8346
  function lab2srgbvalues(token) {
@@ -8464,25 +8713,6 @@
8464
8713
  return lch2srgbvalues(token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
8465
8714
  }
8466
8715
 
8467
- function hwb2hsv(h, w, b, a) {
8468
- // @ts-ignore
8469
- return [h, 1 - w / (1 - b), 1 - b, a];
8470
- }
8471
- // https://gist.github.com/defims/0ca2ef8832833186ed396a2f8a204117#file-annotated-js
8472
- function hsl2hsv(h, s, l, a = null) {
8473
- s *= l < .5 ? l : 1 - l;
8474
- const result = [
8475
- //Range should be between 0 - 1
8476
- h, //Hue stays the same
8477
- 2 * s / (l + s), //Saturation
8478
- l + s //Value
8479
- ];
8480
- if (a != null) {
8481
- result.push(a);
8482
- }
8483
- return result;
8484
- }
8485
-
8486
8716
  function hex2HslToken(token) {
8487
8717
  let values = hex2srgbvalues(token);
8488
8718
  if (values == null) {
@@ -8547,11 +8777,17 @@
8547
8777
  return hslToken(srgb2hslvalues(values[0], values[1], values[2], values[3]));
8548
8778
  }
8549
8779
  function hslToken(values) {
8550
- values[0] = values[0] * 360;
8780
+ if (values[1] == 0) {
8781
+ values[0] = 0;
8782
+ }
8783
+ if (values[2] == 1 || values[2] == 0) {
8784
+ values[0] = 0;
8785
+ values[1] = 0;
8786
+ }
8551
8787
  const chi = [
8552
8788
  { typ: exports.EnumToken.NumberTokenType, val: values[0] },
8553
- { typ: exports.EnumToken.PercentageTokenType, val: values[1] * 100 },
8554
- { typ: exports.EnumToken.PercentageTokenType, val: values[2] * 100 },
8789
+ { typ: exports.EnumToken.PercentageTokenType, val: values[1] },
8790
+ { typ: exports.EnumToken.PercentageTokenType, val: values[2] },
8555
8791
  ];
8556
8792
  if (values.length == 4 && values[3] != 1) {
8557
8793
  chi.push({ typ: exports.EnumToken.LiteralTokenType, val: "/" }, {
@@ -8594,32 +8830,33 @@
8594
8830
  }
8595
8831
  return rgbvalues2hslvalues(values[0], values[1], values[2], values[3]);
8596
8832
  }
8597
- // https://gist.github.com/defims/0ca2ef8832833186ed396a2f8a204117#file-annotated-js
8598
- function hsv2hsl(h, s, v, a) {
8599
- const result = [
8600
- //[hue, saturation, lightness]
8601
- //Range should be between 0 - 1
8602
- h, //Hue stays the same
8603
- //Saturation is very different between the two color spaces
8604
- //If (2-sat)*val < 1 set it to sat*val/((2-sat)*val)
8605
- //Otherwise sat*val/(2-(2-sat)*val)
8606
- //Conditional is not operating with hue, it is reassigned!
8607
- (s * v) / ((h = (2 - s) * v) < 1 ? h : 2 - h),
8608
- h / 2, //Lightness is (2-sat)*val/2
8609
- ];
8610
- if (a != null) {
8611
- result.push(a);
8612
- }
8613
- return result;
8614
- }
8615
8833
  function cmyk2hslvalues(token) {
8616
8834
  const values = cmyk2rgbvalues(token);
8617
8835
  return values == null ? null : rgbvalues2hslvalues(values[0], values[1], values[2], values[3]);
8618
8836
  }
8619
8837
  function hwb2hslvalues(token) {
8620
- const hsla = hslvalues(token);
8621
- const hwba = hwb2hsv(hsla.h, hsla.s, hsla.l, hsla.a);
8622
- return hsv2hsl(hwba[0], hwba[1], hwba[2], hwba[3]);
8838
+ const { h, w, b, a } = hwbvalues(token) ?? {};
8839
+ if (h == null || w == null || b == null) {
8840
+ return null;
8841
+ }
8842
+ const wNorm = w / 100;
8843
+ const bNorm = b / 100;
8844
+ const hue = ((h % 360) + 360) % 360;
8845
+ if (wNorm + bNorm >= 1) {
8846
+ const grayLightness = wNorm / (wNorm + bNorm);
8847
+ return [hue, 0, grayLightness * 100];
8848
+ }
8849
+ const lightness = (1 - bNorm + wNorm) / 2;
8850
+ let saturation = 0;
8851
+ if (lightness > 0 && lightness < 1) {
8852
+ const chroma = 1 - wNorm - bNorm;
8853
+ saturation = chroma / (1 - Math.abs(2 * lightness - 1));
8854
+ }
8855
+ const hsl = [hue, saturation * 100, lightness * 100];
8856
+ if (a != null && a != 1) {
8857
+ hsl.push(a);
8858
+ }
8859
+ return hsl;
8623
8860
  }
8624
8861
  function lab2hslvalues(token) {
8625
8862
  const values = lab2rgbvalues(token);
@@ -8650,28 +8887,28 @@
8650
8887
  return srgb2hslvalues(r / 255, g / 255, b / 255, a);
8651
8888
  }
8652
8889
  function srgb2hslvalues(r, g, b, a = null) {
8653
- let max = Math.max(r, g, b);
8654
- let min = Math.min(r, g, b);
8890
+ const max = Math.max(r, g, b);
8891
+ const min = Math.min(r, g, b);
8892
+ const delta = max - min;
8893
+ const l = (max + min) / 2;
8655
8894
  let h = 0;
8656
8895
  let s = 0;
8657
- let l = (max + min) / 2;
8658
- if (max != min) {
8659
- let d = max - min;
8660
- s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
8896
+ if (delta !== 0) {
8897
+ s = delta / (1 - Math.abs(2 * l - 1));
8661
8898
  switch (max) {
8662
8899
  case r:
8663
- h = (g - b) / d + (g < b ? 6 : 0);
8900
+ h = 60 * (((g - b) / delta) % 6);
8664
8901
  break;
8665
8902
  case g:
8666
- h = (b - r) / d + 2;
8903
+ h = 60 * ((b - r) / delta + 2);
8667
8904
  break;
8668
8905
  case b:
8669
- h = (r - g) / d + 4;
8906
+ h = 60 * ((r - g) / delta + 4);
8670
8907
  break;
8671
8908
  }
8672
- h /= 6;
8673
8909
  }
8674
- const hsl = [h, s, l];
8910
+ h = ((h % 360) + 360) % 360;
8911
+ const hsl = [h, s * 100, l * 100];
8675
8912
  if (a != null && a < 1) {
8676
8913
  // @ts-ignore
8677
8914
  return hsl.concat([a]);
@@ -8680,191 +8917,6 @@
8680
8917
  return hsl;
8681
8918
  }
8682
8919
 
8683
- function rgb2hwbToken(token) {
8684
- const values = rgb2hwbvalues(token);
8685
- if (values == null) {
8686
- return null;
8687
- }
8688
- return hwbToken(values);
8689
- }
8690
- function hsl2hwbToken(token) {
8691
- const values = hsl2hwbvalues(token);
8692
- if (values == null) {
8693
- return null;
8694
- }
8695
- return hwbToken(values);
8696
- }
8697
- function cmyk2hwbToken(token) {
8698
- const values = cmyk2hwbvalues(token);
8699
- if (values == null) {
8700
- return null;
8701
- }
8702
- return hwbToken(values);
8703
- }
8704
- function oklab2hwbToken(token) {
8705
- const values = oklab2hwbvalues(token);
8706
- if (values == null) {
8707
- return null;
8708
- }
8709
- return hwbToken(values);
8710
- }
8711
- function oklch2hwbToken(token) {
8712
- const values = oklch2hwbvalues(token);
8713
- if (values == null) {
8714
- return null;
8715
- }
8716
- return hwbToken(values);
8717
- }
8718
- function lab2hwbToken(token) {
8719
- const values = lab2hwbvalues(token);
8720
- if (values == null) {
8721
- return null;
8722
- }
8723
- return hwbToken(values);
8724
- }
8725
- function lch2hwbToken(token) {
8726
- const values = lch2hwbvalues(token);
8727
- if (values == null) {
8728
- return null;
8729
- }
8730
- return hwbToken(values);
8731
- }
8732
- function color2hwbToken(token) {
8733
- const values = color2hwbvalues(token);
8734
- if (values == null) {
8735
- return null;
8736
- }
8737
- return hwbToken(values);
8738
- }
8739
- function hwbToken(values) {
8740
- values[0] = values[0] * 360;
8741
- const chi = [
8742
- { typ: exports.EnumToken.NumberTokenType, val: values[0] },
8743
- { typ: exports.EnumToken.PercentageTokenType, val: values[1] * 100 },
8744
- { typ: exports.EnumToken.PercentageTokenType, val: values[2] * 100 },
8745
- ];
8746
- if (values.length == 4) {
8747
- chi.push({ typ: exports.EnumToken.LiteralTokenType, val: "/" }, {
8748
- typ: exports.EnumToken.PercentageTokenType,
8749
- val: values[3] * 100,
8750
- });
8751
- }
8752
- return {
8753
- typ: exports.EnumToken.ColorTokenType,
8754
- val: "hwb",
8755
- chi,
8756
- kin: exports.ColorType.HWB,
8757
- };
8758
- }
8759
- function rgb2hwbvalues(token) {
8760
- const values = getColorComponents(token).map((t, index) => {
8761
- if (index == 3) {
8762
- return getNumber(t);
8763
- }
8764
- return getNumber(t) / 255;
8765
- });
8766
- // @ts-ignore
8767
- return srgb2hwb(values[0], values[1], values[2], values[3]);
8768
- }
8769
- function cmyk2hwbvalues(token) {
8770
- const values = cmyk2srgbvalues(token);
8771
- return values == null ? null : srgb2hwb(values[0], values[1], values[2], values[3]);
8772
- }
8773
- function hsl2hwbvalues(token) {
8774
- const values = getColorComponents(token).map((t, index) => {
8775
- if (index == 3 && t.typ == exports.EnumToken.IdenTokenType && t.val == "none") {
8776
- return 1;
8777
- }
8778
- if (index == 0) {
8779
- return getAngle(t);
8780
- }
8781
- return getNumber(t);
8782
- });
8783
- // @ts-ignore
8784
- return hslvalues2hwbvalues(values[0], values[1], values[2], values[3]);
8785
- }
8786
- function lab2hwbvalues(token) {
8787
- const values = lab2srgbvalues(token);
8788
- if (values == null) {
8789
- return null;
8790
- }
8791
- return srgb2hwb(values[0], values[1], values[2], values[3]);
8792
- }
8793
- function lch2hwbvalues(token) {
8794
- const values = lch2srgbvalues(token);
8795
- if (values == null) {
8796
- return null;
8797
- }
8798
- return srgb2hwb(values[0], values[1], values[2], values[3]);
8799
- }
8800
- function oklab2hwbvalues(token) {
8801
- const values = oklab2srgbvalues(token);
8802
- if (values == null) {
8803
- return null;
8804
- }
8805
- // @ts-ignore
8806
- return srgb2hwb(values[0], values[1], values[2], values[3]);
8807
- }
8808
- function oklch2hwbvalues(token) {
8809
- const values = oklch2srgbvalues(token);
8810
- // @ts-ignore
8811
- return values == null ? null : srgb2hwb(values[0], values[1], values[2], values[3]);
8812
- }
8813
- function rgb2hue(r, g, b, fallback = 0) {
8814
- let value = rgb2value(r, g, b);
8815
- let whiteness = rgb2whiteness(r, g, b);
8816
- let delta = value - whiteness;
8817
- if (delta > 0) {
8818
- // calculate segment
8819
- let segment = value === r ? (g - b) / delta : value === g ? (b - r) / delta : (r - g) / delta;
8820
- // calculate shift
8821
- let shift = value === r ? (segment < 0 ? 360 / 60 : 0 / 60) : value === g ? 120 / 60 : 240 / 60;
8822
- // calculate hue
8823
- return (segment + shift) * 60;
8824
- }
8825
- return fallback;
8826
- }
8827
- function rgb2value(r, g, b) {
8828
- return Math.max(r, g, b);
8829
- }
8830
- function rgb2whiteness(r, g, b) {
8831
- return Math.min(r, g, b);
8832
- }
8833
- function color2hwbvalues(token) {
8834
- const values = color2srgbvalues(token);
8835
- if (values == null) {
8836
- return null;
8837
- }
8838
- // @ts-ignore
8839
- return srgb2hwb(values[0], values[1], values[2], values[3]);
8840
- }
8841
- function srgb2hwb(r, g, b, a = null, fallback = 0) {
8842
- r *= 100;
8843
- g *= 100;
8844
- b *= 100;
8845
- let hue = rgb2hue(r, g, b, fallback);
8846
- let whiteness = rgb2whiteness(r, g, b);
8847
- let value = Math.round(rgb2value(r, g, b));
8848
- let blackness = 100 - value;
8849
- const result = [hue / 360, whiteness / 100, blackness / 100];
8850
- if (a != null) {
8851
- result.push(a);
8852
- }
8853
- return result;
8854
- }
8855
- function hsv2hwb(h, s, v, a = null) {
8856
- const result = [h, (1 - s) * v, 1 - v];
8857
- if (a != null) {
8858
- result.push(a);
8859
- }
8860
- return result;
8861
- }
8862
- function hslvalues2hwbvalues(h, s, l, a = null) {
8863
- let values = hsl2hsv(h, s, l);
8864
- // @ts-ignore
8865
- return hsv2hwb(values[0], values[1], values[2], a);
8866
- }
8867
-
8868
8920
  function prophotorgb2srgbvalues(r, g, b, a = null) {
8869
8921
  let values = prophotorgb2xyz50(r, g, b);
8870
8922
  // @ts-ignore
@@ -9166,6 +9218,7 @@
9166
9218
  const percentages = [];
9167
9219
  const srgbComponentValues = [];
9168
9220
  const colorComponents = [];
9221
+ const colors = [];
9169
9222
  if (args[i]?.typ === exports.EnumToken.IdenTokenType) {
9170
9223
  if (!equalsIgnoreCase(args[i++].val, "in")) {
9171
9224
  return null;
@@ -9215,9 +9268,10 @@
9215
9268
  if (values == null) {
9216
9269
  return null;
9217
9270
  }
9218
- if (colorSpace == "hwb") {
9219
- colorSpace = "srgb";
9220
- }
9271
+ // if (colorSpace == "hwb") {
9272
+ // colorSpace = "srgb";
9273
+ // }
9274
+ colors.push(processPowerlessComponents(args[i]));
9221
9275
  switch (colorSpace) {
9222
9276
  case "srgb":
9223
9277
  break;
@@ -9247,17 +9301,12 @@
9247
9301
  values = srgb2xyz_d65(values[0], values[1], values[2], values[3]);
9248
9302
  values = XYZ_D65_to_D50(values[0], values[1], values[2], values[3]);
9249
9303
  break;
9250
- // case "rgb":
9251
- // for (let j = 0; j < values.length; j++) {
9252
- // values[j] = j == 3 ? values[j] : srgb2rgb(values[j]);
9253
- // }
9254
- // break;
9255
9304
  case "hsl":
9256
9305
  values = srgb2hslvalues(values[0], values[1], values[2], values[3]);
9257
9306
  break;
9258
- // case "hwb":
9259
- // values = srgb2hwb(values[0], values[1], values[2], values[3]);
9260
- // break;
9307
+ case "hwb":
9308
+ values = srgb2hwb(values[0], values[1], values[2], values[3]);
9309
+ break;
9261
9310
  case "lab":
9262
9311
  values = srgb2labvalues(values[0], values[1], values[2], values[3]);
9263
9312
  break;
@@ -9319,7 +9368,8 @@
9319
9368
  }
9320
9369
  }
9321
9370
  const stack = [];
9322
- const lchSpaces = ["lch", "oklch"];
9371
+ // const lchSpaces: string[] = ["lch", "oklch"];
9372
+ const isLCHOrOKLCH = colorSpace === "lch" || colorSpace == "oklch";
9323
9373
  i = srgbComponentValues.length;
9324
9374
  while (i--) {
9325
9375
  stack.push({
@@ -9337,46 +9387,73 @@
9337
9387
  let premult1;
9338
9388
  let premult2;
9339
9389
  let mixedPremult;
9340
- let colorSpace1 = exports.ColorType[
9341
- // @ts-expect-error
9342
- colorComponents.at(-1).kin
9343
- // @ts-expect-error
9344
- ]?.toLowerCase?.();
9345
- if (colorComponents[0][3] != null &&
9346
- colorComponents[0][3].typ == exports.EnumToken.IdenTokenType &&
9347
- colorComponents[0][3].val == "none" &&
9348
- colorComponents[1].length == 4) {
9390
+ let offset1 = colors[0].kin == exports.ColorType.COLOR ? 1 : 0;
9391
+ if (colorComponents[0][offset1 + 3] != null &&
9392
+ colorComponents[0][offset1 + 3].typ == exports.EnumToken.IdenTokenType &&
9393
+ colorComponents[0][offset1 + 3].val == "none" &&
9394
+ colorComponents[1].length == offset1 + 4) {
9349
9395
  // fix powerless alpha for last color if previous color has alpha
9350
9396
  stack[stack.length - 1].color[3] = stack[stack.length - 2].color[3];
9351
9397
  }
9352
- // powerless hue for lch spaces
9353
- if (lchSpaces.includes(colorSpace1) || lchSpaces.includes(colorSpace)) {
9354
- if ((stack.length > 1 &&
9355
- colorComponents[0][2].typ == exports.EnumToken.IdenTokenType &&
9356
- colorComponents[0][2].val == "none") ||
9357
- stack[stack.length - 1].color[2] == 0) {
9398
+ // console.error(stack);
9399
+ if (stack.length > 1) {
9400
+ // powerless hue for lch spaces
9401
+ // if (lchSpaces.includes(colorSpace)) {
9402
+ // console.error(JSON.stringify({colorSpace1, colorSpace, stack, colorComponents}, null, 1));
9403
+ if ((stack[stack.length - 1].color[0] == 0 && colorComponents[0][offset1].typ == exports.EnumToken.IdenTokenType &&
9404
+ colorComponents[0][offset1].val == "none") ||
9405
+ colorComponents[0][0][STATE] & exports.EnumAstNodeStatus.PowerlessColorComponent) {
9406
+ stack[stack.length - 1].color[0] = stack[stack.length - 2].color[0];
9407
+ }
9408
+ if ((stack[stack.length - 1].color[1] == 0 && colorComponents[0][offset1 + 1].typ == exports.EnumToken.IdenTokenType &&
9409
+ colorComponents[0][offset1 + 1].val == "none") ||
9410
+ colorComponents[0][1][STATE] & exports.EnumAstNodeStatus.PowerlessColorComponent) {
9411
+ stack[stack.length - 1].color[1] = stack[stack.length - 2].color[1];
9412
+ }
9413
+ if ((stack[stack.length - 1].color[2] == 0 && colorComponents[0][offset1 + 2].typ == exports.EnumToken.IdenTokenType &&
9414
+ colorComponents[0][offset1 + 2].val == "none") ||
9415
+ colorComponents[0][2][STATE] & exports.EnumAstNodeStatus.PowerlessColorComponent ||
9416
+ (isLCHOrOKLCH &&
9417
+ (stack[stack.length - 1].color[1] == 0 ||
9418
+ stack[stack.length - 1].color[0] == 0 ||
9419
+ stack[stack.length - 1].color[0] ==
9420
+ colorRange[colors[0].kin == exports.ColorType.LCH ? "lch" : "oklch"].l[1]))) {
9358
9421
  stack[stack.length - 1].color[2] = stack[stack.length - 2].color[2];
9359
9422
  }
9423
+ // }
9360
9424
  }
9425
+ // console.error([colorComponents[0][offset1].typ == EnumToken.IdenTokenType, (colorComponents[0][offset1] as IdentToken).val == "none"]);
9426
+ // console.error(isLCHOrOKLCH, colorComponents, stack);
9361
9427
  while (stack.length > 1) {
9362
9428
  r1 = stack.pop();
9363
9429
  r2 = stack.pop();
9430
+ offset1 = colors[++currentIndex].kin == exports.ColorType.COLOR ? 1 : 0;
9364
9431
  // r2 powerless alpha
9365
- if (colorComponents[++currentIndex][3] != null &&
9366
- colorComponents[currentIndex][3].typ == exports.EnumToken.IdenTokenType &&
9367
- colorComponents[currentIndex][3].val == "none") {
9432
+ if (colorComponents[currentIndex][offset1 + 3] != null &&
9433
+ colorComponents[currentIndex][offset1 + 3].typ == exports.EnumToken.IdenTokenType &&
9434
+ colorComponents[currentIndex][offset1 + 3].val == "none") {
9368
9435
  // fix powerless alpha for last color if previous color has alpha
9369
9436
  r2.color[3] = r1.color[3];
9370
9437
  }
9371
- // @ts-expect-error
9372
- colorSpace1 = exports.ColorType[colorComponents[currentIndex].kin]?.toLowerCase?.();
9373
9438
  // powerless hue for lch spaces
9374
- if (lchSpaces.includes(colorSpace1) || lchSpaces.includes(colorSpace)) {
9375
- if ((colorComponents[currentIndex][2].typ == exports.EnumToken.IdenTokenType &&
9376
- colorComponents[currentIndex][2].val == "none") ||
9377
- r2.color[2] == 0) {
9378
- r2.color[2] = r1.color[2];
9379
- }
9439
+ if ((r2.color[0] == 0 && colorComponents[currentIndex][offset1].typ == exports.EnumToken.IdenTokenType &&
9440
+ colorComponents[currentIndex][offset1].val == "none") ||
9441
+ colorComponents[currentIndex][offset1][STATE] & exports.EnumAstNodeStatus.PowerlessColorComponent) {
9442
+ r2.color[0] = r1.color[0];
9443
+ }
9444
+ if ((r2.color[1] == 0 && colorComponents[currentIndex][offset1 + 1].typ == exports.EnumToken.IdenTokenType &&
9445
+ colorComponents[currentIndex][offset1 + 1].val == "none") ||
9446
+ colorComponents[currentIndex][offset1 + 1][STATE] & exports.EnumAstNodeStatus.PowerlessColorComponent) {
9447
+ r2.color[1] = r1.color[1];
9448
+ }
9449
+ if ((r2.color[2] == 0 && colorComponents[currentIndex][offset1 + 2].typ == exports.EnumToken.IdenTokenType &&
9450
+ colorComponents[currentIndex][offset1 + 2].val == "none") ||
9451
+ colorComponents[currentIndex][offset1 + 2][STATE] & exports.EnumAstNodeStatus.PowerlessColorComponent ||
9452
+ (isLCHOrOKLCH &&
9453
+ (r2.color[1] == 0 ||
9454
+ r2.color[0] == 0 ||
9455
+ r2.color[0] == colorRange[colors[currentIndex].kin == exports.ColorType.LCH ? "lch" : "oklch"].l[1]))) {
9456
+ r2.color[2] = r1.color[2];
9380
9457
  }
9381
9458
  if (hueInterpolationMethod != null) {
9382
9459
  let hueIndex = 2;
@@ -9404,6 +9481,7 @@
9404
9481
  alpha: mult,
9405
9482
  };
9406
9483
  }
9484
+ // console.error({ r1, r2, r, premult1, mult1, premult2, mult2, mixedPremult, mult });
9407
9485
  stack.push(r);
9408
9486
  }
9409
9487
  const result = stack.pop();
@@ -9458,16 +9536,16 @@
9458
9536
  };
9459
9537
  // case "rgb":
9460
9538
  case "hsl":
9461
- // case "hwb":
9539
+ case "hwb":
9462
9540
  case "lab":
9463
9541
  case "lch":
9464
9542
  case "oklab":
9465
9543
  case "oklch":
9466
- if (colorSpace == "hsl") {
9544
+ if (colorSpace == "hsl" || colorSpace == "hwb") {
9467
9545
  // @ts-ignore
9468
9546
  if (values[2] < 0) {
9469
9547
  // @ts-ignore
9470
- values[2] += 1;
9548
+ values[2] += 360;
9471
9549
  }
9472
9550
  }
9473
9551
  else if (colorSpace == "lch" || colorSpace == "oklch") {
@@ -9495,16 +9573,72 @@
9495
9573
  };
9496
9574
  if (colorSpace == "hsl") {
9497
9575
  // @ts-ignore
9498
- result.chi[0] = { typ: exports.EnumToken.AngleTokenType, val: result.chi[0].val * 360 };
9576
+ result.chi[0] = { typ: exports.EnumToken.AngleTokenType, val: result.chi[0].val };
9499
9577
  // @ts-ignore
9500
- result.chi[1] = { typ: exports.EnumToken.PercentageTokenType, val: result.chi[1].val * 100 };
9578
+ result.chi[1] = { typ: exports.EnumToken.PercentageTokenType, val: result.chi[1].val };
9501
9579
  // @ts-ignore
9502
- result.chi[2] = { typ: exports.EnumToken.PercentageTokenType, val: result.chi[2].val * 100 };
9580
+ result.chi[2] = { typ: exports.EnumToken.PercentageTokenType, val: result.chi[2].val };
9503
9581
  }
9504
9582
  return result;
9505
9583
  }
9506
9584
  return null;
9507
9585
  }
9586
+ function processPowerlessComponents(color) {
9587
+ let components;
9588
+ let values;
9589
+ switch (color.kin) {
9590
+ case exports.ColorType.LCH:
9591
+ case exports.ColorType.OKLCH:
9592
+ values = color.kin == exports.ColorType.LCH ? getLCHComponents(color) : getOKLCHComponents(color);
9593
+ components = getColorComponents(color);
9594
+ if (values == null || values.length < 3 || components == null) {
9595
+ break;
9596
+ }
9597
+ const max = color.kin == exports.ColorType.LCH ? colorRange.lch.l[1] : colorRange.oklch.l[1];
9598
+ // https://www.w3.org/TR/css-color-4/#specifying-lab-lch
9599
+ // https://www.w3.org/TR/css-color-4/#specifying-oklab-oklch
9600
+ const epsilon = color.kin == exports.ColorType.LCH ? 0.0015 : 0.000004;
9601
+ if (values[0] < epsilon || Math.abs(values[0] - max) < epsilon) {
9602
+ Object.assign(components[0], { val: values[0] < epsilon ? 0 : max, typ: exports.EnumToken.NumberTokenType });
9603
+ Object.assign(components[1], { val: "none", typ: exports.EnumToken.IdenTokenType });
9604
+ Object.assign(components[2], { val: "none", typ: exports.EnumToken.IdenTokenType });
9605
+ setColorFlag(components[1], exports.EnumAstNodeStatus.PowerlessColorComponent);
9606
+ setColorFlag(components[2], exports.EnumAstNodeStatus.PowerlessColorComponent);
9607
+ }
9608
+ else if (values[1] < epsilon) {
9609
+ values[1] = 0;
9610
+ Object.assign(components[1], { val: 0, typ: exports.EnumToken.NumberTokenType });
9611
+ Object.assign(components[2], { val: "none", typ: exports.EnumToken.IdenTokenType });
9612
+ setColorFlag(components[2], exports.EnumAstNodeStatus.PowerlessColorComponent);
9613
+ }
9614
+ break;
9615
+ case exports.ColorType.HSL:
9616
+ case exports.ColorType.HWB:
9617
+ values = Object.values((color.kin == exports.ColorType.HSL ? hslvalues(color) : hwbvalues(color)) ?? {});
9618
+ components = getColorComponents(color);
9619
+ if (values == null || values.length < 3 || components == null) {
9620
+ break;
9621
+ }
9622
+ // https://www.w3.org/TR/css-color-4/#the-hsl-notation
9623
+ // https://www.w3.org/TR/css-color-4/#the-hwb-notation
9624
+ if ((color.kin == exports.ColorType.HSL && values[1] <= 0.001) ||
9625
+ (color.kin == exports.ColorType.HWB && values[1] + values[2] >= 99.999)) {
9626
+ Object.assign(components[0], { val: "none", typ: exports.EnumToken.IdenTokenType });
9627
+ setColorFlag(components[0], exports.EnumAstNodeStatus.PowerlessColorComponent);
9628
+ }
9629
+ break;
9630
+ }
9631
+ // console.error({ color: color.chi ?? color, values });
9632
+ return color;
9633
+ }
9634
+ function setColorFlag(token, flag) {
9635
+ if (token[STATE] == null) {
9636
+ token[STATE] = flag;
9637
+ }
9638
+ else {
9639
+ token[STATE] |= flag;
9640
+ }
9641
+ }
9508
9642
 
9509
9643
  /**
9510
9644
  * Options for the walk function
@@ -9542,6 +9676,7 @@
9542
9676
  */
9543
9677
  WalkerEvent[WalkerEvent["Leave"] = 2] = "Leave";
9544
9678
  })(exports.WalkerEvent || (exports.WalkerEvent = {}));
9679
+ const maxQueueLength = 25000;
9545
9680
  /**
9546
9681
  * Walk ast nodes
9547
9682
  * @param node initial node
@@ -9645,6 +9780,10 @@
9645
9780
  },
9646
9781
  };
9647
9782
  while ((node = parents[i++])) {
9783
+ if (i > maxQueueLength) {
9784
+ parents.splice(0, maxQueueLength);
9785
+ i -= maxQueueLength;
9786
+ }
9648
9787
  let option = null;
9649
9788
  if (filter != null) {
9650
9789
  // @ts-expect-error
@@ -9788,6 +9927,10 @@
9788
9927
  while (++i < stack.length) {
9789
9928
  value = stack[i];
9790
9929
  option = null;
9930
+ if (i > maxQueueLength) {
9931
+ stack.splice(0, maxQueueLength);
9932
+ i -= maxQueueLength;
9933
+ }
9791
9934
  if (filter.fn != null && eventType & exports.WalkerEvent.Enter) {
9792
9935
  const isValid = filter.type == null ||
9793
9936
  value.typ == filter.type ||
@@ -10027,6 +10170,7 @@
10027
10170
  return g > 1 ? [a / g, b / g] : [a, b];
10028
10171
  }
10029
10172
 
10173
+ const numbers = new Set(["sin", "cos", "asin", "acos", "sign", "tan"]);
10030
10174
  /**
10031
10175
  * evaluate an array of tokens
10032
10176
  * @param tokens
@@ -10216,9 +10360,22 @@
10216
10360
  r.typ == exports.EnumToken.NegativeInfinityTokenType) {
10217
10361
  return computeInfinity(l, r, op, defaultReturn);
10218
10362
  }
10363
+ if (l.typ == exports.EnumToken.FunctionTokenType || l.typ == exports.EnumToken.MathFunctionTokenType) {
10364
+ const val = evaluateFunc(l);
10365
+ if (val == null ||
10366
+ (val.length == 1 &&
10367
+ (val[0].typ == exports.EnumToken.MathFunctionTokenType || val[0].typ == exports.EnumToken.FunctionTokenType))) {
10368
+ return defaultReturn;
10369
+ }
10370
+ if (val.length == 1) {
10371
+ l = val[0];
10372
+ }
10373
+ }
10219
10374
  if (r.typ == exports.EnumToken.FunctionTokenType || r.typ == exports.EnumToken.MathFunctionTokenType) {
10220
10375
  const val = evaluateFunc(r);
10221
- if (val == null) {
10376
+ if (val == null ||
10377
+ (val.length == 1 &&
10378
+ (val[0].typ == exports.EnumToken.MathFunctionTokenType || val[0].typ == exports.EnumToken.FunctionTokenType))) {
10222
10379
  return defaultReturn;
10223
10380
  }
10224
10381
  if (val.length == 1) {
@@ -10436,45 +10593,43 @@
10436
10593
  function evaluateFunc(token) {
10437
10594
  const values = token.chi.slice();
10438
10595
  switch (token.val) {
10439
- case "abs":
10440
10596
  case "sin":
10441
10597
  case "cos":
10442
10598
  case "tan":
10599
+ for (let i = 0; i < values.length; i++) {
10600
+ if (values[i].typ == exports.EnumToken.NumberTokenType) {
10601
+ values[i] = Object.assign(values[i], { typ: exports.EnumToken.AngleTokenType, unit: "rad" });
10602
+ }
10603
+ else if (values[i].typ == exports.EnumToken.AngleTokenType && values[i].unit != "rad") {
10604
+ switch (values[i].unit) {
10605
+ case "deg":
10606
+ Object.assign(values[i], {
10607
+ unit: "rad",
10608
+ val: values[i].val * (Math.PI / 180),
10609
+ });
10610
+ break;
10611
+ case "grad":
10612
+ Object.assign(values[i], {
10613
+ unit: "rad",
10614
+ val: values[i].val * (Math.PI / 200),
10615
+ });
10616
+ break;
10617
+ case "turn":
10618
+ Object.assign(values[i], {
10619
+ unit: "rad",
10620
+ val: values[i].val * (2 * Math.PI),
10621
+ });
10622
+ break;
10623
+ }
10624
+ }
10625
+ }
10443
10626
  case "asin":
10444
10627
  case "acos":
10445
10628
  case "atan":
10629
+ case "abs":
10446
10630
  case "sign":
10447
10631
  case "sqrt":
10448
- case "exp": {
10449
- if (token.val == "tan" || token.val == "atan") {
10450
- for (let i = 0; i < values.length; i++) {
10451
- if (values[i].typ == exports.EnumToken.NumberTokenType) {
10452
- values[i] = Object.assign(values[i], { typ: exports.EnumToken.AngleTokenType, unit: "rad" });
10453
- }
10454
- else if (values[i].typ == exports.EnumToken.AngleTokenType && values[i].unit != "rad") {
10455
- switch (values[i].unit) {
10456
- case "deg":
10457
- Object.assign(values[i], {
10458
- unit: "rad",
10459
- val: values[i].val * (Math.PI / 180),
10460
- });
10461
- break;
10462
- case "grad":
10463
- Object.assign(values[i], {
10464
- unit: "rad",
10465
- val: values[i].val * (Math.PI / 200),
10466
- });
10467
- break;
10468
- case "turn":
10469
- Object.assign(values[i], {
10470
- unit: "rad",
10471
- val: values[i].val * (2 * Math.PI),
10472
- });
10473
- break;
10474
- }
10475
- }
10476
- }
10477
- }
10632
+ case "exp":
10478
10633
  const value = evaluate(values);
10479
10634
  // @ts-ignore
10480
10635
  let val = value[0].typ == exports.EnumToken.NumberTokenType ||
@@ -10484,7 +10639,7 @@
10484
10639
  : // @ts-expect-error
10485
10640
  value[0].l.val / value[0].r.val;
10486
10641
  return [
10487
- token.val == "tan" || token.val == "atan"
10642
+ token.val == "asin" || token.val == "acos" || token.val == "atan"
10488
10643
  ? {
10489
10644
  typ: exports.EnumToken.AngleTokenType,
10490
10645
  val: Math[token.val](val),
@@ -10495,7 +10650,7 @@
10495
10650
  [PARENT]: value[0][PARENT],
10496
10651
  }
10497
10652
  : {
10498
- typ: token.val == "sign" ? exports.EnumToken.NumberTokenType : value[0].typ,
10653
+ typ: numbers.has(token.val) ? exports.EnumToken.NumberTokenType : value[0].typ,
10499
10654
  val: Math[token.val](val),
10500
10655
  [LOCSRCID]: value[0][LOCSRCID],
10501
10656
  [LOCSTA]: value[0][LOCSTA],
@@ -10503,7 +10658,6 @@
10503
10658
  [PARENT]: value[0][PARENT],
10504
10659
  },
10505
10660
  ];
10506
- }
10507
10661
  case "hypot": {
10508
10662
  const chi = values.filter((t) => ![exports.EnumToken.WhitespaceTokenType, exports.EnumToken.CommentTokenType, exports.EnumToken.CommaTokenType].includes(t.typ));
10509
10663
  let all = [];
@@ -11009,7 +11163,36 @@
11009
11163
  : aExp),
11010
11164
  };
11011
11165
  const result = computeComponentValue(keys, values);
11166
+ if (result != null) {
11167
+ if ("hsl" == relativeKeys) {
11168
+ Object.assign(result.s, {
11169
+ typ: exports.EnumToken.PercentageTokenType,
11170
+ // @ts-ignore
11171
+ val: result.s.val * 100,
11172
+ });
11173
+ Object.assign(result.l, {
11174
+ typ: exports.EnumToken.PercentageTokenType,
11175
+ // @ts-ignore
11176
+ val: result.l.val * 100,
11177
+ });
11178
+ }
11179
+ else if ("hwb" == relativeKeys) {
11180
+ // @ts
11181
+ Object.assign(result.w, {
11182
+ typ: exports.EnumToken.PercentageTokenType,
11183
+ // @ts-ignore
11184
+ val: result.w.val * 100,
11185
+ });
11186
+ Object.assign(result.b, {
11187
+ typ: exports.EnumToken.PercentageTokenType,
11188
+ // @ts-ignore
11189
+ val: result.b.val * 100,
11190
+ });
11191
+ }
11192
+ }
11193
+ // @ts-ignore
11012
11194
  if (result?.alpha?.typ == exports.EnumToken.NumberTokenType && result.alpha.val === 1) {
11195
+ // @ts-ignore
11013
11196
  const { alpha, ...components } = result;
11014
11197
  return components;
11015
11198
  }
@@ -12258,7 +12441,8 @@
12258
12441
  return { valid: true, success: false, isValueAllowed: false };
12259
12442
  }
12260
12443
  featureName = featureName.toLowerCase();
12261
- if (!(featureName in config$3.mediaFeatures)) {
12444
+ // @ts-expect-error
12445
+ if (config$3.mediaFeatures[featureName] == null) {
12262
12446
  return { valid: false, success: false };
12263
12447
  }
12264
12448
  // @ts-expect-error
@@ -12302,8 +12486,7 @@
12302
12486
  return {
12303
12487
  valid: true,
12304
12488
  success: (tokens.length == 1 &&
12305
- tokens[0].typ == exports.EnumToken.NumberTokenType &&
12306
- tokens[0].val.typ == exports.EnumToken.FractionTokenType) ||
12489
+ tokens[0].typ == exports.EnumToken.NumberTokenType) ||
12307
12490
  (tokens.length === 3 &&
12308
12491
  tokens[0].typ == exports.EnumToken.NumberTokenType &&
12309
12492
  typeof tokens[0].val === "number" &&
@@ -15151,7 +15334,6 @@
15151
15334
  return +token.val;
15152
15335
  }
15153
15336
  }
15154
- // @ts-ignore
15155
15337
  return token.val / 360;
15156
15338
  }
15157
15339
 
@@ -17009,9 +17191,7 @@
17009
17191
  }
17010
17192
  }
17011
17193
  run(node) {
17012
- if (node[STATE] == exports.EnumAstNodeStatus.Disallowed ||
17013
- node[STATE] == exports.EnumAstNodeStatus.Invalid ||
17014
- node[STATE] == exports.EnumAstNodeStatus.Malformed) {
17194
+ if (node[STATE] & (exports.EnumAstNodeStatus.Disallowed | exports.EnumAstNodeStatus.Invalid | exports.EnumAstNodeStatus.Malformed)) {
17015
17195
  return null;
17016
17196
  }
17017
17197
  if (node.typ == exports.EnumToken.RuleNodeType) {
@@ -19726,7 +19906,8 @@
19726
19906
  const tokens = {};
19727
19907
  const values = [];
19728
19908
  const val = [[]];
19729
- for (const curr of this.declarations.get(this.config.shorthand).val) { // @ts-ignore
19909
+ for (const curr of this.declarations.get(this.config.shorthand).val) {
19910
+ // @ts-ignore
19730
19911
  // @ts-ignore
19731
19912
  if (separator != null && separator.typ == curr.typ && separator.val == curr.val) {
19732
19913
  val.push([]);
@@ -20670,9 +20851,8 @@
20670
20851
  let result;
20671
20852
  for (const declaration of declarations) {
20672
20853
  name = declaration.typ != exports.EnumToken.DeclarationNodeType ? null : declaration.nam;
20673
- if (declaration[STATE] == exports.EnumAstNodeStatus.Invalid ||
20674
- declaration[STATE] == exports.EnumAstNodeStatus.Unknown ||
20675
- declaration[STATE] == exports.EnumAstNodeStatus.ValidationFailed ||
20854
+ if (declaration[STATE] &
20855
+ (exports.EnumAstNodeStatus.Invalid | exports.EnumAstNodeStatus.Unknown | exports.EnumAstNodeStatus.ValidationFailed) ||
20676
20856
  declaration.typ != exports.EnumToken.DeclarationNodeType ||
20677
20857
  equalsIgnoreCase("composes", name) ||
20678
20858
  (typeof this.options.removeDuplicateDeclarations === "string" &&
@@ -20691,17 +20871,17 @@
20691
20871
  this.declarations.set(declaration.nam, declaration);
20692
20872
  continue;
20693
20873
  }
20694
- if (declaration[STATE] == exports.EnumAstNodeStatus.Unvalidated) {
20874
+ if (declaration[STATE] & exports.EnumAstNodeStatus.Unvalidated) {
20695
20875
  syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, declaration.nam.toLowerCase());
20696
20876
  if (syntaxRules != null) {
20697
20877
  result = matchAllSyntaxes(syntaxRules, createValidationContext(declaration.val), this.options);
20698
- declaration[STATE] = result.success
20699
- ? exports.EnumAstNodeStatus.Validated
20700
- : exports.EnumAstNodeStatus.ValidationFailed;
20878
+ declaration[STATE] |=
20879
+ (result.success ? exports.EnumAstNodeStatus.Validated : exports.EnumAstNodeStatus.ValidationFailed) &
20880
+ ~exports.EnumAstNodeStatus.Unvalidated;
20701
20881
  }
20702
20882
  }
20703
20883
  // do not compute shorthand for invalid declarations
20704
- if (declaration[STATE] !== exports.EnumAstNodeStatus.Validated) {
20884
+ if (!(declaration[STATE] & exports.EnumAstNodeStatus.Validated)) {
20705
20885
  this.declarations.set(objectHash(declaration), declaration);
20706
20886
  continue;
20707
20887
  }
@@ -20731,6 +20911,16 @@
20731
20911
  // @ts-ignore
20732
20912
  shorthand = config.property[propertyName];
20733
20913
  }
20914
+ if (this.declarations.has(shorthand)) {
20915
+ let existing = this.declarations.get(shorthand);
20916
+ if (existing instanceof PropertySet || existing instanceof PropertyMap) {
20917
+ existing = existing.declarations.get(declaration.nam);
20918
+ }
20919
+ if (existing?.[STATE] & exports.EnumAstNodeStatus.ImportantDeclaration &&
20920
+ declaration[STATE] & ~exports.EnumAstNodeStatus.ImportantDeclaration) {
20921
+ continue;
20922
+ }
20923
+ }
20734
20924
  // @ts-ignore
20735
20925
  if (shortHandType == "map") {
20736
20926
  let owner = this;
@@ -21047,7 +21237,8 @@
21047
21237
  }
21048
21238
  }
21049
21239
  run(ast) {
21050
- if (!("chi" in ast)) {
21240
+ // @ts-ignore
21241
+ if (ast.chi == null) {
21051
21242
  return null;
21052
21243
  }
21053
21244
  for (const node of ast.chi) {
@@ -21065,15 +21256,7 @@
21065
21256
  // @ts-ignore
21066
21257
  replaceNodeOrValue(parent, value, result);
21067
21258
  }
21068
- catch (e) {
21069
- // @ts-ignore
21070
- if (Array.isArray(parent.chi)) {
21071
- // @ts-ignore
21072
- parent.chi.length = 0;
21073
- // @ts-ignore
21074
- parent.chi.push(...result);
21075
- }
21076
- }
21259
+ catch (e) { }
21077
21260
  continue;
21078
21261
  }
21079
21262
  if (value != null && tokensfuncSet.has(value.typ)) {
@@ -22379,7 +22562,7 @@
22379
22562
  for (; i < ast.chi.length; i++) {
22380
22563
  // @ts-ignore
22381
22564
  node = ast.chi[i];
22382
- if (node[STATE] == exports.EnumAstNodeStatus.Invalid || node[STATE] == exports.EnumAstNodeStatus.ValidationFailed) {
22565
+ if (node[STATE] & (exports.EnumAstNodeStatus.Invalid | exports.EnumAstNodeStatus.ValidationFailed)) {
22383
22566
  continue;
22384
22567
  }
22385
22568
  if (node.typ != exports.EnumToken.DeclarationNodeType ||
@@ -24322,11 +24505,12 @@
24322
24505
  * @private
24323
24506
  */
24324
24507
  function expand(ast) {
24325
- if (ast[STATE] == exports.EnumAstNodeStatus.Invalid ||
24326
- ast[STATE] == exports.EnumAstNodeStatus.Disallowed ||
24327
- ast[STATE] == exports.EnumAstNodeStatus.Unknown ||
24328
- ast[STATE] == exports.EnumAstNodeStatus.Unparsed ||
24329
- ast[STATE] == exports.EnumAstNodeStatus.Malformed) {
24508
+ if (ast[STATE] &
24509
+ (exports.EnumAstNodeStatus.Invalid |
24510
+ exports.EnumAstNodeStatus.Disallowed |
24511
+ exports.EnumAstNodeStatus.Unknown |
24512
+ exports.EnumAstNodeStatus.Unparsed |
24513
+ exports.EnumAstNodeStatus.Malformed)) {
24330
24514
  return ast;
24331
24515
  }
24332
24516
  const result = Object.assign(cloneNode(ast), { chi: [] });
@@ -24374,11 +24558,12 @@
24374
24558
  return result;
24375
24559
  }
24376
24560
  function expandRule(node) {
24377
- if (node[STATE] == exports.EnumAstNodeStatus.Invalid ||
24378
- node[STATE] == exports.EnumAstNodeStatus.Disallowed ||
24379
- node[STATE] == exports.EnumAstNodeStatus.Unknown ||
24380
- node[STATE] == exports.EnumAstNodeStatus.Unparsed ||
24381
- node[STATE] == exports.EnumAstNodeStatus.Malformed) {
24561
+ if (node[STATE] &
24562
+ (exports.EnumAstNodeStatus.Invalid |
24563
+ exports.EnumAstNodeStatus.Disallowed |
24564
+ exports.EnumAstNodeStatus.Unknown |
24565
+ exports.EnumAstNodeStatus.Unparsed |
24566
+ exports.EnumAstNodeStatus.Malformed)) {
24382
24567
  return [node];
24383
24568
  }
24384
24569
  const ast = Object.assign(cloneNode(node), { chi: node.chi.slice() });
@@ -26638,17 +26823,20 @@
26638
26823
  ? ""
26639
26824
  : token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "") +
26640
26825
  ":" +
26826
+ (options.indent ?? "") +
26641
26827
  token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "");
26642
26828
  case exports.EnumToken.IfElseConditionTokenType:
26643
- return renderValue(token.l) + renderValue(token.r);
26829
+ return (renderValue(token.l, options, cache, reducer) +
26830
+ (options.indent ?? "") +
26831
+ renderValue(token.r, options, cache, reducer));
26644
26832
  case exports.EnumToken.DeclarationNodeType:
26645
26833
  return (token.nam +
26646
26834
  ":" +
26647
- (options.minifyValues ? filterValues(token.val) : token.val).reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
26835
+ (options.minifyValues ? filterValues(token.val) : token.val).reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), ""));
26648
26836
  case exports.EnumToken.MediaQueryUnaryFeatureTokenType:
26649
26837
  return (renderValue(token.l, options, cache, reducer) +
26650
26838
  " " +
26651
- token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
26839
+ token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), ""));
26652
26840
  case exports.EnumToken.MediaQueryConditionTokenType: {
26653
26841
  const indent = token.op.typ == exports.EnumToken.LtTokenType ||
26654
26842
  token.op.typ == exports.EnumToken.GtTokenType ||
@@ -26662,14 +26850,14 @@
26662
26850
  indent +
26663
26851
  renderValue(token.op, options, cache, reducer) +
26664
26852
  indent +
26665
- token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
26853
+ token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), ""));
26666
26854
  }
26667
26855
  case exports.EnumToken.MediaRangeQueryTokenType:
26668
- return (token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache), "") +
26669
- renderValue(token.op1) +
26856
+ return (token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "") +
26857
+ renderValue(token.op1, options, cache, reducer) +
26670
26858
  token.val.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "") +
26671
- renderValue(token.op2) +
26672
- token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
26859
+ renderValue(token.op2, options, cache, reducer) +
26860
+ token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), ""));
26673
26861
  case exports.EnumToken.MediaFeatureTokenType:
26674
26862
  return token.val;
26675
26863
  case exports.EnumToken.NotTokenType:
@@ -28873,6 +29061,7 @@
28873
29061
  let rules = null;
28874
29062
  let syntaxRules = null;
28875
29063
  let success = true;
29064
+ let hasImportantFlag = false;
28876
29065
  let validate = typeof options.validation === "boolean" ? options.validation : !!options.validation;
28877
29066
  if (renamedStandardProperties.has(name.val)) {
28878
29067
  name.val = renamedStandardProperties.get(name.val);
@@ -29174,6 +29363,9 @@
29174
29363
  }
29175
29364
  }
29176
29365
  break;
29366
+ case exports.EnumToken.ImportantTokenType:
29367
+ hasImportantFlag = true;
29368
+ break;
29177
29369
  }
29178
29370
  if (trimTokenSpace$1.has(token.typ)) {
29179
29371
  if (tokens[i + 1]?.typ === exports.EnumToken.WhitespaceTokenType) {
@@ -29240,6 +29432,9 @@
29240
29432
  }
29241
29433
  name[STATE] = exports.EnumAstNodeStatus.Unknown;
29242
29434
  name[ERRORS] = result?.errors ?? [];
29435
+ if (hasImportantFlag) {
29436
+ name[STATE] |= exports.EnumAstNodeStatus.ImportantDeclaration;
29437
+ }
29243
29438
  // @ts-expect-error
29244
29439
  return Object.assign(name, {
29245
29440
  typ: exports.EnumToken.DeclarationNodeType,
@@ -29277,6 +29472,9 @@
29277
29472
  : exports.EnumAstNodeStatus.Validated
29278
29473
  : exports.EnumAstNodeStatus.ValidationFailed;
29279
29474
  name[ERRORS] = result?.errors ?? [];
29475
+ if (hasImportantFlag) {
29476
+ name[STATE] |= exports.EnumAstNodeStatus.ImportantDeclaration;
29477
+ }
29280
29478
  // @ts-expect-error
29281
29479
  return Object.assign(name, {
29282
29480
  typ: exports.EnumToken.DeclarationNodeType,
@@ -29468,8 +29666,6 @@
29468
29666
  }
29469
29667
  }
29470
29668
  }
29471
- // let isValidMFValue = isMFValue(name, left, true);
29472
- // isValidMFValue = isMFValue(name, right, true);
29473
29669
  for (const val of [left, right]) {
29474
29670
  if (mfInfo?.type === MediaFeatureType.RatioType) {
29475
29671
  const filteredValues = val.filter((n) => n.typ !== exports.EnumToken.WhitespaceTokenType &&
@@ -30265,7 +30461,7 @@
30265
30461
  };
30266
30462
  }
30267
30463
  {
30268
- for (const stream of parts.slice()) {
30464
+ for (const stream of parts) {
30269
30465
  let success = true;
30270
30466
  let i;
30271
30467
  let currentScope = new Set();
@@ -30384,6 +30580,22 @@
30384
30580
  [LOCSTA]: names[0][LOCSTA],
30385
30581
  [LOCEND]: values.at(-1)[LOCEND],
30386
30582
  });
30583
+ const token = tokens[index3 + 1];
30584
+ if (token.op.typ === exports.EnumToken.ColonTokenType) {
30585
+ let name = token.l.find((t) => t.typ === exports.EnumToken.IdenTokenType);
30586
+ if (name != null) {
30587
+ if (name.val.startsWith("min-")) {
30588
+ name.val = name.val.substring(4);
30589
+ // @ts-ignore
30590
+ token.op.typ = exports.EnumToken.GteTokenType;
30591
+ }
30592
+ else if (name.val.startsWith("max-")) {
30593
+ name.val = name.val.substring(4);
30594
+ // @ts-ignore
30595
+ token.op.typ = exports.EnumToken.LteTokenType;
30596
+ }
30597
+ }
30598
+ }
30387
30599
  // check <style()> or <scroll-state()>
30388
30600
  }
30389
30601
  if (tokensfuncDefMap.has(stack.at(-1)?.typ)) {
@@ -30475,6 +30687,58 @@
30475
30687
  };
30476
30688
  tokens.length = l + 1;
30477
30689
  expectAndOr = true;
30690
+ if (tokens[l].op.typ === exports.EnumToken.AndTokenType) {
30691
+ let left = tokens[l].l.find((t) => t.typ != exports.EnumToken.WhitespaceTokenType && t.typ != exports.EnumToken.IdenTokenType);
30692
+ let right = tokens[l].r.find((t) => t.typ != exports.EnumToken.WhitespaceTokenType && t.typ != exports.EnumToken.IdenTokenType);
30693
+ if (left.typ == exports.EnumToken.ParensTokenType && right.typ == exports.EnumToken.ParensTokenType) {
30694
+ left = left.chi.find((t) => t.typ != exports.EnumToken.WhitespaceTokenType && t.typ != exports.EnumToken.IdenTokenType);
30695
+ right = right.chi.find((t) => t.typ != exports.EnumToken.WhitespaceTokenType && t.typ != exports.EnumToken.IdenTokenType);
30696
+ if (left.typ == exports.EnumToken.MediaQueryConditionTokenType &&
30697
+ (mFLT.has(left.op.typ) ||
30698
+ mFGT.has(left.op.typ)) &&
30699
+ right.typ == exports.EnumToken.MediaQueryConditionTokenType &&
30700
+ (mFLT.has(right.op.typ) ||
30701
+ mFGT.has(right.op.typ))) {
30702
+ const name = left.l.find((t) => t.typ == exports.EnumToken.IdenTokenType);
30703
+ const name2 = right.l.find((t) => t.typ == exports.EnumToken.IdenTokenType);
30704
+ if (equalsIgnoreCase(name.val, name2.val)) {
30705
+ switch (left.op.typ) {
30706
+ case exports.EnumToken.LtTokenType:
30707
+ left.op.typ = exports.EnumToken.GtTokenType;
30708
+ break;
30709
+ case exports.EnumToken.LteTokenType:
30710
+ left.op.typ = exports.EnumToken.GteTokenType;
30711
+ break;
30712
+ case exports.EnumToken.GtTokenType:
30713
+ left.op.typ = exports.EnumToken.LtTokenType;
30714
+ break;
30715
+ case exports.EnumToken.GteTokenType:
30716
+ left.op.typ = exports.EnumToken.LteTokenType;
30717
+ break;
30718
+ }
30719
+ tokens[l] = {
30720
+ typ: exports.EnumToken.ParensTokenType,
30721
+ chi: [
30722
+ {
30723
+ typ: exports.EnumToken.MediaRangeQueryTokenType,
30724
+ l: left.r,
30725
+ val: [name],
30726
+ op1: left.op,
30727
+ op2: right.op,
30728
+ r: right.r,
30729
+ [LOCSRCID]: name[LOCSRCID],
30730
+ [LOCSTA]: left[LOCSTA],
30731
+ [LOCEND]: right[LOCEND],
30732
+ },
30733
+ ],
30734
+ [LOCSRCID]: name[LOCSRCID],
30735
+ [LOCSTA]: left[LOCSTA],
30736
+ [LOCEND]: right[LOCEND],
30737
+ };
30738
+ }
30739
+ }
30740
+ }
30741
+ }
30478
30742
  }
30479
30743
  break;
30480
30744
  }
@@ -31108,7 +31372,6 @@
31108
31372
  // let currentItemIndex: number;
31109
31373
  ast[LOCSRCID] = options.source.id;
31110
31374
  ast[LOCSTA] = 0;
31111
- // let tokenizer: Tokenizer;
31112
31375
  while (!tokenizer.done()) {
31113
31376
  tokenizer.next();
31114
31377
  // item = (iter as Array<TokenizeResult>)[currentItemIndex];
@@ -31439,14 +31702,14 @@
31439
31702
  if (invalidNodes.length > 0) {
31440
31703
  let count = invalidNodes.length;
31441
31704
  for (const { node, parent } of walk(ast)) {
31442
- if (options.lenient && node[STATE] == exports.EnumAstNodeStatus.Unknown) {
31705
+ if (options.lenient && node[STATE] & exports.EnumAstNodeStatus.Unknown) {
31443
31706
  continue;
31444
31707
  }
31445
- if (node[STATE] === exports.EnumAstNodeStatus.Invalid ||
31446
- node[STATE] === exports.EnumAstNodeStatus.Unknown ||
31447
- node[STATE] === exports.EnumAstNodeStatus.Unparsed ||
31448
- node[STATE] === exports.EnumAstNodeStatus.Malformed ||
31449
- node[STATE] === exports.EnumAstNodeStatus.Disallowed) {
31708
+ if (node[STATE] & exports.EnumAstNodeStatus.Invalid ||
31709
+ node[STATE] & exports.EnumAstNodeStatus.Unknown ||
31710
+ node[STATE] & exports.EnumAstNodeStatus.Unparsed ||
31711
+ node[STATE] & exports.EnumAstNodeStatus.Malformed ||
31712
+ node[STATE] & exports.EnumAstNodeStatus.Disallowed) {
31450
31713
  // @ts-ignore
31451
31714
  parent.chi.splice(parent.chi.indexOf(node), 1);
31452
31715
  node[PARENT] = null;
@@ -32434,14 +32697,14 @@
32434
32697
  if (invalidNodes.length > 0) {
32435
32698
  let count = invalidNodes.length;
32436
32699
  for (const { node, parent } of walk(ast)) {
32437
- if (options.lenient && node[STATE] == exports.EnumAstNodeStatus.Unknown) {
32700
+ if (options.lenient && node[STATE] & exports.EnumAstNodeStatus.Unknown) {
32438
32701
  continue;
32439
32702
  }
32440
- if (node[STATE] === exports.EnumAstNodeStatus.Invalid ||
32441
- node[STATE] === exports.EnumAstNodeStatus.Unknown ||
32442
- node[STATE] === exports.EnumAstNodeStatus.Unparsed ||
32443
- node[STATE] === exports.EnumAstNodeStatus.Malformed ||
32444
- node[STATE] === exports.EnumAstNodeStatus.Disallowed) {
32703
+ if (node[STATE] & exports.EnumAstNodeStatus.Invalid ||
32704
+ node[STATE] & exports.EnumAstNodeStatus.Unknown ||
32705
+ node[STATE] & exports.EnumAstNodeStatus.Unparsed ||
32706
+ node[STATE] & exports.EnumAstNodeStatus.Malformed ||
32707
+ node[STATE] & exports.EnumAstNodeStatus.Disallowed) {
32445
32708
  // @ts-ignore
32446
32709
  parent.chi.splice(parent.chi.indexOf(node), 1);
32447
32710
  node[PARENT] = null;
@@ -33172,11 +33435,12 @@
33172
33435
  if (node == null) {
33173
33436
  return null;
33174
33437
  }
33175
- if (node[STATE] == exports.EnumAstNodeStatus.Invalid ||
33176
- node[STATE] == exports.EnumAstNodeStatus.Disallowed ||
33177
- node[STATE] == exports.EnumAstNodeStatus.Unknown ||
33178
- node[STATE] == exports.EnumAstNodeStatus.Unparsed ||
33179
- node[STATE] == exports.EnumAstNodeStatus.Malformed) {
33438
+ if (node[STATE] &
33439
+ (exports.EnumAstNodeStatus.Invalid |
33440
+ exports.EnumAstNodeStatus.Disallowed |
33441
+ exports.EnumAstNodeStatus.Unknown |
33442
+ exports.EnumAstNodeStatus.Unparsed |
33443
+ exports.EnumAstNodeStatus.Malformed)) {
33180
33444
  invalidNodes.push(node);
33181
33445
  }
33182
33446
  stats.nodesCount++;
@@ -33194,11 +33458,11 @@
33194
33458
  context.chi.push(node);
33195
33459
  node[PARENT] = context;
33196
33460
  node[ROOT] = context[ROOT];
33197
- if (node[STATE] == exports.EnumAstNodeStatus.Invalid ||
33198
- node[STATE] == exports.EnumAstNodeStatus.Disallowed ||
33199
- node[STATE] == exports.EnumAstNodeStatus.Unknown ||
33200
- node[STATE] == exports.EnumAstNodeStatus.Unparsed ||
33201
- node[STATE] == exports.EnumAstNodeStatus.Malformed) {
33461
+ if (node[STATE] & exports.EnumAstNodeStatus.Invalid ||
33462
+ node[STATE] & exports.EnumAstNodeStatus.Disallowed ||
33463
+ node[STATE] & exports.EnumAstNodeStatus.Unknown ||
33464
+ node[STATE] & exports.EnumAstNodeStatus.Unparsed ||
33465
+ node[STATE] & exports.EnumAstNodeStatus.Malformed) {
33202
33466
  invalidNodes.push(node);
33203
33467
  }
33204
33468
  return node;
@@ -33208,7 +33472,9 @@
33208
33472
  node[PARENT] = context;
33209
33473
  node[ROOT] = context[ROOT];
33210
33474
  if (context.typ === exports.EnumToken.StyleSheetNodeType && node.typ === exports.EnumToken.DeclarationNodeType) {
33211
- node[STATE] = exports.EnumAstNodeStatus.Invalid;
33475
+ node[STATE] |=
33476
+ exports.EnumAstNodeStatus.Invalid &
33477
+ ~(exports.EnumAstNodeStatus.Validated | exports.EnumAstNodeStatus.Unvalidated | exports.EnumAstNodeStatus.Unparsed);
33212
33478
  errors.push({
33213
33479
  message: "<declaration> not allowed in <stylesheet>",
33214
33480
  action: "drop",
@@ -33219,11 +33485,12 @@
33219
33485
  else if (options.lenient || node.typ === exports.EnumToken.DeclarationNodeType) {
33220
33486
  context.chi.push(node);
33221
33487
  }
33222
- if (node[STATE] == exports.EnumAstNodeStatus.Invalid ||
33223
- node[STATE] == exports.EnumAstNodeStatus.Disallowed ||
33224
- node[STATE] == exports.EnumAstNodeStatus.Unknown ||
33225
- node[STATE] == exports.EnumAstNodeStatus.Unparsed ||
33226
- node[STATE] == exports.EnumAstNodeStatus.Malformed) {
33488
+ if (node[STATE] &
33489
+ (exports.EnumAstNodeStatus.Invalid |
33490
+ exports.EnumAstNodeStatus.Disallowed |
33491
+ exports.EnumAstNodeStatus.Unknown |
33492
+ exports.EnumAstNodeStatus.Unparsed |
33493
+ exports.EnumAstNodeStatus.Malformed)) {
33227
33494
  invalidNodes.push(node);
33228
33495
  }
33229
33496
  }