@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.umd.js
CHANGED
|
@@ -61,6 +61,30 @@
|
|
|
61
61
|
isShow(element) {
|
|
62
62
|
return Boolean(element.getClientRects().length);
|
|
63
63
|
},
|
|
64
|
+
/**
|
|
65
|
+
* 在CSP策略下设置innerHTML
|
|
66
|
+
* @param $el 元素
|
|
67
|
+
* @param text 文本
|
|
68
|
+
*/
|
|
69
|
+
setSafeHTML($el, text) {
|
|
70
|
+
// 创建 TrustedHTML 策略(需 CSP 允许)
|
|
71
|
+
try {
|
|
72
|
+
$el.innerHTML = text;
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
// @ts-ignore
|
|
76
|
+
if (globalThis.trustedTypes) {
|
|
77
|
+
// @ts-ignore
|
|
78
|
+
const policy = globalThis.trustedTypes.createPolicy("safe-innerHTML", {
|
|
79
|
+
createHTML: (html) => html,
|
|
80
|
+
});
|
|
81
|
+
$el.innerHTML = policy.createHTML(text);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
throw new Error("trustedTypes is not defined");
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
},
|
|
64
88
|
/**
|
|
65
89
|
* 用于显示元素并获取它的高度宽度等其它属性
|
|
66
90
|
* @param element
|
|
@@ -268,44 +292,45 @@
|
|
|
268
292
|
* @param event
|
|
269
293
|
*/
|
|
270
294
|
function domUtilsEventCallBack(event) {
|
|
271
|
-
let eventTarget = listenerOption.isComposedPath
|
|
272
|
-
? event.composedPath()[0]
|
|
273
|
-
: event.target;
|
|
274
295
|
if (selectorList.length) {
|
|
275
296
|
/* 存在子元素选择器 */
|
|
297
|
+
// 这时候的this和target都是子元素选择器的元素
|
|
298
|
+
let eventTarget = listenerOption.isComposedPath
|
|
299
|
+
? event.composedPath()[0]
|
|
300
|
+
: event.target;
|
|
276
301
|
let totalParent = DOMUtilsCommonUtils.isWin(elementItem)
|
|
277
302
|
? DOMUtilsContext.windowApi.document.documentElement
|
|
278
303
|
: elementItem;
|
|
279
|
-
|
|
280
|
-
|
|
304
|
+
let findValue = selectorList.find((selectorItem) => {
|
|
305
|
+
// 判断目标元素是否匹配选择器
|
|
281
306
|
if (eventTarget.matches(selectorItem)) {
|
|
282
307
|
/* 当前目标可以被selector所匹配到 */
|
|
283
|
-
|
|
284
|
-
checkOptionOnceToRemoveEventListener();
|
|
285
|
-
break;
|
|
308
|
+
return true;
|
|
286
309
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
// 这里尝试使用defineProperty修改event的target值
|
|
293
|
-
try {
|
|
294
|
-
OriginPrototype.Object.defineProperty(event, "target", {
|
|
295
|
-
get() {
|
|
296
|
-
return $closestMatches;
|
|
297
|
-
},
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
catch (error) { }
|
|
301
|
-
listenerCallBack.call($closestMatches, event, $closestMatches);
|
|
302
|
-
checkOptionOnceToRemoveEventListener();
|
|
303
|
-
break;
|
|
304
|
-
}
|
|
310
|
+
/* 在上层与主元素之间寻找可以被selector所匹配到的 */
|
|
311
|
+
let $closestMatches = eventTarget.closest(selectorItem);
|
|
312
|
+
if ($closestMatches && totalParent.contains($closestMatches)) {
|
|
313
|
+
eventTarget = $closestMatches;
|
|
314
|
+
return true;
|
|
305
315
|
}
|
|
316
|
+
return false;
|
|
317
|
+
});
|
|
318
|
+
if (findValue) {
|
|
319
|
+
// 这里尝试使用defineProperty修改event的target值
|
|
320
|
+
try {
|
|
321
|
+
OriginPrototype.Object.defineProperty(event, "target", {
|
|
322
|
+
get() {
|
|
323
|
+
return eventTarget;
|
|
324
|
+
},
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
catch (error) { }
|
|
328
|
+
listenerCallBack.call(eventTarget, event, eventTarget);
|
|
329
|
+
checkOptionOnceToRemoveEventListener();
|
|
306
330
|
}
|
|
307
331
|
}
|
|
308
332
|
else {
|
|
333
|
+
// 这时候的this指向监听的元素
|
|
309
334
|
listenerCallBack.call(elementItem, event);
|
|
310
335
|
checkOptionOnceToRemoveEventListener();
|
|
311
336
|
}
|
|
@@ -1038,7 +1063,7 @@
|
|
|
1038
1063
|
super(option);
|
|
1039
1064
|
}
|
|
1040
1065
|
/** 版本号 */
|
|
1041
|
-
version = "
|
|
1066
|
+
version = "2025.3.2";
|
|
1042
1067
|
attr(element, attrName, attrValue) {
|
|
1043
1068
|
let DOMUtilsContext = this;
|
|
1044
1069
|
if (typeof element === "string") {
|
|
@@ -1095,7 +1120,7 @@
|
|
|
1095
1120
|
let DOMUtilsContext = this;
|
|
1096
1121
|
let tempElement = DOMUtilsContext.windowApi.document.createElement(tagName);
|
|
1097
1122
|
if (typeof property === "string") {
|
|
1098
|
-
tempElement
|
|
1123
|
+
DOMUtilsContext.html(tempElement, property);
|
|
1099
1124
|
return tempElement;
|
|
1100
1125
|
}
|
|
1101
1126
|
if (property == null) {
|
|
@@ -1106,6 +1131,10 @@
|
|
|
1106
1131
|
}
|
|
1107
1132
|
Object.keys(property).forEach((key) => {
|
|
1108
1133
|
let value = property[key];
|
|
1134
|
+
if (key === "innerHTML") {
|
|
1135
|
+
DOMUtilsContext.html(tempElement, value);
|
|
1136
|
+
return;
|
|
1137
|
+
}
|
|
1109
1138
|
tempElement[key] = value;
|
|
1110
1139
|
});
|
|
1111
1140
|
Object.keys(attributes).forEach((key) => {
|
|
@@ -1274,7 +1303,7 @@
|
|
|
1274
1303
|
html = html.innerHTML;
|
|
1275
1304
|
}
|
|
1276
1305
|
if ("innerHTML" in element) {
|
|
1277
|
-
element
|
|
1306
|
+
DOMUtilsCommonUtils.setSafeHTML(element, html);
|
|
1278
1307
|
}
|
|
1279
1308
|
}
|
|
1280
1309
|
}
|
|
@@ -1380,7 +1409,12 @@
|
|
|
1380
1409
|
return Reflect.get(element, propName);
|
|
1381
1410
|
}
|
|
1382
1411
|
else {
|
|
1383
|
-
|
|
1412
|
+
if (element instanceof Element && propName === "innerHTML") {
|
|
1413
|
+
DOMUtilsContext.html(element, propValue);
|
|
1414
|
+
}
|
|
1415
|
+
else {
|
|
1416
|
+
Reflect.set(element, propName, propValue);
|
|
1417
|
+
}
|
|
1384
1418
|
}
|
|
1385
1419
|
}
|
|
1386
1420
|
/**
|
|
@@ -1778,7 +1812,7 @@
|
|
|
1778
1812
|
});
|
|
1779
1813
|
return;
|
|
1780
1814
|
}
|
|
1781
|
-
element
|
|
1815
|
+
DOMUtilsContext.html(element, "");
|
|
1782
1816
|
}
|
|
1783
1817
|
/**
|
|
1784
1818
|
* 获取元素相对于文档的偏移坐标(加上文档的滚动条)
|
|
@@ -1988,10 +2022,10 @@
|
|
|
1988
2022
|
if (typeof duration !== "number" || duration <= 0) {
|
|
1989
2023
|
throw new TypeError("duration must be a positive number");
|
|
1990
2024
|
}
|
|
1991
|
-
if (typeof callback !== "function" && callback !==
|
|
2025
|
+
if (typeof callback !== "function" && callback !== undefined) {
|
|
1992
2026
|
throw new TypeError("callback must be a function or null");
|
|
1993
2027
|
}
|
|
1994
|
-
if (typeof styles !== "object" || styles ===
|
|
2028
|
+
if (typeof styles !== "object" || styles === undefined) {
|
|
1995
2029
|
throw new TypeError("styles must be an object");
|
|
1996
2030
|
}
|
|
1997
2031
|
if (Object.keys(styles).length === 0) {
|
|
@@ -2050,7 +2084,7 @@
|
|
|
2050
2084
|
element = element;
|
|
2051
2085
|
// 创建一个新的div元素,并将wrapperHTML作为其innerHTML
|
|
2052
2086
|
let wrapper = DOMUtilsContext.windowApi.document.createElement("div");
|
|
2053
|
-
wrapper
|
|
2087
|
+
DOMUtilsContext.html(wrapper, wrapperHTML);
|
|
2054
2088
|
let wrapperFirstChild = wrapper.firstChild;
|
|
2055
2089
|
// 将要包裹的元素插入目标元素前面
|
|
2056
2090
|
let parentElement = element.parentElement;
|
|
@@ -2146,7 +2180,7 @@
|
|
|
2146
2180
|
}
|
|
2147
2181
|
function parseHTMLByCreateDom() {
|
|
2148
2182
|
let tempDIV = DOMUtilsContext.windowApi.document.createElement("div");
|
|
2149
|
-
tempDIV
|
|
2183
|
+
DOMUtilsContext.html(tempDIV, html);
|
|
2150
2184
|
if (isComplete) {
|
|
2151
2185
|
return tempDIV;
|
|
2152
2186
|
}
|