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
npm install css-sniff
yarn add css-sniff
Usage
import { getCSSRules, serializeCSSRules } from "css-sniff";
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
});
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: {
media: ["media substring match"],
stylesheet: ["url substring match"],
rule: ["selector substring match"]
},
blacklist: {
media: ["media substring match"],
stylesheet: ["url substring match"],
rule: ["selector substring match"]
},
document,
ignoreChildren: false,
}
• 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).