Skip to content

Commit

Permalink
fix: Remove elements without any text content
Browse files Browse the repository at this point in the history
  • Loading branch information
ericof committed Mar 10, 2023
1 parent 50b4290 commit c085ebd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/converters/fromHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
29 changes: 25 additions & 4 deletions src/converters/fromHtml.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -506,3 +506,24 @@ describe('convertFromHTML parsing image', () => {
});
});
});

describe('convertFromHTML parsing nested tags', () => {
describe('with an image and without line breaks', () => {
const html = `<div>
<div><p><span><img src="image.jpg" /></span></p></div>
<p ><span><a href="link"><span><span><span>Text</span></span></span></a>, </span><a href="link"><span>text</span></a>, <a href="link"><span><span>text</span></span> </a></p>
</div>`;
const result = convertFromHTML(html, 'slate');
expect(result).toHaveLength(2);
});
describe('with an image and with an additional line break', () => {
const html = `<div>
<div>
<p><span><img src="image.jpg" /></span></p>
</div>
<p ><span><a href="link"><span><span><span>Text</span></span></span></a>, </span><a href="link"><span>text</span></a>, <a href="link"><span><span>text</span></span> </a></p>
</div>`;
const result = convertFromHTML(html, 'slate');
expect(result).toHaveLength(2);
});
});

0 comments on commit c085ebd

Please sign in to comment.