Skip to content

Commit 2e956be

Browse files
authored
Fix whitespace in table elements
Closes GH-576. Closes GH-577. Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent d36048a commit 2e956be

File tree

3 files changed

+17
-42
lines changed

3 files changed

+17
-42
lines changed

src/ast-to-react.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ exports.hastChildrenToReact = childrenToReact
154154

155155
const own = {}.hasOwnProperty
156156

157+
// The table-related elements that must not contain whitespace text according
158+
// to React.
159+
const tableElements = new Set(['table', 'thead', 'tbody', 'tfoot', 'tr'])
160+
157161
/**
158162
* @param {Context} context
159163
* @param {Element|Root} node
@@ -171,7 +175,14 @@ function childrenToReact(context, node) {
171175
if (child.type === 'element') {
172176
children.push(toReact(context, child, childIndex, node))
173177
} else if (child.type === 'text') {
174-
children.push(child.value)
178+
/** @type {ReactMarkdownNames} */
179+
// @ts-ignore assume a known HTML/SVG element.
180+
const name = node.tagName
181+
// React does not permit whitespace text elements as children of table:
182+
// cf. https://github.com/remarkjs/react-markdown/issues/576
183+
if (!tableElements.has(name) || child.value !== '\n') {
184+
children.push(child.value)
185+
}
175186
}
176187
// @ts-ignore `raw` nodes are non-standard
177188
else if (child.type === 'raw' && !context.options.skipHtml) {

test/__snapshots__/react-markdown.test.js.snap

+2-38
Original file line numberDiff line numberDiff line change
@@ -2335,14 +2335,7 @@ exports[`should render link references 1`] = `"<p>Stuff were changed in <a href=
23352335
23362336
exports[`should render partial tables 1`] = `
23372337
"<p>User is writing a table by hand</p>
2338-
<table>
2339-
<thead>
2340-
<tr>
2341-
<th>Test</th>
2342-
<th>Test</th>
2343-
</tr>
2344-
</thead>
2345-
</table>"
2338+
<table><thead><tr><th>Test</th><th>Test</th></tr></thead></table>"
23462339
`;
23472340
23482341
exports[`should render table of contents plugin 1`] = `
@@ -2446,36 +2439,7 @@ Array [
24462439

24472440
exports[`should render tables 1`] = `
24482441
"<p>Languages are fun, right?</p>
2449-
<table>
2450-
<thead>
2451-
<tr>
2452-
<th style=\\"text-align:left\\">ID</th>
2453-
<th style=\\"text-align:center\\">English</th>
2454-
<th style=\\"text-align:right\\">Norwegian</th>
2455-
<th>Italian</th>
2456-
</tr>
2457-
</thead>
2458-
<tbody>
2459-
<tr>
2460-
<td style=\\"text-align:left\\">1</td>
2461-
<td style=\\"text-align:center\\">one</td>
2462-
<td style=\\"text-align:right\\">en</td>
2463-
<td>uno</td>
2464-
</tr>
2465-
<tr>
2466-
<td style=\\"text-align:left\\">2</td>
2467-
<td style=\\"text-align:center\\">two</td>
2468-
<td style=\\"text-align:right\\">to</td>
2469-
<td>due</td>
2470-
</tr>
2471-
<tr>
2472-
<td style=\\"text-align:left\\">3</td>
2473-
<td style=\\"text-align:center\\">three</td>
2474-
<td style=\\"text-align:right\\">tre</td>
2475-
<td>tre</td>
2476-
</tr>
2477-
</tbody>
2478-
</table>"
2442+
<table><thead><tr><th style=\\"text-align:left\\">ID</th><th style=\\"text-align:center\\">English</th><th style=\\"text-align:right\\">Norwegian</th><th>Italian</th></tr></thead><tbody><tr><td style=\\"text-align:left\\">1</td><td style=\\"text-align:center\\">one</td><td style=\\"text-align:right\\">en</td><td>uno</td></tr><tr><td style=\\"text-align:left\\">2</td><td style=\\"text-align:center\\">two</td><td style=\\"text-align:right\\">to</td><td>due</td></tr><tr><td style=\\"text-align:left\\">3</td><td style=\\"text-align:center\\">three</td><td style=\\"text-align:right\\">tre</td><td>tre</td></tr></tbody></table>"
24792443
`;
24802444
24812445
exports[`should set source position attributes if sourcePos option is enabled 1`] = `

test/react-markdown.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ test('should pass `isHeader: boolean` to `tr`s', () => {
406406
/>
407407
)
408408
const expected =
409-
'<table>\n<thead>\n<tr>\n<th>a</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>b</td>\n</tr>\n<tr>\n<td>c</td>\n</tr>\n</tbody>\n</table>'
409+
'<table><thead><tr><th>a</th></tr></thead><tbody><tr><td>b</td></tr><tr><td>c</td></tr></tbody></table>'
410410
expect(actual).toEqual(expected)
411411
})
412412

@@ -429,7 +429,7 @@ test('should pass `isHeader: true` to `th`s, `isHeader: false` to `td`s', () =>
429429
/>
430430
)
431431
const expected =
432-
'<table>\n<thead>\n<tr>\n<th>a</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>b</td>\n</tr>\n<tr>\n<td>c</td>\n</tr>\n</tbody>\n</table>'
432+
'<table><thead><tr><th>a</th></tr></thead><tbody><tr><td>b</td></tr><tr><td>c</td></tr></tbody></table>'
433433
expect(actual).toEqual(expected)
434434
})
435435

@@ -1169,7 +1169,7 @@ test('should support table cells w/ style', () => {
11691169
<Markdown children={input} remarkPlugins={[gfm]} rehypePlugins={[plugin]} />
11701170
)
11711171
const expected =
1172-
'<table>\n<thead>\n<tr>\n<th style="color:red;text-align:left">a</th>\n</tr>\n</thead>\n</table>'
1172+
'<table><thead><tr><th style="color:red;text-align:left">a</th></tr></thead></table>'
11731173

11741174
expect(actual).toEqual(expected)
11751175
})

0 commit comments

Comments
 (0)