@whitesev/domutils 2.0.4 → 2.0.6

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.umd.js CHANGED
@@ -4,7 +4,7 @@
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.DOMUtils = factory());
5
5
  })(this, (function () { 'use strict';
6
6
 
7
- const version = "2.0.4";
7
+ const version = "2.0.6";
8
8
 
9
9
  class WindowApi {
10
10
  /** 默认的配置 */
@@ -111,15 +111,15 @@
111
111
  * @param $el
112
112
  */
113
113
  forceShow($el) {
114
- const dupNode = $el.cloneNode(true);
115
- dupNode.setAttribute("style", "visibility: hidden !important;display:block !important;");
116
- this.windowApi.document.documentElement.appendChild(dupNode);
114
+ const $clone = $el.cloneNode(true);
115
+ $clone.setAttribute("style", "visibility: hidden !important;display:block !important;");
116
+ this.windowApi.document.documentElement.appendChild($clone);
117
117
  return {
118
118
  /**
119
119
  * 恢复修改的style
120
120
  */
121
121
  recovery() {
122
- dupNode.remove();
122
+ $clone.remove();
123
123
  },
124
124
  };
125
125
  },
@@ -152,37 +152,37 @@
152
152
  },
153
153
  /**
154
154
  * 判断是否是window,例如window、self、globalThis
155
- * @param target
155
+ * @param obj
156
156
  */
157
- isWin(target) {
158
- if (typeof target !== "object") {
157
+ isWin(obj) {
158
+ if (typeof obj !== "object") {
159
159
  return false;
160
160
  }
161
- if (target instanceof Node) {
161
+ if (obj instanceof Node) {
162
162
  return false;
163
163
  }
164
- if (target === globalThis) {
164
+ if (obj === globalThis) {
165
165
  return true;
166
166
  }
167
- if (target === window) {
167
+ if (obj === window) {
168
168
  return true;
169
169
  }
170
- if (target === self) {
170
+ if (obj === self) {
171
171
  return true;
172
172
  }
173
- if (target === globalThis) {
173
+ if (obj === globalThis) {
174
174
  return true;
175
175
  }
176
- if (target === window) {
176
+ if (obj === window) {
177
177
  return true;
178
178
  }
179
- if (target === self) {
179
+ if (obj === self) {
180
180
  return true;
181
181
  }
182
- if (typeof unsafeWindow !== "undefined" && target === unsafeWindow) {
182
+ if (typeof unsafeWindow !== "undefined" && obj === unsafeWindow) {
183
183
  return true;
184
184
  }
185
- if (target?.Math?.toString() !== "[object Math]") {
185
+ if (obj?.Math?.toString() !== "[object Math]") {
186
186
  return false;
187
187
  }
188
188
  return true;
@@ -202,23 +202,23 @@
202
202
  },
203
203
  /**
204
204
  * 删除对象上的属性
205
- * @param target
205
+ * @param obj
206
206
  * @param propName
207
207
  */
208
- delete(target, propName) {
208
+ delete(obj, propName) {
209
209
  if (typeof Reflect === "object" && Reflect != null && Reflect.deleteProperty) {
210
- return Reflect.deleteProperty(target, propName);
210
+ return Reflect.deleteProperty(obj, propName);
211
211
  }
212
212
  else {
213
- delete target[propName];
213
+ delete obj[propName];
214
214
  }
215
215
  },
216
216
  /**
217
217
  * 判断是否是元素列表
218
- * @param $ele
218
+ * @param $el
219
219
  */
220
- isNodeList($ele) {
221
- return Array.isArray($ele) || $ele instanceof NodeList;
220
+ isNodeList($el) {
221
+ return Array.isArray($el) || $el instanceof NodeList;
222
222
  },
223
223
  /** 获取 animationend 在各个浏览器的兼容名 */
224
224
  getAnimationEndNameList() {
@@ -1360,6 +1360,7 @@
1360
1360
  const OriginPrototype = {
1361
1361
  Object: {
1362
1362
  defineProperty: Object.defineProperty,
1363
+ defineProperties: Object.defineProperties,
1363
1364
  },
1364
1365
  };
1365
1366
 
@@ -1395,20 +1396,12 @@
1395
1396
  option.passive = args[startIndex + 2];
1396
1397
  }
1397
1398
  }
1398
- else if (currentParam &&
1399
- typeof currentParam === "object" &&
1400
- ("capture" in currentParam ||
1401
- "once" in currentParam ||
1402
- "passive" in currentParam ||
1403
- "isComposedPath" in currentParam ||
1404
- "overrideTarget" in currentParam ||
1405
- "isPreventEvent" in currentParam)) {
1406
- option.capture = currentParam.capture;
1407
- option.once = currentParam.once;
1408
- option.passive = currentParam.passive;
1409
- option.isComposedPath = currentParam.isComposedPath;
1410
- option.overrideTarget = currentParam.overrideTarget;
1411
- option.isPreventEvent = currentParam.isPreventEvent;
1399
+ else if (currentParam && typeof currentParam === "object") {
1400
+ for (const key in option) {
1401
+ if (Reflect.has(currentParam, key)) {
1402
+ Reflect.set(option, key, currentParam[key]);
1403
+ }
1404
+ }
1412
1405
  }
1413
1406
  return option;
1414
1407
  };
@@ -1528,16 +1521,16 @@
1528
1521
  // 这里尝试使用defineProperty修改event的target值
1529
1522
  try {
1530
1523
  const originTarget = event.target;
1531
- OriginPrototype.Object.defineProperty(event, "target", {
1532
- value: $target,
1533
- get() {
1534
- return $target;
1524
+ OriginPrototype.Object.defineProperties(event, {
1525
+ target: {
1526
+ get() {
1527
+ return $target;
1528
+ },
1535
1529
  },
1536
- });
1537
- OriginPrototype.Object.defineProperty(event, "originTarget", {
1538
- value: $target,
1539
- get() {
1540
- return originTarget;
1530
+ originTarget: {
1531
+ get() {
1532
+ return originTarget;
1533
+ },
1541
1534
  },
1542
1535
  });
1543
1536
  // oxlint-disable-next-line no-empty
@@ -1609,10 +1602,7 @@
1609
1602
  if (typeof currentParam === "boolean") {
1610
1603
  option.capture = currentParam;
1611
1604
  }
1612
- else if (currentParam &&
1613
- typeof currentParam === "object" &&
1614
- currentParam != null &&
1615
- "capture" in currentParam) {
1605
+ else if (currentParam && typeof currentParam === "object" && "capture" in currentParam) {
1616
1606
  option.capture = currentParam.capture;
1617
1607
  }
1618
1608
  return option;
@@ -3327,7 +3317,7 @@
3327
3317
  handler($el, $fragment);
3328
3318
  }
3329
3319
  /**
3330
- * 移除元素
3320
+ * 移除元素(包括它和内部使用.on添加的监听事件)
3331
3321
  * @param $el 目标元素,可以是数组、单个元素、NodeList、元素选择器
3332
3322
  * @example
3333
3323
  * DOMUtils.remove(document.querySelector("a.xx"))
@@ -3336,19 +3326,23 @@
3336
3326
  * DOMUtils.remove([a.xxx, div.xxx, span.xxx])
3337
3327
  * */
3338
3328
  remove($el) {
3339
- const that = this;
3340
3329
  if (typeof $el === "string") {
3341
- $el = that.selectorAll($el);
3330
+ $el = this.selectorAll($el);
3342
3331
  }
3343
3332
  if ($el == null) {
3344
3333
  return;
3345
3334
  }
3346
3335
  if (CommonUtils.isNodeList($el)) {
3347
3336
  $el.forEach(($elItem) => {
3348
- that.remove($elItem);
3337
+ this.remove($elItem);
3349
3338
  });
3350
3339
  return;
3351
3340
  }
3341
+ // 移除事件
3342
+ $el.querySelectorAll("*").forEach(($elItem) => {
3343
+ this.offAll($elItem);
3344
+ });
3345
+ this.offAll($el);
3352
3346
  if (typeof $el.remove === "function") {
3353
3347
  $el.remove();
3354
3348
  }
@@ -3383,7 +3377,7 @@
3383
3377
  return;
3384
3378
  }
3385
3379
  if ($el.innerHTML) {
3386
- $el.innerHTML = "";
3380
+ CommonUtils.setSafeHTML($el, "");
3387
3381
  }
3388
3382
  else if ($el.textContent) {
3389
3383
  $el.textContent = "";
@@ -3581,7 +3575,7 @@
3581
3575
  }
3582
3576
  else {
3583
3577
  that.after($el, $newEl);
3584
- $el.remove();
3578
+ this.remove($el);
3585
3579
  }
3586
3580
  }
3587
3581
  /**