@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.amd.js CHANGED
@@ -57,6 +57,46 @@ define((function () { 'use strict';
57
57
  isShow(element) {
58
58
  return Boolean(element.getClientRects().length);
59
59
  },
60
+ /**
61
+ * 获取安全的html
62
+ */
63
+ getSafeHTML(text) {
64
+ // @ts-ignore
65
+ if (globalThis.trustedTypes) {
66
+ // @ts-ignore
67
+ const policy = globalThis.trustedTypes.createPolicy("safe-innerHTML", {
68
+ createHTML: (html) => html,
69
+ });
70
+ return policy.createHTML(text);
71
+ }
72
+ else {
73
+ return text;
74
+ }
75
+ },
76
+ /**
77
+ * 在CSP策略下设置innerHTML
78
+ * @param $el 元素
79
+ * @param text 文本
80
+ */
81
+ setSafeHTML($el, text) {
82
+ // 创建 TrustedHTML 策略(需 CSP 允许)
83
+ try {
84
+ $el.innerHTML = text;
85
+ }
86
+ catch (error) {
87
+ // @ts-ignore
88
+ if (globalThis.trustedTypes) {
89
+ // @ts-ignore
90
+ const policy = globalThis.trustedTypes.createPolicy("safe-innerHTML", {
91
+ createHTML: (html) => html,
92
+ });
93
+ $el.innerHTML = policy.createHTML(text);
94
+ }
95
+ else {
96
+ throw new Error("trustedTypes is not defined");
97
+ }
98
+ }
99
+ },
60
100
  /**
61
101
  * 用于显示元素并获取它的高度宽度等其它属性
62
102
  * @param element
@@ -1035,7 +1075,7 @@ define((function () { 'use strict';
1035
1075
  super(option);
1036
1076
  }
1037
1077
  /** 版本号 */
1038
- version = "2024.12.4";
1078
+ version = "2025.3.2";
1039
1079
  attr(element, attrName, attrValue) {
1040
1080
  let DOMUtilsContext = this;
1041
1081
  if (typeof element === "string") {
@@ -1092,7 +1132,7 @@ define((function () { 'use strict';
1092
1132
  let DOMUtilsContext = this;
1093
1133
  let tempElement = DOMUtilsContext.windowApi.document.createElement(tagName);
1094
1134
  if (typeof property === "string") {
1095
- tempElement.innerHTML = property;
1135
+ DOMUtilsContext.html(tempElement, property);
1096
1136
  return tempElement;
1097
1137
  }
1098
1138
  if (property == null) {
@@ -1103,6 +1143,10 @@ define((function () { 'use strict';
1103
1143
  }
1104
1144
  Object.keys(property).forEach((key) => {
1105
1145
  let value = property[key];
1146
+ if (key === "innerHTML") {
1147
+ DOMUtilsContext.html(tempElement, value);
1148
+ return;
1149
+ }
1106
1150
  tempElement[key] = value;
1107
1151
  });
1108
1152
  Object.keys(attributes).forEach((key) => {
@@ -1271,7 +1315,7 @@ define((function () { 'use strict';
1271
1315
  html = html.innerHTML;
1272
1316
  }
1273
1317
  if ("innerHTML" in element) {
1274
- element.innerHTML = html;
1318
+ DOMUtilsCommonUtils.setSafeHTML(element, html);
1275
1319
  }
1276
1320
  }
1277
1321
  }
@@ -1377,7 +1421,12 @@ define((function () { 'use strict';
1377
1421
  return Reflect.get(element, propName);
1378
1422
  }
1379
1423
  else {
1380
- Reflect.set(element, propName, propValue);
1424
+ if (element instanceof Element && propName === "innerHTML") {
1425
+ DOMUtilsContext.html(element, propValue);
1426
+ }
1427
+ else {
1428
+ Reflect.set(element, propName, propValue);
1429
+ }
1381
1430
  }
1382
1431
  }
1383
1432
  /**
@@ -1593,7 +1642,7 @@ define((function () { 'use strict';
1593
1642
  }
1594
1643
  function elementAppendChild(ele, text) {
1595
1644
  if (typeof content === "string") {
1596
- ele.insertAdjacentHTML("beforeend", text);
1645
+ ele.insertAdjacentHTML("beforeend", DOMUtilsCommonUtils.getSafeHTML(text));
1597
1646
  }
1598
1647
  else {
1599
1648
  ele.appendChild(text);
@@ -1639,7 +1688,7 @@ define((function () { 'use strict';
1639
1688
  return;
1640
1689
  }
1641
1690
  if (typeof content === "string") {
1642
- element.insertAdjacentHTML("afterbegin", content);
1691
+ element.insertAdjacentHTML("afterbegin", DOMUtilsCommonUtils.getSafeHTML(content));
1643
1692
  }
1644
1693
  else {
1645
1694
  let $firstChild = element.firstChild;
@@ -1676,7 +1725,7 @@ define((function () { 'use strict';
1676
1725
  return;
1677
1726
  }
1678
1727
  if (typeof content === "string") {
1679
- element.insertAdjacentHTML("afterend", content);
1728
+ element.insertAdjacentHTML("afterend", DOMUtilsCommonUtils.getSafeHTML(content));
1680
1729
  }
1681
1730
  else {
1682
1731
  let $parent = element.parentElement;
@@ -1715,7 +1764,7 @@ define((function () { 'use strict';
1715
1764
  return;
1716
1765
  }
1717
1766
  if (typeof content === "string") {
1718
- element.insertAdjacentHTML("beforebegin", content);
1767
+ element.insertAdjacentHTML("beforebegin", DOMUtilsCommonUtils.getSafeHTML(content));
1719
1768
  }
1720
1769
  else {
1721
1770
  let $parent = element.parentElement;
@@ -1775,7 +1824,7 @@ define((function () { 'use strict';
1775
1824
  });
1776
1825
  return;
1777
1826
  }
1778
- element.innerHTML = "";
1827
+ DOMUtilsContext.html(element, "");
1779
1828
  }
1780
1829
  /**
1781
1830
  * 获取元素相对于文档的偏移坐标(加上文档的滚动条)
@@ -1985,10 +2034,10 @@ define((function () { 'use strict';
1985
2034
  if (typeof duration !== "number" || duration <= 0) {
1986
2035
  throw new TypeError("duration must be a positive number");
1987
2036
  }
1988
- if (typeof callback !== "function" && callback !== void 0) {
2037
+ if (typeof callback !== "function" && callback !== undefined) {
1989
2038
  throw new TypeError("callback must be a function or null");
1990
2039
  }
1991
- if (typeof styles !== "object" || styles === void 0) {
2040
+ if (typeof styles !== "object" || styles === undefined) {
1992
2041
  throw new TypeError("styles must be an object");
1993
2042
  }
1994
2043
  if (Object.keys(styles).length === 0) {
@@ -2047,7 +2096,7 @@ define((function () { 'use strict';
2047
2096
  element = element;
2048
2097
  // 创建一个新的div元素,并将wrapperHTML作为其innerHTML
2049
2098
  let wrapper = DOMUtilsContext.windowApi.document.createElement("div");
2050
- wrapper.innerHTML = wrapperHTML;
2099
+ DOMUtilsContext.html(wrapper, wrapperHTML);
2051
2100
  let wrapperFirstChild = wrapper.firstChild;
2052
2101
  // 将要包裹的元素插入目标元素前面
2053
2102
  let parentElement = element.parentElement;
@@ -2143,7 +2192,7 @@ define((function () { 'use strict';
2143
2192
  }
2144
2193
  function parseHTMLByCreateDom() {
2145
2194
  let tempDIV = DOMUtilsContext.windowApi.document.createElement("div");
2146
- tempDIV.innerHTML = html;
2195
+ DOMUtilsContext.html(tempDIV, html);
2147
2196
  if (isComplete) {
2148
2197
  return tempDIV;
2149
2198
  }