Skip to content

Commit 12e2739

Browse files
Multiple ignored CSS blocks
If there are multiple ignored CSS blocks within a single clean-css warning message, only the first is replaced with the ignore code. This means that some ingored blocks are simply deleted from the output. The problem is simply that the RegExp is only applied once, despite having the global flag set on it. This is fixed by calling `exec` on the RegExp until it yields no further matches. Added a test for this error case. Fixes terser#180
1 parent c4a7ae0 commit 12e2739

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/htmlminifier.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -905,8 +905,8 @@ async function minifyHTML(value, options, partialMarkup) {
905905

906906
const ids = [];
907907
new CleanCSS().minify(wrapCSS(text, type)).warnings.forEach(function (warning) {
908-
const match = uidPattern.exec(warning);
909-
if (match) {
908+
let match;
909+
while ((match = uidPattern.exec(warning))) {
910910
const id = uidAttr + match[2] + uidAttr;
911911
text = text.replace(id, ignoreCSS(id));
912912
ids.push(id);

tests/minifier.spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -3594,3 +3594,14 @@ test('minify Content-Security-Policy', async () => {
35943594
input = '<meta http-equiv="content-security-policy" content="default-src \'self\'; img-src https://*;">';
35953595
expect(await minify(input)).toBe(input);
35963596
});
3597+
3598+
test('minify CSS multiple ignore in single warning', async () => {
3599+
const input = '<style type="text/css">\n{% ignore1 %}\na {\n{% ignore2 %}\n}\n{% ignore3 %}\n</style>';
3600+
const output = '<style type="text/css">\n{% ignore1 %}\na{\n{% ignore2 %}\n}\n{% ignore3 %}\n</style>';
3601+
expect(await minify(input, {
3602+
ignoreCustomFragments: [/\{%[\s\S]*?%\}/],
3603+
minifyCSS: {
3604+
level: 0
3605+
}
3606+
})).toBe(output);
3607+
});

0 commit comments

Comments
 (0)