@asamuzakjp/dom-selector 7.0.0 → 7.0.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/package.json +14 -11
- package/src/index.js +13 -27
- package/src/js/finder.js +33 -21
- package/src/js/matcher.js +4 -4
- package/src/js/parser.js +1 -2
package/package.json
CHANGED
|
@@ -23,37 +23,40 @@
|
|
|
23
23
|
},
|
|
24
24
|
"./package.json": "./package.json"
|
|
25
25
|
},
|
|
26
|
-
"types": "types/index.d.ts",
|
|
27
26
|
"dependencies": {
|
|
28
27
|
"@asamuzakjp/nwsapi": "^2.3.9",
|
|
29
28
|
"bidi-js": "^1.0.3",
|
|
30
|
-
"css-tree": "^3.1
|
|
29
|
+
"css-tree": "^3.2.1",
|
|
31
30
|
"is-potential-custom-element-name": "^1.0.1",
|
|
32
31
|
"lru-cache": "^11.2.6"
|
|
33
32
|
},
|
|
34
33
|
"devDependencies": {
|
|
35
34
|
"@types/css-tree": "^2.3.11",
|
|
36
35
|
"benchmark": "^2.1.4",
|
|
37
|
-
"c8": "^
|
|
36
|
+
"c8": "^11.0.0",
|
|
38
37
|
"chai": "^6.2.2",
|
|
39
38
|
"commander": "^14.0.3",
|
|
40
|
-
"eslint": "^9.39.
|
|
39
|
+
"eslint": "^9.39.4",
|
|
41
40
|
"eslint-config-prettier": "^10.1.8",
|
|
42
|
-
"eslint-plugin-jsdoc": "^62.
|
|
41
|
+
"eslint-plugin-jsdoc": "^62.8.0",
|
|
43
42
|
"eslint-plugin-prettier": "^5.5.5",
|
|
44
|
-
"eslint-plugin-regexp": "^3.
|
|
43
|
+
"eslint-plugin-regexp": "^3.1.0",
|
|
45
44
|
"eslint-plugin-unicorn": "^63.0.0",
|
|
46
|
-
"globals": "^17.
|
|
45
|
+
"globals": "^17.4.0",
|
|
47
46
|
"jsdom": "^28.1.0",
|
|
48
47
|
"mocha": "^11.7.5",
|
|
49
|
-
"neostandard": "^0.
|
|
48
|
+
"neostandard": "^0.13.0",
|
|
50
49
|
"prettier": "^3.8.1",
|
|
51
|
-
"sinon": "^21.0.
|
|
50
|
+
"sinon": "^21.0.2",
|
|
52
51
|
"typescript": "^5.9.3",
|
|
53
52
|
"wpt-runner": "^6.1.0"
|
|
54
53
|
},
|
|
55
54
|
"overrides": {
|
|
56
|
-
"
|
|
55
|
+
"c8": {
|
|
56
|
+
"yargs": "^18.0.0"
|
|
57
|
+
},
|
|
58
|
+
"jsdom": "$jsdom",
|
|
59
|
+
"serialize-javascript": "^7.0.4"
|
|
57
60
|
},
|
|
58
61
|
"scripts": {
|
|
59
62
|
"bench": "node benchmark/bench.js",
|
|
@@ -68,5 +71,5 @@
|
|
|
68
71
|
"engines": {
|
|
69
72
|
"node": "^20.19.0 || ^22.12.0 || >=24.0.0"
|
|
70
73
|
},
|
|
71
|
-
"version": "7.0.
|
|
74
|
+
"version": "7.0.2"
|
|
72
75
|
}
|
package/src/index.js
CHANGED
|
@@ -119,20 +119,13 @@ export class DOMSelector {
|
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
if (this.#idlUtils) {
|
|
125
|
-
node = this.#idlUtils.wrapperForImpl(node);
|
|
126
|
-
}
|
|
127
|
-
opt.check = true;
|
|
128
|
-
opt.noexept = true;
|
|
129
|
-
opt.warn = false;
|
|
130
|
-
this.#finder.setup(selector, node, opt);
|
|
131
|
-
res = this.#finder.find(TARGET_SELF);
|
|
132
|
-
} catch (e) {
|
|
133
|
-
this.#finder.onError(e, opt);
|
|
122
|
+
if (this.#idlUtils) {
|
|
123
|
+
node = this.#idlUtils.wrapperForImpl(node);
|
|
134
124
|
}
|
|
135
|
-
|
|
125
|
+
opt.check = true;
|
|
126
|
+
opt.noexcept = true;
|
|
127
|
+
opt.warn = false;
|
|
128
|
+
return this.#finder.setup(selector, node, opt).find(TARGET_SELF);
|
|
136
129
|
};
|
|
137
130
|
|
|
138
131
|
/**
|
|
@@ -168,8 +161,7 @@ export class DOMSelector {
|
|
|
168
161
|
if (filterMatches) {
|
|
169
162
|
try {
|
|
170
163
|
const n = this.#idlUtils ? this.#idlUtils.wrapperForImpl(node) : node;
|
|
171
|
-
|
|
172
|
-
return res;
|
|
164
|
+
return this.#nwsapi.match(selector, n);
|
|
173
165
|
} catch (e) {
|
|
174
166
|
// fall through
|
|
175
167
|
}
|
|
@@ -180,8 +172,7 @@ export class DOMSelector {
|
|
|
180
172
|
if (this.#idlUtils) {
|
|
181
173
|
node = this.#idlUtils.wrapperForImpl(node);
|
|
182
174
|
}
|
|
183
|
-
this.#finder.setup(selector, node, opt);
|
|
184
|
-
const nodes = this.#finder.find(TARGET_SELF);
|
|
175
|
+
const nodes = this.#finder.setup(selector, node, opt).find(TARGET_SELF);
|
|
185
176
|
res = nodes.size;
|
|
186
177
|
} catch (e) {
|
|
187
178
|
this.#finder.onError(e, opt);
|
|
@@ -222,8 +213,7 @@ export class DOMSelector {
|
|
|
222
213
|
if (filterMatches) {
|
|
223
214
|
try {
|
|
224
215
|
const n = this.#idlUtils ? this.#idlUtils.wrapperForImpl(node) : node;
|
|
225
|
-
|
|
226
|
-
return res;
|
|
216
|
+
return this.#nwsapi.closest(selector, n);
|
|
227
217
|
} catch (e) {
|
|
228
218
|
// fall through
|
|
229
219
|
}
|
|
@@ -234,8 +224,7 @@ export class DOMSelector {
|
|
|
234
224
|
if (this.#idlUtils) {
|
|
235
225
|
node = this.#idlUtils.wrapperForImpl(node);
|
|
236
226
|
}
|
|
237
|
-
this.#finder.setup(selector, node, opt);
|
|
238
|
-
const nodes = this.#finder.find(TARGET_LINEAL);
|
|
227
|
+
const nodes = this.#finder.setup(selector, node, opt).find(TARGET_LINEAL);
|
|
239
228
|
if (nodes.size) {
|
|
240
229
|
let refNode = node;
|
|
241
230
|
while (refNode) {
|
|
@@ -269,8 +258,7 @@ export class DOMSelector {
|
|
|
269
258
|
if (this.#idlUtils) {
|
|
270
259
|
node = this.#idlUtils.wrapperForImpl(node);
|
|
271
260
|
}
|
|
272
|
-
this.#finder.setup(selector, node, opt);
|
|
273
|
-
const nodes = this.#finder.find(TARGET_FIRST);
|
|
261
|
+
const nodes = this.#finder.setup(selector, node, opt).find(TARGET_FIRST);
|
|
274
262
|
if (nodes.size) {
|
|
275
263
|
[res] = [...nodes];
|
|
276
264
|
}
|
|
@@ -312,8 +300,7 @@ export class DOMSelector {
|
|
|
312
300
|
if (filterMatches) {
|
|
313
301
|
try {
|
|
314
302
|
const n = this.#idlUtils ? this.#idlUtils.wrapperForImpl(node) : node;
|
|
315
|
-
|
|
316
|
-
return res;
|
|
303
|
+
return this.#nwsapi.select(selector, n);
|
|
317
304
|
} catch (e) {
|
|
318
305
|
// fall through
|
|
319
306
|
}
|
|
@@ -324,8 +311,7 @@ export class DOMSelector {
|
|
|
324
311
|
if (this.#idlUtils) {
|
|
325
312
|
node = this.#idlUtils.wrapperForImpl(node);
|
|
326
313
|
}
|
|
327
|
-
this.#finder.setup(selector, node, opt);
|
|
328
|
-
const nodes = this.#finder.find(TARGET_ALL);
|
|
314
|
+
const nodes = this.#finder.setup(selector, node, opt).find(TARGET_ALL);
|
|
329
315
|
if (nodes.size) {
|
|
330
316
|
res = [...nodes];
|
|
331
317
|
}
|
package/src/js/finder.js
CHANGED
|
@@ -215,7 +215,6 @@ export class Finder {
|
|
|
215
215
|
this.#scoped =
|
|
216
216
|
this.#node !== this.#root && this.#node.nodeType === ELEMENT_NODE;
|
|
217
217
|
this.#selector = selector;
|
|
218
|
-
[this.#ast, this.#nodes] = this._correspond(selector);
|
|
219
218
|
this.#pseudoElement = [];
|
|
220
219
|
this.#walkers = new WeakMap();
|
|
221
220
|
this.#nodeWalker = null;
|
|
@@ -379,12 +378,7 @@ export class Finder {
|
|
|
379
378
|
nodes[i] = [];
|
|
380
379
|
}
|
|
381
380
|
} else {
|
|
382
|
-
|
|
383
|
-
this.#selectorAST = parseSelector(selector);
|
|
384
|
-
} catch (e) {
|
|
385
|
-
this.#selectorAST = null;
|
|
386
|
-
return this.onError(e);
|
|
387
|
-
}
|
|
381
|
+
this.#selectorAST = parseSelector(selector);
|
|
388
382
|
const { branches, info } = walkAST(this.#selectorAST, true);
|
|
389
383
|
const {
|
|
390
384
|
hasHasPseudoFunc,
|
|
@@ -802,13 +796,13 @@ export class Finder {
|
|
|
802
796
|
};
|
|
803
797
|
|
|
804
798
|
/**
|
|
805
|
-
*
|
|
799
|
+
* Matches pseudo-class selector.
|
|
806
800
|
* @private
|
|
807
801
|
* @see https://html.spec.whatwg.org/#pseudo-classes
|
|
808
|
-
* @param {object} ast - AST
|
|
809
|
-
* @param {object} node - Element node
|
|
810
|
-
* @param {object} [opt] -
|
|
811
|
-
* @returns {Set.<object>}
|
|
802
|
+
* @param {object} ast - The AST.
|
|
803
|
+
* @param {object} node - The Element node.
|
|
804
|
+
* @param {object} [opt] - Options.
|
|
805
|
+
* @returns {Set.<object>} A collection of matched nodes.
|
|
812
806
|
*/
|
|
813
807
|
_matchPseudoClassSelector(ast, node, opt = {}) {
|
|
814
808
|
const { children: astChildren, name: astName } = ast;
|
|
@@ -1535,7 +1529,7 @@ export class Finder {
|
|
|
1535
1529
|
const attrType = node.getAttribute('type');
|
|
1536
1530
|
if (
|
|
1537
1531
|
localName === 'input' &&
|
|
1538
|
-
!(node.
|
|
1532
|
+
!(node.readOnly || node.hasAttribute('readonly')) &&
|
|
1539
1533
|
!(node.disabled || node.hasAttribute('disabled')) &&
|
|
1540
1534
|
KEYS_INPUT_RANGE.has(attrType)
|
|
1541
1535
|
) {
|
|
@@ -2408,7 +2402,7 @@ export class Finder {
|
|
|
2408
2402
|
let nodes = [];
|
|
2409
2403
|
let filtered = false;
|
|
2410
2404
|
if (targetType === TARGET_SELF) {
|
|
2411
|
-
[nodes, filtered] = this._matchSelf(leaves);
|
|
2405
|
+
[nodes, filtered] = this._matchSelf(leaves, this.#check);
|
|
2412
2406
|
} else if (targetType === TARGET_LINEAL) {
|
|
2413
2407
|
[nodes, filtered] = this._findLineal(leaves, { complex });
|
|
2414
2408
|
} else if (
|
|
@@ -2447,7 +2441,7 @@ export class Finder {
|
|
|
2447
2441
|
let nodes = [];
|
|
2448
2442
|
let filtered = false;
|
|
2449
2443
|
if (targetType === TARGET_SELF) {
|
|
2450
|
-
[nodes, filtered] = this._matchSelf(leaves);
|
|
2444
|
+
[nodes, filtered] = this._matchSelf(leaves, this.#check);
|
|
2451
2445
|
} else if (targetType === TARGET_LINEAL) {
|
|
2452
2446
|
[nodes, filtered] = this._findLineal(leaves, { complex });
|
|
2453
2447
|
} else {
|
|
@@ -2470,7 +2464,7 @@ export class Finder {
|
|
|
2470
2464
|
let nodes = [];
|
|
2471
2465
|
let filtered = false;
|
|
2472
2466
|
if (targetType === TARGET_SELF) {
|
|
2473
|
-
[nodes, filtered] = this._matchSelf(leaves);
|
|
2467
|
+
[nodes, filtered] = this._matchSelf(leaves, this.#check);
|
|
2474
2468
|
} else if (targetType === TARGET_LINEAL) {
|
|
2475
2469
|
[nodes, filtered] = this._findLineal(leaves, { complex });
|
|
2476
2470
|
} else {
|
|
@@ -2542,7 +2536,7 @@ export class Finder {
|
|
|
2542
2536
|
}
|
|
2543
2537
|
}
|
|
2544
2538
|
} else if (targetType === TARGET_SELF) {
|
|
2545
|
-
[nodes, filtered] = this._matchSelf(leaves);
|
|
2539
|
+
[nodes, filtered] = this._matchSelf(leaves, this.#check);
|
|
2546
2540
|
} else if (targetType === TARGET_LINEAL) {
|
|
2547
2541
|
[nodes, filtered] = this._findLineal(leaves, { complex });
|
|
2548
2542
|
} else if (targetType === TARGET_FIRST) {
|
|
@@ -2733,6 +2727,7 @@ export class Finder {
|
|
|
2733
2727
|
* @returns {Array.<Array.<object>>} An array containing the AST and nodes.
|
|
2734
2728
|
*/
|
|
2735
2729
|
_collectNodes = targetType => {
|
|
2730
|
+
[this.#ast, this.#nodes] = this._correspond(this.#selector);
|
|
2736
2731
|
const ast = this.#ast.values();
|
|
2737
2732
|
if (targetType === TARGET_ALL || targetType === TARGET_FIRST) {
|
|
2738
2733
|
const pendingItems = new Set();
|
|
@@ -3039,7 +3034,27 @@ export class Finder {
|
|
|
3039
3034
|
* @returns {Set.<object>} A collection of matched nodes.
|
|
3040
3035
|
*/
|
|
3041
3036
|
find = targetType => {
|
|
3042
|
-
|
|
3037
|
+
let collection;
|
|
3038
|
+
try {
|
|
3039
|
+
collection = this._collectNodes(targetType);
|
|
3040
|
+
} catch (e) {
|
|
3041
|
+
if (this.#check) {
|
|
3042
|
+
let pseudoElement;
|
|
3043
|
+
if (this.#pseudoElement.length) {
|
|
3044
|
+
pseudoElement = this.#pseudoElement.join('');
|
|
3045
|
+
} else {
|
|
3046
|
+
pseudoElement = null;
|
|
3047
|
+
}
|
|
3048
|
+
return {
|
|
3049
|
+
pseudoElement,
|
|
3050
|
+
match: false,
|
|
3051
|
+
ast: this.#selectorAST ?? null
|
|
3052
|
+
};
|
|
3053
|
+
} else {
|
|
3054
|
+
throw e;
|
|
3055
|
+
}
|
|
3056
|
+
}
|
|
3057
|
+
const [[...branches], collectedNodes] = collection;
|
|
3043
3058
|
const l = branches.length;
|
|
3044
3059
|
let sort =
|
|
3045
3060
|
l > 1 && targetType === TARGET_ALL && this.#selector.includes(':scope');
|
|
@@ -3137,9 +3152,6 @@ export class Finder {
|
|
|
3137
3152
|
* @returns {object} The AST for the selector.
|
|
3138
3153
|
*/
|
|
3139
3154
|
getAST = selector => {
|
|
3140
|
-
if (selector === this.#selector && this.#selectorAST) {
|
|
3141
|
-
return this.#selectorAST;
|
|
3142
|
-
}
|
|
3143
3155
|
return parseSelector(selector);
|
|
3144
3156
|
};
|
|
3145
3157
|
}
|
package/src/js/matcher.js
CHANGED
|
@@ -427,15 +427,15 @@ export const matchAttributeSelector = (ast, node, opt = {}) => {
|
|
|
427
427
|
let attrValue;
|
|
428
428
|
if (astIdentValue) {
|
|
429
429
|
if (caseInsensitive) {
|
|
430
|
-
attrValue = astIdentValue.toLowerCase();
|
|
430
|
+
attrValue = astIdentValue.toLowerCase().replace(/\\(?!\\)/g, '');
|
|
431
431
|
} else {
|
|
432
|
-
attrValue = astIdentValue;
|
|
432
|
+
attrValue = astIdentValue.replace(/\\(?!\\)/g, '');
|
|
433
433
|
}
|
|
434
434
|
} else if (astStringValue) {
|
|
435
435
|
if (caseInsensitive) {
|
|
436
|
-
attrValue = astStringValue.toLowerCase();
|
|
436
|
+
attrValue = astStringValue.toLowerCase().replace(/\\(?!\\)/g, '');
|
|
437
437
|
} else {
|
|
438
|
-
attrValue = astStringValue;
|
|
438
|
+
attrValue = astStringValue.replace(/\\(?!\\)/g, '');
|
|
439
439
|
}
|
|
440
440
|
} else if (astStringValue === '') {
|
|
441
441
|
attrValue = astStringValue;
|
package/src/js/parser.js
CHANGED