@asamuzakjp/dom-selector 0.15.10 → 0.15.13

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.
Files changed (2) hide show
  1. package/package.json +4 -4
  2. package/src/js/matcher.js +43 -18
package/package.json CHANGED
@@ -24,11 +24,11 @@
24
24
  },
25
25
  "devDependencies": {
26
26
  "@types/css-tree": "^2.3.1",
27
- "c8": "^7.13.0",
27
+ "c8": "^7.14.0",
28
28
  "chai": "^4.3.7",
29
29
  "eslint": "^8.41.0",
30
- "eslint-config-standard": "^17.0.0",
31
- "eslint-plugin-jsdoc": "^44.2.7",
30
+ "eslint-config-standard": "^17.1.0",
31
+ "eslint-plugin-jsdoc": "^46.0.0",
32
32
  "eslint-plugin-regexp": "^1.15.0",
33
33
  "eslint-plugin-unicorn": "^47.0.0",
34
34
  "jsdom": "^22.1.0",
@@ -42,5 +42,5 @@
42
42
  "test": "c8 --reporter=text mocha --exit test/**/*.test.js",
43
43
  "tsc": "npx tsc"
44
44
  },
45
- "version": "0.15.10"
45
+ "version": "0.15.13"
46
46
  }
package/src/js/matcher.js CHANGED
@@ -100,7 +100,7 @@ const isAttached = (node = {}) => {
100
100
  const root = ownerDocument.documentElement;
101
101
  if (node === root) {
102
102
  res = true;
103
- } else {
103
+ } else if (root) {
104
104
  const posBit =
105
105
  node.compareDocumentPosition(root) & DOCUMENT_POSITION_CONTAINS;
106
106
  res = posBit;
@@ -1735,43 +1735,68 @@ class Matcher {
1735
1735
  * @returns {Array.<object|undefined>} - collection of matched nodes
1736
1736
  */
1737
1737
  _getMatchedNodes(branch = [], node = {}) {
1738
- const matched = [];
1739
1738
  const twig = groupASTLeaves(branch);
1740
1739
  const l = twig.length;
1740
+ const matched = [];
1741
1741
  if (l) {
1742
+ const lastIndex = l - 1;
1742
1743
  const iterator =
1743
1744
  this.#document.createNodeIterator(node, FILTER_SHOW_ELEMENT);
1744
1745
  let nextNode = iterator.nextNode();
1745
1746
  while (nextNode) {
1746
1747
  let i = 0;
1747
- while (i < l) {
1748
- const { leaves } = twig[i];
1748
+ if (nextNode.children.length === 0) {
1749
+ const { leaves } = twig[lastIndex];
1749
1750
  const bool = leaves.every(leaf => {
1750
1751
  const arr = this._match(leaf, nextNode);
1751
1752
  return arr.includes(nextNode);
1752
1753
  });
1753
1754
  if (bool) {
1754
- twig[i].nodes.add(nextNode);
1755
+ twig[lastIndex].nodes.add(nextNode);
1756
+ }
1757
+ } else {
1758
+ while (i < l) {
1759
+ const { leaves } = twig[i];
1760
+ const bool = leaves.every(leaf => {
1761
+ const arr = this._match(leaf, nextNode);
1762
+ return arr.includes(nextNode);
1763
+ });
1764
+ if (bool) {
1765
+ twig[i].nodes.add(nextNode);
1766
+ }
1767
+ i++;
1755
1768
  }
1756
- i++;
1757
1769
  }
1758
1770
  nextNode = iterator.nextNode();
1759
1771
  }
1760
- if (l === 1) {
1772
+ if (lastIndex === 0) {
1761
1773
  const [{ nodes }] = twig;
1762
1774
  matched.push(...nodes);
1763
- } else if (l > 1) {
1764
- const { nodes } = twig.reduce((prevItem, nextItem) => {
1775
+ } else if (lastIndex) {
1776
+ const prevItem = twig[0];
1777
+ let j = 1;
1778
+ while (j < l) {
1765
1779
  const { combo: prevCombo, nodes: prevNodes } = prevItem;
1766
- const { combo: nextCombo, nodes: nextNodes } = nextItem;
1767
- const matchedNodes =
1768
- matchCombinator(prevCombo, [...prevNodes], [...nextNodes]);
1769
- return {
1770
- combo: nextCombo,
1771
- nodes: new Set(matchedNodes)
1772
- };
1773
- });
1774
- matched.push(...nodes);
1780
+ const { combo: nextCombo, nodes: nextNodes } = twig[j];
1781
+ if (prevCombo && prevNodes.size && nextNodes.size) {
1782
+ const nodes =
1783
+ matchCombinator(prevCombo, [...prevNodes], [...nextNodes]);
1784
+ if (nodes.length) {
1785
+ if (j === lastIndex) {
1786
+ matched.push(...new Set(nodes));
1787
+ break;
1788
+ } else {
1789
+ prevItem.combo = nextCombo;
1790
+ prevItem.nodes = new Set(nodes);
1791
+ }
1792
+ } else {
1793
+ break;
1794
+ }
1795
+ } else {
1796
+ break;
1797
+ }
1798
+ j++;
1799
+ }
1775
1800
  }
1776
1801
  }
1777
1802
  return matched;