Skip to content

Commit

Permalink
fix(postcss-discard-comments): remove comments in selectors adjacent …
Browse files Browse the repository at this point in the history
…to spaces (#1621)
  • Loading branch information
ota-meshi committed Jun 9, 2024
1 parent ffcebc7 commit 1953e30
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
23 changes: 14 additions & 9 deletions packages/postcss-discard-comments/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,18 @@ function pluginCreator(opts = {}) {
return;
}

if (
node.type === 'rule' &&
node.raws.selector &&
node.raws.selector.raw
) {
node.raws.selector.raw = replaceCommentsInSelector(
node.raws.selector.raw,
list.space
);
if (node.type === 'rule') {
if (node.raws.selector && node.raws.selector.raw) {
node.raws.selector.raw = replaceCommentsInSelector(
node.raws.selector.raw,
list.space
);
} else if (node.selector && node.selector.includes('/*')) {
node.selector = replaceCommentsInSelector(
node.selector,
list.space
);
}

return;
}
Expand All @@ -181,6 +184,8 @@ function pluginCreator(opts = {}) {
node.raws.params.raw,
list.space
);
} else if (node.params && node.params.includes('/*')) {
node.params = replaceComments(node.params, list.space);
}
}
});
Expand Down
18 changes: 18 additions & 0 deletions packages/postcss-discard-comments/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,24 @@ test(
)
);

test(
'should remove comments in selector',
processCSS('.x/*comment*/.y{color:#000}', '.x.y{color:#000}')
);

test(
'should remove comments in pseudo-class-function',
processCSS(':not(/*comment*/.foo){color:#000}', ':not(.foo){color:#000}')
);

test(
'should remove comments in @rule param',
processCSS(
'@media/*a*/screen/*b*/and/*c*/(min-width:/*d*/900px)/*e*/{color: red}',
'@media screen and (min-width: 900px){color:red}'
)
);

test(
"should pass through when it doesn't find a comment",
passthroughCSS('h1{color:#000;font-weight:700}')
Expand Down

0 comments on commit 1953e30

Please sign in to comment.