@asamuzakjp/dom-selector 7.0.0 → 7.0.1
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 +10 -6
- package/src/index.js +7 -12
- package/src/js/finder.js +34 -21
package/package.json
CHANGED
|
@@ -34,26 +34,30 @@
|
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@types/css-tree": "^2.3.11",
|
|
36
36
|
"benchmark": "^2.1.4",
|
|
37
|
-
"c8": "^
|
|
37
|
+
"c8": "^11.0.0",
|
|
38
38
|
"chai": "^6.2.2",
|
|
39
39
|
"commander": "^14.0.3",
|
|
40
40
|
"eslint": "^9.39.3",
|
|
41
41
|
"eslint-config-prettier": "^10.1.8",
|
|
42
|
-
"eslint-plugin-jsdoc": "^62.7.
|
|
42
|
+
"eslint-plugin-jsdoc": "^62.7.1",
|
|
43
43
|
"eslint-plugin-prettier": "^5.5.5",
|
|
44
44
|
"eslint-plugin-regexp": "^3.0.0",
|
|
45
45
|
"eslint-plugin-unicorn": "^63.0.0",
|
|
46
|
-
"globals": "^17.
|
|
46
|
+
"globals": "^17.4.0",
|
|
47
47
|
"jsdom": "^28.1.0",
|
|
48
48
|
"mocha": "^11.7.5",
|
|
49
|
-
"neostandard": "^0.
|
|
49
|
+
"neostandard": "^0.13.0",
|
|
50
50
|
"prettier": "^3.8.1",
|
|
51
51
|
"sinon": "^21.0.1",
|
|
52
52
|
"typescript": "^5.9.3",
|
|
53
53
|
"wpt-runner": "^6.1.0"
|
|
54
54
|
},
|
|
55
55
|
"overrides": {
|
|
56
|
-
"
|
|
56
|
+
"c8": {
|
|
57
|
+
"yargs": "^18.0.0"
|
|
58
|
+
},
|
|
59
|
+
"jsdom": "$jsdom",
|
|
60
|
+
"serialize-javascript": "^7.0.4"
|
|
57
61
|
},
|
|
58
62
|
"scripts": {
|
|
59
63
|
"bench": "node benchmark/bench.js",
|
|
@@ -68,5 +72,5 @@
|
|
|
68
72
|
"engines": {
|
|
69
73
|
"node": "^20.19.0 || ^22.12.0 || >=24.0.0"
|
|
70
74
|
},
|
|
71
|
-
"version": "7.0.
|
|
75
|
+
"version": "7.0.1"
|
|
72
76
|
}
|
package/src/index.js
CHANGED
|
@@ -119,19 +119,14 @@ 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
|
}
|
|
125
|
+
opt.check = true;
|
|
126
|
+
opt.noexcept = true;
|
|
127
|
+
opt.warn = false;
|
|
128
|
+
this.#finder.setup(selector, node, opt);
|
|
129
|
+
const res = this.#finder.find(TARGET_SELF);
|
|
135
130
|
return res;
|
|
136
131
|
};
|
|
137
132
|
|
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,7 @@ export class Finder {
|
|
|
3137
3152
|
* @returns {object} The AST for the selector.
|
|
3138
3153
|
*/
|
|
3139
3154
|
getAST = selector => {
|
|
3140
|
-
|
|
3141
|
-
return this.#selectorAST;
|
|
3142
|
-
}
|
|
3155
|
+
// TBD: get cached AST if possible.
|
|
3143
3156
|
return parseSelector(selector);
|
|
3144
3157
|
};
|
|
3145
3158
|
}
|