Skip to content

Commit

Permalink
fix: Handle nested table with sibling elements
Browse files Browse the repository at this point in the history
  • Loading branch information
ericof committed Mar 16, 2023
1 parent 018a8d3 commit bc6a215
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 33 deletions.
51 changes: 18 additions & 33 deletions src/converters/fromHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,40 +94,26 @@ const isInline = (n) =>

const extractElementsWithConverters = (el, defaultTextBlock, href) => {
const result = [];
if (el.tagName === 'TABLE') {
for (const child of el.childNodes) {
const tmpResult = extractElementsWithConverters(
child,
defaultTextBlock,
href,
);
if (tmpResult.length > 0) {
result.push(...tmpResult);
}
}
result.push(blockFromElement(el, defaultTextBlock, href));
} else if (elementsWithConverters.includes(el.tagName)) {
result.push(blockFromElement(el, defaultTextBlock, href));
} else {
const children = el.childNodes;
if (el.tagName === 'A') {
href = el.getAttribute('href');
if (el.tagName === 'A') {
href = el.getAttribute('href');
}
// First, traverse all childNodes
for (const child of el.childNodes) {
const tmpResult = extractElementsWithConverters(
child,
defaultTextBlock,
href,
);
if (tmpResult.length > 0) {
result.push(...tmpResult);
}
for (const child of children) {
if (elementsWithConverters.includes(child.tagName)) {
el.removeChild(child);
result.push(blockFromElement(child, defaultTextBlock, href));
} else {
const tmpResult = extractElementsWithConverters(
child,
defaultTextBlock,
href,
);
if (tmpResult.length > 0) {
result.push(...tmpResult);
}
}
}
if (elementsWithConverters.includes(el.tagName)) {
const parent = el.parentElement;
if (parent) {
parent.removeChild(el);
}
result.push(blockFromElement(el, defaultTextBlock, href));
}

return result;
Expand Down Expand Up @@ -165,7 +151,6 @@ const convertFromHTML = (input, defaultTextBlock) => {
for (const child of children) {
// With children nodes, we keep the wrapper only
// if at least one child is not in elementsWithConverters
keepWrapper = keepWrapper || false;
const tmpResult = extractElementsWithConverters(
child,
defaultTextBlock,
Expand Down
44 changes: 44 additions & 0 deletions src/converters/fromHtml.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,4 +543,48 @@ describe('convertFromHTML parsing nested tags', () => {
expect(result[0]['@type']).toBe('image');
expect(result[1]['@type']).toBe('slateTable');
});
describe('with paragraph, table and image blocks', () => {
const html = `<div>
<div>
<p>
<table>
<tbody>
<tr>
<td>
<div><img src="image.png"/></div>
</td>
</tr>
</tbody>
</table>
</p>
</div>
<p></p>
</div>`;
const result = convertFromHTML(html, 'slate');
expect(result).toHaveLength(2);
expect(result[0]['@type']).toBe('image');
expect(result[1]['@type']).toBe('slateTable');
});
describe('with paragraph, table, image blocks, and other paragraph', () => {
const html = `<div>
<p>
<table>
<tbody>
<tr>
<td>
<div><img src="image.png"/></div>
</td>
</tr>
</tbody>
</table>
</p>
<p></p>
<p>A text</p>
</div>`;
const result = convertFromHTML(html, 'slate');
expect(result).toHaveLength(3);
expect(result[0]['@type']).toBe('image');
expect(result[1]['@type']).toBe('slateTable');
expect(result[2]['@type']).toBe('slate');
});
});

0 comments on commit bc6a215

Please sign in to comment.