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

Fix(collapseWhitespace): handle textNode when comment is preserved #184

Merged
merged 1 commit into from
Apr 6, 2022
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
5 changes: 4 additions & 1 deletion lib/helpers.es6
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export function isAmpBoilerplate(node) {
}

export function isComment(content) {
return (content || '').trim().startsWith('<!--');
if (typeof content === 'string') {
return content.trim().startsWith('<!--');
}
return false;
}

export function isConditionalComment(content) {
Expand Down
12 changes: 10 additions & 2 deletions lib/modules/collapseWhitespace.es6
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ function collapseRedundantWhitespaces(text, collapseType, shouldTrim = false, pa
if (shouldTrim) {
if (collapseType === 'aggressive') {
if (!noTrimWhitespacesInsideElements.has(parent && parent.node && parent.node.tag)) {
if (!noTrimWhitespacesArroundElements.has(prevNode && prevNode.tag)) {
if (
// It is the first child node of the parent
!prevNode
// It is not the first child node, and prevNode not a text node, and prevNode is safe to trim around
|| prevNode && prevNode.tag && !noTrimWhitespacesArroundElements.has(prevNode.tag)
) {
text = text.trimStart();
} else {
// previous node is a "no trim whitespaces arround element"
Expand All @@ -99,7 +104,10 @@ function collapseRedundantWhitespaces(text, collapseType, shouldTrim = false, pa
}
}

if (!noTrimWhitespacesArroundElements.has(nextNode && nextNode.tag)) {
if (
!nextNode
|| nextNode && nextNode.tag && !noTrimWhitespacesArroundElements.has(nextNode.tag)
) {
text = text.trimEnd();
}
} else {
Expand Down
8 changes: 8 additions & 0 deletions test/modules/collapseWhitespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ describe('collapseWhitespace', () => {
);
});

it('handle whitespace along with comment', () => {
return init(
'<div>before<!-- --> <!-- --><a href="#link"></a> <!-- --> <!-- --> after</div>',
'<div>before<!-- --> <!-- --><a href="#link"></a> <!-- --> <!-- --> after</div>',
options
);
});

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