@asamuzakjp/dom-selector 0.6.1 → 0.7.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 +18 -14
package/package.json CHANGED
@@ -40,5 +40,5 @@
40
40
  "test": "c8 --reporter=text mocha --exit test/**/*.test.js",
41
41
  "tsc": "npx tsc"
42
42
  },
43
- "version": "0.6.1"
43
+ "version": "0.7.0"
44
44
  }
package/src/js/matcher.js CHANGED
@@ -433,18 +433,20 @@ const matchAttributeSelector = (ast = {}, node = {}) => {
433
433
  } else {
434
434
  attrValue = astAttrStringValue;
435
435
  }
436
+ } else if (astAttrStringValue === '') {
437
+ attrValue = astAttrStringValue;
436
438
  }
437
439
  switch (astMatcher) {
438
440
  case null:
439
441
  res = node;
440
442
  break;
441
443
  case '=':
442
- if (attrValue && attrValues.includes(attrValue)) {
444
+ if (typeof attrValue === 'string' && attrValues.includes(attrValue)) {
443
445
  res = node;
444
446
  }
445
447
  break;
446
448
  case '~=':
447
- if (attrValue) {
449
+ if (typeof attrValue === 'string') {
448
450
  for (const item of attrValues) {
449
451
  const arr = item.split(/\s+/);
450
452
  if (arr.includes(attrValue)) {
@@ -455,7 +457,7 @@ const matchAttributeSelector = (ast = {}, node = {}) => {
455
457
  }
456
458
  break;
457
459
  case '|=':
458
- if (attrValue) {
460
+ if (typeof attrValue === 'string') {
459
461
  const item = attrValues.find(v =>
460
462
  (v === attrValue || v.startsWith(`${attrValue}-`))
461
463
  );
@@ -465,7 +467,7 @@ const matchAttributeSelector = (ast = {}, node = {}) => {
465
467
  }
466
468
  break;
467
469
  case '^=':
468
- if (attrValue) {
470
+ if (typeof attrValue === 'string') {
469
471
  const item = attrValues.find(v => v.startsWith(`${attrValue}`));
470
472
  if (item) {
471
473
  res = node;
@@ -473,7 +475,7 @@ const matchAttributeSelector = (ast = {}, node = {}) => {
473
475
  }
474
476
  break;
475
477
  case '$=':
476
- if (attrValue) {
478
+ if (typeof attrValue === 'string') {
477
479
  const item = attrValues.find(v => v.endsWith(`${attrValue}`));
478
480
  if (item) {
479
481
  res = node;
@@ -481,7 +483,7 @@ const matchAttributeSelector = (ast = {}, node = {}) => {
481
483
  }
482
484
  break;
483
485
  case '*=':
484
- if (attrValue) {
486
+ if (typeof attrValue === 'string') {
485
487
  const item = attrValues.find(v => v.includes(`${attrValue}`));
486
488
  if (item) {
487
489
  res = node;
@@ -766,6 +768,11 @@ const matchPseudoClassSelector = (
766
768
  matched.push(node);
767
769
  }
768
770
  break;
771
+ case 'empty':
772
+ if (!node.hasChildNodes()) {
773
+ matched.push(node);
774
+ }
775
+ break;
769
776
  case 'first-child':
770
777
  if (node === node.parentNode.firstElementChild) {
771
778
  matched.push(node);
@@ -823,7 +830,6 @@ const matchPseudoClassSelector = (
823
830
  case 'blank':
824
831
  case 'buffering':
825
832
  case 'current':
826
- case 'empty':
827
833
  case 'focus-visible':
828
834
  case 'fullscreen':
829
835
  case 'future':
@@ -1248,7 +1254,7 @@ class Matcher {
1248
1254
  if (nextNode) {
1249
1255
  matched.push(nextNode);
1250
1256
  }
1251
- nextNode = nextNode.nextElementSibling;
1257
+ nextNode = nextNode?.nextElementSibling;
1252
1258
  }
1253
1259
  }
1254
1260
  }
@@ -1337,15 +1343,13 @@ class Matcher {
1337
1343
  */
1338
1344
  querySelector() {
1339
1345
  const arr = this._match(this.#ast, this.#node);
1340
- let res;
1341
1346
  if (arr.length) {
1342
- const [i, j] = arr;
1343
- if (i !== this.#node && i.nodeType === ELEMENT_NODE) {
1344
- res = i;
1345
- } else if (j) {
1346
- res = j;
1347
+ const i = arr.findIndex(node => node === this.#node);
1348
+ if (i >= 0) {
1349
+ arr.splice(i, 1);
1347
1350
  }
1348
1351
  }
1352
+ const [res] = arr;
1349
1353
  return res || null;
1350
1354
  }
1351
1355