Skip to content

Commit

Permalink
Fix HTML parsing of elements with a single child vs. multiple children (
Browse files Browse the repository at this point in the history
  • Loading branch information
nvenegas authored and rexxars committed Apr 15, 2019
1 parent 3ab5e62 commit 4ced275
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ast-to-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ function mergeNodeChildren(node, parsedChildren) {
}

if (el.props.children || parsedChildren) {
const children = (el.props.children || []).concat(parsedChildren)
const children = React.Children.toArray(el.props.children).concat(parsedChildren)
return React.cloneElement(el, null, children)
}
return React.cloneElement(el, null)
Expand Down
31 changes: 31 additions & 0 deletions test/__snapshots__/react-markdown.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,37 @@ Array [
]
`;

exports[`should be able to render a table with a single child with HTML parser plugin 1`] = `
<table>
<tbody>
<tr>
<td>
I am having so much fun
</td>
</tr>
</tbody>
</table>
`;

exports[`should be able to render a table with multiple children with HTML parser plugin 1`] = `
<table>
<thead>
<tr>
<th>
Title
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
I am having so much fun
</td>
</tr>
</tbody>
</table>
`;

exports[`should be able to render basic inline html without containers 1`] = `
<p>
I am having
Expand Down
16 changes: 16 additions & 0 deletions test/react-markdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,22 @@ test('should be able to render multiple inline html elements with self-closing t
expect(component.toJSON()).toMatchSnapshot()
})

test('should be able to render a table with a single child with HTML parser plugin', () => {
const input = '<table><tbody><tr><td>I am having so much fun</td></tr></tbody></table>'
const component = renderer.create(
<Markdown source={input} escapeHtml={false} astPlugins={[htmlParser()]} />
)
expect(component.toJSON()).toMatchSnapshot()
})

test('should be able to render a table with multiple children with HTML parser plugin', () => {
const input = '<table><thead><tr><th>Title</th></tr></thead><tbody><tr><td>I am having so much fun</td></tr></tbody></table>'
const component = renderer.create(
<Markdown source={input} escapeHtml={false} astPlugins={[htmlParser()]} />
)
expect(component.toJSON()).toMatchSnapshot()
})

test('should be able to render replaced non-void html elements with HTML parser plugin', () => {
const input = 'I am having <code>so much</code> fun'
const config = {
Expand Down

0 comments on commit 4ced275

Please sign in to comment.