Skip to content

Commit

Permalink
handle custom fragments within CSS/JS correctly (#1015)
Browse files Browse the repository at this point in the history
fixes #1001
  • Loading branch information
alexlamsl authored Apr 1, 2019
1 parent 47b7042 commit 583e086
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/htmlminifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -878,19 +878,19 @@ function minify(value, options, partialMarkup) {
value = value.replace(reCustomIgnore, function(match) {
if (!uidAttr) {
uidAttr = uniqueId(value);
uidPattern = new RegExp('(\\s*)' + uidAttr + '([0-9]+)(\\s*)', 'g');
uidPattern = new RegExp('(\\s*)' + uidAttr + '([0-9]+)' + uidAttr + '(\\s*)', 'g');
if (options.minifyCSS) {
options.minifyCSS = (function(fn) {
return function(text, type) {
text = text.replace(uidPattern, function(match, prefix, index) {
var chunks = ignoredCustomMarkupChunks[+index];
return chunks[1] + uidAttr + index + chunks[2];
return chunks[1] + uidAttr + index + uidAttr + chunks[2];
});
var ids = [];
new CleanCSS().minify(wrapCSS(text, type)).warnings.forEach(function(warning) {
var match = uidPattern.exec(warning);
if (match) {
var id = uidAttr + match[2];
var id = uidAttr + match[2] + uidAttr;
text = text.replace(id, ignoreCSS(id));
ids.push(id);
}
Expand All @@ -908,13 +908,13 @@ function minify(value, options, partialMarkup) {
return function(text, type) {
return fn(text.replace(uidPattern, function(match, prefix, index) {
var chunks = ignoredCustomMarkupChunks[+index];
return chunks[1] + uidAttr + index + chunks[2];
return chunks[1] + uidAttr + index + uidAttr + chunks[2];
}), type);
};
})(options.minifyJS);
}
}
var token = uidAttr + ignoredCustomMarkupChunks.length;
var token = uidAttr + ignoredCustomMarkupChunks.length + uidAttr;
ignoredCustomMarkupChunks.push(/^(\s*)[\s\S]*?(\s*)$/.exec(match));
return '\t' + token + '\t';
});
Expand Down
11 changes: 11 additions & 0 deletions tests/minifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,14 @@ QUnit.test('Ignore custom fragments', function(assert) {
input = '<pre>\nfoo\n<? bar ?>\nbaz\n</pre>';
assert.equal(minify(input), input);
assert.equal(minify(input, { collapseWhitespace: true }), input);

input = '<script>var value="<?php ?>+<?php ?>0"</script>';
assert.equal(minify(input), input);
assert.equal(minify(input, { minifyJS: true }), input);

input = '<style>body{font-size:<%=1%>2pt}</style>';
assert.equal(minify(input), input);
assert.equal(minify(input, { minifyCSS: true }), input);
});

QUnit.test('bootstrap\'s span > button > span', function(assert) {
Expand Down Expand Up @@ -2853,6 +2861,9 @@ QUnit.test('ignore', function(assert) {
input = '<p>foo <!-- htmlmin:ignore --><span>\n\tbar\n</span><!-- htmlmin:ignore -->.</p>';
output = '<p>foo <span>\n\tbar\n</span>.</p>';
assert.equal(minify(input, { collapseWhitespace: true }), output);

input = '<!-- htmlmin:ignore -->+<!-- htmlmin:ignore -->0';
assert.equal(minify(input), '+0');
});

QUnit.test('meta viewport', function(assert) {
Expand Down

0 comments on commit 583e086

Please sign in to comment.