{"name":"selderee","version":"0.8.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","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.9.0"},"_id":"selderee@0.8.0","_nodeVersion":"12.22.6","_npmVersion":"6.14.15","dist":{"integrity":"sha512-d+8R+EYT69xreidSzxfrvy9M6sEeHZhNJrwTRL2dGD1YeF9TCmp54C4GdRgH4iMeavdO7b1eildI5yhuWYxyrA==","shasum":"9b9290171c675b16945476333fd99483e41e7329","tarball":"http://registry-npm.ywwl.com/selderee/download/selderee-0.8.0.tgz","fileCount":12,"unpackedSize":28168,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhrTLYCRA9TVsSAnZWagAA6eUP/iyWqa5dY+7BZ3w+dQeu\nMF2k+KWBX5JV191l5cBbJC/36LiMVZASsmJMSKoyXvrb6UgQ1sGaX0goX7nh\nbkki2B1SxDJfJSSldP2j8g/kr626a2zE+mWKlKa1ExS8BCnQpDia1+2MK7G/\nYptY+u3wFn7U/9mBJn+rcbuElnv9Tyqi5xMVjGzODpXjj2cn/I9O3myv2ttw\n1AYUVxb44ZkuR8VC15kZMp8B9oK767VoxMyz3Rtk4ncxY5HMcgrqTX0b5W5x\nh+PF61WOeKWvUpc4mqbCBN44gLfF4q8A9g5sjhlhxm/ukTE66GiRJPRero2t\neLgVoB0fO6jZHCGD8ThdAZdKKu8LUxMvanlDYIhmPV6iSgs+yOgkGD6RRzej\nf/tqqiAPuAvmUd17vyhes+5iLD9NJSb+WPQffP9pOcwukw6svn8hgApgiG20\ngGp6BMwpeiAb+m3za8bpITBWm8vEKrFbP8EulvtveR6rR139HD1pe5a1kK/e\nkWnaC3zNOfRUMe5GG+2PkJ3QTKE37UC+wePGaXf/ikUQFpPZNqI38Z6SZgXs\n9viTpgvvelNCUdD505rigHWxPitJMeVteH9nfMry6t4/F9cb6rwv5rTI1Kvx\n+WldNOBHh2KAnwHZL/AiCTUWZsgjuOmcRH49J0qtlpqW0SEgXNa6eNcdGdWV\nZrgH\r\n=Ksex\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIC2x9ZlmU7FiSsv/qOiTHPwBImPnIoE2cDtp5hqga4urAiEAxLUg7g+rU37eWv3m/s1/31rFFH4rJDynIQeWoM8lfVk="}],"size":9195,"noattachment":false,"key":"/selderee/-/selderee-0.8.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.8.0_1638740696734_0.9248772880487899"},"_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":1638740696871,"_cnpm_publish_time":1638740696871,"dist-tags":{"latest":"0.12.0"}}