Skip to content

Commit 5d9cd17

Browse files
committed
Compact multilines attributes
1 parent 3d53bb3 commit 5d9cd17

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/htmltojsx.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,14 @@ HTMLtoJSX.prototype = {
610610
* @return {string}
611611
*/
612612
_getElementAttribute: function(node, attribute) {
613+
var value = attribute.value;
614+
// If there's some newlines in the value, compact it
615+
if ( value.indexOf('\n') > -1 ) {
616+
value = value.replace(/^\s+/mg, ' ').replace(/\n/g, '');
617+
}
613618
switch (attribute.name) {
614619
case 'style':
615-
return this._getStyleAttribute(attribute.value);
620+
return this._getStyleAttribute(value);
616621
default:
617622
var tagName = jsxTagName(node.tagName);
618623
var name =
@@ -623,10 +628,10 @@ HTMLtoJSX.prototype = {
623628
var result = name;
624629

625630
// Numeric values should be output as {123} not "123"
626-
if (isNumeric(attribute.value)) {
627-
result += '={' + attribute.value + '}';
628-
} else if (attribute.value.length > 0) {
629-
result += '="' + attribute.value.replace(/"/gm, '"') + '"';
631+
if (isNumeric(value)) {
632+
result += '={' + value + '}';
633+
} else if (value.length > 0) {
634+
result += '="' + value.replace(/"/gm, '"') + '"';
630635
}
631636
return result;
632637
}

test/htmltojsx-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,17 @@ describe('htmltojsx', function() {
238238
expect(converter.convert('<svg height="100" width="100"><circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" fill-rule="evenodd"/></svg>').trim())
239239
.toBe('<svg height={100} width={100}><circle cx={50} cy={50} r={40} stroke="black" strokeWidth={3} fill="red" fillRule="evenodd" /></svg>');
240240
});
241+
242+
it('should compact multilignes attributes', function() {
243+
var converter = new HTMLtoJSX({ createClass: false });
244+
expect(converter.convert([
245+
'<div class="cc1',
246+
' cc2',
247+
' cc3',
248+
' cc4">multilignes classes</div>'
249+
].join('\n')).trim())
250+
.toBe('<div className="cc1 cc2 cc3 cc4">multilignes classes</div>');
251+
});
241252
});
242253

243254
describe('special tags', function() {

0 commit comments

Comments
 (0)