From a7e914ff9464f09be5a78d11835c214e48aec615 Mon Sep 17 00:00:00 2001 From: Jacob Bare Date: Mon, 20 May 2019 13:08:09 -0500 Subject: [PATCH 1/2] Do not strip single spaces inside elements --- src/utils/strip-whitespace.js | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/src/utils/strip-whitespace.js b/src/utils/strip-whitespace.js index c04489a..e01cd69 100644 --- a/src/utils/strip-whitespace.js +++ b/src/utils/strip-whitespace.js @@ -1,28 +1,11 @@ -const cheerio = require('cheerio'); - -const cleanElementText = ($, $el) => { - const contents = $el.contents()[0]; - - if (contents && contents.type === 'text') { - const { data = '' } = contents; - const cleaned = data.replace(/\s\s+/g, ' ').trim(); - contents.data = cleaned; - } - if ($el.children().length) { - $el.children().each(function () { - cleanElementText($, $(this)); - }); - } -}; - module.exports = (html) => { const str = (html || '') - .replace(/[\r\n\f\v\t\b\\]/g, ' ') + .replace(/[\r\n\f\v\t\b\\]/g, ' ') + .replace(/ /g, ' ') .trim() .replace(/>\s+<') .replace(/\s+%{\[/g, '%{[') - .replace(/\]}%\s+/g, ']}%'); - const $ = cheerio.load(str, { decodeEntities: false }); - cleanElementText($, $('body')); - return $('body').html(); + .replace(/\]}%\s+/g, ']}%') + .replace(/\s\s+/g, ' '); + return str; }; From 5cae153460b3276c7e1baf31895dd1531d762a4c Mon Sep 17 00:00:00 2001 From: Jacob Bare Date: Mon, 20 May 2019 13:08:17 -0500 Subject: [PATCH 2/2] Update strip whitepaces tests --- test/rules/pennwell/default.spec.js | 2 +- test/utils/strip-whitespace.spec.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/rules/pennwell/default.spec.js b/test/rules/pennwell/default.spec.js index 8493b1d..ec1f532 100644 --- a/test/rules/pennwell/default.spec.js +++ b/test/rules/pennwell/default.spec.js @@ -23,7 +23,7 @@ describe('rules/pennwell/default', () => {

Hello, World

\t `; const result = await rule(body); - expect(result.html.cleaned).to.equal('
BarFoo BarBaz Bar

Hello, World

'); + expect(result.html.cleaned).to.equal('
Bar Foo BarBaz Bar

Hello, World

'); }); it('should remove
elements.', async () => { const body = ` diff --git a/test/utils/strip-whitespace.spec.js b/test/utils/strip-whitespace.spec.js index feba3b6..011898d 100644 --- a/test/utils/strip-whitespace.spec.js +++ b/test/utils/strip-whitespace.spec.js @@ -13,7 +13,7 @@ describe('utils/strip-whitespace', () => {

Hello, World

\t `; const result = stripWhitespace(body); - expect(result).to.equal('
BarFooBar FooBaz Bar

Hello, World

'); + expect(result).to.equal('
Bar Foo Bar FooBaz Bar

Hello, World

'); }); it('should return an empty string for null values.', async () => { expect(stripWhitespace()).to.equal('');