Giter VIP home page Giter VIP logo

style-to-object's Introduction

style-to-object

NPM

NPM version Bundlephobia minified + gzip build codecov NPM downloads

Parse CSS inline style to JavaScript object:

import parse from 'style-to-object';

parse('color: #C0FFEE; background: #BADA55;');

Output:

{ color: '#C0FFEE', background: '#BADA55' }

JSFiddle | Replit | Examples

Installation

NPM:

npm install style-to-object --save

Yarn:

yarn add style-to-object

CDN:

<script src="https://unpkg.com/style-to-object@latest/dist/style-to-object.min.js"></script>
<script>
  window.StyleToObject(/* string */);
</script>

Usage

Import with ES Modules:

import parse from 'style-to-object';

Require with CommonJS:

const parse = require('style-to-object').default;

Parse single declaration:

parse('line-height: 42');

Output:

{ 'line-height': '42' }

Parse multiple declarations:

parse(`
  border-color: #ACE;
  z-index: 1337;
`);

Output:

{ 'border-color': '#ACE', 'z-index': '1337' }

Parse unknown declarations:

parse('answer: 42;');

Output:

{ 'answer': '42' }

Invalid declarations/arguments:

parse(`
  top: ;
  right: 1em;
`); // { right: '1em' }

parse();        // null
parse(null);    // null
parse(1);       // null
parse(true);    // null
parse('top:');  // null
parse(':12px'); // null
parse(':');     // null
parse(';');     // null

parse('top'); // throws Error
parse('/*');  // throws Error

Iterator

If the 2nd argument is a function, then the parser will return null:

parse('color: #f00', () => {}); // null

But the function will iterate through each declaration:

parse('color: #f00', (name, value, declaration) => {
  console.log(name);        // 'color'
  console.log(value);       // '#f00'
  console.log(declaration); // { type: 'declaration', property: 'color', value: '#f00' }
});

This makes it easy to customize the output:

const style = `
  color: red;
  background: blue;
`;
const output = [];

function iterator(name, value) {
  output.push([name, value]);
}

parse(style, iterator);
console.log(output); // [['color', 'red'], ['background', 'blue']]

Migration

v1

Migrated to TypeScript. Iterator excludes Comment. CommonJS requires the .default key:

const parse = require('style-to-object').default;

Release

Release and publish are automated by Release Please.

Special Thanks

License

MIT

style-to-object's People

Contributors

christianmurphy avatar dependabot[bot] avatar github-actions[bot] avatar julienpradet avatar mergify[bot] avatar moonrailgun avatar remarkablemark avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

style-to-object's Issues

Dynamic require of "inline-style-parser" is not supported

Expected Behavior

inline-style-parser should not be dynamically required if using ESM

Actual Behavior

ESM module is importing from CSM which results in dynamic require of inline-style-parser

Steps to Reproduce

  1. install react-markdown@^8.0.4
  2. bundle react-markdown
  3. run your app that uses ESM
  4. observe error

Reproducible Demo

Environment

  • Version: >=0.4.4<=1.0.5
  • Platform: Web
  • Browser: Edge
  • OS: Win32

Keywords

Move to ESM

Hello,

a few days ago Node.js 10 reached it's EOL so it might be worth converting this dependency to ES Modules (node.js docs). LTS 12.x and 14.x will still work.

ECMAScript modules are the official standard format to package JavaScript code for reuse. Modules are defined using a variety of import and export statements.

Step-by-step guide of migrating to ESM is here: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

I will make a PR soon ๐Ÿ˜ƒ

"Uncaught ReferenceError: exports is not defined" when loading script as UMD

Expected Behavior

Load UMD script without errors

Actual Behavior

UMD script throws error:

Uncaught ReferenceError: exports is not defined

Steps to Reproduce

Load script in webpage:

<script src="https://unpkg.com/style-to-object/dist/style-to-object.js"></script>

Reproducible Demo

https://jsfiddle.net/remarkablemark/y3kq07es/

Environment

  • Version: 0.4.1
  • Platform:
  • Browser: Chrome
  • OS: macOS

