Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

inline-style-parser

remarkablemark38mMIT0.2.4TypeScript support: included

An inline style parser.

inline-style-parser, inline-style, style, parser, css

readme

inline-style-parser

NPM

NPM version Bundlephobia minified + gzip build codecov NPM downloads

Inline style parser copied from css/lib/parse/index.js:

InlineStyleParser(string)

Example:

const parse = require('inline-style-parser');

parse('color: #BADA55;');

Output:

[ { type: 'declaration',
    property: 'color',
    value: '#BADA55',
    position: Position { start: [Object], end: [Object], source: undefined } } ]

JSFiddle | Replit | Examples

Installation

NPM:

npm install inline-style-parser --save

Yarn:

yarn add inline-style-parser

CDN:

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

Usage

Import with ES Modules:

import parse from 'inline-style-parser';

Or require with CommonJS:

const parse = require('inline-style-parser');

Parse single declaration:

parse('left: 0');

Output:

[
  {
    type: 'declaration',
    property: 'left',
    value: '0',
    position: {
      start: { line: 1, column: 1 },
      end: { line: 1, column: 8 },
      source: undefined
    }
  }
]

Parse multiple declarations:

parse('left: 0; right: 100px;');

Output:

[
  {
    type: 'declaration',
    property: 'left',
    value: '0',
    position: {
      start: { line: 1, column: 1 },
      end: { line: 1, column: 8 },
      source: undefined
    }
  },
  {
    type: 'declaration',
    property: 'right',
    value: '100px',
    position: {
      start: { line: 1, column: 10 },
      end: { line: 1, column: 22 },
      source: undefined
    }
  }
]

Parse declaration with missing value:

parse('top:');

Output:

[
  {
    type: 'declaration',
    property: 'top',
    value: '',
    position: {
      start: { line: 1, column: 1 },
      end: { line: 1, column: 5 },
      source: undefined
    }
  }
]

Parse unknown declaration:

parse('answer: 42;');

Output:

[
  {
    type: 'declaration',
    property: 'answer',
    value: '42',
    position: {
      start: { line: 1, column: 1 },
      end: { line: 1, column: 11 },
      source: undefined
    }
  }
]

Invalid declarations:

parse('');      // []
parse();        // throws TypeError
parse(1);       // throws TypeError
parse('width'); // throws Error
parse('/*');    // throws Error

Testing

Run tests:

npm test

Run tests in watch mode:

npm run test:watch

Run tests with coverage:

npm run test:coverage

Run tests in CI mode:

npm run test:ci

Lint files:

npm run lint

Fix lint errors:

npm run lint:fix

Release

Release and publish are automated by Release Please.

License

MIT. See the license from the original project.

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

0.2.4 (2024-08-29)

Continuous Integration

  • github: publish package to npm registry with provenance (0b0e756)

0.2.3 (2024-03-26)

Documentation

0.2.2 (2023-10-15)

Bug Fixes

  • index: export types Declaration and Comment (5500e24)

0.2.1 (2023-10-15)

Bug Fixes

  • package: add index.d.ts to "files" in package.json (bac1b29)

0.2.0 (2023-10-15)

Features

  • index: create type declaration file index.d.ts (fc0a669)

0.1.2 (2023-10-14)

Build System

  • rollup: upgrade rollup, replace plugins, and generate sourcemap (090cfad)

0.1.1 (2019-06-20)

Build System

  • package: fix whitelisting of /dist in "files" field (2c13b2f)

0.1.0 (2019-06-19)

Bug Fixes

  • index: do not throw an error if a comment precedes the colon (7f962ee)

Build System

  • package: add build and clean scripts (d27a653)
  • package: add script prepublishOnly and "files" field (5fad9ff)
  • package: save `css@2.2.4` to devDependencies (93ad729)
  • package: save devDependencies for rollup and its plugins (872b1fa)
  • package: set NODE_ENV=development in script build:unmin (5a7877b)
  • package: update build:min to generate sourcemap (external) (c81d66a)
  • rollup: add rollup.config.js (ac60124)

Features

  • clone project from npm-package-template (5976c6f)
  • index: copy parse module from css package (3bf4bee)
  • index: parse only declarations and remove all unused code (a04d918)
  • index: throw error if first argument is not a string (346ae28)

Tests

  • add snapshot for the parsed output of a single declaration (c2c774c)
  • data: add more cases for 'content' and 'background-image' (204c574)
  • index: add more misc and one-off test cases (a08f521)
  • index: check that a comment before colon is parsed correctly (bf9518c)
  • index: check that the error message matches (9169525)
  • add snapshots for the parsed output of multiple declarations (8708031)
  • index: disable placeholder test suite (20bf8af)
  • add cases and compare parser output with css.parse output (361974b)
  • index: refactor tests and use expect and it.each (dbf2ef0)
  • organize tests with describe blocks and tidy test names (5c5fcd4)
  • replace mocha and nyc with jest (100311d)
  • index: test "End of comment missing" error and silent option (9f0c832)
  • index: verify that expected errors are thrown (d0ed3bd)
  • package: collect coverage from index.js only (bc503b7)