@whitesev/domutils 1.4.8 → 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 +62 -13
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +62 -13
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +62 -13
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +62 -13
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +62 -13
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +62 -13
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/DOMUtilsCommonUtils.d.ts +10 -0
- package/dist/types/src/types/DOMUtilsEvent.d.ts +1 -0
- package/package.json +1 -1
- package/src/DOMUtils.ts +31 -11
- package/src/DOMUtilsCommonUtils.ts +24 -0
- package/src/types/DOMUtilsEvent.d.ts +1 -0
package/dist/index.system.js
CHANGED
|
@@ -60,6 +60,46 @@ System.register('DOMUtils', [], (function (exports) {
|
|
|
60
60
|
isShow(element) {
|
|
61
61
|
return Boolean(element.getClientRects().length);
|
|
62
62
|
},
|
|
63
|
+
/**
|
|
64
|
+
* 获取安全的html
|
|
65
|
+
*/
|
|
66
|
+
getSafeHTML(text) {
|
|
67
|
+
// @ts-ignore
|
|
68
|
+
if (globalThis.trustedTypes) {
|
|
69
|
+
// @ts-ignore
|
|
70
|
+
const policy = globalThis.trustedTypes.createPolicy("safe-innerHTML", {
|
|
71
|
+
createHTML: (html) => html,
|
|
72
|
+
});
|
|
73
|
+
return policy.createHTML(text);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
return text;
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
/**
|
|
80
|
+
* 在CSP策略下设置innerHTML
|
|
81
|
+
* @param $el 元素
|
|
82
|
+
* @param text 文本
|
|
83
|
+
*/
|
|
84
|
+
setSafeHTML($el, text) {
|
|
85
|
+
// 创建 TrustedHTML 策略(需 CSP 允许)
|
|
86
|
+
try {
|
|
87
|
+
$el.innerHTML = text;
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
// @ts-ignore
|
|
91
|
+
if (globalThis.trustedTypes) {
|
|
92
|
+
// @ts-ignore
|
|
93
|
+
const policy = globalThis.trustedTypes.createPolicy("safe-innerHTML", {
|
|
94
|
+
createHTML: (html) => html,
|
|
95
|
+
});
|
|
96
|
+
$el.innerHTML = policy.createHTML(text);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
throw new Error("trustedTypes is not defined");
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
},
|
|
63
103
|
/**
|
|
64
104
|
* 用于显示元素并获取它的高度宽度等其它属性
|
|
65
105
|
* @param element
|
|
@@ -1038,7 +1078,7 @@ System.register('DOMUtils', [], (function (exports) {
|
|
|
1038
1078
|
super(option);
|
|
1039
1079
|
}
|
|
1040
1080
|
/** 版本号 */
|
|
1041
|
-
version = "
|
|
1081
|
+
version = "2025.3.2";
|
|
1042
1082
|
attr(element, attrName, attrValue) {
|
|
1043
1083
|
let DOMUtilsContext = this;
|
|
1044
1084
|
if (typeof element === "string") {
|
|
@@ -1095,7 +1135,7 @@ System.register('DOMUtils', [], (function (exports) {
|
|
|
1095
1135
|
let DOMUtilsContext = this;
|
|
1096
1136
|
let tempElement = DOMUtilsContext.windowApi.document.createElement(tagName);
|
|
1097
1137
|
if (typeof property === "string") {
|
|
1098
|
-
tempElement
|
|
1138
|
+
DOMUtilsContext.html(tempElement, property);
|
|
1099
1139
|
return tempElement;
|
|
1100
1140
|
}
|
|
1101
1141
|
if (property == null) {
|
|
@@ -1106,6 +1146,10 @@ System.register('DOMUtils', [], (function (exports) {
|
|
|
1106
1146
|
}
|
|
1107
1147
|
Object.keys(property).forEach((key) => {
|
|
1108
1148
|
let value = property[key];
|
|
1149
|
+
if (key === "innerHTML") {
|
|
1150
|
+
DOMUtilsContext.html(tempElement, value);
|
|
1151
|
+
return;
|
|
1152
|
+
}
|
|
1109
1153
|
tempElement[key] = value;
|
|
1110
1154
|
});
|
|
1111
1155
|
Object.keys(attributes).forEach((key) => {
|
|
@@ -1274,7 +1318,7 @@ System.register('DOMUtils', [], (function (exports) {
|
|
|
1274
1318
|
html = html.innerHTML;
|
|
1275
1319
|
}
|
|
1276
1320
|
if ("innerHTML" in element) {
|
|
1277
|
-
element
|
|
1321
|
+
DOMUtilsCommonUtils.setSafeHTML(element, html);
|
|
1278
1322
|
}
|
|
1279
1323
|
}
|
|
1280
1324
|
}
|
|
@@ -1380,7 +1424,12 @@ System.register('DOMUtils', [], (function (exports) {
|
|
|
1380
1424
|
return Reflect.get(element, propName);
|
|
1381
1425
|
}
|
|
1382
1426
|
else {
|
|
1383
|
-
|
|
1427
|
+
if (element instanceof Element && propName === "innerHTML") {
|
|
1428
|
+
DOMUtilsContext.html(element, propValue);
|
|
1429
|
+
}
|
|
1430
|
+
else {
|
|
1431
|
+
Reflect.set(element, propName, propValue);
|
|
1432
|
+
}
|
|
1384
1433
|
}
|
|
1385
1434
|
}
|
|
1386
1435
|
/**
|
|
@@ -1596,7 +1645,7 @@ System.register('DOMUtils', [], (function (exports) {
|
|
|
1596
1645
|
}
|
|
1597
1646
|
function elementAppendChild(ele, text) {
|
|
1598
1647
|
if (typeof content === "string") {
|
|
1599
|
-
ele.insertAdjacentHTML("beforeend", text);
|
|
1648
|
+
ele.insertAdjacentHTML("beforeend", DOMUtilsCommonUtils.getSafeHTML(text));
|
|
1600
1649
|
}
|
|
1601
1650
|
else {
|
|
1602
1651
|
ele.appendChild(text);
|
|
@@ -1642,7 +1691,7 @@ System.register('DOMUtils', [], (function (exports) {
|
|
|
1642
1691
|
return;
|
|
1643
1692
|
}
|
|
1644
1693
|
if (typeof content === "string") {
|
|
1645
|
-
element.insertAdjacentHTML("afterbegin", content);
|
|
1694
|
+
element.insertAdjacentHTML("afterbegin", DOMUtilsCommonUtils.getSafeHTML(content));
|
|
1646
1695
|
}
|
|
1647
1696
|
else {
|
|
1648
1697
|
let $firstChild = element.firstChild;
|
|
@@ -1679,7 +1728,7 @@ System.register('DOMUtils', [], (function (exports) {
|
|
|
1679
1728
|
return;
|
|
1680
1729
|
}
|
|
1681
1730
|
if (typeof content === "string") {
|
|
1682
|
-
element.insertAdjacentHTML("afterend", content);
|
|
1731
|
+
element.insertAdjacentHTML("afterend", DOMUtilsCommonUtils.getSafeHTML(content));
|
|
1683
1732
|
}
|
|
1684
1733
|
else {
|
|
1685
1734
|
let $parent = element.parentElement;
|
|
@@ -1718,7 +1767,7 @@ System.register('DOMUtils', [], (function (exports) {
|
|
|
1718
1767
|
return;
|
|
1719
1768
|
}
|
|
1720
1769
|
if (typeof content === "string") {
|
|
1721
|
-
element.insertAdjacentHTML("beforebegin", content);
|
|
1770
|
+
element.insertAdjacentHTML("beforebegin", DOMUtilsCommonUtils.getSafeHTML(content));
|
|
1722
1771
|
}
|
|
1723
1772
|
else {
|
|
1724
1773
|
let $parent = element.parentElement;
|
|
@@ -1778,7 +1827,7 @@ System.register('DOMUtils', [], (function (exports) {
|
|
|
1778
1827
|
});
|
|
1779
1828
|
return;
|
|
1780
1829
|
}
|
|
1781
|
-
element
|
|
1830
|
+
DOMUtilsContext.html(element, "");
|
|
1782
1831
|
}
|
|
1783
1832
|
/**
|
|
1784
1833
|
* 获取元素相对于文档的偏移坐标(加上文档的滚动条)
|
|
@@ -1988,10 +2037,10 @@ System.register('DOMUtils', [], (function (exports) {
|
|
|
1988
2037
|
if (typeof duration !== "number" || duration <= 0) {
|
|
1989
2038
|
throw new TypeError("duration must be a positive number");
|
|
1990
2039
|
}
|
|
1991
|
-
if (typeof callback !== "function" && callback !==
|
|
2040
|
+
if (typeof callback !== "function" && callback !== undefined) {
|
|
1992
2041
|
throw new TypeError("callback must be a function or null");
|
|
1993
2042
|
}
|
|
1994
|
-
if (typeof styles !== "object" || styles ===
|
|
2043
|
+
if (typeof styles !== "object" || styles === undefined) {
|
|
1995
2044
|
throw new TypeError("styles must be an object");
|
|
1996
2045
|
}
|
|
1997
2046
|
if (Object.keys(styles).length === 0) {
|
|
@@ -2050,7 +2099,7 @@ System.register('DOMUtils', [], (function (exports) {
|
|
|
2050
2099
|
element = element;
|
|
2051
2100
|
// 创建一个新的div元素,并将wrapperHTML作为其innerHTML
|
|
2052
2101
|
let wrapper = DOMUtilsContext.windowApi.document.createElement("div");
|
|
2053
|
-
wrapper
|
|
2102
|
+
DOMUtilsContext.html(wrapper, wrapperHTML);
|
|
2054
2103
|
let wrapperFirstChild = wrapper.firstChild;
|
|
2055
2104
|
// 将要包裹的元素插入目标元素前面
|
|
2056
2105
|
let parentElement = element.parentElement;
|
|
@@ -2146,7 +2195,7 @@ System.register('DOMUtils', [], (function (exports) {
|
|
|
2146
2195
|
}
|
|
2147
2196
|
function parseHTMLByCreateDom() {
|
|
2148
2197
|
let tempDIV = DOMUtilsContext.windowApi.document.createElement("div");
|
|
2149
|
-
tempDIV
|
|
2198
|
+
DOMUtilsContext.html(tempDIV, html);
|
|
2150
2199
|
if (isComplete) {
|
|
2151
2200
|
return tempDIV;
|
|
2152
2201
|
}
|