DOM Selector
Retrieve DOM node from the given CSS selector.
Experimental
Install
npm i @asamuzakjp/dom-selector
Usage
const {
matches, closest, querySelector, querySelectorAll
} = require('@asamuzakjp/dom-selector');
matches(selector, node, opt)
matches - Element.matches()
Parameters
selector
string CSS selectornode
object Element nodeopt
object? options
opt.globalObject
object? global object, e.g. window
, globalThis
opt.jsdom
boolean? is jsdom
Returns boolean result
closest(selector, node, opt)
closest - Element.closest()
Parameters
selector
string CSS selectornode
object Element nodeopt
object? options
opt.globalObject
object? global object, e.g. window
, globalThis
opt.jsdom
boolean? is jsdom
Returns object? matched node
querySelector(selector, refPoint, opt)
querySelector - Document.querySelector(), DocumentFragment.querySelector(), Element.querySelector()
Parameters
selector
string CSS selectorrefPoint
object Document, DocumentFragment or Element nodeopt
object? options
opt.globalObject
object? global object, e.g. window
, globalThis
opt.jsdom
boolean? is jsdom
Returns object? matched node
querySelectorAll(selector, refPoint, opt)
querySelectorAll - Document.querySelectorAll(), Document.querySelectorAll(), Element.querySelectorAll()
NOTE: returns Array, not NodeList
Parameters
selector
string CSS selectorrefPoint
object Document, DocumentFragment or Element nodeopt
object? options
opt.globalObject
object? global object, e.g. window
, globalThis
opt.jsdom
boolean? is jsdom
Returns Array<(object | undefined)> array of matched nodes
Acknowledgments
The following resources have been of great help in the development of the DOM Selector.