@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.iife.js
CHANGED
|
@@ -58,6 +58,30 @@ var DOMUtils = (function () {
|
|
|
58
58
|
isShow(element) {
|
|
59
59
|
return Boolean(element.getClientRects().length);
|
|
60
60
|
},
|
|
61
|
+
/**
|
|
62
|
+
* 在CSP策略下设置innerHTML
|
|
63
|
+
* @param $el 元素
|
|
64
|
+
* @param text 文本
|
|
65
|
+
*/
|
|
66
|
+
setSafeHTML($el, text) {
|
|
67
|
+
// 创建 TrustedHTML 策略(需 CSP 允许)
|
|
68
|
+
try {
|
|
69
|
+
$el.innerHTML = text;
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
// @ts-ignore
|
|
73
|
+
if (globalThis.trustedTypes) {
|
|
74
|
+
// @ts-ignore
|
|
75
|
+
const policy = globalThis.trustedTypes.createPolicy("safe-innerHTML", {
|
|
76
|
+
createHTML: (html) => html,
|
|
77
|
+
});
|
|
78
|
+
$el.innerHTML = policy.createHTML(text);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
throw new Error("trustedTypes is not defined");
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
},
|
|
61
85
|
/**
|
|
62
86
|
* 用于显示元素并获取它的高度宽度等其它属性
|
|
63
87
|
* @param element
|
|
@@ -265,44 +289,45 @@ var DOMUtils = (function () {
|
|
|
265
289
|
* @param event
|
|
266
290
|
*/
|
|
267
291
|
function domUtilsEventCallBack(event) {
|
|
268
|
-
let eventTarget = listenerOption.isComposedPath
|
|
269
|
-
? event.composedPath()[0]
|
|
270
|
-
: event.target;
|
|
271
292
|
if (selectorList.length) {
|
|
272
293
|
/* 存在子元素选择器 */
|
|
294
|
+
// 这时候的this和target都是子元素选择器的元素
|
|
295
|
+
let eventTarget = listenerOption.isComposedPath
|
|
296
|
+
? event.composedPath()[0]
|
|
297
|
+
: event.target;
|
|
273
298
|
let totalParent = DOMUtilsCommonUtils.isWin(elementItem)
|
|
274
299
|
? DOMUtilsContext.windowApi.document.documentElement
|
|
275
300
|
: elementItem;
|
|
276
|
-
|
|
277
|
-
|
|
301
|
+
let findValue = selectorList.find((selectorItem) => {
|
|
302
|
+
// 判断目标元素是否匹配选择器
|
|
278
303
|
if (eventTarget.matches(selectorItem)) {
|
|
279
304
|
/* 当前目标可以被selector所匹配到 */
|
|
280
|
-
|
|
281
|
-
checkOptionOnceToRemoveEventListener();
|
|
282
|
-
break;
|
|
305
|
+
return true;
|
|
283
306
|
}
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
// 这里尝试使用defineProperty修改event的target值
|
|
290
|
-
try {
|
|
291
|
-
OriginPrototype.Object.defineProperty(event, "target", {
|
|
292
|
-
get() {
|
|
293
|
-
return $closestMatches;
|
|
294
|
-
},
|
|
295
|
-
});
|
|
296
|
-
}
|
|
297
|
-
catch (error) { }
|
|
298
|
-
listenerCallBack.call($closestMatches, event, $closestMatches);
|
|
299
|
-
checkOptionOnceToRemoveEventListener();
|
|
300
|
-
break;
|
|
301
|
-
}
|
|
307
|
+
/* 在上层与主元素之间寻找可以被selector所匹配到的 */
|
|
308
|
+
let $closestMatches = eventTarget.closest(selectorItem);
|
|
309
|
+
if ($closestMatches && totalParent.contains($closestMatches)) {
|
|
310
|
+
eventTarget = $closestMatches;
|
|
311
|
+
return true;
|
|
302
312
|
}
|
|
313
|
+
return false;
|
|
314
|
+
});
|
|
315
|
+
if (findValue) {
|
|
316
|
+
// 这里尝试使用defineProperty修改event的target值
|
|
317
|
+
try {
|
|
318
|
+
OriginPrototype.Object.defineProperty(event, "target", {
|
|
319
|
+
get() {
|
|
320
|
+
return eventTarget;
|
|
321
|
+
},
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
catch (error) { }
|
|
325
|
+
listenerCallBack.call(eventTarget, event, eventTarget);
|
|
326
|
+
checkOptionOnceToRemoveEventListener();
|
|
303
327
|
}
|
|
304
328
|
}
|
|
305
329
|
else {
|
|
330
|
+
// 这时候的this指向监听的元素
|
|
306
331
|
listenerCallBack.call(elementItem, event);
|
|
307
332
|
checkOptionOnceToRemoveEventListener();
|
|
308
333
|
}
|
|
@@ -1035,7 +1060,7 @@ var DOMUtils = (function () {
|
|
|
1035
1060
|
super(option);
|
|
1036
1061
|
}
|
|
1037
1062
|
/** 版本号 */
|
|
1038
|
-
version = "
|
|
1063
|
+
version = "2025.3.2";
|
|
1039
1064
|
attr(element, attrName, attrValue) {
|
|
1040
1065
|
let DOMUtilsContext = this;
|
|
1041
1066
|
if (typeof element === "string") {
|
|
@@ -1092,7 +1117,7 @@ var DOMUtils = (function () {
|
|
|
1092
1117
|
let DOMUtilsContext = this;
|
|
1093
1118
|
let tempElement = DOMUtilsContext.windowApi.document.createElement(tagName);
|
|
1094
1119
|
if (typeof property === "string") {
|
|
1095
|
-
tempElement
|
|
1120
|
+
DOMUtilsContext.html(tempElement, property);
|
|
1096
1121
|
return tempElement;
|
|
1097
1122
|
}
|
|
1098
1123
|
if (property == null) {
|
|
@@ -1103,6 +1128,10 @@ var DOMUtils = (function () {
|
|
|
1103
1128
|
}
|
|
1104
1129
|
Object.keys(property).forEach((key) => {
|
|
1105
1130
|
let value = property[key];
|
|
1131
|
+
if (key === "innerHTML") {
|
|
1132
|
+
DOMUtilsContext.html(tempElement, value);
|
|
1133
|
+
return;
|
|
1134
|
+
}
|
|
1106
1135
|
tempElement[key] = value;
|
|
1107
1136
|
});
|
|
1108
1137
|
Object.keys(attributes).forEach((key) => {
|
|
@@ -1271,7 +1300,7 @@ var DOMUtils = (function () {
|
|
|
1271
1300
|
html = html.innerHTML;
|
|
1272
1301
|
}
|
|
1273
1302
|
if ("innerHTML" in element) {
|
|
1274
|
-
element
|
|
1303
|
+
DOMUtilsCommonUtils.setSafeHTML(element, html);
|
|
1275
1304
|
}
|
|
1276
1305
|
}
|
|
1277
1306
|
}
|
|
@@ -1377,7 +1406,12 @@ var DOMUtils = (function () {
|
|
|
1377
1406
|
return Reflect.get(element, propName);
|
|
1378
1407
|
}
|
|
1379
1408
|
else {
|
|
1380
|
-
|
|
1409
|
+
if (element instanceof Element && propName === "innerHTML") {
|
|
1410
|
+
DOMUtilsContext.html(element, propValue);
|
|
1411
|
+
}
|
|
1412
|
+
else {
|
|
1413
|
+
Reflect.set(element, propName, propValue);
|
|
1414
|
+
}
|
|
1381
1415
|
}
|
|
1382
1416
|
}
|
|
1383
1417
|
/**
|
|
@@ -1775,7 +1809,7 @@ var DOMUtils = (function () {
|
|
|
1775
1809
|
});
|
|
1776
1810
|
return;
|
|
1777
1811
|
}
|
|
1778
|
-
element
|
|
1812
|
+
DOMUtilsContext.html(element, "");
|
|
1779
1813
|
}
|
|
1780
1814
|
/**
|
|
1781
1815
|
* 获取元素相对于文档的偏移坐标(加上文档的滚动条)
|
|
@@ -1985,10 +2019,10 @@ var DOMUtils = (function () {
|
|
|
1985
2019
|
if (typeof duration !== "number" || duration <= 0) {
|
|
1986
2020
|
throw new TypeError("duration must be a positive number");
|
|
1987
2021
|
}
|
|
1988
|
-
if (typeof callback !== "function" && callback !==
|
|
2022
|
+
if (typeof callback !== "function" && callback !== undefined) {
|
|
1989
2023
|
throw new TypeError("callback must be a function or null");
|
|
1990
2024
|
}
|
|
1991
|
-
if (typeof styles !== "object" || styles ===
|
|
2025
|
+
if (typeof styles !== "object" || styles === undefined) {
|
|
1992
2026
|
throw new TypeError("styles must be an object");
|
|
1993
2027
|
}
|
|
1994
2028
|
if (Object.keys(styles).length === 0) {
|
|
@@ -2047,7 +2081,7 @@ var DOMUtils = (function () {
|
|
|
2047
2081
|
element = element;
|
|
2048
2082
|
// 创建一个新的div元素,并将wrapperHTML作为其innerHTML
|
|
2049
2083
|
let wrapper = DOMUtilsContext.windowApi.document.createElement("div");
|
|
2050
|
-
wrapper
|
|
2084
|
+
DOMUtilsContext.html(wrapper, wrapperHTML);
|
|
2051
2085
|
let wrapperFirstChild = wrapper.firstChild;
|
|
2052
2086
|
// 将要包裹的元素插入目标元素前面
|
|
2053
2087
|
let parentElement = element.parentElement;
|
|
@@ -2143,7 +2177,7 @@ var DOMUtils = (function () {
|
|
|
2143
2177
|
}
|
|
2144
2178
|
function parseHTMLByCreateDom() {
|
|
2145
2179
|
let tempDIV = DOMUtilsContext.windowApi.document.createElement("div");
|
|
2146
|
-
tempDIV
|
|
2180
|
+
DOMUtilsContext.html(tempDIV, html);
|
|
2147
2181
|
if (isComplete) {
|
|
2148
2182
|
return tempDIV;
|
|
2149
2183
|
}
|