Keywords

umd, library, dist, error, build

Error๏ผšโ€œproperty missing ':'โ€ when I use background and svg together

Expected Behavior

no Error, parse success

Actual Behavior

An error exception has occurred

Error: undefined:1:1675: property missing ':'
at m (index.js:103:15)
at A (index.js:208:37)
at index.js:239:20
at c (index.js:250:10)
at l (index.js:21:22)
at ?editor_console=true:126:16

Steps to Reproduce

window.StyleToObject(`display: block; width: 100px; background: url(&quot;data:image/svg+xml,%3C%3Fxml version='1.0' encoding='UTF-8'%3F%3E%3Csvg width='80px' height='80px' viewBox='0 0 80 80' version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'%3E%3Ctitle%3E9.%E5%85%83%E7%B4%A0/%E5%8A%A0%E8%BD%BD/Black%3C/title%3E%3Cdefs%3E%3ClinearGradient x1='94.0869141%25' y1='0%25' x2='94.0869141%25' y2='90.559082%25' id='linearGradient-1'%3E%3Cstop stop-color='%23606060' stop-opacity='0' offset='0%25'%3E%3C/stop%3E%3Cstop stop-color='%23606060' stop-opacity='0.3' offset='100%25'%3E%3C/stop%3E%3C/linearGradient%3E%3ClinearGradient x1='100%25' y1='8.67370605%25' x2='100%25' y2='90.6286621%25' id='linearGradient-2'%3E%3Cstop stop-color='%23606060' offset='0%25'%3E%3C/stop%3E%3Cstop stop-color='%23606060' stop-opacity='0.3' offset='100%25'%3E%3C/stop%3E%3C/linearGradient%3E%3C/defs%3E%3Cg id='%E9%A1%B5%E9%9D%A2-1' stroke='none' stroke-width='1' fill='none' fill-rule='evenodd' opacity='0.9'%3E%3Cg id='LoadingDefault'%3E%3Cpath d='M40,0 C62.09139,0 80,17.90861 80,40 C80,62.09139 62.09139,80 40,80 L40,73 C58.2253967,73 73,58.2253967 73,40 C73,21.7746033 58.2253967,7 40,7 L40,0 Z' id='%E8%B7%AF%E5%BE%84' fill='url(%23linearGradient-1)'%3E%3C/path%3E%3Cpath d='M40,0 L40,7 C21.7746033,7 7,21.7746033 7,40 C7,58.2253967 21.7746033,73 40,73 L40,80 C17.90861,80 0,62.09139 0,40 C0,17.90861 17.90861,0 40,0 Z' id='%E8%B7%AF%E5%BE%84' fill='url(%23linearGradient-2)'%3E%3C/path%3E%3Ccircle id='Oval' fill='%23606060' cx='40.5' cy='3.5' r='3.5'%3E%3C/circle%3E%3C/g%3E%3CanimateTransform attributeName='transform' begin='0s' dur='1s' type='rotate' values='0 40 40;360 40 40' repeatCount='indefinite'/%3E%3C/g%3E%3C/svg%3E%0A&quot;) 50% 50% / 16px no-repeat scroll padding-box border-box rgb(32, 32, 32); height: 100px;`)

Reference demo

Reproducible Demo

https://jsfiddle.net/t6bkepf9/10/

Environment

  • Version: 0.4.1
  • Platform: web
  • Browser: chrome
  • OS: mac os

Keywords

ESM types are wrong

Expected Behavior

Be able to use the default normally from ESM.

Actual Behavior

The CJS types are specified for ESM users.
But the CJS types are for CJS, not for ESM.

Steps to Reproduce

Should be possible to reproduce from any ESM file.

Reproducible Demo

syntax-tree/hast-util-to-jsx-runtime@89b9292

Environment

n/a

Keywords

Extra

a) you can drop the root types field in package.json
b) generate an esm/index.d.mts
c) you can drop the exports.types fields, they are the defaults for cjs/index.js -> cjs/index.d.ts, esm/index.mjs -> esm/index.d.mts

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.