Skip to content

Commit

Permalink
Merge pull request #128 from capricorn86/task/123-element-matches
Browse files Browse the repository at this point in the history
#123@minor: Thanks to @beckerei we now have support for Element.match…
  • Loading branch information
capricorn86 authored Oct 20, 2020
2 parents 41ce558 + 1722681 commit 63be9ae
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/happy-dom/src/nodes/basic/element/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import DOMRect from './DOMRect';
import Range from './Range';
import ClassList from './ClassList';
import QuerySelector from '../../../query-selector/QuerySelector';
import SelectorItem from '../../../query-selector/SelectorItem';
import MutationRecord from '../../../mutation-observer/MutationRecord';
import MutationTypeConstant from '../../../mutation-observer/MutationType';
import NamespaceURI from '../../../html-config/NamespaceURI';
Expand Down Expand Up @@ -558,6 +559,16 @@ export default class Element extends Node implements IElement {
return new Range();
}

/**
* The matches() method checks to see if the Element would be selected by the provided selectorString.
*
* @param selector Selector.
* @returns "true" if matching.
*/
public matches(selector: string): boolean {
return new SelectorItem(selector).match(this);
}

/**
* Query CSS selector to find matching nodes.
*
Expand Down
8 changes: 8 additions & 0 deletions packages/happy-dom/src/nodes/basic/element/IElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ export default interface IElement extends IChildNode, INonDocumentTypeChildNode,
*/
createTextRange(): Range;

/**
* The matches() method checks to see if the Element would be selected by the provided selectorString.
*
* @param selector Selector.
* @returns "true" if matching.
*/
matches(selector: string): boolean;

/**
* Returns an elements by tag name.
*
Expand Down
10 changes: 10 additions & 0 deletions packages/happy-dom/test/nodes/basic/element/Element.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,16 @@ describe('Element', () => {
});
});

describe('matches()', () => {
test('Checks if the element matches a selector string.', () => {
const element = document.createElement('div');

element.className = 'container active';

expect(element.matches('.container.active')).toBe(true);
});
});

describe('querySelectorAll()', () => {
test('Query CSS selector to find matching elements.', () => {
const element = document.createElement('div');
Expand Down

0 comments on commit 63be9ae

Please sign in to comment.