@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.
@@ -58,6 +58,30 @@ var DOMUtils = (function () {
58
58
  isShow(element) {
59
59
  return Boolean(element.getClientRects().length);
60
60
  },
61
+ /**
62
+ * 在CSP策略下设置innerHTML
63
+ * @param $el 元素
64
+ * @param text 文本
65
+ */
66
+ setSafeHTML($el, text) {
67
+ // 创建 TrustedHTML 策略(需 CSP 允许)
68
+ try {
69
+ $el.innerHTML = text;
70
+ }
71
+ catch (error) {
72
+ // @ts-ignore
73
+ if (globalThis.trustedTypes) {
74
+ // @ts-ignore
75
+ const policy = globalThis.trustedTypes.createPolicy("safe-innerHTML", {
76
+ createHTML: (html) => html,
77
+ });
78
+ $el.innerHTML = policy.createHTML(text);
79
+ }
80
+ else {
81
+ throw new Error("trustedTypes is not defined");
82
+ }
83
+ }
84
+ },
61
85
  /**
62
86
  * 用于显示元素并获取它的高度宽度等其它属性
63
87
  * @param element
@@ -1036,7 +1060,7 @@ var DOMUtils = (function () {
1036
1060
  super(option);
1037
1061
  }
1038
1062
  /** 版本号 */
1039
- version = "2024.12.4";
1063
+ version = "2025.3.2";
1040
1064
  attr(element, attrName, attrValue) {
1041
1065
  let DOMUtilsContext = this;
1042
1066
  if (typeof element === "string") {
@@ -1093,7 +1117,7 @@ var DOMUtils = (function () {
1093
1117
  let DOMUtilsContext = this;
1094
1118
  let tempElement = DOMUtilsContext.windowApi.document.createElement(tagName);
1095
1119
  if (typeof property === "string") {
1096
- tempElement.innerHTML = property;
1120
+ DOMUtilsContext.html(tempElement, property);
1097
1121
  return tempElement;
1098
1122
  }
1099
1123
  if (property == null) {
@@ -1104,6 +1128,10 @@ var DOMUtils = (function () {
1104
1128
  }
1105
1129
  Object.keys(property).forEach((key) => {
1106
1130
  let value = property[key];
1131
+ if (key === "innerHTML") {
1132
+ DOMUtilsContext.html(tempElement, value);
1133
+ return;
1134
+ }
1107
1135
  tempElement[key] = value;
1108
1136
  });
1109
1137
  Object.keys(attributes).forEach((key) => {
@@ -1272,7 +1300,7 @@ var DOMUtils = (function () {
1272
1300
  html = html.innerHTML;
1273
1301
  }
1274
1302
  if ("innerHTML" in element) {
1275
- element.innerHTML = html;
1303
+ DOMUtilsCommonUtils.setSafeHTML(element, html);
1276
1304
  }
1277
1305
  }
1278
1306
  }
@@ -1378,7 +1406,12 @@ var DOMUtils = (function () {
1378
1406
  return Reflect.get(element, propName);
1379
1407
  }
1380
1408
  else {
1381
- Reflect.set(element, propName, propValue);
1409
+ if (element instanceof Element && propName === "innerHTML") {
1410
+ DOMUtilsContext.html(element, propValue);
1411
+ }
1412
+ else {
1413
+ Reflect.set(element, propName, propValue);
1414
+ }
1382
1415
  }
1383
1416
  }
1384
1417
  /**
@@ -1776,7 +1809,7 @@ var DOMUtils = (function () {
1776
1809
  });
1777
1810
  return;
1778
1811
  }
1779
- element.innerHTML = "";
1812
+ DOMUtilsContext.html(element, "");
1780
1813
  }
1781
1814
  /**
1782
1815
  * 获取元素相对于文档的偏移坐标(加上文档的滚动条)
@@ -1986,10 +2019,10 @@ var DOMUtils = (function () {
1986
2019
  if (typeof duration !== "number" || duration <= 0) {
1987
2020
  throw new TypeError("duration must be a positive number");
1988
2021
  }
1989
- if (typeof callback !== "function" && callback !== void 0) {
2022
+ if (typeof callback !== "function" && callback !== undefined) {
1990
2023
  throw new TypeError("callback must be a function or null");
1991
2024
  }
1992
- if (typeof styles !== "object" || styles === void 0) {
2025
+ if (typeof styles !== "object" || styles === undefined) {
1993
2026
  throw new TypeError("styles must be an object");
1994
2027
  }
1995
2028
  if (Object.keys(styles).length === 0) {
@@ -2048,7 +2081,7 @@ var DOMUtils = (function () {
2048
2081
  element = element;
2049
2082
  // 创建一个新的div元素,并将wrapperHTML作为其innerHTML
2050
2083
  let wrapper = DOMUtilsContext.windowApi.document.createElement("div");
2051
- wrapper.innerHTML = wrapperHTML;
2084
+ DOMUtilsContext.html(wrapper, wrapperHTML);
2052
2085
  let wrapperFirstChild = wrapper.firstChild;
2053
2086
  // 将要包裹的元素插入目标元素前面
2054
2087
  let parentElement = element.parentElement;
@@ -2144,7 +2177,7 @@ var DOMUtils = (function () {
2144
2177
  }
2145
2178
  function parseHTMLByCreateDom() {
2146
2179
  let tempDIV = DOMUtilsContext.windowApi.document.createElement("div");
2147
- tempDIV.innerHTML = html;
2180
+ DOMUtilsContext.html(tempDIV, html);
2148
2181
  if (isComplete) {
2149
2182
  return tempDIV;
2150
2183
  }