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