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