@whitesev/domutils 1.6.5 → 1.6.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.
@@ -1,203 +1,203 @@
1
1
  import { WindowApi } from "./WindowApi";
2
2
  import {
3
- clearInterval as WorkerClearInterval,
4
- clearTimeout as WorkerClearTimeout,
5
- setInterval as WorkerSetInterval,
6
- setTimeout as WorkerSetTimeout,
3
+ clearInterval as WorkerClearInterval,
4
+ clearTimeout as WorkerClearTimeout,
5
+ setInterval as WorkerSetInterval,
6
+ setTimeout as WorkerSetTimeout,
7
7
  } from "worker-timers";
8
8
 
9
9
  /** 通用工具类 */
10
10
  export const DOMUtilsCommonUtils = {
11
- windowApi: new WindowApi({
12
- document: document,
13
- window: window,
14
- top: top!,
15
- setTimeout: setTimeout,
16
- clearTimeout: clearTimeout,
17
- setInterval: setInterval,
18
- clearInterval: clearInterval,
19
- }),
20
- /**
21
- * 判断元素是否已显示或已连接
22
- * @param element
23
- */
24
- isShow(element: HTMLElement) {
25
- return Boolean(element.getClientRects().length);
26
- },
27
- /**
28
- * 获取安全的html
29
- */
30
- getSafeHTML(text: string) {
31
- // @ts-ignore
32
- if (globalThis.trustedTypes) {
33
- // @ts-ignore
34
- const policy = globalThis.trustedTypes.createPolicy("safe-innerHTML", {
35
- createHTML: (html: string) => html,
36
- });
37
- return policy.createHTML(text);
38
- } else {
39
- return text;
40
- }
41
- },
42
- /**
43
- * 在CSP策略下设置innerHTML
44
- * @param $el 元素
45
- * @param text 文本
46
- */
47
- setSafeHTML($el: HTMLElement, text: string) {
48
- // 创建 TrustedHTML 策略(需 CSP 允许)
49
- $el.innerHTML = this.getSafeHTML(text);
50
- },
51
- /**
52
- * 用于显示元素并获取它的高度宽度等其它属性
53
- * @param element
54
- */
55
- showElement(element: HTMLElement) {
56
- let dupNode = element.cloneNode(true) as HTMLElement;
57
- dupNode.setAttribute("style", "visibility: hidden !important;display:block !important;");
58
- this.windowApi.document.documentElement.appendChild(dupNode);
59
- return {
60
- /**
61
- * 恢复修改的style
62
- */
63
- recovery() {
64
- dupNode.remove();
65
- },
66
- };
67
- },
68
- /**
69
- * 获取元素上的Float格式的属性px
70
- * @param element
71
- * @param styleName style名
72
- */
73
- getStyleValue(element: HTMLElement | CSSStyleDeclaration, styleName: string) {
74
- let view = null;
75
- let styles = null;
76
- if (element instanceof CSSStyleDeclaration) {
77
- /* 直接就获取了style属性 */
78
- styles = element;
79
- } else {
80
- view = element.ownerDocument.defaultView;
81
- if (!view || !view.opener) {
82
- view = window;
83
- }
84
- styles = view.getComputedStyle(element);
85
- }
86
- let value = parseFloat(styles[styleName as any]);
87
- if (isNaN(value)) {
88
- return 0;
89
- } else {
90
- return value;
91
- }
92
- },
93
- /**
94
- * 判断是否是window,例如window、self、globalThis
95
- * @param target
96
- */
97
- isWin(target: any) {
98
- if (typeof target !== "object") {
99
- return false;
100
- }
101
- if (target instanceof Node) {
102
- return false;
103
- }
104
- if (target === globalThis) {
105
- return true;
106
- }
107
- if (target === window) {
108
- return true;
109
- }
110
- if (target === self) {
111
- return true;
112
- }
113
- if (target === globalThis) {
114
- return true;
115
- }
116
- if (target === window) {
117
- return true;
118
- }
119
- if (target === self) {
120
- return true;
121
- }
122
- if (typeof unsafeWindow !== "undefined" && target === unsafeWindow) {
123
- return true;
124
- }
125
- if (target?.Math?.toString() !== "[object Math]") {
126
- return false;
127
- }
128
- return true;
129
- },
130
- /**
131
- * 删除对象上的属性
132
- * @param target
133
- * @param propName
134
- */
135
- delete(target: any, propName: any) {
136
- if (typeof Reflect === "object" && Reflect.deleteProperty) {
137
- Reflect.deleteProperty(target, propName);
138
- } else {
139
- delete target[propName];
140
- }
141
- },
142
- /**
143
- * 自动使用 Worker 执行 setTimeout
144
- */
145
- setTimeout(callback: Function, timeout: number = 0) {
146
- try {
147
- return WorkerSetTimeout(callback, timeout);
148
- } catch (error) {
149
- return this.windowApi.setTimeout(callback, timeout);
150
- }
151
- },
152
- /**
153
- * 配合 .setTimeout 使用
154
- */
155
- clearTimeout(timeId: number | undefined) {
156
- try {
157
- if (timeId != null) {
158
- WorkerClearTimeout(timeId);
159
- }
160
- } catch (error) {
161
- } finally {
162
- this.windowApi.clearTimeout(timeId);
163
- }
164
- },
165
- /**
166
- * 自动使用 Worker 执行 setInterval
167
- */
168
- setInterval(callback: Function, timeout: number = 0) {
169
- try {
170
- return WorkerSetInterval(callback, timeout);
171
- } catch (error) {
172
- return this.windowApi.setInterval(callback, timeout);
173
- }
174
- },
175
- /**
176
- * 配合 .setInterval 使用
177
- */
178
- clearInterval(timeId: number | undefined) {
179
- try {
180
- if (timeId != null) {
181
- WorkerClearInterval(timeId);
182
- }
183
- } catch (error) {
184
- } finally {
185
- this.windowApi.clearInterval(timeId);
186
- }
187
- },
188
- /**
189
- * 判断是否是元素列表
190
- * @param $ele
191
- */
192
- isNodeList($ele: any): $ele is any[] | NodeList {
193
- return Array.isArray($ele) || $ele instanceof NodeList;
194
- },
195
- /** 获取 animationend 在各个浏览器的兼容名 */
196
- getAnimationEndNameList() {
197
- return ["webkitAnimationEnd", "mozAnimationEnd", "MSAnimationEnd", "oanimationend", "animationend"];
198
- },
199
- /** 获取 transitionend 在各个浏览器的兼容名 */
200
- getTransitionEndNameList() {
201
- return ["webkitTransitionEnd", "mozTransitionEnd", "MSTransitionEnd", "otransitionend", "transitionend"];
202
- },
11
+ windowApi: new WindowApi({
12
+ document: document,
13
+ window: window,
14
+ top: top!,
15
+ setTimeout: setTimeout,
16
+ clearTimeout: clearTimeout,
17
+ setInterval: setInterval,
18
+ clearInterval: clearInterval,
19
+ }),
20
+ /**
21
+ * 判断元素是否已显示或已连接
22
+ * @param element
23
+ */
24
+ isShow(element: HTMLElement) {
25
+ return Boolean(element.getClientRects().length);
26
+ },
27
+ /**
28
+ * 获取安全的html
29
+ */
30
+ getSafeHTML(text: string) {
31
+ if (window.trustedTypes) {
32
+ const policy = window.trustedTypes.createPolicy("safe-innerHTML", {
33
+ createHTML: (html: string) => html,
34
+ });
35
+ return policy.createHTML(text);
36
+ } else {
37
+ return text;
38
+ }
39
+ },
40
+ /**
41
+ * 在CSP策略下设置innerHTML
42
+ * @param $el 元素
43
+ * @param text 文本
44
+ */
45
+ setSafeHTML($el: HTMLElement, text: string) {
46
+ // 创建 TrustedHTML 策略(需 CSP 允许)
47
+ $el.innerHTML = this.getSafeHTML(text);
48
+ },
49
+ /**
50
+ * 用于显示元素并获取它的高度宽度等其它属性
51
+ * @param element
52
+ */
53
+ showElement(element: HTMLElement) {
54
+ const dupNode = element.cloneNode(true) as HTMLElement;
55
+ dupNode.setAttribute("style", "visibility: hidden !important;display:block !important;");
56
+ this.windowApi.document.documentElement.appendChild(dupNode);
57
+ return {
58
+ /**
59
+ * 恢复修改的style
60
+ */
61
+ recovery() {
62
+ dupNode.remove();
63
+ },
64
+ };
65
+ },
66
+ /**
67
+ * 获取元素上的Float格式的属性px
68
+ * @param element
69
+ * @param styleName style名
70
+ */
71
+ getStyleValue(element: HTMLElement | CSSStyleDeclaration, styleName: string) {
72
+ let view = null;
73
+ let styles = null;
74
+ if (element instanceof CSSStyleDeclaration) {
75
+ /* 直接就获取了style属性 */
76
+ styles = element;
77
+ } else {
78
+ view = element.ownerDocument.defaultView;
79
+ if (!view || !view.opener) {
80
+ view = window;
81
+ }
82
+ styles = view.getComputedStyle(element);
83
+ }
84
+ const value = parseFloat(styles[styleName as any]);
85
+ if (isNaN(value)) {
86
+ return 0;
87
+ } else {
88
+ return value;
89
+ }
90
+ },
91
+ /**
92
+ * 判断是否是window,例如window、self、globalThis
93
+ * @param target
94
+ */
95
+ isWin(target: any) {
96
+ if (typeof target !== "object") {
97
+ return false;
98
+ }
99
+ if (target instanceof Node) {
100
+ return false;
101
+ }
102
+ if (target === globalThis) {
103
+ return true;
104
+ }
105
+ if (target === window) {
106
+ return true;
107
+ }
108
+ if (target === self) {
109
+ return true;
110
+ }
111
+ if (target === globalThis) {
112
+ return true;
113
+ }
114
+ if (target === window) {
115
+ return true;
116
+ }
117
+ if (target === self) {
118
+ return true;
119
+ }
120
+ if (typeof unsafeWindow !== "undefined" && target === unsafeWindow) {
121
+ return true;
122
+ }
123
+ if (target?.Math?.toString() !== "[object Math]") {
124
+ return false;
125
+ }
126
+ return true;
127
+ },
128
+ /**
129
+ * 删除对象上的属性
130
+ * @param target
131
+ * @param propName
132
+ */
133
+ delete(target: any, propName: any) {
134
+ if (typeof Reflect === "object" && Reflect.deleteProperty) {
135
+ Reflect.deleteProperty(target, propName);
136
+ } else {
137
+ delete target[propName];
138
+ }
139
+ },
140
+ /**
141
+ * 自动使用 Worker 执行 setTimeout
142
+ */
143
+ setTimeout(callback: (...args: any[]) => any, timeout: number = 0) {
144
+ try {
145
+ return WorkerSetTimeout(callback, timeout);
146
+ } catch {
147
+ return this.windowApi.setTimeout(callback, timeout);
148
+ }
149
+ },
150
+ /**
151
+ * 配合 .setTimeout 使用
152
+ */
153
+ clearTimeout(timeId: number | undefined) {
154
+ try {
155
+ if (timeId != null) {
156
+ WorkerClearTimeout(timeId);
157
+ }
158
+ } catch {
159
+ // TODO
160
+ } finally {
161
+ this.windowApi.clearTimeout(timeId);
162
+ }
163
+ },
164
+ /**
165
+ * 自动使用 Worker 执行 setInterval
166
+ */
167
+ setInterval(callback: (...args: any[]) => any, timeout: number = 0) {
168
+ try {
169
+ return WorkerSetInterval(callback, timeout);
170
+ } catch {
171
+ return this.windowApi.setInterval(callback, timeout);
172
+ }
173
+ },
174
+ /**
175
+ * 配合 .setInterval 使用
176
+ */
177
+ clearInterval(timeId: number | undefined) {
178
+ try {
179
+ if (timeId != null) {
180
+ WorkerClearInterval(timeId);
181
+ }
182
+ } catch {
183
+ // TODO
184
+ } finally {
185
+ this.windowApi.clearInterval(timeId);
186
+ }
187
+ },
188
+ /**
189
+ * 判断是否是元素列表
190
+ * @param $ele
191
+ */
192
+ isNodeList($ele: any): $ele is any[] | NodeList {
193
+ return Array.isArray($ele) || $ele instanceof NodeList;
194
+ },
195
+ /** 获取 animationend 在各个浏览器的兼容名 */
196
+ getAnimationEndNameList() {
197
+ return ["webkitAnimationEnd", "mozAnimationEnd", "MSAnimationEnd", "oanimationend", "animationend"];
198
+ },
199
+ /** 获取 transitionend 在各个浏览器的兼容名 */
200
+ getTransitionEndNameList() {
201
+ return ["webkitTransitionEnd", "mozTransitionEnd", "MSTransitionEnd", "otransitionend", "transitionend"];
202
+ },
203
203
  };
@@ -1,7 +1,7 @@
1
1
  /* 数据 */
2
2
  const DOMUtilsData = {
3
- /** .on添加在元素存储的事件 */
4
- SymbolEvents: Symbol("events_" + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)),
3
+ /** .on添加在元素存储的事件 */
4
+ SymbolEvents: Symbol("events_" + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)),
5
5
  };
6
6
 
7
7
  export { DOMUtilsData };