@asamuzakjp/dom-selector 0.15.8 → 0.15.10

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 +55 -8
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.8"
45
+ "version": "0.15.10"
46
46
  }
package/src/js/matcher.js CHANGED
@@ -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;
@@ -1864,14 +1865,36 @@ class Matcher {
1864
1865
  querySelector() {
1865
1866
  let res;
1866
1867
  try {
1867
- const arr = this._find(this.#ast, this.#node);
1868
+ let node;
1869
+ if (isAttached(this.#node)) {
1870
+ node = this.#document;
1871
+ } else {
1872
+ node = this.#node;
1873
+ while (node) {
1874
+ if (!node.parentNode) {
1875
+ break;
1876
+ }
1877
+ node = node.parentNode;
1878
+ }
1879
+ }
1880
+ const arr = this._find(this.#ast, node);
1868
1881
  if (arr.length) {
1869
- const i = arr.findIndex(node => node === this.#node);
1882
+ const i = arr.findIndex(n => n === this.#node);
1870
1883
  if (i >= 0) {
1871
1884
  arr.splice(i, 1);
1872
1885
  }
1886
+ const nodes = new Set(arr);
1887
+ const iterator =
1888
+ this.#document.createNodeIterator(this.#node, FILTER_SHOW_ELEMENT);
1889
+ let nextNode = iterator.nextNode();
1890
+ while (nextNode) {
1891
+ if (nodes.has(nextNode)) {
1892
+ res = nextNode;
1893
+ break;
1894
+ }
1895
+ nextNode = iterator.nextNode();
1896
+ }
1873
1897
  }
1874
- [res] = arr;
1875
1898
  } catch (e) {
1876
1899
  if (e instanceof DOMException && e.name === 'NotSupportedError') {
1877
1900
  if (this.#warn) {
@@ -1892,15 +1915,39 @@ class Matcher {
1892
1915
  querySelectorAll() {
1893
1916
  const res = [];
1894
1917
  try {
1895
- const arr = this._find(this.#ast, this.#node);
1918
+ let node;
1919
+ if (isAttached(this.#node)) {
1920
+ node = this.#document;
1921
+ } else {
1922
+ node = this.#node;
1923
+ while (node) {
1924
+ if (!node.parentNode) {
1925
+ break;
1926
+ }
1927
+ node = node.parentNode;
1928
+ }
1929
+ }
1930
+ const arr = this._find(this.#ast, node);
1896
1931
  if (arr.length) {
1897
- const i = arr.findIndex(node => node === this.#node);
1932
+ const i = arr.findIndex(n => n === this.#node);
1898
1933
  if (i >= 0) {
1899
1934
  arr.splice(i, 1);
1900
1935
  }
1936
+ const nodes = new Set(arr);
1937
+ const iterator =
1938
+ this.#document.createNodeIterator(this.#node, FILTER_SHOW_ELEMENT);
1939
+ let nextNode = iterator.nextNode();
1940
+ while (nextNode) {
1941
+ if (nodes.has(nextNode)) {
1942
+ res.push(nextNode);
1943
+ nodes.delete(nextNode);
1944
+ }
1945
+ if (!nodes.size) {
1946
+ break;
1947
+ }
1948
+ nextNode = iterator.nextNode();
1949
+ }
1901
1950
  }
1902
- const a = new Set(arr);
1903
- res.push(...a);
1904
1951
  } catch (e) {
1905
1952
  if (e instanceof DOMException && e.name === 'NotSupportedError') {
1906
1953
  if (this.#warn) {