Skip to content

Commit 0ba1f43

Browse files
committed
Fixes & Updates
1 parent 3d0246e commit 0ba1f43

File tree

2 files changed

+57
-23
lines changed

2 files changed

+57
-23
lines changed

index.js

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,31 @@
11
var jsx = require('jsx-runtime');
22
var escape = require('escape-html');
3-
var Tag = require('./tag');
3+
var Tag = require('./lib/tag');
4+
var hasOwn = Object.prototype.hasOwnProperty;
45

56
var renderer = jsx.register('HTML', {
67
tags: {
78
'*': {
89
enter: function(tag, props) {
9-
return new Tag(tag, props);
10+
return new Tag(escape(tag), props);
1011
},
1112
leave: function(parent, tag) {
1213
return parent;
1314
},
1415
child: function(child, parent) {
1516
if (child == null) return parent;
1617

17-
if (typeof child === 'string') {
18-
child = escape(child);
18+
if (child instanceof Tag) {
19+
parent.children.push(child);
20+
} else {
21+
child = escape(child + '');
1922
}
2023

21-
parent.children.push(child);
2224
return parent;
2325
},
2426
props: function(props) {
25-
return Object.keys(props).map(function(key) {
26-
var val = key && props[key];
27-
28-
if (!key || val == null) return '';
29-
if (val instanceof Tag) return '';
30-
31-
if (typeof val === 'string') {
32-
val = JSON.stringify(val);
33-
val = val.slice(1, val.length - 1);
34-
} else {
35-
val = JSON.stringify(val);
36-
}
37-
38-
return key + '="' + escape(val) + '"';
39-
}).join(' ');
27+
return Object.keys(props)
28+
.map(mapProps).join(' ');
4029
}
4130
}
4231
},
@@ -45,4 +34,49 @@ var renderer = jsx.register('HTML', {
4534
}
4635
});
4736

48-
module.exports = renderer;
37+
module.exports = renderer;
38+
39+
function mapProps(key) {
40+
var val = key && props[key];
41+
42+
if (!key || val == null) return '';
43+
if (val instanceof Tag) return '';
44+
45+
if (key === 'className') key = 'class';
46+
else if (key === 'cssFor') key = 'for';
47+
else key = key.toLowerCase();
48+
49+
if (key === 'style') {
50+
val = handleStyle(val);
51+
}
52+
53+
if (typeof val === 'string') {
54+
// do nothing
55+
} else {
56+
val = JSON.stringify(val);
57+
}
58+
59+
return escape(key) + '="' + escape(val) + '"';
60+
}
61+
62+
function handleStyle(style) {
63+
if (typeof style === 'string') return style;
64+
65+
var string = '';
66+
67+
for (var key in style) {
68+
if (!hasOwn.call(style, key)) continue;
69+
70+
key = key.replace(/[A-Z]/g, function(m) {
71+
return '-' + m.toLowerCase();
72+
});
73+
74+
if (key.search(/moz-|webkit-|o-|ms-/) === 0) {
75+
key = '-' + key;
76+
}
77+
78+
string += key + ': ' + val + ';';
79+
}
80+
81+
return string;
82+
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
"author": "Arthur Stolyar <[email protected]>",
1010
"license": "MIT",
1111
"dependencies": {
12-
"babel-plugin-jsx": "^1.0.1",
12+
"babel-plugin-jsx": "^1.1.0",
13+
"jsx-runtime": "^1.1.0",
1314
"escape-html": "^1.0.2",
1415
"html-tags": "^1.1.1",
15-
"jsx-runtime": "^1.0.2",
1616
"svg-tags": "^1.0.0"
1717
},
1818
"repository": "jsx-ir/jsx-to-html",

0 commit comments

Comments
 (0)