@whitesev/domutils 1.4.8 → 1.5.0

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.esm.js CHANGED
@@ -55,6 +55,30 @@ const DOMUtilsCommonUtils = {
55
55
  isShow(element) {
56
56
  return Boolean(element.getClientRects().length);
57
57
  },
58
+ /**
59
+ * 在CSP策略下设置innerHTML
60
+ * @param $el 元素
61
+ * @param text 文本
62
+ */
63
+ setSafeHTML($el, text) {
64
+ // 创建 TrustedHTML 策略(需 CSP 允许)
65
+ try {
66
+ $el.innerHTML = text;
67
+ }
68
+ catch (error) {
69
+ // @ts-ignore
70
+ if (globalThis.trustedTypes) {
71
+ // @ts-ignore
72
+ const policy = globalThis.trustedTypes.createPolicy("safe-innerHTML", {
73
+ createHTML: (html) => html,
74
+ });
75
+ $el.innerHTML = policy.createHTML(text);
76
+ }
77
+ else {
78
+ throw new Error("trustedTypes is not defined");
79
+ }
80
+ }
81
+ },
58
82
  /**
59
83
  * 用于显示元素并获取它的高度宽度等其它属性
60
84
  * @param element
@@ -1033,7 +1057,7 @@ class DOMUtils extends DOMUtilsEvent {
1033
1057
  super(option);
1034
1058
  }
1035
1059
  /** 版本号 */
1036
- version = "2024.12.4";
1060
+ version = "2025.3.2";
1037
1061
  attr(element, attrName, attrValue) {
1038
1062
  let DOMUtilsContext = this;
1039
1063
  if (typeof element === "string") {
@@ -1090,7 +1114,7 @@ class DOMUtils extends DOMUtilsEvent {
1090
1114
  let DOMUtilsContext = this;
1091
1115
  let tempElement = DOMUtilsContext.windowApi.document.createElement(tagName);
1092
1116
  if (typeof property === "string") {
1093
- tempElement.innerHTML = property;
1117
+ DOMUtilsContext.html(tempElement, property);
1094
1118
  return tempElement;
1095
1119
  }
1096
1120
  if (property == null) {
@@ -1101,6 +1125,10 @@ class DOMUtils extends DOMUtilsEvent {
1101
1125
  }
1102
1126
  Object.keys(property).forEach((key) => {
1103
1127
  let value = property[key];
1128
+ if (key === "innerHTML") {
1129
+ DOMUtilsContext.html(tempElement, value);
1130
+ return;
1131
+ }
1104
1132
  tempElement[key] = value;
1105
1133
  });
1106
1134
  Object.keys(attributes).forEach((key) => {
@@ -1269,7 +1297,7 @@ class DOMUtils extends DOMUtilsEvent {
1269
1297
  html = html.innerHTML;
1270
1298
  }
1271
1299
  if ("innerHTML" in element) {
1272
- element.innerHTML = html;
1300
+ DOMUtilsCommonUtils.setSafeHTML(element, html);
1273
1301
  }
1274
1302
  }
1275
1303
  }
@@ -1375,7 +1403,12 @@ class DOMUtils extends DOMUtilsEvent {
1375
1403
  return Reflect.get(element, propName);
1376
1404
  }
1377
1405
  else {
1378
- Reflect.set(element, propName, propValue);
1406
+ if (element instanceof Element && propName === "innerHTML") {
1407
+ DOMUtilsContext.html(element, propValue);
1408
+ }
1409
+ else {
1410
+ Reflect.set(element, propName, propValue);
1411
+ }
1379
1412
  }
1380
1413
  }
1381
1414
  /**
@@ -1773,7 +1806,7 @@ class DOMUtils extends DOMUtilsEvent {
1773
1806
  });
1774
1807
  return;
1775
1808
  }
1776
- element.innerHTML = "";
1809
+ DOMUtilsContext.html(element, "");
1777
1810
  }
1778
1811
  /**
1779
1812
  * 获取元素相对于文档的偏移坐标(加上文档的滚动条)
@@ -1983,10 +2016,10 @@ class DOMUtils extends DOMUtilsEvent {
1983
2016
  if (typeof duration !== "number" || duration <= 0) {
1984
2017
  throw new TypeError("duration must be a positive number");
1985
2018
  }
1986
- if (typeof callback !== "function" && callback !== void 0) {
2019
+ if (typeof callback !== "function" && callback !== undefined) {
1987
2020
  throw new TypeError("callback must be a function or null");
1988
2021
  }
1989
- if (typeof styles !== "object" || styles === void 0) {
2022
+ if (typeof styles !== "object" || styles === undefined) {
1990
2023
  throw new TypeError("styles must be an object");
1991
2024
  }
1992
2025
  if (Object.keys(styles).length === 0) {
@@ -2045,7 +2078,7 @@ class DOMUtils extends DOMUtilsEvent {
2045
2078
  element = element;
2046
2079
  // 创建一个新的div元素,并将wrapperHTML作为其innerHTML
2047
2080
  let wrapper = DOMUtilsContext.windowApi.document.createElement("div");
2048
- wrapper.innerHTML = wrapperHTML;
2081
+ DOMUtilsContext.html(wrapper, wrapperHTML);
2049
2082
  let wrapperFirstChild = wrapper.firstChild;
2050
2083
  // 将要包裹的元素插入目标元素前面
2051
2084
  let parentElement = element.parentElement;
@@ -2141,7 +2174,7 @@ class DOMUtils extends DOMUtilsEvent {
2141
2174
  }
2142
2175
  function parseHTMLByCreateDom() {
2143
2176
  let tempDIV = DOMUtilsContext.windowApi.document.createElement("div");
2144
- tempDIV.innerHTML = html;
2177
+ DOMUtilsContext.html(tempDIV, html);
2145
2178
  if (isComplete) {
2146
2179
  return tempDIV;
2147
2180
  }