Skip to content

Commit

Permalink
Feat: strip more spaces for collapseWhitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Oct 21, 2020
1 parent b9f0d22 commit efd0e95
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
11 changes: 11 additions & 0 deletions lib/modules/collapseWhitespace.es6
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
60 changes: 56 additions & 4 deletions test/modules/collapseWhitespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@ describe('collapseWhitespace', () => {
<code> posthtml htmlnano </code>
<b> hello world! </b> <a>other link
</a>
Example </div> `;
Example </div> `;

const spaceInsideTextNodeHtml = `
<div>
<span> lorem
<span>
iorem
</span>
</span>
</div>
`;

const documentationHtml = `<div>
hello world!
Expand Down Expand Up @@ -46,7 +56,7 @@ describe('collapseWhitespace', () => {
options
);
});

it('should not collapse whitespaces inside ' + inviolateTags, () => {
return init(
inviolateTagsHtml,
Expand All @@ -64,6 +74,14 @@ describe('collapseWhitespace', () => {
);
});

it('should collapse whitespaces inside text node', () => {
return init(
spaceInsideTextNodeHtml,
'<div><span>lorem<span>iorem</span></span></div>',
options
);
});

it('renders the documentation example correctly', () => {
return init(
documentationHtml,
Expand All @@ -72,8 +90,8 @@ describe('collapseWhitespace', () => {
);
});
});


context('aggressive', () => {
const options = {
collapseWhitespace: 'aggressive',
Expand Down Expand Up @@ -104,13 +122,39 @@ describe('collapseWhitespace', () => {
);
});

it('should collapse whitespaces inside text node', () => {
return init(
spaceInsideTextNodeHtml,
'<div><span> lorem <span> iorem </span> </span></div>',
options
);
});

it('renders the documentation example correctly', () => {
return init(
documentationHtml,
'<div>hello world! <a href="#">answer</a> <style>div { color: red; } </style><main></main></div>',
options
);
});

it('test', () => {
const html = `
<div class="post-meta">
<time datetime="2020-10-13T09:25:00.000Z">2020-10-13</time>
<span class="dot">
<span> </span>
</span>
</div>`;

const expected = '<div class="post-meta"><time datetime="2020-10-13T09:25:00.000Z">2020-10-13</time> <span class="dot"> <span> </span> </span></div>';

return init(
html,
expected,
options
);
});
});


Expand Down Expand Up @@ -144,6 +188,14 @@ describe('collapseWhitespace', () => {
);
});

it('should collapse whitespaces inside text node', () => {
return init(
spaceInsideTextNodeHtml,
'<div> <span> lorem <span> iorem </span> </span> </div>',
options
);
});

it('renders the documentation example correctly', () => {
return init(
documentationHtml,
Expand Down

0 comments on commit efd0e95

Please sign in to comment.