UNPKG

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