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

scopeify-html

v0.10.2

Published

Scopeify CSS in HTML

Downloads

68

Readme

Scopeify HTML Build Status

The goal of this library is to scope all CSS selectors in an HTML document.

Features

  • Every CSS selector is scoped
  • Media queries are scoped and preserved
  • Fonts are scoped and preserved
  • Keyframes are scoped and preserved
  • Can use PostCSS plugins to modify all extracted CSS
  • Ability to do aditional processing with PostCSS

Why

The primary reason for creating this library was to render HTML and CSS inside a document without that CSS effecting the parent document. Basically this library was created to avoid having to use an iframe.

Other libraries that attempt to solve this problem, such as juice do so by inlining all the CSS, which loses pseudo selectors, keyframes, and font-face names.

How it works

The core functionality of this library comes from PostCSS. We are using postcss-scopeify-everything to perform all the selector scope transformations.

We iterate over all the CSS rules within an HTML document and scope all of them using a hash of the content. Then we iterate over all the DOM elements in the document and apply the newly scoped selectors. Then we return the document and the CSS separated.

  • Scope CSS by converting the following selectors and names in the HTML document:
    • Convert HTML elements into scoped classes,
    • Ids,
    • Classes,
    • Keyframe names, and
    • Font-face names
  • Convert all HTML selectors into scoped selectors

Usage

const scopeifyHtml = require('scopeify-html');
const insertCss = scopeifyHtml.insertCss;
const getCss = scopeifyHtml.getCss;

const html = `
<!DOCTYPE html>
<html>
<head>
  <style>
    .foo { display: flex; }
    div { margin: 10px; border: 1px solid black; }
    #bar { flex: 1; font-size: 18px; }
  </style>
</head>
<body>
  <div class="foo">
    <div>All your base</div>
    <div id="bar">Are belong to us</div>
  </div>
</body>
</html>
`;

const parser = new DOMParser();
const doc = parser.parseFromString(html, "text/html");

const scoped = scopeifyHtml().sync(doc);
console.log(scoped);
/*
{
  elements: { div: 'div_el_3BuKMO' },
  classes: { foo: 'foo_3BuKMO' },
  ids: { bar: 'bar_3BuKMO' },
  keyframes: {},
  fontFaces: {}
}
*/

const scopedCss = getCss(scoped);
console.log(scopedCss);
/*
  .foo_3BuKMO { display: flex; }
  .div_el_3BuKMO { margin: 10px; border: 1px solid black; }
  #bar_3BuKMO { flex: 1; font-size: 18px; }
*/

// insert scoped CSS into DOM <head>
insertCss(scopedCss, doc);

console.log(doc.documentElement.outerHTML);
/*
<html>
<head>
  <style type="text/css">
    .foo_3BuKMO { display: flex; }
    .div_el_3BuKMO { margin: 10px; border: 1px solid black; }
    #bar_3BuKMO { flex: 1; font-size: 18px; }
  </style>
</head>
<body>
  <div class="foo_3BuKMO div_el_3BuKMO">
    <div class="div_el_3BuKMO">All your base</div>
    <div id="bar_3BuKMO" class="div_el_3BuKMO">Are belong to us</div>
  </div>
</body>
</html>
*/

Async promise

const scopeifyHtml = require('scopeify-html');
const insertCss = scopeifyHtml.insertCss;
const getCss = scopeifyHtml.getCss;

const html = '<div>hi mom</div>';
const parser = new DOMParser();
const doc = parser.parseFromString(html, "text/html");

scopeifyHtml({ replaceClassName: true })
  .promise(doc)
  .then((scoped) => {
    const scopedCss = getCss(scoped);
    insertCss(scopedCss, doc);
  })
  .catch(console.log);

API

scopeifyHtml

Primary entry point for the library.

scopeifyHtml(options: Object)

  • replaceClassName (Boolean, default false): Removes any classnames not used in CSS

The options passed here will also be passed to postcss-scopeify-everything

  • plugins (Array, default []): adds PostCSS plugins before the scopeify plugin
  • scopeifyFn (Function): the function that hashes the identifier name
  • scopeifyElFn (Function): the function that converts an element name to a class name
  • asteriskName (Function|String, default __asterisk): the string that is used for the wildcard selector *
  • ids (Boolean, default false): determines whether or not to disable scoping ids
  • elements (Boolean, default false): determines whether or not to disable scoping elements
  • classes (Boolean, default false): determines whether or not to disable scoping classes
  • keyframes (Boolean, default false): determines whether or not to disable scoping keyframes
  • fontFaces (Boolean, default false): determines whether or not to disable scoping fontFaces

sync

Synchronously processes the CSS and HTML

scopeifyHtml().sync(doc: Document) => scopedSelectors

promise

Asynchronously processes the CSS and HTML

scopeifyHtml().promise(doc: Document) => Promise(scopedSelectors)

getCss

Returns the CSS from scopeify

scopeifyHtml.getCss(scopedSelectors) => string

insertCss

Inserts CSS into document

scopeify.insertCss(css: string, doc: Document) => undefined

Perf

All speeds are measured in milliseconds (ms).

fixture | scopeify-html | juice v4 | ---------------|---------------|------------| zillow.html | 43.316 | 81.557 | gog.html | 126.074 | 55.336 | readme_ex.html | 1.301 | 1.240 | apple.html | 114.198 | 26.452 | costco.html | 1.623 | 0.654 |