From d4d48bae356e10d5ce03f8e7e5563cfa94eccf99 Mon Sep 17 00:00:00 2001 From: Ivan Voskoboinyk Date: Tue, 4 Apr 2023 17:44:20 +0300 Subject: [PATCH] [CARE-1352] Do not consider whitespace-only elements empty --- src/__tests__/input/whitespaces-4.html | 21 +++++++++++++++++++++ src/__tests__/output/whitespaces-4.html | 1 + src/cleanDocx.test.ts | 7 +++++++ src/lib/cleanEmptyElements.ts | 2 +- 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/__tests__/input/whitespaces-4.html create mode 100644 src/__tests__/output/whitespaces-4.html diff --git a/src/__tests__/input/whitespaces-4.html b/src/__tests__/input/whitespaces-4.html new file mode 100644 index 0000000..b248d50 --- /dev/null +++ b/src/__tests__/input/whitespaces-4.html @@ -0,0 +1,21 @@ + + + + + + + + +

+ euros +in

+ + \ No newline at end of file diff --git a/src/__tests__/output/whitespaces-4.html b/src/__tests__/output/whitespaces-4.html new file mode 100644 index 0000000..1449f60 --- /dev/null +++ b/src/__tests__/output/whitespaces-4.html @@ -0,0 +1 @@ +

euros in

\ No newline at end of file diff --git a/src/cleanDocx.test.ts b/src/cleanDocx.test.ts index eb7a85f..c938274 100644 --- a/src/cleanDocx.test.ts +++ b/src/cleanDocx.test.ts @@ -45,4 +45,11 @@ describe('cleanDocx', () => { const result = cleanDocx(html, MOCK_RTF); expect(result).toBe(expected); }); + + it('Preserves newline-only spans', () => { + const html = readTestFile('input/whitespaces-4.html'); + const expected = readTestFile('output/whitespaces-4.html'); + const result = cleanDocx(html, MOCK_RTF); + expect(result).toBe(expected); + }); }); diff --git a/src/lib/cleanEmptyElements.ts b/src/lib/cleanEmptyElements.ts index 63cbe28..2a1faa4 100644 --- a/src/lib/cleanEmptyElements.ts +++ b/src/lib/cleanEmptyElements.ts @@ -3,7 +3,7 @@ import { traverseElements } from './traverseElements'; const ALLOWED_EMPTY_ELEMENTS = ['BR', 'IMG']; function isEmpty(element: Element): boolean { - return !ALLOWED_EMPTY_ELEMENTS.includes(element.nodeName) && !element.innerHTML.trim(); + return !ALLOWED_EMPTY_ELEMENTS.includes(element.nodeName) && !element.innerHTML; } function removeIfEmpty(element: Element): void {