fix: removeEmpty shouldn't remove elements with images or iframes inside

This commit is contained in:
Adam Pash 2016-10-26 11:47:27 -07:00
parent c63f500433
commit 38c90d239e
2 changed files with 9 additions and 1 deletions

View File

@ -1,7 +1,7 @@
export default function removeEmpty($article, $) { export default function removeEmpty($article, $) {
$article.find('p').each((index, p) => { $article.find('p').each((index, p) => {
const $p = $(p); const $p = $(p);
if ($p.text().trim() === '') $p.remove(); if ($p.find('iframe, img').length === 0 && $p.text().trim() === '') $p.remove();
}); });
return $; return $;

View File

@ -27,5 +27,13 @@ describe('removeEmpty($)', () => {
const result = removeEmpty($('*').first(), $); const result = removeEmpty($('*').first(), $);
assertClean(result.html(), HTML.removeEmptyP.after); assertClean(result.html(), HTML.removeEmptyP.after);
}); });
it('does not remove empty p tags containing an iframe', () => {
const html = '<div><p><span><iframe src="foo"></iframe></span></p></div>';
const $ = cheerio.load(html);
const result = removeEmpty($('*').first(), $);
assertClean(result.html(), html);
});
}); });