Skip to content

Compact multilines attributes #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 10 additions & 5 deletions src/htmltojsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,9 +610,14 @@ HTMLtoJSX.prototype = {
* @return {string}
*/
_getElementAttribute: function(node, attribute) {
var value = attribute.value;
// If there's some newlines in the value, compact it
if ( value.indexOf('\n') > -1 ) {
value = value.replace(/^\s+/mg, ' ').replace(/\n/g, '');
}
switch (attribute.name) {
case 'style':
return this._getStyleAttribute(attribute.value);
return this._getStyleAttribute(value);
default:
var tagName = jsxTagName(node.tagName);
var name =
Expand All @@ -623,10 +628,10 @@ HTMLtoJSX.prototype = {
var result = name;

// Numeric values should be output as {123} not "123"
if (isNumeric(attribute.value)) {
result += '={' + attribute.value + '}';
} else if (attribute.value.length > 0) {
result += '="' + attribute.value.replace(/"/gm, '"') + '"';
if (isNumeric(value)) {
result += '={' + value + '}';
} else if (value.length > 0) {
result += '="' + value.replace(/"/gm, '"') + '"';
}
return result;
}
Expand Down
11 changes: 11 additions & 0 deletions test/htmltojsx-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,17 @@ describe('htmltojsx', function() {
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())
.toBe('<svg height={100} width={100}><circle cx={50} cy={50} r={40} stroke="black" strokeWidth={3} fill="red" fillRule="evenodd" /></svg>');
});

it('should compact multilignes attributes', function() {
var converter = new HTMLtoJSX({ createClass: false });
expect(converter.convert([
'<div class="cc1',
' cc2',
' cc3',
' cc4">multilignes classes</div>'
].join('\n')).trim())
.toBe('<div className="cc1 cc2 cc3 cc4">multilignes classes</div>');
});
});

describe('special tags', function() {
Expand Down