{"name":"@asamuzakjp/dom-selector","version":"0.16.3","author":{"name":"asamuzaK"},"license":"MIT","_id":"@asamuzakjp/dom-selector@0.16.3","maintainers":[{"name":"asamuzakjp","email":"info@asamuzak.jp"}],"homepage":"https://github.com/asamuzaK/domSelector#readme","bugs":{"url":"https://github.com/asamuzaK/domSelector/issues"},"dist":{"shasum":"7aeaaa21d1a8e3dd4e2b77c41a95af6b0a67fcb9","tarball":"http://registry-npm.ywwl.com/@asamuzakjp/dom-selector/download/@asamuzakjp/dom-selector-0.16.3.tgz","fileCount":13,"integrity":"sha512-cEl0TezXFdrfOVUaxjZAdobG3COa20vYIXL8oFS/jLEJbX/bDpy7oPUH7w0iu0N4xWwaIvXHy6qzDLdKZhwctg==","signatures":[{"sig":"MEUCIAXsfGyjLwMRLkqPleqwRe3g+i5Xqb6A9hkhBMNc9qEKAiEA1YkJlvy01DlcXCOY9++36B7uCrnZBz8d9yrjG0C2VpY=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":85079,"size":16985,"noattachment":false,"key":"/@asamuzakjp/dom-selector/-/@asamuzakjp/dom-selector-0.16.3.tgz"},"main":"src/index.js","types":"types/index.d.ts","gitHead":"dd7646deb9925921204df31e02416b76ae04be35","scripts":{"tsc":"npx tsc","lint":"eslint --fix .","test":"c8 --reporter=text mocha --exit test/**/*.test.js","bench":"node benchmark/index.benchmark.js","build":"npm run tsc && npm run lint && npm test"},"_npmUser":{"name":"asamuzakjp","email":"info@asamuzak.jp"},"repository":{"url":"git+https://github.com/asamuzaK/domSelector.git","type":"git"},"_npmVersion":"9.6.7","description":"Retrieve DOM node from the given CSS selector.","directories":{},"_nodeVersion":"18.16.0","dependencies":{"css-tree":"^2.3.1","domexception":"^4.0.0","is-potential-custom-element-name":"^1.0.1"},"_hasShrinkwrap":false,"devDependencies":{"c8":"^7.14.0","chai":"^4.3.7","jsdom":"^22.1.0","mocha":"^10.2.0","sinon":"^15.1.0","eslint":"^8.42.0","benchmark":"^2.1.4","typescript":"^5.1.3","@types/css-tree":"^2.3.1","eslint-plugin-jsdoc":"^46.2.6","eslint-plugin-regexp":"^1.15.0","eslint-plugin-unicorn":"^47.0.0","eslint-config-standard":"^17.1.0"},"_npmOperationalInternal":{"tmp":"tmp/dom-selector_0.16.3_1686466320270_0.8135082178339033","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\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## 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 is 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')`|989,361 ops/sec ±0.27%|7,447 ops/sec ±1.10%|9,037 ops/sec ±1.74%|947,094 ops/sec ±0.24%|jsdom is the fastest and 1.0 times faster than patched-jsdom.|\n|compound selector:<br>`matches('p.content[id]:is(:last-child, :only-child)')`|581,187 ops/sec ±0.21%|7,137 ops/sec ±0.82%|8,808 ops/sec ±1.24%|492,756 ops/sec ±0.25%|jsdom is the fastest and 1.2 times faster than patched-jsdom.|\n|compound selector:<br>`matches('p.content[id]:is(:invalid-nth-child, :only-child)')`|F|7,144 ops/sec ±0.51%|F|145,203 ops/sec ±0.94%|patched-jsdom is the fastest.|\n|compound selector:<br>`matches('p.content[id]:not(:is(.foo, .bar))')`|472,261 ops/sec ±1.64%|7,199 ops/sec ±0.58%|8,612 ops/sec ±0.51%|403,920 ops/sec ±0.29%|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[id] .block.inner > .content')`|152,011 ops/sec ±0.66%|F|5,737 ops/sec ±0.43%|135,540 ops/sec ±0.41%|jsdom is the fastest and 1.1 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,767 ops/sec ±0.47%|39,996 ops/sec ±0.79%|patched-jsdom is the fastest.|\n|complex selector within logical pseudo-class:<br>`matches(':is(.box > .content, .block > .content)')`|416,215 ops/sec ±0.49%|F|6,105 ops/sec ±0.29%|358,945 ops/sec ±0.29%|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,877 ops/sec ±1.14%|7,218 ops/sec ±0.76%|9,181 ops/sec ±0.64%|356,762 ops/sec ±1.02%|jsdom is the fastest and 1.0 times faster than patched-jsdom.|\n|compound selector:<br>`closest('div.container[id]:not(.foo, .box)')`|134,650 ops/sec ±0.32%|F|8,549 ops/sec ±0.62%|125,888 ops/sec ±3.16%|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')`|134,654 ops/sec ±2.02%|F|5,751 ops/sec ±1.01%|119,709 ops/sec ±2.00%|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 .block.inner:has(> .content)')`|F|F|5,596 ops/sec ±0.81%|28,732 ops/sec ±1.13%|patched-jsdom is the fastest.|\n|complex selector within logical pseudo-class:<br>`closest(':is(.container > .content, .container > .box)')`|192,885 ops/sec ±1.15%|4,685 ops/sec ±1.30%|5,927 ops/sec ±0.64%|181,927 ops/sec ±1.39%|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,400 ops/sec ±0.96%|9,042 ops/sec ±1.19%|10,550 ops/sec ±0.69%|25,885 ops/sec ±1.48%|jsdom is the fastest and 1.1 times faster than patched-jsdom.|\n|compound selector:<br>`querySelector('p.content[id]:is(:last-child, :only-child)')`|8,685 ops/sec ±1.47%|8,723 ops/sec ±1.20%|9,836 ops/sec ±0.69%|8,418 ops/sec ±1.41%|linkedom is the fastest and 1.2 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')`|206 ops/sec ±1.48%|F|1,289 ops/sec ±0.42%|709 ops/sec ±1.75%|linkedom is the fastest and 1.8 times faster than patched-jsdom. patched-jsdom is 3.4 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,598 ops/sec ±0.35%|476 ops/sec ±1.94%|linkedom is the fastest and 3.4 times faster than patched-jsdom.|\n|complex selector within logical pseudo-class:<br>`querySelector(':is(.box > .content, .block > .content)')`|2,921 ops/sec ±1.78%|F|9,782 ops/sec ±0.52%|97,513 ops/sec ±1.46%|patched-jsdom is the fastest. patched-jsdom is 33.4 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,549 ops/sec ±0.55%|729 ops/sec ±0.48%|1,188 ops/sec ±1.57%|3,153 ops/sec ±0.95%|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)')`|880 ops/sec ±1.32%|706 ops/sec ±1.55%|1,156 ops/sec ±0.93%|938 ops/sec ±1.27%|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')`|196 ops/sec ±0.73%|F|414 ops/sec ±0.24%|797 ops/sec ±1.66%|patched-jsdom is the fastest. patched-jsdom is 4.1 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 ±0.36%|504 ops/sec ±1.88%|patched-jsdom is the fastest.|\n|complex selector within logical pseudo-class:<br>`querySelectorAll(':is(.box > .content, .block > .content)')`|273 ops/sec ±0.89%|F|498 ops/sec ±1.38%|236 ops/sec ±2.36%|linkedom is the fastest and 2.1 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":1686466320459,"_cnpm_publish_time":1686466320459,"dist-tags":{"beta":"0.2.2","dev":"6.5.0-a.16","latest":"10.0.5","next":"8.0.0-a.2"}}