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