Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/htmltojsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ HTMLtoJSX.prototype = {
// Hax: textareas need their inner text moved to a "defaultValue" attribute.
attributes.push('defaultValue={' + JSON.stringify(node.value) + '}');
}
if (tagName === 'style') {
// Hax: style tag contents need to be dangerously set due to liberal curly brace usage
attributes.push('dangerouslySetInnerHTML={{__html: ' + JSON.stringify(node.textContent) + ' }}');
}
if (tagName === 'pre') {
this._inPreTag = true;
}
Expand Down Expand Up @@ -413,8 +417,8 @@ HTMLtoJSX.prototype = {
*/
_isSelfClosing: function(node) {
// If it has children, it's not self-closing
// Exception: All children of a textarea are moved to a "defaultValue" attribute.
return !node.firstChild || node.tagName.toLowerCase() === 'textarea';
// Exception: All children of a textarea are moved to a "defaultValue" attribute, style attributes are dangerously set.
return !node.firstChild || node.tagName.toLowerCase() === 'textarea' || node.tagName.toLowerCase() === 'style';
},

/**
Expand All @@ -424,9 +428,9 @@ HTMLtoJSX.prototype = {
*/
_visitText: function(node) {
var parentTag = node.parentNode && node.parentNode.tagName.toLowerCase();
if (parentTag === 'textarea') {
// Ignore text content of textareas, as it will have already been moved
// to a "defaultValue" attribute.
if (parentTag === 'textarea' || parentTag === 'style') {
// Ignore text content of textareas and styles, as it will have already been moved
// to a "defaultValue" attribute and "dangerouslySetInnerHTML" attribute respectively.
return;
}

Expand Down
6 changes: 6 additions & 0 deletions test/htmltojsx-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,11 @@ describe('htmltojsx', function() {
'<pre><b>Hello world{" "}yo</b>this{" "}is{" "}a<i>{" "}test</i></pre>'
);
});

it('should dangerously set <style> tag contents', function() {
var converter = new HTMLtoJSX({ createClass: false });
expect(converter.convert('<style>\nh1 {\n background: url(\'http://foo.bar/img.jpg\';\n}\n</style>').trim())
.toBe('<style dangerouslySetInnerHTML={{__html: "\\nh1 {\\n background: url(\'http://foo.bar/img.jpg\';\\n}\\n" }} />');
});
});
});
⌂ Proxy