@asamuzakjp/dom-selector 0.15.6 → 0.15.8

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.
package/package.json CHANGED
@@ -42,5 +42,5 @@
42
42
  "test": "c8 --reporter=text mocha --exit test/**/*.test.js",
43
43
  "tsc": "npx tsc"
44
44
  },
45
- "version": "0.15.6"
45
+ "version": "0.15.8"
46
46
  }
package/src/js/matcher.js CHANGED
@@ -528,7 +528,7 @@ const matchTypeSelector = (ast = {}, node = {}) => {
528
528
  throw new DOMException(`invalid selector ${astName}`, 'SyntaxError');
529
529
  }
530
530
  } else {
531
- astPrefix = '';
531
+ astPrefix = '*';
532
532
  astNodeName = astName;
533
533
  }
534
534
  if (ownerDocument?.contentType === 'text/html') {
@@ -542,12 +542,19 @@ const matchTypeSelector = (ast = {}, node = {}) => {
542
542
  nodePrefix = prefix || '';
543
543
  nodeName = localName;
544
544
  }
545
- if (astName === '*' || astName === '*|*' || astName === nodeName ||
546
- (astName === '|*' && !nodePrefix) ||
547
- (astPrefix === '*' && astNodeName === nodeName) ||
548
- ((astPrefix === nodePrefix || (astPrefix === '' && !nodePrefix)) &&
549
- (astNodeName === '*' || astNodeName === nodeName))) {
550
- res = node;
545
+ if (astPrefix === '' && nodePrefix === '') {
546
+ if (node.namespaceURI === null &&
547
+ (astNodeName === '*' || astNodeName === nodeName)) {
548
+ res = node;
549
+ }
550
+ } else if (astPrefix === '*') {
551
+ if (astNodeName === '*' || astNodeName === nodeName) {
552
+ res = node;
553
+ }
554
+ } else if (astPrefix === nodePrefix) {
555
+ if (astNodeName === '*' || astNodeName === nodeName) {
556
+ res = node;
557
+ }
551
558
  }
552
559
  }
553
560
  return res ?? null;
@@ -605,51 +612,53 @@ const matchAttributeSelector = (ast = {}, node = {}) => {
605
612
  if (typeof astFlags === 'string' && !/^[is]$/i.test(astFlags)) {
606
613
  throw new DOMException('invalid attribute selector', 'SyntaxError');
607
614
  }
608
- const { attributes, nodeType } = node;
615
+ const { attributes, nodeType, ownerDocument } = node;
609
616
  let res;
610
617
  if (astType === ATTRIBUTE_SELECTOR && nodeType === ELEMENT_NODE &&
611
618
  attributes?.length) {
612
- const caseInsensitive =
613
- !(typeof astFlags === 'string' && /^s$/i.test(astFlags));
614
- const attrValues = [];
615
- const l = attributes.length;
616
619
  let { name: astAttrName } = astName;
617
620
  astAttrName = unescapeSelector(astAttrName);
621
+ let caseInsensitive;
622
+ if (ownerDocument?.contentType === 'text/html') {
623
+ if (typeof astFlags === 'string' && /^s$/i.test(astFlags)) {
624
+ caseInsensitive = false;
625
+ } else {
626
+ caseInsensitive = true;
627
+ }
628
+ } else if (typeof astFlags === 'string' && /^i$/i.test(astFlags)) {
629
+ caseInsensitive = true;
630
+ } else {
631
+ caseInsensitive = false;
632
+ }
618
633
  if (caseInsensitive) {
619
634
  astAttrName = astAttrName.toLowerCase();
620
635
  }
636
+ const l = attributes.length;
637
+ const attrValues = [];
621
638
  // namespaced
622
639
  if (/\|/.test(astAttrName)) {
623
640
  const [astAttrPrefix, astAttrLocalName] = astAttrName.split('|');
624
641
  let i = 0;
625
642
  while (i < l) {
626
- const { name: itemName, value: itemValue } = attributes.item(i);
643
+ let { name: itemName, value: itemValue } = attributes.item(i);
644
+ if (caseInsensitive) {
645
+ itemName = itemName.toLowerCase();
646
+ itemValue = itemValue.toLowerCase();
647
+ }
627
648
  switch (astAttrPrefix) {
628
649
  case '': {
629
650
  if (astAttrLocalName === itemName) {
630
- if (caseInsensitive) {
631
- attrValues.push(itemValue.toLowerCase());
632
- } else {
633
- attrValues.push(itemValue);
634
- }
651
+ attrValues.push(itemValue);
635
652
  }
636
653
  break;
637
654
  }
638
655
  case '*': {
639
656
  if (/:/.test(itemName)) {
640
657
  if (itemName.endsWith(`:${astAttrLocalName}`)) {
641
- if (caseInsensitive) {
642
- attrValues.push(itemValue.toLowerCase());
643
- } else {
644
- attrValues.push(itemValue);
645
- }
646
- }
647
- } else if (astAttrLocalName === itemName) {
648
- if (caseInsensitive) {
649
- attrValues.push(itemValue.toLowerCase());
650
- } else {
651
658
  attrValues.push(itemValue);
652
659
  }
660
+ } else if (astAttrLocalName === itemName) {
661
+ attrValues.push(itemValue);
653
662
  }
654
663
  break;
655
664
  }
@@ -658,11 +667,7 @@ const matchAttributeSelector = (ast = {}, node = {}) => {
658
667
  const [itemNamePrefix, itemNameLocalName] = itemName.split(':');
659
668
  if (astAttrPrefix === itemNamePrefix &&
660
669
  astAttrLocalName === itemNameLocalName) {
661
- if (caseInsensitive) {
662
- attrValues.push(itemValue.toLowerCase());
663
- } else {
664
- attrValues.push(itemValue);
665
- }
670
+ attrValues.push(itemValue);
666
671
  }
667
672
  }
668
673
  }
@@ -672,22 +677,18 @@ const matchAttributeSelector = (ast = {}, node = {}) => {
672
677
  } else {
673
678
  let i = 0;
674
679
  while (i < l) {
675
- const { name: itemName, value: itemValue } = attributes.item(i);
680
+ let { name: itemName, value: itemValue } = attributes.item(i);
681
+ if (caseInsensitive) {
682
+ itemName = itemName.toLowerCase();
683
+ itemValue = itemValue.toLowerCase();
684
+ }
676
685
  if (/:/.test(itemName)) {
677
686
  const [, itemNameLocalName] = itemName.split(':');
678
687
  if (astAttrName === itemNameLocalName) {
679
- if (caseInsensitive) {
680
- attrValues.push(itemValue.toLowerCase());
681
- } else {
682
- attrValues.push(itemValue);
683
- }
684
- }
685
- } else if (astAttrName === itemName) {
686
- if (caseInsensitive) {
687
- attrValues.push(itemValue.toLowerCase());
688
- } else {
689
688
  attrValues.push(itemValue);
690
689
  }
690
+ } else if (astAttrName === itemName) {
691
+ attrValues.push(itemValue);
691
692
  }
692
693
  i++;
693
694
  }
@@ -724,7 +725,7 @@ const matchAttributeSelector = (ast = {}, node = {}) => {
724
725
  break;
725
726
  }
726
727
  case '~=': {
727
- if (typeof attrValue === 'string') {
728
+ if (attrValue && typeof attrValue === 'string') {
728
729
  for (const item of attrValues) {
729
730
  const arr = item.split(/\s+/);
730
731
  if (arr.includes(attrValue)) {
@@ -736,7 +737,7 @@ const matchAttributeSelector = (ast = {}, node = {}) => {
736
737
  break;
737
738
  }
738
739
  case '|=': {
739
- if (typeof attrValue === 'string') {
740
+ if (attrValue && typeof attrValue === 'string') {
740
741
  const item = attrValues.find(v =>
741
742
  (v === attrValue || v.startsWith(`${attrValue}-`))
742
743
  );
@@ -747,7 +748,7 @@ const matchAttributeSelector = (ast = {}, node = {}) => {
747
748
  break;
748
749
  }
749
750
  case '^=': {
750
- if (typeof attrValue === 'string') {
751
+ if (attrValue && typeof attrValue === 'string') {
751
752
  const item = attrValues.find(v => v.startsWith(`${attrValue}`));
752
753
  if (item) {
753
754
  res = node;
@@ -756,7 +757,7 @@ const matchAttributeSelector = (ast = {}, node = {}) => {
756
757
  break;
757
758
  }
758
759
  case '$=': {
759
- if (typeof attrValue === 'string') {
760
+ if (attrValue && typeof attrValue === 'string') {
760
761
  const item = attrValues.find(v => v.endsWith(`${attrValue}`));
761
762
  if (item) {
762
763
  res = node;
@@ -765,7 +766,7 @@ const matchAttributeSelector = (ast = {}, node = {}) => {
765
766
  break;
766
767
  }
767
768
  case '*=': {
768
- if (typeof attrValue === 'string') {
769
+ if (attrValue && typeof attrValue === 'string') {
769
770
  const item = attrValues.find(v => v.includes(`${attrValue}`));
770
771
  if (item) {
771
772
  res = node;
package/src/js/parser.js CHANGED
@@ -107,7 +107,7 @@ const walkAST = (ast = {}) => {
107
107
  walk(ast, opt);
108
108
  findAll(ast, (node, item, list) => {
109
109
  if (node.type === PSEUDO_CLASS_SELECTOR && PSEUDO_FUNC.test(node.name) &&
110
- item) {
110
+ list && list.length) {
111
111
  for (const i of list) {
112
112
  const { children, name, type } = i;
113
113
  if (type === PSEUDO_CLASS_SELECTOR && PSEUDO_FUNC.test(name) &&