@tb-dev/prototype-dom 6.1.0 → 6.1.2
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.
- package/dist/index.d.ts +146 -23
- package/dist/index.js +195 -256
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,37 +1,160 @@
|
|
|
1
1
|
declare global {
|
|
2
|
-
interface ArrayConstructor {
|
|
3
|
-
fromElements: <T extends Element[] | NodeList | string, V>(source: T, valueSelector: (element: Element) => V) => V[];
|
|
4
|
-
}
|
|
5
2
|
interface Document {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Return the first element that is a descendant of node that matches selector.
|
|
5
|
+
* @param selectors CSS selector to match.
|
|
6
|
+
*
|
|
7
|
+
* @throws {Error} If no element is found.
|
|
8
|
+
*/
|
|
9
|
+
queryStrict: <T extends Element = Element>(selectors: string) => T;
|
|
10
|
+
/**
|
|
11
|
+
* Query all element descendants of node that match selector, then create an {@link Array} from the result.
|
|
12
|
+
*
|
|
13
|
+
* The values of the array can be customized by providing a `valueFn` function.
|
|
14
|
+
* Defaults to the element itself.
|
|
15
|
+
* @param selector CSS selector to match.
|
|
16
|
+
*/
|
|
17
|
+
queryAsArray: <T = Element>(selector: string, valueFn?: (element: Element) => T) => T[];
|
|
18
|
+
/**
|
|
19
|
+
* Query all element descendants of node that match selector, then create a {@link Set} from the result.
|
|
20
|
+
*
|
|
21
|
+
* The values of the set can be customized by providing a `valueFn` function.
|
|
22
|
+
* Defaults to the elements themselves.
|
|
23
|
+
* @param selector CSS selector to match.
|
|
24
|
+
*/
|
|
25
|
+
queryAsSet: <T = Element>(selector: string, valueFn?: (element: Element) => T) => Set<T>;
|
|
26
|
+
/**
|
|
27
|
+
* Query all element descendants of node that match selector, then create a {@link Map} from the result.
|
|
28
|
+
*
|
|
29
|
+
* The keys of the map are determined by the required `keyFn` function.
|
|
30
|
+
*
|
|
31
|
+
* The values can be customized by providing a `valueFn` function. Default to the elements themselves.
|
|
32
|
+
* @param selector CSS selector to match.
|
|
33
|
+
* @param keyFn Function that returns the key for each element.
|
|
34
|
+
* @param valueFn Function that returns the value for each element.
|
|
35
|
+
*/
|
|
36
|
+
queryAsMap: <T extends Element, K, V = T>(selector: string, keyFn: (element: T) => K, valueFn?: (element: T) => V) => Map<K, V>;
|
|
37
|
+
/**
|
|
38
|
+
* Wait for the first descendant element that matches the given selector to exist.
|
|
39
|
+
* @param selector CSS selector to match.
|
|
40
|
+
* @param timeout Maximum time to wait for the element, in milliseconds.
|
|
41
|
+
*/
|
|
10
42
|
waitChild: (selector: string, timeout?: number) => Promise<Element>;
|
|
11
43
|
}
|
|
12
44
|
interface Element {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
45
|
+
/**
|
|
46
|
+
* Return the first attribute whose qualified name is `name`.
|
|
47
|
+
* @param name Attribute to search for.
|
|
48
|
+
*
|
|
49
|
+
* @throws {Error} If the attribute is not found.
|
|
50
|
+
*/
|
|
51
|
+
getAttributeStrict: (name: string) => string;
|
|
52
|
+
/**
|
|
53
|
+
* Get the first attribute whose qualified name is `name`,
|
|
54
|
+
* then tries to parse the attribute as a floating point number.
|
|
55
|
+
* @param name Attribute to search for.
|
|
56
|
+
*
|
|
57
|
+
* @throws {Error} If the attribute is not found.
|
|
58
|
+
* @throws {TypeError} If the attribute cannot be parsed as a floating point number.
|
|
59
|
+
*/
|
|
60
|
+
getAttributeAsFloatStrict: (name: string) => number;
|
|
61
|
+
/**
|
|
62
|
+
* Get the first attribute whose qualified name is `name`,
|
|
63
|
+
* then tries to parse the attribute as an integer using the specified radix.
|
|
64
|
+
* @param name Attribute to search for.
|
|
65
|
+
* @param radix A value between 2 and 36 that specifies the base of the number.
|
|
66
|
+
*
|
|
67
|
+
* @throws {Error} If the attribute is not found.
|
|
68
|
+
* @throws {TypeError} If the attribute cannot be parsed as an integer.
|
|
69
|
+
*/
|
|
70
|
+
getAttributeAsIntStrict: (name: string, radix?: number) => number;
|
|
71
|
+
/**
|
|
72
|
+
* Return the text content of the element.
|
|
73
|
+
*
|
|
74
|
+
* @throws {Error} If the element has no text content.
|
|
75
|
+
*/
|
|
76
|
+
getTextStrict: () => string;
|
|
77
|
+
/**
|
|
78
|
+
* Get the text content of the element, then tries to parse it as an integer.
|
|
79
|
+
* @param radix A value between 2 and 36 that specifies the base of the number.
|
|
80
|
+
*
|
|
81
|
+
* @throws {Error} If the element has no text content.
|
|
82
|
+
* @throws {TypeError} If the text content cannot be parsed as an integer.
|
|
83
|
+
*/
|
|
84
|
+
getTextAsIntStrict: (radix?: number) => number;
|
|
85
|
+
/**
|
|
86
|
+
* Get the text content of the element, then tries to parse it as a floating point number.
|
|
87
|
+
*
|
|
88
|
+
* @throws {Error} If the element has no text content.
|
|
89
|
+
* @throws {TypeError} If the text content cannot be parsed as a floating point number.
|
|
90
|
+
*/
|
|
91
|
+
getTextAsFloatStrict: () => number;
|
|
92
|
+
/**
|
|
93
|
+
* Query all element descendants of node that match selector, then create an {@link Array} from the result.
|
|
94
|
+
*
|
|
95
|
+
* The values of the array can be customized by providing a `valueFn` function.
|
|
96
|
+
* Defaults to the element itself.
|
|
97
|
+
* @param selector CSS selector to match.
|
|
98
|
+
*/
|
|
99
|
+
queryAsArray: <T = Element>(selector: string, valueFn?: (element: Element) => T) => T[];
|
|
100
|
+
/**
|
|
101
|
+
* Query all element descendants of node that match selector, then create a {@link Set} from the result.
|
|
102
|
+
*
|
|
103
|
+
* The values of the set can be customized by providing a `valueFn` function.
|
|
104
|
+
* Defaults to the elements themselves.
|
|
105
|
+
* @param selector CSS selector to match.
|
|
106
|
+
*/
|
|
107
|
+
queryAsSet: <T = Element>(selector: string, valueFn?: (element: Element) => T) => Set<T>;
|
|
108
|
+
/**
|
|
109
|
+
* Return the first element that is a descendant of node that matches selector.
|
|
110
|
+
* @param selectors CSS selector to match.
|
|
111
|
+
*
|
|
112
|
+
* @throws {Error} If no element is found.
|
|
113
|
+
*/
|
|
21
114
|
queryStrict: <T extends Element>(selector: string) => T;
|
|
22
|
-
|
|
115
|
+
/**
|
|
116
|
+
* Query all element descendants of node that match selector, then create a {@link Map} from the result.
|
|
117
|
+
*
|
|
118
|
+
* The keys of the map are determined by the required `keyFn` function.
|
|
119
|
+
*
|
|
120
|
+
* The values can be customized by providing a `valueFn` function. Default to the elements themselves.
|
|
121
|
+
* @param selector CSS selector to match.
|
|
122
|
+
* @param keyFn Function that returns the key for each element.
|
|
123
|
+
* @param valueFn Function that returns the value for each element.
|
|
124
|
+
*/
|
|
125
|
+
queryAsMap: <T extends Element, K, V = T>(selector: string, keyFn: (element: T) => K, valueFn?: (element: T) => V) => Map<K, V>;
|
|
126
|
+
/**
|
|
127
|
+
* Wait for the first descendant element that matches the given selector to exist.
|
|
128
|
+
* @param selector CSS selector to match.
|
|
129
|
+
* @param timeout Maximum time to wait for the element, in milliseconds.
|
|
130
|
+
*/
|
|
23
131
|
waitChild: (selector: string, timeout?: number) => Promise<Element>;
|
|
24
132
|
}
|
|
25
133
|
interface URLSearchParams {
|
|
26
|
-
|
|
134
|
+
/**
|
|
135
|
+
* Return the first value associated to the given search parameter.
|
|
136
|
+
* @param name The name of the search parameter to get.
|
|
137
|
+
*
|
|
138
|
+
* @throws {Error} If the search parameter is not found.
|
|
139
|
+
*/
|
|
140
|
+
getStrict: (name: string) => string;
|
|
141
|
+
/**
|
|
142
|
+
* Get the first value associated to the given search parameter, then tries to parse it as an integer.
|
|
143
|
+
* The returned value may be `NaN` if it cannot be parsed as an integer.
|
|
144
|
+
* To throw an error if the value cannot be parsed, use {@link getAsIntegerStrict} instead.
|
|
145
|
+
* @param name The name of the search parameter to get.
|
|
146
|
+
* @param radix A value between 2 and 36 that specifies the base of the number.
|
|
147
|
+
*/
|
|
27
148
|
getAsInteger: (name: string, radix?: number) => number | null;
|
|
149
|
+
/**
|
|
150
|
+
* Get the first value associated to the given search parameter, then tries to parse it as an integer.
|
|
151
|
+
* @param name The name of the search parameter to get.
|
|
152
|
+
* @param radix A value between 2 and 36 that specifies the base of the number.
|
|
153
|
+
*
|
|
154
|
+
* @throws {Error} If the search parameter is not found.
|
|
155
|
+
* @throws {TypeError} If the value cannot be parsed as an integer.
|
|
156
|
+
*/
|
|
28
157
|
getAsIntegerStrict: (name: string, radix?: number) => number;
|
|
29
158
|
}
|
|
30
|
-
interface MapConstructor {
|
|
31
|
-
fromElements: <T extends Element[] | NodeList | string, K, V>(source: T, keySelector: (element: Element) => K, valueSelector: (element: Element) => V) => Map<K, V>;
|
|
32
|
-
}
|
|
33
|
-
interface SetConstructor {
|
|
34
|
-
fromElements: <T extends Element[] | NodeList | string, K>(source: T, valueSelector: (element: Element) => K) => Set<K>;
|
|
35
|
-
}
|
|
36
159
|
}
|
|
37
160
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,273 +1,212 @@
|
|
|
1
1
|
(function () {
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
};
|
|
10
|
-
Document.prototype.queryAsArray = function (selector, valueSelector) {
|
|
11
|
-
valueSelector ??= (element) => element;
|
|
12
|
-
const elements = this.querySelectorAll(selector);
|
|
13
|
-
return Array.from(elements, valueSelector);
|
|
14
|
-
};
|
|
15
|
-
Document.prototype.queryAsSet = function (selector, valueSelector) {
|
|
16
|
-
valueSelector ??= (element) => element;
|
|
17
|
-
const elements = this.queryAsArray(selector, valueSelector);
|
|
18
|
-
return new Set(elements);
|
|
19
|
-
};
|
|
20
|
-
Document.prototype.queryAsMap = function (selector, keySelector, valueSelector = (el) => el) {
|
|
21
|
-
const elements = this.queryAsArray(selector);
|
|
22
|
-
const map = new Map();
|
|
23
|
-
for (const element of elements) {
|
|
24
|
-
const key = keySelector(element);
|
|
25
|
-
const value = valueSelector(element);
|
|
26
|
-
map.set(key, value);
|
|
27
|
-
}
|
|
28
|
-
return map;
|
|
29
|
-
};
|
|
30
|
-
Document.prototype.waitChild = function (selector, timeoutMillis = 300_000) {
|
|
31
|
-
const el = this.querySelector(selector);
|
|
32
|
-
if (el)
|
|
33
|
-
return Promise.resolve(el);
|
|
34
|
-
return new Promise((resolve, reject) => {
|
|
35
|
-
let timeout = null;
|
|
36
|
-
let interval = null;
|
|
37
|
-
const observer = new MutationObserver((mutations) => {
|
|
38
|
-
const element = this.querySelector(selector);
|
|
39
|
-
if (element) {
|
|
40
|
-
onElementFound(element);
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
for (const mutation of mutations) {
|
|
44
|
-
if (mutation.type !== 'childList')
|
|
45
|
-
continue;
|
|
46
|
-
for (const node of Array.from(mutation.addedNodes)) {
|
|
47
|
-
if (node instanceof Element && node.matches(`:has(${selector})`)) {
|
|
48
|
-
onElementFound(node);
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
function onElementFound(element) {
|
|
55
|
-
if (typeof timeout === 'number')
|
|
56
|
-
clearTimeout(timeout);
|
|
57
|
-
if (typeof interval === 'number')
|
|
58
|
-
clearInterval(interval);
|
|
59
|
-
observer.disconnect();
|
|
60
|
-
resolve(element);
|
|
4
|
+
function getAttributeStrict() {
|
|
5
|
+
return function (attribute) {
|
|
6
|
+
const attr = this.getAttribute(attribute)?.trim();
|
|
7
|
+
if (typeof attr !== 'string' || attr.length === 0) {
|
|
8
|
+
throw new Error(`attribute "${attribute}" not found`);
|
|
61
9
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
10
|
+
return attr;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
function getAttributeAsFloatStrict() {
|
|
14
|
+
return function (attribute) {
|
|
15
|
+
const attr = this.getAttributeStrict(attribute);
|
|
16
|
+
const value = Number.parseFloat(attr.trim());
|
|
17
|
+
if (!Number.isFinite(value)) {
|
|
18
|
+
throw new TypeError(`could not parse attribute "${attribute}" as float`);
|
|
67
19
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
20
|
+
return value;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function getAttributeAsIntStrict() {
|
|
24
|
+
return function (attribute, radix = 10) {
|
|
25
|
+
const attr = this.getAttributeStrict(attribute);
|
|
26
|
+
const value = Number.parseInt(attr.trim(), radix);
|
|
27
|
+
if (!Number.isInteger(value)) {
|
|
28
|
+
throw new TypeError(`could not parse attribute "${attribute}" as integer`);
|
|
73
29
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
for (const mutation of mutations) {
|
|
164
|
-
if (mutation.type !== 'childList')
|
|
165
|
-
continue;
|
|
166
|
-
for (const node of Array.from(mutation.addedNodes)) {
|
|
167
|
-
if (node instanceof Element && node.matches(`:has(${selector})`)) {
|
|
168
|
-
onElementFound(node);
|
|
169
|
-
return;
|
|
30
|
+
return value;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function getTextStrict() {
|
|
34
|
+
return function () {
|
|
35
|
+
const text = this.textContent?.trim();
|
|
36
|
+
if (typeof text !== 'string' || text.length === 0) {
|
|
37
|
+
throw new Error('element has no text content');
|
|
38
|
+
}
|
|
39
|
+
return text;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function getTextAsIntStrict() {
|
|
43
|
+
return function (radix = 10) {
|
|
44
|
+
const value = Number.parseInt(this.getTextStrict(), radix);
|
|
45
|
+
if (!Number.isInteger(value)) {
|
|
46
|
+
throw new TypeError('could not parse text content as integer');
|
|
47
|
+
}
|
|
48
|
+
return value;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function getTextAsFloatStrict() {
|
|
52
|
+
return function () {
|
|
53
|
+
const value = Number.parseFloat(this.getTextStrict());
|
|
54
|
+
if (!Number.isFinite(value)) {
|
|
55
|
+
throw new TypeError('could not parse text content as float');
|
|
56
|
+
}
|
|
57
|
+
return value;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function queryStrict() {
|
|
61
|
+
return function (selector) {
|
|
62
|
+
const element = this.querySelector(selector);
|
|
63
|
+
if (!element)
|
|
64
|
+
throw new Error(`no element found for selector "${selector}"`);
|
|
65
|
+
return element;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function queryAsArray() {
|
|
69
|
+
return function (selector, valueFn) {
|
|
70
|
+
valueFn ??= (element) => element;
|
|
71
|
+
const elements = this.querySelectorAll(selector);
|
|
72
|
+
return Array.from(elements, valueFn);
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function queryAsSet() {
|
|
76
|
+
return function (selector, valueFn) {
|
|
77
|
+
valueFn ??= (element) => element;
|
|
78
|
+
const elements = this.queryAsArray(selector, valueFn);
|
|
79
|
+
return new Set(elements);
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function queryAsMap() {
|
|
83
|
+
return function (selector, keyFn, valueFn) {
|
|
84
|
+
valueFn ??= (element) => element;
|
|
85
|
+
const elements = this.queryAsArray(selector);
|
|
86
|
+
const map = new Map();
|
|
87
|
+
for (const element of elements) {
|
|
88
|
+
const key = keyFn(element);
|
|
89
|
+
const value = valueFn(element);
|
|
90
|
+
map.set(key, value);
|
|
91
|
+
}
|
|
92
|
+
return map;
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function waitChild() {
|
|
96
|
+
return function (selector, timeoutMillis = 300_000) {
|
|
97
|
+
const el = this.querySelector(selector);
|
|
98
|
+
if (el)
|
|
99
|
+
return Promise.resolve(el);
|
|
100
|
+
return new Promise((resolve, reject) => {
|
|
101
|
+
let timeout = null;
|
|
102
|
+
let interval = null;
|
|
103
|
+
const observer = new MutationObserver((mutations) => {
|
|
104
|
+
const element = this.querySelector(selector);
|
|
105
|
+
if (element) {
|
|
106
|
+
onElementFound(element);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
for (const mutation of mutations) {
|
|
110
|
+
if (mutation.type !== 'childList')
|
|
111
|
+
continue;
|
|
112
|
+
for (const node of Array.from(mutation.addedNodes)) {
|
|
113
|
+
if (node instanceof Element && node.matches(`:has(${selector})`)) {
|
|
114
|
+
onElementFound(node);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
170
117
|
}
|
|
171
118
|
}
|
|
119
|
+
});
|
|
120
|
+
function onElementFound(element) {
|
|
121
|
+
if (typeof timeout === 'number')
|
|
122
|
+
clearTimeout(timeout);
|
|
123
|
+
if (typeof interval === 'number')
|
|
124
|
+
clearInterval(interval);
|
|
125
|
+
observer.disconnect();
|
|
126
|
+
resolve(element);
|
|
172
127
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
if (typeof interval === 'number')
|
|
178
|
-
clearInterval(interval);
|
|
179
|
-
observer.disconnect();
|
|
180
|
-
resolve(element);
|
|
181
|
-
}
|
|
182
|
-
function onInterval() {
|
|
183
|
-
const element = this.querySelector(selector);
|
|
184
|
-
if (element) {
|
|
185
|
-
onElementFound(element);
|
|
128
|
+
function onInterval() {
|
|
129
|
+
const element = this.querySelector(selector);
|
|
130
|
+
if (element)
|
|
131
|
+
onElementFound(element);
|
|
186
132
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
};
|
|
212
|
-
URLSearchParams.prototype.getAsIntegerStrict = function (name, radix = 10) {
|
|
213
|
-
const item = this.getStrict(name);
|
|
214
|
-
const parsed = Number.parseInt(item, radix);
|
|
215
|
-
if (!Number.isInteger(parsed)) {
|
|
216
|
-
throw new TypeError(`could not parse "${item}" as an integer`);
|
|
217
|
-
}
|
|
218
|
-
return parsed;
|
|
219
|
-
};
|
|
220
|
-
Array.fromElements = function (source, valueSelector) {
|
|
221
|
-
isValidElementSource(source);
|
|
222
|
-
const elements = parseElementSource(source);
|
|
223
|
-
return Array.from(elements, valueSelector);
|
|
133
|
+
function onTimeout() {
|
|
134
|
+
if (typeof interval === 'number')
|
|
135
|
+
clearInterval(interval);
|
|
136
|
+
observer.disconnect();
|
|
137
|
+
reject(new Error(`timeout waiting for element: ${selector}`));
|
|
138
|
+
}
|
|
139
|
+
interval = setInterval(onInterval.bind(this), 100);
|
|
140
|
+
timeout = setTimeout(onTimeout, timeoutMillis);
|
|
141
|
+
observer.observe(this, { childList: true, subtree: true });
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
const element = {
|
|
146
|
+
getAttributeStrict,
|
|
147
|
+
getAttributeAsFloatStrict,
|
|
148
|
+
getAttributeAsIntStrict,
|
|
149
|
+
getTextStrict,
|
|
150
|
+
getTextAsIntStrict,
|
|
151
|
+
getTextAsFloatStrict,
|
|
152
|
+
queryStrict,
|
|
153
|
+
queryAsArray,
|
|
154
|
+
queryAsSet,
|
|
155
|
+
queryAsMap,
|
|
156
|
+
waitChild
|
|
224
157
|
};
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
throw new TypeError('item in source array is not an element');
|
|
158
|
+
|
|
159
|
+
function getStrict() {
|
|
160
|
+
return function (name) {
|
|
161
|
+
const item = this.get(name);
|
|
162
|
+
if (typeof item !== 'string') {
|
|
163
|
+
throw new TypeError(`key "${name}" not found in URL search parameters`);
|
|
232
164
|
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
return
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
const elements = parseElementSource(source);
|
|
242
|
-
const set = new Set();
|
|
243
|
-
for (const element of elements) {
|
|
244
|
-
if (!(element instanceof Element)) {
|
|
245
|
-
throw new TypeError('item in source array is not an element');
|
|
165
|
+
return item;
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
function getAsInteger() {
|
|
169
|
+
return function (name, radix = 10) {
|
|
170
|
+
const item = this.get(name);
|
|
171
|
+
if (typeof item !== 'string') {
|
|
172
|
+
return null;
|
|
246
173
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
}
|
|
250
|
-
return set;
|
|
251
|
-
};
|
|
252
|
-
function isValidElementSource(source) {
|
|
253
|
-
if (!Array.isArray(source) &&
|
|
254
|
-
!(source instanceof NodeList) &&
|
|
255
|
-
(typeof source !== 'string' || source.length === 0)) {
|
|
256
|
-
throw new TypeError('source must be an array, a NodeList or a string');
|
|
257
|
-
}
|
|
258
|
-
return true;
|
|
174
|
+
return Number.parseInt(item, radix);
|
|
175
|
+
};
|
|
259
176
|
}
|
|
260
|
-
function
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
return Array.from(source);
|
|
269
|
-
}
|
|
270
|
-
throw new TypeError('source must be an array, a NodeList or a string');
|
|
177
|
+
function getAsIntegerStrict() {
|
|
178
|
+
return function (name, radix = 10) {
|
|
179
|
+
const value = Number.parseInt(this.getStrict(name), radix);
|
|
180
|
+
if (!Number.isInteger(value)) {
|
|
181
|
+
throw new TypeError(`could not parse "${name}" as an integer`);
|
|
182
|
+
}
|
|
183
|
+
return value;
|
|
184
|
+
};
|
|
271
185
|
}
|
|
186
|
+
const urlSearchParams = {
|
|
187
|
+
getStrict,
|
|
188
|
+
getAsInteger,
|
|
189
|
+
getAsIntegerStrict
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
Document.prototype.queryStrict = element.queryStrict();
|
|
193
|
+
Document.prototype.queryAsArray = element.queryAsArray();
|
|
194
|
+
Document.prototype.queryAsSet = element.queryAsSet();
|
|
195
|
+
Document.prototype.queryAsMap = element.queryAsMap();
|
|
196
|
+
Document.prototype.waitChild = element.waitChild();
|
|
197
|
+
Element.prototype.getAttributeStrict = element.getAttributeStrict();
|
|
198
|
+
Element.prototype.getAttributeAsFloatStrict = element.getAttributeAsFloatStrict();
|
|
199
|
+
Element.prototype.getAttributeAsIntStrict = element.getAttributeAsIntStrict();
|
|
200
|
+
Element.prototype.getTextStrict = element.getTextStrict();
|
|
201
|
+
Element.prototype.getTextAsIntStrict = element.getTextAsIntStrict();
|
|
202
|
+
Element.prototype.getTextAsFloatStrict = element.getTextAsFloatStrict();
|
|
203
|
+
Element.prototype.queryStrict = element.queryStrict();
|
|
204
|
+
Element.prototype.queryAsArray = element.queryAsArray();
|
|
205
|
+
Element.prototype.queryAsSet = element.queryAsSet();
|
|
206
|
+
Element.prototype.queryAsMap = element.queryAsMap();
|
|
207
|
+
Element.prototype.waitChild = element.waitChild();
|
|
208
|
+
URLSearchParams.prototype.getStrict = urlSearchParams.getStrict();
|
|
209
|
+
URLSearchParams.prototype.getAsInteger = urlSearchParams.getAsInteger();
|
|
210
|
+
URLSearchParams.prototype.getAsIntegerStrict = urlSearchParams.getAsIntegerStrict();
|
|
272
211
|
|
|
273
212
|
})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tb-dev/prototype-dom",
|
|
3
|
-
"version": "6.1.
|
|
3
|
+
"version": "6.1.2",
|
|
4
4
|
"description": "Adds prototype methods to DOM objects",
|
|
5
5
|
"packageManager": "pnpm@8.15.4",
|
|
6
6
|
"license": "MIT",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@rollup/plugin-typescript": "^11.1.6",
|
|
27
|
-
"@tb-dev/eslint-config": "^2.
|
|
27
|
+
"@tb-dev/eslint-config": "^2.1.1",
|
|
28
28
|
"@vitest/ui": "^1.3.1",
|
|
29
29
|
"eslint": "^8.57.0",
|
|
30
30
|
"husky": "^9.0.11",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"prettier": "^3.2.5",
|
|
33
33
|
"rollup": "^4.12.0",
|
|
34
34
|
"tslib": "^2.6.2",
|
|
35
|
-
"typedoc": "^0.25.
|
|
35
|
+
"typedoc": "^0.25.9",
|
|
36
36
|
"typedoc-plugin-mdn-links": "^3.1.17",
|
|
37
37
|
"typescript": "5.3.3",
|
|
38
38
|
"vitest": "^1.3.1"
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"main": "./dist/index.js",
|
|
44
44
|
"types": "./dist/index.d.ts",
|
|
45
45
|
"scripts": {
|
|
46
|
-
"build": "rollup --config rollup.config.js",
|
|
46
|
+
"build": "rollup --config rollup.config.js && node scripts/delete-artifacts.mjs",
|
|
47
47
|
"docs": "typedoc --plugin typedoc-plugin-mdn-links",
|
|
48
48
|
"format": "prettier . --write",
|
|
49
49
|
"format-check": "prettier . --check",
|