@whitesev/domutils 2.0.5 → 2.0.7

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.
@@ -351,7 +351,7 @@ declare class DOMUtils extends ElementHandler {
351
351
  * */
352
352
  before($el: DOMUtilsTargetElementType, ...args: (HTMLElement | string | Element | DocumentFragment | (HTMLElement | string | Element | DocumentFragment)[] | NodeList)[]): void;
353
353
  /**
354
- * 移除元素
354
+ * 移除元素(包括它和内部使用.on添加的监听事件)
355
355
  * @param $el 目标元素,可以是数组、单个元素、NodeList、元素选择器
356
356
  * @example
357
357
  * DOMUtils.remove(document.querySelector("a.xx"))
@@ -359,7 +359,7 @@ declare class DOMUtils extends ElementHandler {
359
359
  * DOMUtils.remove("a.xx")
360
360
  * DOMUtils.remove([a.xxx, div.xxx, span.xxx])
361
361
  * */
362
- remove($el: DOMUtilsTargetElementType | Element): void;
362
+ remove($el: DOMUtilsTargetElementType | Element | null | undefined): void;
363
363
  /**
364
364
  * 移除元素内所有的子元素
365
365
  * @param $el 目标元素
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@whitesev/domutils",
4
- "version": "2.0.5",
4
+ "version": "2.0.7",
5
5
  "description": "适合在浏览器中操作DOM的常用工具类",
6
6
  "keywords": [
7
7
  "typescript",
@@ -15,7 +15,7 @@ export const CommonUtils = {
15
15
  * 判断元素是否已显示或已连接
16
16
  * @param $el
17
17
  */
18
- isShow($el: HTMLElement) {
18
+ isShow($el: Element) {
19
19
  return Boolean($el.getClientRects().length);
20
20
  },
21
21
  /**
@@ -37,7 +37,7 @@ export const CommonUtils = {
37
37
  * @param $el 元素
38
38
  * @param text 文本
39
39
  */
40
- setSafeHTML($el: HTMLElement, text: string) {
40
+ setSafeHTML($el: Element, text: string) {
41
41
  // 创建 TrustedHTML 策略(需 CSP 允许)
42
42
  $el.innerHTML = this.createSafeHTML(text);
43
43
  },
@@ -45,16 +45,16 @@ export const CommonUtils = {
45
45
  * 用于强制显示元素并获取它的高度宽度等其它属性
46
46
  * @param $el
47
47
  */
48
- forceShow($el: HTMLElement) {
49
- const dupNode = $el.cloneNode(true) as HTMLElement;
50
- dupNode.setAttribute("style", "visibility: hidden !important;display:block !important;");
51
- this.windowApi.document.documentElement.appendChild(dupNode);
48
+ forceShow($el: Element) {
49
+ const $clone = $el.cloneNode(true) as HTMLElement;
50
+ $clone.setAttribute("style", "visibility: hidden !important;display:block !important;");
51
+ this.windowApi.document.documentElement.appendChild($clone);
52
52
  return {
53
53
  /**
54
54
  * 恢复修改的style
55
55
  */
56
56
  recovery() {
57
- dupNode.remove();
57
+ $clone.remove();
58
58
  },
59
59
  };
60
60
  },
@@ -63,7 +63,7 @@ export const CommonUtils = {
63
63
  * @param element
64
64
  * @param styleName style名
65
65
  */
66
- getStyleValue(element: HTMLElement | CSSStyleDeclaration, styleName: string) {
66
+ getStyleValue(element: Element | CSSStyleDeclaration, styleName: string) {
67
67
  let view = null;
68
68
  let styles = null;
69
69
  if (element instanceof CSSStyleDeclaration) {
@@ -85,37 +85,37 @@ export const CommonUtils = {
85
85
  },
86
86
  /**
87
87
  * 判断是否是window,例如window、self、globalThis
88
- * @param target
88
+ * @param obj
89
89
  */
90
- isWin(target: any) {
91
- if (typeof target !== "object") {
90
+ isWin(obj: any) {
91
+ if (typeof obj !== "object") {
92
92
  return false;
93
93
  }
94
- if (target instanceof Node) {
94
+ if (obj instanceof Node) {
95
95
  return false;
96
96
  }
97
- if (target === globalThis) {
97
+ if (obj === globalThis) {
98
98
  return true;
99
99
  }
100
- if (target === window) {
100
+ if (obj === window) {
101
101
  return true;
102
102
  }
103
- if (target === self) {
103
+ if (obj === self) {
104
104
  return true;
105
105
  }
106
- if (target === globalThis) {
106
+ if (obj === globalThis) {
107
107
  return true;
108
108
  }
109
- if (target === window) {
109
+ if (obj === window) {
110
110
  return true;
111
111
  }
112
- if (target === self) {
112
+ if (obj === self) {
113
113
  return true;
114
114
  }
115
- if (typeof unsafeWindow !== "undefined" && target === unsafeWindow) {
115
+ if (typeof unsafeWindow !== "undefined" && obj === unsafeWindow) {
116
116
  return true;
117
117
  }
118
- if (target?.Math?.toString() !== "[object Math]") {
118
+ if (obj?.Math?.toString() !== "[object Math]") {
119
119
  return false;
120
120
  }
121
121
  return true;
@@ -135,22 +135,22 @@ export const CommonUtils = {
135
135
  },
136
136
  /**
137
137
  * 删除对象上的属性
138
- * @param target
138
+ * @param obj
139
139
  * @param propName
140
140
  */
141
- delete(target: any, propName: any) {
141
+ delete(obj: any, propName: any) {
142
142
  if (typeof Reflect === "object" && Reflect != null && Reflect.deleteProperty) {
143
- return Reflect.deleteProperty(target, propName);
143
+ return Reflect.deleteProperty(obj, propName);
144
144
  } else {
145
- delete target[propName];
145
+ delete obj[propName];
146
146
  }
147
147
  },
148
148
  /**
149
149
  * 判断是否是元素列表
150
- * @param $ele
150
+ * @param $el
151
151
  */
152
- isNodeList($ele: any): $ele is any[] | NodeList {
153
- return Array.isArray($ele) || $ele instanceof NodeList;
152
+ isNodeList($el: any): $el is any[] | NodeList {
153
+ return Array.isArray($el) || $el instanceof NodeList;
154
154
  },
155
155
  /** 获取 animationend 在各个浏览器的兼容名 */
156
156
  getAnimationEndNameList() {
@@ -631,19 +631,19 @@ class ElementEvent extends ElementAnimate {
631
631
  /**
632
632
  * 取消绑定所有的事件
633
633
  * @param element 需要取消绑定的元素|元素数组
634
- * @param eventType (可选)需要取消监听的事件
634
+ * @param eventType (可选)需要取消监听的事件,不传入该参数则遍历所有监听的事件
635
635
  */
636
636
  offAll(element: DOMUtilsElementEventType, eventType?: string): void;
637
637
  /**
638
638
  * 取消绑定所有的事件
639
639
  * @param element 需要取消绑定的元素|元素数组
640
- * @param eventType (可选)需要取消监听的事件
640
+ * @param eventType (可选)需要取消监听的事件,不传入该参数则遍历所有监听的事件
641
641
  */
642
642
  offAll(element: DOMUtilsElementEventType, eventType?: DOMUtils_EventType | DOMUtils_EventType[]): void;
643
643
  /**
644
644
  * 取消绑定所有的事件
645
645
  * @param element 需要取消绑定的元素|元素数组
646
- * @param eventType (可选)需要取消监听的事件
646
+ * @param eventType (可选)需要取消监听的事件,不传入该参数则遍历所有监听的事件
647
647
  */
648
648
  offAll(element: DOMUtilsElementEventType, eventType?: DOMUtils_EventType | DOMUtils_EventType[] | string) {
649
649
  const that = this;
package/src/index.ts CHANGED
@@ -1059,7 +1059,7 @@ class DOMUtils extends ElementHandler {
1059
1059
  handler($el, $fragment);
1060
1060
  }
1061
1061
  /**
1062
- * 移除元素
1062
+ * 移除元素(包括它和内部使用.on添加的监听事件)
1063
1063
  * @param $el 目标元素,可以是数组、单个元素、NodeList、元素选择器
1064
1064
  * @example
1065
1065
  * DOMUtils.remove(document.querySelector("a.xx"))
@@ -1067,20 +1067,25 @@ class DOMUtils extends ElementHandler {
1067
1067
  * DOMUtils.remove("a.xx")
1068
1068
  * DOMUtils.remove([a.xxx, div.xxx, span.xxx])
1069
1069
  * */
1070
- remove($el: DOMUtilsTargetElementType | Element) {
1071
- const that = this;
1070
+ remove($el: DOMUtilsTargetElementType | Element | null | undefined) {
1072
1071
  if (typeof $el === "string") {
1073
- $el = that.selectorAll($el);
1072
+ $el = this.selectorAll($el);
1074
1073
  }
1075
1074
  if ($el == null) {
1076
1075
  return;
1077
1076
  }
1078
1077
  if (CommonUtils.isNodeList($el)) {
1079
1078
  $el.forEach(($elItem) => {
1080
- that.remove($elItem as HTMLElement);
1079
+ this.remove($elItem as HTMLElement);
1081
1080
  });
1082
1081
  return;
1083
1082
  }
1083
+ // 移除事件
1084
+ $el.querySelectorAll("*").forEach(($elItem) => {
1085
+ if (!($elItem instanceof Element)) return;
1086
+ this.offAll($elItem);
1087
+ });
1088
+ this.offAll($el);
1084
1089
  if (typeof $el.remove === "function") {
1085
1090
  $el.remove();
1086
1091
  } else if ($el.parentElement) {
@@ -1113,7 +1118,7 @@ class DOMUtils extends ElementHandler {
1113
1118
  return;
1114
1119
  }
1115
1120
  if ($el.innerHTML) {
1116
- $el.innerHTML = "";
1121
+ CommonUtils.setSafeHTML($el, "");
1117
1122
  } else if ($el.textContent) {
1118
1123
  $el.textContent = "";
1119
1124
  }
@@ -1392,7 +1397,7 @@ class DOMUtils extends ElementHandler {
1392
1397
  $parent.replaceChild($newEl as Node, $el);
1393
1398
  } else {
1394
1399
  that.after($el, $newEl as HTMLElement);
1395
- $el.remove();
1400
+ this.remove($el);
1396
1401
  }
1397
1402
  }
1398
1403
  /**