@asamuzakjp/dom-selector 8.1.4 → 8.2.0
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 +7 -7
- package/src/index.js +15 -32
- package/src/js/evaluator.js +2149 -0
- package/src/js/finder.js +477 -2572
- package/src/js/nwsapi.js +1 -1
- package/src/js/selector.js +11 -3
- package/src/js/utility.js +0 -37
- package/types/js/evaluator.d.ts +58 -0
- package/types/js/finder.d.ts +4 -43
- package/types/js/utility.d.ts +0 -5
package/src/js/nwsapi.js
CHANGED
|
@@ -10,7 +10,7 @@ import { isContentEditable } from './utility.js';
|
|
|
10
10
|
|
|
11
11
|
/* constants */
|
|
12
12
|
import { DOCUMENT_NODE, ELEMENT_NODE } from './constant.js';
|
|
13
|
-
const CACHE_SIZE =
|
|
13
|
+
const CACHE_SIZE = 4096;
|
|
14
14
|
const F_INIT = '"use strict";return function resolver(c,f,x,r)';
|
|
15
15
|
const S_HEAD = 'var e,n,o,j=r.length-1,k=-1';
|
|
16
16
|
const M_HEAD = 'var e,n,o';
|
package/src/js/selector.js
CHANGED
|
@@ -8,6 +8,7 @@ import { generateException } from './utility.js';
|
|
|
8
8
|
|
|
9
9
|
/* constants */
|
|
10
10
|
import {
|
|
11
|
+
ATTR_TYPE,
|
|
11
12
|
COMBINATOR,
|
|
12
13
|
COMBO,
|
|
13
14
|
COMPOUND_I,
|
|
@@ -21,8 +22,8 @@ import {
|
|
|
21
22
|
PS_CLASS_SELECTOR,
|
|
22
23
|
PS_ELEMENT_SELECTOR,
|
|
23
24
|
SELECTOR,
|
|
24
|
-
SUB_TYPE,
|
|
25
25
|
SYNTAX_ERR,
|
|
26
|
+
TAG_TYPE_WO_UNIVERSAL,
|
|
26
27
|
TARGET_ALL
|
|
27
28
|
} from './constant.js';
|
|
28
29
|
|
|
@@ -30,7 +31,7 @@ import {
|
|
|
30
31
|
const REG_EXCLUDE_BASIC =
|
|
31
32
|
/[|\\]|::|[^\u0021-\u007F\s]|\[\s*[\w$*=^|~-]+(?:(?:"[\w$*=^|~\s'-]+"|'[\w$*=^|~\s"-]+')?(?:\s+[\w$*=^|~-]+)+|"[^"\]]{1,255}|'[^'\]]{1,255})\s*\]|:(?:is|where)\(\s*\)/;
|
|
32
33
|
const REG_EXCLUDE_QSA = new RegExp(
|
|
33
|
-
`(?:^(?:[A-Z]|\\.)[\\w-]
|
|
34
|
+
`(?:^(?:[A-Z]|\\.)[\\w-]*$|${COMPOUND_I}${COMBO}${COMPOUND_I})`,
|
|
34
35
|
'i'
|
|
35
36
|
);
|
|
36
37
|
const REG_COMPLEX = new RegExp(`${COMPOUND_I}${COMBO}${COMPOUND_I}`, 'i');
|
|
@@ -51,6 +52,9 @@ const REG_CLASS = /\.(\D[^#.*]+)/g;
|
|
|
51
52
|
const REG_TAG = /^([^#.]+)/;
|
|
52
53
|
const REG_INVALID_SYNTAX =
|
|
53
54
|
/[+~>]\s*[+~>]|^\s*[+~>]|[+~>]\s*$|^\s*,|,\s*,|,\s*$/;
|
|
55
|
+
const REG_TEST_LIB = new RegExp(
|
|
56
|
+
`^(?:[*]?${ATTR_TYPE}(?:\\s*,\\s*${TAG_TYPE_WO_UNIVERSAL}${COMBO}${TAG_TYPE_WO_UNIVERSAL})?)$`
|
|
57
|
+
);
|
|
54
58
|
|
|
55
59
|
/**
|
|
56
60
|
* Find a nested :has() pseudo-class.
|
|
@@ -278,7 +282,11 @@ export const filterSelector = (selector, target) => {
|
|
|
278
282
|
return false;
|
|
279
283
|
}
|
|
280
284
|
// Target-specific early exits.
|
|
281
|
-
if (
|
|
285
|
+
if (
|
|
286
|
+
target === TARGET_ALL &&
|
|
287
|
+
REG_EXCLUDE_QSA.test(selector) &&
|
|
288
|
+
!REG_TEST_LIB.test(selector)
|
|
289
|
+
) {
|
|
282
290
|
return false;
|
|
283
291
|
}
|
|
284
292
|
// Exclude various complex or unsupported selectors early.
|
package/src/js/utility.js
CHANGED
|
@@ -90,43 +90,6 @@ export const generateException = (msg, name, globalObject = globalThis) => {
|
|
|
90
90
|
return new globalObject.DOMException(msg, name);
|
|
91
91
|
};
|
|
92
92
|
|
|
93
|
-
/**
|
|
94
|
-
* Filter a list of nodes based on An+B logic
|
|
95
|
-
* @param {Array.<object>} nodes - array of nodes to filter
|
|
96
|
-
* @param {object} anb - An+B options
|
|
97
|
-
* @param {number} anb.a - a
|
|
98
|
-
* @param {number} anb.b - b
|
|
99
|
-
* @param {boolean} [anb.reverse] - reverse order
|
|
100
|
-
* @returns {Array.<object>} - array of matched nodes
|
|
101
|
-
*/
|
|
102
|
-
export const filterNodesByAnB = (nodes, anb) => {
|
|
103
|
-
const { a, b, reverse } = anb;
|
|
104
|
-
const processedNodes = reverse ? [...nodes].reverse() : nodes;
|
|
105
|
-
const l = nodes.length;
|
|
106
|
-
const matched = [];
|
|
107
|
-
if (a === 0) {
|
|
108
|
-
if (b > 0 && b <= l) {
|
|
109
|
-
matched.push(processedNodes[b - 1]);
|
|
110
|
-
}
|
|
111
|
-
return matched;
|
|
112
|
-
}
|
|
113
|
-
let startIndex = b - 1;
|
|
114
|
-
if (a > 0) {
|
|
115
|
-
while (startIndex < 0) {
|
|
116
|
-
startIndex += a;
|
|
117
|
-
}
|
|
118
|
-
for (let i = startIndex; i < l; i += a) {
|
|
119
|
-
matched.push(processedNodes[i]);
|
|
120
|
-
}
|
|
121
|
-
} else if (startIndex >= 0) {
|
|
122
|
-
for (let i = startIndex; i >= 0; i += a) {
|
|
123
|
-
matched.push(processedNodes[i]);
|
|
124
|
-
}
|
|
125
|
-
return matched.reverse();
|
|
126
|
-
}
|
|
127
|
-
return matched;
|
|
128
|
-
};
|
|
129
|
-
|
|
130
93
|
/**
|
|
131
94
|
* Resolve content document, root node, and check if it's in a shadow DOM.
|
|
132
95
|
* @param {object} node - Document, DocumentFragment, or Element node.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export class Evaluator {
|
|
2
|
+
constructor(window: object);
|
|
3
|
+
window: object;
|
|
4
|
+
documentCache: WeakMap<WeakKey, any>;
|
|
5
|
+
onError: (e: Error, opt?: {
|
|
6
|
+
noexcept?: boolean | undefined;
|
|
7
|
+
}) => void;
|
|
8
|
+
setup(selector: string, node: object, opt?: {
|
|
9
|
+
check?: boolean | undefined;
|
|
10
|
+
noexcept?: boolean | undefined;
|
|
11
|
+
warn?: boolean | undefined;
|
|
12
|
+
}): object;
|
|
13
|
+
check: boolean | undefined;
|
|
14
|
+
noexcept: boolean | undefined;
|
|
15
|
+
warn: boolean | undefined;
|
|
16
|
+
matchOpts: {
|
|
17
|
+
warn: boolean;
|
|
18
|
+
} | undefined;
|
|
19
|
+
node: object | undefined;
|
|
20
|
+
pseudoElements: any[] | undefined;
|
|
21
|
+
invalidate: boolean | undefined;
|
|
22
|
+
clearResults(all?: boolean): void;
|
|
23
|
+
matchSelector: (ast: object, node: object, opt: object) => boolean;
|
|
24
|
+
matchLeaves: (leaves: Array<object>, node: object, opt: object) => boolean;
|
|
25
|
+
getFilterLeaves: (leaves: Array<object>) => Array<object>;
|
|
26
|
+
evaluateShadowHost: (ast: object, node: object) => boolean;
|
|
27
|
+
matchPseudoClassSelector: (ast: object, node: object, opt?: {
|
|
28
|
+
forgive?: boolean | undefined;
|
|
29
|
+
warn?: boolean | undefined;
|
|
30
|
+
}) => Set<object> | boolean;
|
|
31
|
+
createTreeWalker: (node: object, opt?: {
|
|
32
|
+
force?: boolean | undefined;
|
|
33
|
+
whatToShow?: number | undefined;
|
|
34
|
+
}) => object;
|
|
35
|
+
yieldCombinatorMatches(twig: object, node: object, opt?: {
|
|
36
|
+
dir?: string | undefined;
|
|
37
|
+
}): Generator<any, void, unknown>;
|
|
38
|
+
private _handleFocusEvent;
|
|
39
|
+
private _handleKeyboardEvent;
|
|
40
|
+
private _handleMouseEvent;
|
|
41
|
+
private _registerEventListeners;
|
|
42
|
+
private _getSelectorBranches;
|
|
43
|
+
private _matchAnPlusB;
|
|
44
|
+
private _hasCombinatorMatch;
|
|
45
|
+
private _matchHasPseudoFunc;
|
|
46
|
+
private _buildHasAllowlist;
|
|
47
|
+
private _evaluateHasPseudo;
|
|
48
|
+
private _matchLogicalPseudoFunc;
|
|
49
|
+
private _evaluateLogicalPseudo;
|
|
50
|
+
private _evaluatePseudoClassFunc;
|
|
51
|
+
private _evaluateHostPseudo;
|
|
52
|
+
private _evaluateHostContextPseudo;
|
|
53
|
+
private _matchSelectorForElement;
|
|
54
|
+
private _matchSelectorForShadowRoot;
|
|
55
|
+
private _traverseAllDescendants;
|
|
56
|
+
private _findDescendantNodes;
|
|
57
|
+
#private;
|
|
58
|
+
}
|
package/types/js/finder.d.ts
CHANGED
|
@@ -1,45 +1,6 @@
|
|
|
1
|
-
export class Finder {
|
|
2
|
-
constructor(window: object);
|
|
3
|
-
onError: (e: Error, opt?: {
|
|
4
|
-
noexcept?: boolean | undefined;
|
|
5
|
-
}) => void;
|
|
6
|
-
setup: (selector: string, node: object, opt?: {
|
|
7
|
-
check?: boolean | undefined;
|
|
8
|
-
noexcept?: boolean | undefined;
|
|
9
|
-
warn?: boolean | undefined;
|
|
10
|
-
}) => object;
|
|
11
|
-
clearResults: (all?: boolean) => void;
|
|
12
|
-
private _handleFocusEvent;
|
|
13
|
-
private _handleKeyboardEvent;
|
|
14
|
-
private _handleMouseEvent;
|
|
15
|
-
private _registerEventListeners;
|
|
1
|
+
export class Finder extends Evaluator {
|
|
16
2
|
private _processSelectorBranches;
|
|
17
3
|
private _correspond;
|
|
18
|
-
private _createTreeWalker;
|
|
19
|
-
private _getSelectorBranches;
|
|
20
|
-
private _getFilteredChildren;
|
|
21
|
-
private _collectNthChild;
|
|
22
|
-
private _collectNthOfType;
|
|
23
|
-
private _matchAnPlusB;
|
|
24
|
-
private _matchHasPseudoFunc;
|
|
25
|
-
private _buildHasAllowlist;
|
|
26
|
-
private _evaluateHasPseudo;
|
|
27
|
-
private _matchLogicalPseudoFunc;
|
|
28
|
-
private _evaluateLogicalPseudo;
|
|
29
|
-
private _evaluatePseudoClassFunc;
|
|
30
|
-
private _matchPseudoClassSelector;
|
|
31
|
-
private _evaluateHostPseudo;
|
|
32
|
-
private _evaluateHostContextPseudo;
|
|
33
|
-
private _evaluateShadowHost;
|
|
34
|
-
private _matchSelectorForElement;
|
|
35
|
-
private _matchSelectorForShadowRoot;
|
|
36
|
-
private _matchSelector;
|
|
37
|
-
private _matchLeaves;
|
|
38
|
-
private _getFilterLeaves;
|
|
39
|
-
private _traverseAllDescendants;
|
|
40
|
-
private _findDescendantNodes;
|
|
41
|
-
private _collectCombinatorMatches;
|
|
42
|
-
private _matchCombinator;
|
|
43
4
|
private _traverseAndCollectNodes;
|
|
44
5
|
private _findPrecede;
|
|
45
6
|
private _findNodeWalker;
|
|
@@ -54,12 +15,12 @@ export class Finder {
|
|
|
54
15
|
private _determineTraversalStrategy;
|
|
55
16
|
private _processPendingItems;
|
|
56
17
|
private _collectNodes;
|
|
57
|
-
private _getCombinedNodes;
|
|
58
18
|
private _matchNodeNext;
|
|
59
|
-
private
|
|
19
|
+
private _hasValidPathPrev;
|
|
60
20
|
private _processComplexBranchAll;
|
|
61
21
|
private _processComplexBranchFirst;
|
|
62
|
-
find: (targetType: string) => Set<object
|
|
22
|
+
find: (targetType: string) => Set<object> | object;
|
|
63
23
|
getAST: (selector: string) => object;
|
|
64
24
|
#private;
|
|
65
25
|
}
|
|
26
|
+
import { Evaluator } from './evaluator.js';
|
package/types/js/utility.d.ts
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
export function getType(o: object): string;
|
|
2
2
|
export function verifyArray(arr: any[], type: string): any[];
|
|
3
3
|
export function generateException(msg: string, name: string, globalObject?: object): DOMException;
|
|
4
|
-
export function filterNodesByAnB(nodes: Array<object>, anb: {
|
|
5
|
-
a: number;
|
|
6
|
-
b: number;
|
|
7
|
-
reverse?: boolean | undefined;
|
|
8
|
-
}): Array<object>;
|
|
9
4
|
export function resolveContent(node: object): Array<object | boolean>;
|
|
10
5
|
export function traverseNode(node: object, walker: object, force?: boolean): object | null;
|
|
11
6
|
export function isCustomElement(node: object, { formAssociated }?: {
|