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