@whitesev/domutils 2.0.5 → 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.
@@ -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() {
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,24 @@ 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
+ this.offAll($elItem);
1086
+ });
1087
+ this.offAll($el);
1084
1088
  if (typeof $el.remove === "function") {
1085
1089
  $el.remove();
1086
1090
  } else if ($el.parentElement) {
@@ -1113,7 +1117,7 @@ class DOMUtils extends ElementHandler {
1113
1117
  return;
1114
1118
  }
1115
1119
  if ($el.innerHTML) {
1116
- $el.innerHTML = "";
1120
+ CommonUtils.setSafeHTML($el, "");
1117
1121
  } else if ($el.textContent) {
1118
1122
  $el.textContent = "";
1119
1123
  }
@@ -1392,7 +1396,7 @@ class DOMUtils extends ElementHandler {
1392
1396
  $parent.replaceChild($newEl as Node, $el);
1393
1397
  } else {
1394
1398
  that.after($el, $newEl as HTMLElement);
1395
- $el.remove();
1399
+ this.remove($el);
1396
1400
  }
1397
1401
  }
1398
1402
  /**