@whitesev/domutils 1.5.0 → 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.umd.js CHANGED
@@ -61,6 +61,22 @@
61
61
  isShow(element) {
62
62
  return Boolean(element.getClientRects().length);
63
63
  },
64
+ /**
65
+ * 获取安全的html
66
+ */
67
+ getSafeHTML(text) {
68
+ // @ts-ignore
69
+ if (globalThis.trustedTypes) {
70
+ // @ts-ignore
71
+ const policy = globalThis.trustedTypes.createPolicy("safe-innerHTML", {
72
+ createHTML: (html) => html,
73
+ });
74
+ return policy.createHTML(text);
75
+ }
76
+ else {
77
+ return text;
78
+ }
79
+ },
64
80
  /**
65
81
  * 在CSP策略下设置innerHTML
66
82
  * @param $el 元素
@@ -1630,7 +1646,7 @@
1630
1646
  }
1631
1647
  function elementAppendChild(ele, text) {
1632
1648
  if (typeof content === "string") {
1633
- ele.insertAdjacentHTML("beforeend", text);
1649
+ ele.insertAdjacentHTML("beforeend", DOMUtilsCommonUtils.getSafeHTML(text));
1634
1650
  }
1635
1651
  else {
1636
1652
  ele.appendChild(text);
@@ -1676,7 +1692,7 @@
1676
1692
  return;
1677
1693
  }
1678
1694
  if (typeof content === "string") {
1679
- element.insertAdjacentHTML("afterbegin", content);
1695
+ element.insertAdjacentHTML("afterbegin", DOMUtilsCommonUtils.getSafeHTML(content));
1680
1696
  }
1681
1697
  else {
1682
1698
  let $firstChild = element.firstChild;
@@ -1713,7 +1729,7 @@
1713
1729
  return;
1714
1730
  }
1715
1731
  if (typeof content === "string") {
1716
- element.insertAdjacentHTML("afterend", content);
1732
+ element.insertAdjacentHTML("afterend", DOMUtilsCommonUtils.getSafeHTML(content));
1717
1733
  }
1718
1734
  else {
1719
1735
  let $parent = element.parentElement;
@@ -1752,7 +1768,7 @@
1752
1768
  return;
1753
1769
  }
1754
1770
  if (typeof content === "string") {
1755
- element.insertAdjacentHTML("beforebegin", content);
1771
+ element.insertAdjacentHTML("beforebegin", DOMUtilsCommonUtils.getSafeHTML(content));
1756
1772
  }
1757
1773
  else {
1758
1774
  let $parent = element.parentElement;