@asamuzakjp/dom-selector
A CSS selector engine.
Last updated 3 years ago by asamuzakjp .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @asamuzakjp/dom-selector 
SYNC missed versions from official npm registry.

DOM Selector

build CodeQL npm (scoped)

A CSS selector engine.

Install

npm i @asamuzakjp/dom-selector

Usage

import {
  matches, closest, querySelector, querySelectorAll
} from '@asamuzakjp/dom-selector';

matches(selector, node, opt)

matches - same functionality as Element.matches()

Parameters

  • selector string CSS selector
  • node object Element node
  • opt object? options
    • opt.noexcept boolean? no exception
    • opt.warn boolean? console warn e.g. unsupported pseudo-class

Returns boolean true if matched, false otherwise

closest(selector, node, opt)

closest - same functionality as Element.closest()

Parameters

  • selector string CSS selector
  • node object Element node
  • opt object? options
    • opt.noexcept boolean? no exception
    • opt.warn boolean? console warn e.g. unsupported pseudo-class

Returns object? matched node

querySelector(selector, node, opt)

querySelector - same functionality as Document.querySelector(), DocumentFragment.querySelector(), Element.querySelector()

Parameters

  • selector string CSS selector
  • node object Document, DocumentFragment or Element node
  • opt object? options
    • opt.noexcept boolean? no exception
    • opt.warn boolean? console warn e.g. unsupported pseudo-class

Returns object? matched node

querySelectorAll(selector, node, opt)

querySelectorAll - same functionality as Document.querySelectorAll(), DocumentFragment.querySelectorAll(), Element.querySelectorAll()
NOTE: returns Array, not NodeList

Parameters

  • selector string CSS selector
  • node object Document, DocumentFragment or Element node
  • opt object? options
    • opt.noexcept boolean? no exception
    • opt.warn boolean? console warn e.g. unsupported pseudo-class

Returns Array<(object | undefined)> array of matched nodes

Supported CSS selectors

Pattern Supported Note
* ✓
ns|E ✓
*|E ✓
|E ✓
E ✓
E:not(s1, s2, …) ✓
E:is(s1, s2, …) ✓
E:where(s1, s2, …) ✓
E:has(rs1, rs2, …) ✓
E.warning ✓
E#myid ✓
E[foo] ✓
E[foo="bar"] ✓
E[foo="bar" i] ✓
E[foo="bar" s] ✓
E[foo~="bar"] ✓
E[foo^="bar"] ✓
E[foo$="bar"] ✓
E[foo*="bar"] ✓
E[foo|="en"] ✓
E:defined Unsupported
E:dir(ltr) ✓
E:lang(en) Partially supported Comma-separated list of language codes, e.g. :lang(en, fr), is not yet supported.
E:any‑link ✓
E:link ✓
E:visited ✓ Returns false or null to prevent fingerprinting.
E:local‑link ✓
E:target ✓
E:target‑within ✓
E:scope ✓
E:current Unsupported
E:current(s) Unsupported
E:past Unsupported
E:future Unsupported
E:active Unsupported
E:hover Unsupported
E:focus ✓
E:focus‑within ✓
E:focus‑visible Unsupported
E:enabled
E:disabled
✓
E:read‑write
E:read‑only
✓
E:placeholder‑shown ✓
E:default ✓
E:checked ✓
E:indeterminate ✓
E:valid
E:invalid
✓
E:required
E:optional
✓
E:blank Unsupported
E:user‑invalid Unsupported
E:root ✓
E:empty ✓
E:nth‑child(n [of S]?) ✓
E:nth‑last‑child(n [of S]?) ✓
E:first‑child ✓
E:last‑child ✓
E:only‑child ✓
E:nth‑of‑type(n) ✓
E:nth‑last‑of‑type(n) ✓
E:first‑of‑type ✓
E:last‑of‑type ✓
E:only‑of‑type ✓
E F ✓
E > F ✓
E + F ✓
E ~ F ✓
F || E Unsupported
E:nth‑col(n) Unsupported
E:nth‑last‑col(n) Unsupported
:host ✓
:host(s) ✓
:host‑context(s) ✓

Monkey patch jsdom

