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..c054ecf 100644 --- a/test/modules/collapseWhitespace.js +++ b/test/modules/collapseWhitespace.js @@ -10,7 +10,23 @@ describe('collapseWhitespace', () => { posthtml htmlnano hello world! other link - Example `; + Example `; + + const spaceInsideTextNodeHtml = ` +
+ lorem + + iorem + + +
+
+ lorem + + opren + +
+`; const documentationHtml = `
hello world! @@ -46,7 +62,7 @@ describe('collapseWhitespace', () => { options ); }); - + it('should not collapse whitespaces inside ' + inviolateTags, () => { return init( inviolateTagsHtml, @@ -64,6 +80,14 @@ describe('collapseWhitespace', () => { ); }); + it('should collapse whitespaces inside text node', () => { + return init( + spaceInsideTextNodeHtml, + '
loremiorem
loremopren
', + options + ); + }); + it('renders the documentation example correctly', () => { return init( documentationHtml, @@ -72,8 +96,8 @@ describe('collapseWhitespace', () => { ); }); }); - - + + context('aggressive', () => { const options = { collapseWhitespace: 'aggressive', @@ -104,6 +128,14 @@ describe('collapseWhitespace', () => { ); }); + it('should collapse whitespaces inside text node', () => { + return init( + spaceInsideTextNodeHtml, + '
lorem iorem
lorem opren
', + options + ); + }); + it('renders the documentation example correctly', () => { return init( documentationHtml, @@ -144,6 +176,14 @@ describe('collapseWhitespace', () => { ); }); + it('should collapse whitespaces inside text node', () => { + return init( + spaceInsideTextNodeHtml, + '
lorem iorem
lorem opren
', + options + ); + }); + it('renders the documentation example correctly', () => { return init( documentationHtml,