Skip to content

Commit

Permalink
capricorn86#622@patch: Fix Element.matches failing when using non-mat…
Browse files Browse the repository at this point in the history
…ching descendant selector on element attached to document.
  • Loading branch information
IGx89 committed Oct 15, 2022
1 parent 414e6ab commit 901a709
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/happy-dom/src/query-selector/QuerySelector.ts
Expand Up @@ -104,15 +104,15 @@ export default class QuerySelector {
}

const selector = new SelectorItem(selectorParts[0]);
const result = selector.match(<Element>currentNode);
const result = selector.match(<IElement>currentNode);

if ((targetNode === currentNode || !currentNode.parentNode) && !result.matches) {
if ((targetNode === currentNode || !currentNode.parentElement) && !result.matches) {
return { priorityWeight: 0, matches: false };
}

return this.matchesSelector(
isDirectChild ? currentNode.parentNode : targetNode,
currentNode.parentNode,
isDirectChild ? currentNode.parentElement : targetNode,
currentNode.parentElement,
result.matches ? selectorParts.slice(1) : selectorParts,
priorityWeight + result.priorityWeight
);
Expand Down
2 changes: 2 additions & 0 deletions packages/happy-dom/test/nodes/element/Element.test.ts
Expand Up @@ -698,6 +698,7 @@ describe('Element', () => {
it('Checks if the element matches with a descendant combinator', () => {
const grandparentElement = document.createElement('div');
grandparentElement.setAttribute('role', 'alert');
document.appendChild(grandparentElement);

const parentElement = document.createElement('div');
parentElement.setAttribute('role', 'status');
Expand All @@ -709,6 +710,7 @@ describe('Element', () => {

expect(element.matches('div[role="alert"] div.active')).toBe(true);
expect(element.matches('div[role="article"] div.active')).toBe(false);
expect(element.matches('.nonexistent-class div.active')).toBe(false);
});

it('Checks if the element matches with a child combinator', () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/happy-dom/test/nodes/node/Node.test.ts
Expand Up @@ -196,6 +196,14 @@ describe('Node', () => {

expect(text.parentElement).toBe(null);
});

it('Returns null if parent node is not an element.', () => {
const htmlElement = document.createElement('html');
document.appendChild(htmlElement);

expect(htmlElement.parentNode).toBe(document);
expect(htmlElement.parentElement).toBe(null);
});
});

describe('get baseURI()', () => {
Expand Down

0 comments on commit 901a709

Please sign in to comment.