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 |
|
11 | Inline style parser copied from [`css/lib/parse/index.js`](https://github.com/reworkcss/css/blob/v2.2.4/lib/parse/index.js):
|
12 |
|
13 | ```
|
14 | InlineStyleParser(string)
|
15 | ```
|
16 |
|
17 | Example:
|
18 |
|
19 | ```js
|
20 | const parse = require('inline-style-parser');
|
21 |
|
22 | parse('color: #BADA55;');
|
23 | ```
|
24 |
|
25 | Output:
|
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
|
41 | npm install inline-style-parser --save
|
42 | ```
|
43 |
|
44 | [Yarn](https://yarnpkg.com/package/inline-style-parser):
|
45 |
|
46 | ```sh
|
47 | yarn 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 |
|
61 | Import with ES Modules:
|
62 |
|
63 | ```js
|
64 | import parse from 'inline-style-parser';
|
65 | ```
|
66 |
|
67 | Or require with CommonJS:
|
68 |
|
69 | ```js
|
70 | const parse = require('inline-style-parser');
|
71 | ```
|
72 |
|
73 | Parse single declaration:
|
74 |
|
75 | ```js
|
76 | parse('left: 0');
|
77 | ```
|
78 |
|
79 | Output:
|
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 |
|
96 | Parse multiple declarations:
|
97 |
|
98 | ```js
|
99 | parse('left: 0; right: 100px;');
|
100 | ```
|
101 |
|
102 | Output:
|
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 |
|
129 | Parse declaration with missing value:
|
130 |
|
131 | ```js
|
132 | parse('top:');
|
133 | ```
|
134 |
|
135 | Output:
|
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 |
|
152 | Parse unknown declaration:
|
153 |
|
154 | ```js
|
155 | parse('answer: 42;');
|
156 | ```
|
157 |
|
158 | Output:
|
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 |
|
175 | Invalid declarations:
|
176 |
|
177 | ```js
|
178 | parse(''); // []
|
179 | parse(); // throws TypeError
|
180 | parse(1); // throws TypeError
|
181 | parse('width'); // throws Error
|
182 | parse('/*'); // throws Error
|
183 | ```
|
184 |
|
185 | ## Testing
|
186 |
|
187 | Run tests:
|
188 |
|
189 | ```sh
|
190 | npm test
|
191 | ```
|
192 |
|
193 | Run tests in watch mode:
|
194 |
|
195 | ```sh
|
196 | npm run test:watch
|
197 | ```
|
198 |
|
199 | Run tests with coverage:
|
200 |
|
201 | ```sh
|
202 | npm run test:coverage
|
203 | ```
|
204 |
|
205 | Run tests in CI mode:
|
206 |
|
207 | ```sh
|
208 | npm run test:ci
|
209 | ```
|
210 |
|
211 | Lint files:
|
212 |
|
213 | ```sh
|
214 | npm run lint
|
215 | ```
|
216 |
|
217 | Fix lint errors:
|
218 |
|
219 | ```sh
|
220 | npm run lint:fix
|
221 | ```
|
222 |
|
223 | ## Release
|
224 |
|
225 | Release 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.
|