{"name":"@asamuzakjp/dom-selector","version":"6.0.4","author":{"name":"asamuzaK"},"license":"MIT","_id":"@asamuzakjp/dom-selector@6.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":"e550a567879d964509dc9d833b0c064d1e4238e2","tarball":"http://registry-npm.ywwl.com/@asamuzakjp/dom-selector/download/@asamuzakjp/dom-selector-6.0.4.tgz","fileCount":28,"integrity":"sha512-oROs2SIkR8Emue1E3q1dxrD64Oh2IIkXoaV1QfLCpmcaRj1wJWupCZxMa/97FirQfN7R9VkyNRinFeo5N2sHSA==","signatures":[{"sig":"MEUCIEHL0amvMMYvBzOQEZhzZmx/zAYRbveXulYrhXLdKihaAiEA9kMtl/T2FeoYkwNST27IuhOyLK/r7JAV1NoU3adjXfA=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":443281,"size":94837,"noattachment":false,"key":"/@asamuzakjp/dom-selector/-/@asamuzakjp/dom-selector-6.0.4.tgz"},"type":"module","types":"types/index.d.ts","exports":{"import":"./src/index.js","require":"./dist/cjs/index.js"},"gitHead":"f9f73be49a9bacc58737b7ecb94782f084569dbf","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"},"overrides":{"jsdom":"^25.0.0"},"repository":{"url":"git+https://github.com/asamuzaK/domSelector.git","type":"git"},"_npmVersion":"10.8.3","description":"A CSS selector engine.","directories":{},"_nodeVersion":"20.17.0","dependencies":{"bidi-js":"^1.0.3","css-tree":"^2.3.1","@asamuzakjp/nwsapi":"^2.2.16","is-potential-custom-element-name":"^1.0.1"},"_hasShrinkwrap":false,"devDependencies":{"c8":"^10.1.2","chai":"^5.1.1","jsdom":"^25.0.0","mocha":"^10.7.3","sinon":"^18.0.0","eslint":"^8.57.0","esbuild":"^0.23.1","linkedom":"^0.18.4","benchmark":"^2.1.4","commander":"^12.1.0","happy-dom":"^15.7.3","typescript":"^5.5.4","wpt-runner":"^5.0.0","@types/css-tree":"^2.3.8","eslint-plugin-jsdoc":"^50.2.2","eslint-plugin-import":"^2.30.0","eslint-plugin-regexp":"^2.6.0","eslint-plugin-unicorn":"^55.0.0","eslint-config-standard":"^17.1.0"},"_npmOperationalInternal":{"tmp":"tmp/dom-selector_6.0.4_1725801449624_0.7704550112031749","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 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":1725801449768,"_cnpm_publish_time":1725801449768,"dist-tags":{"beta":"0.2.2","dev":"6.5.0-a.16","latest":"10.0.5","next":"8.0.0-a.2"}}