{"name":"selderee","version":"0.10.0","description":"Selectors decision tree - choose matching selectors, fast","keywords":["CSS","selectors","decision tree","match"],"repository":{"type":"git","url":"git+https://github.com/mxxii/selderee.git"},"bugs":{"url":"https://github.com/mxxii/selderee/issues"},"homepage":"https://github.com/mxxii/selderee","author":{"name":"KillyMXI"},"funding":"https://ko-fi.com/killymxi","license":"MIT","exports":{"import":"./lib/selderee.mjs","require":"./lib/selderee.cjs"},"type":"module","main":"./lib/selderee.cjs","module":"./lib/selderee.mjs","types":"./lib/selderee.d.ts","typedocMain":"./src/selderee.ts","sideEffects":false,"scripts":{"build:rollup":"rollup -c","build:types":"tsc -d --emitDeclarationOnly --declarationDir ./lib","build":"npm run clean && npm run build:rollup && npm run build:types","clean":"rimraf lib"},"dependencies":{"parseley":"^0.11.0"},"_id":"selderee@0.10.0","_nodeVersion":"14.20.0","_npmVersion":"6.14.17","dist":{"integrity":"sha512-DEL/RW/f4qLw/NrVg97xKaEBC8IpzIG2fvxnzCp3Z4yk4jQ3MXom+Imav9wApjxX2dfS3eW7x0DXafJr85i39A==","shasum":"ec83d6044d9026668dc9bd2561acfde99a4e3a1c","tarball":"http://registry-npm.ywwl.com/selderee/download/selderee-0.10.0.tgz","fileCount":12,"unpackedSize":44461,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQD9GRDcMdYbxRMsjNIss4z4YFLxanzpOyAwNg2E8uFiogIgEA2BgaiZ2NSpG6v9tj3zmzRuv3/5XMnMVvsTS9F3MJc="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjikwbACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmqk0A//UC5mKUhzOl3bCh6Q50xdOuPGjavlKUwDXX5mjUC4D+8TuwBS\r\nUHCpj675SpROcctOJJDqpzNJVgsiPs/tUMn+OIRVwkt5T9CgfeePDwbByhmT\r\ndzUC+9hEXTS6GJqXrxNlO1NpQA/xnpxhdWzNftdQVhMJ6Jo6EHXLdaGEUmwP\r\nqZQQv3zNQr+CbpO/fuOepUUmd8ORpUDKwTegTNhEOANdyiU6fkY34gUFgZrG\r\n5y6CICflZsHyHblijh5x9sgPCAJhOPKMb42Nt/omIvkuiq0VunRBo7nc1QEU\r\nB++oT7SrwP16Rp9jwQNa62giLV3eSWwN2P/q+BLE0XOvllTjJvddtnhtg/Na\r\nzy6iLRVFyc+UtNb6CR7ENRvAbtD2lb6Vpl0JZ8Di7aAtrQzMh5emC8VySNzI\r\n/32f3N8U4z/splc5M8xgn6RFIjtZPtCOrhlHztALlkU2zbOQe7Vfzlff/V7z\r\nvXB4d+7r3v5rR/T5o3xZ78+RJALMHn5P9Br6neDKICw5xeCzDn0fL1TogO0B\r\nu7cJwPQN74plHh91KO5gK/8terAFKi6K9zazmzsQNC3lJgmqZxPzeO58yQzF\r\nVncwLHRUkUHHbvtslZ6eYa1D9P2Q+jyM3L3Z6xzt3/UPQEmTLhSZYLSeeAOe\r\nKIe0/P02mjS3IxCCkjMG8ESbaMsv2hGC2gA=\r\n=qXjM\r\n-----END PGP SIGNATURE-----\r\n","size":9666,"noattachment":false,"key":"/selderee/-/selderee-0.10.0.tgz"},"_npmUser":{"name":"killy.mxi","email":"killy.mxi@gmail.com"},"directories":{},"maintainers":[{"name":"killy.mxi","email":"killy.mxi@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/selderee_0.10.0_1670007834940_0.5202512844788336"},"_hasShrinkwrap":false,"readme":"# selderee\n\n![lint status badge](https://github.com/mxxii/selderee/workflows/lint/badge.svg)\n![test status badge](https://github.com/mxxii/selderee/workflows/test/badge.svg)\n[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/mxxii/selderee/blob/main/LICENSE)\n[![npm](https://img.shields.io/npm/dw/selderee?color=informational&logo=npm)](https://www.npmjs.com/package/selderee)\n\n**Sel**ectors **de**cision t**ree** - pick matching selectors, fast.\n\n----\n\n\n## What is it for\n\nThe problem statement: there are multiple CSS selectors with attached handlers, and a HTML DOM to process. For each HTML Element a matching handler has to be found and applied.\n\nThe naive approach is to walk through the DOM and test each and every selector against each Element. This means *O(n\\*m)* complexity.\n\nIt is pretty clear though that if we have selectors that share something in common then we can reduce the number of checks.\n\nThe main `selderee` package offers the selectors tree structure. Runnable decision functions for specific DOM implementations are built via plugins.\n\n\n## Limitations\n\n- Pseudo-classes and pseudo-elements are not supported by the underlying library [parseley](https://github.com/mxxii/parseley) (yet?);\n- General siblings (`~`), descendants (` `) and same column combinators (`||`) are also not supported.\n\n\n## `selderee` vs `css-select`\n\n[css-select](https://github.com/fb55/css-select) - a CSS selector compiler & engine.\n\n| Feature                               | `selderee` | `css-select` |\n| ------------------------------------- | :--------: | :----------: |\n| Support for `htmlparser2` DOM AST     | plugin     | +            |\n| \"Compiles\" into a function            | +          | +            |\n| Pick selector(s) for a given Element  | +          |              |\n| Query Element(s) for a given selector |            | +            |\n\n\n## Packages\n\n| Package   | Version   | Folder    | Changelog |\n| --------- | --------- | --------- | --------- |\n| [selderee](https://www.npmjs.com/package/selderee) | [![npm](https://img.shields.io/npm/v/selderee?logo=npm)](https://www.npmjs.com/package/selderee) | [/packages/selderee](https://github.com/mxxii/selderee/tree/main/packages/selderee/) | [changelog](https://github.com/mxxii/selderee/blob/main/packages/selderee/CHANGELOG.md) |\n| [@selderee/plugin-htmlparser2](https://www.npmjs.com/package/@selderee/plugin-htmlparser2) | [![npm](https://img.shields.io/npm/v/@selderee/plugin-htmlparser2?logo=npm)](https://www.npmjs.com/package/@selderee/plugin-htmlparser2) | [/packages/plugin-htmlparser2](https://github.com/mxxii/selderee/tree/main/packages/plugin-htmlparser2/) | [changelog](https://github.com/mxxii/selderee/blob/main/packages/plugin-htmlparser2/CHANGELOG.md) |\n\n\n## Install\n\n```shell\n> npm i selderee @selderee/plugin-htmlparser2\n```\n\n\n## Documentation\n\n- [API](https://github.com/mxxii/selderee/blob/main/docs/index.md)\n\n\n## Usage example\n\n```js\nconst htmlparser2 = require('htmlparser2');\nconst util = require('util');\n\nconst { DecisionTree, Treeify } = require('selderee');\nconst { hp2Builder } = require('@selderee/plugin-htmlparser2');\n\nconst selectorValuePairs = [\n  ['p', 'A'],\n  ['p.foo[bar]', 'B'],\n  ['p[class~=foo]', 'C'],\n  ['div.foo', 'D'],\n  ['div > p.foo', 'E'],\n  ['div > p', 'F'],\n  ['#baz', 'G']\n];\n\n// Make a tree structure from all given selectors.\nconst selectorsDecisionTree = new DecisionTree(selectorValuePairs);\n\n// `treeify` builder produces a string output for testing and debug purposes.\n// `treeify` expects string values attached to each selector.\nconst prettyTree = selectorsDecisionTree.build(Treeify.treeify);\nconsole.log(prettyTree);\n\nconst html = /*html*/`<html><body>\n  <div><p class=\"foo qux\">second</p></div>\n</body></html>`;\nconst dom = htmlparser2.parseDocument(html);\nconst element = dom.children[0].children[0].children[1].children[0];\n\n// `hp2Builder` produces a picker that can pick values\n// from the selectors tree.\nconst picker = selectorsDecisionTree.build(hp2Builder);\n\n// Get all matches\nconst allMatches = picker.pickAll(element);\nconsole.log(util.inspect(allMatches, { breakLength: 70, depth: null }));\n\n// or get the value from the most specific match.\nconst bestMatch = picker.pick1(element);\nconsole.log(`Best matched value: ${bestMatch}`);\n```\n\n<details><summary>Example output</summary>\n\n```text\n▽\n├─◻ Tag name\n│ ╟─◇ = p\n│ ║ ┠─▣ Attr value: class\n│ ║ ┃ ╙─◈ ~= \"foo\"\n│ ║ ┃   ┠─◨ Attr presence: bar\n│ ║ ┃   ┃ ┖─◁ #1 [0,2,1] B\n│ ║ ┃   ┠─◁ #2 [0,1,1] C\n│ ║ ┃   ┖─◉ Push element: >\n│ ║ ┃     └─◻ Tag name\n│ ║ ┃       ╙─◇ = div\n│ ║ ┃         ┖─◁ #4 [0,1,2] E\n│ ║ ┠─◁ #0 [0,0,1] A\n│ ║ ┖─◉ Push element: >\n│ ║   └─◻ Tag name\n│ ║     ╙─◇ = div\n│ ║       ┖─◁ #5 [0,0,2] F\n│ ╙─◇ = div\n│   ┖─▣ Attr value: class\n│     ╙─◈ ~= \"foo\"\n│       ┖─◁ #3 [0,1,1] D\n└─▣ Attr value: id\n  ╙─◈ = \"baz\"\n    ┖─◁ #6 [1,0,0] G\n[ { index: 2, value: 'C', specificity: [ 0, 1, 1 ] },\n  { index: 4, value: 'E', specificity: [ 0, 1, 2 ] },\n  { index: 0, value: 'A', specificity: [ 0, 0, 1 ] },\n  { index: 5, value: 'F', specificity: [ 0, 0, 2 ] } ]\nBest matched value: E\n```\n\n*Some gotcha: you may notice the check for `#baz` has to be performed every time the decision tree is called. If it happens to be `p#baz` or `div#baz` or even `.foo#baz` - it would be much better to write it like this. Deeper, narrower tree means less checks on average. (in case of `.foo#baz` the class check might finally outweigh the tag name check and rebalance the tree.)*\n\n</details>\n\n\n## Development\n\nTargeting Node.js version >=14.\n\nMonorepo uses NPM v7 workspaces (make sure v7 is installed when used with Node.js v14.)\n","publish_time":1670007835216,"_cnpm_publish_time":1670007835216,"dist-tags":{"latest":"0.12.0"}}