UNPKG

4.22 kBMarkdownView Raw
1# inline-style-parser
2
3[![NPM](https://nodei.co/npm/inline-style-parser.png)](https://nodei.co/npm/inline-style-parser/)
4
5[![NPM version](https://badgen.net/npm/v/inline-style-parser)](https://www.npmjs.com/package/inline-style-parser)
6[![Bundlephobia minified + gzip](https://badgen.net/bundlephobia/minzip/inline-style-parser)](https://bundlephobia.com/package/inline-style-parser)
7[![build](https://github.com/remarkablemark/inline-style-parser/actions/workflows/build.yml/badge.svg)](https://github.com/remarkablemark/inline-style-parser/actions/workflows/build.yml)
8[![codecov](https://codecov.io/gh/remarkablemark/inline-style-parser/branch/master/graph/badge.svg?token=B8EEK5709W)](https://codecov.io/gh/remarkablemark/inline-style-parser)
9[![NPM downloads](https://badgen.net/npm/dm/inline-style-parser)](https://www.npmjs.com/package/inline-style-parser)
10
11Inline style parser copied from [`css/lib/parse/index.js`](https://github.com/reworkcss/css/blob/v2.2.4/lib/parse/index.js):
12
13```
14InlineStyleParser(string)
15```
16
17Example:
18
19```js
20const parse = require('inline-style-parser');
21
22parse('color: #BADA55;');
23```
24
25Output:
26
27```js
28[ { type: 'declaration',
29 property: 'color',
30 value: '#BADA55',
31 position: Position { start: [Object], end: [Object], source: undefined } } ]
32```
33
34[JSFiddle](https://jsfiddle.net/remarkablemark/hcxbpwq8/) | [Replit](https://replit.com/@remarkablemark/inline-style-parser) | [Examples](https://github.com/remarkablemark/inline-style-parser/tree/master/examples)
35
36## Installation
37
38[NPM](https://www.npmjs.com/package/inline-style-parser):
39
40```sh
41npm install inline-style-parser --save
42```
43
44[Yarn](https://yarnpkg.com/package/inline-style-parser):
45
46```sh
47yarn add inline-style-parser
48```
49
50[CDN](https://unpkg.com/inline-style-parser/):
51
52```html
53<script src="https://unpkg.com/inline-style-parser@latest/dist/inline-style-parser.min.js"></script>
54<script>
55 window.InlineStyleParser(/* string */);
56</script>
57```
58
59## Usage
60
61Import with ES Modules:
62
63```js
64import parse from 'inline-style-parser';
65```
66
67Or require with CommonJS:
68
69```js
70const parse = require('inline-style-parser');
71```
72
73Parse single declaration:
74
75```js
76parse('left: 0');
77```
78
79Output:
80
81```js
82[
83 {
84 type: 'declaration',
85 property: 'left',
86 value: '0',
87 position: {
88 start: { line: 1, column: 1 },
89 end: { line: 1, column: 8 },
90 source: undefined
91 }
92 }
93]
94```
95
96Parse multiple declarations:
97
98```js
99parse('left: 0; right: 100px;');
100```
101
102Output:
103
104```js
105[
106 {
107 type: 'declaration',
108 property: 'left',
109 value: '0',
110 position: {
111 start: { line: 1, column: 1 },
112 end: { line: 1, column: 8 },
113 source: undefined
114 }
115 },
116 {
117 type: 'declaration',
118 property: 'right',
119 value: '100px',
120 position: {
121 start: { line: 1, column: 10 },
122 end: { line: 1, column: 22 },
123 source: undefined
124 }
125 }
126]
127```
128
129Parse declaration with missing value:
130
131```js
132parse('top:');
133```
134
135Output:
136
137```js
138[
139 {
140 type: 'declaration',
141 property: 'top',
142 value: '',
143 position: {
144 start: { line: 1, column: 1 },
145 end: { line: 1, column: 5 },
146 source: undefined
147 }
148 }
149]
150```
151
152Parse unknown declaration:
153
154```js
155parse('answer: 42;');
156```
157
158Output:
159
160```js
161[
162 {
163 type: 'declaration',
164 property: 'answer',
165 value: '42',
166 position: {
167 start: { line: 1, column: 1 },
168 end: { line: 1, column: 11 },
169 source: undefined
170 }
171 }
172]
173```
174
175Invalid declarations:
176
177```js
178parse(''); // []
179parse(); // throws TypeError
180parse(1); // throws TypeError
181parse('width'); // throws Error
182parse('/*'); // throws Error
183```
184
185## Testing
186
187Run tests:
188
189```sh
190npm test
191```
192
193Run tests in watch mode:
194
195```sh
196npm run test:watch
197```
198
199Run tests with coverage:
200
201```sh
202npm run test:coverage
203```
204
205Run tests in CI mode:
206
207```sh
208npm run test:ci
209```
210
211Lint files:
212
213```sh
214npm run lint
215```
216
217Fix lint errors:
218
219```sh
220npm run lint:fix
221```
222
223## Release
224
225Release and publish are automated by [Release Please](https://github.com/googleapis/release-please).
226
227## License
228
229[MIT](https://github.com/remarkablemark/inline-style-parser/blob/master/LICENSE). See the [license](https://github.com/reworkcss/css/blob/v2.2.4/LICENSE) from the original project.