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