@bamboocss/token-dictionary 1.60.2 → 1.60.3

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/dist/index.cjs CHANGED
@@ -71,9 +71,29 @@ const opacityPercent = (value) => `${Number((Number(value) * 100).toPrecision(12
71
71
  *
72
72
  * The comma exclusion keeps a fallback form — `token(spacing.4, 4)` — out of this pass
73
73
  * deliberately: only `expandTokenReferences` can resolve one, and matching it here would report a
74
- * reference where none is substitutable.
74
+ * reference where none is substitutable. One level of parentheses is let in, for a literal colour
75
+ * given an opacity: `token(rgb(0 0 0)/64)` was left in the css as written.
75
76
  */
76
- const REFERENCE_REGEX = /token\(([^(),]+)\)/g;
77
+ const REFERENCE_REGEX = /token\(((?:[^(),]|\([^()]*\))+)\)/g;
78
+ /**
79
+ * Where the opacity of a `token(colour/opacity)` path begins: its last `/` outside parentheses, or
80
+ * -1 for none. A slash inside them is the colour's own, `rgb(0 0 0 / 50%)`.
81
+ */
82
+ const opacitySlash = (path) => {
83
+ let depth = 0;
84
+ for (let index = path.length - 1; index >= 0; index--) {
85
+ const character = path[index];
86
+ if (character === ")") depth++;
87
+ else if (character === "(") depth--;
88
+ else if (character === "/" && depth === 0) return index;
89
+ }
90
+ return -1;
91
+ };
92
+ /**
93
+ * Whether a `token()` path is a colour written out rather than a token: `#000`, `rgb(0 0 0)`. It
94
+ * stands for itself, and given an opacity is mixed like a token's colour, `token(#000/64)`.
95
+ */
96
+ const isLiteralColor = (path) => path.startsWith("#") || path.includes("(");
77
97
  /**
78
98
  * How a reference to `key` is spelled, for a caller building one rather than reading one.
79
99
  *
@@ -672,7 +692,7 @@ const transforms = [
672
692
  transform(token, dict) {
673
693
  if (!token.value.includes("/")) return token.value;
674
694
  return expandReferences(token.value, (path) => {
675
- if (!path.includes("/")) return referenceOf(path);
695
+ if (opacitySlash(path) === -1) return referenceOf(path);
676
696
  const tokenFn = (tokenPath) => {
677
697
  return dict.getByName(tokenPath)?.extensions.varRef;
678
698
  };
@@ -689,8 +709,8 @@ const transforms = [
689
709
  const { prefix, hash } = dictionary;
690
710
  const refs = getReferences(token.value);
691
711
  if (!refs.length) return token.value;
692
- if (!refs.some((ref) => ref.includes("/"))) refs.forEach((ref) => {
693
- const variable = dictionary.formatCssVar(ref.split("."), {
712
+ if (!refs.some((ref) => opacitySlash(ref) !== -1)) refs.forEach((ref) => {
713
+ const variable = isLiteralColor(ref) ? ref : dictionary.formatCssVar(ref.split("."), {
694
714
  prefix,
695
715
  hash
696
716
  }).ref;
@@ -701,7 +721,7 @@ const transforms = [
701
721
  return dictionary.getByName(name)?.extensions.varRef;
702
722
  };
703
723
  token.value = expandReferences(token.value, (path) => {
704
- if (!path.includes("/")) return dictionary.formatCssVar(path.split("."), {
724
+ if (opacitySlash(path) === -1) return isLiteralColor(path) ? path : dictionary.formatCssVar(path.split("."), {
705
725
  prefix,
706
726
  hash
707
727
  }).ref;
@@ -820,6 +840,8 @@ const transforms = [
820
840
  //#region src/expand-token-references.ts
821
841
  /**
822
842
  * Expand `token(path)` references in a string value to whatever `resolve` returns for the path.
843
+ * One level of parentheses is let in, for a literal colour given an opacity: `token(rgb(0 0 0)/64)`.
844
+ * A comma outside them is not, which leaves the retired fallback, `token(a, var(--b))`, as written.
823
845
  *
824
846
  * This was a 180-line character-state parser, and every line of it was there for the fallback
825
847
  * form — `token(a, b)`, which could nest arbitrarily as `token(a, token(b, var(--c, blue)))`.
@@ -828,7 +850,7 @@ const transforms = [
828
850
  * A path that resolves to nothing is escaped rather than dropped, so it survives into the class
829
851
  * name as written instead of silently becoming empty.
830
852
  */
