From c085ebd092e9abba768c09c78fa63829c426cf79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89rico=20Andrei?= Date: Fri, 10 Mar 2023 14:00:33 -0300 Subject: [PATCH] fix: Remove elements without any text content --- src/converters/fromHtml.js | 3 ++- src/converters/fromHtml.test.js | 29 +++++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/converters/fromHtml.js b/src/converters/fromHtml.js index e057933..cd25d3a 100644 --- a/src/converters/fromHtml.js +++ b/src/converters/fromHtml.js @@ -45,7 +45,8 @@ const elementsShouldHaveText = [ const shouldKeepWrapper = (el) => { if (elementsShouldHaveText.includes(el.tagName)) { - return el.textContent ? true : false; + const textContent = el.textContent.trim(); + return textContent ? true : false; } return true; }; diff --git a/src/converters/fromHtml.test.js b/src/converters/fromHtml.test.js index 01578af..d8aced2 100644 --- a/src/converters/fromHtml.test.js +++ b/src/converters/fromHtml.test.js @@ -25,7 +25,7 @@ describe('convertFromHTML parsing html', () => { const result = convertFromHTML(html, 'draftjs'); test('will return an array of blocks', () => { - expect(result).toHaveLength(11); + expect(result).toHaveLength(10); }); test('will have a first block with text content', () => { @@ -46,7 +46,7 @@ describe('convertFromHTML parsing html', () => { const result = convertFromHTML(html, 'slate'); test('will return an array of blocks', () => { - expect(result).toHaveLength(11); + expect(result).toHaveLength(10); }); test('will have a first block with text content', () => { @@ -220,7 +220,7 @@ describe('convertFromHTML parsing html with nested divs', () => { const result = convertFromHTML(html, 'slate'); test('will return an array of blocks', () => { - expect(result).toHaveLength(8); + expect(result).toHaveLength(7); }); test('will have a paragraph with a nested p', () => { @@ -429,7 +429,7 @@ describe('convertFromHTML parsing image', () => { `; const result = convertFromHTML(html, 'slate'); - expect(result).toHaveLength(2); + expect(result).toHaveLength(1); expect(result[0]).toEqual({ '@type': 'image', align: 'center', @@ -506,3 +506,24 @@ describe('convertFromHTML parsing image', () => { }); }); }); + +describe('convertFromHTML parsing nested tags', () => { + describe('with an image and without line breaks', () => { + const html = `
+

+

Text, text, text

+
`; + const result = convertFromHTML(html, 'slate'); + expect(result).toHaveLength(2); + }); + describe('with an image and with an additional line break', () => { + const html = `
+
+

+
+

Text, text, text

+
`; + const result = convertFromHTML(html, 'slate'); + expect(result).toHaveLength(2); + }); +});