@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/dist/index.esm.js
CHANGED
|
@@ -55,6 +55,22 @@ const DOMUtilsCommonUtils = {
|
|
|
55
55
|
isShow(element) {
|
|
56
56
|
return Boolean(element.getClientRects().length);
|
|
57
57
|
},
|
|
58
|
+
/**
|
|
59
|
+
* 获取安全的html
|
|
60
|
+
*/
|
|
61
|
+
getSafeHTML(text) {
|
|
62
|
+
// @ts-ignore
|
|
63
|
+
if (globalThis.trustedTypes) {
|
|
64
|
+
// @ts-ignore
|
|
65
|
+
const policy = globalThis.trustedTypes.createPolicy("safe-innerHTML", {
|
|
66
|
+
createHTML: (html) => html,
|
|
67
|
+
});
|
|
68
|
+
return policy.createHTML(text);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
return text;
|
|
72
|
+
}
|
|
73
|
+
},
|
|
58
74
|
/**
|
|
59
75
|
* 在CSP策略下设置innerHTML
|
|
60
76
|
* @param $el 元素
|
|
@@ -62,22 +78,7 @@ const DOMUtilsCommonUtils = {
|
|
|
62
78
|
*/
|
|
63
79
|
setSafeHTML($el, text) {
|
|
64
80
|
// 创建 TrustedHTML 策略(需 CSP 允许)
|
|
65
|
-
|
|
66
|
-
$el.innerHTML = text;
|
|
67
|
-
}
|
|
68
|
-
catch (error) {
|
|
69
|
-
// @ts-ignore
|
|
70
|
-
if (globalThis.trustedTypes) {
|
|
71
|
-
// @ts-ignore
|
|
72
|
-
const policy = globalThis.trustedTypes.createPolicy("safe-innerHTML", {
|
|
73
|
-
createHTML: (html) => html,
|
|
74
|
-
});
|
|
75
|
-
$el.innerHTML = policy.createHTML(text);
|
|
76
|
-
}
|
|
77
|
-
else {
|
|
78
|
-
throw new Error("trustedTypes is not defined");
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
+
$el.innerHTML = this.getSafeHTML(text);
|
|
81
82
|
},
|
|
82
83
|
/**
|
|
83
84
|
* 用于显示元素并获取它的高度宽度等其它属性
|
|
@@ -1057,7 +1058,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1057
1058
|
super(option);
|
|
1058
1059
|
}
|
|
1059
1060
|
/** 版本号 */
|
|
1060
|
-
version = "2025.
|
|
1061
|
+
version = "2025.4.8";
|
|
1061
1062
|
attr(element, attrName, attrValue) {
|
|
1062
1063
|
let DOMUtilsContext = this;
|
|
1063
1064
|
if (typeof element === "string") {
|
|
@@ -1200,6 +1201,19 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1200
1201
|
else ;
|
|
1201
1202
|
return;
|
|
1202
1203
|
}
|
|
1204
|
+
let setStyleProperty = (propertyName, propertyValue) => {
|
|
1205
|
+
if (propertyValue === "string" && propertyValue.includes("!important")) {
|
|
1206
|
+
propertyValue = propertyValue
|
|
1207
|
+
.trim()
|
|
1208
|
+
.replace(/!important$/gi, "")
|
|
1209
|
+
.trim();
|
|
1210
|
+
element.style.setProperty(propertyName, propertyValue, "important");
|
|
1211
|
+
}
|
|
1212
|
+
else {
|
|
1213
|
+
propertyValue = handlePixe(propertyName, propertyValue);
|
|
1214
|
+
element.style.setProperty(propertyName, propertyValue);
|
|
1215
|
+
}
|
|
1216
|
+
};
|
|
1203
1217
|
if (typeof property === "string") {
|
|
1204
1218
|
if (value == null) {
|
|
1205
1219
|
return DOMUtilsContext.windowApi.globalThis
|
|
@@ -1207,28 +1221,19 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1207
1221
|
.getPropertyValue(property);
|
|
1208
1222
|
}
|
|
1209
1223
|
else {
|
|
1210
|
-
|
|
1211
|
-
element.style.setProperty(property, value, "important");
|
|
1212
|
-
}
|
|
1213
|
-
else {
|
|
1214
|
-
value = handlePixe(property, value);
|
|
1215
|
-
element.style.setProperty(property, value);
|
|
1216
|
-
}
|
|
1224
|
+
setStyleProperty(property, value);
|
|
1217
1225
|
}
|
|
1218
1226
|
}
|
|
1219
1227
|
else if (typeof property === "object") {
|
|
1220
1228
|
for (let prop in property) {
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
element.style.setProperty(prop, property[prop], "important");
|
|
1224
|
-
}
|
|
1225
|
-
else {
|
|
1226
|
-
property[prop] = handlePixe(prop, property[prop]);
|
|
1227
|
-
element.style.setProperty(prop, property[prop]);
|
|
1228
|
-
}
|
|
1229
|
+
let value = property[prop];
|
|
1230
|
+
setStyleProperty(prop, value);
|
|
1229
1231
|
}
|
|
1230
1232
|
}
|
|
1231
|
-
else
|
|
1233
|
+
else {
|
|
1234
|
+
// 其他情况
|
|
1235
|
+
throw new TypeError("property must be string or object");
|
|
1236
|
+
}
|
|
1232
1237
|
}
|
|
1233
1238
|
text(element, text) {
|
|
1234
1239
|
let DOMUtilsContext = this;
|
|
@@ -1624,7 +1629,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1624
1629
|
}
|
|
1625
1630
|
function elementAppendChild(ele, text) {
|
|
1626
1631
|
if (typeof content === "string") {
|
|
1627
|
-
ele.insertAdjacentHTML("beforeend", text);
|
|
1632
|
+
ele.insertAdjacentHTML("beforeend", DOMUtilsCommonUtils.getSafeHTML(text));
|
|
1628
1633
|
}
|
|
1629
1634
|
else {
|
|
1630
1635
|
ele.appendChild(text);
|
|
@@ -1670,7 +1675,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1670
1675
|
return;
|
|
1671
1676
|
}
|
|
1672
1677
|
if (typeof content === "string") {
|
|
1673
|
-
element.insertAdjacentHTML("afterbegin", content);
|
|
1678
|
+
element.insertAdjacentHTML("afterbegin", DOMUtilsCommonUtils.getSafeHTML(content));
|
|
1674
1679
|
}
|
|
1675
1680
|
else {
|
|
1676
1681
|
let $firstChild = element.firstChild;
|
|
@@ -1707,7 +1712,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1707
1712
|
return;
|
|
1708
1713
|
}
|
|
1709
1714
|
if (typeof content === "string") {
|
|
1710
|
-
element.insertAdjacentHTML("afterend", content);
|
|
1715
|
+
element.insertAdjacentHTML("afterend", DOMUtilsCommonUtils.getSafeHTML(content));
|
|
1711
1716
|
}
|
|
1712
1717
|
else {
|
|
1713
1718
|
let $parent = element.parentElement;
|
|
@@ -1746,7 +1751,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1746
1751
|
return;
|
|
1747
1752
|
}
|
|
1748
1753
|
if (typeof content === "string") {
|
|
1749
|
-
element.insertAdjacentHTML("beforebegin", content);
|
|
1754
|
+
element.insertAdjacentHTML("beforebegin", DOMUtilsCommonUtils.getSafeHTML(content));
|
|
1750
1755
|
}
|
|
1751
1756
|
else {
|
|
1752
1757
|
let $parent = element.parentElement;
|