@asamuzakjp/dom-selector 0.20.1 → 0.20.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 +26 -18
package/package.json CHANGED
@@ -45,5 +45,5 @@
45
45
  "test": "c8 --reporter=text mocha --exit test/**/*.test.js",
46
46
  "tsc": "npx tsc"
47
47
  },
48
- "version": "0.20.1"
48
+ "version": "0.20.2"
49
49
  }
package/src/js/matcher.js CHANGED
@@ -2031,7 +2031,7 @@ export class Matcher {
2031
2031
  const iterator = document.createNodeIterator(root, SHOW_ELEMENT);
2032
2032
  let nextNode = iterator.nextNode();
2033
2033
  while (nextNode) {
2034
- let bool;
2034
+ let bool = false;
2035
2035
  if (targetType === TARGET_ALL || targetType === TARGET_FIRST) {
2036
2036
  if (this.#node.nodeType === ELEMENT_NODE) {
2037
2037
  bool = isSameOrDescendant(nextNode, this.#node);
@@ -2096,24 +2096,32 @@ export class Matcher {
2096
2096
  if (skip) {
2097
2097
  continue;
2098
2098
  } else if (branchLen) {
2099
- const startNodes = this.#nodes[i];
2099
+ const matched = this.#nodes[i];
2100
2100
  const lastIndex = branchLen - 1;
2101
2101
  if (lastIndex === 0) {
2102
2102
  if ((targetType === TARGET_ALL || targetType === TARGET_FIRST) &&
2103
2103
  this.#node.nodeType === ELEMENT_NODE) {
2104
- for (const node of startNodes) {
2105
- if (isSameOrDescendant(node, this.#node)) {
2106
- nodes.add(node);
2104
+ for (const node of matched) {
2105
+ if (node !== this.#node) {
2106
+ if (isSameOrDescendant(node, this.#node)) {
2107
+ nodes.add(node);
2108
+ if (targetType === TARGET_FIRST) {
2109
+ break;
2110
+ }
2111
+ }
2107
2112
  }
2108
2113
  }
2114
+ } else if (targetType === TARGET_FIRST) {
2115
+ const [node] = [...matched];
2116
+ nodes.add(node);
2109
2117
  } else {
2110
2118
  const n = [...nodes];
2111
- const s = [...startNodes];
2112
- nodes = new Set([...n, ...s]);
2119
+ const m = [...matched];
2120
+ nodes = new Set([...n, ...m]);
2113
2121
  }
2114
2122
  } else if (targetType === TARGET_ALL || targetType === TARGET_FIRST) {
2115
2123
  let { combo } = branch[0];
2116
- for (const node of startNodes) {
2124
+ for (const node of matched) {
2117
2125
  let nextNodes = new Set([node]);
2118
2126
  for (let j = 1; j < branchLen; j++) {
2119
2127
  const { combo: nextCombo, leaves } = branch[j];
@@ -2130,21 +2138,21 @@ export class Matcher {
2130
2138
  arr.push(...m);
2131
2139
  }
2132
2140
  }
2133
- const matched = new Set(arr);
2134
- if (matched.size) {
2141
+ const matchedNodes = new Set(arr);
2142
+ if (matchedNodes.size) {
2135
2143
  if (j === lastIndex) {
2136
2144
  if (targetType === TARGET_FIRST) {
2137
- const [matchedNode] = this._sortNodes(matched);
2138
- nodes.add(matchedNode);
2145
+ const [node] = [...matchedNodes];
2146
+ nodes.add(node);
2139
2147
  } else {
2140
2148
  const n = [...nodes];
2141
- const m = [...matched];
2149
+ const m = [...matchedNodes];
2142
2150
  nodes = new Set([...n, ...m]);
2143
2151
  }
2144
2152
  break;
2145
2153
  } else {
2146
2154
  combo = nextCombo;
2147
- nextNodes = matched;
2155
+ nextNodes = matchedNodes;
2148
2156
  }
2149
2157
  } else {
2150
2158
  break;
@@ -2152,7 +2160,7 @@ export class Matcher {
2152
2160
  }
2153
2161
  }
2154
2162
  } else {
2155
- for (const node of startNodes) {
2163
+ for (const node of matched) {
2156
2164
  let nextNodes = new Set([node]);
2157
2165
  let bool;
2158
2166
  for (let j = lastIndex - 1; j >= 0; j--) {
@@ -2166,14 +2174,14 @@ export class Matcher {
2166
2174
  arr.push(...m);
2167
2175
  }
2168
2176
  }
2169
- const matched = new Set(arr);
2170
- if (matched.size) {
2177
+ const matchedNodes = new Set(arr);
2178
+ if (matchedNodes.size) {
2171
2179
  bool = true;
2172
2180
  if (j === 0) {
2173
2181
  nodes.add(node);
2174
2182
  break;
2175
2183
  } else {
2176
- nextNodes = matched;
2184
+ nextNodes = matchedNodes;
2177
2185
  }
2178
2186
  } else {
2179
2187
  bool = false;