Skip to content

Commit 13c0b1b

Browse files
authored
added matches method to HTMLElement
1 parent d604652 commit 13c0b1b

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class HTMLElement{
114114
this removeWhitespace()
115115
Node[] querySelectorAll(string selector)
116116
Node querySelector(string selector)
117+
this matches(string selector)
117118
HTMLElement[] getElementsByTagName(string tagName)
118119
Node closest(string selector)
119120
Node appendChild(Node node)
@@ -208,6 +209,10 @@ Note: Full range of CSS3 selectors supported since v3.0.0.
208209

209210
Query CSS Selector to find matching node. `null` if not found.
210211

212+
### matches(selector)
213+
214+
Tests whether the node matches a given CSS selector.
215+
211216
### getElementsByTagName(tagName)
212217

213218
Get all elements with the specified tagName.

src/nodes/html.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { selectAll, selectOne } from 'css-select';
1+
import { is, selectAll, selectOne } from 'css-select';
22
import he from 'he';
33
import arr_back from '../back';
44
import Matcher from '../matcher';
@@ -498,6 +498,18 @@ export default class HTMLElement extends Node {
498498
});
499499
}
500500

501+
/**
502+
* Tests whether the node matches a given CSS selector.
503+
* @param {string} selector Simplified CSS selector
504+
* @return {boolean}
505+
*/
506+
public matches(selector: string): boolean {
507+
return is(this as HTMLElement, selector, {
508+
xmlMode: true,
509+
adapter: Matcher,
510+
});
511+
}
512+
501513
/**
502514
* find elements by their tagName
503515
* @param {string} tagName the tagName of the elements to select

0 commit comments

Comments
 (0)