Skip to content

Commit

Permalink
Merge pull request #623 from IGx89/task/622-fix-matches-non-matching-…
Browse files Browse the repository at this point in the history
…descendant-seelctor

#622@patch: Fix Element.matches failing when using non-matching desce…
  • Loading branch information
capricorn86 committed Oct 17, 2022
2 parents 414e6ab + 901a709 commit a6407b7
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 a6407b7

Please sign in to comment.