@asamuzakjp/dom-selector 6.5.4 → 6.5.6

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.
@@ -32,6 +32,7 @@ __export(index_exports, {
32
32
  DOMSelector: () => DOMSelector
33
33
  });
34
34
  module.exports = __toCommonJS(index_exports);
35
+ var import_lru_cache = require("lru-cache");
35
36
 
36
37
  // src/js/parser.js
37
38
  var import_css_tree2 = require("css-tree");
@@ -88,7 +89,7 @@ var ANB = `[+-]?(?:${DIGIT}n?|n)|(?:[+-]?${DIGIT})?n\\s*[+-]\\s*${DIGIT}`;
88
89
  var N_TH = `nth-(?:last-)?(?:child|of-type)\\(\\s*(?:even|odd|${ANB})\\s*\\)`;
89
90
  var SUB_TYPE = "\\[[^|\\]]+\\]|[#.:][\\w-]+";
90
91
  var SUB_TYPE_WO_PSEUDO = "\\[[^|\\]]+\\]|[#.][\\w-]+";
91
- var TAG_ID_CLASS = "(?:[A-Za-z][\\w-]*|[#.][\\w-]+)";
92
+ var SUB_CLASS_TYPE = "(?:\\.[\\w-]+)";
92
93
  var TAG_TYPE = "\\*|[A-Za-z][\\w-]*";
93
94
  var TAG_TYPE_I = "\\*|[A-Z][\\w-]*";
94
95
  var COMPOUND = `(?:${TAG_TYPE}|(?:${TAG_TYPE})?(?:${SUB_TYPE})+)`;
@@ -156,6 +157,7 @@ var KEY_PS_STATE = Object.freeze([
156
157
  var KEY_SHADOW_HOST = Object.freeze(["host", "host-context"]);
157
158
 
158
159
  // src/js/utility.js
160
+ var REG_EXCLUDE_FILTER = /[|\\]|::|[^\u0021-\u007F\s]|\[\s*[\w$*=^|~-]+(?:(?:"[\w$*=^|~\s'-]+"|'[\w$*=^|~\s"-]+')?(?:\s+[\w$*=^|~-]+)+|"[^"\]]{1,255}|'[^'\]]{1,255})\s*\]|:(?:is|where)\(\s*\)/;
159
161
  var REG_LOGIC_COMPLEX = new RegExp(`:(?!${PSEUDO_CLASS}|${N_TH}|${LOGIC_COMPLEX})`);
160
162
  var REG_LOGIC_COMPOUND = new RegExp(`:(?!${PSEUDO_CLASS}|${N_TH}|${LOGIC_COMPOUND})`);
161
163
  var REG_LOGIC_HAS_COMPOUND = new RegExp(`:(?!${PSEUDO_CLASS}|${N_TH}|${LOGIC_COMPOUND}|${HAS_COMPOUND})`);
