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

css-simple-parser

v3.0.0

Published

A (S)CSS parser that's tiny, blazing fast and (too) simple.

Downloads

2,449

Readme

CSS Simple Parser

A (S)CSS parser that's tiny, blazing fast and (too) simple.

Features

  • Tiny: at ~1.5kb (min + gzip) this is about as small a CSS parser as you can get.
  • Blazing fast: on a mid-2014 MBP it takes ~0.03ms, on average when benchmarking, to generate an AST for this ~500 lines CSS string, about 100x faster than PostCSS.
  • Nesting: nested (S)CSS rules are supported.

Caveats

The big caveat is that this is not a full-blown CSS parser, it can only parse (S)CSS strings with the following limitations:

  • Only rule blocks are supported at the top-level, e.g. no @charset, @import etc.
  • No {, } or ; can be used inside strings, e.g. div[attr=";{}"], content: "{}" etc.

Lastly, the generated AST is quite crude and might require further processing.

Install

npm install --save css-simple-parser

Usage

AST

The AST (Abstract Syntax Tree) provided by this library has the following shape:

type AST = ROOT_NODE;

type ROOT_NODE = {
  parent: null,
  children: NODE[]
};

type NODE = {
  parent: ROOT_NODE | NODE,
  index: number,
  indexEnd: number,
  selector: string,
  selectorIndex: number,
  selectorIndexEnd: number,
  body: string,
  bodyIndex: number,
  bodyIndexEnd: number,
  children: NODE[]
};

Parser.parse

This method computes an AST from the given CSS string.

import Parser from 'css-simple-parser';

const ast = Parser.parse ( '.foo {}' );

Parser.stringify

This method computes a CSS string given an AST.

import Parser from 'css-simple-parser';

const ast = Parser.parse ( '.foo {}' ),
      css = Parser.stringify ( ast );

Parser.traverse

This method calls a function with each node found in the AST, the AST is being traversed depth-first.

import Parser from 'css-simple-parser';

const ast = Parser.parse ( '.foo {}' );

Parser.traverse ( ast, node => {
  console.log ( node.selector );
});

Related

License

MIT © Fabio Spampinato