@asamuzakjp/dom-selector 7.1.1 → 8.0.0-a.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 +5 -5
- package/src/index.js +24 -40
- package/src/js/event-tracker.js +131 -0
- package/src/js/finder.js +170 -1512
- package/src/js/matcher.js +21 -9
- package/src/js/nwsapi.js +1320 -0
- package/src/js/parser.js +41 -2
- package/src/js/pseudo-evaluator.js +1226 -0
- package/src/js/utility.js +54 -33
- package/types/index.d.ts +1 -1
- package/types/js/event-tracker.d.ts +12 -0
- package/types/js/finder.d.ts +12 -11
- 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/pseudo-evaluator.d.ts +11 -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.1"
|
|
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 });
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* event-tracker.js
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/* constants */
|
|
6
|
+
const KEYS_MODIFIER = new Set([
|
|
7
|
+
'Alt',
|
|
8
|
+
'AltGraph',
|
|
9
|
+
'CapsLock',
|
|
10
|
+
'Control',
|
|
11
|
+
'Fn',
|
|
12
|
+
'FnLock',
|
|
13
|
+
'Hyper',
|
|
14
|
+
'Meta',
|
|
15
|
+
'NumLock',
|
|
16
|
+
'ScrollLock',
|
|
17
|
+
'Shift',
|
|
18
|
+
'Super',
|
|
19
|
+
'Symbol',
|
|
20
|
+
'SymbolLock'
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A class to track and manage browser events for the Finder.
|
|
25
|
+
*/
|
|
26
|
+
export class EventTracker {
|
|
27
|
+
#event = null;
|
|
28
|
+
#eventHandlers;
|
|
29
|
+
#focus = null;
|
|
30
|
+
#lastFocusVisible = null;
|
|
31
|
+
#window;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Creates an instance of EventTracker.
|
|
35
|
+
* @param {object} window - The browser window object.
|
|
36
|
+
*/
|
|
37
|
+
constructor(window) {
|
|
38
|
+
this.#window = window;
|
|
39
|
+
this.#eventHandlers = new Set([
|
|
40
|
+
{ keys: ['focus', 'focusin'], handler: this._handleFocusEvent },
|
|
41
|
+
{ keys: ['keydown', 'keyup'], handler: this._handleKeyboardEvent },
|
|
42
|
+
{
|
|
43
|
+
keys: ['mouseover', 'mousedown', 'mouseup', 'click', 'mouseout'],
|
|
44
|
+
handler: this._handleMouseEvent
|
|
45
|
+
}
|
|
46
|
+
]);
|
|
47
|
+
this._registerEventListeners();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Gets the last significant UI event.
|
|
52
|
+
* @returns {Event|null} The last event.
|
|
53
|
+
*/
|
|
54
|
+
get event() {
|
|
55
|
+
return this.#event;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Gets the last focus event.
|
|
60
|
+
* @returns {FocusEvent|null} The last focus event.
|
|
61
|
+
*/
|
|
62
|
+
get focus() {
|
|
63
|
+
return this.#focus;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Gets the last element that had a focus-visible state.
|
|
68
|
+
* @returns {Element|null} The last focus-visible element.
|
|
69
|
+
*/
|
|
70
|
+
get lastFocusVisible() {
|
|
71
|
+
return this.#lastFocusVisible;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Sets the last element that had a focus-visible state.
|
|
76
|
+
* @param {Element|null} node - The element to set.
|
|
77
|
+
*/
|
|
78
|
+
set lastFocusVisible(node) {
|
|
79
|
+
this.#lastFocusVisible = node;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Handles focus events.
|
|
84
|
+
* @private
|
|
85
|
+
* @param {FocusEvent} evt - The focus event.
|
|
86
|
+
*/
|
|
87
|
+
_handleFocusEvent = evt => {
|
|
88
|
+
this.#focus = evt;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Handles keyboard events. Modifier keys are ignored.
|
|
93
|
+
* @private
|
|
94
|
+
* @param {KeyboardEvent} evt - The keyboard event.
|
|
95
|
+
*/
|
|
96
|
+
_handleKeyboardEvent = evt => {
|
|
97
|
+
const { key } = evt;
|
|
98
|
+
if (!KEYS_MODIFIER.has(key)) {
|
|
99
|
+
this.#event = evt;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Handles mouse events.
|
|
105
|
+
* @private
|
|
106
|
+
* @param {MouseEvent} evt - The mouse event.
|
|
107
|
+
*/
|
|
108
|
+
_handleMouseEvent = evt => {
|
|
109
|
+
this.#event = evt;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Registers event listeners to the window object.
|
|
114
|
+
* @private
|
|
115
|
+
* @returns {Array<void>} An array of return values from addEventListener.
|
|
116
|
+
*/
|
|
117
|
+
_registerEventListeners = () => {
|
|
118
|
+
const func = [];
|
|
119
|
+
for (const { keys, handler } of this.#eventHandlers) {
|
|
120
|
+
for (const key of keys) {
|
|
121
|
+
func.push(
|
|
122
|
+
this.#window.addEventListener(key, handler, {
|
|
123
|
+
capture: true,
|
|
124
|
+
passive: true
|
|
125
|
+
})
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return func;
|
|
130
|
+
};
|
|
131
|
+
}
|