@whitesev/domutils 1.5.0 → 1.5.2

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.
@@ -60,6 +60,22 @@ System.register('DOMUtils', [], (function (exports) {
60
60
  isShow(element) {
61
61
  return Boolean(element.getClientRects().length);
62
62
  },
63
+ /**
64
+ * 获取安全的html
65
+ */
66
+ getSafeHTML(text) {
67
+ // @ts-ignore
68
+ if (globalThis.trustedTypes) {
69
+ // @ts-ignore
70
+ const policy = globalThis.trustedTypes.createPolicy("safe-innerHTML", {
71
+ createHTML: (html) => html,
72
+ });
73
+ return policy.createHTML(text);
74
+ }
75
+ else {
76
+ return text;
77
+ }
78
+ },
63
79
  /**
64
80
  * 在CSP策略下设置innerHTML
65
81
  * @param $el 元素
@@ -67,22 +83,7 @@ System.register('DOMUtils', [], (function (exports) {
67
83
  */
68
84
  setSafeHTML($el, text) {
69
85
  // 创建 TrustedHTML 策略(需 CSP 允许)
70
- try {
71
- $el.innerHTML = text;
72
- }
73
- catch (error) {
74
- // @ts-ignore
75
- if (globalThis.trustedTypes) {
76
- // @ts-ignore
77
- const policy = globalThis.trustedTypes.createPolicy("safe-innerHTML", {
78
- createHTML: (html) => html,
79
- });
80
- $el.innerHTML = policy.createHTML(text);
81
- }
82
- else {
83
- throw new Error("trustedTypes is not defined");
84
- }
85
- }
86
+ $el.innerHTML = this.getSafeHTML(text);
86
87
  },
87
88
  /**
88
89
  * 用于显示元素并获取它的高度宽度等其它属性
@@ -1062,7 +1063,7 @@ System.register('DOMUtils', [], (function (exports) {
1062
1063
  super(option);
1063
1064
  }
1064
1065
  /** 版本号 */
1065
- version = "2025.3.2";
1066
+ version = "2025.4.8";
1066
1067
  attr(element, attrName, attrValue) {
1067
1068
  let DOMUtilsContext = this;
1068
1069
  if (typeof element === "string") {
@@ -1205,6 +1206,19 @@ System.register('DOMUtils', [], (function (exports) {
1205
1206
  else ;
1206
1207
  return;
1207
1208
  }
1209
+ let setStyleProperty = (propertyName, propertyValue) => {
1210
+ if (propertyValue === "string" && propertyValue.includes("!important")) {
1211
+ propertyValue = propertyValue
1212
+ .trim()
1213
+ .replace(/!important$/gi, "")
1214
+ .trim();
1215
+ element.style.setProperty(propertyName, propertyValue, "important");
1216
+ }
1217
+ else {
1218
+ propertyValue = handlePixe(propertyName, propertyValue);
1219
+ element.style.setProperty(propertyName, propertyValue);
1220
+ }
1221
+ };
1208
1222
  if (typeof property === "string") {
1209
1223
  if (value == null) {
1210
1224
  return DOMUtilsContext.windowApi.globalThis
@@ -1212,28 +1226,19 @@ System.register('DOMUtils', [], (function (exports) {
1212
1226
  .getPropertyValue(property);
1213
1227
  }
1214
1228
  else {
1215
- if (value === "string" && value.includes("!important")) {
1216
- element.style.setProperty(property, value, "important");
1217
- }
1218
- else {
1219
- value = handlePixe(property, value);
1220
- element.style.setProperty(property, value);
1221
- }
1229
+ setStyleProperty(property, value);
1222
1230
  }
1223
1231
  }
1224
1232
  else if (typeof property === "object") {
1225
1233
  for (let prop in property) {
1226
- if (typeof property[prop] === "string" &&
1227
- property[prop].includes("!important")) {
1228
- element.style.setProperty(prop, property[prop], "important");
1229
- }
1230
- else {
1231
- property[prop] = handlePixe(prop, property[prop]);
1232
- element.style.setProperty(prop, property[prop]);
1233
- }
1234
+ let value = property[prop];
1235
+ setStyleProperty(prop, value);
1234
1236
  }
1235
1237
  }
1236
- else ;
1238
+ else {
1239
+ // 其他情况
1240
+ throw new TypeError("property must be string or object");
1241
+ }
1237
1242
  }
1238
1243
  text(element, text) {
1239
1244
  let DOMUtilsContext = this;
@@ -1629,7 +1634,7 @@ System.register('DOMUtils', [], (function (exports) {
1629
1634
  }
1630
1635
  function elementAppendChild(ele, text) {
1631
1636
  if (typeof content === "string") {
1632
- ele.insertAdjacentHTML("beforeend", text);
1637
+ ele.insertAdjacentHTML("beforeend", DOMUtilsCommonUtils.getSafeHTML(text));
1633
1638
  }
1634
1639
  else {
1635
1640
  ele.appendChild(text);
@@ -1675,7 +1680,7 @@ System.register('DOMUtils', [], (function (exports) {
1675
1680
  return;
1676
1681
  }
1677
1682
  if (typeof content === "string") {
1678
- element.insertAdjacentHTML("afterbegin", content);
1683
+ element.insertAdjacentHTML("afterbegin", DOMUtilsCommonUtils.getSafeHTML(content));
1679
1684
  }
1680
1685
  else {
1681
1686
  let $firstChild = element.firstChild;
@@ -1712,7 +1717,7 @@ System.register('DOMUtils', [], (function (exports) {
1712
1717
  return;
1713
1718
  }
1714
1719
  if (typeof content === "string") {
1715
- element.insertAdjacentHTML("afterend", content);
1720
+ element.insertAdjacentHTML("afterend", DOMUtilsCommonUtils.getSafeHTML(content));
1716
1721
  }
1717
1722
  else {
1718
1723
  let $parent = element.parentElement;
@@ -1751,7 +1756,7 @@ System.register('DOMUtils', [], (function (exports) {
1751
1756
  return;
1752
1757
  }
1753
1758
  if (typeof content === "string") {
1754
- element.insertAdjacentHTML("beforebegin", content);
1759
+ element.insertAdjacentHTML("beforebegin", DOMUtilsCommonUtils.getSafeHTML(content));
1755
1760
  }
1756
1761
  else {
1757
1762
  let $parent = element.parentElement;