import { JSDOM } from 'jsdom';
import {
  closest, matches, querySelector, querySelectorAll
} from '@asamuzakjp/dom-selector';

const dom = new JSDOM('', {
  runScripts: 'dangerously',
  url: 'http://localhost/',
  beforeParse: window => {
    window.Element.prototype.matches = function (...args) {
      if (!args.length) {
        throw new window.TypeError('1 argument required, but only 0 present.');
      }
      const [selector] = args;
      return matches(selector, this);
    };
    window.Element.prototype.closest = function (...args) {
      if (!args.length) {
        throw new window.TypeError('1 argument required, but only 0 present.');
      }
      const [selector] = args;
      return closest(selector, this);
    };
    window.Document.prototype.querySelector = function (...args) {
      if (!args.length) {
        throw new window.TypeError('1 argument required, but only 0 present.');
      }
      const [selector] = args;
      return querySelector(selector, this);
    };
    window.DocumentFragment.prototype.querySelector = function (...args) {
      if (!args.length) {
        throw new window.TypeError('1 argument required, but only 0 present.');
      }
      const [selector] = args;
      return querySelector(selector, this);
    };
    window.Element.prototype.querySelector = function (...args) {
      if (!args.length) {
        throw new window.TypeError('1 argument required, but only 0 present.');
      }
      const [selector] = args;
      return querySelector(selector, this);
    };
    window.Document.prototype.querySelectorAll = function (...args) {
      if (!args.length) {
        throw new window.TypeError('1 argument required, but only 0 present.');
      }
      const [selector] = args;
      return querySelectorAll(selector, this);
    };
    window.DocumentFragment.prototype.querySelectorAll = function (...args) {
      if (!args.length) {
        throw new window.TypeError('1 argument required, but only 0 present.');
      }
      const [selector] = args;
      return querySelectorAll(selector, this);
    };
    window.Element.prototype.querySelectorAll = function (...args) {
      if (!args.length) {
        throw new window.TypeError('1 argument required, but only 0 present.');
      }
      const [selector] = args;
      return querySelectorAll(selector, this);
    };
  }
});

Performance

matches()

Selector jsdom v24.0.0 (nwsapi) happy-dom linkeDom patched-jsdom (dom-selector) Result
simple selector:
matches('.content')
2,730,719 ops/sec ±0.80% 1,669,966 ops/sec ±0.28% 657,226 ops/sec ±1.30% 191,661 ops/sec ±0.17% jsdom is the fastest and 14.2 times faster than patched-jsdom.
compound selector:
matches('p.content[id]:only-child')
1,153,187 ops/sec ±0.26% 470,767 ops/sec ±0.85% 309,344 ops/sec ±0.18% 89,231 ops/sec ±0.70% jsdom is the fastest and 12.9 times faster than patched-jsdom.
complex selector:
matches('.box:first-child ~ .box:nth-of-type(4n+1) + .box[id] .block.inner > .content')
165,982 ops/sec ±0.46% N/A 72,727 ops/sec ±0.25% 21,674 ops/sec ±1.35% jsdom is the fastest and 7.7 times faster than patched-jsdom.

closest()

Selector jsdom v24.0.0 (nwsapi) happy-dom linkeDom patched-jsdom (dom-selector) Result
simple selector:
closest('.container')
578,462 ops/sec ±1.64% 361,946 ops/sec ±2.15% 397,660 ops/sec ±1.70% 66,355 ops/sec ±1.68% jsdom is the fastest and 8.7 times faster than patched-jsdom.
compound selector:
closest('div.container[id]:not(.box)')
216,725 ops/sec ±1.66% 79,273 ops/sec ±1.88% 169,185 ops/sec ±1.81% 33,135 ops/sec ±0.98% jsdom is the fastest and 6.5 times faster than patched-jsdom.
complex selector:
closest('.box:first-child ~ .box:nth-of-type(4n+1) + .box[id] .block.inner > .content')
159,848 ops/sec ±0.29% N/A 72,070 ops/sec ±1.68% 17,559 ops/sec ±2.09% jsdom is the fastest and 9.1 times faster than patched-jsdom.

querySelector()

