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