Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: strip more spaces for collapseWhitespace #112

Merged
merged 2 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
48 changes: 44 additions & 4 deletions test/modules/collapseWhitespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,23 @@ 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>
<div>
lorem
<span>
opren
</span>
</div>
`;

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

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

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

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


context('aggressive', () => {
const options = {
collapseWhitespace: 'aggressive',
Expand Down Expand Up @@ -104,6 +128,14 @@ describe('collapseWhitespace', () => {
);
});

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

it('renders the documentation example correctly', () => {
return init(
documentationHtml,
Expand Down Expand Up @@ -144,6 +176,14 @@ describe('collapseWhitespace', () => {
);
});

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

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