style-to-object 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ <a name="0.2.2"></a>
6
+ ## [0.2.2](https://github.com/remarkablemark/style-to-object/compare/v0.2.1...v0.2.2) (2018-09-13)
7
+
8
+
9
+
5
10
  <a name="0.2.1"></a>
6
11
  ## [0.2.1](https://github.com/remarkablemark/style-to-object/compare/v0.2.0...v0.2.1) (2018-05-09)
7
12
 
package/README.md CHANGED
@@ -15,7 +15,7 @@ parser('color: #C0FFEE; background: #BADA55;');
15
15
  // { color: "#C0FFEE", background: "#BADA55" }
16
16
  ```
17
17
 
18
- [JSFiddle](https://jsfiddle.net/remarkablemark/ykz2meot/)
18
+ [JSFiddle](https://jsfiddle.net/remarkablemark/ykz2meot/) | [repl.it](https://repl.it/@remarkablemark/style-to-object)
19
19
 
20
20
  ## Installation
21
21
 
@@ -143,6 +143,7 @@ $ git push --follow-tags
143
143
  ## Special Thanks
144
144
 
145
145
  - [css](https://github.com/reworkcss/css)
146
+ - [Contributors](https://github.com/remarkablemark/style-to-object/graphs/contributors)
146
147
 
147
148
  ## License
148
149
 
@@ -1,648 +1,648 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
- typeof define === 'function' && define.amd ? define(factory) :
4
- (global.StyleToObject = factory());
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
+ typeof define === 'function' && define.amd ? define(factory) :
4
+ (global.StyleToObject = factory());
5
5
  }(this, (function () { 'use strict';
6
6
 
7
- // http://www.w3.org/TR/CSS21/grammar.html
8
- // https://github.com/visionmedia/css-parse/pull/49#issuecomment-30088027
9
- var commentre = /\/\*[^*]*\*+([^/*][^*]*\*+)*\//g;
7
+ // http://www.w3.org/TR/CSS21/grammar.html
8
+ // https://github.com/visionmedia/css-parse/pull/49#issuecomment-30088027
9
+ var commentre = /\/\*[^*]*\*+([^/*][^*]*\*+)*\//g;
10
10
 
11
- var parse = function(css, options){
12
- options = options || {};
11
+ var parse = function(css, options){
12
+ options = options || {};
13
13
 
14
- /**
15
- * Positional.
16
- */
14
+ /**
15
+ * Positional.
16
+ */
17
17
 
18
- var lineno = 1;
19
- var column = 1;
18
+ var lineno = 1;
19
+ var column = 1;
20
20
 
21
- /**
22
- * Update lineno and column based on `str`.
23
- */
21
+ /**
22
+ * Update lineno and column based on `str`.
23
+ */
24
24
 
25
- function updatePosition(str) {
26
- var lines = str.match(/\n/g);
27
- if (lines) lineno += lines.length;
28
- var i = str.lastIndexOf('\n');
29
- column = ~i ? str.length - i : column + str.length;
30
- }
25
+ function updatePosition(str) {
26
+ var lines = str.match(/\n/g);
27
+ if (lines) lineno += lines.length;
28
+ var i = str.lastIndexOf('\n');
29
+ column = ~i ? str.length - i : column + str.length;
30
+ }
31
31
 
32
- /**
33
- * Mark position and patch `node.position`.
34
- */
32
+ /**
33
+ * Mark position and patch `node.position`.
34
+ */
35
+
36
+ function position() {
37
+ var start = { line: lineno, column: column };
38
+ return function(node){
39
+ node.position = new Position(start);
40
+ whitespace();
41
+ return node;
42
+ };
43
+ }
35
44
 
36
- function position() {
37
- var start = { line: lineno, column: column };
38
- return function(node){
39
- node.position = new Position(start);
40
- whitespace();
41
- return node;
42
- };
43
- }
45
+ /**
46
+ * Store position information for a node
47
+ */
44
48
 
45
- /**
46
- * Store position information for a node
47
- */
49
+ function Position(start) {
50
+ this.start = start;
51
+ this.end = { line: lineno, column: column };
52
+ this.source = options.source;
53
+ }
48
54
 
49
- function Position(start) {
50
- this.start = start;
51
- this.end = { line: lineno, column: column };
52
- this.source = options.source;
53
- }
55
+ /**
56
+ * Non-enumerable source string
57
+ */
54
58
 
55
- /**
56
- * Non-enumerable source string
57
- */
59
+ Position.prototype.content = css;
58
60
 
59
- Position.prototype.content = css;
61
+ /**
62
+ * Error `msg`.
63
+ */
60
64
 
61
- /**
62
- * Error `msg`.
63
- */
64
-
65
- var errorsList = [];
65
+ var errorsList = [];
66
66
 
67
- function error(msg) {
68
- var err = new Error(options.source + ':' + lineno + ':' + column + ': ' + msg);
69
- err.reason = msg;
70
- err.filename = options.source;
71
- err.line = lineno;
72
- err.column = column;
73
- err.source = css;
67
+ function error(msg) {
68
+ var err = new Error(options.source + ':' + lineno + ':' + column + ': ' + msg);
69
+ err.reason = msg;
70
+ err.filename = options.source;
71
+ err.line = lineno;
72
+ err.column = column;
73
+ err.source = css;
74
74
 
75
- if (options.silent) {
76
- errorsList.push(err);
77
- } else {
78
- throw err;
75
+ if (options.silent) {
76
+ errorsList.push(err);
77
+ } else {
78
+ throw err;
79
+ }
79
80
  }
80
- }
81
81
 
82
- /**
83
- * Parse stylesheet.
84
- */
85
-
86
- function stylesheet() {
87
- var rulesList = rules();
82
+ /**
83
+ * Parse stylesheet.
84
+ */
85
+
86
+ function stylesheet() {
87
+ var rulesList = rules();
88
+
89
+ return {
90
+ type: 'stylesheet',
91
+ stylesheet: {
92
+ source: options.source,
93
+ rules: rulesList,
94
+ parsingErrors: errorsList
95
+ }
96
+ };
97
+ }
88
98
 
89
- return {
90
- type: 'stylesheet',
91
- stylesheet: {
92
- source: options.source,
93
- rules: rulesList,
94
- parsingErrors: errorsList
95
- }
96
- };
97
- }
99
+ /**
100
+ * Opening brace.
101
+ */
98
102
 
99
- /**
100
- * Opening brace.
101
- */
102
-
103
- function open() {
104
- return match(/^{\s*/);
105
- }
103
+ function open() {
104
+ return match(/^{\s*/);
105
+ }
106
106
 
107
- /**
108
- * Closing brace.
109
- */
107
+ /**
108
+ * Closing brace.
109
+ */
110
110
 
111
- function close() {
112
- return match(/^}/);
113
- }
111
+ function close() {
112
+ return match(/^}/);
113
+ }
114
114
 
115
- /**
116
- * Parse ruleset.
117
- */
115
+ /**
116
+ * Parse ruleset.
117
+ */
118
118
 
119
- function rules() {
120
- var node;
121
- var rules = [];
122
- whitespace();
123
- comments(rules);
124
- while (css.length && css.charAt(0) != '}' && (node = atrule() || rule())) {
125
- if (node !== false) {
126
- rules.push(node);
127
- comments(rules);
119
+ function rules() {
120
+ var node;
121
+ var rules = [];
122
+ whitespace();
123
+ comments(rules);
124
+ while (css.length && css.charAt(0) != '}' && (node = atrule() || rule())) {
125
+ if (node !== false) {
126
+ rules.push(node);
127
+ comments(rules);
128
+ }
128
129
  }
130
+ return rules;
129
131
  }
130
- return rules;
131
- }
132
132
 
133
- /**
134
- * Match `re` and return captures.
135
- */
136
-
137
- function match(re) {
138
- var m = re.exec(css);
139
- if (!m) return;
140
- var str = m[0];
141
- updatePosition(str);
142
- css = css.slice(str.length);
143
- return m;
144
- }
133
+ /**
134
+ * Match `re` and return captures.
135
+ */
145
136
 
146
- /**
147
- * Parse whitespace.
148
- */
137
+ function match(re) {
138
+ var m = re.exec(css);
139
+ if (!m) return;
140
+ var str = m[0];
141
+ updatePosition(str);
142
+ css = css.slice(str.length);
143
+ return m;
144
+ }
149
145
 
150
- function whitespace() {
151
- match(/^\s*/);
152
- }
146
+ /**
147
+ * Parse whitespace.
148
+ */
153
149
 
154
- /**
155
- * Parse comments;
156
- */
150
+ function whitespace() {
151
+ match(/^\s*/);
152
+ }
157
153
 
158
- function comments(rules) {
159
- var c;
160
- rules = rules || [];
161
- while (c = comment()) {
162
- if (c !== false) {
163
- rules.push(c);
154
+ /**
155
+ * Parse comments;
156
+ */
157
+
158
+ function comments(rules) {
159
+ var c;
160
+ rules = rules || [];
161
+ while (c = comment()) {
162
+ if (c !== false) {
163
+ rules.push(c);
164
+ }
164
165
  }
166
+ return rules;
165
167
  }
166
- return rules;
167
- }
168
168
 
169
- /**
170
- * Parse comment.
171
- */
169
+ /**
170
+ * Parse comment.
171
+ */
172
172
 
173
- function comment() {
174
- var pos = position();
175
- if ('/' != css.charAt(0) || '*' != css.charAt(1)) return;
173
+ function comment() {
174
+ var pos = position();
175
+ if ('/' != css.charAt(0) || '*' != css.charAt(1)) return;
176
176
 
177
- var i = 2;
178
- while ("" != css.charAt(i) && ('*' != css.charAt(i) || '/' != css.charAt(i + 1))) ++i;
179
- i += 2;
177
+ var i = 2;
178
+ while ("" != css.charAt(i) && ('*' != css.charAt(i) || '/' != css.charAt(i + 1))) ++i;
179
+ i += 2;
180
180
 
181
- if ("" === css.charAt(i-1)) {
182
- return error('End of comment missing');
183
- }
181
+ if ("" === css.charAt(i-1)) {
182
+ return error('End of comment missing');
183
+ }
184
184
 
185
- var str = css.slice(2, i - 2);
186
- column += 2;
187
- updatePosition(str);
188
- css = css.slice(i);
189
- column += 2;
185
+ var str = css.slice(2, i - 2);
186
+ column += 2;
187
+ updatePosition(str);
188
+ css = css.slice(i);
189
+ column += 2;
190
190
 
191
- return pos({
192
- type: 'comment',
193
- comment: str
194
- });
195
- }
191
+ return pos({
192
+ type: 'comment',
193
+ comment: str
194
+ });
195
+ }
196
196
 
197
- /**
198
- * Parse selector.
199
- */
197
+ /**
198
+ * Parse selector.
199
+ */
200
200
 
201
- function selector() {
202
- var m = match(/^([^{]+)/);
203
- if (!m) return;
204
- /* @fix Remove all comments from selectors
205
- * http://ostermiller.org/findcomment.html */
206
- return trim(m[0])
207
- .replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/g, '')
208
- .replace(/"(?:\\"|[^"])*"|'(?:\\'|[^'])*'/g, function(m) {
209
- return m.replace(/,/g, '\u200C');
210
- })
211
- .split(/\s*(?![^(]*\)),\s*/)
212
- .map(function(s) {
213
- return s.replace(/\u200C/g, ',');
214
- });
215
- }
201
+ function selector() {
202
+ var m = match(/^([^{]+)/);
203
+ if (!m) return;
204
+ /* @fix Remove all comments from selectors
205
+ * http://ostermiller.org/findcomment.html */
206
+ return trim(m[0])
207
+ .replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/g, '')
208
+ .replace(/"(?:\\"|[^"])*"|'(?:\\'|[^'])*'/g, function(m) {
209
+ return m.replace(/,/g, '\u200C');
210
+ })
211
+ .split(/\s*(?![^(]*\)),\s*/)
212
+ .map(function(s) {
213
+ return s.replace(/\u200C/g, ',');
214
+ });
215
+ }
216
216
 
217
- /**
218
- * Parse declaration.
219
- */
217
+ /**
218
+ * Parse declaration.
219
+ */
220
220
 
221
- function declaration() {
222
- var pos = position();
221
+ function declaration() {
222
+ var pos = position();
223
223
 
224
- // prop
225
- var prop = match(/^(\*?[-#\/\*\\\w]+(\[[0-9a-z_-]+\])?)\s*/);
226
- if (!prop) return;
227
- prop = trim(prop[0]);
224
+ // prop
225
+ var prop = match(/^(\*?[-#\/\*\\\w]+(\[[0-9a-z_-]+\])?)\s*/);
226
+ if (!prop) return;
227
+ prop = trim(prop[0]);
228
228
 
229
- // :
230
- if (!match(/^:\s*/)) return error("property missing ':'");
229
+ // :
230
+ if (!match(/^:\s*/)) return error("property missing ':'");
231
231
 
232
- // val
233
- var val = match(/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^\)]*?\)|[^};])+)/);
232
+ // val
233
+ var val = match(/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^\)]*?\)|[^};])+)/);
234
234
 
235
- var ret = pos({
236
- type: 'declaration',
237
- property: prop.replace(commentre, ''),
238
- value: val ? trim(val[0]).replace(commentre, '') : ''
239
- });
235
+ var ret = pos({
236
+ type: 'declaration',
237
+ property: prop.replace(commentre, ''),
238
+ value: val ? trim(val[0]).replace(commentre, '') : ''
239
+ });
240
240
 
241
- // ;
242
- match(/^[;\s]*/);
241
+ // ;
242
+ match(/^[;\s]*/);
243
243
 
244
- return ret;
245
- }
244
+ return ret;
245
+ }
246
246
 
247
- /**
248
- * Parse declarations.
249
- */
247
+ /**
248
+ * Parse declarations.
249
+ */
250
250
 
251
- function declarations() {
252
- var decls = [];
251
+ function declarations() {
252
+ var decls = [];
253
253
 
254
- if (!open()) return error("missing '{'");
255
- comments(decls);
254
+ if (!open()) return error("missing '{'");
255
+ comments(decls);
256
256
 
257
- // declarations
258
- var decl;
259
- while (decl = declaration()) {
260
- if (decl !== false) {
261
- decls.push(decl);
262
- comments(decls);
257
+ // declarations
258
+ var decl;
259
+ while (decl = declaration()) {
260
+ if (decl !== false) {
261
+ decls.push(decl);
262
+ comments(decls);
263
+ }
263
264
  }
265
+
266
+ if (!close()) return error("missing '}'");
267
+ return decls;
264
268
  }
265
269
 
266
- if (!close()) return error("missing '}'");
267
- return decls;
268
- }
270
+ /**
271
+ * Parse keyframe.
272
+ */
269
273
 
270
- /**
271
- * Parse keyframe.
272
- */
274
+ function keyframe() {
275
+ var m;
276
+ var vals = [];
277
+ var pos = position();
278
+
279
+ while (m = match(/^((\d+\.\d+|\.\d+|\d+)%?|[a-z]+)\s*/)) {
280
+ vals.push(m[1]);
281
+ match(/^,\s*/);
282
+ }
273
283
 
274
- function keyframe() {
275
- var m;
276
- var vals = [];
277
- var pos = position();
284
+ if (!vals.length) return;
278
285
 
279
- while (m = match(/^((\d+\.\d+|\.\d+|\d+)%?|[a-z]+)\s*/)) {
280
- vals.push(m[1]);
281
- match(/^,\s*/);
286
+ return pos({
287
+ type: 'keyframe',
288
+ values: vals,
289
+ declarations: declarations()
290
+ });
282
291
  }
283
292
 
284
- if (!vals.length) return;
293
+ /**
294
+ * Parse keyframes.
295
+ */
285
296
 
286
- return pos({
287
- type: 'keyframe',
288
- values: vals,
289
- declarations: declarations()
290
- });
291
- }
297
+ function atkeyframes() {
298
+ var pos = position();
299
+ var m = match(/^@([-\w]+)?keyframes\s*/);
292
300
 
293
- /**
294
- * Parse keyframes.
295
- */
301
+ if (!m) return;
302
+ var vendor = m[1];
296
303
 
297
- function atkeyframes() {
298
- var pos = position();
299
- var m = match(/^@([-\w]+)?keyframes\s*/);
304
+ // identifier
305
+ var m = match(/^([-\w]+)\s*/);
306
+ if (!m) return error("@keyframes missing name");
307
+ var name = m[1];
300
308
 
301
- if (!m) return;
302
- var vendor = m[1];
309
+ if (!open()) return error("@keyframes missing '{'");
303
310
 
304
- // identifier
305
- var m = match(/^([-\w]+)\s*/);
306
- if (!m) return error("@keyframes missing name");
307
- var name = m[1];
311
+ var frame;
312
+ var frames = comments();
313
+ while (frame = keyframe()) {
314
+ frames.push(frame);
315
+ frames = frames.concat(comments());
316
+ }
308
317
 
309
- if (!open()) return error("@keyframes missing '{'");
318
+ if (!close()) return error("@keyframes missing '}'");
310
319
 
311
- var frame;
312
- var frames = comments();
313
- while (frame = keyframe()) {
314
- frames.push(frame);
315
- frames = frames.concat(comments());
320
+ return pos({
321
+ type: 'keyframes',
322
+ name: name,
323
+ vendor: vendor,
324
+ keyframes: frames
325
+ });
316
326
  }
317
327
 
318
- if (!close()) return error("@keyframes missing '}'");
328
+ /**
329
+ * Parse supports.
330
+ */
319
331
 
320
- return pos({
321
- type: 'keyframes',
322
- name: name,
323
- vendor: vendor,
324
- keyframes: frames
325
- });
326
- }
332
+ function atsupports() {
333
+ var pos = position();
334
+ var m = match(/^@supports *([^{]+)/);
327
335
 
328
- /**
329
- * Parse supports.
330
- */
336
+ if (!m) return;
337
+ var supports = trim(m[1]);
331
338
 
332
- function atsupports() {
333
- var pos = position();
334
- var m = match(/^@supports *([^{]+)/);
339
+ if (!open()) return error("@supports missing '{'");
335
340
 
336
- if (!m) return;
337
- var supports = trim(m[1]);
341
+ var style = comments().concat(rules());
338
342
 
339
- if (!open()) return error("@supports missing '{'");
343
+ if (!close()) return error("@supports missing '}'");
340
344
 
341
- var style = comments().concat(rules());
345
+ return pos({
346
+ type: 'supports',
347
+ supports: supports,
348
+ rules: style
349
+ });
350
+ }
342
351
 
343
- if (!close()) return error("@supports missing '}'");
352
+ /**
353
+ * Parse host.
354
+ */
344
355
 
345
- return pos({
346
- type: 'supports',
347
- supports: supports,
348
- rules: style
349
- });
350
- }
356
+ function athost() {
357
+ var pos = position();
358
+ var m = match(/^@host\s*/);
351
359
 
352
- /**
353
- * Parse host.
354
- */
360
+ if (!m) return;
355
361
 
356
- function athost() {
357
- var pos = position();
358
- var m = match(/^@host\s*/);
362
+ if (!open()) return error("@host missing '{'");
359
363
 
360
- if (!m) return;
364
+ var style = comments().concat(rules());
361
365
 
362
- if (!open()) return error("@host missing '{'");
366
+ if (!close()) return error("@host missing '}'");
363
367
 
364
- var style = comments().concat(rules());
368
+ return pos({
369
+ type: 'host',
370
+ rules: style
371
+ });
372
+ }
365
373
 
366
- if (!close()) return error("@host missing '}'");
374
+ /**
375
+ * Parse media.
376
+ */
367
377
 
368
- return pos({
369
- type: 'host',
370
- rules: style
371
- });
372
- }
378
+ function atmedia() {
379
+ var pos = position();
380
+ var m = match(/^@media *([^{]+)/);
373
381
 
374
- /**
375
- * Parse media.
376
- */
382
+ if (!m) return;
383
+ var media = trim(m[1]);
377
384
 
378
- function atmedia() {
379
- var pos = position();
380
- var m = match(/^@media *([^{]+)/);
385
+ if (!open()) return error("@media missing '{'");
381
386
 
382
- if (!m) return;
383
- var media = trim(m[1]);
387
+ var style = comments().concat(rules());
384
388
 
385
- if (!open()) return error("@media missing '{'");
389
+ if (!close()) return error("@media missing '}'");
386
390
 
387
- var style = comments().concat(rules());
391
+ return pos({
392
+ type: 'media',
393
+ media: media,
394
+ rules: style
395
+ });
396
+ }
388
397
 
389
- if (!close()) return error("@media missing '}'");
390
398
 
391
- return pos({
392
- type: 'media',
393
- media: media,
394
- rules: style
395
- });
396
- }
399
+ /**
400
+ * Parse custom-media.
401
+ */
397
402
 
403
+ function atcustommedia() {
404
+ var pos = position();
405
+ var m = match(/^@custom-media\s+(--[^\s]+)\s*([^{;]+);/);
406
+ if (!m) return;
398
407
 
399
- /**
400
- * Parse custom-media.
401
- */
408
+ return pos({
409
+ type: 'custom-media',
410
+ name: trim(m[1]),
411
+ media: trim(m[2])
412
+ });
413
+ }
402
414
 
403
- function atcustommedia() {
404
- var pos = position();
405
- var m = match(/^@custom-media\s+(--[^\s]+)\s*([^{;]+);/);
406
- if (!m) return;
415
+ /**
416
+ * Parse paged media.
417
+ */
407
418
 
408
- return pos({
409
- type: 'custom-media',
410
- name: trim(m[1]),
411
- media: trim(m[2])
412
- });
413
- }
419
+ function atpage() {
420
+ var pos = position();
421
+ var m = match(/^@page */);
422
+ if (!m) return;
414
423
 
415
- /**
416
- * Parse paged media.
417
- */
424
+ var sel = selector() || [];
418
425
 
419
- function atpage() {
420
- var pos = position();
421
- var m = match(/^@page */);
422
- if (!m) return;
426
+ if (!open()) return error("@page missing '{'");
427
+ var decls = comments();
423
428
 
424
- var sel = selector() || [];
429
+ // declarations
430
+ var decl;
431
+ while (decl = declaration()) {
432
+ decls.push(decl);
433
+ decls = decls.concat(comments());
434
+ }
425
435
 
426
- if (!open()) return error("@page missing '{'");
427
- var decls = comments();
436
+ if (!close()) return error("@page missing '}'");
428
437
 
429
- // declarations
430
- var decl;
431
- while (decl = declaration()) {
432
- decls.push(decl);
433
- decls = decls.concat(comments());
438
+ return pos({
439
+ type: 'page',
440
+ selectors: sel,
441
+ declarations: decls
442
+ });
434
443
  }
435
444
 
436
- if (!close()) return error("@page missing '}'");
445
+ /**
446
+ * Parse document.
447
+ */
437
448
 
438
- return pos({
439
- type: 'page',
440
- selectors: sel,
441
- declarations: decls
442
- });
443
- }
449
+ function atdocument() {
450
+ var pos = position();
451
+ var m = match(/^@([-\w]+)?document *([^{]+)/);
452
+ if (!m) return;
444
453
 
445
- /**
446
- * Parse document.
447
- */
454
+ var vendor = trim(m[1]);
455
+ var doc = trim(m[2]);
448
456
 
449
- function atdocument() {
450
- var pos = position();
451
- var m = match(/^@([-\w]+)?document *([^{]+)/);
452
- if (!m) return;
457
+ if (!open()) return error("@document missing '{'");
453
458
 
454
- var vendor = trim(m[1]);
455
- var doc = trim(m[2]);
459
+ var style = comments().concat(rules());
456
460
 
457
- if (!open()) return error("@document missing '{'");
461
+ if (!close()) return error("@document missing '}'");
458
462
 
459
- var style = comments().concat(rules());
463
+ return pos({
464
+ type: 'document',
465
+ document: doc,
466
+ vendor: vendor,
467
+ rules: style
468
+ });
469
+ }
460
470
 
461
- if (!close()) return error("@document missing '}'");
471
+ /**
472
+ * Parse font-face.
473
+ */
462
474
 
463
- return pos({
464
- type: 'document',
465
- document: doc,
466
- vendor: vendor,
467
- rules: style
468
- });
469
- }
475
+ function atfontface() {
476
+ var pos = position();
477
+ var m = match(/^@font-face\s*/);
478
+ if (!m) return;
470
479
 
471
- /**
472
- * Parse font-face.
473
- */
480
+ if (!open()) return error("@font-face missing '{'");
481
+ var decls = comments();
474
482
 
475
- function atfontface() {
476
- var pos = position();
477
- var m = match(/^@font-face\s*/);
478
- if (!m) return;
483
+ // declarations
484
+ var decl;
485
+ while (decl = declaration()) {
486
+ decls.push(decl);
487
+ decls = decls.concat(comments());
488
+ }
479
489
 
480
- if (!open()) return error("@font-face missing '{'");
481
- var decls = comments();
490
+ if (!close()) return error("@font-face missing '}'");
482
491
 
483
- // declarations
484
- var decl;
485
- while (decl = declaration()) {
486
- decls.push(decl);
487
- decls = decls.concat(comments());
492
+ return pos({
493
+ type: 'font-face',
494
+ declarations: decls
495
+ });
488
496
  }
489
497
 
490
- if (!close()) return error("@font-face missing '}'");
498
+ /**
499
+ * Parse import
500
+ */
491
501
 
492
- return pos({
493
- type: 'font-face',
494
- declarations: decls
495
- });
496
- }
502
+ var atimport = _compileAtrule('import');
497
503
 
498
- /**
499
- * Parse import
500
- */
504
+ /**
505
+ * Parse charset
506
+ */
501
507
 
502
- var atimport = _compileAtrule('import');
508
+ var atcharset = _compileAtrule('charset');
503
509
 
504
- /**
505
- * Parse charset
506
- */
510
+ /**
511
+ * Parse namespace
512
+ */
507
513
 
508
- var atcharset = _compileAtrule('charset');
514
+ var atnamespace = _compileAtrule('namespace');
509
515
 
510
- /**
511
- * Parse namespace
512
- */
516
+ /**
517
+ * Parse non-block at-rules
518
+ */
513
519
 
514
- var atnamespace = _compileAtrule('namespace');
515
520
 
516
- /**
517
- * Parse non-block at-rules
518
- */
521
+ function _compileAtrule(name) {
522
+ var re = new RegExp('^@' + name + '\\s*([^;]+);');
523
+ return function() {
524
+ var pos = position();
525
+ var m = match(re);
526
+ if (!m) return;
527
+ var ret = { type: name };
528
+ ret[name] = m[1].trim();
529
+ return pos(ret);
530
+ }
531
+ }
532
+
533
+ /**
534
+ * Parse at rule.
535
+ */
536
+
537
+ function atrule() {
538
+ if (css[0] != '@') return;
539
+
540
+ return atkeyframes()
541
+ || atmedia()
542
+ || atcustommedia()
543
+ || atsupports()
544
+ || atimport()
545
+ || atcharset()
546
+ || atnamespace()
547
+ || atdocument()
548
+ || atpage()
549
+ || athost()
550
+ || atfontface();
551
+ }
519
552
 
553
+ /**
554
+ * Parse rule.
555
+ */
520
556
 
521
- function _compileAtrule(name) {
522
- var re = new RegExp('^@' + name + '\\s*([^;]+);');
523
- return function() {
557
+ function rule() {
524
558
  var pos = position();
525
- var m = match(re);
526
- if (!m) return;
527
- var ret = { type: name };
528
- ret[name] = m[1].trim();
529
- return pos(ret);
559
+ var sel = selector();
560
+
561
+ if (!sel) return error('selector missing');
562
+ comments();
563
+
564
+ return pos({
565
+ type: 'rule',
566
+ selectors: sel,
567
+ declarations: declarations()
568
+ });
530
569
  }
531
- }
570
+
571
+ return addParent(stylesheet());
572
+ };
532
573
 
533
574
  /**
534
- * Parse at rule.
575
+ * Trim `str`.
535
576
  */
536
577
 
537
- function atrule() {
538
- if (css[0] != '@') return;
539
-
540
- return atkeyframes()
541
- || atmedia()
542
- || atcustommedia()
543
- || atsupports()
544
- || atimport()
545
- || atcharset()
546
- || atnamespace()
547
- || atdocument()
548
- || atpage()
549
- || athost()
550
- || atfontface();
578
+ function trim(str) {
579
+ return str ? str.replace(/^\s+|\s+$/g, '') : '';
551
580
  }
552
581
 
553
582
  /**
554
- * Parse rule.
583
+ * Adds non-enumerable parent node reference to each node.
555
584
  */
556
585
 
557
- function rule() {
558
- var pos = position();
559
- var sel = selector();
560
-
561
- if (!sel) return error('selector missing');
562
- comments();
586
+ function addParent(obj, parent) {
587
+ var isNode = obj && typeof obj.type === 'string';
588
+ var childParent = isNode ? obj : parent;
563
589
 
564
- return pos({
565
- type: 'rule',
566
- selectors: sel,
567
- declarations: declarations()
568
- });
569
- }
570
-
571
- return addParent(stylesheet());
572
- };
590
+ for (var k in obj) {
591
+ var value = obj[k];
592
+ if (Array.isArray(value)) {
593
+ value.forEach(function(v) { addParent(v, childParent); });
594
+ } else if (value && typeof value === 'object') {
595
+ addParent(value, childParent);
596
+ }
597
+ }
573
598
 
574
- /**
575
- * Trim `str`.
576
- */
599
+ if (isNode) {
600
+ Object.defineProperty(obj, 'parent', {
601
+ configurable: true,
602
+ writable: true,
603
+ enumerable: false,
604
+ value: parent || null
605
+ });
606
+ }
577
607
 
578
- function trim(str) {
579
- return str ? str.replace(/^\s+|\s+$/g, '') : '';
580
- }
608
+ return obj;
609
+ }
581
610
 
582
- /**
583
- * Adds non-enumerable parent node reference to each node.
584
- */
611
+ /**
612
+ * Parses inline style.
613
+ *
614
+ * Example: 'color:red' => { color: 'red' }
615
+ *
616
+ * @param {String} style - The inline style.
617
+ * @param {Function} [iterator] - The iterator function.
618
+ * @return {null|Object}
619
+ */
620
+ var styleToObject = function parseInlineStyle(style, iterator) {
621
+ if (!style || typeof style !== 'string') return null;
585
622
 
586
- function addParent(obj, parent) {
587
- var isNode = obj && typeof obj.type === 'string';
588
- var childParent = isNode ? obj : parent;
623
+ // make sure to wrap declarations in placeholder
624
+ var declarations = parse('p{' + style + '}').stylesheet.rules[0].declarations;
625
+ var declaration, property, value;
589
626
 
590
- for (var k in obj) {
591
- var value = obj[k];
592
- if (Array.isArray(value)) {
593
- value.forEach(function(v) { addParent(v, childParent); });
594
- } else if (value && typeof value === 'object') {
595
- addParent(value, childParent);
596
- }
597
- }
627
+ var output = null;
628
+ var hasIterator = typeof iterator === 'function';
598
629
 
599
- if (isNode) {
600
- Object.defineProperty(obj, 'parent', {
601
- configurable: true,
602
- writable: true,
603
- enumerable: false,
604
- value: parent || null
605
- });
606
- }
630
+ for (var i = 0, len = declarations.length; i < len; i++) {
631
+ declaration = declarations[i];
632
+ property = declaration.property;
633
+ value = declaration.value;
607
634
 
608
- return obj;
609
- }
610
-
611
- /**
612
- * Parses inline style.
613
- *
614
- * Example: 'color:red' => { color: 'red' }
615
- *
616
- * @param {String} style - The inline style.
617
- * @param {Function} [iterator] - The iterator function.
618
- * @return {null|Object}
619
- */
620
- var styleToObject = function parseInlineStyle(style, iterator) {
621
- if (!style || typeof style !== 'string') return null;
622
-
623
- // make sure to wrap declarations in placeholder
624
- var declarations = parse('p{' + style + '}').stylesheet.rules[0].declarations;
625
- var declaration, property, value;
626
-
627
- var output = null;
628
- var hasIterator = typeof iterator === 'function';
629
-
630
- for (var i = 0, len = declarations.length; i < len; i++) {
631
- declaration = declarations[i];
632
- property = declaration.property;
633
- value = declaration.value;
634
-
635
- if (hasIterator) {
636
- iterator(property, value, declaration);
637
- } else if (value) {
638
- output || (output = {});
639
- output[property] = value;
635
+ if (hasIterator) {
636
+ iterator(property, value, declaration);
637
+ } else if (value) {
638
+ output || (output = {});
639
+ output[property] = value;
640
+ }
640
641
  }
641
- }
642
642
 
643
- return output;
644
- };
643
+ return output;
644
+ };
645
645
 
646
- return styleToObject;
646
+ return styleToObject;
647
647
 
648
648
  })));
@@ -1 +1 @@
1
- !function(r,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):r.StyleToObject=e()}(this,function(){"use strict";function r(r){return r?r.replace(/^\s+|\s+$/g,""):""}function e(r,n){var t=r&&"string"==typeof r.type,i=t?r:n;for(var s in r){var u=r[s];Array.isArray(u)?u.forEach(function(r){e(r,i)}):u&&"object"==typeof u&&e(u,i)}return t&&Object.defineProperty(r,"parent",{configurable:!0,writable:!0,enumerable:!1,value:n||null}),r}var n=/\/\*[^*]*\*+([^/*][^*]*\*+)*\//g;return function(t,i){if(!t||"string"!=typeof t)return null;for(var s,u,a,o=function(t,i){function s(r){var e=r.match(/\n/g);e&&(A+=e.length);var n=r.lastIndexOf("\n");b=~n?r.length-n:b+r.length}function u(){var r={line:A,column:b};return function(e){return e.position=new a(r),l(),e}}function a(r){this.start=r,this.end={line:A,column:b},this.source=i.source}function o(r){var e=new Error(i.source+":"+A+":"+b+": "+r);if(e.reason=r,e.filename=i.source,e.line=A,e.column=b,e.source=t,!i.silent)throw e;k.push(e)}function c(){return m(/^{\s*/)}function f(){return m(/^}/)}function p(){var e,n=[];for(l(),v(n);t.length&&"}"!=t.charAt(0)&&(e=function(){if("@"==t[0])return function(){var r=u(),e=m(/^@([-\w]+)?keyframes\s*/);if(e){var n=e[1];if(!(e=m(/^([-\w]+)\s*/)))return o("@keyframes missing name");var t=e[1];if(!c())return o("@keyframes missing '{'");for(var i,s=v();i=h();)s.push(i),s=s.concat(v());return f()?r({type:"keyframes",name:t,vendor:n,keyframes:s}):o("@keyframes missing '}'")}}()||function(){var e=u(),t=m(/^@media *([^{]+)/);if(t){var i=r(t[1]);if(!c())return o("@media missing '{'");var s=v().concat(n());return f()?e({type:"media",media:i,rules:s}):o("@media missing '}'")}}()||function(){var e=u(),n=m(/^@custom-media\s+(--[^\s]+)\s*([^{;]+);/);if(n)return e({type:"custom-media",name:r(n[1]),media:r(n[2])})}()||function(){var e=u(),t=m(/^@supports *([^{]+)/);if(t){var i=r(t[1]);if(!c())return o("@supports missing '{'");var s=v().concat(n());return f()?e({type:"supports",supports:i,rules:s}):o("@supports missing '}'")}}()||x()||E()||j()||function(){var e=u(),t=m(/^@([-\w]+)?document *([^{]+)/);if(t){var i=r(t[1]),s=r(t[2]);if(!c())return o("@document missing '{'");var a=v().concat(n());return f()?e({type:"document",document:s,vendor:i,rules:a}):o("@document missing '}'")}}()||function(){var r=u();if(m(/^@page */)){var e=g()||[];if(!c())return o("@page missing '{'");for(var n,t=v();n=y();)t.push(n),t=t.concat(v());return f()?r({type:"page",selectors:e,declarations:t}):o("@page missing '}'")}}()||function(){var r=u();if(m(/^@host\s*/)){if(!c())return o("@host missing '{'");var e=v().concat(n());return f()?r({type:"host",rules:e}):o("@host missing '}'")}}()||function(){var r=u();if(m(/^@font-face\s*/)){if(!c())return o("@font-face missing '{'");for(var e,n=v();e=y();)n.push(e),n=n.concat(v());return f()?r({type:"font-face",declarations:n}):o("@font-face missing '}'")}}()}()||function(){var r=u(),e=g();return e?(v(),r({type:"rule",selectors:e,declarations:d()})):o("selector missing")}());)!1!==e&&(n.push(e),v(n));return n}function m(r){var e=r.exec(t);if(e){var n=e[0];return s(n),t=t.slice(n.length),e}}function l(){m(/^\s*/)}function v(r){var e;for(r=r||[];e=function(){var r=u();if("/"==t.charAt(0)&&"*"==t.charAt(1)){for(var e=2;""!=t.charAt(e)&&("*"!=t.charAt(e)||"/"!=t.charAt(e+1));)++e;if(e+=2,""===t.charAt(e-1))return o("End of comment missing");var n=t.slice(2,e-2);return b+=2,s(n),t=t.slice(e),b+=2,r({type:"comment",comment:n})}}();)!1!==e&&r.push(e);return r}function g(){var e=m(/^([^{]+)/);if(e)return r(e[0]).replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/g,"").replace(/"(?:\\"|[^"])*"|'(?:\\'|[^'])*'/g,function(r){return r.replace(/,/g,"‌")}).split(/\s*(?![^(]*\)),\s*/).map(function(r){return r.replace(/\u200C/g,",")})}function y(){var e=u(),t=m(/^(\*?[-#\/\*\\\w]+(\[[0-9a-z_-]+\])?)\s*/);if(t){if(t=r(t[0]),!m(/^:\s*/))return o("property missing ':'");var i=m(/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^\)]*?\)|[^};])+)/),s=e({type:"declaration",property:t.replace(n,""),value:i?r(i[0]).replace(n,""):""});return m(/^[;\s]*/),s}}function d(){var r=[];if(!c())return o("missing '{'");v(r);for(var e;e=y();)!1!==e&&(r.push(e),v(r));return f()?r:o("missing '}'")}function h(){for(var r,e=[],n=u();r=m(/^((\d+\.\d+|\.\d+|\d+)%?|[a-z]+)\s*/);)e.push(r[1]),m(/^,\s*/);if(e.length)return n({type:"keyframe",values:e,declarations:d()})}function w(r){var e=new RegExp("^@"+r+"\\s*([^;]+);");return function(){var n=u(),t=m(e);if(t){var i={type:r};return i[r]=t[1].trim(),n(i)}}}i=i||{};var A=1,b=1;a.prototype.content=t;var k=[],x=w("import"),E=w("charset"),j=w("namespace");return e(function(){var r=p();return{type:"stylesheet",stylesheet:{source:i.source,rules:r,parsingErrors:k}}}())}("p{"+t+"}").stylesheet.rules[0].declarations,c=null,f="function"==typeof i,p=0,m=o.length;p<m;p++)u=(s=o[p]).property,a=s.value,f?i(u,a,s):a&&(c||(c={}),c[u]=a);return c}});
1
+ !function(r,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):r.StyleToObject=e()}(this,function(){"use strict";var z=/\/\*[^*]*\*+([^/*][^*]*\*+)*\//g,f=function(t,n){n=n||{};var i=1,s=1;function a(r){var e=r.match(/\n/g);e&&(i+=e.length);var n=r.lastIndexOf("\n");s=~n?r.length-n:s+r.length}function u(){var e={line:i,column:s};return function(r){return r.position=new o(e),g(),r}}function o(r){this.start=r,this.end={line:i,column:s},this.source=n.source}o.prototype.content=t;var c=[];function f(r){var e=new Error(n.source+":"+i+":"+s+": "+r);if(e.reason=r,e.filename=n.source,e.line=i,e.column=s,e.source=t,!n.silent)throw e;c.push(e)}function p(){return v(/^{\s*/)}function m(){return v(/^}/)}function l(){var r,e=[];for(g(),y(e);t.length&&"}"!=t.charAt(0)&&(r=j()||O());)!1!==r&&(e.push(r),y(e));return e}function v(r){var e=r.exec(t);if(e){var n=e[0];return a(n),t=t.slice(n.length),e}}function g(){v(/^\s*/)}function y(r){var e;for(r=r||[];e=d();)!1!==e&&r.push(e);return r}function d(){var r=u();if("/"==t.charAt(0)&&"*"==t.charAt(1)){for(var e=2;""!=t.charAt(e)&&("*"!=t.charAt(e)||"/"!=t.charAt(e+1));)++e;if(e+=2,""===t.charAt(e-1))return f("End of comment missing");var n=t.slice(2,e-2);return s+=2,a(n),t=t.slice(e),s+=2,r({type:"comment",comment:n})}}function h(){var r=v(/^([^{]+)/);if(r)return C(r[0]).replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/g,"").replace(/"(?:\\"|[^"])*"|'(?:\\'|[^'])*'/g,function(r){return r.replace(/,/g,"‌")}).split(/\s*(?![^(]*\)),\s*/).map(function(r){return r.replace(/\u200C/g,",")})}function w(){var r=u(),e=v(/^(\*?[-#\/\*\\\w]+(\[[0-9a-z_-]+\])?)\s*/);if(e){if(e=C(e[0]),!v(/^:\s*/))return f("property missing ':'");var n=v(/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^\)]*?\)|[^};])+)/),t=r({type:"declaration",property:e.replace(z,""),value:n?C(n[0]).replace(z,""):""});return v(/^[;\s]*/),t}}function A(){var r,e=[];if(!p())return f("missing '{'");for(y(e);r=w();)!1!==r&&(e.push(r),y(e));return m()?e:f("missing '}'")}function b(){for(var r,e=[],n=u();r=v(/^((\d+\.\d+|\.\d+|\d+)%?|[a-z]+)\s*/);)e.push(r[1]),v(/^,\s*/);if(e.length)return n({type:"keyframe",values:e,declarations:A()})}var r,e=E("import"),k=E("charset"),x=E("namespace");function E(t){var i=new RegExp("^@"+t+"\\s*([^;]+);");return function(){var r=u(),e=v(i);if(e){var n={type:t};return n[t]=e[1].trim(),r(n)}}}function j(){if("@"==t[0])return function(){var r=u();if(e=v(/^@([-\w]+)?keyframes\s*/)){var e,n=e[1];if(!(e=v(/^([-\w]+)\s*/)))return f("@keyframes missing name");var t,i=e[1];if(!p())return f("@keyframes missing '{'");for(var s=y();t=b();)s.push(t),s=s.concat(y());return m()?r({type:"keyframes",name:i,vendor:n,keyframes:s}):f("@keyframes missing '}'")}}()||function(){var r=u(),e=v(/^@media *([^{]+)/);if(e){var n=C(e[1]);if(!p())return f("@media missing '{'");var t=y().concat(l());return m()?r({type:"media",media:n,rules:t}):f("@media missing '}'")}}()||function(){var r=u(),e=v(/^@custom-media\s+(--[^\s]+)\s*([^{;]+);/);if(e)return r({type:"custom-media",name:C(e[1]),media:C(e[2])})}()||function(){var r=u(),e=v(/^@supports *([^{]+)/);if(e){var n=C(e[1]);if(!p())return f("@supports missing '{'");var t=y().concat(l());return m()?r({type:"supports",supports:n,rules:t}):f("@supports missing '}'")}}()||e()||k()||x()||function(){var r=u(),e=v(/^@([-\w]+)?document *([^{]+)/);if(e){var n=C(e[1]),t=C(e[2]);if(!p())return f("@document missing '{'");var i=y().concat(l());return m()?r({type:"document",document:t,vendor:n,rules:i}):f("@document missing '}'")}}()||function(){var r=u();if(v(/^@page */)){var e=h()||[];if(!p())return f("@page missing '{'");for(var n,t=y();n=w();)t.push(n),t=t.concat(y());return m()?r({type:"page",selectors:e,declarations:t}):f("@page missing '}'")}}()||function(){var r=u();if(v(/^@host\s*/)){if(!p())return f("@host missing '{'");var e=y().concat(l());return m()?r({type:"host",rules:e}):f("@host missing '}'")}}()||function(){var r=u();if(v(/^@font-face\s*/)){if(!p())return f("@font-face missing '{'");for(var e,n=y();e=w();)n.push(e),n=n.concat(y());return m()?r({type:"font-face",declarations:n}):f("@font-face missing '}'")}}()}function O(){var r=u(),e=h();return e?(y(),r({type:"rule",selectors:e,declarations:A()})):f("selector missing")}return function e(r,n){var t=r&&"string"==typeof r.type;var i=t?r:n;for(var s in r){var a=r[s];Array.isArray(a)?a.forEach(function(r){e(r,i)}):a&&"object"==typeof a&&e(a,i)}t&&Object.defineProperty(r,"parent",{configurable:!0,writable:!0,enumerable:!1,value:n||null});return r}((r=l(),{type:"stylesheet",stylesheet:{source:n.source,rules:r,parsingErrors:c}}))};function C(r){return r?r.replace(/^\s+|\s+$/g,""):""}return function(r,e){if(!r||"string"!=typeof r)return null;for(var n,t,i,s=f("p{"+r+"}").stylesheet.rules[0].declarations,a=null,u="function"==typeof e,o=0,c=s.length;o<c;o++)t=(n=s[o]).property,i=n.value,u?e(t,i,n):i&&(a||(a={}),a[t]=i);return a}});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "style-to-object",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Converts inline style to object.",
5
5
  "author": "Mark <mark@remarkablemark.org>",
6
6
  "main": "index.js",
@@ -31,19 +31,19 @@
31
31
  "pojo"
32
32
  ],
33
33
  "dependencies": {
34
- "css": "2.2.3"
34
+ "css": "2.2.4"
35
35
  },
36
36
  "devDependencies": {
37
- "coveralls": "3.0.0",
38
- "eslint": "4.11.0",
37
+ "coveralls": "3.0.2",
38
+ "eslint": "5.5.0",
39
39
  "istanbul": "0.4.5",
40
- "mocha": "4.0.1",
41
- "rollup": "0.51.5",
42
- "rollup-plugin-commonjs": "8.2.6",
43
- "rollup-plugin-node-resolve": "3.0.0",
44
- "rollup-plugin-uglify": "2.0.1",
45
- "standard-version": "4.2.0",
46
- "uglify-es": "3.1.9"
40
+ "mocha": "5.2.0",
41
+ "rollup": "0.65.2",
42
+ "rollup-plugin-commonjs": "9.1.6",
43
+ "rollup-plugin-node-resolve": "3.4.0",
44
+ "rollup-plugin-uglify": "5.0.2",
45
+ "standard-version": "4.4.0",
46
+ "uglify-es": "3.3.9"
47
47
  },
48
48
  "license": "MIT"
49
49
  }