@kbach/react 0.6.14 → 0.6.16

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.
@@ -671,25 +671,6 @@ function resolveColor(value, colors, isArbitrary) {
671
671
  }
672
672
  }
673
673
  }
674
- if (hex && !hex.startsWith("#") && !hex.startsWith("rgb") && hex !== "transparent" && hex !== "currentColor") {
675
- if (hex in colors) {
676
- const alias = colors[hex];
677
- if (typeof alias === "string") hex = alias;
678
- else if (typeof alias === "object" && "6" in alias) hex = alias["6"] ?? hex;
679
- } else {
680
- const aliasLastDash = hex.lastIndexOf("-");
681
- if (aliasLastDash > 0) {
682
- const aliasColorName = hex.slice(0, aliasLastDash);
683
- const aliasShade = hex.slice(aliasLastDash + 1);
684
- const aliasScale = colors[aliasColorName];
685
- if (aliasScale && typeof aliasScale === "object") {
686
- hex = aliasScale[aliasShade] ?? null;
687
- } else if (typeof aliasScale === "string") {
688
- hex = aliasScale;
689
- }
690
- }
691
- }
692
- }
693
674
  if (!hex) return null;
694
675
  if (!opacityPart) {
695
676
  if (!getEffectiveIsWeb() && hex.startsWith("#") && hex.length === 9) {
@@ -705,6 +686,8 @@ function resolveColor(value, colors, isArbitrary) {
705
686
  alpha = parseFloat(opacityPart) / 100;
706
687
  }
707
688
  if (isNaN(alpha)) return hex;
689
+ if (alpha < 0) alpha = 0;
690
+ else if (alpha > 1) alpha = 1;
708
691
  return hexToRgba(hex, alpha);
709
692
  }
710
693
  function parseHexRgb(hex) {
@@ -2223,7 +2206,7 @@ var RESOLVERS = {
2223
2206
  "translate-x": ({ value, negative, isArbitrary }, { spacing }) => {
2224
2207
  const v = resolveSpacing(value, negative, spacing, isArbitrary);
2225
2208
  if (v === null) return null;
2226
- if (getEffectiveIsWeb()) return { transform: `translateX(${v})` };
2209
+ if (getEffectiveIsWeb()) return { transform: `translateX(${typeof v === "number" ? `${v}px` : v})` };
2227
2210
  if (typeof v === "string") {
2228
2211
  const n = parseFloat(v);
2229
2212
  return isNaN(n) ? null : { transform: [{ translateX: n }] };
@@ -2233,7 +2216,7 @@ var RESOLVERS = {
2233
2216
  "translate-y": ({ value, negative, isArbitrary }, { spacing }) => {
2234
2217
  const v = resolveSpacing(value, negative, spacing, isArbitrary);
2235
2218
  if (v === null) return null;
2236
- if (getEffectiveIsWeb()) return { transform: `translateY(${v})` };
2219
+ if (getEffectiveIsWeb()) return { transform: `translateY(${typeof v === "number" ? `${v}px` : v})` };
2237
2220
  if (typeof v === "string") {
2238
2221
  const n = parseFloat(v);
2239
2222
  return isNaN(n) ? null : { transform: [{ translateY: n }] };
@@ -2519,6 +2502,14 @@ function isKnownModifier(name) {
2519
2502
  }
2520
2503
 
2521
2504
  // src/core/parser.ts
2505
+ var UNSAFE_ARBITRARY_VALUE = /[{};\\]|\/\*|\*\//;
2506
+ function isSafeArbitraryValue(value) {
2507
+ if (!UNSAFE_ARBITRARY_VALUE.test(value)) return true;
2508
+ if (process.env.NODE_ENV !== "production") {
2509
+ kbachWarn(`Unsafe arbitrary value rejected: "${value}"`);
2510
+ }
2511
+ return false;
2512
+ }
2522
2513
  function parseClass(className) {
2523
2514
  const trimmed = className.trim();
2524
2515
  if (!trimmed) return null;
@@ -2557,6 +2548,7 @@ function parseClass(className) {
2557
2548
  if (bracketStart > 0 && remaining[bracketStart - 1] === "-" && remaining.endsWith("]")) {
2558
2549
  const utility = remaining.slice(0, bracketStart - 1);
2559
2550
  const value = remaining.slice(bracketStart + 1, -1).replace(/_/g, " ");
2551
+ if (!isSafeArbitraryValue(value)) return null;
2560
2552
  return { original: trimmed, modifiers, negative, important, utility, value, isArbitrary: true };
2561
2553
  }
2562
2554
  if (remaining.endsWith("]") && bracketStart === -1) {
@@ -2566,6 +2558,7 @@ function parseClass(className) {
2566
2558
  }
2567
2559
  if (remaining.startsWith("[") && remaining.endsWith("]")) {
2568
2560
  const value = remaining.slice(1, -1).replace(/_/g, " ");
2561
+ if (!isSafeArbitraryValue(value)) return null;
2569
2562
  return { original: trimmed, modifiers, negative, important, utility: "", value, isArbitrary: true };
2570
2563
  }
2571
2564
  if (getBuiltinStandaloneNames().has(remaining)) {
@@ -2701,17 +2694,20 @@ function setDefaultFontFamily(font) {
2701
2694
  globalThis[FONT_KEY] = font;
2702
2695
  }
2703
2696
  var _styleEl = null;
2697
+ var _ruleIndexByKey = /* @__PURE__ */ new Map();
2704
2698
  function evictInjectedRule(rule) {
2699
+ const idx = _ruleIndexByKey.get(rule);
2700
+ _ruleIndexByKey.delete(rule);
2701
+ if (idx === void 0) return;
2705
2702
  const sheet = _styleEl?.sheet;
2706
2703
  if (!sheet) return;
2707
- for (let i = 0; i < sheet.cssRules.length; i++) {
2708
- if (sheet.cssRules[i].cssText === rule) {
2709
- try {
2710
- sheet.deleteRule(i);
2711
- } catch {
2712
- }
2713
- return;
2714
- }
2704
+ try {
2705
+ sheet.deleteRule(idx);
2706
+ } catch {
2707
+ return;
2708
+ }
2709
+ for (const [key, i] of _ruleIndexByKey) {
2710
+ if (i > idx) _ruleIndexByKey.set(key, i - 1);
2715
2711
  }
2716
2712
  }
2717
2713
  var _injectedRules = new LRUCache(5e4, evictInjectedRule);
@@ -637,25 +637,6 @@ function resolveColor(value, colors, isArbitrary) {
637
637
  }
638
638
  }
639
639
  }
640
- if (hex && !hex.startsWith("#") && !hex.startsWith("rgb") && hex !== "transparent" && hex !== "currentColor") {
641
- if (hex in colors) {
642
- const alias = colors[hex];
643
- if (typeof alias === "string") hex = alias;
644
- else if (typeof alias === "object" && "6" in alias) hex = alias["6"] ?? hex;
645
- } else {
646
- const aliasLastDash = hex.lastIndexOf("-");
647
- if (aliasLastDash > 0) {
648
- const aliasColorName = hex.slice(0, aliasLastDash);
649
- const aliasShade = hex.slice(aliasLastDash + 1);
650
- const aliasScale = colors[aliasColorName];
651
- if (aliasScale && typeof aliasScale === "object") {
652
- hex = aliasScale[aliasShade] ?? null;
653
- } else if (typeof aliasScale === "string") {
654
- hex = aliasScale;
655
- }
656
- }
657
- }
658
- }
659
640
  if (!hex) return null;
660
641
  if (!opacityPart) {
661
642
  if (!getEffectiveIsWeb() && hex.startsWith("#") && hex.length === 9) {
@@ -671,6 +652,8 @@ function resolveColor(value, colors, isArbitrary) {
671
652
  alpha = parseFloat(opacityPart) / 100;
672
653
  }
673
654
  if (isNaN(alpha)) return hex;
655
+ if (alpha < 0) alpha = 0;
656
+ else if (alpha > 1) alpha = 1;
674
657
  return hexToRgba(hex, alpha);
675
658
  }
676
659
  function parseHexRgb(hex) {
@@ -2189,7 +2172,7 @@ var RESOLVERS = {
2189
2172
  "translate-x": ({ value, negative, isArbitrary }, { spacing }) => {
2190
2173
  const v = resolveSpacing(value, negative, spacing, isArbitrary);
2191
2174
  if (v === null) return null;
2192
- if (getEffectiveIsWeb()) return { transform: `translateX(${v})` };
2175
+ if (getEffectiveIsWeb()) return { transform: `translateX(${typeof v === "number" ? `${v}px` : v})` };
2193
2176
  if (typeof v === "string") {
2194
2177
  const n = parseFloat(v);
2195
2178
  return isNaN(n) ? null : { transform: [{ translateX: n }] };
@@ -2199,7 +2182,7 @@ var RESOLVERS = {
2199
2182
  "translate-y": ({ value, negative, isArbitrary }, { spacing }) => {
2200
2183
  const v = resolveSpacing(value, negative, spacing, isArbitrary);
2201
2184
  if (v === null) return null;
2202
- if (getEffectiveIsWeb()) return { transform: `translateY(${v})` };
2185
+ if (getEffectiveIsWeb()) return { transform: `translateY(${typeof v === "number" ? `${v}px` : v})` };
2203
2186
  if (typeof v === "string") {
2204
2187
  const n = parseFloat(v);
2205
2188
  return isNaN(n) ? null : { transform: [{ translateY: n }] };
@@ -2485,6 +2468,14 @@ function isKnownModifier(name) {
2485
2468
  }
2486
2469
 
2487
2470
  // src/core/parser.ts
2471
+ var UNSAFE_ARBITRARY_VALUE = /[{};\\]|\/\*|\*\//;
2472
+ function isSafeArbitraryValue(value) {
2473
+ if (!UNSAFE_ARBITRARY_VALUE.test(value)) return true;
2474
+ if (process.env.NODE_ENV !== "production") {
2475
+ kbachWarn(`Unsafe arbitrary value rejected: "${value}"`);
2476
+ }
2477
+ return false;
2478
+ }
2488
2479
  function parseClass(className) {
2489
2480
  const trimmed = className.trim();
2490
2481
  if (!trimmed) return null;
@@ -2523,6 +2514,7 @@ function parseClass(className) {
2523
2514
  if (bracketStart > 0 && remaining[bracketStart - 1] === "-" && remaining.endsWith("]")) {
2524
2515
  const utility = remaining.slice(0, bracketStart - 1);
2525
2516
  const value = remaining.slice(bracketStart + 1, -1).replace(/_/g, " ");
2517
+ if (!isSafeArbitraryValue(value)) return null;
2526
2518
  return { original: trimmed, modifiers, negative, important, utility, value, isArbitrary: true };
2527
2519
  }
2528
2520
  if (remaining.endsWith("]") && bracketStart === -1) {
@@ -2532,6 +2524,7 @@ function parseClass(className) {
2532
2524
  }
2533
2525
  if (remaining.startsWith("[") && remaining.endsWith("]")) {
2534
2526
  const value = remaining.slice(1, -1).replace(/_/g, " ");
2527
+ if (!isSafeArbitraryValue(value)) return null;
2535
2528
  return { original: trimmed, modifiers, negative, important, utility: "", value, isArbitrary: true };
2536
2529
  }
2537
2530
  if (getBuiltinStandaloneNames().has(remaining)) {
@@ -2667,17 +2660,20 @@ function setDefaultFontFamily(font) {
2667
2660
  globalThis[FONT_KEY] = font;
2668
2661
  }
2669
2662
  var _styleEl = null;
2663
+ var _ruleIndexByKey = /* @__PURE__ */ new Map();
2670
2664
  function evictInjectedRule(rule) {
2665
+ const idx = _ruleIndexByKey.get(rule);
2666
+ _ruleIndexByKey.delete(rule);
2667
+ if (idx === void 0) return;
2671
2668
  const sheet = _styleEl?.sheet;
2672
2669
  if (!sheet) return;
2673
- for (let i = 0; i < sheet.cssRules.length; i++) {
2674
- if (sheet.cssRules[i].cssText === rule) {
2675
- try {
2676
- sheet.deleteRule(i);
2677
- } catch {
2678
- }
2679
- return;
2680
- }
2670
+ try {
2671
+ sheet.deleteRule(idx);
2672
+ } catch {
2673
+ return;
2674
+ }
2675
+ for (const [key, i] of _ruleIndexByKey) {
2676
+ if (i > idx) _ruleIndexByKey.set(key, i - 1);
2681
2677
  }
2682
2678
  }
2683
2679
  var _injectedRules = new LRUCache(5e4, evictInjectedRule);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kbach/react",
3
- "version": "0.6.14",
3
+ "version": "0.6.16",
4
4
  "description": "React / React Native components and hooks for the Kbach framework",
5
5
  "source": "./src/index.ts",
6
6
  "main": "./dist/index.js",