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 #180
  • Loading branch information
chrisstaite-menlo committed Aug 21, 2024
1 parent c4a7ae0 commit dfa0247
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
37 changes: 30 additions & 7 deletions src/htmlminifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -904,14 +904,37 @@ 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) {
const id = uidAttr + match[2] + uidAttr;
text = text.replace(id, ignoreCSS(id));
ids.push(id);
// Loop removing a single ID at a time from the warnings, a
// warning might contain multiple IDs in the context, but we only
// handle the first match on each attempt.
while (true) {
const minifyTest = new CleanCSS().minify(wrapCSS(text, type));
if (minifyTest.warnings.length === 0) {
// There are no warnings.
break;
}
});
minifyTest.warnings.forEach(function (warning) {
// It is very important to reset the RegExp before searching
// as it's re-used each time.
uidPattern.lastIndex = 0;
const match = uidPattern.exec(warning);
if (match) {
const id = uidAttr + match[2] + uidAttr;
// Only substitute each ID once, if this has come up
// multiple times, then we need to abort.
if (ids.indexOf(id) !== -1) {
uidPattern.lastIndex = 0;
} else {
text = text.replace(id, ignoreCSS(id));
ids.push(id);
}
}
});
if (uidPattern.lastIndex === 0) {
// There was a warning that didn't match the pattern.
break;
}
}

return fn(text, type).then(chunk => {
ids.forEach(function (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 dfa0247

Please sign in to comment.