Skip to content

Commit 814512b

Browse files
mika-cnsindresorhus
authored andcommitted
Ignore string separator in important comments (#19)
1 parent 9710fbc commit 814512b

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

index.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,33 @@ module.exports = (cssString, options = {}) => {
3434
// Find beginning of `/*` type comment
3535
if (!isInsideString && currentCharacter === '/' && cssString[i + 1] === '*') {
3636
// Ignore important comment when configured to preserve comments using important syntax: /*!
37-
if (!(preserveImportant && cssString[i + 2] === '!')) {
38-
let j = i + 2;
37+
const isImportantComment = cssString[i + 2] === '!';
38+
let j = i + 2;
3939

40-
// Iterate over comment
41-
for (; j < cssString.length; j++) {
42-
// Find end of comment
43-
if (cssString[j] === '*' && cssString[j + 1] === '/') {
44-
if (preserveFilter) {
45-
// Evaluate comment text
46-
returnValue = preserveFilter(comment) ? returnValue + ('/*' + comment + '*/') : returnValue;
47-
comment = '';
48-
}
49-
50-
break;
40+
// Iterate over comment
41+
for (; j < cssString.length; j++) {
42+
// Find end of comment
43+
if (cssString[j] === '*' && cssString[j + 1] === '/') {
44+
if (preserveImportant && isImportantComment) {
45+
returnValue += `/*${comment}*/`;
46+
} else if (preserveFilter) {
47+
// Evaluate comment text
48+
returnValue = preserveFilter(comment) ? returnValue + `/*${comment}*/` : returnValue;
5149
}
5250

53-
// Store comment text to be evaluated by the filter when the end of the comment is reached
54-
if (preserveFilter) {
55-
comment += cssString[j];
56-
}
57-
}
51+
comment = '';
5852

59-
// Resume iteration over CSS string from the end of the comment
60-
i = j + 1;
53+
break;
54+
}
6155

62-
continue;
56+
// Store comment text
57+
comment += cssString[j];
6358
}
59+
60+
// Resume iteration over CSS string from the end of the comment
61+
i = j + 1;
62+
63+
continue;
6464
}
6565

6666
returnValue += currentCharacter;

test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,57 @@ test('main', t => {
88
t.is(stripCssComments('body{content: "\'/*ad*/\' \\""}'), 'body{content: "\'/*ad*/\' \\""}');
99
t.is(stripCssComments('body{\r\n /*\n\n\n\nfoo*/\n}'), 'body{\r\n \n}');
1010
t.is(stripCssComments('body/*foo*/{}'), 'body{}');
11+
t.is(stripCssComments('body{/*"*/}'), 'body{}');
12+
t.is(stripCssComments('body{/*\'*/}'), 'body{}');
1113
t.is(stripCssComments('body{/*"\'\\"*/}'), 'body{}');
14+
t.is(stripCssComments('body{/*"\'"\'*/}'), 'body{}');
1215

1316
t.is(stripCssComments('/*!//comment*/body{}'), '/*!//comment*/body{}');
17+
t.is(stripCssComments('/*!//"comment*/body{/*//comment*/}'), '/*!//"comment*/body{}');
18+
t.is(stripCssComments('/*!//\'comment*/body{/*//comment*/}'), '/*!//\'comment*/body{}');
1419
t.is(stripCssComments('body{/*!comment*/}'), 'body{/*!comment*/}');
1520
t.is(stripCssComments('body{/*!\ncomment\n\\*/}'), 'body{/*!\ncomment\n\\*/}');
1621
t.is(stripCssComments('body{content: "\'/*!ad*/\' \\""}'), 'body{content: "\'/*!ad*/\' \\""}');
1722
t.is(stripCssComments('body{\r\n /*!\n\n\n\nfoo*/\n}'), 'body{\r\n /*!\n\n\n\nfoo*/\n}');
1823
t.is(stripCssComments('body/*!foo*/{}'), 'body/*!foo*/{}');
24+
t.is(stripCssComments('body{/*!"*/}/*foo*/'), 'body{/*!"*/}');
25+
t.is(stripCssComments('body{/*!\'*/}/*foo*/'), 'body{/*!\'*/}');
1926
t.is(stripCssComments('body{/*!"\'\\"*/}'), 'body{/*!"\'\\"*/}');
27+
t.is(stripCssComments('body{/*!"\'"\'*/}'), 'body{/*!"\'"\'*/}');
2028

2129
t.is(stripCssComments('/*!//comment*/body{}', {all: true}), 'body{}');
2230
t.is(stripCssComments('/*!//comment*/body{}', {all: true}), 'body{}');
31+
t.is(stripCssComments('/*!//"comment*/body{}', {all: true}), 'body{}');
32+
t.is(stripCssComments('/*!//\'comment*/body{}', {all: true}), 'body{}');
2333
t.is(stripCssComments('body{/*!comment*/}', {all: true}), 'body{}');
2434
t.is(stripCssComments('body{/*!\ncomment\n\\*/}', {all: true}), 'body{}');
2535
t.is(stripCssComments('body{content: "\'/*!ad*/\' \\""}', {all: true}), 'body{content: "\'/*!ad*/\' \\""}');
2636
t.is(stripCssComments('body{\r\n /*!\n\n\n\nfoo*/\n}', {all: true}), 'body{\r\n \n}');
2737
t.is(stripCssComments('body/*!foo*/{}', {all: true}), 'body{}');
38+
t.is(stripCssComments('body{/*!"*/}/*foo*/', {all: true}), 'body{}');
39+
t.is(stripCssComments('body{/*!\'*/}/*foo*/', {all: true}), 'body{}');
2840
t.is(stripCssComments('body{/*!"\'\\"*/}', {all: true}), 'body{}');
41+
t.is(stripCssComments('body{/*!"\'"\'*/}', {all: true}), 'body{}');
2942

3043
t.is(stripCssComments('/*!//comment*/body{}', {preserve: false}), 'body{}');
44+
t.is(stripCssComments('/*!//"comment*/body{}', {preserve: false}), 'body{}');
45+
t.is(stripCssComments('/*!//\'comment*/body{}', {preserve: false}), 'body{}');
3146
t.is(stripCssComments('body{/*!comment*/}', {preserve: false}), 'body{}');
3247
t.is(stripCssComments('body{/*!\ncomment\n\\*/}', {preserve: false}), 'body{}');
3348
t.is(stripCssComments('body{content: "\'/*!ad*/\' \\""}', {preserve: false}), 'body{content: "\'/*!ad*/\' \\""}');
3449
t.is(stripCssComments('body{\r\n /*!\n\n\n\nfoo*/\n}', {preserve: false}), 'body{\r\n \n}');
3550
t.is(stripCssComments('body/*!foo*/{}', {preserve: false}), 'body{}');
51+
t.is(stripCssComments('body{/*!"*/}/*foo*/', {preserve: false}), 'body{}');
52+
t.is(stripCssComments('body{/*!\'*/}/*foo*/', {preserve: false}), 'body{}');
3653
t.is(stripCssComments('body{/*!"\'\\"*/}', {preserve: false}), 'body{}');
54+
t.is(stripCssComments('body{/*!"\'"\'*/}', {preserve: false}), 'body{}');
3755

3856
t.is(stripCssComments('body{/*##foo##*/}', {preserve: /^##foo##/}), 'body{/*##foo##*/}');
3957
t.is(stripCssComments('body{/*foo*/}', {preserve: /^##foo##/}), 'body{}');
4058
t.is(stripCssComments('body{/*##foo##*//*foo*/}', {preserve: /^##foo##/}), 'body{/*##foo##*/}');
4159
t.is(stripCssComments('body{/*##foo##*//*!foo*/}', {preserve: /^##foo##/}), 'body{/*##foo##*/}');
4260
t.is(stripCssComments('body{/*!##foo##*//*foo*/}', {preserve: /^##foo##/}), 'body{}');
61+
t.is(stripCssComments('body{/*!##foo*//*foo*/}', {preserve: /foo$/}), 'body{/*!##foo*//*foo*/}');
4362

4463
t.is(
4564
stripCssComments('body{/*##foo##*/}', {
@@ -70,6 +89,13 @@ test('main', t => {
7089
preserve: comment => comment.startsWith('##foo##')
7190
}), 'body{}'
7291
);
92+
93+
t.is(
94+
stripCssComments('body{/*!##foo*//*foo*/}', {
95+
preserve: comment => comment.endsWith('foo')
96+
}),
97+
'body{/*!##foo*//*foo*/}'
98+
);
7399
});
74100

75101
test.failing('strips trailing comment newline', t => {

0 commit comments

Comments
 (0)