Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit 1110a79

Browse files
Zyntondmo-odoo
authored andcommitted
[FIX] Table: allow the parsing of a table with inconsistent cell numbers
We expect each row in a table to have the same number of cells. But when we need to parse one that doesn't, we also don't want the system to crash. To emulate what the browser does, this commit introduces empty cells where they were missing, extrapolating on the inconsistent table that is being parsed.
1 parent 1ca0602 commit 1110a79

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

packages/plugin-table/src/TableXmlDomParser.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ export class TableXmlDomParser extends AbstractParser<Node> {
142142
}
143143
}
144144
}
145-
return grid;
145+
146+
// Insert empty cells in every undefined element of the grid.
147+
return grid.map(row => Array.from(row, cell => cell || new TableCellNode()));
146148
}
147149
}

packages/plugin-table/test/Table.test.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,48 @@ describePlugin(Table, testEditor => {
373373
/* eslint-enable prettier/prettier */
374374
});
375375
});
376+
it('should parse and render a table with inconsistent cell numbers', async () => {
377+
await testEditor(BasicEditor, {
378+
/* eslint-disable prettier/prettier */
379+
contentBefore: [
380+
'<table>',
381+
'<thead>',
382+
'<tr>',
383+
'<td>ab</td>',
384+
'<td>cd</td>',
385+
'</tr>',
386+
'</thead>',
387+
'<tr>',
388+
'<td>ef</td>',
389+
'</tr>',
390+
'<tr>',
391+
'<td>gh</td>',
392+
'</tr>',
393+
'</table>',
394+
].join(''),
395+
contentAfter: [
396+
'<table>',
397+
'<thead>',
398+
'<tr>',
399+
'<td>ab</td>',
400+
'<td>cd</td>',
401+
'</tr>',
402+
'</thead>',
403+
'<tbody>',
404+
'<tr>',
405+
'<td>ef</td>',
406+
'<td><br></td>',
407+
'</tr>',
408+
'<tr>',
409+
'<td>gh</td>',
410+
'<td><br></td>',
411+
'</tr>',
412+
'</tbody>',
413+
'</table>',
414+
].join(''),
415+
/* eslint-enable prettier/prettier */
416+
});
417+
});
376418
});
377419
describe('parse string and render in the DOM', () => {
378420
let editor: BasicEditor;
@@ -785,6 +827,52 @@ describePlugin(Table, testEditor => {
785827
].join(''));
786828
/* eslint-enable prettier/prettier */
787829
});
830+
it('should parse and render a table with inconsistent cell numbers', async () => {
831+
/* eslint-disable prettier/prettier */
832+
const vNodes = await editor.plugins.get(Parser).parse('text/html', [
833+
'<table>',
834+
'<thead>',
835+
'<tr>',
836+
'<td>ab</td>',
837+
'<td>cd</td>',
838+
'</tr>',
839+
'</thead>',
840+
'<tr>',
841+
'<td>ef</td>',
842+
'</tr>',
843+
'<tr>',
844+
'<td>gh</td>',
845+
'</tr>',
846+
'</table>',
847+
].join(''));
848+
expect(vNodes.length).to.equal(1);
849+
850+
const renderer = editor.plugins.get(Renderer);
851+
const domNodes = await renderer.render('dom/html', vNodes[0]);
852+
const table = domNodes[0] as HTMLTableElement;
853+
/* eslint-disable prettier/prettier */
854+
expect(table.outerHTML).to.equal([
855+
'<table>',
856+
'<thead>',
857+
'<tr>',
858+
'<td>ab</td>',
859+
'<td>cd</td>',
860+
'</tr>',
861+
'</thead>',
862+
'<tbody>',
863+
'<tr>',
864+
'<td>ef</td>',
865+
'<td><br></td>',
866+
'</tr>',
867+
'<tr>',
868+
'<td>gh</td>',
869+
'<td><br></td>',
870+
'</tr>',
871+
'</tbody>',
872+
'</table>',
873+
].join(''));
874+
/* eslint-enable prettier/prettier */
875+
});
788876
});
789877
describe('addRowAbove', () => {
790878
beforeEach(async () => {

0 commit comments

Comments
 (0)