@jsenv/dom 0.7.2 → 0.7.4

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 +74 -1
  2. package/package.json +1 -1
package/dist/jsenv_dom.js CHANGED
@@ -463,6 +463,66 @@ 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
+
521
+ // url(
522
+ // linear-gradient(
523
+ // radial-gradient(
524
+ // ...
525
+ const STARTS_WITH_CSS_IMAGE_FUNCTION_REGEX = /^[a-z-]+\(/;
466
526
  // Normalize a single style value
467
527
  const normalizeStyle = (value, propertyName, context = "js") => {
468
528
  if (propertyName === "transform") {
@@ -516,6 +576,19 @@ const normalizeStyle = (value, propertyName, context = "js") => {
516
576
  return undefined;
517
577
  }
518
578
 
579
+ if (propertyName === "backgroundImage") {
580
+ if (context === "css") {
581
+ if (
582
+ typeof value === "string" &&
583
+ !isKeyword(value) &&
584
+ !STARTS_WITH_CSS_IMAGE_FUNCTION_REGEX.test(value)
585
+ ) {
586
+ return `url(${value})`;
587
+ }
588
+ }
589
+ return value;
590
+ }
591
+
519
592
  if (pxProperties.includes(propertyName)) {
520
593
  return normalizeNumber(value, {
521
594
  propertyName,
@@ -544,7 +617,7 @@ const normalizeNumber = (value, { unit, propertyName, preferedType }) => {
544
617
  if (typeof value === "string") {
545
618
  // Keep strings as-is (including %, em, rem, auto, none, etc.)
546
619
  if (preferedType === "string") {
547
- if (unit && !value.endsWith(unit)) {
620
+ if (unit && !hasUnit(value) && !isKeyword(value)) {
548
621
  return `${value}${unit}`;
549
622
  }
550
623
  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.4",
4
4
  "description": "DOM utilities for writing frontend code",
5
5
  "repository": {
6
6
  "type": "git",