{"name":"@asamuzakjp/dom-selector","version":"4.4.9","author":{"name":"asamuzaK"},"license":"MIT","_id":"@asamuzakjp/dom-selector@4.4.9","homepage":"https://github.com/asamuzaK/domSelector#readme","bugs":{"url":"https://github.com/asamuzaK/domSelector/issues"},"dist":{"shasum":"bab2cc808defa6d5918aa56904581d222428c1a8","tarball":"http://registry-npm.ywwl.com/@asamuzakjp/dom-selector/download/@asamuzakjp/dom-selector-4.4.9.tgz","fileCount":28,"integrity":"sha512-rhU5+kpePAcuD8SS7Ok6wbydImR2zFBPtSNkQ7a/QvhCJ+C03W8F+IFPCThztkP28Hz3YA8nN4HyRnhC8vnB4A==","signatures":[{"sig":"MEUCIQDWSVnP23Xh56MzS+haEDGjJd6l083ERXBbBYjQLPShOQIgSnDQN8tNF2S+RjLz8aexh8+eyYqOOysOeEvFT6N0UQQ=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":423064,"size":87496,"noattachment":false,"key":"/@asamuzakjp/dom-selector/-/@asamuzakjp/dom-selector-4.4.9.tgz"},"type":"module","types":"types/index.d.ts","exports":{"import":"./src/index.js","require":"./dist/cjs/index.js"},"gitHead":"e48fc53bf0aeb8dedb18378f08c82b0c13263167","scripts":{"tsc":"node scripts/index clean --dir=types -i && npx tsc","lint":"eslint --fix .","test":"c8 --reporter=text mocha --exit test/**/*.test.js","bench":"node benchmark/bench.js","build":"npm run tsc && npm run lint && npm test && npm run compat","compat":"esbuild --format=cjs --platform=node --outdir=dist/cjs/ --minify --sourcemap src/**/*.js","test-wpt":"node test/wpt/wpt-runner.js","update-wpt":"git submodule update --init --recursive --remote","bench-sizzle":"node benchmark/bench-sizzle.js"},"_npmUser":{"name":"asamuzakjp","email":"info@asamuzak.jp"},"repository":{"url":"git+https://github.com/asamuzaK/domSelector.git","type":"git"},"_npmVersion":"10.8.0","description":"A CSS selector engine.","directories":{},"_nodeVersion":"20.13.1","dependencies":{"bidi-js":"^1.0.3","css-tree":"^2.3.1","@asamuzakjp/nwsapi":"^2.2.5","is-potential-custom-element-name":"^1.0.1"},"_hasShrinkwrap":false,"devDependencies":{"c8":"^10.0.0","chai":"^5.1.1","jsdom":"^24.1.0","mocha":"^10.4.0","sinon":"^18.0.0","eslint":"^8.57.0","esbuild":"^0.21.5","linkedom":"^0.18.3","benchmark":"^2.1.4","commander":"^12.1.0","happy-dom":"^14.12.0","typescript":"^5.4.5","wpt-runner":"^5.0.0","@types/css-tree":"^2.3.7","eslint-plugin-jsdoc":"^48.2.9","eslint-plugin-import":"^2.29.1","eslint-plugin-regexp":"^2.6.0","eslint-plugin-unicorn":"^53.0.0","eslint-config-standard":"^17.1.0"},"_npmOperationalInternal":{"tmp":"tmp/dom-selector_4.4.9_1718068679482_0.6014087936276353","host":"s3://npm-registry-packages"},"readme":"# DOM Selector\n\n[![build](https://github.com/asamuzaK/domSelector/actions/workflows/node.js.yml/badge.svg)](https://github.com/asamuzaK/domSelector/actions/workflows/node.js.yml)\n[![CodeQL](https://github.com/asamuzaK/domSelector/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/asamuzaK/domSelector/actions/workflows/github-code-scanning/codeql)\n[![npm (scoped)](https://img.shields.io/npm/v/@asamuzakjp/dom-selector)](https://www.npmjs.com/package/@asamuzakjp/dom-selector)\n\nA CSS selector engine.\n\n## Install\n\n```console\nnpm i @asamuzakjp/dom-selector\n```\n\n## Usage\n\n```javascript\nimport { DOMSelector } from '@asamuzakjp/dom-selector';\nimport { JSDOM } from 'jsdom';\n\nconst { window } = new JSDOM();\nconst {\n  closest, matches, querySelector, querySelectorAll\n} = new DOMSelector(window);\n```\n\n<!-- Generated by documentation.js. Update this documentation by updating the source code. -->\n\n### matches(selector, node, opt)\n\nmatches - same functionality as [Element.matches()][64]\n\n#### Parameters\n\n- `selector` **[string][59]** CSS selector\n- `node` **[object][60]** Element node\n- `opt` **[object][60]?** options\n  - `opt.event` **[object][60]?** instance of MouseEvent, KeyboardEvent\n  - `opt.noexcept` **[boolean][61]?** no exception\n  - `opt.warn` **[boolean][61]?** console warn e.g. unsupported pseudo-class\n\nReturns **[boolean][61]** `true` if matched, `false` otherwise\n\n\n### closest(selector, node, opt)\n\nclosest - same functionality as [Element.closest()][65]\n\n#### Parameters\n\n- `selector` **[string][59]** CSS selector\n- `node` **[object][60]** Element node\n- `opt` **[object][60]?** options\n  - `opt.event` **[object][60]?** instance of MouseEvent, KeyboardEvent\n  - `opt.noexcept` **[boolean][61]?** no exception\n  - `opt.warn` **[boolean][61]?** console warn e.g. unsupported pseudo-class\n\nReturns **[object][60]?** matched node\n\n\n### querySelector(selector, node, opt)\n\nquerySelector - same functionality as [Document.querySelector()][66], [DocumentFragment.querySelector()][67], [Element.querySelector()][68]\n\n#### Parameters\n\n- `selector` **[string][59]** CSS selector\n- `node` **[object][60]** Document, DocumentFragment or Element node\n- `opt` **[object][60]?** options\n  - `opt.event` **[object][60]?** instance of MouseEvent, KeyboardEvent\n  - `opt.noexcept` **[boolean][61]?** no exception\n  - `opt.warn` **[boolean][61]?** console warn e.g. unsupported pseudo-class\n\nReturns **[object][60]?** matched node\n\n\n### querySelectorAll(selector, node, opt)\n\nquerySelectorAll - same functionality as [Document.querySelectorAll()][69], [DocumentFragment.querySelectorAll()][70], [Element.querySelectorAll()][71]  \n**NOTE**: returns Array, not NodeList\n\n#### Parameters\n\n- `selector` **[string][59]** CSS selector\n- `node` **[object][60]** Document, DocumentFragment or Element node\n- `opt` **[object][60]?** options\n  - `opt.event` **[object][60]?** instance of MouseEvent, KeyboardEvent\n  - `opt.noexcept` **[boolean][61]?** no exception\n  - `opt.warn` **[boolean][61]?** console warn e.g. unsupported pseudo-class\n\nReturns **[Array][62]&lt;([object][60] \\| [undefined][63])>** array of matched nodes\n\n\n## Supported CSS selectors\n\n|Pattern|Supported|Note|\n|:--------|:-------:|:--------|\n|\\*|✓| |\n|ns\\|E|✓| |\n|\\*\\|E|✓| |\n|\\|E|✓| |\n|E|✓| |\n|E:not(s1, s2, …)|✓| |\n|E:is(s1, s2, …)|✓| |\n|E:where(s1, s2, …)|✓| |\n|E:has(rs1, rs2, …)|✓| |\n|E.warning|✓| |\n|E#myid|✓| |\n|E\\[foo\\]|✓| |\n|E\\[foo=\"bar\"\\]|✓| |\n|E\\[foo=\"bar\"&nbsp;i\\]|✓| |\n|E\\[foo=\"bar\"&nbsp;s\\]|✓| |\n|E\\[foo~=\"bar\"\\]|✓| |\n|E\\[foo^=\"bar\"\\]|✓| |\n|E\\[foo$=\"bar\"\\]|✓| |\n|E\\[foo*=\"bar\"\\]|✓| |\n|E\\[foo\\|=\"en\"\\]|✓| |\n|E:defined|Partially supported|Matching with MathML is not yet supported.|\n|E:dir(ltr)|✓| |\n|E:lang(en)|Partially supported|Comma-separated list of language codes, e.g. `:lang(en, fr)`, is not yet supported.|\n|E:any&#8209;link|✓| |\n|E:link|✓| |\n|E:visited|✓|Returns `false` or `null` to prevent fingerprinting.|\n|E:local&#8209;link|✓| |\n|E:target|✓| |\n|E:target&#8209;within|✓| |\n|E:scope|✓| |\n|E:current|Unsupported| |\n|E:current(s)|Unsupported| |\n|E:past|Unsupported| |\n|E:future|Unsupported| |\n|E:active|✓|Enabled if a `mousedown` / `pointerdown` event is passed as an option.|\n|E:hover|✓|Enabled if a `mouseover` / `pointerover` event is passed as an option.|\n|E:focus|✓| |\n|E:focus&#8209;within|✓| |\n|E:focus&#8209;visible|✓|Enabled if a `keydown` event is passed as an option.|\n|E:open<br>E:closed|Partially supported|Matching with &lt;select&gt;, e.g. `select:open`, is not supported.|\n|E:enabled<br>E:disabled|✓| |\n|E:read&#8209;write<br>E:read&#8209;only|✓| |\n|E:placeholder&#8209;shown|✓| |\n|E:default|✓| |\n|E:checked|✓| |\n|E:indeterminate|✓| |\n|E:valid<br>E:invalid|✓| |\n|E:required<br>E:optional|✓| |\n|E:blank|Unsupported| |\n|E:user&#8209;valid<br>E:user&#8209;invalid|Unsupported| |\n|E:root|✓| |\n|E:empty|✓| |\n|E:nth&#8209;child(n&nbsp;[of&nbsp;S]?)|✓| |\n|E:nth&#8209;last&#8209;child(n&nbsp;[of&nbsp;S]?)|✓| |\n|E:first&#8209;child|✓| |\n|E:last&#8209;child|✓| |\n|E:only&#8209;child|✓| |\n|E:nth&#8209;of&#8209;type(n)|✓| |\n|E:nth&#8209;last&#8209;of&#8209;type(n)|✓| |\n|E:first&#8209;of&#8209;type|✓| |\n|E:last&#8209;of&#8209;type|✓| |\n|E:only&#8209;of&#8209;type|✓| |\n|E&nbsp;F|✓| |\n|E > F|✓| |\n|E + F|✓| |\n|E ~ F|✓| |\n|F \\|\\| E|Unsupported| |\n|E:nth&#8209;col(n)|Unsupported| |\n|E:nth&#8209;last&#8209;col(n)|Unsupported| |\n|E:popover-open|✓| |\n|E:host|✓| |\n|E:host(s)|✓| |\n|E:host&#8209;context(s)|✓| |\n|E:state(v)|✓|*1|\n|E:host(:state(v))|✓|*1|\n\n*1: `ElementInternals.states`, i.e. `CustomStateSet`, is not implemented in jsdom, so you need to patch yourself.\n\n``` javascript\nclass LabeledCheckbox extends window.HTMLElement {\n  #internals;\n  constructor() {\n    super();\n    this.#internals = this.attachInternals();\n    // patch CustomStateSet\n    if (!this.#internals.states) {\n      this.#internals.states = new Set();\n    }\n    this.addEventListener('click', this._onClick.bind(this));\n  }\n  get checked() {\n    return this.#internals.states.has('checked');\n  }\n  set checked(flag) {\n    if (flag) {\n      this.#internals.states.add('checked');\n    } else {\n      this.#internals.states.delete('checked');\n    }\n  }\n  _onClick(event) {\n    this.checked = !this.checked;\n  }\n}\n```\n\n\n## Monkey patch jsdom\n\n``` javascript\nimport { DOMSelector } from '@asamuzakjp/dom-selector';\nimport { JSDOM } from 'jsdom';\n\nconst dom = new JSDOM('', {\n  runScripts: 'dangerously',\n  url: 'http://localhost/',\n  beforeParse: window => {\n    const domSelector = new DOMSelector(window);\n\n    const matches = domSelector.matches.bind(domSelector);\n    window.Element.prototype.matches = function (...args) {\n      if (!args.length) {\n        throw new window.TypeError('1 argument required, but only 0 present.');\n      }\n      const [selector] = args;\n      return matches(selector, this);\n    };\n\n    const closest = domSelector.closest.bind(domSelector);\n    window.Element.prototype.closest = function (...args) {\n      if (!args.length) {\n        throw new window.TypeError('1 argument required, but only 0 present.');\n      }\n      const [selector] = args;\n      return closest(selector, this);\n    };\n\n    const querySelector = domSelector.querySelector.bind(domSelector);\n    window.Document.prototype.querySelector = function (...args) {\n      if (!args.length) {\n        throw new window.TypeError('1 argument required, but only 0 present.');\n      }\n      const [selector] = args;\n      return querySelector(selector, this);\n    };\n    window.DocumentFragment.prototype.querySelector = function (...args) {\n      if (!args.length) {\n        throw new window.TypeError('1 argument required, but only 0 present.');\n      }\n      const [selector] = args;\n      return querySelector(selector, this);\n    };\n    window.Element.prototype.querySelector = function (...args) {\n      if (!args.length) {\n        throw new window.TypeError('1 argument required, but only 0 present.');\n      }\n      const [selector] = args;\n      return querySelector(selector, this);\n    };\n\n    const querySelectorAll = domSelector.querySelectorAll.bind(domSelector);\n    window.Document.prototype.querySelectorAll = function (...args) {\n      if (!args.length) {\n        throw new window.TypeError('1 argument required, but only 0 present.');\n      }\n      const [selector] = args;\n      return querySelectorAll(selector, this);\n    };\n    window.DocumentFragment.prototype.querySelectorAll = function (...args) {\n      if (!args.length) {\n        throw new window.TypeError('1 argument required, but only 0 present.');\n      }\n      const [selector] = args;\n      return querySelectorAll(selector, this);\n    };\n    window.Element.prototype.querySelectorAll = function (...args) {\n      if (!args.length) {\n        throw new window.TypeError('1 argument required, but only 0 present.');\n      }\n      const [selector] = args;\n      return querySelectorAll(selector, this);\n    };\n  }\n});\n```\n\n\n## Performance\n\nSee [benchmark](https://github.com/asamuzaK/domSelector/actions/workflows/benchmark.yml) for the latest results.\n\n`F`: Failed because the selector is not supported or the result was incorrect.\n\n### matches()\n\n|Selector|jsdom v24.1.0 (nwsapi)|happy-dom|linkeDom|patched-jsdom (dom-selector)|Result|\n|:-----------|:-----------|:-----------|:-----------|:-----------|:-----------|\n|simple selector:<br>`matches('.content')`|955,191 ops/sec ±1.19%|7,301 ops/sec ±0.75%|9,322 ops/sec ±0.65%|819,169 ops/sec ±0.43%|jsdom is the fastest and 1.2 times faster than patched-jsdom.|\n|compound selector:<br>`matches('p.content[id]:is(:last-child, :only-child)')`|581,444 ops/sec ±1.46%|7,152 ops/sec ±0.70%|8,836 ops/sec ±1.32%|402,486 ops/sec ±0.77%|jsdom is the fastest and 1.4 times faster than patched-jsdom.|\n|compound selector:<br>`matches('p.content[id]:is(:invalid-nth-child, :only-child)')`|F|7,124 ops/sec ±0.90%|F|129,958 ops/sec ±0.22%|patched-jsdom is the fastest.|\n|compound selector:<br>`matches('p.content[id]:not(:is(.foo, .bar))')`|469,482 ops/sec ±1.48%|7,179 ops/sec ±0.74%|8,851 ops/sec ±0.71%|343,674 ops/sec ±0.48%|jsdom is the fastest and 1.4 times faster than patched-jsdom.|\n|complex selector:<br>`matches('.box:first-child ~ .box:nth-of-type(4n+1) + .box[id] .block.inner > .content')`|152,787 ops/sec ±0.23%|F|5,806 ops/sec ±0.82%|131,245 ops/sec ±0.28%|jsdom is the fastest and 1.2 times faster than patched-jsdom.|\n|complex selector:<br>`matches('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner:has(> .content)')`|F|F|5,610 ops/sec ±0.57%|9,285 ops/sec ±0.60%|patched-jsdom is the fastest.|\n|complex selector within logical pseudo-class:<br>`matches(':is(.box > .content, .block > .content)')`|407,178 ops/sec ±0.44%|F|6,039 ops/sec ±0.33%|332,047 ops/sec ±0.36%|jsdom is the fastest and 1.2 times faster than patched-jsdom.|\n\n### closest()\n\n|Selector|jsdom v24.1.0 (nwsapi)|happy-dom|linkeDom|patched-jsdom (dom-selector)|Result|\n|:-----------|:-----------|:-----------|:-----------|:-----------|:-----------|\n|simple selector:<br>`closest('.container')`|367,267 ops/sec ±0.24%|7,283 ops/sec ±0.67%|9,298 ops/sec ±0.69%|338,618 ops/sec ±1.73%|jsdom is the fastest and 1.1 times faster than patched-jsdom.|\n|compound selector:<br>`closest('div.container[id]:not(.foo, .box)')`|135,537 ops/sec ±0.34%|F|8,413 ops/sec ±1.50%|120,204 ops/sec ±1.90%|jsdom is the fastest and 1.1 times faster than patched-jsdom.|\n|complex selector:<br>`closest('.box:first-child ~ .box:nth-of-type(4n+1) + .box[id] .block.inner > .content')`|139,190 ops/sec ±1.50%|F|5,821 ops/sec ±0.64%|119,681 ops/sec ±1.44%|jsdom is the fastest and 1.2 times faster than patched-jsdom.|\n|complex selector:<br>`closest('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner:has(> .content)')`|F|F|5,695 ops/sec ±0.58%|7,659 ops/sec ±1.85%|patched-jsdom is the fastest.|\n|complex selector within logical pseudo-class:<br>`closest(':is(.container > .content, .container > .box)')`|198,275 ops/sec ±0.59%|4,568 ops/sec ±0.73%|5,922 ops/sec ±0.50%|175,254 ops/sec ±0.27%|jsdom is the fastest and 1.1 times faster than patched-jsdom.|\n\n### querySelector()\n\n|Selector|jsdom v24.1.0 (nwsapi)|happy-dom|linkeDom|patched-jsdom (dom-selector)|Result|\n|:-----------|:-----------|:-----------|:-----------|:-----------|:-----------|\n|simple selector:<br>`querySelector('.content')`|27,778 ops/sec ±1.10%|8,861 ops/sec ±0.73%|11,318 ops/sec ±0.66%|29,314 ops/sec ±1.45%|patched-jsdom is the fastest. patched-jsdom is 1.1 times faster than jsdom.|\n|compound selector:<br>`querySelector('p.content[id]:is(:last-child, :only-child)')`|9,731 ops/sec ±1.41%|8,732 ops/sec ±1.50%|9,954 ops/sec ±0.57%|9,492 ops/sec ±1.59%|linkedom is the fastest and 1.0 times faster than patched-jsdom. jsdom is 1.0 times faster than patched-jsdom.|\n|complex selector:<br>`querySelector('.box:first-child ~ .box:nth-of-type(4n+1) + .box[id] .block.inner > .content')`|221 ops/sec ±1.70%|F|1,273 ops/sec ±1.13%|272 ops/sec ±1.28%|linkedom is the fastest and 4.7 times faster than patched-jsdom. patched-jsdom is 1.2 times faster than jsdom.|\n|complex selector:<br>`querySelector('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner:has(> .content)')`|F|F|1,594 ops/sec ±1.48%|487 ops/sec ±2.03%|linkedom is the fastest and 3.3 times faster than patched-jsdom.|\n|complex selector within logical pseudo-class:<br>`querySelector(':is(.box > .content, .block > .content)')`|3,108 ops/sec ±2.01%|F|9,977 ops/sec ±0.99%|90,725 ops/sec ±1.26%|patched-jsdom is the fastest. patched-jsdom is 29.2 times faster than jsdom.|\n\n### querySelectorAll()\n\n|Selector|jsdom v24.1.0 (nwsapi)|happy-dom|linkeDom|patched-jsdom (dom-selector)|Result|\n|:-----------|:-----------|:-----------|:-----------|:-----------|:-----------|\n|simple selector:<br>`querySelectorAll('.content')`|2,610 ops/sec ±0.33%|804 ops/sec ±0.47%|1,190 ops/sec ±1.78%|3,190 ops/sec ±0.91%|patched-jsdom is the fastest. patched-jsdom is 1.2 times faster than jsdom.|\n|compound selector:<br>`querySelectorAll('p.content[id]:is(:last-child, :only-child)')`|928 ops/sec ±1.31%|753 ops/sec ±0.25%|1,201 ops/sec ±0.18%|1,041 ops/sec ±0.30%|linkedom is the fastest and 1.2 times faster than patched-jsdom. patched-jsdom is 1.1 times faster than jsdom.|\n|complex selector:<br>`querySelectorAll('.box:first-child ~ .box:nth-of-type(4n+1) + .box[id] .block.inner > .content')`|227 ops/sec ±1.32%|F|425 ops/sec ±1.37%|270 ops/sec ±1.39%|linkedom is the fastest and 1.6 times faster than patched-jsdom. patched-jsdom is 1.2 times faster than jsdom.|\n|complex selector:<br>`querySelectorAll('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner:has(> .content)')`|F|F|451 ops/sec ±1.73%|535 ops/sec ±2.00%|patched-jsdom is the fastest.|\n|complex selector within logical pseudo-class:<br>`querySelectorAll(':is(.box > .content, .block > .content)')`|302 ops/sec ±0.67%|F|520 ops/sec ±0.32%|254 ops/sec ±1.65%|linkedom is the fastest and 2.0 times faster than patched-jsdom. jsdom is 1.2 times faster than patched-jsdom.|\n\n\n## Acknowledgments\n\nThe following resources have been of great help in the development of the DOM Selector.\n\n- [CSSTree](https://github.com/csstree/csstree)\n- [selery](https://github.com/danburzo/selery)\n- [jsdom](https://github.com/jsdom/jsdom)\n- [nwsapi](https://github.com/dperini/nwsapi)\n\n---\nCopyright (c) 2023 [asamuzaK (Kazz)](https://github.com/asamuzaK/)\n\n\n[1]: #matches\n[2]: #parameters\n[3]: #closest\n[4]: #parameters-1\n[5]: #queryselector\n[6]: #parameters-2\n[7]: #queryselectorall\n[8]: #parameters-3\n[59]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String\n[60]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object\n[61]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean\n[62]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array\n[63]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined\n[64]: https://developer.mozilla.org/docs/Web/API/Element/matches\n[65]: https://developer.mozilla.org/docs/Web/API/Element/closest\n[66]: https://developer.mozilla.org/docs/Web/API/Document/querySelector\n[67]: https://developer.mozilla.org/docs/Web/API/DocumentFragment/querySelector\n[68]: https://developer.mozilla.org/docs/Web/API/Element/querySelector\n[69]: https://developer.mozilla.org/docs/Web/API/Document/querySelectorAll\n[70]: https://developer.mozilla.org/docs/Web/API/DocumentFragment/querySelectorAll\n[71]: https://developer.mozilla.org/docs/Web/API/Element/querySelectorAll\n","publish_time":1718068679701,"maintainers":[{"name":"asamuzakjp","email":"info@asamuzak.jp"}],"_cnpm_publish_time":1718068679701,"dist-tags":{"beta":"0.2.2","dev":"6.5.0-a.16","latest":"10.0.5","next":"8.0.0-a.2"}}