Skip to content

Commit 6146c8a

Browse files
author
Ian Vieira
committed
Fix component style attribute parser using AST
1 parent f678c3f commit 6146c8a

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

lib/attributes-to-props.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
var utilities = require('./utilities');
77
var propertyConfig = require('./property-config');
8-
var cssToReactNative = require('css-to-react-native');
8+
var csstree = require('css-tree');
99
var config = propertyConfig.config;
1010
var isCustomAttribute = propertyConfig.HTMLDOMPropertyConfig.isCustomAttribute;
1111

@@ -64,16 +64,21 @@ function cssToJs(style) {
6464
throw new Error('`cssToJs`: first argument must be a string. ');
6565
}
6666

67-
var styles = style.split(';');
68-
styles = styles.map(function (style) {
69-
var parts = style.split(':');
70-
var propName = parts[0];
71-
parts.shift();
72-
var propValue = parts.join(':');
73-
return [propName.trim(), propValue.trim()];
67+
var styleObj = {};
68+
69+
var ast = csstree.parse(style, {
70+
context: 'declarationList',
71+
parseValue: false
72+
});
73+
74+
csstree.walk(ast, function (node) {
75+
if(node.type === 'Declaration') {
76+
var propertyCamelCase = node.property.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase(); });
77+
styleObj[propertyCamelCase] = node.value.value;
78+
}
7479
});
7580

76-
return cssToReactNative.default(styles);
81+
return styleObj;
7782
}
7883

7984
/**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"converter"
3030
],
3131
"dependencies": {
32-
"css-to-react-native": "^2.0.4",
32+
"css-tree": "^1.0.0-alpha.26",
3333
"html-dom-parser": "0.1.2",
3434
"react-dom-core": "0.0.2"
3535
},

0 commit comments

Comments
 (0)