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

mu-selector-set

v1.1.0

Published

A CSS selector set with fast DOM element matching.

Downloads

4

Readme

Version Version Build Status Coverage Status

mu-selector-set

A CSS selector set with fast DOM element matching.

Background in this post (as well as an alternative implementation) by josh.

Here we provide a faster, more modular, UMD-compliant implementation of Selector Set.

See benchmarks.

Usage

var set = new SelectorSet()

set.add(selector, data ...)

Add a selector to the set.

  1. selector {String} - The selector to add.
  2. data ... - Arbitrary number of additional parameters which will be added with the selector as associated data.

Returns the set instance, so remove / add calls can be chained.

set.remove(selector, data ...)

Remove a selector (+data) from the set.

  1. selector {String} - The selector to remove.
  2. data ... - Arbitrary number of additional parameters for more specific removal of selectors.

Returns the set instance, so remove / add calls can be chained.

set.matches(element ...)

Matches DOM elements to selectors in the set.

  1. element ... {DOMElement} - The DOM elements to match.

Returns array of arrays. Each sub-array is a selector that matches the elements

  • the data this selector was added with.

SelectorSet.prototype.matchesSelector

matchesSelector is a function which checks if an element matches a selector. It's used internally by SelectorSet, but exposed through the prototype to allow overriding with a custom method.

It takes the following parameters:

  1. element {DOMElement} - The element to match
  2. selector {String} - The selector to match against

It returns true if the element matches the selector. false otherwise.

The default implementation tries to use the browser's native match or matchesSelector DOM functions, or a polyfill for older browsers.

It can be overridden by:

  1. Overriding the prototype, thus modifying all instances of SelectorSet:

    SelectorSet.prototype.matchesSelector = customFunction;
    var sSet = new SelectorSet();
  2. Overriding the instance, thus modifying a specific instance of SelectorSet:

    var sSet = new SelectorSet();
    sSet.matchesSelector = customFunction;

Installation

  • Node: 0. npm install mu-selector-set 0. var SelectorSet = require('mu-selector-set');
  • AMD (install with bower): 0. bower install mu-selector-set 0. require(['mu-selector-set'], function(SelectorSet){ /* ... */ });

Run tests with npm test.

Run coverage analysis with npm run coverage (coverage report is saved to ./coverage).

Examples

var el = $("<div id='foo' class='bar'/>").get(0),
    sSet = new SelectorSet();

sSet.add("div")
    .add(".bar", 123, 456)
    .add(".baz", 123, "abc")
    .add("#foo.bar")
    .add("*");

sSet.matches(el);
//[ [ '#foo.bar' ],
//  [ '.bar', 123, 456 ],
//  [ 'div' ],
//  [ '*' ] ]

Multiple elements:

var el1 = $("<div id='foo'/>").get(0),
    el2 = $("<div class='bar'/>").get(0),
    sSet = new SelectorSet();

sSet.add("div")
    .add(".bar", 123, 456)
    .add(".baz", 123, "abc")
    .add("#foo.bar, .bar.baz")
    .add("*");

set.matches(el1, el2);
//[ [ 'div' ],
//  [ '*' ],
//  [ '.bar', 123, 456 ] ]