Skip to content

Commit 532cd32

Browse files
yaodingydsindresorhus
authored andcommitted
Add whitespace option (#21)
1 parent 814512b commit 532cd32

File tree

6 files changed

+52
-8
lines changed

6 files changed

+52
-8
lines changed

bench.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ bench('strip CSS comments', () => {
1212
bench('preserve option', () => {
1313
stripCssComments(fixture, {preserve: /^!/});
1414
});
15+
16+
bench('whitespace option', () => {
17+
stripCssComments(fixture, {whitespace: false});
18+
});

index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ declare namespace stripCssComments {
99
@default true
1010
*/
1111
readonly preserve?: boolean | RegExp | ((comment: string) => boolean);
12+
13+
/**
14+
Replace comments with whitespace instead of stripping them entirely.
15+
16+
@default true
17+
*/
18+
readonly whitespace?: boolean;
1219
}
1320
}
1421

index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const isRegExp = require('is-regexp');
33

44
module.exports = (cssString, options = {}) => {
55
let preserveImportant = !(options.preserve === false || options.all === true);
6+
const stripWhitespace = options.whitespace === false;
67

78
let preserveFilter;
89
if (typeof options.preserve === 'function') {
@@ -41,11 +42,14 @@ module.exports = (cssString, options = {}) => {
4142
for (; j < cssString.length; j++) {
4243
// Find end of comment
4344
if (cssString[j] === '*' && cssString[j + 1] === '/') {
44-
if (preserveImportant && isImportantComment) {
45+
if ((preserveImportant && isImportantComment) || (preserveFilter && preserveFilter(comment))) {
4546
returnValue += `/*${comment}*/`;
46-
} else if (preserveFilter) {
47-
// Evaluate comment text
48-
returnValue = preserveFilter(comment) ? returnValue + `/*${comment}*/` : returnValue;
47+
} else if (stripWhitespace) {
48+
if (cssString[j + 2] === '\n') {
49+
j++;
50+
} else if (cssString[j + 2] + cssString[j + 3] === '\r\n') {
51+
j += 2;
52+
}
4953
}
5054

5155
comment = '';

index.test-d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,8 @@ expectType<string>(
2020
preserve: comment => comment.charAt(0) === '#'
2121
})
2222
);
23+
expectType<string>(
24+
stripCssComments('/*# preserved */ body { /* unicorns */color: hotpink; }', {
25+
whitespace: false
26+
})
27+
);

readme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ Default: `true`
7070
- `RegExp` - Preserve comments where the comment body matches a regular expression.
7171
- `Function` - Preserve comments for which a function returns `true`. The function is called on each comment, gets the comment body as the first argument, and is expected to return a boolean of whether to preserve the comment.
7272

73+
### whitespace
74+
75+
Type: `boolean`<br>
76+
Default: `true`
77+
78+
Replace comments with whitespace instead of stripping them entirely.
7379

7480
## Benchmark
7581

test.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,26 @@ test('main', t => {
9898
);
9999
});
100100

101-
test.failing('strips trailing comment newline', t => {
102-
t.is(stripCssComments('/* foo */\n\nbody{}'), '\nbody{}');
103-
t.is(stripCssComments('/* foo */\r\n\r\nbody{}'), '\nbody{}');
104-
t.is(stripCssComments('/*! foo */\r\n\r\nbody{}'), '/*! foo */\r\n\r\nbody{}');
101+
test('`whitespace` option', t => {
102+
t.is(stripCssComments('/* foo */\n\nbody{}', {whitespace: false}), '\nbody{}');
103+
t.is(stripCssComments('/* foo */\r\n\r\nbody{}', {whitespace: false}), '\r\nbody{}');
104+
t.is(stripCssComments('/*! foo */\r\n\r\nbody{}', {whitespace: false}), '/*! foo */\r\n\r\nbody{}');
105+
t.is(stripCssComments('/*! foo */\r\n\r\nbody{}', {preserve: false, whitespace: false}), '\r\nbody{}');
106+
107+
t.is(stripCssComments('/*##foo##*/\nbody{}', {preserve: /^##foo##/, whitespace: false}), '/*##foo##*/\nbody{}');
108+
t.is(stripCssComments('/*##foo##*/\r\nbody{}', {preserve: /^##foo##/, whitespace: false}), '/*##foo##*/\r\nbody{}');
109+
110+
t.is(
111+
stripCssComments('body{/*!##foo*/\n/*foo*/}', {
112+
preserve: comment => comment.endsWith('foo'), whitespace: false
113+
}),
114+
'body{/*!##foo*/\n/*foo*/}'
115+
);
116+
117+
t.is(
118+
stripCssComments('body{/*!##foo*/\r\n/*foo*/}', {
119+
preserve: comment => comment.endsWith('foo'), whitespace: false
120+
}),
121+
'body{/*!##foo*/\r\n/*foo*/}'
122+
);
105123
});

0 commit comments

Comments
 (0)