Skip to content

Commit 68cd565

Browse files
author
Ian Vieira
committed
fix(attributes-to-props.js): Use AST to transform style attributes into an style object
1 parent 6146c8a commit 68cd565

File tree

4 files changed

+19
-32
lines changed

4 files changed

+19
-32
lines changed

lib/attributes-to-props.js

+5-10
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 csstree = require('css-tree');
8+
var parser = require('style-to-object');
99
var config = propertyConfig.config;
1010
var isCustomAttribute = propertyConfig.HTMLDOMPropertyConfig.isCustomAttribute;
1111

@@ -66,17 +66,12 @@ function cssToJs(style) {
6666

6767
var styleObj = {};
6868

69-
var ast = csstree.parse(style, {
70-
context: 'declarationList',
71-
parseValue: false
69+
parser(style, function(propName, propValue) {
70+
styleObj[utilities.camelCase(propName)] = propValue;
7271
});
7372

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-
}
79-
});
73+
console.log(styleObj);
74+
8075

8176
return styleObj;
8277
}

lib/utilities.js

+5-16
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,13 @@
66
* @param {String} string - The string.
77
* @return {String}
88
*/
9-
function camelCase(string) {
10-
if (typeof string !== 'string') {
11-
throw new TypeError('First argument must be a string');
12-
}
139

14-
// hyphen found after first character
15-
if (string.indexOf('-') > 0) {
16-
var strings = string.toLowerCase().split('-');
17-
18-
// capitalize starting from the second string item
19-
for (var i = 1, len = strings.length; i < len; i++) {
20-
strings[i] = strings[i].charAt(0).toUpperCase() + strings[i].slice(1);
21-
}
10+
var _hyphenPattern = /-(.)/g;
2211

23-
return strings.join('');
24-
}
25-
26-
return string;
12+
function camelCase(string) {
13+
return string.replace(_hyphenPattern, function(_, character) {
14+
return character.toUpperCase();
15+
});
2716
}
2817

2918
/**

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
"converter"
3030
],
3131
"dependencies": {
32-
"css-tree": "^1.0.0-alpha.26",
3332
"html-dom-parser": "0.1.2",
34-
"react-dom-core": "0.0.2"
33+
"react-dom-core": "0.0.2",
34+
"style-to-object": "^0.2.0"
3535
},
3636
"devDependencies": {
3737
"coveralls": "^2.13.1",

test/attributes-to-props.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -160,27 +160,30 @@ describe('attributes to props helper', function() {
160160
// proper css
161161
assert.deepEqual(
162162
attributesToProps({
163-
style: 'color: #f00; font-size: 42px; z-index: -1;'
163+
style: 'color: #f00; font-size: 42px; z-index: -1; -moz-border-radius-topright: 10px; background: url(data:image/png; base64,ivborw0kggoaaaansaaaabgdbtueaalgpc/xhbqaaaafzmuexurczmzpf399fx1+bm5mzy9avzxbesmgces5/p8/t9furvcrmu73jwlzosgsiizurcjo/ad+eqjjb4hv8bft+idpqocx1wjosbfhh2xssxeiyn3uli/6mnree07uiwjev8u8czwyuqdlkpg1bkb4nnm+veanfhqn1k4+gpt6ugqcvu2h2ovuif)'
164164
}),
165165
{
166166
style: {
167167
color: '#f00',
168168
fontSize: '42px',
169-
zIndex: '-1'
169+
zIndex: '-1',
170+
MozBorderRadiusTopright: '10px',
171+
background: "url(data:image/png; base64,ivborw0kggoaaaansaaaabgdbtueaalgpc/xhbqaaaafzmuexurczmzpf399fx1+bm5mzy9avzxbesmgces5/p8/t9furvcrmu73jwlzosgsiizurcjo/ad+eqjjb4hv8bft+idpqocx1wjosbfhh2xssxeiyn3uli/6mnree07uiwjev8u8czwyuqdlkpg1bkb4nnm+veanfhqn1k4+gpt6ugqcvu2h2ovuif)"
170172
}
171173
}
172174
);
173175

174176
// valid but messy
175177
assert.deepEqual(
176178
attributesToProps({
177-
style: 'border-bottom-left-radius:1em;border-right-style:solid;Z-Index:-1'
179+
style: 'border-bottom-left-radius:1em;border-right-style:solid;Z-Index:-1;-moz-border-radius-bottomleft:20px'
178180
}),
179181
{
180182
style: {
181183
borderBottomLeftRadius: '1em',
182184
borderRightStyle: 'solid',
183-
zIndex: '-1'
185+
zIndex: '-1',
186+
MozBorderRadiusBottomleft: '20px'
184187
}
185188
}
186189
);

0 commit comments

Comments
 (0)