831
- const REFERENCE = /token\(([^()]+)\)/g;
853
+ const REFERENCE = /token\(((?:[^(),]|\([^()]*\))+)\)/g;
832
854
  const expandTokenReferences = (str, resolve) => {
833
855
  if (!str.includes("token(")) return str;
834
856
  return str.replace(REFERENCE, (_match, path) => {
@@ -1105,14 +1127,17 @@ var TokenDictionary = class {
1105
1127
  invalid: true,
1106
1128
  value
1107
1129
  };
1108
- const [colorPath, rawOpacity] = value.split("/");
1130
+ const slash = opacitySlash(value);
1131
+ const colorPath = slash === -1 ? value : value.slice(0, slash);
1132
+ const rawOpacity = slash === -1 ? "" : value.slice(slash + 1);
1109
1133
  if (!colorPath || !rawOpacity) return {
1110
1134
  invalid: true,
1111
1135
  value: colorPath
1112
1136
  };
1113
1137
  const colorToken = tokenFn(colorPath);
1114
1138
  const opacityToken = this.getByName(`opacity.${rawOpacity}`)?.value;
1115
- if (!opacityToken && isNaN(Number(rawOpacity))) return {
1139
+ const opacity = opacityToken ? Number(opacityToken) * 100 : Number(rawOpacity);
1140
+ if (!(opacity >= 0 && opacity <= 100)) return {
1116
1141
  invalid: true,
1117
1142
  value: colorPath
1118
1143
  };
@@ -1134,7 +1159,7 @@ var TokenDictionary = class {
1134
1159
  if (withFallback) throw new _bamboocss_shared.BambooError("INVALID_TOKEN", fallbackReferenceMessage(withFallback, `the value \`${value}\``));
1135
1160
  return expandTokenReferences(value, (path) => {
1136
1161
  if (!path) return;
1137
- if (path.includes("/")) {
1162
+ if (opacitySlash(path) !== -1) {
1138
1163
  const mix = this.colorMix(path, this.view.getVar.bind(this.view));
1139
1164
  if (mix.invalid) throw new _bamboocss_shared.BambooError("INVALID_TOKEN", "Invalid color mix at " + path + ": " + mix.value);
1140
1165
  return mix.value;
@@ -1152,8 +1177,9 @@ var TokenDictionary = class {
1152
1177
  * `color-mix(in srgb, colors.nope 50%, transparent)`.
1153
1178
  */
1154
1179
  resolvesReference(path) {
1155
- const slash = path.indexOf("/");
1156
- return Boolean(this.view.getVar(slash === -1 ? path : path.slice(0, slash)));
1180
+ const slash = opacitySlash(path);
1181
+ const color = slash === -1 ? path : path.slice(0, slash);
1182
+ return isLiteralColor(color) || Boolean(this.view.getVar(color));
1157
1183
  }
1158
1184
  /**
1159
1185
  * Whether `expandReferenceInValue` takes a `token()` path without complaint: it resolves, and a
@@ -1162,7 +1188,7 @@ var TokenDictionary = class {
1162
1188
  */
1163
1189
  isValidReference(path) {
1164
1190
  if (!this.resolvesReference(path)) return false;
1165
- return !path.includes("/") || !this.colorMix(path, this.view.getVar.bind(this.view)).invalid;
1191
+ return opacitySlash(path) === -1 || !this.colorMix(path, this.view.getVar.bind(this.view)).invalid;
1166
1192
  }
1167
1193
  /**
1168
1194
  * The `token()` paths in a value that name no token.
@@ -1375,8 +1401,10 @@ exports.findCurlyReference = findCurlyReference;
1375
1401
  exports.findFallbackReference = findFallbackReference;
1376
1402
  exports.getReferences = getReferences;
1377
1403
  exports.hasReference = hasReference;
1404
+ exports.isLiteralColor = isLiteralColor;
1378
1405
  exports.isToken = isToken;
1379
1406
  exports.mapToJson = mapToJson;
1380
1407
  exports.opacityPercent = opacityPercent;
1408
+ exports.opacitySlash = opacitySlash;
1381
1409
  exports.referenceOf = referenceOf;
1382
1410
  exports.replaceReference = replaceReference;
package/dist/index.d.cts CHANGED
@@ -270,6 +270,16 @@ declare class TokenDictionaryView {
270
270
  * lives, so rounding there removes the noise without touching any intended value.
271
271
  */
272
272
  declare const opacityPercent: (value: unknown) => string;
273
+ /**
274
+ * Where the opacity of a `token(colour/opacity)` path begins: its last `/` outside parentheses, or
275
+ * -1 for none. A slash inside them is the colour's own, `rgb(0 0 0 / 50%)`.
276
+ */
277
+ declare const opacitySlash: (path: string) => number;
278
+ /**
279
+ * Whether a `token()` path is a colour written out rather than a token: `#000`, `rgb(0 0 0)`. It
280
+ * stands for itself, and given an opacity is mixed like a token's colour, `token(#000/64)`.
281
+ */
282
+ declare const isLiteralColor: (path: string) => boolean;
273
283
  /**
274
284
  * How a reference to `key` is spelled, for a caller building one rather than reading one.
275
285
  *
@@ -303,4 +313,4 @@ declare function mapToJson(map: Map<string, any>): Record<string, unknown>;
303
313
  declare const isToken: (value: any) => value is Token$1;
304
314
  declare function assertTokenFormat(token: any): asserts token is Token$1;
305
315
  //#endregion
306
- export { TOKEN_CATEGORIES, Token, type TokenCategory, TokenDictionary, type TokenExtensions, assertTokenFormat, curlyReferenceMessage, expandReferences, fallbackReferenceMessage, findCurlyReference, findFallbackReference, getReferences, hasReference, isToken, mapToJson, opacityPercent, referenceOf, replaceReference };
316
+ export { TOKEN_CATEGORIES, Token, type TokenCategory, TokenDictionary, type TokenExtensions, assertTokenFormat, curlyReferenceMessage, expandReferences, fallbackReferenceMessage, findCurlyReference, findFallbackReference, getReferences, hasReference, isLiteralColor, isToken, mapToJson, opacityPercent, opacitySlash, referenceOf, replaceReference };
package/dist/index.d.mts CHANGED
@@ -270,6 +270,16 @@ declare class TokenDictionaryView {
270
270
  * lives, so rounding there removes the noise without touching any intended value.
271
271
  */
272
272
  declare const opacityPercent: (value: unknown) => string;
273
+ /**
274
+ * Where the opacity of a `token(colour/opacity)` path begins: its last `/` outside parentheses, or
275
+ * -1 for none. A slash inside them is the colour's own, `rgb(0 0 0 / 50%)`.
276
+ */
277
+ declare const opacitySlash: (path: string) => number;
278
+ /**
279
+ * Whether a `token()` path is a colour written out rather than a token: `#000`, `rgb(0 0 0)`. It
280
+ * stands for itself, and given an opacity is mixed like a token's colour, `token(#000/64)`.
281
+ */
282
+ declare const isLiteralColor: (path: string) => boolean;
273
283
  /**
274
284
  * How a reference to `key` is spelled, for a caller building one rather than reading one.
275
285
  *
@@ -303,4 +313,4 @@ declare function mapToJson(map: Map<string, any>): Record<string, unknown>;
303
313
  declare const isToken: (value: any) => value is Token$1;
304
314
  declare function assertTokenFormat(token: any): asserts token is Token$1;
305
315
  //#endregion
306
- export { TOKEN_CATEGORIES, Token, type TokenCategory, TokenDictionary, type TokenExtensions, assertTokenFormat, curlyReferenceMessage, expandReferences, fallbackReferenceMessage, findCurlyReference, findFallbackReference, getReferences, hasReference, isToken, mapToJson, opacityPercent, referenceOf, replaceReference };
316
+ export { TOKEN_CATEGORIES, Token, type TokenCategory, TokenDictionary, type TokenExtensions, assertTokenFormat, curlyReferenceMessage, expandReferences, fallbackReferenceMessage, findCurlyReference, findFallbackReference, getReferences, hasReference, isLiteralColor, isToken, mapToJson, opacityPercent, opacitySlash, referenceOf, replaceReference };
package/dist/index.mjs CHANGED
@@ -47,9 +47,29 @@ const opacityPercent = (value) => `${Number((Number(value) * 100).toPrecision(12
47
47
  *
48
48
  * The comma exclusion keeps a fallback form — `token(spacing.4, 4)` — out of this pass
49
49
  * deliberately: only `expandTokenReferences` can resolve one, and matching it here would report a
50
- * reference where none is substitutable.
50
+ * reference where none is substitutable. One level of parentheses is let in, for a literal colour
51
+ * given an opacity: `token(rgb(0 0 0)/64)` was left in the css as written.
51
52
  */
52
- const REFERENCE_REGEX = /token\(([^(),]+)\)/g;
53
+ const REFERENCE_REGEX = /token\(((?:[^(),]|\([^()]*\))+)\)/g;
54
+ /**
55
+ * Where the opacity of a `token(colour/opacity)` path begins: its last `/` outside parentheses, or
56
+ * -1 for none. A slash inside them is the colour's own, `rgb(0 0 0 / 50%)`.
57
+ */
58
+ const opacitySlash = (path) => {
59
+ let depth = 0;
60
+ for (let index = path.length - 1; index >= 0; index--) {
61
+ const character = path[index];
62
+ if (character === ")") depth++;
63
+ else if (character === "(") depth--;
64
+ else if (character === "/" && depth === 0) return index;
65
+ }
66
+ return -1;
67
+ };
68
+ /**
69
+ * Whether a `token()` path is a colour written out rather than a token: `#000`, `rgb(0 0 0)`. It
70
+ * stands for itself, and given an opacity is mixed like a token's colour, `token(#000/64)`.
71
+ */
72
+ const isLiteralColor = (path) => path.startsWith("#") || path.includes("(");
53
73
  /**
54
74
  * How a reference to `key` is spelled, for a caller building one rather than reading one.
55
75
  *
@@ -648,7 +668,7 @@ const transforms = [
648
668
  transform(token, dict) {
649
669
  if (!token.value.includes("/")) return token.value;
650
670
  return expandReferences(token.value, (path) => {
651
- if (!path.includes("/")) return referenceOf(path);
671
+ if (opacitySlash(path) === -1) return referenceOf(path);
652
672
  const tokenFn = (tokenPath) => {
653
673
  return dict.getByName(tokenPath)?.extensions.varRef;
654
674
  };
@@ -665,8 +685,8 @@ const transforms = [
665
685
  const { prefix, hash } = dictionary;
666
686
  const refs = getReferences(token.value);
667
687
  if (!refs.length) return token.value;
668
- if (!refs.some((ref) => ref.includes("/"))) refs.forEach((ref) => {
669
- const variable = dictionary.formatCssVar(ref.split("."), {
688
+ if (!refs.some((ref) => opacitySlash(ref) !== -1)) refs.forEach((ref) => {
689
+ const variable = isLiteralColor(ref) ? ref : dictionary.formatCssVar(ref.split("."), {
670
690
  prefix,
671
691
  hash
672
692
  }).ref;
@@ -677,7 +697,7 @@ const transforms = [
677
697
  return dictionary.getByName(name)?.extensions.varRef;
678
698
  };
679
699
  token.value = expandReferences(token.value, (path) => {
680
- if (!path.includes("/")) return dictionary.formatCssVar(path.split("."), {
700
+ if (opacitySlash(path) === -1) return isLiteralColor(path) ? path : dictionary.formatCssVar(path.split("."), {
681
701
  prefix,
682
702
  hash
683
703
  }).ref;
@@ -796,6 +816,8 @@ const transforms = [
796
816
  //#region src/expand-token-references.ts
797
817
  /**
798
818
  * Expand `token(path)` references in a string value to whatever `resolve` returns for the path.
819
+ * One level of parentheses is let in, for a literal colour given an opacity: `token(rgb(0 0 0)/64)`.
820
+ * A comma outside them is not, which leaves the retired fallback, `token(a, var(--b))`, as written.
799
821
  *
800
822
  * This was a 180-line character-state parser, and every line of it was there for the fallback
801
823
  * form — `token(a, b)`, which could nest arbitrarily as `token(a, token(b, var(--c, blue)))`.
@@ -804,7 +826,7 @@ const transforms = [
804
826
  * A path that resolves to nothing is escaped rather than dropped, so it survives into the class
805
827
  * name as written instead of silently becoming empty.
806
828
  */
807
- const REFERENCE = /token\(([^()]+)\)/g;
829
+ const REFERENCE = /token\(((?:[^(),]|\([^()]*\))+)\)/g;
808
830
  const expandTokenReferences = (str, resolve) => {
809
831
  if (!str.includes("token(")) return str;
810
832
  return str.replace(REFERENCE, (_match, path) => {
@@ -1081,14 +1103,17 @@ var TokenDictionary = class {
1081
1103
  invalid: true,
1082
1104
  value
1083
1105
  };
1084
- const [colorPath, rawOpacity] = value.split("/");
1106
+ const slash = opacitySlash(value);
1107
+ const colorPath = slash === -1 ? value : value.slice(0, slash);
1108
+ const rawOpacity = slash === -1 ? "" : value.slice(slash + 1);
1085
1109
  if (!colorPath || !rawOpacity) return {
1086
1110
  invalid: true,
1087
1111
  value: colorPath
1088
1112
  };
1089
1113
  const colorToken = tokenFn(colorPath);
1090
1114
  const opacityToken = this.getByName(`opacity.${rawOpacity}`)?.value;
1091
- if (!opacityToken && isNaN(Number(rawOpacity))) return {
1115
+ const opacity = opacityToken ? Number(opacityToken) * 100 : Number(rawOpacity);
1116
+ if (!(opacity >= 0 && opacity <= 100)) return {
1092
1117
  invalid: true,
1093
1118
  value: colorPath
1094
1119
  };
@@ -1110,7 +1135,7 @@ var TokenDictionary = class {
1110
1135
  if (withFallback) throw new BambooError("INVALID_TOKEN", fallbackReferenceMessage(withFallback, `the value \`${value}\``));
1111
1136
  return expandTokenReferences(value, (path) => {
1112
1137
  if (!path) return;
1113
- if (path.includes("/")) {
1138
+ if (opacitySlash(path) !== -1) {
1114
1139
  const mix = this.colorMix(path, this.view.getVar.bind(this.view));
1115
1140
  if (mix.invalid) throw new BambooError("INVALID_TOKEN", "Invalid color mix at " + path + ": " + mix.value);
1116
1141
  return mix.value;
@@ -1128,8 +1153,9 @@ var TokenDictionary = class {
1128
1153
  * `color-mix(in srgb, colors.nope 50%, transparent)`.
1129
1154
  */
1130
1155
  resolvesReference(path) {
1131
- const slash = path.indexOf("/");
1132
- return Boolean(this.view.getVar(slash === -1 ? path : path.slice(0, slash)));
1156
+ const slash = opacitySlash(path);
1157
+ const color = slash === -1 ? path : path.slice(0, slash);
1158
+ return isLiteralColor(color) || Boolean(this.view.getVar(color));
1133
1159
  }
1134
1160
  /**
1135
1161
  * Whether `expandReferenceInValue` takes a `token()` path without complaint: it resolves, and a
@@ -1138,7 +1164,7 @@ var TokenDictionary = class {
1138
1164
  */
1139
1165
  isValidReference(path) {
1140
1166
  if (!this.resolvesReference(path)) return false;
1141
- return !path.includes("/") || !this.colorMix(path, this.view.getVar.bind(this.view)).invalid;
1167
+ return opacitySlash(path) === -1 || !this.colorMix(path, this.view.getVar.bind(this.view)).invalid;
1142
1168
  }
1143
1169
  /**
1144
1170
  * The `token()` paths in a value that name no token.
@@ -1340,4 +1366,4 @@ function replaceRootWithColorPalette(path, colorPaletteRoot) {
1340
1366
  }
1341
1367
  const tokenPathRegex = /\w+\.\w+/;
1342
1368
  //#endregion
1343
- export { TOKEN_CATEGORIES, Token, TokenDictionary, assertTokenFormat, curlyReferenceMessage, expandReferences, fallbackReferenceMessage, findCurlyReference, findFallbackReference, getReferences, hasReference, isToken, mapToJson, opacityPercent, referenceOf, replaceReference };
1369
+ export { TOKEN_CATEGORIES, Token, TokenDictionary, assertTokenFormat, curlyReferenceMessage, expandReferences, fallbackReferenceMessage, findCurlyReference, findFallbackReference, getReferences, hasReference, isLiteralColor, isToken, mapToJson, opacityPercent, opacitySlash, referenceOf, replaceReference };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bamboocss/token-dictionary",
3
- "version": "1.60.2",
3
+ "version": "1.60.3",
4
4
  "description": "Common error messages for css bamboo",
5
5
  "homepage": "https://bamboocss.com",
6
6
  "license": "MIT",
@@ -35,9 +35,9 @@
35
35
  "dependencies": {
36
36
  "picomatch": "4.0.5",
37
37
  "ts-pattern": "5.9.0",
38
- "@bamboocss/logger": "^1.60.2",
39
- "@bamboocss/shared": "1.60.2",
40
- "@bamboocss/types": "1.60.2"
38
+ "@bamboocss/logger": "^1.60.3",
39
+ "@bamboocss/shared": "1.60.3",
40
+ "@bamboocss/types": "1.60.3"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/picomatch": "4.0.2"