@whitesev/domutils 1.8.5 → 1.8.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.
@@ -12,7 +12,7 @@ declare class ElementWait extends ElementSelector {
12
12
  * @param timeout 超时时间,默认0
13
13
  * @param parent (可选)父元素,默认document
14
14
  * @example
15
- * Utils.wait(()=> {
15
+ * DOMUtils.wait(()=> {
16
16
  * let $test = document.querySelector("#test");
17
17
  * return {
18
18
  * success: $test !== null,
@@ -40,26 +40,48 @@ declare class ElementWait extends ElementSelector {
40
40
  */
41
41
  data: T;
42
42
  }, timeout?: number, parent?: Node | Element | Document | HTMLElement): Promise<T | null>;
43
+ /**
44
+ * 等待元素出现
45
+ * @param selectorFn 获取元素的函数
46
+ * @example
47
+ * DOMUtils.waitNode(()=>document.querySelector("div")).then( $div =>{
48
+ * console.log($div); // $div => HTMLDivELement
49
+ * })
50
+ */
51
+ waitNode<K>(selectorFn: () => K | null | undefined): Promise<K>;
43
52
  /**
44
53
  * 等待元素出现
45
54
  * @param selectorFn 获取元素的函数
46
55
  * @param timeout 超时时间,默认0
47
56
  * @example
48
- * Utils.waitNode(()=>document.querySelector("div"), 1000).then( $div =>{
57
+ * DOMUtils.waitNode(()=>document.querySelector("div"), 1000).then( $div =>{
49
58
  * console.log($div); // $div => HTMLDivELement | null
50
59
  * })
51
60
  */
52
- waitNode<K>(selectorFn: () => K | null | undefined): Promise<K>;
53
61
  waitNode<K>(selectorFn: () => K | null | undefined, timeout: number): Promise<K | null | undefined>;
62
+ /**
63
+ * 等待元素出现
64
+ * @param selectorFn 获取元素的函数
65
+ * @param parent 父元素
66
+ * @param timeout 超时时间,默认0
67
+ * @example
68
+ * DOMUtils.waitNode(()=>document.querySelector("div"), document).then( $div =>{
69
+ * console.log($div); // $div => HTMLDivELement
70
+ * })
71
+ * DOMUtils.waitNode(()=>document.querySelector("div"), document, 1000).then( $div =>{
72
+ * console.log($div); // $div => HTMLDivELement | null
73
+ * })
74
+ */
75
+ waitNode<K>(selectorFn: () => K | null | undefined, parent: Node | Element | Document | HTMLElement, timeout?: number): Promise<K | null | undefined>;
54
76
  /**
55
77
  * 等待元素出现
56
78
  * @param selector CSS选择器
57
79
  * @param parent (可选)父元素,默认document
58
80
  * @example
59
- * Utils.waitNode("div").then( $div =>{
81
+ * DOMUtils.waitNode("div").then( $div =>{
60
82
  * console.log($div); // div => HTMLDivELement
61
83
  * })
62
- * Utils.waitNode("div", document).then( $div =>{
84
+ * DOMUtils.waitNode("div", document).then( $div =>{
63
85
  * console.log($div); // div => HTMLDivELement
64
86
  * })
65
87
  */
@@ -70,10 +92,10 @@ declare class ElementWait extends ElementSelector {
70
92
  * @param selectorList CSS选择器数组
71
93
  * @param parent (可选)父元素,默认document
72
94
  * @example
73
- * Utils.waitNode(["div"]).then( ([$div]) =>{
95
+ * DOMUtils.waitNode(["div"]).then( ([$div]) =>{
74
96
  * console.log($div); // div => HTMLDivELement[]
75
97
  * })
76
- * Utils.waitNode(["div"], document).then( ([$div]) =>{
98
+ * DOMUtils.waitNode(["div"], document).then( ([$div]) =>{
77
99
  * console.log($div); // div => HTMLDivELement[]
78
100
  * })
79
101
  */
@@ -85,7 +107,7 @@ declare class ElementWait extends ElementSelector {
85
107
  * @param parent 父元素,默认document
86
108
  * @param timeout 超时时间,默认0
87
109
  * @example
88
- * Utils.waitNode("div", document, 1000).then( $div =>{
110
+ * DOMUtils.waitNode("div", document, 1000).then( $div =>{
89
111
  * console.log($div); // $div => HTMLDivELement | null
90
112
  * })
91
113
  */
@@ -97,7 +119,7 @@ declare class ElementWait extends ElementSelector {
97
119
  * @param parent 父元素,默认document
98
120
  * @param timeout 超时时间,默认0
99
121
  * @example
100
- * Utils.waitNode(["div"], document, 1000).then( ([$div]) =>{
122
+ * DOMUtils.waitNode(["div"], document, 1000).then( ([$div]) =>{
101
123
  * console.log($div); // $div => HTMLDivELement[] | null
102
124
  * })
103
125
  */
@@ -108,7 +130,7 @@ declare class ElementWait extends ElementSelector {
108
130
  * @param selector CSS选择器
109
131
  * @param timeout 超时时间,默认0
110
132
  * @example
111
- * Utils.waitNode("div", 1000).then( $div =>{
133
+ * DOMUtils.waitNode("div", 1000).then( $div =>{
112
134
  * console.log($div); // $div => HTMLDivELement | null
113
135
  * })
114
136
  */
@@ -119,7 +141,7 @@ declare class ElementWait extends ElementSelector {
119
141
  * @param selectorList CSS选择器数组
120
142
  * @param timeout 超时时间,默认0
121
143
  * @example
122
- * Utils.waitNode(["div"], 1000).then( [$div] =>{
144
+ * DOMUtils.waitNode(["div"], 1000).then( [$div] =>{
123
145
  * console.log($div); // $div => HTMLDivELement[] | null
124
146
  * })
125
147
  */
@@ -130,10 +152,10 @@ declare class ElementWait extends ElementSelector {
130
152
  * @param selectorList CSS选择器数组
131
153
  * @param parent (可选)监听的父元素
132
154
  * @example
133
- * Utils.waitAnyNode(["div","div"]).then( $div =>{
155
+ * DOMUtils.waitAnyNode(["div","div"]).then( $div =>{
134
156
  * console.log($div); // $div => HTMLDivELement 这里是第一个
135
157
  * })
136
- * Utils.waitAnyNode(["a","div"], document).then( $a =>{
158
+ * DOMUtils.waitAnyNode(["a","div"], document).then( $a =>{
137
159
  * console.log($a); // $a => HTMLAnchorElement 这里是第一个
138
160
  * })
139
161
  */
@@ -145,7 +167,7 @@ declare class ElementWait extends ElementSelector {
145
167
  * @param parent 父元素,默认document
146
168
  * @param timeout 超时时间,默认0
147
169
  * @example
148
- * Utils.waitAnyNode(["div","div"], document, 10000).then( $div =>{
170
+ * DOMUtils.waitAnyNode(["div","div"], document, 10000).then( $div =>{
149
171
  * console.log($div); // $div => HTMLDivELement | null
150
172
  * })
151
173
  */
@@ -156,7 +178,7 @@ declare class ElementWait extends ElementSelector {
156
178
  * @param selectorList CSS选择器数组
157
179
  * @param timeout 超时时间,默认0
158
180
  * @example
159
- * Utils.waitAnyNode(["div","div"], 10000).then( $div =>{
181
+ * DOMUtils.waitAnyNode(["div","div"], 10000).then( $div =>{
160
182
  * console.log($div); // $div => HTMLDivELement | null
161
183
  * })
162
184
  */
@@ -167,10 +189,10 @@ declare class ElementWait extends ElementSelector {
167
189
  * @param selector CSS选择器
168
190
  * @param parent (可选)监听的父元素
169
191
  * @example
170
- * Utils.waitNodeList("div").then( $result =>{
192
+ * DOMUtils.waitNodeList("div").then( $result =>{
171
193
  * console.log($result); // $result => NodeListOf<HTMLDivElement>
172
194
  * })
173
- * Utils.waitNodeList("div", document).then( $result =>{
195
+ * DOMUtils.waitNodeList("div", document).then( $result =>{
174
196
  * console.log($result); // $result => NodeListOf<HTMLDivElement>
175
197
  * })
176
198
  */
@@ -181,10 +203,10 @@ declare class ElementWait extends ElementSelector {
181
203
  * @param selectorList CSS选择器数组
182
204
  * @param parent (可选)监听的父元素
183
205
  * @example
184
- * Utils.waitNodeList(["div"]).then( $result =>{
206
+ * DOMUtils.waitNodeList(["div"]).then( $result =>{
185
207
  * console.log($result); // $result => NodeListOf<HTMLDivElement>[]
186
208
  * })
187
- * Utils.waitNodeList(["div"], document).then( $result =>{
209
+ * DOMUtils.waitNodeList(["div"], document).then( $result =>{
188
210
  * console.log($result); // $result => NodeListOf<HTMLDivElement>[]
189
211
  * })
190
212
  */
@@ -196,7 +218,7 @@ declare class ElementWait extends ElementSelector {
196
218
  * @param parent 监听的父元素
197
219
  * @param timeout 超时时间,默认0
198
220
  * @example
199
- * Utils.waitNodeList("div", document, 10000).then( $result =>{
221
+ * DOMUtils.waitNodeList("div", document, 10000).then( $result =>{
200
222
  * console.log($result); // $result => NodeListOf<HTMLDivElement> | null
201
223
  * })
202
224
  */
@@ -208,7 +230,7 @@ declare class ElementWait extends ElementSelector {
208
230
  * @param parent 监听的父元素
209
231
  * @param timeout 超时时间,默认0
210
232
  * @example
211
- * Utils.waitNodeList(["div"], document, 10000).then( $result =>{
233
+ * DOMUtils.waitNodeList(["div"], document, 10000).then( $result =>{
212
234
  * console.log($result); // $result => NodeListOf<HTMLDivElement>[] | null
213
235
  * })
214
236
  */
@@ -219,7 +241,7 @@ declare class ElementWait extends ElementSelector {
219
241
  * @param selector CSS选择器数组
220
242
  * @param timeout 超时时间,默认0
221
243
  * @example
222
- * Utils.waitNodeList("div", 10000).then( $result =>{
244
+ * DOMUtils.waitNodeList("div", 10000).then( $result =>{
223
245
  * console.log($result); // $result => NodeListOf<HTMLDivElement> | null
224
246
  * })
225
247
  */
@@ -230,7 +252,7 @@ declare class ElementWait extends ElementSelector {
230
252
  * @param selectorList CSS选择器数组
231
253
  * @param timeout 超时时间,默认0
232
254
  * @example
233
- * Utils.waitNodeList(["div"], 10000).then( $result =>{
255
+ * DOMUtils.waitNodeList(["div"], 10000).then( $result =>{
234
256
  * console.log($result); // $result => NodeListOf<HTMLDivElement>[] | null
235
257
  * })
236
258
  */
@@ -241,10 +263,10 @@ declare class ElementWait extends ElementSelector {
241
263
  * @param selectorList CSS选择器数组
242
264
  * @param parent (可选)监听的父元素
243
265
  * @example
244
- * Utils.waitAnyNodeList(["div","a"]).then( $result =>{
266
+ * DOMUtils.waitAnyNodeList(["div","a"]).then( $result =>{
245
267
  * console.log($result); // $result => NodeListOf<HTMLDivElement>
246
268
  * })
247
- * Utils.waitAnyNodeList(["div","a"], document).then( $result =>{
269
+ * DOMUtils.waitAnyNodeList(["div","a"], document).then( $result =>{
248
270
  * console.log($result); // $result => NodeListOf<HTMLDivElement>
249
271
  * })
250
272
  */
@@ -256,7 +278,7 @@ declare class ElementWait extends ElementSelector {
256
278
  * @param parent 父元素,默认document
257
279
  * @param timeout 超时时间,默认0
258
280
  * @example
259
- * Utils.waitAnyNodeList(["div","a"], document, 10000).then( $result =>{
281
+ * DOMUtils.waitAnyNodeList(["div","a"], document, 10000).then( $result =>{
260
282
  * console.log($result); // $result => NodeListOf<HTMLDivElement> | null
261
283
  * })
262
284
  */
@@ -267,7 +289,7 @@ declare class ElementWait extends ElementSelector {
267
289
  * @param selectorList CSS选择器数组
268
290
  * @param timeout 超时时间,默认0
269
291
  * @example
270
- * Utils.waitAnyNodeList(["div","div"], 10000).then( $result =>{
292
+ * DOMUtils.waitAnyNodeList(["div","div"], 10000).then( $result =>{
271
293
  * console.log($result); // $result => NodeListOf<HTMLDivElement> | null
272
294
  * })
273
295
  */
@@ -1,7 +1,8 @@
1
1
  import type { DOMUtilsCreateElementAttributesMap } from "./types/DOMUtilsEvent";
2
- import { type DOMUtilsTargetElementType } from "./types/global";
2
+ import { type DOMUtilsCSSProperty, type DOMUtilsCSSPropertyType } from "./types/DOMUtilsCSSProperty";
3
3
  import type { WindowApiOption } from "./types/WindowApi";
4
4
  import { ElementHandler } from "./ElementHandler";
5
+ import type { DOMUtilsTargetElementType } from "./types/global";
5
6
  declare class DOMUtils extends ElementHandler {
6
7
  constructor(option?: WindowApiOption);
7
8
  /** 版本号 */
@@ -102,7 +103,7 @@ declare class DOMUtils extends ElementHandler {
102
103
  * DOMUtils.css("a.xx","display");
103
104
  * > "none"
104
105
  * */
105
- css($el: DOMUtilsTargetElementType, property: keyof Omit<CSSStyleDeclaration, "zIndex"> | "z-index"): string;
106
+ css($el: DOMUtilsTargetElementType, property: DOMUtilsCSSPropertyType): string;
106
107
  /**
107
108
  * 获取元素的样式属性值
108
109
  * @param $el 目标元素
@@ -130,7 +131,7 @@ declare class DOMUtils extends ElementHandler {
130
131
  * DOMUtils.css(document.querySelector("a.xx"),"top","10px");
131
132
  * DOMUtils.css(document.querySelector("a.xx"),"top",10);
132
133
  * */
133
- css($el: DOMUtilsTargetElementType, property: (keyof Omit<CSSStyleDeclaration, "zIndex"> | "z-index") & string, value: string | number): string;
134
+ css($el: DOMUtilsTargetElementType, property: DOMUtilsCSSPropertyType & string, value: string | number): string;
134
135
  /**
135
136
  * 设置元素的样式属性
136
137
  * @param $el 目标元素
@@ -145,13 +146,9 @@ declare class DOMUtils extends ElementHandler {
145
146
  * DOMUtils.css(document.querySelector("a.xx"),{ top: "10px" });
146
147
  * DOMUtils.css(document.querySelector("a.xx"),{ top: 10 });
147
148
  * */
148
- css($el: DOMUtilsTargetElementType, property: {
149
- [P in keyof Omit<CSSStyleDeclaration, "zIndex">]?: CSSStyleDeclaration[P];
150
- } | {
151
- "z-index": string | number;
152
- } | {
149
+ css($el: DOMUtilsTargetElementType, property: DOMUtilsCSSProperty | {
153
150
  [key: string]: string | number;
154
- }): string;
151
+ } | string): string;
155
152
  /**
156
153
  * 获取元素的文本内容,优先返回textContent
157
154
  * @param $el 目标元素
@@ -0,0 +1,36 @@
1
+ /**
2
+ * 属性转驼峰
3
+ */
4
+ export type DOMUtilsCamelToKebabCSSProperty<S extends string> = S extends `webkit${infer U}`
5
+ ? `-${Lowercase<"webkit">}${U extends `${infer First}${infer Rest}`
6
+ ? First extends Uppercase<First>
7
+ ? `-${Uncapitalize<First>}${DOMUtilsCamelToKebabCSSProperty<Rest>}`
8
+ : `${First}${DOMUtilsCamelToKebabCSSProperty<Rest>}`
9
+ : U}`
10
+ : S extends `${infer T}${infer U}`
11
+ ? U extends Uncapitalize<U>
12
+ ? `${Uncapitalize<T>}${DOMUtilsCamelToKebabCSSProperty<U>}`
13
+ : `${Uncapitalize<T>}-${DOMUtilsCamelToKebabCSSProperty<U>}`
14
+ : S;
15
+
16
+ export type DOMUtilsCSSPropertyType = DOMUtilsCamelToKebabCSSProperty<
17
+ Extract<
18
+ keyof Omit<
19
+ CSSStyleDeclaration,
20
+ | "cssFloat"
21
+ | "cssText"
22
+ | "length"
23
+ | "parentRule"
24
+ | "getPropertyPriority"
25
+ | "getPropertyValue"
26
+ | "item"
27
+ | "removeProperty"
28
+ | "setProperty"
29
+ >,
30
+ string
31
+ >
32
+ >;
33
+
34
+ export type DOMUtilsCSSProperty = {
35
+ [P in DOMUtilsCSSPropertyType]: string | number;
36
+ };
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": "1.8.5",
4
+ "version": "1.8.7",
5
5
  "type": "module",
6
6
  "description": "适合在浏览器中操作DOM的常用工具类",
7
7
  "main": "dist/index.cjs.js",
@@ -52,7 +52,7 @@
52
52
  "rollup-plugin-clear": "^2.0.7",
53
53
  "tslib": "^2.8.1",
54
54
  "typescript": "^5.9.3",
55
- "typescript-eslint": "^8.50.0"
55
+ "typescript-eslint": "^8.50.1"
56
56
  },
57
57
  "scripts": {
58
58
  "lint": "eslint .",