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

splattercss

v1.0.2

Published

Tiny CSS lib to distribute inline styles

Downloads

5

Readme

Splatter Css 🥗

JS library to split and splatter your CSS rules into the style attribute of each related DOM element.

The idea first came as a trial for an alternative to ShadyCss while waiting for fully ShadowDom v1 support.

Install


npm install splattercss
// browser
<script src="./node_modules/build/splattercss.min.js"></script>

// ES import
import SplatterCss from 'splattercss/index'

// Node
// NOTE: you might need some DOM enabler
// like https://www.npmjs.com/package/global-jsdom
const SplatterCss = require('splattercss')

Usage


<div id="wrapper">
  <p class="red"></p>
  <p class="green"></p>
</div>
import SplatterCss from 'splattercss'
// Get HTML and pass it as String
// together with the styles to apply
const node = document.getElementById('wrapper')
const _styledHtml = SplatterCss.mesh(`
  .red { color: red; }
  .green { color: green; }
`, node.innerHTML)
// Update with the new HTML
node.innerHTML = _styledHtml

Result

<div id="wrapper">
  <p class="red" style="color: red;"></p>
  <p class="green" style="color: green;"></p>
</div>

API


.mesh() splatters the CSS in the HTML provided and returns the updated HTML as String. It will parse the CSS and distributes the styles in the HTML by matching the CSS selectors. It's combination of the two APIs described below.

Example

const newHTML = SplatterCss.mesh(`
  .red { color: red; }
  .green { color: green; }
`,`
  <p class="red"></p>
  <p class="green"></p>
`)

// newHTML = `
//  <p class="red" style="color: red;"></p>
//  <p class="green" style="color: green;"></p>
// `

.parse() will parse a CSS string and build an array of Objects

// @return
[ { selector:String, rules:String }, ... ]

Note It's not a linter tool. Wrong syntax CSS will be parsed anyways with no rising Exceptions. Eventually, with the possibility of loosing some rules on the way.

Example

const cssArr = SplatterCss.parse(`
  .red { color: red; }
  .green { color: green; }
`)

// cssArr = [
//   {
//     selector: '.red',
//     rules: 'color: red;'
//   },
//   {
//     selector: '.green',
//     rules: 'color: green;'
//   }
// ]

.inject() intermediary function that accepts the Array result of .parse() and the DOM as a String. It will injects the styles defined in the given DOM string and return the modified string.

Example

const cssArr = [
  {
    selector: '.red',
    rules: 'color: red;'
  },
  {
    selector: '.green',
    rules: 'color: green;'
  }
]
const newHTML = SplatterCss.inject(cssArr, `
  <p class="red"></p>
  <p class="green"></p>
`)

// newHTML = `
//   <p class="red" style="color: red;"></p>
//   <p class="green" style="color: green;"></p>
// `

License


MIT License (MIT)

Contributing


Feel free to contribute and submit an issue or a pull request for any bug or enhancements.