Skip to content

Commit

Permalink
fix(formatting): trailing space
Browse files Browse the repository at this point in the history
Closes #135

BREAKING CHANGE: Trailing are now preserved. In some rare case, `react-element-to-jsx-string` failed to respect the JSX specs for the trailing space. Event is the space were in the final output. There were silentrly ignored by JSX parser. This commit fix this bug by protecting the trailing space in the output.

If we take the JSX:
```jsx
<div>
  foo <strong>bar</strong> baz
</div>
```

Before it was converted to (the trailing space are replace by `*` for the readability):
```html
<div>
  foo*
  <strong>
    bar
  </strong>
  *baz
</div>
```

Now there are preserved:
```html
<div>
  foo{' '}
  <strong>
    bar
  </strong>
  {' '}baz
</div>
```
  • Loading branch information
armandabric committed Oct 9, 2017
1 parent 2747f1b commit 2a07d5e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/formatter/formatProp.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default (
formattedPropValue === '{false}' &&
!hasDefaultValue
) {
// If a boolean is false an is not different from it's default, we do not render the attribute
// If a boolean is false and not different from it's default, we do not render the attribute
attributeFormattedInline = '';
attributeFormattedMultiline = '';
} else if (useBooleanShorthandSyntax && formattedPropValue === '{true}') {
Expand Down
17 changes: 16 additions & 1 deletion src/formatter/formatTreeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ const escape = (s: string) => {
return `{\`${s}\`}`;
};

const preserveTrailingSpace = (s: string) => {
let result = s;
if (result.endsWith(' ')) {
result = result.replace(/^(\S*)(\s*)$/, "$1{'$2'}");
}

if (result.startsWith(' ')) {
result = result.replace(/^(\s*)(\S*)$/, "{'$1'}$2");
}

return result;
};

export default (
node: TreeNode,
inline: boolean,
Expand All @@ -27,7 +40,9 @@ export default (
}

if (node.type === 'string') {
return node.value ? escape(String(node.value)) : '';
return node.value
? `${preserveTrailingSpace(escape(String(node.value)))}`
: '';
}

if (node.type === 'ReactElement') {
Expand Down
20 changes: 19 additions & 1 deletion src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,22 @@ describe('reactElementToJSXString(ReactElement)', () => {
);
});

it('reactElementToJSXString(<div>\nfoo <span>bar</span> baz\n</div>)', () => {
expect(
reactElementToJSXString(
<div>
foo <span>bar</span> baz
</div>
)
).toEqual(`<div>
foo{' '}
<span>
bar
</span>
{' '}baz
</div>`);
});

it('reactElementToJSXString(<div a={[1, 2, 3, 4]} />', () => {
expect(reactElementToJSXString(<div a={[1, 2, 3, 4]} />)).toEqual(
`<div
Expand Down Expand Up @@ -722,7 +738,9 @@ describe('reactElementToJSXString(ReactElement)', () => {

it('reactElementToJSXString(<div> {false} </div>)', () => {
expect(reactElementToJSXString(<div> {false} </div>)).toEqual(
'<div>\n \n</div>'
`<div>
{' '}
</div>`
);
});

Expand Down

0 comments on commit 2a07d5e

Please sign in to comment.