Skip to content

Commit 04793b0

Browse files
feat(parser): parse inline style to object with css.parse
1 parent b9c3d99 commit 04793b0

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1+
var parse = require('css/lib/parse');
2+
13
/**
24
* Parses inline style to object.
35
*
6+
* Example: 'color:red' => { color: 'red' }
7+
*
48
* @param {String} style
59
* @return {Object|null}
610
*/
711
module.exports = function parseInlineStyle(style) {
812
if (!style || typeof style !== 'string') return null;
9-
}
13+
14+
// make sure to wrap declarations in placeholder
15+
var declarations = parse('p{' + style + '}').stylesheet.rules[0].declarations;
16+
var output = {};
17+
18+
declarations.forEach(function(declaration) {
19+
output[declaration.property] = declaration.value;
20+
});
21+
22+
return output;
23+
};

0 commit comments

Comments
 (0)