Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
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.
npm install css-sniff yarn add css-sniff
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();
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.
An array of Nodes (not a NodeList).
All nodes below these are searched for CSS Rules.
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).
}
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).
Serializes matchedCSS
into a CSS string.
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.
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).
FAQs
Extracts matching CSS Rules (Selectors) that apply to elements
The npm package css-sniff receives a total of 1 weekly downloads. As such, css-sniff popularity was classified as not popular.
We found that css-sniff demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.