|
551 | 551 | "top" |
552 | 552 | ]; |
553 | 553 |
|
554 | | - const queryTypesRegex = new RegExp( |
555 | | - `\\$(?:${queryTypes.join("|")})\\b`, |
556 | | - "g" |
557 | | - ); |
| 554 | + const queryTypesRegex = new RegExp(`\\$(?:${queryTypes.join("|")})\\b`); // Find the *first* match |
558 | 555 |
|
559 | 556 | function queryElements({ element = document, prefix, type, selector }) { |
560 | 557 | let elements = new Set(); |
561 | 558 |
|
562 | 559 | let hasAttribute = false; |
563 | 560 |
|
564 | | - if (!prefix) { |
565 | | - for (let attr of element.attributes) { |
566 | | - let parts = attr.name.split("-"); |
567 | | - if (parts.length < 2) continue; |
| 561 | + if (!selector) { |
| 562 | + if (!prefix && element.nodeType === 1) { |
| 563 | + for (let attr of element.attributes) { |
| 564 | + let parts = attr.name.split("-"); |
| 565 | + if (parts.length < 2) continue; |
568 | 566 |
|
569 | | - let possibleType = parts.pop(); |
570 | | - if (queryTypes.includes(possibleType)) { |
571 | | - type = [possibleType]; |
572 | | - prefix = parts.join("-"); |
573 | | - break; |
| 567 | + let possibleType = parts.pop(); |
| 568 | + if (queryTypes.includes(possibleType)) { |
| 569 | + type = [possibleType]; |
| 570 | + prefix = parts.join("-"); |
| 571 | + break; |
| 572 | + } |
574 | 573 | } |
575 | | - } |
576 | | - } else if (!type) { |
577 | | - for (let i = 0; i < queryTypes.length; i++) { |
578 | | - if (element.hasAttribute(`${prefix}-${queryTypes[i]}`)) { |
579 | | - type = [queryTypes[i]]; |
| 574 | + if (!prefix) return false; |
| 575 | + } else if (!type && element.nodeType === 1) { |
| 576 | + for (let i = 0; i < queryTypes.length; i++) { |
| 577 | + if (element.hasAttribute(`${prefix}-${queryTypes[i]}`)) { |
| 578 | + type = [queryTypes[i]]; |
| 579 | + } |
580 | 580 | } |
581 | 581 | } |
582 | 582 | } |
583 | 583 |
|
584 | | - if (!prefix) return false; |
585 | | - |
586 | 584 | if (!type) type = selector ? ["selector"] : queryTypes; |
587 | 585 |
|
588 | 586 | if (!Array.isArray(type)) type = [type]; |
|
636 | 634 | selectors[j] = remainingSelector; |
637 | 635 | } |
638 | 636 |
|
639 | | - // Split selector while preserving special operators |
640 | | - let specialSelectors = selector |
641 | | - .split(queryTypesRegex) |
642 | | - .filter(Boolean); |
643 | | - |
644 | | - // If no special operators are found, return selector as a single-element array |
645 | | - specialSelectors = |
646 | | - specialSelectors.length === 1 |
647 | | - ? [selector] |
648 | | - : specialSelectors; |
649 | | - |
650 | | - for (let k = 0; k < specialSelectors.length; k++) { |
651 | | - specialSelectors[k] = specialSelectors[k].trim(); |
652 | | - if (!specialSelectors[k]) continue; |
653 | | - if (queryTypes.includes(specialSelectors[k])) { |
654 | | - queriedElement = queryType( |
| 637 | + let remainingSelector = selector.trim(); |
| 638 | + let match; |
| 639 | + |
| 640 | + while ( |
| 641 | + (match = queryTypesRegex.exec(remainingSelector)) !== |
| 642 | + null |
| 643 | + ) { |
| 644 | + const matchIndex = match.index; |
| 645 | + const operator = match[0]; |
| 646 | + |
| 647 | + // Process the part before the operator (if any) |
| 648 | + const part = remainingSelector |
| 649 | + .substring(0, matchIndex) |
| 650 | + .trim() |
| 651 | + .replace(/,$/, ""); |
| 652 | + if (part) { |
| 653 | + queriedElement = querySelector( |
655 | 654 | queriedElement, |
656 | | - specialSelectors[k] |
| 655 | + part |
657 | 656 | ); |
658 | | - continue; // Skip directly to the next specialSelector |
| 657 | + if (!queriedElement) break; |
659 | 658 | } |
660 | 659 |
|
661 | | - if (queriedElement.contentDocument) { |
662 | | - queriedElement = queriedElement.contentDocument; |
663 | | - } |
| 660 | + // Process the operator |
| 661 | + queriedElement = queryType( |
| 662 | + queriedElement, |
| 663 | + operator.substring(1) |
| 664 | + ); |
| 665 | + if (!queriedElement) break; |
664 | 666 |
|
| 667 | + // Remove the processed part and operator from the remaining selector |
| 668 | + remainingSelector = remainingSelector |
| 669 | + .substring(matchIndex + operator.length) |
| 670 | + .trim(); |
| 671 | + } |
| 672 | + |
| 673 | + // Process the remaining part after the last operator (if any) |
| 674 | + if (remainingSelector) { |
665 | 675 | queriedElement = querySelector( |
666 | 676 | queriedElement, |
667 | | - specialSelectors[k] |
| 677 | + remainingSelector.trim().replace(/,$/, "") |
668 | 678 | ); |
669 | | - |
670 | | - if (!queriedElement) break; |
671 | 679 | } |
672 | 680 |
|
673 | 681 | if ( |
|
0 commit comments