All Projects → fb55 → Css Select

fb55 / Css Select

Licence: bsd-2-clause
a CSS selector compiler & engine

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Css Select

Dom
Modern DOM API.
Stars: ✭ 88 (-68.46%)
Mutual labels:  dom, css-selector
CDom
Simple HTML/XML/BBCode DOM component for PHP.
Stars: ✭ 26 (-90.68%)
Mutual labels:  dom, css-selector
Browser Monkey
Reliable DOM testing
Stars: ✭ 53 (-81%)
Mutual labels:  dom, css-selector
ElementFinder
Fetch data from HTML and XML via xpath/css and prepare it with regexp
Stars: ✭ 29 (-89.61%)
Mutual labels:  dom, css-selector
domonic
Create HTML with python 3 using a standard DOM API. Includes a python port of JavaScript for interoperability and tons of other cool features. A fast prototyping library.
Stars: ✭ 86 (-69.18%)
Mutual labels:  dom
Openrunner
Computest Openrunner: Benchmark and functional testing for frontend-heavy web applications
Stars: ✭ 16 (-94.27%)
Mutual labels:  dom
replace-jquery
Automatically finds jQuery methods from existing projects and generates vanilla js alternatives.
Stars: ✭ 1,101 (+294.62%)
Mutual labels:  dom
preact-delegate
Preact delegate DOM events
Stars: ✭ 17 (-93.91%)
Mutual labels:  dom
Arachni
Web Application Security Scanner Framework
Stars: ✭ 2,942 (+954.48%)
Mutual labels:  dom
gh-web-ui
Package for building web-based User Interfaces (UI) in Rhino Grasshopper.
Stars: ✭ 69 (-75.27%)
Mutual labels:  dom
poseidon
A no-dependency, intuitive, and lightweight web framework from scratch in Javascript
Stars: ✭ 41 (-85.3%)
Mutual labels:  dom
dababy
Data binding so simple even DaBaby could do it!
Stars: ✭ 27 (-90.32%)
Mutual labels:  dom
modest ex
Elixir library to do pipeable transformations on html strings (with CSS selectors)
Stars: ✭ 31 (-88.89%)
Mutual labels:  css-selector
svgdom
Library to represent an SVG as a DOM.
Stars: ✭ 30 (-89.25%)
Mutual labels:  dom
Temme
📄 Concise selector to extract JSON from HTML.
Stars: ✭ 257 (-7.89%)
Mutual labels:  css-selector
lish
Javascript Selection Object Library
Stars: ✭ 14 (-94.98%)
Mutual labels:  dom
html-query
A fluent and functional approach to querying HTML
Stars: ✭ 48 (-82.8%)
Mutual labels:  dom
js-utils
A collection of dependency-free JavaScript utilities 🔧
Stars: ✭ 22 (-92.11%)
Mutual labels:  dom
web
🧱 Write your website in pure Swift with power of webassembly. DOM, CSS and all the WebAPIs are available out of the box.
Stars: ✭ 44 (-84.23%)
Mutual labels:  dom
callbag-jsx
callbags + JSX: fast and tiny interactive web apps
Stars: ✭ 69 (-75.27%)
Mutual labels:  dom

css-select NPM version Build Status Downloads Coverage

a CSS selector compiler/engine

What?

css-select turns CSS selectors into functions that tests if elements match them. When searching for elements, testing is executed "from the top", similar to how browsers execute CSS selectors.

In its default configuration, css-select queries the DOM structure of the domhandler module (also known as htmlparser2 DOM). It uses domutils as its default adapter over the DOM structure. See Options below for details on querying alternative DOM structures.

Features:

  • Full implementation of CSS3 selectors
  • Partial implementation of jQuery/Sizzle extensions
  • Very high test coverage
  • Pretty good performance

Why?

The traditional approach of executing CSS selectors, named left-to-right execution, is to execute every component of the selector in order, from left to right (duh). The execution of the selector a b for example will first query for a elements, then search these for b elements. (That's the approach of eg. Sizzle, nwmatcher and qwery.)

While this works, it has some downsides: Children of as will be checked multiple times; first, to check if they are also as, then, for every superior a once, if they are bs. Using Big O notation, that would be O(n^(k+1)), where k is the number of descendant selectors (that's the space in the example above).

The far more efficient approach is to first look for b elements, then check if they have superior a elements: Using big O notation again, that would be O(n). That's called right-to-left execution.

And that's what css-select does – and why it's quite performant.

How does it work?

By building a stack of functions.

Wait, what?

Okay, so let's suppose we want to compile the selector a b again, for right-to-left execution. We start by parsing the selector, which means we turn the selector into an array of the building-blocks of the selector, so we can distinguish them easily. That's what the css-what module is for, if you want to have a look.

Anyway, after parsing, we end up with an array like this one:

[
    { type: "tag", name: "a" },
    { type: "descendant" },
    { type: "tag", name: "b" },
];

Actually, this array is wrapped in another array, but that's another story (involving commas in selectors).

Now that we know the meaning of every part of the selector, we can compile it. That's where it becomes interesting.

The basic idea is to turn every part of the selector into a function, which takes an element as its only argument. The function checks whether a passed element matches its part of the selector: If it does, the element is passed to the next turned-into-a-function part of the selector, which does the same. If an element is accepted by all parts of the selector, it matches the selector and double rainbow ALL THE WAY.

As said before, we want to do right-to-left execution with all the big O improvements nonsense, so elements are passed from the rightmost part of the selector (b in our example) to the leftmost (which would be c of course a).

//TODO: More in-depth description. Implementation details. Build a spaceship.

API

const CSSselect = require("css-select");

Note: css-select throws errors when invalid selectors are passed to it, contrary to the behavior in browsers, which swallow them. This is done to aid with writing css selectors, but can be unexpected when processing arbitrary strings.

CSSselect.selectAll(query, elems, options)

Queries elems, returns an array containing all matches.

  • query can be either a CSS selector or a function.
  • elems can be either an array of elements, or a single element. If it is an element, its children will be queried.
  • options is described below.

Aliases: default export, CSSselect.iterate(query, elems).

CSSselect.compile(query, options)

Compiles the query, returns a function.

CSSselect.is(elem, query, options)

Tests whether or not an element is matched by query. query can be either a CSS selector or a function.

CSSselect.selectOne(query, elems, options)

Arguments are the same as for CSSselect.selectAll(query, elems). Only returns the first match, or null if there was no match.

Options

All options are optional.

  • xmlMode: When enabled, tag names will be case-sensitive. Default: false.
  • rootFunc: The last function in the stack, will be called with the last element that's looked at.
  • adapter: The adapter to use when interacting with the backing DOM structure. By default it uses the domutils module.
  • context: The context of the current query. Used to limit the scope of searches. Can be matched directly using the :scope pseudo-selector.
  • cacheResults: Allow css-select to cache results for some selectors, sometimes greatly improving querying performance. Disable this if your document can change in between queries with the same compiled selector. Default: true.

Custom Adapters

A custom adapter must match the interface described here.

You may want to have a look at domutils to see the default implementation, or at css-select-browser-adapter for an implementation backed by the DOM.

Supported selectors

As defined by CSS 4 and / or jQuery.


License: BSD-2-Clause

Security contact information

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.

css-select for enterprise

Available as part of the Tidelift Subscription

The maintainers of css-select and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].