@whitesev/domutils 1.4.8 → 1.5.1

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,46 @@ const DOMUtilsCommonUtils = {
55
55
  isShow(element) {
56
56
  return Boolean(element.getClientRects().length);
57
57
  },
58
+ /**
59
+ * 获取安全的html
60
+ */
61
+ getSafeHTML(text) {
62
+ // @ts-ignore
63
+ if (globalThis.trustedTypes) {
64
+ // @ts-ignore
65
+ const policy = globalThis.trustedTypes.createPolicy("safe-innerHTML", {
66
+ createHTML: (html) => html,
67
+ });
68
+ return policy.createHTML(text);
69
+ }
70
+ else {
71
+ return text;
72
+ }
73
+ },
74
+ /**
75
+ * 在CSP策略下设置innerHTML
76
+ * @param $el 元素
77
+ * @param text 文本
78
+ */
79
+ setSafeHTML($el, text) {
80
+ // 创建 TrustedHTML 策略(需 CSP 允许)
81
+ try {
82
+ $el.innerHTML = text;
83
+ }
84
+ catch (error) {
85
+ // @ts-ignore
86
+ if (globalThis.trustedTypes) {
87
+ // @ts-ignore
88
+ const policy = globalThis.trustedTypes.createPolicy("safe-innerHTML", {
89
+ createHTML: (html) => html,
90
+ });
91
+ $el.innerHTML = policy.createHTML(text);
92
+ }
93
+ else {
94
+ throw new Error("trustedTypes is not defined");
95
+ }
96
+ }
97
+ },
58
98
  /**
59
99
  * 用于显示元素并获取它的高度宽度等其它属性
60
100
  * @param element
@@ -1033,7 +1073,7 @@ class DOMUtils extends DOMUtilsEvent {
1033
1073
  super(option);
1034
1074
  }
1035
1075
  /** 版本号 */
1036
- version = "2024.12.4";
1076
+ version = "2025.3.2";
1037
1077
  attr(element, attrName, attrValue) {
1038
1078
  let DOMUtilsContext = this;
1039
1079
  if (typeof element === "string") {
@@ -1090,7 +1130,7 @@ class DOMUtils extends DOMUtilsEvent {
1090
1130
  let DOMUtilsContext = this;
1091
1131
  let tempElement = DOMUtilsContext.windowApi.document.createElement(tagName);
1092
1132
  if (typeof property === "string") {
1093
- tempElement.innerHTML = property;
1133
+ DOMUtilsContext.html(tempElement, property);
1094
1134
  return tempElement;
1095
1135
  }
1096
1136
  if (property == null) {
@@ -1101,6 +1141,10 @@ class DOMUtils extends DOMUtilsEvent {
1101
1141
  }
1102
1142
  Object.keys(property).forEach((key) => {
1103
1143
  let value = property[key];
1144
+ if (key === "innerHTML") {
1145
+ DOMUtilsContext.html(tempElement, value);
1146
+ return;
1147
+ }
1104
1148
  tempElement[key] = value;
1105
1149
  });
1106
1150
  Object.keys(attributes).forEach((key) => {
@@ -1269,7 +1313,7 @@ class DOMUtils extends DOMUtilsEvent {
1269
1313
  html = html.innerHTML;
1270
1314
  }
1271
1315
  if ("innerHTML" in element) {
1272
- element.innerHTML = html;
1316
+ DOMUtilsCommonUtils.setSafeHTML(element, html);
1273
1317
  }
1274
1318
  }
1275
1319
  }
@@ -1375,7 +1419,12 @@ class DOMUtils extends DOMUtilsEvent {
1375
1419
  return Reflect.get(element, propName);
1376
1420
  }
1377
1421
  else {
1378
- Reflect.set(element, propName, propValue);
1422
+ if (element instanceof Element && propName === "innerHTML") {
1423
+ DOMUtilsContext.html(element, propValue);
1424
+ }
1425
+ else {
1426
+ Reflect.set(element, propName, propValue);
1427
+ }
1379
1428
  }
1380
1429
  }
1381
1430
  /**
@@ -1591,7 +1640,7 @@ class DOMUtils extends DOMUtilsEvent {
1591
1640
  }
1592
1641
  function elementAppendChild(ele, text) {
1593
1642
  if (typeof content === "string") {
1594
- ele.insertAdjacentHTML("beforeend", text);
1643
+ ele.insertAdjacentHTML("beforeend", DOMUtilsCommonUtils.getSafeHTML(text));
1595
1644
  }
1596
1645
  else {
1597
1646
  ele.appendChild(text);
@@ -1637,7 +1686,7 @@ class DOMUtils extends DOMUtilsEvent {
1637
1686
  return;
1638
1687
  }
1639
1688
  if (typeof content === "string") {
1640
- element.insertAdjacentHTML("afterbegin", content);
1689
+ element.insertAdjacentHTML("afterbegin", DOMUtilsCommonUtils.getSafeHTML(content));
1641
1690
  }
1642
1691
  else {
1643
1692
  let $firstChild = element.firstChild;
@@ -1674,7 +1723,7 @@ class DOMUtils extends DOMUtilsEvent {
1674
1723
  return;
1675
1724
  }
1676
1725
  if (typeof content === "string") {
1677
- element.insertAdjacentHTML("afterend", content);
1726
+ element.insertAdjacentHTML("afterend", DOMUtilsCommonUtils.getSafeHTML(content));
1678
1727
  }
1679
1728
  else {
1680
1729
  let $parent = element.parentElement;
@@ -1713,7 +1762,7 @@ class DOMUtils extends DOMUtilsEvent {
1713
1762
  return;
1714
1763
  }
1715
1764
  if (typeof content === "string") {
1716
- element.insertAdjacentHTML("beforebegin", content);
1765
+ element.insertAdjacentHTML("beforebegin", DOMUtilsCommonUtils.getSafeHTML(content));
1717
1766
  }
1718
1767
  else {
1719
1768
  let $parent = element.parentElement;
@@ -1773,7 +1822,7 @@ class DOMUtils extends DOMUtilsEvent {
1773
1822
  });
1774
1823
  return;
1775
1824
  }
1776
- element.innerHTML = "";
1825
+ DOMUtilsContext.html(element, "");
1777
1826
  }
1778
1827
  /**
1779
1828
  * 获取元素相对于文档的偏移坐标(加上文档的滚动条)
@@ -1983,10 +2032,10 @@ class DOMUtils extends DOMUtilsEvent {
1983
2032
  if (typeof duration !== "number" || duration <= 0) {
1984
2033
  throw new TypeError("duration must be a positive number");
1985
2034
  }
1986
- if (typeof callback !== "function" && callback !== void 0) {
2035
+ if (typeof callback !== "function" && callback !== undefined) {
1987
2036
  throw new TypeError("callback must be a function or null");
1988
2037
  }
1989
- if (typeof styles !== "object" || styles === void 0) {
2038
+ if (typeof styles !== "object" || styles === undefined) {
1990
2039
  throw new TypeError("styles must be an object");
1991
2040
  }
1992
2041
  if (Object.keys(styles).length === 0) {
@@ -2045,7 +2094,7 @@ class DOMUtils extends DOMUtilsEvent {
2045
2094
  element = element;
2046
2095
  // 创建一个新的div元素,并将wrapperHTML作为其innerHTML
2047
2096
  let wrapper = DOMUtilsContext.windowApi.document.createElement("div");
2048
- wrapper.innerHTML = wrapperHTML;
2097
+ DOMUtilsContext.html(wrapper, wrapperHTML);
2049
2098
  let wrapperFirstChild = wrapper.firstChild;
2050
2099
  // 将要包裹的元素插入目标元素前面
2051
2100
  let parentElement = element.parentElement;
@@ -2141,7 +2190,7 @@ class DOMUtils extends DOMUtilsEvent {
2141
2190
  }
2142
2191
  function parseHTMLByCreateDom() {
2143
2192
  let tempDIV = DOMUtilsContext.windowApi.document.createElement("div");
2144
- tempDIV.innerHTML = html;
2193
+ DOMUtilsContext.html(tempDIV, html);
2145
2194
  if (isComplete) {
2146
2195
  return tempDIV;
2147
2196
  }