@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.
@@ -7,6 +7,10 @@ declare const DOMUtilsCommonUtils: {
7
7
  * @param element
8
8
  */
9
9
  isShow(element: HTMLElement): boolean;
10
+ /**
11
+ * 获取安全的html
12
+ */
13
+ getSafeHTML(text: string): any;
10
14
  /**
11
15
  * 在CSP策略下设置innerHTML
12
16
  * @param $el 元素
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whitesev/domutils",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
4
4
  "description": "使用js重新对jQuery的部分函数进行了仿写",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",
package/src/DOMUtils.ts CHANGED
@@ -867,7 +867,10 @@ class DOMUtils extends DOMUtilsEvent {
867
867
  }
868
868
  function elementAppendChild(ele: HTMLElement, text: HTMLElement | string) {
869
869
  if (typeof content === "string") {
870
- ele.insertAdjacentHTML("beforeend", text as string);
870
+ ele.insertAdjacentHTML(
871
+ "beforeend",
872
+ DOMUtilsCommonUtils.getSafeHTML(text as string)
873
+ );
871
874
  } else {
872
875
  ele.appendChild(text as HTMLElement);
873
876
  }
@@ -912,7 +915,10 @@ class DOMUtils extends DOMUtilsEvent {
912
915
  return;
913
916
  }
914
917
  if (typeof content === "string") {
915
- element.insertAdjacentHTML("afterbegin", content);
918
+ element.insertAdjacentHTML(
919
+ "afterbegin",
920
+ DOMUtilsCommonUtils.getSafeHTML(content)
921
+ );
916
922
  } else {
917
923
  let $firstChild = element.firstChild;
918
924
  if ($firstChild == null) {
@@ -947,7 +953,10 @@ class DOMUtils extends DOMUtilsEvent {
947
953
  return;
948
954
  }
949
955
  if (typeof content === "string") {
950
- element.insertAdjacentHTML("afterend", content);
956
+ element.insertAdjacentHTML(
957
+ "afterend",
958
+ DOMUtilsCommonUtils.getSafeHTML(content)
959
+ );
951
960
  } else {
952
961
  let $parent = element.parentElement;
953
962
  let $nextSlibling = element.nextSibling;
@@ -984,7 +993,10 @@ class DOMUtils extends DOMUtilsEvent {
984
993
  return;
985
994
  }
986
995
  if (typeof content === "string") {
987
- element.insertAdjacentHTML("beforebegin", content);
996
+ element.insertAdjacentHTML(
997
+ "beforebegin",
998
+ DOMUtilsCommonUtils.getSafeHTML(content)
999
+ );
988
1000
  } else {
989
1001
  let $parent = element.parentElement;
990
1002
  if (!$parent) {
@@ -14,6 +14,21 @@ const DOMUtilsCommonUtils = {
14
14
  isShow(element: HTMLElement) {
15
15
  return Boolean(element.getClientRects().length);
16
16
  },
17
+ /**
18
+ * 获取安全的html
19
+ */
20
+ getSafeHTML(text: string) {
21
+ // @ts-ignore
22
+ if (globalThis.trustedTypes) {
23
+ // @ts-ignore
24
+ const policy = globalThis.trustedTypes.createPolicy("safe-innerHTML", {
25
+ createHTML: (html: string) => html,
26
+ });
27
+ return policy.createHTML(text);
28
+ } else {
29
+ return text;
30
+ }
31
+ },
17
32
  /**
18
33
  * 在CSP策略下设置innerHTML
19
34
  * @param $el 元素
@@ -21,20 +36,7 @@ const DOMUtilsCommonUtils = {
21
36
  */
22
37
  setSafeHTML($el: HTMLElement, text: string) {
23
38
  // 创建 TrustedHTML 策略(需 CSP 允许)
24
- try {
25
- $el.innerHTML = text;
26
- } catch (error) {
27
- // @ts-ignore
28
- if (globalThis.trustedTypes) {
29
- // @ts-ignore
30
- const policy = globalThis.trustedTypes.createPolicy("safe-innerHTML", {
31
- createHTML: (html: string) => html,
32
- });
33
- $el.innerHTML = policy.createHTML(text);
34
- } else {
35
- throw new Error("trustedTypes is not defined");
36
- }
37
- }
39
+ $el.innerHTML = this.getSafeHTML(text);
38
40
  },
39
41
  /**
40
42
  * 用于显示元素并获取它的高度宽度等其它属性