From 38c90d239ede0633295de2294a59a7a69ae898d7 Mon Sep 17 00:00:00 2001 From: Adam Pash Date: Wed, 26 Oct 2016 11:47:27 -0700 Subject: [PATCH] fix: removeEmpty shouldn't remove elements with images or iframes inside --- src/utils/dom/remove-empty.js | 2 +- src/utils/dom/remove-empty.test.js | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/utils/dom/remove-empty.js b/src/utils/dom/remove-empty.js index 46346ba9..523c7769 100644 --- a/src/utils/dom/remove-empty.js +++ b/src/utils/dom/remove-empty.js @@ -1,7 +1,7 @@ export default function removeEmpty($article, $) { $article.find('p').each((index, p) => { const $p = $(p); - if ($p.text().trim() === '') $p.remove(); + if ($p.find('iframe, img').length === 0 && $p.text().trim() === '') $p.remove(); }); return $; diff --git a/src/utils/dom/remove-empty.test.js b/src/utils/dom/remove-empty.test.js index 7c7ab911..9cd4cfdd 100644 --- a/src/utils/dom/remove-empty.test.js +++ b/src/utils/dom/remove-empty.test.js @@ -27,5 +27,13 @@ describe('removeEmpty($)', () => { const result = removeEmpty($('*').first(), $); assertClean(result.html(), HTML.removeEmptyP.after); }); + + it('does not remove empty p tags containing an iframe', () => { + const html = '

'; + const $ = cheerio.load(html); + + const result = removeEmpty($('*').first(), $); + assertClean(result.html(), html); + }); });