@whitesev/domutils 1.5.0 → 1.5.2
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 +42 -37
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +42 -37
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +42 -37
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +42 -37
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +42 -37
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +42 -37
- 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 +37 -24
- package/src/DOMUtilsCommonUtils.ts +16 -14
package/package.json
CHANGED
package/src/DOMUtils.ts
CHANGED
|
@@ -20,7 +20,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
20
20
|
super(option);
|
|
21
21
|
}
|
|
22
22
|
/** 版本号 */
|
|
23
|
-
version = "2025.
|
|
23
|
+
version = "2025.4.8";
|
|
24
24
|
/**
|
|
25
25
|
* 获取元素的属性值
|
|
26
26
|
* @param element 目标元素
|
|
@@ -280,36 +280,37 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
280
280
|
}
|
|
281
281
|
return;
|
|
282
282
|
}
|
|
283
|
+
let setStyleProperty = (
|
|
284
|
+
propertyName: string,
|
|
285
|
+
propertyValue: string | number
|
|
286
|
+
) => {
|
|
287
|
+
if (propertyValue === "string" && propertyValue.includes("!important")) {
|
|
288
|
+
propertyValue = propertyValue
|
|
289
|
+
.trim()
|
|
290
|
+
.replace(/!important$/gi, "")
|
|
291
|
+
.trim();
|
|
292
|
+
element.style.setProperty(propertyName, propertyValue, "important");
|
|
293
|
+
} else {
|
|
294
|
+
propertyValue = handlePixe(propertyName, propertyValue);
|
|
295
|
+
element.style.setProperty(propertyName, propertyValue);
|
|
296
|
+
}
|
|
297
|
+
};
|
|
283
298
|
if (typeof property === "string") {
|
|
284
299
|
if (value == null) {
|
|
285
300
|
return DOMUtilsContext.windowApi.globalThis
|
|
286
301
|
.getComputedStyle(element)
|
|
287
302
|
.getPropertyValue(property);
|
|
288
303
|
} else {
|
|
289
|
-
|
|
290
|
-
element.style.setProperty(property, value, "important");
|
|
291
|
-
} else {
|
|
292
|
-
value = handlePixe(property, value);
|
|
293
|
-
element.style.setProperty(property, value);
|
|
294
|
-
}
|
|
304
|
+
setStyleProperty(property, value);
|
|
295
305
|
}
|
|
296
306
|
} else if (typeof property === "object") {
|
|
297
307
|
for (let prop in property) {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
(property[prop] as string).includes("!important")
|
|
301
|
-
) {
|
|
302
|
-
element.style.setProperty(
|
|
303
|
-
prop,
|
|
304
|
-
property[prop] as string,
|
|
305
|
-
"important"
|
|
306
|
-
);
|
|
307
|
-
} else {
|
|
308
|
-
property[prop] = handlePixe(prop, property[prop] as string);
|
|
309
|
-
element.style.setProperty(prop, property[prop] as string);
|
|
310
|
-
}
|
|
308
|
+
let value = property[prop];
|
|
309
|
+
setStyleProperty(prop, value!);
|
|
311
310
|
}
|
|
312
311
|
} else {
|
|
312
|
+
// 其他情况
|
|
313
|
+
throw new TypeError("property must be string or object");
|
|
313
314
|
}
|
|
314
315
|
}
|
|
315
316
|
/**
|
|
@@ -867,7 +868,10 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
867
868
|
}
|
|
868
869
|
function elementAppendChild(ele: HTMLElement, text: HTMLElement | string) {
|
|
869
870
|
if (typeof content === "string") {
|
|
870
|
-
ele.insertAdjacentHTML(
|
|
871
|
+
ele.insertAdjacentHTML(
|
|
872
|
+
"beforeend",
|
|
873
|
+
DOMUtilsCommonUtils.getSafeHTML(text as string)
|
|
874
|
+
);
|
|
871
875
|
} else {
|
|
872
876
|
ele.appendChild(text as HTMLElement);
|
|
873
877
|
}
|
|
@@ -912,7 +916,10 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
912
916
|
return;
|
|
913
917
|
}
|
|
914
918
|
if (typeof content === "string") {
|
|
915
|
-
element.insertAdjacentHTML(
|
|
919
|
+
element.insertAdjacentHTML(
|
|
920
|
+
"afterbegin",
|
|
921
|
+
DOMUtilsCommonUtils.getSafeHTML(content)
|
|
922
|
+
);
|
|
916
923
|
} else {
|
|
917
924
|
let $firstChild = element.firstChild;
|
|
918
925
|
if ($firstChild == null) {
|
|
@@ -947,7 +954,10 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
947
954
|
return;
|
|
948
955
|
}
|
|
949
956
|
if (typeof content === "string") {
|
|
950
|
-
element.insertAdjacentHTML(
|
|
957
|
+
element.insertAdjacentHTML(
|
|
958
|
+
"afterend",
|
|
959
|
+
DOMUtilsCommonUtils.getSafeHTML(content)
|
|
960
|
+
);
|
|
951
961
|
} else {
|
|
952
962
|
let $parent = element.parentElement;
|
|
953
963
|
let $nextSlibling = element.nextSibling;
|
|
@@ -984,7 +994,10 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
984
994
|
return;
|
|
985
995
|
}
|
|
986
996
|
if (typeof content === "string") {
|
|
987
|
-
element.insertAdjacentHTML(
|
|
997
|
+
element.insertAdjacentHTML(
|
|
998
|
+
"beforebegin",
|
|
999
|
+
DOMUtilsCommonUtils.getSafeHTML(content)
|
|
1000
|
+
);
|
|
988
1001
|
} else {
|
|
989
1002
|
let $parent = element.parentElement;
|
|
990
1003
|
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
|
* 用于显示元素并获取它的高度宽度等其它属性
|