@asamuzakjp/dom-selector 0.2.2 → 0.3.0

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 +81 -61
package/package.json CHANGED
@@ -41,5 +41,5 @@
41
41
  "test": "c8 --reporter=text mocha --exit test/**/*.test.js",
42
42
  "tsc": "npx tsc"
43
43
  },
44
- "version": "0.2.2"
44
+ "version": "0.3.0"
45
45
  }
package/src/js/matcher.js CHANGED
@@ -1081,81 +1081,94 @@ class Matcher {
1081
1081
  }
1082
1082
  const iterator = this._createIterator(iteratorLeaf, node);
1083
1083
  let nextNode = iterator.nextNode();
1084
- while (nextNode) {
1085
- const [...items] = children;
1086
- if (items.length) {
1087
- if (items.length === 1) {
1088
- const item = items.shift();
1089
- const { name: itemName, type: itemType } = item;
1090
- if (itemType === PSEUDO_CLASS_SELECTOR &&
1091
- REG_PSEUDO_FUNC.test(itemName)) {
1092
- nextNode = this._matchLogicalPseudoFunc(item, nextNode);
1093
- if (nextNode) {
1094
- res.add(nextNode);
1095
- nextNode = null;
1096
- }
1097
- } else {
1098
- const arr = this._match(item, nextNode);
1099
- if (arr.length) {
1100
- for (const i of arr) {
1101
- res.add(i);
1102
- }
1103
- }
1104
- }
1105
- } else {
1106
- do {
1084
+ if (nextNode) {
1085
+ while (nextNode) {
1086
+ const [...items] = children;
1087
+ if (items.length) {
1088
+ if (items.length === 1) {
1107
1089
  const item = items.shift();
1108
1090
  const { name: itemName, type: itemType } = item;
1109
1091
  if (itemType === PSEUDO_CLASS_SELECTOR &&
1110
1092
  REG_PSEUDO_FUNC.test(itemName)) {
1111
1093
  nextNode = this._matchLogicalPseudoFunc(item, nextNode);
1112
- } else if (itemType === COMBINATOR) {
1113
- const leaves = [];
1114
- leaves.push(item);
1115
- while (items.length) {
1116
- const [nextItem] = items;
1117
- if (nextItem.type === COMBINATOR ||
1118
- (nextItem.type === PSEUDO_CLASS_SELECTOR &&
1119
- REG_PSEUDO_NTH.test(nextItem.name)) ||
1120
- (nextItem.type === PSEUDO_CLASS_SELECTOR &&
1121
- REG_PSEUDO_FUNC.test(nextItem.name))) {
1122
- break;
1123
- } else {
1124
- leaves.push(items.shift());
1094
+ if (nextNode) {
1095
+ res.add(nextNode);
1096
+ nextNode = null;
1097
+ }
1098
+ } else {
1099
+ const arr = this._match(item, nextNode);
1100
+ if (arr.length) {
1101
+ for (const i of arr) {
1102
+ res.add(i);
1125
1103
  }
1126
1104
  }
1127
- const arr = this._matchCombinator(leaves, nextNode);
1128
- if (!arr.length || arr.length === 1) {
1129
- [nextNode] = arr;
1130
- } else {
1131
- if (items.length) {
1132
- for (const i of arr) {
1133
- const a = this._matchSelector(items, i);
1134
- if (a.length) {
1135
- for (const j of a) {
1136
- res.add(j);
1137
- }
1138
- }
1105
+ }
1106
+ } else {
1107
+ do {
1108
+ const item = items.shift();
1109
+ const { name: itemName, type: itemType } = item;
1110
+ if (itemType === PSEUDO_CLASS_SELECTOR &&
1111
+ REG_PSEUDO_FUNC.test(itemName)) {
1112
+ nextNode = this._matchLogicalPseudoFunc(item, nextNode);
1113
+ } else if (itemType === COMBINATOR) {
1114
+ const leaves = [];
1115
+ leaves.push(item);
1116
+ while (items.length) {
1117
+ const [nextItem] = items;
1118
+ if (nextItem.type === COMBINATOR ||
1119
+ (nextItem.type === PSEUDO_CLASS_SELECTOR &&
1120
+ REG_PSEUDO_NTH.test(nextItem.name)) ||
1121
+ (nextItem.type === PSEUDO_CLASS_SELECTOR &&
1122
+ REG_PSEUDO_FUNC.test(nextItem.name))) {
1123
+ break;
1124
+ } else {
1125
+ leaves.push(items.shift());
1139
1126
  }
1127
+ }
1128
+ const arr = this._matchCombinator(leaves, nextNode);
1129
+ if (!arr.length || arr.length === 1) {
1130
+ [nextNode] = arr;
1140
1131
  } else {
1141
- for (const i of arr) {
1142
- res.add(i);
1132
+ if (items.length) {
1133
+ for (const i of arr) {
1134
+ const a = this._matchSelector(items, i);
1135
+ if (a.length) {
1136
+ for (const j of a) {
1137
+ res.add(j);
1138
+ }
1139
+ }
1140
+ }
1141
+ } else {
1142
+ for (const i of arr) {
1143
+ res.add(i);
1144
+ }
1143
1145
  }
1146
+ nextNode = null;
1144
1147
  }
1145
- nextNode = null;
1148
+ } else {
1149
+ [nextNode] = this._match(item, nextNode);
1146
1150
  }
1147
- } else {
1148
- [nextNode] = this._match(item, nextNode);
1151
+ } while (items.length && nextNode);
1152
+ if (nextNode) {
1153
+ res.add(nextNode);
1149
1154
  }
1150
- } while (items.length && nextNode);
1151
- if (nextNode) {
1152
- res.add(nextNode);
1153
1155
  }
1156
+ } else if (nextNode) {
1157
+ res.add(nextNode);
1154
1158
  }
1155
- } else if (nextNode) {
1156
- res.add(nextNode);
1159
+ nextNode = iterator.nextNode();
1160
+ }
1161
+ } else if (firstChild.type === PSEUDO_CLASS_SELECTOR &&
1162
+ REG_PSEUDO_FUNC.test(firstChild.name) &&
1163
+ node.nodeType === ELEMENT_NODE) {
1164
+ nextNode = node;
1165
+ while (nextNode) {
1166
+ nextNode = this._matchLogicalPseudoFunc(firstChild, nextNode);
1167
+ if (nextNode) {
1168
+ res.add(nextNode);
1169
+ }
1170
+ nextNode = nextNode.nextElementSibling;
1157
1171
  }
1158
- nextNode = iterator.nextNode();
1159
1172
  }
1160
1173
  }
1161
1174
  return [...res];
@@ -1263,7 +1276,12 @@ class Matcher {
1263
1276
  const arr = this._match(this.#ast, this.#node);
1264
1277
  let res;
1265
1278
  if (arr.length) {
1266
- [res] = arr;
1279
+ const [i, j] = arr;
1280
+ if (i !== this.#node) {
1281
+ res = i;
1282
+ } else if (j) {
1283
+ res = j;
1284
+ }
1267
1285
  }
1268
1286
  return res || null;
1269
1287
  }
@@ -1278,7 +1296,9 @@ class Matcher {
1278
1296
  const res = new Set();
1279
1297
  if (arr.length) {
1280
1298
  for (const i of arr) {
1281
- res.add(i);
1299
+ if (i !== this.#node) {
1300
+ res.add(i);
1301
+ }
1282
1302
  }
1283
1303
  }
1284
1304
  return [...res];