@asamuzakjp/dom-selector 0.15.10 → 0.15.12

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 +1 -1
  2. package/src/js/matcher.js +24 -11
package/package.json CHANGED
@@ -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.12"
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;
@@ -1761,17 +1761,30 @@ class Matcher {
1761
1761
  const [{ nodes }] = twig;
1762
1762
  matched.push(...nodes);
1763
1763
  } else if (l > 1) {
1764
- const { nodes } = twig.reduce((prevItem, nextItem) => {
1764
+ const prevItem = twig[0];
1765
+ let j = 1;
1766
+ while (j < l) {
1765
1767
  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);
1768
+ const { combo: nextCombo, nodes: nextNodes } = twig[j];
1769
+ if (prevCombo && prevNodes.size && nextNodes.size) {
1770
+ const nodes =
1771
+ matchCombinator(prevCombo, [...prevNodes], [...nextNodes]);
1772
+ if (nodes.length) {
1773
+ if (j === l - 1) {
1774
+ matched.push(...new Set(nodes));
1775
+ break;
1776
+ } else {
1777
+ prevItem.combo = nextCombo;
1778
+ prevItem.nodes = new Set(nodes);
1779
+ }
1780
+ } else {
1781
+ break;
1782
+ }
1783
+ } else {
1784
+ break;
1785
+ }
1786
+ j++;
1787
+ }
1775
1788
  }
1776
1789
  }
1777
1790
  return matched;