Skip to content

Commit

Permalink
Desktop, Cli: Fix importing certain ENEX notes that include invalid t…
Browse files Browse the repository at this point in the history
…ables
  • Loading branch information
laurent22 committed Dec 31, 2023
1 parent a6df9f1 commit 229b9a5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
@@ -1,4 +1,5 @@
<table>
<div></div> <!-- INVALID! -->
<tr>
<td>one</td>
<td>two</td>
Expand Down
36 changes: 22 additions & 14 deletions packages/lib/import-enex-md-gen.test.ts
Expand Up @@ -142,20 +142,28 @@ describe('import-enex-md-gen', () => {
expect(all[0].mime).toBe('application/zip');
});

it('should keep importing notes when one of them is corrupted', async () => {
const filePath = `${enexSampleBaseDir}/ImportTestCorrupt.enex`;
const errors: any[] = [];
await importEnex('', filePath, {
onError: (error: any) => errors.push(error),
});
const notes = await Note.all();
expect(notes.length).toBe(2);

// Check that an error was recorded and that it includes the title
// of the note, so that it can be found back by the user
expect(errors.length).toBe(1);
expect(errors[0].message.includes('Note 2')).toBe(true);
});
// Disabled for now because the ENEX parser has become so error-tolerant
// that it's no longer possible to generate a note that would generate a
// failure.

// it('should keep importing notes when one of them is corrupted', async () => {
// const filePath = `${enexSampleBaseDir}/ImportTestCorrupt.enex`;
// const errors: any[] = [];
// const consoleSpy = jest.spyOn(console, 'warn').mockImplementation(jest.fn());
// await importEnex('', filePath, {
// onError: (error: any) => errors.push(error),
// });
// consoleSpy.mockRestore();
// const notes:NoteEntity[] = await Note.all();
// expect(notes.length).toBe(2);
// expect(notes.find(n => n.title === 'Note 1')).toBeTruthy();
// expect(notes.find(n => n.title === 'Note 3')).toBeTruthy();

// // Check that an error was recorded and that it includes the title
// // of the note, so that it can be found back by the user
// expect(errors.length).toBe(1);
// expect(errors[0].message.includes('Note 2')).toBe(true);
// });

it('should throw an error and stop if the outer XML is invalid', async () => {
await expectThrow(async () => importEnexFile('invalid_html.enex'));
Expand Down
13 changes: 9 additions & 4 deletions packages/lib/import-enex-md-gen.ts
Expand Up @@ -1239,6 +1239,14 @@ function drawTable(table: Section) {
continue;
}

if (typeof tr === 'string') {
// A <TABLE> tag should only have <TR> tags as direct children.
// However certain Evernote notes can contain other random tags
// such as empty DIVs. In that case we just skip the content.
// See test "table_with_invalid_content.html".
continue;
}

const isHeader = tr.isHeader;
const line = [];
const headerLine = [];
Expand All @@ -1247,10 +1255,7 @@ function drawTable(table: Section) {
const td = tr.lines[tdIndex];

if (typeof td === 'string') {
// A <TR> tag should only have <TD> tags as direct children.
// However certain Evernote notes can contain other random tags
// such as empty DIVs. In that case we just skip the content.
// See test "table_with_invalid_content.html".
// Same comment as above the <TR> tags.
continue;
}

Expand Down

0 comments on commit 229b9a5

Please sign in to comment.