style-parser

css style parser in Node.js

Usage no npm install needed!

<script type="module">
  import styleParser from 'https://cdn.skypack.dev/style-parser';
</script>

README

style-parser

NPM version Build status Dependency Status Test coverage Downloads

A Node.js library to parse the inline css styles.

Tutorial

const parse = require('style-parser');
parse('font-size:13px;');

Use cases

Here we would list possible use cases that this library might be used.

Working with cherrio

cherrio is server-side jQuery implementation that scraper would use, but the jQuery/cherrio API doesn't provide the ability to get structured style object from the style attribute like the below example:

The HTML:

<div style="font-size:13px;width:50px"></div>

And the cherrio code:

cherrio(html).attr('style');
// this will only return in string

So with the library style-parser, we can do:

const parse = require('style-parser')
parse(cherrio(html).attr('style'));
// this will return an object: {'font-size': '13px', 'width': '50px'}

Working on browsers

Because the only dependency are Parsimmon which is in pure JavaScript, that you are able to build it for browser environments and make it work with browser-side jQuery or other useful lands.

Installation

$ npm install style-parser --save

License

MIT