Selector jsdom v24.0.0 (nwsapi) happy-dom linkeDom patched-jsdom (dom-selector) Result
simple selector:
querySelector('.content')
3,326 ops/sec ±1.31% 342,846 ops/sec ±1.70% 322,199 ops/sec ±2.38% 70,933 ops/sec ±1.88% happydom is the fastest and 4.8 times faster than patched-jsdom.
compound selector:
querySelector('p.content[id]:only-child')
1,309 ops/sec ±1.19% 382,636 ops/sec ±1.56% 252,823 ops/sec ±2.03% 47,759 ops/sec ±1.44% happydom is the fastest and 8.0 times faster than patched-jsdom.
complex selector:
querySelector('.box:first-child ~ .box:nth-of-type(4n+1) + .box[id] .block.inner > .content')
242 ops/sec ±0.16% N/A 1,342 ops/sec ±0.37% 911 ops/sec ±1.80% linkedom is the fastest and 1.5 times faster than patched-jsdom.

querySelectorAll()

Selector jsdom v24.0.0 (nwsapi) happy-dom linkeDom patched-jsdom (dom-selector) Result
simple selector:
querySelectorAll('.content')
3,090 ops/sec ±1.08% 778 ops/sec ±1.56% 1,203 ops/sec ±1.37% 2,483 ops/sec ±0.90% jsdom is the fastest and 1.2 times faster than patched-jsdom.
compound selector:
querySelectorAll('p.content[id]:only-child')
1,280 ops/sec ±0.96% 915 ops/sec ±1.57% 1,029 ops/sec ±0.89% 151 ops/sec ±0.91% jsdom is the fastest and 8.5 times faster than patched-jsdom.
complex selector:
querySelectorAll('.box:first-child ~ .box:nth-of-type(4n+1) + .box[id] .block.inner > .content')
245 ops/sec ±0.26% N/A 438 ops/sec ±1.42% 904 ops/sec ±1.49% patched-jsdom is the fastest.

Acknowledgments

The following resources have been of great help in the development of the DOM Selector.


Copyright (c) 2023 asamuzaK (Kazz)

Current Tags

  • 0.2.2                                ...           beta (4 years ago)
  • 6.5.0-a.16                                ...           dev (2 years ago)
  • 10.0.5                                ...           latest (a month ago)
  • 8.0.0-a.2                                ...           next (7 months ago)

