@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.amd.js +20 -4
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +20 -4
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +20 -4
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +20 -4
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +20 -4
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +20 -4
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/DOMUtilsCommonUtils.d.ts +4 -0
- package/package.json +1 -1
- package/src/DOMUtils.ts +16 -4
- package/src/DOMUtilsCommonUtils.ts +16 -14
package/package.json
CHANGED
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(
|
|
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(
|
|
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(
|
|
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(
|
|
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
|
-
|
|
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
|
* 用于显示元素并获取它的高度宽度等其它属性
|