1 | # inline-style-parser
|
2 |
|
3 | [](https://nodei.co/npm/inline-style-parser/)
|
4 |
|
5 | [](https://www.npmjs.com/package/inline-style-parser)
|
6 | [](https://travis-ci.org/remarkablemark/inline-style-parser)
|
7 | [](https://coveralls.io/github/remarkablemark/inline-style-parser?branch=master)
|
8 |
|
9 | An 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 | ```
|
12 | InlineStyleParser(string)
|
13 | ```
|
14 |
|
15 | Example:
|
16 |
|
17 | ```js
|
18 | var parse = require('inline-style-parser');
|
19 | parse('color: #BADA55;');
|
20 | ```
|
21 |
|
22 | Output:
|
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 |
|
33 | See [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 |
|
60 | Import the module:
|
61 |
|
62 | ```js
|
63 | // CommonJS
|
64 | const parse = require('inline-style-parser');
|
65 |
|
66 | // ES Modules
|
67 | import parse from 'inline-style-parser';
|
68 | ```
|
69 |
|
70 | Parse single declaration:
|
71 |
|
72 | ```js
|
73 | parse('left: 0');
|
74 | ```
|
75 |
|
76 | Output:
|
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 |
|
93 | Parse multiple declarations:
|
94 |
|
95 | ```js
|
96 | parse('left: 0; right: 100px;');
|
97 | ```
|
98 |
|
99 | Output:
|
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 |
|
126 | Parse declaration with missing value:
|
127 |
|
128 | ```js
|
129 | parse('top:');
|
130 | ```
|
131 |
|
132 | Output:
|
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 |
|
149 | Parse unknown declaration:
|
150 |
|
151 | ```js
|
152 | parse('answer: 42;');
|
153 | ```
|
154 |
|
155 | Output:
|
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 |
|
172 | Invalid declarations:
|
173 |
|
174 | ```js
|
175 | parse(''); // []
|
176 | parse(); // throws TypeError
|
177 | parse(1); // throws TypeError
|
178 | parse('width'); // throws Error
|
179 | parse('/*'); // throws Error
|
180 | ```
|
181 |
|
182 | ## Testing
|
183 |
|
184 | Run tests:
|
185 |
|
186 | ```sh
|
187 | $ npm test
|
188 | ```
|
189 |
|
190 | Run tests in watch mode:
|
191 |
|
192 | ```sh
|
193 | $ npm run test:watch
|
194 | ```
|
195 |
|
196 | Run tests with coverage:
|
197 |
|
198 | ```sh
|
199 | $ npm run test:coverage
|
200 | ```
|
201 |
|
202 | Run tests in CI mode:
|
203 |
|
204 | ```sh
|
205 | $ npm run test:ci
|
206 | ```
|
207 |
|
208 | Lint files:
|
209 |
|
210 | ```sh
|
211 | $ npm run lint
|
212 | ```
|
213 |
|
214 | Fix lint errors:
|
215 |
|
216 | ```sh
|
217 | $ npm run lint:fix
|
218 | ```
|
219 |
|
220 | ## Release
|
221 |
|
222 | Only 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 |
|
231 | MIT. See [license](https://github.com/reworkcss/css/blob/v2.2.4/LICENSE) from original project.
|