@jsenv/dom 0.7.2 → 0.7.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.
Files changed (2) hide show
  1. package/dist/jsenv_dom.js +56 -1
  2. package/package.json +1 -1
package/dist/jsenv_dom.js CHANGED
@@ -463,6 +463,61 @@ const unitlessProperties = [
463
463
  "scaleZ",
464
464
  ];
465
465
 
466
+ // Well-known CSS units and keywords that indicate a value already has proper formatting
467
+ const cssSizeUnitSet = new Set([
468
+ "px",
469
+ "em",
470
+ "rem",
471
+ "ex",
472
+ "ch",
473
+ "vw",
474
+ "vh",
475
+ "vmin",
476
+ "vmax",
477
+ "cm",
478
+ "mm",
479
+ "in",
480
+ "pt",
481
+ "pc",
482
+ ]);
483
+ const cssUnitSet = new Set([
484
+ ...cssSizeUnitSet,
485
+ "%",
486
+ // Angle units
487
+ "deg",
488
+ "rad",
489
+ "grad",
490
+ "turn",
491
+ // Time units
492
+ "s",
493
+ "ms",
494
+ // Frequency units
495
+ "Hz",
496
+ "kHz",
497
+ ]);
498
+ const cssKeywordSet = new Set([
499
+ // Keywords that shouldn't get units
500
+ "auto",
501
+ "none",
502
+ "inherit",
503
+ "initial",
504
+ "unset",
505
+ "revert",
506
+ ]);
507
+
508
+ // Check if value already has a unit or is a keyword
509
+ const hasUnit = (value) => {
510
+ for (const cssUnit of cssUnitSet) {
511
+ if (value.endsWith(cssUnit)) {
512
+ return true;
513
+ }
514
+ }
515
+ return false;
516
+ };
517
+ const isKeyword = (value) => {
518
+ return cssKeywordSet.has(value);
519
+ };
520
+
466
521
  // Normalize a single style value
467
522
  const normalizeStyle = (value, propertyName, context = "js") => {
468
523
  if (propertyName === "transform") {
@@ -544,7 +599,7 @@ const normalizeNumber = (value, { unit, propertyName, preferedType }) => {
544
599
  if (typeof value === "string") {
545
600
  // Keep strings as-is (including %, em, rem, auto, none, etc.)
546
601
  if (preferedType === "string") {
547
- if (unit && !value.endsWith(unit)) {
602
+ if (unit && !hasUnit(value) && !isKeyword(value)) {
548
603
  return `${value}${unit}`;
549
604
  }
550
605
  return value;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/dom",
3
- "version": "0.7.2",
3
+ "version": "0.7.3",
4
4
  "description": "DOM utilities for writing frontend code",
5
5
  "repository": {
6
6
  "type": "git",