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

@cssfn/css-selector

v1.0.6

Published

Manipulates css selector - parse, transform, calculate specificity, and more.

Downloads

18

Readme

css-selector - Parses & Manipulates .css>.selector

A lib for parsing, transforming, calculating specificity, and more.

Preview

import { parseSelectors, flatMapSelectors, selectorsToString } from '@cssfn/css-selector'

const selectors = parseSelectors("div>.gallery img[data-msg='you & me'], *>:is(div, .container:nth-child(2n+3)).wrapper");

console.log(selectors.length); // result: 2

console.log(selectors); // result:
/*
// selectors:
[
    // first selector:
    [
        ["", "div"], ">", [".", "gallery"], " ", ["", "img"], ["[", null, ["data-msg", "=", "you & me"]],
    ],
    
    // second selector:
    [
        ["*"], ">",
        [":", "is", [ // nested selectors:
            // nested first selector:
            [
                ["", "div"],
            ],
            
            // nested second selector:
            [
                [".", "container"], [":", "nth-child", "2n+3"],
            ],
        ]],
        [".", "wrapper"],
    ],
]
*/

const replacement = parseSelectors(".awesome>.yeah")[0];

const mutated = flatMapSelectors(selectors, (selector) => {
    const [
        /*
            selector types:
            '&'  = parent         selector
            '*'  = universal      selector
            '['  = attribute      selector
            ''   = element        selector
            '#'  = ID             selector
            '.'  = class          selector
            ':'  = pseudo class   selector
            '::' = pseudo element selector
        */
        selectorType,
        
        /*
            selector name:
            string = the name of [element, ID, class, pseudo class, pseudo element] selector
        */
        selectorName,
        
        /*
            selector parameter(s):
            string       = the parameter of pseudo class selector, eg: nth-child(2n+3) => '2n+3'
            array        = [name, operator, value, options] of attribute selector, eg: [data-msg*="you & me" i] => ['data-msg', '*=', 'you & me', 'i']
            SelectorList = nested selector(s) of pseudo class [:is(...), :where(...), :not(...)]
        */
        selectorParams,
    ] = selector;
    
    if ((selectorType === '' /* element selector */) && (selectorName === 'div')) {
        return replacement;
    } // if
    
    // leave everything else unchanged:
    return selector;
});

console.log(mutated); // result:
/*
// selectors:
[
    // first selector:
    [
        [".", "awesome"], ">", [".", "yeah"], ">", [".", "gallery"], " ", ["", "img"], ["[", null, ["data-msg", "=", "you & me"]],
    ],
    
    // second selector:
    [
        ["*"], ">",
        [":", "is", [ // nested selectors:
            // nested first selector:
            [
                [".", "awesome"], ">", [".", "yeah"],
            ],
            
            // nested second selector:
            [
                [".", "container"], [":", "nth-child", "2n+3"],
            ],
        ]],
        [".", "wrapper"],
    ],
]
*/

const rendered = selectorsToString(mutated);
console.log(rendered); // result:
/*
".awesome>.yeah>.gallery img[data-msg=\"you & me\"], *>:is(.awesome>.yeah, .container:nth-child(2n+3)).wrapper"
*/

Features

  • Recognized selectors:
    • parent selector &.
    • universal selector *.
    • attribute selector [some="value" S].
    • element selector div.
    • ID selector #id.
    • class selector .my-class.
    • pseudo class selector :enabled.
    • pseudo element selector ::before.
  • Recognized combinators:
    • descendant combinator .
    • child combinator >.
    • sibling combinator ~.
    • next sibling combinator +.
  • Recognized strings:
    • unquoted string hello world.
    • single quoted string 'i\'m cool'.
    • double quoted string "that's \"right\"".
  • Recognized nested selectors (up to infinite) inside :is(), :not(), :where(), :has().
  • Fast - our algorithm relies solely on internal JS functions like skipXXX, eatXXX, isXXX, parseXXX.
    No regular expression (RegExp) for parsing.

Installation

Using npm:

npm i @cssfn/css-selector

Support Us

If you feel our lib is useful for your projects,
please make a donation to avoid our project from extinction.

We always maintain our projects as long as we're still alive.

[Make a donation]