{"name":"@asamuzakjp/dom-selector","version":"9.0.4","author":{"name":"asamuzaK"},"license":"MIT","_id":"@asamuzakjp/dom-selector@9.0.4","maintainers":[{"name":"asamuzakjp","email":"info@asamuzak.jp"}],"homepage":"https://github.com/asamuzaK/domSelector#readme","bugs":{"url":"https://github.com/asamuzaK/domSelector/issues"},"dist":{"shasum":"9c3ca388a3b9c9ac1315ab7f6400a3e6c8335439","tarball":"http://registry-npm.ywwl.com/@asamuzakjp/dom-selector/download/@asamuzakjp/dom-selector-9.0.4.tgz","fileCount":27,"integrity":"sha512-kNLMBsH0gPJeVk1Qm3uCrRj+QinctA9FMxy2daYpVdTE8lfMTWa7K+87RMd46TRon63QzlKwGesfGJ799ToIwQ==","signatures":[{"sig":"MEUCIDFW5CSTG8d9AZiZfLa1ufRGoMmYLSNzIN2qGSPV69qsAiEA97zQW1PeD1ffVYH8FpiheNaVoxS4TP0GlgMD/ODRfUg=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"sig":"MEQCIBwmRv7pSfS1fm4egz2FykRr9yQI3KatnFLnJvnL61ujAiBBMD8b+C3VqToKsz6ef085BTzOOx7ng4FJhn46rb3inQ==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":278768,"size":60089,"noattachment":false,"key":"/@asamuzakjp/dom-selector/-/@asamuzakjp/dom-selector-9.0.4.tgz"},"type":"module","types":"./types/index.d.ts","engines":{"node":"^22.22.2 || ^24.15.0 || >=26.0.0"},"exports":{".":{"types":"./types/index.d.ts","default":"./src/index.js"},"./package.json":"./package.json"},"gitHead":"0836ef718ef3357fed5f3cbe563f01bbbc7256f8","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":{"jsdom":"$jsdom","mocha":{"diff":"^8.0.3","js-yaml":"^4.3.1","serialize-javascript":"^7.0.7"}},"repository":{"url":"git+https://github.com/asamuzaK/domSelector.git","type":"git"},"_npmVersion":"12.0.2","description":"A CSS selector engine.","directories":{},"_nodeVersion":"24.18.1","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":"^12.0.0","chai":"^6.2.2","jsdom":"^30.0.1","mocha":"^11.8.0","sinon":"^22.1.0","eslint":"^10.9.1","mitata":"^1.0.34","globals":"^17.11.0","prettier":"^3.9.6","commander":"^15.0.0","tinybench":"^6.1.4","typescript":"^7.0.2","wpt-runner":"^7.0.0","@types/node":"^26.4.0","neostandard":"^0.14.0-next.1","@types/css-tree":"^3.2.0","eslint-plugin-jsdoc":"^64.2.1","eslint-plugin-regexp":"^3.2.0","eslint-plugin-unicorn":"^74.0.0","eslint-config-prettier":"^10.1.8","eslint-plugin-prettier":"^5.5.6"},"_npmOperationalInternal":{"tmp":"tmp/dom-selector_9.0.4_1787968794652_0.6703677432612865","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 LRU cache. Defaults to `4096`.\n  * `opt.idlUtils` **{object}?** The internal IDL wrapper used for jsdom.\n  * `opt.maxLength` **{number}?** The maximum length of the selector allowed. Defaults to `2048`.\n\n---\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}?** `true` to suppress exceptions (e.g., `DOMException` for invalid selectors) and return `false` instead.\n  * `opt.warn` **{boolean}?** `true` to output warnings to the console when encountering unsupported or deprecated features.\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}?** `true` to suppress exceptions and return `null` instead.\n  * `opt.warn` **{boolean}?** `true` to output warnings to the console when encountering unsupported or deprecated features.\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}?** `true` to suppress exceptions and return `null` instead.\n  * `opt.warn` **{boolean}?** `true` to output warnings to the console when encountering unsupported or deprecated features.\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<Element>`, 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}?** `true` to suppress exceptions and return an empty array `[]` instead.\n  * `opt.warn` **{boolean}?** `true` to output warnings to the console when encountering unsupported or deprecated features.\n* **Returns** **{Array<Element>}** Array of matched elements.\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.requireAst` **{boolean}?** Indicates whether to always try to parse and return the AST.\n  * `opt.noexcept` **{boolean}?** `true` to suppress exceptions and store the error in the `error` property of the result instead of throwing.\n  * `opt.warn` **{boolean}?** `true` to output warnings to the console when encountering unsupported or deprecated features.\n* **Returns** **{CheckResult}** 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  * `error` **{DOMException | Error | null}** The error object, if any.\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<SelectorSubject>}** An array of extracted subject keys. Each object contains:\n  * `id` **{string | null}** The ID.\n  * `className` **{string | null}** The class name.\n  * `tag` **{string | null}** The tag name.\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":1787968794749,"_cnpm_publish_time":1787968794749,"dist-tags":{"beta":"0.2.2","dev":"6.5.0-a.16","latest":"10.0.5","next":"8.0.0-a.2"}}