{"name":"@asamuzakjp/dom-selector","version":"8.2.1","author":{"name":"asamuzaK"},"license":"MIT","_id":"@asamuzakjp/dom-selector@8.2.1","maintainers":[{"name":"asamuzakjp","email":"info@asamuzak.jp"}],"homepage":"https://github.com/asamuzaK/domSelector#readme","bugs":{"url":"https://github.com/asamuzaK/domSelector/issues"},"dist":{"shasum":"ae3b351492b2a1087241606390455ad2981f01a0","tarball":"http://registry-npm.ywwl.com/@asamuzakjp/dom-selector/download/@asamuzakjp/dom-selector-8.2.1.tgz","fileCount":21,"integrity":"sha512-3i/aneXRqdZtgjI91nH4b7iQXNVLAk6RJYnWUZJpE3/mdcJw4wTudXTa4DnC7Mz6/feqYxZLJwWERLk2DoHPRA==","signatures":[{"sig":"MEUCIDqtRHJCCIGg1fyyC6TZbGggDoSH+QNgqT5cGh+pUJ/dAiEAhP1n0xg9pkElvi9G5gKK3rXVnBRl0hiVGDIfWRmJnSs=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":261953,"size":56342,"noattachment":false,"key":"/@asamuzakjp/dom-selector/-/@asamuzakjp/dom-selector-8.2.1.tgz"},"type":"module","types":"./types/index.d.ts","engines":{"node":"^22.13.0 || >=24.0.0"},"exports":{".":{"types":"./types/index.d.ts","default":"./src/index.js"},"./package.json":"./package.json"},"gitHead":"f55d4c2d9a2c4f6aafedf87ecb3d6c3f4ce87f30","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 --expose-gc benchmark/bench.js","build":"npm run tsc && npm run lint && npm test","bench:tl":"node --expose-gc benchmark/bench-testing-library.js","test:wpt":"node test/wpt/wpt-runner.js","update:wpt":"git submodule update --init --recursive --remote","bench:cache":"node --expose-gc benchmark/bench-cache.js","bench:nwsapi":"node --expose-gc benchmark/bench-nwsapi.js","bench:ps-has":"node --expose-gc benchmark/bench-has.js","bench:ps-nth":"node --expose-gc benchmark/bench-nth-child.js && node --expose-gc benchmark/bench-nth-of-type.js","bench:ps-form":"node --expose-gc benchmark/bench-default.js && node --expose-gc benchmark/bench-indeterminate.js && node --expose-gc benchmark/bench-valid-invalid.js","bench:universal":"node --expose-gc benchmark/bench-universal.js","bench:ps-traverse":"node --expose-gc benchmark/bench-dir.js && node --expose-gc benchmark/bench-lang.js"},"_npmUser":{"name":"asamuzakjp","email":"info@asamuzak.jp"},"overrides":{"c8":{"yargs":"^18.0.0"},"jsdom":"$jsdom","mocha":{"diff":"^8.0.3","js-yaml":"^4.1.2"},"eslint":{"js-yaml":"^4.1.2","brace-expansion":"^1.1.13"},"serialize-javascript":"^7.0.4"},"repository":{"url":"git+https://github.com/asamuzaK/domSelector.git","type":"git"},"_npmVersion":"11.18.0","description":"A CSS selector engine.","directories":{},"_nodeVersion":"24.18.0","dependencies":{"bidi-js":"^1.0.3","css-tree":"^3.2.1","lru-cache":"^11.5.2","is-potential-custom-element-name":"^1.0.1"},"_hasShrinkwrap":false,"devDependencies":{"c8":"^11.0.0","chai":"^6.2.2","jsdom":"^29.1.1","mocha":"^11.7.6","sinon":"^22.0.0","eslint":"^9.39.5","mitata":"^1.0.34","globals":"^17.7.0","prettier":"^3.9.5","commander":"^15.0.0","tinybench":"^6.0.2","typescript":"^6.0.3","wpt-runner":"^7.0.0","@types/node":"^26.1.1","neostandard":"^0.13.0","@types/css-tree":"^2.3.11","eslint-plugin-jsdoc":"^63.0.13","eslint-plugin-regexp":"^3.1.1","eslint-plugin-unicorn":"^65.0.1","eslint-config-prettier":"^10.1.8","eslint-plugin-prettier":"^5.5.6"},"_npmOperationalInternal":{"tmp":"tmp/dom-selector_8.2.1_1783833659313_0.5176424277554994","host":"s3://npm-registry-packages-npm-production"},"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 built for strict specification compliance.\n\n## Features\n\n* **Strict Specification Compliance**: Strictly adheres to modern web standards. It accurately parses, evaluates, and extracts elements across complex combinations of pseudo-classes and HTML attributes. Features comprehensive support for CSS Selectors Level 4 (e.g., `:is()`, `:not()`, `:where()`, `:has()`) and Shadow DOM pseudo-classes (`:host`, `:host-context`).\n* **Utility Functions**: Provides utility methods alongside standard querying, such as `check()` for AST evaluation and `extractSubjects()` for extracting subject keys from selectors.\n* **jsdom's Default Engine**: Adopted as the CSS selector engine for [jsdom](https://github.com/jsdom/jsdom).\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();\n\n// Destructuring methods (all methods are bound to the instance)\nconst {\n  check, closest, extractSubjects, matches, querySelector, querySelectorAll, supports\n} = new DOMSelector(window);\n```\n\n## API\n\n### `new DOMSelector(window, document?, opt?)`\n\nCreates an instance of the DOMSelector.\n\n* `window` **{Window}** The window object.\n* `document` **{Document}?** The document object. Defaults to window.document.\n* `opt` **{object}?** Options:\n  * `opt.cacheSize` **{number}?** Maximum number of items to store in the internal cache. Default is 2048.\n\n### `matches(selector, node, opt?)`\n\nEquivalent to [Element.matches()](https://developer.mozilla.org/docs/Web/API/Element/matches).\n\n* `selector` **{string}** CSS selector.\n* `node` **{Element}** Element node.\n* `opt` **{object}?** Options:\n  * `opt.noexcept` **{boolean}?** Do not throw exceptions.\n  * `opt.warn` **{boolean}?** Console warn (e.g. unsupported pseudo-class).\n* **Returns** **{boolean}** `true` if matched, `false` otherwise.\n\n### `closest(selector, node, opt?)`\n\nEquivalent to [Element.closest()](https://developer.mozilla.org/docs/Web/API/Element/closest).\n\n* `selector` **{string}** CSS selector.\n* `node` **{Element}** Element node.\n* `opt` **{object}?** Options:\n  * `opt.noexcept` **{boolean}?** Do not throw exceptions.\n  * `opt.warn` **{boolean}?** Console warn (e.g. unsupported pseudo-class).\n* **Returns** **{Element | null}** The matched ancestor node or `null`.\n\n### `querySelector(selector, node, opt?)`\n\nEquivalent to [Document.querySelector()](https://developer.mozilla.org/docs/Web/API/Document/querySelector), [DocumentFragment.querySelector()](https://developer.mozilla.org/docs/Web/API/DocumentFragment/querySelector) and [Element.querySelector()](https://developer.mozilla.org/docs/Web/API/Element/querySelector).\n\n* `selector` **{string}** CSS selector.\n* `node` **{Document | DocumentFragment | Element}** Node to find within.\n* `opt` **{object}?** Options:\n  * `opt.noexcept` **{boolean}?** Do not throw exceptions.\n  * `opt.warn` **{boolean}?** Console warn (e.g. unsupported pseudo-class).\n* **Returns** **{Element | null}** The matched node or `null`.\n\n### `querySelectorAll(selector, node, opt?)`\n\nEquivalent to [Document.querySelectorAll()](https://developer.mozilla.org/docs/Web/API/Document/querySelectorAll), [DocumentFragment.querySelectorAll()](https://developer.mozilla.org/docs/Web/API/DocumentFragment/querySelectorAll) and [Element.querySelectorAll()](https://developer.mozilla.org/docs/Web/API/Element/querySelectorAll).\n**NOTE**: Returns a standard `Array`, not a `NodeList`.\n\n* `selector` **{string}** CSS selector.\n* `node` **{Document | DocumentFragment | Element}** Node to find within.\n* `opt` **{object}?** Options:\n  * `opt.noexcept` **{boolean}?** Do not throw exceptions.\n  * `opt.warn` **{boolean}?** Console warn (e.g. unsupported pseudo-class).\n* **Returns** **{Array}** Array of matched nodes.\n\n### `check(selector, node, opt?)`\n\nChecks if an element matches a CSS selector and returns additional abstract syntax tree (AST) information.\n**NOTE**: Any pseudo-elements in the selector are excluded from the matching evaluation.\n\n* `selector` **{string}** CSS selector.\n* `node` **{Element}** Element node.\n* `opt` **{object}?** Options:\n  * `opt.noexcept` **{boolean}?** Do not throw exceptions.\n  * `opt.warn` **{boolean}?** Console warn (e.g. unsupported pseudo-class).\n* **Returns** **{object}** An object containing the following properties:\n  * `match` **{boolean}** `true` if the element matches the selector, `false` otherwise.\n  * `pseudoElement` **{string | null}** The pseudo-element extracted from the selector, if any.\n  * `ast` **{object | null}** The parsed AST object.\n\n### `extractSubjects(selector, caseSensitive?)`\n\nParses a selector and extracts the rightmost subject keys (Id, Class, Tag).\n\n* `selector` **{string}** CSS selector.\n* `caseSensitive` **{boolean}?** `true` if the tag key should be case sensitive. Defaults to `false`.\n* **Returns** **{Array\\<{id: string|null, className: string|null, tag: string|null}\\>}** An array of extracted keys.\n\n### `supports(selector)`\n\nChecks if the given CSS selector is supported by this engine.\nSee the table below for the full list of supported selectors.\n\n* `selector` **{string}** CSS selector.\n* **Returns** **{boolean}** `true` if the selector is supported, `false` otherwise.\n\n### `clear(clearAll?)`\n\nClears the internal caches to free up memory.\n\n* `clearAll` **{boolean}?** Whether to clear all caches. If false, only cached matching results are cleared. Defaults to `false`.\n* **Returns** **{void}**\n\n## Supported CSS selectors\n\n| Pattern | Supported | Note |\n| :--- | :---: | :--- |\n| `*` | ✓ | |\n| `E` | ✓ | |\n| <code>ns\\|E</code> | ✓ | |\n| <code>*\\|E</code> | ✓ | |\n| <code>\\|E</code> | ✓ | |\n| `E F` | ✓ | |\n| `E > F` | ✓ | |\n| `E + F` | ✓ | |\n| `E ~ F` | ✓ | |\n| <code>F \\|\\| E</code> | Unsupported | |\n| `E.warning` | ✓ | |\n| `E#myid` | ✓ | |\n| `E[foo]` | ✓ | |\n| `E[foo=\"bar\"]` | ✓ | |\n| `E[foo=\"bar\" i]` | ✓ | |\n| `E[foo=\"bar\" s]` | ✓ | |\n| `E[foo~=\"bar\"]` | ✓ | |\n| `E[foo^=\"bar\"]` | ✓ | |\n| `E[foo$=\"bar\"]` | ✓ | |\n| `E[foo*=\"bar\"]` | ✓ | |\n| <code>E[foo\\|=\"en\"]</code> | ✓ | |\n| `E:is(s1, s2, …)` | ✓ | |\n| `E:not(s1, s2, …)` | ✓ | |\n| `E:where(s1, s2, …)` | ✓ | |\n| `E:has(rs1, rs2, …)` | ✓ | |\n| `E:defined` | Partially supported | Matching with MathML is not yet supported. |\n| `E:dir(ltr)` | ✓ | |\n| `E:lang(en)` | ✓ | |\n| `E:any-link` | ✓ | |\n| `E:link` | ✓ | |\n| `E:visited` | ✓ | Returns `false` or `null` to prevent fingerprinting. |\n| `E:local-link` | ✓ | |\n| `E:target` | ✓ | |\n| `E:target-within` | | Removed from spec. |\n| `E:scope` | ✓ | |\n| `E:hover` | ✓ | |\n| `E:active` | ✓ | |\n| `E:focus` | ✓ | |\n| `E:focus-visible` | ✓ | |\n| `E:focus-within` | ✓ | |\n| `E:current` | Unsupported | |\n| `E:current(s)` | Unsupported | |\n| `E:past` | Unsupported | |\n| `E:future` | Unsupported | |\n| `E:open` | Partially supported | Matching with `<select>`, e.g. `select:open`, or `<input type=\"color\">` are not supported. |\n| `E:popover-open` | Unsupported | |\n| `E:enabled`<br>`E:disabled` | ✓ | |\n| `E:read-write`<br>`E:read-only` | ✓ | |\n| `E:placeholder-shown` | ✓ | |\n| `E:default` | ✓ | |\n| `E:checked` | ✓ | |\n| `E:indeterminate` | ✓ | |\n| `E:blank` | Unsupported | |\n| `E:valid`<br>`E:invalid` | ✓ | |\n| `E:in-range`<br>`E:out-of-range` | ✓ | |\n| `E:required`<br>`E:optional` | ✓ | |\n| `E:user-valid`<br>`E:user-invalid` | Unsupported | |\n| `E:root` | ✓ | |\n| `E:empty` | ✓ | |\n| `E:nth-child(n [of S]?)` | ✓ | |\n| `E:nth-last-child(n [of S]?)` | ✓ | |\n| `E:first-child` | ✓ | |\n| `E:last-child` | ✓ | |\n| `E:only-child` | ✓ | |\n| `E:nth-of-type(n)` | ✓ | |\n| `E:nth-last-of-type(n)` | ✓ | |\n| `E:first-of-type` | ✓ | |\n| `E:last-of-type` | ✓ | |\n| `E:only-of-type` | ✓ | |\n| `E:nth-col(n)` | Unsupported | |\n| `E:nth-last-col(n)` | Unsupported | |\n| `CE:state(v)` | ✓ | \\*1 |\n| `:host` | ✓ | |\n| `:host(s)` | ✓ | |\n| `:host(:state(v))` | ✓ | \\*1 |\n| `:host:has(rs1, rs2, ...)` | ✓ | |\n| `:host(s):has(rs1, rs2, ...)` | ✓ | |\n| `:host-context(s)` | ✓ | |\n| `:host-context(s):has(rs1, rs2, ...)` | ✓ | |\n| `&` | ✓ | Only supports outermost `&`, i.e. equivalent to `:scope` |\n\n\\*1: `ElementInternals.states`, i.e. `CustomStateSet`, is not implemented in jsdom, so you need to apply a patch in the custom element constructor.\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## Performance\n\nSee [benchmark](https://github.com/asamuzaK/domSelector/actions/workflows/benchmark.yml) for the latest results.\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---\n\nCopyright (c) 2023 [asamuzaK (Kazz)](https://github.com/asamuzaK/)\n","publish_time":1783833659449,"_cnpm_publish_time":1783833659449,"dist-tags":{"beta":"0.2.2","dev":"6.5.0-a.16","latest":"10.0.5","next":"8.0.0-a.2"}}