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