Skip to content

Commit

Permalink
Multiple ignored CSS blocks
Browse files Browse the repository at this point in the history
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
  • Loading branch information
chrisstaite-menlo committed Aug 19, 2024
1 parent c4a7ae0 commit 12e2739
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/htmlminifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -905,8 +905,8 @@ async function minifyHTML(value, options, partialMarkup) {

const ids = [];
new CleanCSS().minify(wrapCSS(text, type)).warnings.forEach(function (warning) {
const match = uidPattern.exec(warning);
if (match) {
let match;
while ((match = uidPattern.exec(warning))) {
const id = uidAttr + match[2] + uidAttr;
text = text.replace(id, ignoreCSS(id));
ids.push(id);
Expand Down
11 changes: 11 additions & 0 deletions tests/minifier.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3594,3 +3594,14 @@ test('minify Content-Security-Policy', async () => {
input = '<meta http-equiv="content-security-policy" content="default-src \'self\'; img-src https://*;">';
expect(await minify(input)).toBe(input);
});

test('minify CSS multiple ignore in single warning', async () => {
const input = '<style type="text/css">\n{% ignore1 %}\na {\n{% ignore2 %}\n}\n{% ignore3 %}\n</style>';
const output = '<style type="text/css">\n{% ignore1 %}\na{\n{% ignore2 %}\n}\n{% ignore3 %}\n</style>';
expect(await minify(input, {
ignoreCustomFragments: [/\{%[\s\S]*?%\}/],
minifyCSS: {
level: 0
}
})).toBe(output);
});

0 comments on commit 12e2739

Please sign in to comment.