288 Versions

  • 10.0.5                                ...           a month ago
  • 10.0.4                                ...           a month ago
  • 10.0.3                                ...           a month ago
  • 10.0.2                                ...           2 months ago
  • 10.0.1                                ...           2 months ago
  • 10.0.0                                ...           2 months ago
  • 9.2.4                                ...           2 months ago
  • 9.2.3                                ...           2 months ago
  • 9.2.2                                ...           2 months ago
  • 9.2.1                                ...           2 months ago
  • 9.2.0                                ...           2 months ago
  • 9.1.4                                ...           3 months ago
  • 9.1.3                                ...           3 months ago
  • 9.1.2                                ...           3 months ago
  • 9.1.1                                ...           3 months ago
  • 9.1.0                                ...           3 months ago
  • 9.0.4                                ...           3 months ago
  • 9.0.3                                ...           3 months ago
  • 9.0.2                                ...           4 months ago
  • 9.0.1                                ...           4 months ago
  • 9.0.0                                ...           4 months ago
  • 8.3.2                                ...           4 months ago
  • 8.3.1                                ...           4 months ago
  • 8.3.0                                ...           4 months ago
  • 8.2.5                                ...           4 months ago
  • 8.2.4                                ...           4 months ago
  • 8.2.3                                ...           4 months ago
  • 8.2.2                                ...           4 months ago
  • 8.2.1                                ...           5 months ago
  • 8.2.0                                ...           5 months ago
  • 8.1.5                                ...           5 months ago
  • 8.1.4                                ...           5 months ago
  • 8.1.3                                ...           5 months ago
  • 8.1.2                                ...           6 months ago
  • 8.1.1                                ...           6 months ago
  • 8.0.2                                ...           6 months ago
  • 8.0.1                                ...           7 months ago
  • 8.0.0                                ...           7 months ago
  • 8.0.0-a.2                                ...           7 months ago
  • 8.0.0-a.1                                ...           7 months ago
  • 7.1.1                                ...           7 months ago
  • 7.1.0                                ...           7 months ago
  • 7.0.10                                ...           7 months ago
  • 7.0.9                                ...           8 months ago
  • 7.0.8                                ...           8 months ago
  • 7.0.7                                ...           8 months ago
  • 7.0.6                                ...           8 months ago
  • 7.0.5                                ...           8 months ago
  • 7.0.4                                ...           8 months ago
  • 7.0.3                                ...           8 months ago
  • 7.0.2                                ...           9 months ago
  • 7.0.1                                ...           9 months ago
  • 7.0.0                                ...           9 months ago
  • 6.8.1                                ...           9 months ago
  • 6.8.0                                ...           9 months ago
  • 6.7.8                                ...           10 months ago
  • 6.7.7                                ...           10 months ago
  • 6.7.6                                ...           a year ago
  • 6.7.5                                ...           a year ago
  • 6.7.4                                ...           a year ago
  • 6.7.3                                ...           a year ago
  • 6.7.2                                ...           a year ago
  • 6.7.0                                ...           a year ago
  • 6.6.2                                ...           a year ago
  • 6.6.1                                ...           a year ago
  • 6.5.7                                ...           a year ago
  • 6.5.6                                ...           a year ago
  • 6.5.5                                ...           a year ago
  • 6.5.4                                ...           a year ago
  • 6.5.3                                ...           a year ago
  • 6.5.2                                ...           a year ago
  • 6.5.1                                ...           a year ago
  • 6.5.0                                ...           2 years ago
  • 6.5.0-a.16                                ...           2 years ago
  • 6.5.0-a.15                                ...           2 years ago
  • 6.5.0-a.14                                ...           2 years ago
  • 6.5.0-a.13                                ...           2 years ago
  • 6.5.0-a.12                                ...           2 years ago
  • 6.5.0-a.11                                ...           2 years ago
  • 6.5.0-a.10                                ...           2 years ago
  • 6.5.0-a.9                                ...           2 years ago
  • 6.5.0-a.8                                ...           2 years ago
  • 6.5.0-a.7                                ...           2 years ago
  • 6.5.0-a.6                                ...           2 years ago
  • 6.5.0-a.5                                ...           2 years ago
  • 6.5.0-a.4                                ...           2 years ago
  • 6.5.0-a.3                                ...           2 years ago
  • 6.5.0-a.2                                ...           2 years ago
  • 6.5.0-a.1                                ...           2 years ago
  • 6.4.7                                ...           2 years ago
  • 6.4.6                                ...           2 years ago
  • 6.4.5                                ...           2 years ago
  • 6.4.4                                ...           2 years ago
  • 6.4.3                                ...           2 years ago
  • 6.4.2                                ...           2 years ago
  • 6.4.0                                ...           2 years ago
  • 6.3.7                                ...           2 years ago
  • 6.3.5                                ...           2 years ago
  • 6.3.4                                ...           2 years ago
  • 6.3.3                                ...           2 years ago
  • 6.3.2                                ...           2 years ago
  • 6.3.1                                ...           2 years ago
  • 6.3.0                                ...           2 years ago
  • 6.2.2                                ...           2 years ago
  • 6.2.1                                ...           2 years ago
  • 6.2.0                                ...           2 years ago
  • 6.1.1                                ...           2 years ago
  • 6.1.0                                ...           2 years ago
  • 6.0.5                                ...           2 years ago
  • 6.0.4                                ...           2 years ago
  • 6.0.3                                ...           2 years ago
  • 6.0.2                                ...           2 years ago
  • 6.0.1                                ...           2 years ago
  • 6.0.0                                ...           2 years ago
  • 5.4.0                                ...           2 years ago
  • 5.3.4                                ...           2 years ago
  • 5.3.3                                ...           2 years ago
  • 5.3.2                                ...           2 years ago
  • 5.3.1                                ...           2 years ago
  • 5.3.0                                ...           2 years ago
  • 5.2.2                                ...           2 years ago
  • 5.2.1                                ...           2 years ago
  • 5.2.0                                ...           2 years ago
  • 5.1.0                                ...           2 years ago
  • 5.0.9                                ...           2 years ago
  • 5.0.8                                ...           2 years ago
  • 5.0.7                                ...           2 years ago
  • 5.0.5                                ...           2 years ago
  • 5.0.4                                ...           2 years ago
  • 5.0.3                                ...           2 years ago
  • 5.0.2                                ...           2 years ago
  • 5.0.1                                ...           2 years ago
  • 5.0.0                                ...           2 years ago
  • 4.6.5                                ...           2 years ago
  • 4.6.4                                ...           2 years ago
  • 4.6.3                                ...           2 years ago
  • 4.6.1                                ...           2 years ago
  • 4.6.0                                ...           2 years ago
  • 4.6.0-b.1                                ...           2 years ago
  • 4.5.0                                ...           2 years ago
  • 4.5.0-b.7                                ...           2 years ago
  • 4.5.0-b.6                                ...           2 years ago
  • 4.5.0-b.5                                ...           2 years ago
  • 4.5.0-b.4                                ...           2 years ago
  • 4.5.0-b.3                                ...           2 years ago
  • 4.5.0-b.2                                ...           2 years ago
  • 4.5.0-b.1                                ...           2 years ago
  • 4.4.13                                ...           2 years ago
  • 4.4.12                                ...           2 years ago
  • 4.4.11                                ...           2 years ago
  • 4.4.10                                ...           2 years ago
  • 4.4.9                                ...           2 years ago
  • 4.4.8                                ...           2 years ago
  • 4.4.7                                ...           2 years ago
  • 4.4.6                                ...           2 years ago
  • 4.4.5                                ...           2 years ago
  • 4.4.4                                ...           2 years ago
  • 4.4.3                                ...           2 years ago
  • 4.4.2                                ...           2 years ago
  • 4.4.1                                ...           2 years ago
  • 4.4.0                                ...           2 years ago
  • 4.3.0                                ...           3 years ago
  • 4.2.2                                ...           3 years ago
  • 4.2.1                                ...           3 years ago
  • 4.2.0                                ...           3 years ago
  • 4.1.7                                ...           3 years ago
  • 4.1.6                                ...           3 years ago
  • 4.1.5                                ...           3 years ago
  • 4.1.4                                ...           3 years ago
  • 4.1.3                                ...           3 years ago
  • 4.1.2                                ...           3 years ago
  • 4.1.1                                ...           3 years ago
  • 4.1.0                                ...           3 years ago
  • 4.0.1                                ...           3 years ago
  • 4.0.0                                ...           3 years ago
  • 3.0.5                                ...           3 years ago
  • 3.0.4                                ...           3 years ago
  • 3.0.3                                ...           3 years ago
  • 3.0.2                                ...           3 years ago
  • 3.0.1                                ...           3 years ago
  • 2.1.0-b.4                                ...           3 years ago
  • 2.1.0-b.3                                ...           3 years ago
  • 2.1.0-b.2                                ...           3 years ago
  • 2.1.0-b.1                                ...           3 years ago
  • 2.0.3-a.8                                ...           3 years ago
  • 2.0.3-a.7                                ...           3 years ago
  • 2.0.3-a.6                                ...           3 years ago
  • 2.0.3-a.5                                ...           3 years ago
  • 2.0.3-a.4                                ...           3 years ago
  • 2.0.3-a.3                                ...           3 years ago
  • 2.0.3-a.2                                ...           3 years ago
  • 2.0.3-a.1                                ...           3 years ago
  • 2.0.2                                ...           3 years ago
  • 2.0.2-a.3                                ...           3 years ago
  • 2.0.2-a.2                                ...           3 years ago
  • 2.0.2-a.1                                ...           3 years ago
  • 2.0.1                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
  • 1.2.8                                ...           3 years ago
  • 1.2.7                                ...           3 years ago
  • 1.2.6                                ...           3 years ago
  • 1.2.5                                ...           3 years ago
  • 1.2.4                                ...           3 years ago
  • 1.2.3                                ...           3 years ago
  • 1.2.2                                ...           3 years ago
  • 1.2.1                                ...           3 years ago
  • 1.2.0                                ...           3 years ago
  • 1.1.14                                ...           3 years ago
  • 1.1.13                                ...           3 years ago
  • 1.1.12                                ...           3 years ago
  • 1.1.11                                ...           3 years ago
  • 1.1.10                                ...           3 years ago
  • 1.1.9                                ...           3 years ago
  • 1.1.8                                ...           3 years ago
  • 1.1.6                                ...           3 years ago
  • 1.1.5                                ...           3 years ago
  • 1.1.4                                ...           3 years ago
  • 1.1.3                                ...           3 years ago
  • 1.1.2                                ...           3 years ago
  • 1.1.1                                ...           3 years ago
  • 1.1.0                                ...           3 years ago
  • 1.0.5                                ...           3 years ago
  • 1.0.4                                ...           3 years ago
  • 1.0.3                                ...           3 years ago
  • 1.0.2                                ...           3 years ago
  • 1.0.1                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
  • 0.24.0                                ...           3 years ago
  • 0.23.2                                ...           3 years ago
  • 0.22.0                                ...           3 years ago
  • 0.21.2                                ...           3 years ago
  • 0.21.1                                ...           3 years ago
  • 0.21.0                                ...           3 years ago
  • 0.20.2                                ...           3 years ago
  • 0.20.1                                ...           3 years ago
  • 0.20.0                                ...           3 years ago
  • 0.19.6                                ...           3 years ago
  • 0.19.5                                ...           3 years ago
  • 0.19.4                                ...           3 years ago
  • 0.19.3                                ...           3 years ago
  • 0.19.2                                ...           3 years ago
  • 0.19.1                                ...           3 years ago
  • 0.18.1                                ...           3 years ago
  • 0.18.0                                ...           3 years ago
  • 0.17.0                                ...           3 years ago
  • 0.16.4                                ...           3 years ago
  • 0.16.3                                ...           3 years ago
  • 0.16.2                                ...           3 years ago
  • 0.16.1                                ...           3 years ago
  • 0.16.0                                ...           3 years ago
  • 0.15.13                                ...           3 years ago
  • 0.15.12                                ...           3 years ago
  • 0.15.10                                ...           3 years ago
  • 0.15.9                                ...           3 years ago
  • 0.15.8                                ...           4 years ago
  • 0.15.7                                ...           4 years ago
  • 0.15.6                                ...           4 years ago
  • 0.15.5                                ...           4 years ago
  • 0.15.4                                ...           4 years ago
  • 0.15.3                                ...           4 years ago
  • 0.15.1                                ...           4 years ago
  • 0.14.0                                ...           4 years ago
  • 0.13.5                                ...           4 years ago
  • 0.13.2                                ...           4 years ago
  • 0.13.1                                ...           4 years ago
  • 0.12.8                                ...           4 years ago
  • 0.12.7                                ...           4 years ago
  • 0.12.6                                ...           4 years ago
  • 0.12.5                                ...           4 years ago
  • 0.12.3                                ...           4 years ago
  • 0.12.2                                ...           4 years ago
  • 0.12.1                                ...           4 years ago
  • 0.12.0                                ...           4 years ago
  • 0.11.1                                ...           4 years ago
  • 0.10.0                                ...           4 years ago
  • 0.9.2                                ...           4 years ago
  • 0.8.1                                ...           4 years ago
  • 0.8.0                                ...           4 years ago
  • 0.7.0                                ...           4 years ago
  • 0.6.2                                ...           4 years ago
  • 0.6.1                                ...           4 years ago
  • 0.6.0                                ...           4 years ago
  • 0.5.0                                ...           4 years ago
  • 0.4.2                                ...           4 years ago
  • 0.4.1                                ...           4 years ago
  • 0.3.0                                ...           4 years ago
  • 0.2.2                                ...           4 years ago
  • 0.2.1                                ...           4 years ago
Maintainers (1)
Downloads
Total 558
Today 0
This Week 0
This Month 249
Last Day 0
Last Week 150
Last Month 19
Dependencies (3)
Dev Dependencies (19)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE