@asamuzakjp/dom-selector 8.1.5 → 8.2.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/src/js/matcher.js CHANGED
@@ -33,6 +33,8 @@ const KEYS_FORM_PS_DISABLED = new Set([
33
33
  'option'
34
34
  ]);
35
35
  const KEYS_INPUT_EDIT = new Set(INPUT_EDIT);
36
+
37
+ /* regexp */
36
38
  const REG_LANG_VALID = new RegExp(`^(?:\\*-)?${ALPHA_NUM}${LANG_PART}$`, 'i');
37
39
 
38
40
  /**
package/src/js/nwsapi.js CHANGED
@@ -5,12 +5,12 @@
5
5
  */
6
6
 
7
7
  /* import */
8
- import { GenerationalCache } from '@asamuzakjp/generational-cache';
8
+ import { LRUCache } from 'lru-cache';
9
9
  import { isContentEditable } from './utility.js';
10
10
 
11
11
  /* constants */
12
12
  import { DOCUMENT_NODE, ELEMENT_NODE } from './constant.js';
13
- const CACHE_SIZE = 2048;
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';
@@ -465,13 +465,12 @@ export class Nwsapi {
465
465
  constructor(window, document, cacheSize = CACHE_SIZE) {
466
466
  this.#window = window;
467
467
  const cacheOpt = {
468
- cacheFunction: true,
469
- strictValidate: false
468
+ max: cacheSize
470
469
  };
471
- this.#matchLambdas = new GenerationalCache(cacheSize, cacheOpt);
472
- this.#selectLambdas = new GenerationalCache(cacheSize, cacheOpt);
473
- this.#matchResolvers = new GenerationalCache(cacheSize, cacheOpt);
474
- this.#selectResolvers = new GenerationalCache(cacheSize, cacheOpt);
470
+ this.#matchLambdas = new LRUCache(cacheOpt);
471
+ this.#selectLambdas = new LRUCache(cacheOpt);
472
+ this.#matchResolvers = new LRUCache(cacheOpt);
473
+ this.#selectResolvers = new LRUCache(cacheOpt);
475
474
  this.#nthChildState = {
476
475
  idx: 0,
477
476
  len: 0,
package/src/js/parser.js CHANGED
@@ -55,10 +55,12 @@ const KEYS_PS_CLASS_STATE = new Set([
55
55
  'valid'
56
56
  ]);
57
57
  const KEYS_SHADOW_HOST = new Set(['host', 'host-context']);
58
+ const U_FFFD = '\uFFFD';
59
+
60
+ /* regexp */
58
61
  const REG_EMPTY_PS_FUNC =
59
62
  /(?<=:(?:dir|has|host(?:-context)?|is|lang|not|nth-(?:last-)?(?:child|of-type)|where))\(\s+\)/g;
60
63
  const REG_SHADOW_PS_ELEMENT = /^part|slotted$/;
61
- const U_FFFD = '\uFFFD';
62
64
 
63
65
  /**
64
66
  * Unescapes a CSS selector string.
package/src/js/utility.js CHANGED
@@ -45,6 +45,8 @@ const KEYS_NODE_FOCUSABLE_SVG = new Set([
45
45
  'symbol',
46
46
  'title'
47
47
  ]);
48
+
49
+ /* regexp */
48
50
  const REG_IS_HTML = /^(?:application\/xhtml\+x|text\/ht)ml$/;
49
51
  const REG_IS_XML =
50
52
  /^(?:application\/(?:[\w\-.]+\+)?|image\/[\w\-.]+\+|text\/)xml$/;
@@ -90,43 +92,6 @@ export const generateException = (msg, name, globalObject = globalThis) => {
90
92
  return new globalObject.DOMException(msg, name);
91
93
  };
92
94
 
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
95
  /**
131
96
  * Resolve content document, root node, and check if it's in a shadow DOM.
132
97
  * @param {object} node - Document, DocumentFragment, or Element node.
@@ -2,6 +2,8 @@ export const ATRULE: "Atrule";
2
2
  export const ATTR_SELECTOR: "AttributeSelector";
3
3
  export const CLASS_SELECTOR: "ClassSelector";
4
4
  export const COMBINATOR: "Combinator";
5
+ export const DIR_NEXT: "next";
6
+ export const DIR_PREV: "prev";
5
7
  export const IDENT: "Identifier";
6
8
  export const ID_SELECTOR: "IdSelector";
7
9
  export const NOT_SUPPORTED_ERR: "NotSupportedError";
@@ -0,0 +1,59 @@
1
+ export class Evaluator {
2
+ constructor(window: object);
3
+ window: object;
4
+ documentCache: WeakMap<WeakKey, any>;
5
+ setup(selector: string, node: object, opt?: {
6
+ check?: boolean | undefined;
7
+ noexcept?: boolean | undefined;
8
+ warn?: boolean | undefined;
9
+ }): object;
10
+ check: boolean | undefined;
11
+ noexcept: boolean | undefined;
12
+ warn: boolean | undefined;
13
+ matchOpts: {
14
+ warn: boolean;
15
+ } | undefined;
16
+ node: object | undefined;
17
+ pseudoElements: any[] | undefined;
18
+ invalidate: boolean | undefined;
19
+ onError: (e: Error, opt?: {
20
+ noexcept?: boolean | undefined;
21
+ }) => void;
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
+ yieldTraverseAllDescendants(baseNode: object, leaves: Array<object>, opt: object): Generator<any, void, unknown>;
39
+ yieldFindDescendantNodes(leaves: Array<object>, baseNode: object, opt: object): Generator<any, void, unknown>;
40
+ private _handleFocusEvent;
41
+ private _handleKeyboardEvent;
42
+ private _handleMouseEvent;
43
+ private _registerEventListeners;
44
+ private _getSelectorBranches;
45
+ private _filterNthChildOfSelectorBranches;
46
+ private _matchAnPlusB;
47
+ private _hasCombinatorMatch;
48
+ private _matchHasPseudoFunc;
49
+ private _buildHasAllowlist;
50
+ private _evaluateHasPseudo;
51
+ private _matchLogicalPseudoFunc;
52
+ private _evaluateLogicalPseudo;
53
+ private _evaluatePseudoClassFunc;
54
+ private _evaluateHostPseudo;
55
+ private _evaluateHostContextPseudo;
56
+ private _matchSelectorForElement;
57
+ private _matchSelectorForShadowRoot;
58
+ #private;
59
+ }
@@ -1,45 +1,8 @@
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 {
2
+ find: (targetType: string) => Set<object> | object;
3
+ getAST: (selector: string) => object;
16
4
  private _processSelectorBranches;
17
5
  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
6
  private _traverseAndCollectNodes;
44
7
  private _findPrecede;
45
8
  private _findNodeWalker;
@@ -54,12 +17,10 @@ export class Finder {
54
17
  private _determineTraversalStrategy;
55
18
  private _processPendingItems;
56
19
  private _collectNodes;
57
- private _getCombinedNodes;
58
20
  private _matchNodeNext;
59
- private _matchNodePrev;
21
+ private _hasValidPathPrev;
60
22
  private _processComplexBranchAll;
61
23
  private _processComplexBranchFirst;
62
- find: (targetType: string) => Set<object>;
63
- getAST: (selector: string) => object;
64
24
  #private;
65
25
  }
26
+ import { Evaluator } from './evaluator.js';
@@ -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 }?: {