@asamuzakjp/dom-selector 7.1.1 → 8.0.0-a.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 +5 -5
- package/src/index.js +24 -40
- package/src/js/matcher.js +21 -9
- package/src/js/nwsapi.js +1320 -0
- package/src/js/parser.js +40 -2
- package/src/js/utility.js +54 -33
- package/types/index.d.ts +1 -1
- package/types/js/matcher.d.ts +6 -3
- package/types/js/nwsapi.d.ts +81 -0
- package/types/js/parser.d.ts +5 -0
- package/types/js/utility.d.ts +8 -2
package/package.json
CHANGED
|
@@ -25,7 +25,6 @@
|
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@asamuzakjp/generational-cache": "^1.0.1",
|
|
28
|
-
"@asamuzakjp/nwsapi": "^2.3.9",
|
|
29
28
|
"bidi-js": "^1.0.3",
|
|
30
29
|
"css-tree": "^3.2.1",
|
|
31
30
|
"is-potential-custom-element-name": "^1.0.1"
|
|
@@ -33,7 +32,6 @@
|
|
|
33
32
|
"devDependencies": {
|
|
34
33
|
"@types/css-tree": "^2.3.11",
|
|
35
34
|
"@types/node": "^25.6.0",
|
|
36
|
-
"benchmark": "^2.1.4",
|
|
37
35
|
"c8": "^11.0.0",
|
|
38
36
|
"chai": "^6.2.2",
|
|
39
37
|
"commander": "^14.0.3",
|
|
@@ -44,12 +42,13 @@
|
|
|
44
42
|
"eslint-plugin-regexp": "^3.1.0",
|
|
45
43
|
"eslint-plugin-unicorn": "^64.0.0",
|
|
46
44
|
"globals": "^17.5.0",
|
|
47
|
-
"jsdom": "^29.0
|
|
45
|
+
"jsdom": "^29.1.0",
|
|
48
46
|
"mitata": "^1.0.34",
|
|
49
47
|
"mocha": "^11.7.5",
|
|
50
48
|
"neostandard": "^0.13.0",
|
|
51
49
|
"prettier": "^3.8.3",
|
|
52
50
|
"sinon": "^21.1.2",
|
|
51
|
+
"tinybench": "^6.0.1",
|
|
53
52
|
"typescript": "^6.0.3",
|
|
54
53
|
"wpt-runner": "^7.0.0"
|
|
55
54
|
},
|
|
@@ -64,6 +63,7 @@
|
|
|
64
63
|
"bench": "node benchmark/bench.js",
|
|
65
64
|
"bench:cache": "node benchmark/bench-cache.js",
|
|
66
65
|
"bench:cacheMonster": "node benchmark/bench-cache-monster.js",
|
|
66
|
+
"bench:nwsapi": "node benchmark/bench-nwsapi.js",
|
|
67
67
|
"bench:sizzle": "node benchmark/bench-sizzle.js",
|
|
68
68
|
"build": "npm run tsc && npm run lint && npm test",
|
|
69
69
|
"lint": "eslint --fix .",
|
|
@@ -73,7 +73,7 @@
|
|
|
73
73
|
"update:wpt": "git submodule update --init --recursive --remote"
|
|
74
74
|
},
|
|
75
75
|
"engines": {
|
|
76
|
-
"node": "^20.19.0 || ^22.
|
|
76
|
+
"node": "^20.19.0 || ^22.13.0 || >=24.0.0"
|
|
77
77
|
},
|
|
78
|
-
"version": "
|
|
78
|
+
"version": "8.0.0-a.2"
|
|
79
79
|
}
|
package/src/index.js
CHANGED
|
@@ -8,8 +8,13 @@
|
|
|
8
8
|
/* import */
|
|
9
9
|
import { GenerationalCache } from '@asamuzakjp/generational-cache';
|
|
10
10
|
import { Finder } from './js/finder.js';
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
11
|
+
import { Nwsapi } from './js/nwsapi.js';
|
|
12
|
+
import { extractSubjectsAst } from './js/parser.js';
|
|
13
|
+
import {
|
|
14
|
+
filterSelector,
|
|
15
|
+
getType,
|
|
16
|
+
extractSubjectsRegExp
|
|
17
|
+
} from './js/utility.js';
|
|
13
18
|
|
|
14
19
|
/* constants */
|
|
15
20
|
import {
|
|
@@ -19,14 +24,13 @@ import {
|
|
|
19
24
|
TARGET_ALL,
|
|
20
25
|
TARGET_FIRST,
|
|
21
26
|
TARGET_LINEAL,
|
|
22
|
-
TARGET_SELF
|
|
23
|
-
COMBINATOR,
|
|
24
|
-
ID_SELECTOR,
|
|
25
|
-
CLASS_SELECTOR,
|
|
26
|
-
TYPE_SELECTOR
|
|
27
|
+
TARGET_SELF
|
|
27
28
|
} from './js/constant.js';
|
|
28
29
|
const CACHE_SIZE = 2048;
|
|
29
30
|
|
|
31
|
+
/* regexp */
|
|
32
|
+
const REG_SELECTOR = /[[\]():\\"'`]/;
|
|
33
|
+
|
|
30
34
|
/**
|
|
31
35
|
* @typedef {object} CheckResult
|
|
32
36
|
* @property {boolean} match - The match result.
|
|
@@ -54,10 +58,10 @@ export class DOMSelector {
|
|
|
54
58
|
const { cacheSize, idlUtils } = opt;
|
|
55
59
|
this.#window = window;
|
|
56
60
|
this.#document = document ?? window.document;
|
|
57
|
-
this.#finder = new Finder(window);
|
|
58
61
|
this.#idlUtils = idlUtils;
|
|
59
|
-
this.#nwsapi = initNwsapi(window, document);
|
|
60
62
|
this.#cache = new GenerationalCache(cacheSize ?? CACHE_SIZE);
|
|
63
|
+
this.#finder = new Finder(this.#window);
|
|
64
|
+
this.#nwsapi = new Nwsapi(this.#window, this.#document);
|
|
61
65
|
}
|
|
62
66
|
|
|
63
67
|
/**
|
|
@@ -71,48 +75,28 @@ export class DOMSelector {
|
|
|
71
75
|
/**
|
|
72
76
|
* Parses a selector and extracts the rightmost subject keys (Id, Class, Tag).
|
|
73
77
|
* @param {string} selector - The CSS selector to parse.
|
|
78
|
+
* @param {boolean} caseSensitive - True if tag key should be case sensitive.
|
|
74
79
|
* @returns {Array<{id: string|null, className: string|null, tag: string|null}>} The list of extracted keys for each selector group.
|
|
75
80
|
*/
|
|
76
|
-
extractSubjects = selector => {
|
|
81
|
+
extractSubjects = (selector, caseSensitive = false) => {
|
|
77
82
|
if (!selector || typeof selector !== 'string') {
|
|
78
83
|
return [{ id: null, className: null, tag: null }];
|
|
79
84
|
}
|
|
80
|
-
const cacheKey = `extract_${selector}`;
|
|
85
|
+
const cacheKey = `extract_${selector}_${caseSensitive}`;
|
|
81
86
|
let subjects = this.#cache.get(cacheKey);
|
|
82
87
|
if (subjects !== undefined) {
|
|
83
88
|
return subjects;
|
|
84
89
|
}
|
|
85
90
|
subjects = [];
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
while (current) {
|
|
95
|
-
const node = current.data;
|
|
96
|
-
if (node.type === COMBINATOR) {
|
|
97
|
-
break;
|
|
98
|
-
}
|
|
99
|
-
if (node.type === ID_SELECTOR) {
|
|
100
|
-
idKey = idKey ?? unescapeSelector(node.name);
|
|
101
|
-
} else if (node.type === CLASS_SELECTOR) {
|
|
102
|
-
classKey = classKey ?? unescapeSelector(node.name);
|
|
103
|
-
} else if (node.type === TYPE_SELECTOR) {
|
|
104
|
-
const { localName } = parseAstName(unescapeSelector(node.name));
|
|
105
|
-
if (localName !== '*') {
|
|
106
|
-
tagKey = tagKey ?? localName.toLowerCase();
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
current = current.prev;
|
|
110
|
-
}
|
|
111
|
-
subjects.push({ id: idKey, className: classKey, tag: tagKey });
|
|
112
|
-
}
|
|
91
|
+
if (!REG_SELECTOR.test(selector)) {
|
|
92
|
+
subjects = extractSubjectsRegExp(selector, caseSensitive);
|
|
93
|
+
} else {
|
|
94
|
+
try {
|
|
95
|
+
const ast = this.#finder.getAST(selector);
|
|
96
|
+
subjects = extractSubjectsAst(ast);
|
|
97
|
+
} catch (e) {
|
|
98
|
+
// fall through
|
|
113
99
|
}
|
|
114
|
-
} catch (e) {
|
|
115
|
-
// fall through
|
|
116
100
|
}
|
|
117
101
|
if (!subjects.length) {
|
|
118
102
|
subjects.push({ id: null, className: null, tag: null });
|
package/src/js/matcher.js
CHANGED
|
@@ -41,12 +41,16 @@ const REG_LANG_VALID = new RegExp(`^(?:\\*-)?${ALPHA_NUM}${LANG_PART}$`, 'i');
|
|
|
41
41
|
* @param {string} astType - The type of the selector from the AST.
|
|
42
42
|
* @param {object} [opt] - Optional parameters.
|
|
43
43
|
* @param {boolean} [opt.forgive] - If true, ignores unknown pseudo-elements.
|
|
44
|
+
* @param {object} [opt.globalObject] - The global object.
|
|
44
45
|
* @param {boolean} [opt.warn] - If true, throws an error for unsupported ones.
|
|
45
46
|
* @throws {DOMException} If the selector is invalid or unsupported.
|
|
46
47
|
* @returns {void}
|
|
47
48
|
*/
|
|
48
|
-
export const matchPseudoElementSelector = (
|
|
49
|
-
|
|
49
|
+
export const matchPseudoElementSelector = (
|
|
50
|
+
astName,
|
|
51
|
+
astType,
|
|
52
|
+
{ forgive, globalObject, warn } = {}
|
|
53
|
+
) => {
|
|
50
54
|
if (astType !== PS_ELEMENT_SELECTOR) {
|
|
51
55
|
// Ensure the AST node is a pseudo-element selector.
|
|
52
56
|
throw new TypeError(`Unexpected ast type ${getType(astType)}`);
|
|
@@ -96,8 +100,8 @@ export const matchPseudoElementSelector = (astName, astType, opt = {}) => {
|
|
|
96
100
|
globalObject
|
|
97
101
|
);
|
|
98
102
|
}
|
|
99
|
-
// Throw an error for unknown pseudo-elements if not forgiven.
|
|
100
103
|
} else if (!forgive) {
|
|
104
|
+
// Throw an error for unknown pseudo-elements if not forgiven.
|
|
101
105
|
throw generateException(
|
|
102
106
|
`Unknown pseudo-element ::${astName}`,
|
|
103
107
|
SYNTAX_ERR,
|
|
@@ -301,16 +305,20 @@ export const matchReadOnlyPseudoClass = (astName, node) => {
|
|
|
301
305
|
* @param {object} [opt] - Optional parameters.
|
|
302
306
|
* @param {boolean} [opt.check] - True if running in an internal check.
|
|
303
307
|
* @param {boolean} [opt.forgive] - True to forgive certain syntax errors.
|
|
308
|
+
* @param {object} [opt.globalObject] - The global object.
|
|
304
309
|
* @returns {boolean} - True if the attribute selector matches, otherwise false.
|
|
305
310
|
*/
|
|
306
|
-
export const matchAttributeSelector = (
|
|
311
|
+
export const matchAttributeSelector = (
|
|
312
|
+
ast,
|
|
313
|
+
node,
|
|
314
|
+
{ check, forgive, globalObject } = {}
|
|
315
|
+
) => {
|
|
307
316
|
const {
|
|
308
317
|
flags: astFlags,
|
|
309
318
|
matcher: astMatcher,
|
|
310
319
|
name: astName,
|
|
311
320
|
value: astValue
|
|
312
321
|
} = ast;
|
|
313
|
-
const { check, forgive, globalObject } = opt;
|
|
314
322
|
// Validate selector flags ('i' or 's').
|
|
315
323
|
if (typeof astFlags === 'string' && !/^[is]$/i.test(astFlags) && !forgive) {
|
|
316
324
|
const css = generateCSS(ast);
|
|
@@ -409,8 +417,8 @@ export const matchAttributeSelector = (ast, node, opt = {}) => {
|
|
|
409
417
|
}
|
|
410
418
|
}
|
|
411
419
|
}
|
|
412
|
-
// Handle non-namespaced attribute names.
|
|
413
420
|
} else {
|
|
421
|
+
// Handle non-namespaced attribute names.
|
|
414
422
|
for (let { name: itemName, value: itemValue } of attributes) {
|
|
415
423
|
if (caseInsensitive) {
|
|
416
424
|
itemName = itemName.toLowerCase();
|
|
@@ -425,8 +433,8 @@ export const matchAttributeSelector = (ast, node, opt = {}) => {
|
|
|
425
433
|
// The attribute is starting with ':'.
|
|
426
434
|
if (!itemPrefix && astAttrName === `:${itemLocalName}`) {
|
|
427
435
|
attrValues.add(itemValue);
|
|
428
|
-
// Ignore the 'xml:lang' attribute.
|
|
429
436
|
} else if (itemPrefix === 'xml' && itemLocalName === 'lang') {
|
|
437
|
+
// Ignore the 'xml:lang' attribute.
|
|
430
438
|
continue;
|
|
431
439
|
} else if (astAttrName === itemLocalName) {
|
|
432
440
|
attrValues.add(itemValue);
|
|
@@ -537,12 +545,16 @@ export const matchAttributeSelector = (ast, node, opt = {}) => {
|
|
|
537
545
|
* @param {object} [opt] - options
|
|
538
546
|
* @param {boolean} [opt.check] - running in internal check()
|
|
539
547
|
* @param {boolean} [opt.forgive] - forgive undeclared namespace
|
|
548
|
+
* @param {object} [opt.globalObject] - The global object.
|
|
540
549
|
* @returns {boolean} - result
|
|
541
550
|
*/
|
|
542
|
-
export const matchTypeSelector = (
|
|
551
|
+
export const matchTypeSelector = (
|
|
552
|
+
ast,
|
|
553
|
+
node,
|
|
554
|
+
{ check, forgive, globalObject } = {}
|
|
555
|
+
) => {
|
|
543
556
|
const astName = unescapeSelector(ast.name);
|
|
544
557
|
const { localName, namespaceURI, prefix } = node;
|
|
545
|
-
const { check, forgive, globalObject } = opt;
|
|
546
558
|
let { prefix: astPrefix, localName: astLocalName } = parseAstName(
|
|
547
559
|
astName,
|
|
548
560
|
node
|