npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

selderee

v0.11.0

Published

Selectors decision tree - choose matching selectors, fast

Downloads

8,075,290

Readme

selderee

lint status badge test status badge License: MIT npm

Selectors decision tree - pick matching selectors, fast.


What is it for

The 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.

The naive approach is to walk through the DOM and test each and every selector against each Element. This means O(n*m) complexity.

It is pretty clear though that if we have selectors that share something in common then we can reduce the number of checks.

The main selderee package offers the selectors tree structure. Runnable decision functions for specific DOM implementations are built via plugins.

Limitations

  • Pseudo-classes and pseudo-elements are not supported by the underlying library parseley (yet?);
  • General siblings (~), descendants ( ) and same column combinators (||) are also not supported.

selderee vs css-select

css-select - a CSS selector compiler & engine.

| Feature | selderee | css-select | | ------------------------------------- | :--------: | :----------: | | Support for htmlparser2 DOM AST | plugin | + | | "Compiles" into a function | + | + | | Pick selector(s) for a given Element | + | | | Query Element(s) for a given selector | | + |

Packages

| Package | Version | Folder | Changelog | | --------- | --------- | --------- | --------- | | selderee | npm | /packages/selderee | changelog | | @selderee/plugin-htmlparser2 | npm | /packages/plugin-htmlparser2 | changelog |

Install

> npm i selderee @selderee/plugin-htmlparser2

Documentation

Usage example

const htmlparser2 = require('htmlparser2');
const util = require('util');

const { DecisionTree, Treeify } = require('selderee');
const { hp2Builder } = require('@selderee/plugin-htmlparser2');

const selectorValuePairs = [
  ['p', 'A'],
  ['p.foo[bar]', 'B'],
  ['p[class~=foo]', 'C'],
  ['div.foo', 'D'],
  ['div > p.foo', 'E'],
  ['div > p', 'F'],
  ['#baz', 'G']
];

// Make a tree structure from all given selectors.
const selectorsDecisionTree = new DecisionTree(selectorValuePairs);

// `treeify` builder produces a string output for testing and debug purposes.
// `treeify` expects string values attached to each selector.
const prettyTree = selectorsDecisionTree.build(Treeify.treeify);
console.log(prettyTree);

const html = /*html*/`<html><body>
  <div><p class="foo qux">second</p></div>
</body></html>`;
const dom = htmlparser2.parseDocument(html);
const element = dom.children[0].children[0].children[1].children[0];

// `hp2Builder` produces a picker that can pick values
// from the selectors tree.
const picker = selectorsDecisionTree.build(hp2Builder);

// Get all matches
const allMatches = picker.pickAll(element);
console.log(util.inspect(allMatches, { breakLength: 70, depth: null }));

// or get the value from the most specific match.
const bestMatch = picker.pick1(element);
console.log(`Best matched value: ${bestMatch}`);
▽
├─◻ Tag name
│ ╟─◇ = p
│ ║ ┠─▣ Attr value: class
│ ║ ┃ ╙─◈ ~= "foo"
│ ║ ┃   ┠─◨ Attr presence: bar
│ ║ ┃   ┃ ┖─◁ #1 [0,2,1] B
│ ║ ┃   ┠─◁ #2 [0,1,1] C
│ ║ ┃   ┖─◉ Push element: >
│ ║ ┃     └─◻ Tag name
│ ║ ┃       ╙─◇ = div
│ ║ ┃         ┖─◁ #4 [0,1,2] E
│ ║ ┠─◁ #0 [0,0,1] A
│ ║ ┖─◉ Push element: >
│ ║   └─◻ Tag name
│ ║     ╙─◇ = div
│ ║       ┖─◁ #5 [0,0,2] F
│ ╙─◇ = div
│   ┖─▣ Attr value: class
│     ╙─◈ ~= "foo"
│       ┖─◁ #3 [0,1,1] D
└─▣ Attr value: id
  ╙─◈ = "baz"
    ┖─◁ #6 [1,0,0] G
[ { index: 2, value: 'C', specificity: [ 0, 1, 1 ] },
  { index: 4, value: 'E', specificity: [ 0, 1, 2 ] },
  { index: 0, value: 'A', specificity: [ 0, 0, 1 ] },
  { index: 5, value: 'F', specificity: [ 0, 0, 2 ] } ]
Best matched value: E

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.)

Development

Targeting Node.js version >=14.

Monorepo uses NPM v7 workspaces (make sure v7 is installed when used with Node.js v14.)