Skip to content

Commit 663eec4

Browse files
committed
fix: query elements
1 parent 94dd267 commit 663eec4

File tree

1 file changed

+52
-44
lines changed

1 file changed

+52
-44
lines changed

src/index.js

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -551,38 +551,36 @@
551551
"top"
552552
];
553553

554-
const queryTypesRegex = new RegExp(
555-
`\\$(?:${queryTypes.join("|")})\\b`,
556-
"g"
557-
);
554+
const queryTypesRegex = new RegExp(`\\$(?:${queryTypes.join("|")})\\b`); // Find the *first* match
558555

559556
function queryElements({ element = document, prefix, type, selector }) {
560557
let elements = new Set();
561558

562559
let hasAttribute = false;
563560

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;
568566

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+
}
574573
}
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+
}
580580
}
581581
}
582582
}
583583

584-
if (!prefix) return false;
585-
586584
if (!type) type = selector ? ["selector"] : queryTypes;
587585

588586
if (!Array.isArray(type)) type = [type];
@@ -636,38 +634,48 @@
636634
selectors[j] = remainingSelector;
637635
}
638636

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(
655654
queriedElement,
656-
specialSelectors[k]
655+
part
657656
);
658-
continue; // Skip directly to the next specialSelector
657+
if (!queriedElement) break;
659658
}
660659

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;
664666

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) {
665675
queriedElement = querySelector(
666676
queriedElement,
667-
specialSelectors[k]
677+
remainingSelector.trim().replace(/,$/, "")
668678
);
669-
670-
if (!queriedElement) break;
671679
}
672680

673681
if (

0 commit comments

Comments
 (0)