@asamuzakjp/dom-selector 0.16.0 → 0.16.2

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 +46 -40
package/package.json CHANGED
@@ -44,5 +44,5 @@
44
44
  "test": "c8 --reporter=text mocha --exit test/**/*.test.js",
45
45
  "tsc": "npx tsc"
46
46
  },
47
- "version": "0.16.0"
47
+ "version": "0.16.2"
48
48
  }
package/src/js/matcher.js CHANGED
@@ -1357,10 +1357,8 @@ const matchPseudoClassSelector = (
1357
1357
  const nodeName = n.localName;
1358
1358
  let m;
1359
1359
  if (nodeName === 'button') {
1360
- m = !(
1361
- n.hasAttribute('type') &&
1362
- /^(?:button|reset)$/.test(n.getAttribute('type'))
1363
- );
1360
+ m = !(n.hasAttribute('type') &&
1361
+ /^(?:button|reset)$/.test(n.getAttribute('type')));
1364
1362
  } else if (nodeName === 'input') {
1365
1363
  m = n.hasAttribute('type') &&
1366
1364
  /^(?:image|submit)$/.test(n.getAttribute('type'));
@@ -1440,11 +1438,10 @@ const matchPseudoClassSelector = (
1440
1438
  !(node.disabled || node.hasAttribute('disabled')) &&
1441
1439
  node.hasAttribute('type') &&
1442
1440
  INPUT_RANGE.test(node.getAttribute('type')) &&
1443
- !(node.validity.rangeUnderflow || node.validity.rangeOverflow)) {
1444
- if (node.hasAttribute('min') || node.hasAttribute('max') ||
1445
- node.getAttribute('type') === 'range') {
1446
- matched.add(node);
1447
- }
1441
+ !(node.validity.rangeUnderflow || node.validity.rangeOverflow) &&
1442
+ (node.hasAttribute('min') || node.hasAttribute('max') ||
1443
+ node.getAttribute('type') === 'range')) {
1444
+ matched.add(node);
1448
1445
  }
1449
1446
  break;
1450
1447
  }
@@ -1742,25 +1739,21 @@ class Matcher {
1742
1739
  * @returns {object} - root object
1743
1740
  */
1744
1741
  _getRoot(node) {
1745
- let detached;
1746
1742
  let document;
1747
1743
  let root;
1748
1744
  switch (node.nodeType) {
1749
1745
  case DOCUMENT_NODE: {
1750
- detached = false;
1751
1746
  document = node;
1752
1747
  root = node;
1753
1748
  break;
1754
1749
  }
1755
1750
  case DOCUMENT_FRAGMENT_NODE: {
1756
- detached = true;
1757
1751
  document = node.ownerDocument;
1758
1752
  root = node;
1759
1753
  break;
1760
1754
  }
1761
1755
  default: {
1762
1756
  if (isDescendant(node)) {
1763
- detached = false;
1764
1757
  document = node.ownerDocument;
1765
1758
  root = node.ownerDocument;
1766
1759
  } else {
@@ -1772,14 +1765,12 @@ class Matcher {
1772
1765
  break;
1773
1766
  }
1774
1767
  }
1775
- detached = true;
1776
1768
  document = parent.ownerDocument;
1777
1769
  root = parent;
1778
1770
  }
1779
1771
  }
1780
1772
  }
1781
1773
  return {
1782
- detached,
1783
1774
  document,
1784
1775
  root
1785
1776
  };
@@ -1902,7 +1893,7 @@ class Matcher {
1902
1893
  _findNodes(twig) {
1903
1894
  const { leaves } = twig;
1904
1895
  const l = leaves.length;
1905
- const { detached, root } = this.#root;
1896
+ const { root } = this.#root;
1906
1897
  let nodes = new Set();
1907
1898
  let pending = false;
1908
1899
  for (let i = 0; i < l; i++) {
@@ -1911,22 +1902,23 @@ class Matcher {
1911
1902
  const leafName = unescapeSelector(leaf.name);
1912
1903
  switch (leafType) {
1913
1904
  case ID_SELECTOR: {
1914
- if (nodes.size) {
1915
- const items = new Set([...nodes]);
1916
- nodes.clear();
1917
- for (const node of items) {
1918
- const bool = this._matchSelector(leaf, node).has(node);
1919
- if (bool) {
1920
- nodes.add(node);
1921
- break;
1922
- }
1923
- }
1924
- } else if (detached && root.nodeType === ELEMENT_NODE) {
1905
+ if (root.nodeType === ELEMENT_NODE) {
1925
1906
  pending = true;
1926
1907
  } else {
1927
1908
  const matched = root.getElementById(leafName);
1928
1909
  if (matched) {
1929
- nodes.add(matched);
1910
+ if (nodes.size) {
1911
+ if (nodes.has(matched)) {
1912
+ nodes.clear();
1913
+ nodes.add(matched);
1914
+ } else {
1915
+ nodes.clear();
1916
+ }
1917
+ } else {
1918
+ nodes.add(matched);
1919
+ }
1920
+ } else {
1921
+ nodes.clear();
1930
1922
  }
1931
1923
  }
1932
1924
  break;
@@ -1945,7 +1937,7 @@ class Matcher {
1945
1937
  });
1946
1938
  } else {
1947
1939
  const a = [...root.getElementsByClassName(leafName)];
1948
- if (detached && root.nodeType === ELEMENT_NODE &&
1940
+ if (root.nodeType === ELEMENT_NODE &&
1949
1941
  this._matchSelector(leaf, root).has(root)) {
1950
1942
  a.unshift(root);
1951
1943
  }
@@ -1954,9 +1946,9 @@ class Matcher {
1954
1946
  const matched = new Set(arr);
1955
1947
  if (matched.size) {
1956
1948
  if (nodes.size) {
1957
- nodes.forEach(n => {
1958
- if (!matched.has(n)) {
1959
- nodes.delete(n);
1949
+ nodes.forEach(node => {
1950
+ if (!matched.has(node)) {
1951
+ nodes.delete(node);
1960
1952
  }
1961
1953
  });
1962
1954
  } else {
@@ -1990,10 +1982,10 @@ class Matcher {
1990
1982
  const allNodesLen = allNodes.length;
1991
1983
  const matched = new Set();
1992
1984
  for (let j = 0; j < allNodesLen; j++) {
1993
- const n = allNodes[j];
1994
- const bool = this._matchSelector(leaf, n).has(n);
1985
+ const node = allNodes[j];
1986
+ const bool = this._matchSelector(leaf, node).has(node);
1995
1987
  if (bool) {
1996
- matched.add(n);
1988
+ matched.add(node);
1997
1989
  }
1998
1990
  }
1999
1991
  if (matched.size) {
@@ -2005,10 +1997,10 @@ class Matcher {
2005
1997
  }
2006
1998
  default: {
2007
1999
  if (nodes.size) {
2008
- nodes.forEach(n => {
2009
- const bool = this._matchSelector(leaf, n).has(n);
2000
+ nodes.forEach(node => {
2001
+ const bool = this._matchSelector(leaf, node).has(node);
2010
2002
  if (!bool) {
2011
- nodes.delete(n);
2003
+ nodes.delete(node);
2012
2004
  }
2013
2005
  });
2014
2006
  } else {
@@ -2164,7 +2156,6 @@ class Matcher {
2164
2156
  * @returns {object} - collection of sorted nodes
2165
2157
  */
2166
2158
  _sortNodes(nodes, range) {
2167
- const f = factorial(nodes.size);
2168
2159
  const { document, root } = this.#root;
2169
2160
  let n = 0;
2170
2161
  if (root.nodeType === DOCUMENT_FRAGMENT_NODE) {
@@ -2177,6 +2168,7 @@ class Matcher {
2177
2168
  } else {
2178
2169
  n += document.getElementsByTagName('*').length;
2179
2170
  }
2171
+ const f = factorial(nodes.size);
2180
2172
  const sorted = new Set();
2181
2173
  if (f < n) {
2182
2174
  const items = [...nodes];
@@ -2193,7 +2185,7 @@ class Matcher {
2193
2185
  const nextNode = items[i];
2194
2186
  const posBit = node.compareDocumentPosition(nextNode);
2195
2187
  if (posBit & DOCUMENT_POSITION_PRECEDING ||
2196
- posBit & DOCUMENT_POSITION_CONTAINS) {
2188
+ posBit & DOCUMENT_POSITION_CONTAINS) {
2197
2189
  node = nextNode;
2198
2190
  pos = i;
2199
2191
  }
@@ -2278,6 +2270,13 @@ class Matcher {
2278
2270
  try {
2279
2271
  const nodes = this._find();
2280
2272
  nodes.delete(this.#node);
2273
+ if (this.#node.nodeType === ELEMENT_NODE) {
2274
+ nodes.forEach(node => {
2275
+ if (!isDescendant(node, this.#node)) {
2276
+ nodes.delete(node);
2277
+ }
2278
+ });
2279
+ }
2281
2280
  if (nodes.size > 1) {
2282
2281
  [res] = [...this._sortNodes(nodes)];
2283
2282
  } else if (nodes.size) {
@@ -2299,6 +2298,13 @@ class Matcher {
2299
2298
  try {
2300
2299
  const nodes = this._find();
2301
2300
  nodes.delete(this.#node);
2301
+ if (this.#node.nodeType === ELEMENT_NODE) {
2302
+ nodes.forEach(node => {
2303
+ if (!isDescendant(node, this.#node)) {
2304
+ nodes.delete(node);
2305
+ }
2306
+ });
2307
+ }
2302
2308
  if (nodes.size > 1) {
2303
2309
  const sorted = this._sortNodes(nodes, 'all');
2304
2310
  res.push(...sorted);