@asamuzakjp/dom-selector 0.15.9 → 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 +54 -16
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.9"
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;
@@ -1099,7 +1099,8 @@ const matchPseudoClassSelector = (
1099
1099
  break;
1100
1100
  }
1101
1101
  case 'target': {
1102
- if (docURL.hash && node.id && docURL.hash === `#${node.id}`) {
1102
+ if (isAttached(node) && docURL.hash &&
1103
+ node.id && docURL.hash === `#${node.id}`) {
1103
1104
  matched.push(node);
1104
1105
  }
1105
1106
  break;
@@ -1760,17 +1761,30 @@ class Matcher {
1760
1761
  const [{ nodes }] = twig;
1761
1762
  matched.push(...nodes);
1762
1763
  } else if (l > 1) {
1763
- const { nodes } = twig.reduce((prevItem, nextItem) => {
1764
+ const prevItem = twig[0];
1765
+ let j = 1;
1766
+ while (j < l) {
1764
1767
  const { combo: prevCombo, nodes: prevNodes } = prevItem;
1765
- const { combo: nextCombo, nodes: nextNodes } = nextItem;
1766
- const matchedNodes =
1767
- matchCombinator(prevCombo, [...prevNodes], [...nextNodes]);
1768
- return {
1769
- combo: nextCombo,
1770
- nodes: new Set(matchedNodes)
1771
- };
1772
- });
1773
- 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
+ }
1774
1788
  }
1775
1789
  }
1776
1790
  return matched;
@@ -1864,9 +1878,21 @@ class Matcher {
1864
1878
  querySelector() {
1865
1879
  let res;
1866
1880
  try {
1867
- const arr = this._find(this.#ast, this.#node);
1881
+ let node;
1882
+ if (isAttached(this.#node)) {
1883
+ node = this.#document;
1884
+ } else {
1885
+ node = this.#node;
1886
+ while (node) {
1887
+ if (!node.parentNode) {
1888
+ break;
1889
+ }
1890
+ node = node.parentNode;
1891
+ }
1892
+ }
1893
+ const arr = this._find(this.#ast, node);
1868
1894
  if (arr.length) {
1869
- const i = arr.findIndex(node => node === this.#node);
1895
+ const i = arr.findIndex(n => n === this.#node);
1870
1896
  if (i >= 0) {
1871
1897
  arr.splice(i, 1);
1872
1898
  }
@@ -1902,9 +1928,21 @@ class Matcher {
1902
1928
  querySelectorAll() {
1903
1929
  const res = [];
1904
1930
  try {
1905
- const arr = this._find(this.#ast, this.#node);
1931
+ let node;
1932
+ if (isAttached(this.#node)) {
1933
+ node = this.#document;
1934
+ } else {
1935
+ node = this.#node;
1936
+ while (node) {
1937
+ if (!node.parentNode) {
1938
+ break;
1939
+ }
1940
+ node = node.parentNode;
1941
+ }
1942
+ }
1943
+ const arr = this._find(this.#ast, node);
1906
1944
  if (arr.length) {
1907
- const i = arr.findIndex(node => node === this.#node);
1945
+ const i = arr.findIndex(n => n === this.#node);
1908
1946
  if (i >= 0) {
1909
1947
  arr.splice(i, 1);
1910
1948
  }