@whitesev/domutils 1.4.7 → 1.5.0
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 +69 -35
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +69 -35
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +69 -35
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +69 -35
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +69 -35
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +69 -35
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/DOMUtilsCommonUtils.d.ts +6 -0
- package/dist/types/src/types/DOMUtilsEvent.d.ts +1 -0
- package/package.json +1 -1
- package/src/DOMUtils.ts +15 -7
- package/src/DOMUtilsCommonUtils.ts +22 -0
- package/src/DOMUtilsEvent.ts +28 -31
- package/src/types/DOMUtilsEvent.d.ts +1 -0
package/dist/index.esm.js
CHANGED
|
@@ -55,6 +55,30 @@ const DOMUtilsCommonUtils = {
|
|
|
55
55
|
isShow(element) {
|
|
56
56
|
return Boolean(element.getClientRects().length);
|
|
57
57
|
},
|
|
58
|
+
/**
|
|
59
|
+
* 在CSP策略下设置innerHTML
|
|
60
|
+
* @param $el 元素
|
|
61
|
+
* @param text 文本
|
|
62
|
+
*/
|
|
63
|
+
setSafeHTML($el, text) {
|
|
64
|
+
// 创建 TrustedHTML 策略(需 CSP 允许)
|
|
65
|
+
try {
|
|
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
|
+
},
|
|
58
82
|
/**
|
|
59
83
|
* 用于显示元素并获取它的高度宽度等其它属性
|
|
60
84
|
* @param element
|
|
@@ -262,44 +286,45 @@ class DOMUtilsEvent {
|
|
|
262
286
|
* @param event
|
|
263
287
|
*/
|
|
264
288
|
function domUtilsEventCallBack(event) {
|
|
265
|
-
let eventTarget = listenerOption.isComposedPath
|
|
266
|
-
? event.composedPath()[0]
|
|
267
|
-
: event.target;
|
|
268
289
|
if (selectorList.length) {
|
|
269
290
|
/* 存在子元素选择器 */
|
|
291
|
+
// 这时候的this和target都是子元素选择器的元素
|
|
292
|
+
let eventTarget = listenerOption.isComposedPath
|
|
293
|
+
? event.composedPath()[0]
|
|
294
|
+
: event.target;
|
|
270
295
|
let totalParent = DOMUtilsCommonUtils.isWin(elementItem)
|
|
271
296
|
? DOMUtilsContext.windowApi.document.documentElement
|
|
272
297
|
: elementItem;
|
|
273
|
-
|
|
274
|
-
|
|
298
|
+
let findValue = selectorList.find((selectorItem) => {
|
|
299
|
+
// 判断目标元素是否匹配选择器
|
|
275
300
|
if (eventTarget.matches(selectorItem)) {
|
|
276
301
|
/* 当前目标可以被selector所匹配到 */
|
|
277
|
-
|
|
278
|
-
checkOptionOnceToRemoveEventListener();
|
|
279
|
-
break;
|
|
302
|
+
return true;
|
|
280
303
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
// 这里尝试使用defineProperty修改event的target值
|
|
287
|
-
try {
|
|
288
|
-
OriginPrototype.Object.defineProperty(event, "target", {
|
|
289
|
-
get() {
|
|
290
|
-
return $closestMatches;
|
|
291
|
-
},
|
|
292
|
-
});
|
|
293
|
-
}
|
|
294
|
-
catch (error) { }
|
|
295
|
-
listenerCallBack.call($closestMatches, event, $closestMatches);
|
|
296
|
-
checkOptionOnceToRemoveEventListener();
|
|
297
|
-
break;
|
|
298
|
-
}
|
|
304
|
+
/* 在上层与主元素之间寻找可以被selector所匹配到的 */
|
|
305
|
+
let $closestMatches = eventTarget.closest(selectorItem);
|
|
306
|
+
if ($closestMatches && totalParent.contains($closestMatches)) {
|
|
307
|
+
eventTarget = $closestMatches;
|
|
308
|
+
return true;
|
|
299
309
|
}
|
|
310
|
+
return false;
|
|
311
|
+
});
|
|
312
|
+
if (findValue) {
|
|
313
|
+
// 这里尝试使用defineProperty修改event的target值
|
|
314
|
+
try {
|
|
315
|
+
OriginPrototype.Object.defineProperty(event, "target", {
|
|
316
|
+
get() {
|
|
317
|
+
return eventTarget;
|
|
318
|
+
},
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
catch (error) { }
|
|
322
|
+
listenerCallBack.call(eventTarget, event, eventTarget);
|
|
323
|
+
checkOptionOnceToRemoveEventListener();
|
|
300
324
|
}
|
|
301
325
|
}
|
|
302
326
|
else {
|
|
327
|
+
// 这时候的this指向监听的元素
|
|
303
328
|
listenerCallBack.call(elementItem, event);
|
|
304
329
|
checkOptionOnceToRemoveEventListener();
|
|
305
330
|
}
|
|
@@ -1032,7 +1057,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1032
1057
|
super(option);
|
|
1033
1058
|
}
|
|
1034
1059
|
/** 版本号 */
|
|
1035
|
-
version = "
|
|
1060
|
+
version = "2025.3.2";
|
|
1036
1061
|
attr(element, attrName, attrValue) {
|
|
1037
1062
|
let DOMUtilsContext = this;
|
|
1038
1063
|
if (typeof element === "string") {
|
|
@@ -1089,7 +1114,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1089
1114
|
let DOMUtilsContext = this;
|
|
1090
1115
|
let tempElement = DOMUtilsContext.windowApi.document.createElement(tagName);
|
|
1091
1116
|
if (typeof property === "string") {
|
|
1092
|
-
tempElement
|
|
1117
|
+
DOMUtilsContext.html(tempElement, property);
|
|
1093
1118
|
return tempElement;
|
|
1094
1119
|
}
|
|
1095
1120
|
if (property == null) {
|
|
@@ -1100,6 +1125,10 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1100
1125
|
}
|
|
1101
1126
|
Object.keys(property).forEach((key) => {
|
|
1102
1127
|
let value = property[key];
|
|
1128
|
+
if (key === "innerHTML") {
|
|
1129
|
+
DOMUtilsContext.html(tempElement, value);
|
|
1130
|
+
return;
|
|
1131
|
+
}
|
|
1103
1132
|
tempElement[key] = value;
|
|
1104
1133
|
});
|
|
1105
1134
|
Object.keys(attributes).forEach((key) => {
|
|
@@ -1268,7 +1297,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1268
1297
|
html = html.innerHTML;
|
|
1269
1298
|
}
|
|
1270
1299
|
if ("innerHTML" in element) {
|
|
1271
|
-
element
|
|
1300
|
+
DOMUtilsCommonUtils.setSafeHTML(element, html);
|
|
1272
1301
|
}
|
|
1273
1302
|
}
|
|
1274
1303
|
}
|
|
@@ -1374,7 +1403,12 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1374
1403
|
return Reflect.get(element, propName);
|
|
1375
1404
|
}
|
|
1376
1405
|
else {
|
|
1377
|
-
|
|
1406
|
+
if (element instanceof Element && propName === "innerHTML") {
|
|
1407
|
+
DOMUtilsContext.html(element, propValue);
|
|
1408
|
+
}
|
|
1409
|
+
else {
|
|
1410
|
+
Reflect.set(element, propName, propValue);
|
|
1411
|
+
}
|
|
1378
1412
|
}
|
|
1379
1413
|
}
|
|
1380
1414
|
/**
|
|
@@ -1772,7 +1806,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1772
1806
|
});
|
|
1773
1807
|
return;
|
|
1774
1808
|
}
|
|
1775
|
-
element
|
|
1809
|
+
DOMUtilsContext.html(element, "");
|
|
1776
1810
|
}
|
|
1777
1811
|
/**
|
|
1778
1812
|
* 获取元素相对于文档的偏移坐标(加上文档的滚动条)
|
|
@@ -1982,10 +2016,10 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1982
2016
|
if (typeof duration !== "number" || duration <= 0) {
|
|
1983
2017
|
throw new TypeError("duration must be a positive number");
|
|
1984
2018
|
}
|
|
1985
|
-
if (typeof callback !== "function" && callback !==
|
|
2019
|
+
if (typeof callback !== "function" && callback !== undefined) {
|
|
1986
2020
|
throw new TypeError("callback must be a function or null");
|
|
1987
2021
|
}
|
|
1988
|
-
if (typeof styles !== "object" || styles ===
|
|
2022
|
+
if (typeof styles !== "object" || styles === undefined) {
|
|
1989
2023
|
throw new TypeError("styles must be an object");
|
|
1990
2024
|
}
|
|
1991
2025
|
if (Object.keys(styles).length === 0) {
|
|
@@ -2044,7 +2078,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
2044
2078
|
element = element;
|
|
2045
2079
|
// 创建一个新的div元素,并将wrapperHTML作为其innerHTML
|
|
2046
2080
|
let wrapper = DOMUtilsContext.windowApi.document.createElement("div");
|
|
2047
|
-
wrapper
|
|
2081
|
+
DOMUtilsContext.html(wrapper, wrapperHTML);
|
|
2048
2082
|
let wrapperFirstChild = wrapper.firstChild;
|
|
2049
2083
|
// 将要包裹的元素插入目标元素前面
|
|
2050
2084
|
let parentElement = element.parentElement;
|
|
@@ -2140,7 +2174,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
2140
2174
|
}
|
|
2141
2175
|
function parseHTMLByCreateDom() {
|
|
2142
2176
|
let tempDIV = DOMUtilsContext.windowApi.document.createElement("div");
|
|
2143
|
-
tempDIV
|
|
2177
|
+
DOMUtilsContext.html(tempDIV, html);
|
|
2144
2178
|
if (isComplete) {
|
|
2145
2179
|
return tempDIV;
|
|
2146
2180
|
}
|