@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.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- const version = "2.0.4";
1
+ const version = "2.0.6";
2
2
 
3
3
  class WindowApi {
4
4
  /** 默认的配置 */
@@ -105,15 +105,15 @@ const CommonUtils = {
105
105
  * @param $el
106
106
  */
107
107
  forceShow($el) {
108
- const dupNode = $el.cloneNode(true);
109
- dupNode.setAttribute("style", "visibility: hidden !important;display:block !important;");
110
- this.windowApi.document.documentElement.appendChild(dupNode);
108
+ const $clone = $el.cloneNode(true);
109
+ $clone.setAttribute("style", "visibility: hidden !important;display:block !important;");
110
+ this.windowApi.document.documentElement.appendChild($clone);
111
111
  return {
112
112
  /**
113
113
  * 恢复修改的style
114
114
  */
115
115
  recovery() {
116
- dupNode.remove();
116
+ $clone.remove();
117
117
  },
118
118
  };
119
119
  },
@@ -146,37 +146,37 @@ const CommonUtils = {
146
146
  },
147
147
  /**
148
148
  * 判断是否是window,例如window、self、globalThis
149
- * @param target
149
+ * @param obj
150
150
  */
151
- isWin(target) {
152
- if (typeof target !== "object") {
151
+ isWin(obj) {
152
+ if (typeof obj !== "object") {
153
153
  return false;
154
154
  }
155
- if (target instanceof Node) {
155
+ if (obj instanceof Node) {
156
156
  return false;
157
157
  }
158
- if (target === globalThis) {
158
+ if (obj === globalThis) {
159
159
  return true;
160
160
  }
161
- if (target === window) {
161
+ if (obj === window) {
162
162
  return true;
163
163
  }
164
- if (target === self) {
164
+ if (obj === self) {
165
165
  return true;
166
166
  }
167
- if (target === globalThis) {
167
+ if (obj === globalThis) {
168
168
  return true;
169
169
  }
170
- if (target === window) {
170
+ if (obj === window) {
171
171
  return true;
172
172
  }
173
- if (target === self) {
173
+ if (obj === self) {
174
174
  return true;
175
175
  }
176
- if (typeof unsafeWindow !== "undefined" && target === unsafeWindow) {
176
+ if (typeof unsafeWindow !== "undefined" && obj === unsafeWindow) {
177
177
  return true;
178
178
  }
179
- if (target?.Math?.toString() !== "[object Math]") {
179
+ if (obj?.Math?.toString() !== "[object Math]") {
180
180
  return false;
181
181
  }
182
182
  return true;
@@ -196,23 +196,23 @@ const CommonUtils = {
196
196
  },
197
197
  /**
198
198
  * 删除对象上的属性
199
- * @param target
199
+ * @param obj
200
200
  * @param propName
201
201
  */
202
- delete(target, propName) {
202
+ delete(obj, propName) {
203
203
  if (typeof Reflect === "object" && Reflect != null && Reflect.deleteProperty) {
204
- return Reflect.deleteProperty(target, propName);
204
+ return Reflect.deleteProperty(obj, propName);
205
205
  }
206
206
  else {
207
- delete target[propName];
207
+ delete obj[propName];
208
208
  }
209
209
  },
210
210
  /**
211
211
  * 判断是否是元素列表
212
- * @param $ele
212
+ * @param $el
213
213
  */
214
- isNodeList($ele) {
215
- return Array.isArray($ele) || $ele instanceof NodeList;
214
+ isNodeList($el) {
215
+ return Array.isArray($el) || $el instanceof NodeList;
216
216
  },
217
217
  /** 获取 animationend 在各个浏览器的兼容名 */
218
218
  getAnimationEndNameList() {
@@ -1354,6 +1354,7 @@ const GlobalData = {
1354
1354
  const OriginPrototype = {
1355
1355
  Object: {
1356
1356
  defineProperty: Object.defineProperty,
1357
+ defineProperties: Object.defineProperties,
1357
1358
  },
1358
1359
  };
1359
1360
 
@@ -1389,20 +1390,12 @@ class ElementEvent extends ElementAnimate {
1389
1390
  option.passive = args[startIndex + 2];
1390
1391
  }
1391
1392
  }
1392
- else if (currentParam &&
1393
- typeof currentParam === "object" &&
1394
- ("capture" in currentParam ||
1395
- "once" in currentParam ||
1396
- "passive" in currentParam ||
1397
- "isComposedPath" in currentParam ||
1398
- "overrideTarget" in currentParam ||
1399
- "isPreventEvent" in currentParam)) {
1400
- option.capture = currentParam.capture;
1401
- option.once = currentParam.once;
1402
- option.passive = currentParam.passive;
1403
- option.isComposedPath = currentParam.isComposedPath;
1404
- option.overrideTarget = currentParam.overrideTarget;
1405
- option.isPreventEvent = currentParam.isPreventEvent;
1393
+ else if (currentParam && typeof currentParam === "object") {
1394
+ for (const key in option) {
1395
+ if (Reflect.has(currentParam, key)) {
1396
+ Reflect.set(option, key, currentParam[key]);
1397
+ }
1398
+ }
1406
1399
  }
1407
1400
  return option;
1408
1401
  };
@@ -1522,16 +1515,16 @@ class ElementEvent extends ElementAnimate {
1522
1515
  // 这里尝试使用defineProperty修改event的target值
1523
1516
  try {
1524
1517
  const originTarget = event.target;
1525
- OriginPrototype.Object.defineProperty(event, "target", {
1526
- value: $target,
1527
- get() {
1528
- return $target;
1518
+ OriginPrototype.Object.defineProperties(event, {
1519
+ target: {
1520
+ get() {
1521
+ return $target;
1522
+ },
1529
1523
  },
1530
- });
1531
- OriginPrototype.Object.defineProperty(event, "originTarget", {
1532
- value: $target,
1533
- get() {
1534
- return originTarget;
1524
+ originTarget: {
1525
+ get() {
1526
+ return originTarget;
1527
+ },
1535
1528
  },
1536
1529
  });
1537
1530
  // oxlint-disable-next-line no-empty
@@ -1603,10 +1596,7 @@ class ElementEvent extends ElementAnimate {
1603
1596
  if (typeof currentParam === "boolean") {
1604
1597
  option.capture = currentParam;
1605
1598
  }
1606
- else if (currentParam &&
1607
- typeof currentParam === "object" &&
1608
- currentParam != null &&
1609
- "capture" in currentParam) {
1599
+ else if (currentParam && typeof currentParam === "object" && "capture" in currentParam) {
1610
1600
  option.capture = currentParam.capture;
1611
1601
  }
1612
1602
  return option;
@@ -3321,7 +3311,7 @@ class DOMUtils extends ElementHandler {
3321
3311
  handler($el, $fragment);
3322
3312
  }
3323
3313
  /**
3324
- * 移除元素
3314
+ * 移除元素(包括它和内部使用.on添加的监听事件)
3325
3315
  * @param $el 目标元素,可以是数组、单个元素、NodeList、元素选择器
3326
3316
  * @example
3327
3317
  * DOMUtils.remove(document.querySelector("a.xx"))
@@ -3330,19 +3320,23 @@ class DOMUtils extends ElementHandler {
3330
3320
  * DOMUtils.remove([a.xxx, div.xxx, span.xxx])
3331
3321
  * */
3332
3322
  remove($el) {
3333
- const that = this;
3334
3323
  if (typeof $el === "string") {
3335
- $el = that.selectorAll($el);
3324
+ $el = this.selectorAll($el);
3336
3325
  }
3337
3326
  if ($el == null) {
3338
3327
  return;
3339
3328
  }
3340
3329
  if (CommonUtils.isNodeList($el)) {
3341
3330
  $el.forEach(($elItem) => {
3342
- that.remove($elItem);
3331
+ this.remove($elItem);
3343
3332
  });
3344
3333
  return;
3345
3334
  }
3335
+ // 移除事件
3336
+ $el.querySelectorAll("*").forEach(($elItem) => {
3337
+ this.offAll($elItem);
3338
+ });
3339
+ this.offAll($el);
3346
3340
  if (typeof $el.remove === "function") {
3347
3341
  $el.remove();
3348
3342
  }
@@ -3377,7 +3371,7 @@ class DOMUtils extends ElementHandler {
3377
3371
  return;
3378
3372
  }
3379
3373
  if ($el.innerHTML) {
3380
- $el.innerHTML = "";
3374
+ CommonUtils.setSafeHTML($el, "");
3381
3375
  }
3382
3376
  else if ($el.textContent) {
3383
3377
  $el.textContent = "";
@@ -3575,7 +3569,7 @@ class DOMUtils extends ElementHandler {
3575
3569
  }
3576
3570
  else {
3577
3571
  that.after($el, $newEl);
3578
- $el.remove();
3572
+ this.remove($el);
3579
3573
  }
3580
3574
  }
3581
3575
  /**