@@ -684,7 +686,7 @@ var filterSelector = (selector, opt = {}) => {
684
686
  if (selector.includes("/")) {
685
687
  return false;
686
688
  }
687
- if (/[|\\]|::|[^\u0021-\u007F\s]|\[\s*[\w$*=^|~-]+(?:(?:"[\w$*=^|~\s'-]+"|'[\w$*=^|~\s"-]+')?(?:\s+[\w$*=^|~-]+)+|"[^"\]]{1,255}|'[^'\]]{1,255})\s*\]|:(?:is|where)\(\s*\)/.test(selector)) {
689
+ if (REG_EXCLUDE_FILTER.test(selector)) {
688
690
  return false;
689
691
  }
690
692
  if (selector.includes(":")) {
@@ -753,11 +755,7 @@ var unescapeSelector = (selector = "") => {
753
755
  }
754
756
  return selector;
755
757
  };
756
- var preprocess = (...args) => {
757
- if (!args.length) {
758
- throw new TypeError("1 argument required, but only 0 present.");
759
- }
760
- let [selector] = args;
758
+ var preprocess = (selector) => {
761
759
  if (typeof selector === "string") {
762
760
  let index = 0;
763
761
  while (index >= 0) {
@@ -1374,7 +1372,7 @@ var matchTypeSelector = (ast, node, opt = {}) => {
1374
1372
  prefix: astPrefix,
1375
1373
  localName: astLocalName
1376
1374
  } = parseAstName(astName, node);
1377
- if (node.ownerDocument.contentType === "text/html" && /[A-Z][\\w-]*/i.test(localName)) {
1375
+ if (node.ownerDocument.contentType === "text/html" && (!namespaceURI || namespaceURI === "http://www.w3.org/1999/xhtml") && /[A-Z][\\w-]*/i.test(localName)) {
1378
1376
  astPrefix = astPrefix.toLowerCase();
1379
1377
  astLocalName = astLocalName.toLowerCase();
1380
1378
  }
@@ -1473,7 +1471,7 @@ var Finder = class {
1473
1471
  * @param {Error} e - Error
1474
1472
  * @param {object} [opt] - options
1475
1473
  * @param {boolean} [opt.noexcept] - no exception
1476
- * @throws Error
1474
+ * @throws {Error} - Error
1477
1475
  * @returns {void}
1478
1476
  */
1479
1477
  onError(e, opt = {}) {
@@ -1511,12 +1509,12 @@ var Finder = class {
1511
1509
  this.#domSymbolTree = domSymbolTree;
1512
1510
  this.#noexcept = !!noexcept;
1513
1511
  this.#warn = !!warn;
1514
- this.#node = node;
1515
1512
  [
1516
1513
  this.#document,
1517
1514
  this.#root,
1518
1515
  this.#shadow
1519
1516
  ] = resolveContent(node);
1517
+ this.#node = node;
1520
1518
  this.#selector = selector;
1521
1519
  [
1522
1520
  this.#ast,
@@ -4253,10 +4251,11 @@ var Finder = class {
4253
4251
  };
4254
4252
 
4255
4253
  // src/index.js
4254
+ var MAX_CACHE = 4096;
4256
4255
  var REG_COMPLEX = new RegExp(`${COMPOUND_I}${COMBO}${COMPOUND_I}`, "i");
4257
4256
  var REG_DESCEND = new RegExp(`${COMPOUND_I}${DESCEND}${COMPOUND_I}`, "i");
4258
4257
  var REG_SIBLING = new RegExp(`${COMPOUND_I}${SIBLING}${COMPOUND_I}`, "i");
4259
- var REG_SIMPLE = new RegExp(`^${TAG_ID_CLASS}$`);
4258
+ var REG_SIMPLE = new RegExp(`^${SUB_CLASS_TYPE}$`);
4260
4259
  var DOMSelector = class {
4261
4260
  /* private fields */
4262
4261
  #window;
@@ -4265,6 +4264,7 @@ var DOMSelector = class {
4265
4264
  #finder;
4266
4265
  #idlUtils;
4267
4266
  #nwsapi;
4267
+ #cache;
4268
4268
  /**
4269
4269
  * construct
4270
4270
  * @param {object} window - window
@@ -4279,6 +4279,9 @@ var DOMSelector = class {
4279
4279
  this.#finder = new Finder(window);
4280
4280
  this.#idlUtils = idlUtils;
4281
4281
  this.#nwsapi = initNwsapi(window, document);
4282
+ this.#cache = new import_lru_cache.LRUCache({
4283
+ max: MAX_CACHE
4284
+ });
4282
4285
  }
4283
4286
  /**
4284
4287
  * @typedef CheckResult
@@ -4303,14 +4306,22 @@ var DOMSelector = class {
4303
4306
  }
4304
4307
  const document = this.#domSymbolTree ? node._ownerDocument : node.ownerDocument;
4305
4308
  if (document === this.#document && document.contentType === "text/html" && document.documentElement && node.parentNode) {
4306
- const filterOpt = {
4307
- complex: REG_COMPLEX.test(selector),
4308
- compound: false,
4309
- descend: false,
4310
- simple: false,
4311
- target: TARGET_SELF
4312
- };
4313
- if (filterSelector(selector, filterOpt)) {
4309
+ const cacheKey = `check_${selector}`;
4310
+ let filterMatches = false;
4311
+ if (this.#cache.has(cacheKey)) {
4312
+ filterMatches = this.#cache.get(cacheKey);
4313
+ } else {
4314
+ const filterOpt = {
4315
+ complex: REG_COMPLEX.test(selector),
4316
+ compound: false,
4317
+ descend: false,
4318
+ simple: false,
4319
+ target: TARGET_SELF
4320
+ };
4321
+ filterMatches = filterSelector(selector, filterOpt);
4322
+ this.#cache.set(cacheKey, filterMatches);
4323
+ }
4324
+ if (filterMatches) {
4314
4325
  try {
4315
4326
  const n = this.#idlUtils ? this.#idlUtils.wrapperForImpl(node) : node;
4316
4327
  const match = this.#nwsapi.match(selector, n);
@@ -4355,14 +4366,22 @@ var DOMSelector = class {
4355
4366
  }
4356
4367
  const document = this.#domSymbolTree ? node._ownerDocument : node.ownerDocument;
4357
4368
  if (document === this.#document && document.contentType === "text/html" && document.documentElement && node.parentNode) {
4358
- const filterOpt = {
4359
- complex: REG_COMPLEX.test(selector),
4360
- compound: false,
4361
- descend: false,
4362
- simple: false,
4363
- target: TARGET_SELF
4364
- };
4365
- if (filterSelector(selector, filterOpt)) {
4369
+ const cacheKey = `matches_${selector}`;
4370
+ let filterMatches = false;
4371
+ if (this.#cache.has(cacheKey)) {
4372
+ filterMatches = this.#cache.get(cacheKey);
4373
+ } else {
4374
+ const filterOpt = {
4375
+ complex: REG_COMPLEX.test(selector),
4376
+ compound: false,
4377
+ descend: false,
4378
+ simple: false,
4379
+ target: TARGET_SELF
4380
+ };
4381
+ filterMatches = filterSelector(selector, filterOpt);
4382
+ this.#cache.set(cacheKey, filterMatches);
4383
+ }
4384
+ if (filterMatches) {
4366
4385
  try {
4367
4386
  const n = this.#idlUtils ? this.#idlUtils.wrapperForImpl(node) : node;
4368
4387
  const res2 = this.#nwsapi.match(selector, n);
@@ -4402,14 +4421,22 @@ var DOMSelector = class {
4402
4421
  }
4403
4422
  const document = this.#domSymbolTree ? node._ownerDocument : node.ownerDocument;
4404
4423
  if (document === this.#document && document.contentType === "text/html" && document.documentElement && node.parentNode) {
4405
- const filterOpt = {
4406
- complex: REG_COMPLEX.test(selector),
4407
- compound: false,
4408
- descend: false,
4409
- simple: false,
4410
- target: TARGET_LINEAL
4411
- };
4412
- if (filterSelector(selector, filterOpt)) {
4424
+ const cacheKey = `closest_${selector}`;
4425
+ let filterMatches = false;
4426
+ if (this.#cache.has(cacheKey)) {
4427
+ filterMatches = this.#cache.get(cacheKey);
4428
+ } else {
4429
+ const filterOpt = {
4430
+ complex: REG_COMPLEX.test(selector),
4431
+ compound: false,
4432
+ descend: false,
4433
+ simple: false,
4434
+ target: TARGET_LINEAL
4435
+ };
4436
+ filterMatches = filterSelector(selector, filterOpt);
4437
+ this.#cache.set(cacheKey, filterMatches);
4438
+ }
4439
+ if (filterMatches) {
4413
4440
  try {
4414
4441
  const n = this.#idlUtils ? this.#idlUtils.wrapperForImpl(node) : node;
4415
4442
  const res2 = this.#nwsapi.closest(selector, n);
@@ -4491,14 +4518,22 @@ var DOMSelector = class {
4491
4518
  document = node.ownerDocument;
4492
4519
  }
4493
4520
  if (document === this.#document && document.contentType === "text/html" && document.documentElement) {
4494
- const filterOpt = {
4495
- complex: false,
4496
- compound: false,
4497
- descend: REG_DESCEND.test(selector) && !REG_SIBLING.test(selector),
4498
- simple: REG_SIMPLE.test(selector),
4499
- target: TARGET_ALL
4500
- };
4501
- if (filterSelector(selector, filterOpt)) {
4521
+ const cacheKey = `querySelectorAll_${selector}`;
4522
+ let filterMatches = false;
4523
+ if (this.#cache.has(cacheKey)) {
4524
+ filterMatches = this.#cache.get(cacheKey);
4525
+ } else {
4526
+ const filterOpt = {
4527
+ complex: false,
4528
+ compound: false,
4529
+ descend: REG_DESCEND.test(selector) && !REG_SIBLING.test(selector),
4530
+ simple: REG_SIMPLE.test(selector),
4531
+ target: TARGET_ALL
4532
+ };
4533
+ filterMatches = filterSelector(selector, filterOpt);
4534
+ this.#cache.set(cacheKey, filterMatches);
4535
+ }
4536
+ if (filterMatches) {
4502
4537
  try {
4503
4538
  const n = this.#idlUtils ? this.#idlUtils.wrapperForImpl(node) : node;
4504
4539
  const res2 = this.#nwsapi.select(selector, n);