@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.umd.js CHANGED
@@ -61,6 +61,30 @@
61
61
  isShow(element) {
62
62
  return Boolean(element.getClientRects().length);
63
63
  },
64
+ /**
65
+ * 在CSP策略下设置innerHTML
66
+ * @param $el 元素
67
+ * @param text 文本
68
+ */
69
+ setSafeHTML($el, text) {
70
+ // 创建 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
+ },
64
88
  /**
65
89
  * 用于显示元素并获取它的高度宽度等其它属性
66
90
  * @param element
@@ -1039,7 +1063,7 @@
1039
1063
  super(option);
1040
1064
  }
1041
1065
  /** 版本号 */
1042
- version = "2024.12.4";
1066
+ version = "2025.3.2";
1043
1067
  attr(element, attrName, attrValue) {
1044
1068
  let DOMUtilsContext = this;
1045
1069
  if (typeof element === "string") {
@@ -1096,7 +1120,7 @@
1096
1120
  let DOMUtilsContext = this;
1097
1121
  let tempElement = DOMUtilsContext.windowApi.document.createElement(tagName);
1098
1122
  if (typeof property === "string") {
1099
- tempElement.innerHTML = property;
1123
+ DOMUtilsContext.html(tempElement, property);
1100
1124
  return tempElement;
1101
1125
  }
1102
1126
  if (property == null) {
@@ -1107,6 +1131,10 @@
1107
1131
  }
1108
1132
  Object.keys(property).forEach((key) => {
1109
1133
  let value = property[key];
1134
+ if (key === "innerHTML") {
1135
+ DOMUtilsContext.html(tempElement, value);
1136
+ return;
1137
+ }
1110
1138
  tempElement[key] = value;
1111
1139
  });
1112
1140
  Object.keys(attributes).forEach((key) => {
@@ -1275,7 +1303,7 @@
1275
1303
  html = html.innerHTML;
1276
1304
  }
1277
1305
  if ("innerHTML" in element) {
1278
- element.innerHTML = html;
1306
+ DOMUtilsCommonUtils.setSafeHTML(element, html);
1279
1307
  }
1280
1308
  }
1281
1309
  }
@@ -1381,7 +1409,12 @@
1381
1409
  return Reflect.get(element, propName);
1382
1410
  }
1383
1411
  else {
1384
- Reflect.set(element, propName, propValue);
1412
+ if (element instanceof Element && propName === "innerHTML") {
1413
+ DOMUtilsContext.html(element, propValue);
1414
+ }
1415
+ else {
1416
+ Reflect.set(element, propName, propValue);
1417
+ }
1385
1418
  }
1386
1419
  }
1387
1420
  /**
@@ -1779,7 +1812,7 @@
1779
1812
  });
1780
1813
  return;
1781
1814
  }
1782
- element.innerHTML = "";
1815
+ DOMUtilsContext.html(element, "");
1783
1816
  }
1784
1817
  /**
1785
1818
  * 获取元素相对于文档的偏移坐标(加上文档的滚动条)
@@ -1989,10 +2022,10 @@
1989
2022
  if (typeof duration !== "number" || duration <= 0) {
1990
2023
  throw new TypeError("duration must be a positive number");
1991
2024
  }
1992
- if (typeof callback !== "function" && callback !== void 0) {
2025
+ if (typeof callback !== "function" && callback !== undefined) {
1993
2026
  throw new TypeError("callback must be a function or null");
1994
2027
  }
1995
- if (typeof styles !== "object" || styles === void 0) {
2028
+ if (typeof styles !== "object" || styles === undefined) {
1996
2029
  throw new TypeError("styles must be an object");
1997
2030
  }
1998
2031
  if (Object.keys(styles).length === 0) {
@@ -2051,7 +2084,7 @@
2051
2084
  element = element;
2052
2085
  // 创建一个新的div元素,并将wrapperHTML作为其innerHTML
2053
2086
  let wrapper = DOMUtilsContext.windowApi.document.createElement("div");
2054
- wrapper.innerHTML = wrapperHTML;
2087
+ DOMUtilsContext.html(wrapper, wrapperHTML);
2055
2088
  let wrapperFirstChild = wrapper.firstChild;
2056
2089
  // 将要包裹的元素插入目标元素前面
2057
2090
  let parentElement = element.parentElement;
@@ -2147,7 +2180,7 @@
2147
2180
  }
2148
2181
  function parseHTMLByCreateDom() {
2149
2182
  let tempDIV = DOMUtilsContext.windowApi.document.createElement("div");
2150
- tempDIV.innerHTML = html;
2183
+ DOMUtilsContext.html(tempDIV, html);
2151
2184
  if (isComplete) {
2152
2185
  return tempDIV;
2153
2186
  }