@asamuzakjp/dom-selector 8.1.4 → 8.1.5

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 CHANGED
@@ -76,7 +76,7 @@
76
76
  "bench:ps-has": "node --expose-gc benchmark/bench-has.js",
77
77
  "bench:ps-nth": "node --expose-gc benchmark/bench-nth-child.js && node --expose-gc benchmark/bench-nth-of-type.js",
78
78
  "bench:ps-traverse": "node --expose-gc benchmark/bench-dir.js && node --expose-gc benchmark/bench-lang.js",
79
- "bench:ts": "node --expose-gc benchmark/bench-testing-library.js",
79
+ "bench:tl": "node --expose-gc benchmark/bench-testing-library.js",
80
80
  "bench:universal": "node --expose-gc benchmark/bench-universal.js",
81
81
  "build": "npm run tsc && npm run lint && npm test",
82
82
  "lint": "eslint --fix .",
@@ -88,5 +88,5 @@
88
88
  "engines": {
89
89
  "node": "^22.13.0 || >=24.0.0"
90
90
  },
91
- "version": "8.1.4"
91
+ "version": "8.1.5"
92
92
  }
package/src/index.js CHANGED
@@ -19,11 +19,8 @@ import { collectAllDescendants, getType } from './js/utility.js';
19
19
 
20
20
  /* constants */
21
21
  import {
22
- ATTR_TYPE,
23
- COMBO,
24
22
  DOCUMENT_NODE,
25
23
  ELEMENT_NODE,
26
- TAG_TYPE_WO_UNIVERSAL,
27
24
  TARGET_ALL,
28
25
  TARGET_FIRST,
29
26
  TARGET_LINEAL,
@@ -33,9 +30,6 @@ const CACHE_SIZE = 2048;
33
30
 
34
31
  /* regexp */
35
32
  const REG_SELECTOR = /[[\]():\\"'`]/;
36
- const REG_TEST_LIB = new RegExp(
37
- `^(?:${TAG_TYPE_WO_UNIVERSAL}|[*]?${ATTR_TYPE}(?:\\s*,\\s*${TAG_TYPE_WO_UNIVERSAL}${COMBO}${TAG_TYPE_WO_UNIVERSAL})?)$`
38
- );
39
33
  const REG_UNIVERSAL = /^(?:\*\|)?\*$/;
40
34
 
41
35
  /**
@@ -351,13 +345,11 @@ export class DOMSelector {
351
345
  if (REG_UNIVERSAL.test(selector)) {
352
346
  return node.firstElementChild;
353
347
  }
348
+ // Only enabled when debugging.
354
349
  /*
355
350
  const document =
356
351
  node.nodeType === DOCUMENT_NODE ? node : node.ownerDocument;
357
- if (
358
- (node === this.#document || REG_TEST_LIB.test(selector)) &&
359
- this.#canUseNwsapi(document)
360
- ) {
352
+ if (node === this.#document && this.#canUseNwsapi(document)) {
361
353
  const cacheKey = `querySelector_${selector}`;
362
354
  let filterMatches = this.#cache.get(cacheKey);
363
355
  if (filterMatches === undefined) {
@@ -405,29 +397,17 @@ export class DOMSelector {
405
397
  return collectAllDescendants(node, document);
406
398
  }
407
399
  if (this.#canUseNwsapi(document)) {
408
- let routeToNwsapi = node === this.#document;
409
- if (!routeToNwsapi) {
410
- const testCacheKey = `testlib_${selector}`;
411
- let isTestLib = this.#cache.get(testCacheKey);
412
- if (isTestLib === undefined) {
413
- isTestLib = REG_TEST_LIB.test(selector);
414
- this.#cache.set(testCacheKey, isTestLib);
415
- }
416
- routeToNwsapi = isTestLib;
400
+ const cacheKey = `querySelectorAll_${selector}`;
401
+ let filterMatches = this.#cache.get(cacheKey);
402
+ if (filterMatches === undefined) {
403
+ filterMatches = filterSelector(selector, TARGET_ALL);
404
+ this.#cache.set(cacheKey, filterMatches);
417
405
  }
418
- if (routeToNwsapi) {
419
- const cacheKey = `querySelectorAll_${selector}`;
420
- let filterMatches = this.#cache.get(cacheKey);
421
- if (filterMatches === undefined) {
422
- filterMatches = filterSelector(selector, TARGET_ALL);
423
- this.#cache.set(cacheKey, filterMatches);
424
- }
425
- if (filterMatches) {
426
- try {
427
- return this.#nwsapi.select(selector, this.#wrapNode(node));
428
- } catch (e) {
429
- // fall through
430
- }
406
+ if (filterMatches) {
407
+ try {
408
+ return this.#nwsapi.select(selector, this.#wrapNode(node));
409
+ } catch (e) {
410
+ // fall through
431
411
  }
432
412
  }
433
413
  }
@@ -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-]*$|^(?:${SUB_TYPE}|:${N_TH})+$|${COMPOUND_I}${COMBO}${COMPOUND_I})`,
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 (target === TARGET_ALL && REG_EXCLUDE_QSA.test(selector)) {
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.