From efd0e952f59f1af153a0147cfe3404d5b9b67194 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Wed, 21 Oct 2020 12:42:34 +0800 Subject: [PATCH 1/2] Feat: strip more spaces for collapseWhitespace --- lib/modules/collapseWhitespace.es6 | 11 ++++++ test/modules/collapseWhitespace.js | 60 ++++++++++++++++++++++++++++-- 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/lib/modules/collapseWhitespace.es6 b/lib/modules/collapseWhitespace.es6 index 5ef611b..3d04ee6 100644 --- a/lib/modules/collapseWhitespace.es6 +++ b/lib/modules/collapseWhitespace.es6 @@ -76,6 +76,17 @@ function collapseRedundantWhitespaces(text, collapseType, shouldTrim = false, cu if (shouldTrim) { if (collapseType === 'aggressive') { if (onlyWhitespacePattern.test(text)) { + if (!noTrimWhitespacesArroundElements.has(currentTag)) { + // Remove very first and very end spaces inside text node + if (typeof prevNodeTag === 'undefined') { + text = text.trimStart(); + } + + if (typeof nextNodeTag === 'undefined') { + text = text.trimEnd(); + } + } + // "text" only contains whitespaces. Only trim when both prevNodeTag & nextNodeTag are not "noTrimWhitespacesArroundElement" // Otherwise the required ONE whitespace will be trimmed if ( diff --git a/test/modules/collapseWhitespace.js b/test/modules/collapseWhitespace.js index ed2dcbb..39cd30b 100644 --- a/test/modules/collapseWhitespace.js +++ b/test/modules/collapseWhitespace.js @@ -10,7 +10,17 @@ describe('collapseWhitespace', () => { posthtml htmlnano hello world! other link - Example `; + Example `; + + const spaceInsideTextNodeHtml = ` +
+ lorem + + iorem + + +
+`; const documentationHtml = `
hello world! @@ -46,7 +56,7 @@ describe('collapseWhitespace', () => { options ); }); - + it('should not collapse whitespaces inside ' + inviolateTags, () => { return init( inviolateTagsHtml, @@ -64,6 +74,14 @@ describe('collapseWhitespace', () => { ); }); + it('should collapse whitespaces inside text node', () => { + return init( + spaceInsideTextNodeHtml, + '
loremiorem
', + options + ); + }); + it('renders the documentation example correctly', () => { return init( documentationHtml, @@ -72,8 +90,8 @@ describe('collapseWhitespace', () => { ); }); }); - - + + context('aggressive', () => { const options = { collapseWhitespace: 'aggressive', @@ -104,6 +122,14 @@ describe('collapseWhitespace', () => { ); }); + it('should collapse whitespaces inside text node', () => { + return init( + spaceInsideTextNodeHtml, + '
lorem iorem
', + options + ); + }); + it('renders the documentation example correctly', () => { return init( documentationHtml, @@ -111,6 +137,24 @@ describe('collapseWhitespace', () => { options ); }); + + it('test', () => { + const html = ` +`; + + const expected = ''; + + return init( + html, + expected, + options + ); + }); }); @@ -144,6 +188,14 @@ describe('collapseWhitespace', () => { ); }); + it('should collapse whitespaces inside text node', () => { + return init( + spaceInsideTextNodeHtml, + '
lorem iorem
', + options + ); + }); + it('renders the documentation example correctly', () => { return init( documentationHtml, From 499b70f041fa4d3ff245600e063c5d7aca806065 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Wed, 21 Oct 2020 12:49:25 +0800 Subject: [PATCH 2/2] Test: update test cases for collapseWhitespace --- test/modules/collapseWhitespace.js | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/test/modules/collapseWhitespace.js b/test/modules/collapseWhitespace.js index 39cd30b..c054ecf 100644 --- a/test/modules/collapseWhitespace.js +++ b/test/modules/collapseWhitespace.js @@ -20,6 +20,12 @@ describe('collapseWhitespace', () => {
+
+ lorem + + opren + +
`; const documentationHtml = `
@@ -77,7 +83,7 @@ describe('collapseWhitespace', () => { it('should collapse whitespaces inside text node', () => { return init( spaceInsideTextNodeHtml, - '
loremiorem
', + '
loremiorem
loremopren
', options ); }); @@ -125,7 +131,7 @@ describe('collapseWhitespace', () => { it('should collapse whitespaces inside text node', () => { return init( spaceInsideTextNodeHtml, - '
lorem iorem
', + '
lorem iorem
lorem opren
', options ); }); @@ -137,24 +143,6 @@ describe('collapseWhitespace', () => { options ); }); - - it('test', () => { - const html = ` -`; - - const expected = ''; - - return init( - html, - expected, - options - ); - }); }); @@ -191,7 +179,7 @@ describe('collapseWhitespace', () => { it('should collapse whitespaces inside text node', () => { return init( spaceInsideTextNodeHtml, - '
lorem iorem
', + '
lorem iorem
lorem opren
', options ); });