@asamuzakjp/dom-selector 0.16.2 → 0.16.4

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 +2 -1
  2. package/src/js/matcher.js +31 -23
package/package.json CHANGED
@@ -34,6 +34,7 @@
34
34
  "eslint-plugin-unicorn": "^47.0.0",
35
35
  "jsdom": "^22.1.0",
36
36
  "mocha": "^10.2.0",
37
+ "nwsapi": "^2.2.5",
37
38
  "sinon": "^15.1.0",
38
39
  "typescript": "^5.1.3"
39
40
  },
@@ -44,5 +45,5 @@
44
45
  "test": "c8 --reporter=text mocha --exit test/**/*.test.js",
45
46
  "tsc": "npx tsc"
46
47
  },
47
- "version": "0.16.2"
48
+ "version": "0.16.4"
48
49
  }
package/src/js/matcher.js CHANGED
@@ -116,20 +116,20 @@ const isDescendant = (node = {}, root = {}) => {
116
116
  };
117
117
 
118
118
  /**
119
- * factorial
119
+ * sum of series
120
120
  * @param {number} n - number
121
- * @returns {number} - n!
121
+ * @returns {?number} - sum
122
122
  */
123
- const factorial = n => {
124
- let f;
123
+ const sumSeries = n => {
124
+ let s;
125
125
  if (Number.isInteger(n) && n >= 0) {
126
- f = 1;
127
- for (let i = n; i > 0; i--) {
128
- f *= i;
126
+ s = 0;
127
+ for (let i = 1; i <= n; i++) {
128
+ s += i;
129
129
  }
130
130
  }
131
- return f ?? null;
132
- };
131
+ return s ?? null;
132
+ }
133
133
 
134
134
  /**
135
135
  * unescape selector
@@ -1682,11 +1682,19 @@ class Matcher {
1682
1682
  #warn;
1683
1683
 
1684
1684
  /**
1685
- * #list[{ branch[], skip }, { branch[], skip }]
1686
- * branch[twig{}, twig{}]
1687
- * twig{combo{}, leaves[]}
1688
- * leaves[leaf{}, leaf{}, leaf{}]
1689
- * #matrix[
1685
+ * NOTE: #list[i] corresponds to #matrix[i]
1686
+ * #list: [
1687
+ * { branch: branch[], skip: boolean },
1688
+ * { branch: branch[], skip: boolean }
1689
+ * ]
1690
+ * branch[]: [twig{}, twig{}]
1691
+ * twig{}: {
1692
+ * combo: leaf{}|null,
1693
+ * leaves: leaves[]
1694
+ * }
1695
+ * leaves[]: [leaf{}, leaf{}, leaf{}]
1696
+ * leaf{}: AST leaf
1697
+ * #matrix: [
1690
1698
  * [
1691
1699
  * Set([node, node]),
1692
1700
  * Set([node, node, node, node]),
@@ -1697,7 +1705,7 @@ class Matcher {
1697
1705
  * Set([node, node])
1698
1706
  * ]
1699
1707
  * ]
1700
- * matrix[i] maps to list[i]
1708
+ * node: Element node
1701
1709
  */
1702
1710
 
1703
1711
  /**
@@ -2126,7 +2134,7 @@ class Matcher {
2126
2134
  } else if (matched.size) {
2127
2135
  const { combo: nextCombo } = branch[j];
2128
2136
  prevCombo = nextCombo;
2129
- prevNodes = nextNodes;
2137
+ prevNodes = matched;
2130
2138
  } else {
2131
2139
  break;
2132
2140
  }
@@ -2168,13 +2176,13 @@ class Matcher {
2168
2176
  } else {
2169
2177
  n += document.getElementsByTagName('*').length;
2170
2178
  }
2171
- const f = factorial(nodes.size);
2179
+ const s = sumSeries(nodes.size);
2172
2180
  const sorted = new Set();
2173
- if (f < n) {
2181
+ if (s < n) {
2174
2182
  const items = [...nodes];
2175
2183
  let [node] = items;
2176
2184
  let l = items.length;
2177
- let pos = 0;
2185
+ let index = 0;
2178
2186
  while (node) {
2179
2187
  if (node) {
2180
2188
  if (l === 1) {
@@ -2187,15 +2195,15 @@ class Matcher {
2187
2195
  if (posBit & DOCUMENT_POSITION_PRECEDING ||
2188
2196
  posBit & DOCUMENT_POSITION_CONTAINS) {
2189
2197
  node = nextNode;
2190
- pos = i;
2198
+ index = i;
2191
2199
  }
2192
2200
  }
2193
2201
  sorted.add(node);
2194
2202
  if (range === 'all') {
2195
- items.splice(pos, 1);
2203
+ items.splice(index, 1);
2196
2204
  [node] = items;
2197
2205
  l = items.length;
2198
- pos = 0;
2206
+ index = 0;
2199
2207
  } else {
2200
2208
  break;
2201
2209
  }
@@ -2323,7 +2331,6 @@ module.exports = {
2323
2331
  collectNthChild,
2324
2332
  collectNthOfType,
2325
2333
  createSelectorForNode,
2326
- factorial,
2327
2334
  isContentEditable,
2328
2335
  isDescendant,
2329
2336
  isNamespaceDeclared,
@@ -2339,5 +2346,6 @@ module.exports = {
2339
2346
  matchPseudoElementSelector,
2340
2347
  matchTypeSelector,
2341
2348
  parseASTName,
2349
+ sumSeries,
2342
2350
  unescapeSelector
2343
2351
  };