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-sniff

v2.0.0

Published

Extracts matching CSS Rules (Selectors) that apply to elements

Downloads

8

Readme

CSS Sniff

A tiny utility that looks at parts of webpages (elements) and returns all the matching CSS Rules (selectors+properties). So it's essentially a CSS scraper but only for particular parts of the page.

Because real CSS Rules are extracted it includes responsive modes and pseudo-elements etc.

Requires a browser-like environment, so it works in browsers and JSDOM.

It's used by React-Patterns to extract CSS Rules from components to make a Pattern Library, but could also be used for extracting crucial CSS 'above the fold' etc.

Install

Usage

import { getCSSRules, serializeCSSRules } from "css-sniff";

// Inside an async function...
const elements = document.querySelectorAll("header, .logo");
const matchedCSS = await getCSSRules([...elements]);
const cssString = serializeCSSRules(matchedCSS);

cssString is now a string that might look like:

header {
  background: red;
}
.logo {
  width: 250px;
}
@media only screen and (max-width: 250px) {
  .logo {
    width: 100%;
  }
}

Usage with JSDOM

import { getCSSRules, serializeCSSRules } from "css-sniff";

const main = async () => {
  dom = await JSDOM.fromURL(url, {
    resources: "usable",
    pretendToBeVisual: true
  });

  // Wait for subresources (external CSS) to load so
  // that CSS is in the DOM and CSS detection will work
  await new Promise(resolve => {
    dom.window.document.addEventListener("load", resolve);
  });

  const elements = document.querySelectorAll("header, .logo");
  const matchedCSS = await getCSSRules([...elements], {
    document: dom.window.document
  });
  const cssString = serializeCSSRules(matchedCSS);
};
main();

API

getCSSRules(children, options, matchedCSS)

The bulk of CSS Sniff. This returns a matchedCSS variable that may be given to serializeCSSRules to produce a CSS string.

It's an async function.

• children (required)

An array of Nodes (not a NodeList).

All nodes below these are searched for CSS Rules.

• options (optional)

A map to set options in format

{
   whitelist: {
         // optional pattern to only include
         // CSS if it matches these patterns,
       media: ["media substring match"],
         // useful for only allowing some
         // types of @media such as print
       stylesheet: ["url substring match"],
         // useful for only allowing some
         // CSS files
       rule: ["selector substring match"]
   },
   blacklist: {
       media: ["media substring match"],
       stylesheet: ["url substring match"],
          // Useful for blocking some CSS files
          // such as a site's template.
          rule: ["selector substring match"]
   },
   document, // optional in browsers, but required
             // for JSDOM to provide the `document`
             // instance
   ignoreChildren: false,
             // don't descend childNodes looking
             // for CSS matches (ie, only check
             // top-level nodes).
}

• matchedCSS (optional)

You may provide a previously returned value to add more matched rules to, in order to build up a more complete set of CSS Rules.

This may be useful to chunk up jobs over several event loop cycles, or perhaps it's an easier API to use in some code patterns (ie. a progress indicator pattern).

serializeCSSRules(matchedCSS)

Serializes matchedCSS into a CSS string.

Limitations

Inherited properties (and sorta-inherited properties)

Some CSS properties are inherited (or effectively inherited) from parent elements.

For example, if you have a header with a red background but that red colour comes from body { background: red; } then searching for CSS Rules for header won't include the red background CSS.

Similarly, if a parent element defines a line-height which is used by descendant elements then that won't be included.

split-css-selector

Thanks to @joakimbeng for this useful library which I've inlined in src/index.js. The reason this was inlined is because this utility is a single file and can avoid Webpack (ie, just use Babel).