{"name":"selderee","version":"0.6.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","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.7.0"},"_id":"selderee@0.6.0","_nodeVersion":"10.23.2","_npmVersion":"7.11.2","dist":{"integrity":"sha512-ibqWGV5aChDvfVdqNYuaJP/HnVBhlRGSRrlbttmlMpHcLuTqqbMH36QkSs9GEgj5M88JDYLI8eyP94JaQ8xRlg==","shasum":"f3bee66cfebcb6f33df98e4a1df77388b42a96f7","tarball":"http://registry-npm.ywwl.com/selderee/download/selderee-0.6.0.tgz","fileCount":12,"unpackedSize":50140,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgmWXrCRA9TVsSAnZWagAAXL8P/2LA4mW9vSeCnmfjwUPk\nEWYAzL49pnblgY9RNbr9dimewZdSgdc0sVbCyIFHY/bQtvtXC6bM2GeMuo7N\nUnqPrDLijyvpd+lROEi5UPQ6p1iDcWs64V68hzridnNu4E0eZBIvtyJG4+as\n5iP8h34+qNcZ68i3m1CQs4f1T/UCoiNW/rzHHTxFDk0wCicIu/hiy0xizlVm\nX9LhH64N1k9NdCo/A6yTVOaOpHuvkaZ8L+Q5EdhL6g443Zn2GmNKTuXGNSan\nxnUpr0VoB5gZ5sKB4MjdbI4VvFY5JTu4OLTxQfPE+/f+R3PTbYTdgKkNwbYx\nt/hTSAD2UqLFZZ5fJ88oWwc5YRjKcTt1JUf7/xTdXvUFt06xm2o2pPfmbJDl\ns+CKB7KJHmcoZAxrqNgimNm96QrR04ZJ44btTVP+MeKOLXk4M/XipY/hUXQu\nTTirg1Edu76BU1sS2BurEIxjUHtCt0G4cw1SOe3zeRKAvjAzjCq+UiguEJ+w\nbFGdBPX3jJ42OyifZTskfO0N4vVRdfpcNo5gpGoVG6nWBZjeyB6Q62SlWEe1\nQXRpJf32a1+NQJgB07btbJbU+y7JM3X7F8DeKHtc5dBZa+pMDwN7ezWdO8Y8\n4hYWx3WOZFYmCtFgobB2hWDfRX4kfpr4l/7hnKVB+zRwDpSRso+U0X24tWar\ndQYK\r\n=PIsY\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIFMPWAuiYQB1vKCVvZ20AuPFYGTdA1OyLYrZ7m3rbdKIAiEA/WSjxECJ+JWSsDcJXXub9lpEG3uYRG034pMSc5zOo3E="}],"size":9382,"noattachment":false,"key":"/selderee/-/selderee-0.6.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.6.0_1620665834651_0.6650116888104542"},"_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":1620665834798,"_cnpm_publish_time":1620665834798,"dist-tags":{"latest":"0.12.0"}}