Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/happy-dom/src/css/utilities/CSSParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,11 @@ export default class CSSParser {
* @returns True if valid, false otherwise.
*/
private validateSelectorText(selectorText: string): boolean {
try {
SelectorParser.getSelectorGroups(selectorText);
} catch (e) {
return false;
}
return true;
const window = this.#parentStyleSheet[PropertySymbol.window];
return (
new SelectorParser({ window, scope: window.document, ignoreErrors: true }).getSelectorGroups(
selectorText
).length > 0
);
}
}
44 changes: 36 additions & 8 deletions packages/happy-dom/src/query-selector/QuerySelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export default class QuerySelector {
node[PropertySymbol.nodeType] === NodeTypeEnum.documentNode
? (<Document>node).documentElement
: node;
const groups = SelectorParser.getSelectorGroups(selector, { scope });
const groups = new SelectorParser({ window, scope }).getSelectorGroups(selector);
const items: Element[] = [];
const nodeList = new NodeList<Element>(PropertySymbol.illegalConstructor, items);
const matchesMap: Map<string, Element> = new Map();
Expand Down Expand Up @@ -267,7 +267,7 @@ export default class QuerySelector {
? (<Document>node).documentElement
: node;

for (const items of SelectorParser.getSelectorGroups(selector, { scope })) {
for (const items of new SelectorParser({ window, scope }).getSelectorGroups(selector)) {
const match =
node[PropertySymbol.nodeType] === NodeTypeEnum.elementNode
? this.findFirst(<Element>node, [<Element>node], items, cachedItem)
Expand Down Expand Up @@ -375,10 +375,11 @@ export default class QuerySelector {
scopeOrElement[PropertySymbol.nodeType] === NodeTypeEnum.documentNode
? (<Document>scopeOrElement).documentElement
: scopeOrElement;
for (const items of SelectorParser.getSelectorGroups(selector, {
...options,
for (const items of new SelectorParser({
ignoreErrors: options?.ignoreErrors,
window,
scope
})) {
}).getSelectorGroups(selector)) {
const result = this.matchSelector(element, items.reverse(), cachedItem);

if (result) {
Expand Down Expand Up @@ -409,6 +410,11 @@ export default class QuerySelector {
priorityWeight = 0
): ISelectorMatch | null {
const selectorItem = selectorItems[0];

if (!selectorItem) {
return null;
}

const result = selectorItem.match(element);

if (result) {
Expand Down Expand Up @@ -437,6 +443,7 @@ export default class QuerySelector {
}
}
break;
case SelectorCombinatorEnum.none:
case SelectorCombinatorEnum.child:
case SelectorCombinatorEnum.descendant:
const parentElement = element.parentElement;
Expand Down Expand Up @@ -486,7 +493,10 @@ export default class QuerySelector {
}
}

if (previousSelectorItem?.combinator === SelectorCombinatorEnum.descendant) {
if (
previousSelectorItem?.combinator === SelectorCombinatorEnum.none ||
previousSelectorItem?.combinator === SelectorCombinatorEnum.descendant
) {
const parentElement = element.parentElement;
if (parentElement) {
return this.matchSelector(
Expand Down Expand Up @@ -523,6 +533,10 @@ export default class QuerySelector {
const nextSelectorItem = selectorItems[1];
let matched: DocumentPositionAndElement[] = [];

if (!selectorItem) {
return [];
}

for (let i = 0, max = children.length; i < max; i++) {
const child = children[i];
const childrenOfChild = (<Element>child)[PropertySymbol.elementArray];
Expand Down Expand Up @@ -554,6 +568,7 @@ export default class QuerySelector {
);
}
break;
case SelectorCombinatorEnum.none:
case SelectorCombinatorEnum.descendant:
case SelectorCombinatorEnum.child:
matched = matched.concat(
Expand All @@ -579,7 +594,11 @@ export default class QuerySelector {
}
}

if (selectorItem.combinator === SelectorCombinatorEnum.descendant && childrenOfChild.length) {
if (
(selectorItem.combinator === SelectorCombinatorEnum.none ||
selectorItem.combinator === SelectorCombinatorEnum.descendant) &&
childrenOfChild.length
) {
matched = matched.concat(
this.findAll(rootElement, childrenOfChild, selectorItems, cachedItem, position)
);
Expand Down Expand Up @@ -609,6 +628,10 @@ export default class QuerySelector {
const selectorItem = selectorItems[0];
const nextSelectorItem = selectorItems[1];

if (!selectorItem) {
return null;
}

for (let i = 0, max = children.length; i < max; i++) {
const child = children[i];
const childrenOfChild = (<Element>child)[PropertySymbol.elementArray];
Expand Down Expand Up @@ -638,6 +661,7 @@ export default class QuerySelector {
}
}
break;
case SelectorCombinatorEnum.none:
case SelectorCombinatorEnum.descendant:
case SelectorCombinatorEnum.child:
const match = this.findFirst(
Expand Down Expand Up @@ -671,7 +695,11 @@ export default class QuerySelector {
}
}

if (selectorItem.combinator === SelectorCombinatorEnum.descendant && childrenOfChild.length) {
if (
(selectorItem.combinator === SelectorCombinatorEnum.none ||
selectorItem.combinator === SelectorCombinatorEnum.descendant) &&
childrenOfChild.length
) {
const match = this.findFirst(
rootElement,
childrenOfChild,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
enum SelectorCombinatorEnum {
none = 'none',
descendant = 'descendant',
child = 'child',
adjacentSibling = 'adjacentSibling',
Expand Down
7 changes: 5 additions & 2 deletions packages/happy-dom/src/query-selector/SelectorItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,18 @@ export default class SelectorItem {
combinator?: SelectorCombinatorEnum;
ignoreErrors?: boolean;
}) {
this.root = options?.scope ? options.scope[PropertySymbol.ownerDocument].documentElement : null;
this.root =
options?.scope?.[PropertySymbol.ownerDocument]?.documentElement ||
options?.scope?.[PropertySymbol.window].document?.documentElement ||
null;
this.scope = options?.scope || null;
this.tagName = options?.tagName || null;
this.id = options?.id || null;
this.classNames = options?.classNames || null;
this.attributes = options?.attributes || null;
this.pseudos = options?.pseudos || null;
this.isPseudoElement = options?.isPseudoElement || false;
this.combinator = options?.combinator || SelectorCombinatorEnum.descendant;
this.combinator = options?.combinator || SelectorCombinatorEnum.none;
this.ignoreErrors = options?.ignoreErrors || false;
}

Expand Down
Loading