Skip to content

Commit

Permalink
Merge pull request #567 from alexlamsl/conditional-parsing
Browse files Browse the repository at this point in the history
processConditionalComments
  • Loading branch information
alexlamsl committed Mar 21, 2016
2 parents bbffbc1 + f981e97 commit 69034a7
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ How does HTMLMinifier compare to other solutions — [HTML Minifier from Will Pe
| `ignoreCustomComments` | Array of regex'es that allow to ignore certain comments, when matched | `[ ]` |
| `ignoreCustomFragments` | Array of regex'es that allow to ignore certain fragments, when matched (e.g. `<?php ... ?>`, `{{ ... }}`, etc.) | `[ /<%[\s\S]*?%>/, /<\?[\s\S]*?\?>/ ]` |
| `processScripts` | Array of strings corresponding to types of script elements to process through minifier (e.g. `text/ng-template`, `text/x-handlebars-template`, etc.) | `[ ]` |
| `processConditionalComments` | Process contents of conditional comments through minifier | `false` |
| `maxLineLength` | Specify a maximum line length. Compressed output will be split by newlines at valid HTML split-points. |
| `customEventAttributes` | Arrays of regex'es that allow to support custom event attributes for `minifyJS` (e.g. `ng-click`) | `[ /^on[a-z]{3,}$/ ]` |
| `customAttrAssign` | Arrays of regex'es that allow to support custom attribute assign expressions (e.g. `'<div flex?="{{mode != cover}}"></div>'`) | `[ ]` |
Expand Down
4 changes: 2 additions & 2 deletions dist/htmlminifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -883,9 +883,9 @@
}

function cleanConditionalComment(comment, options) {
return comment.replace(/^(\[if\s[^\]]+\]>)([\s\S]*?)(<!\[endif\])$/, function(match, prefix, text, suffix) {
return options.processConditionalComments ? comment.replace(/^(\[if\s[^\]]+\]>)([\s\S]*?)(<!\[endif\])$/, function(match, prefix, text, suffix) {
return prefix + minify(text, options, true) + suffix;
});
}) : comment;
}

function removeCDATASections(text) {
Expand Down
2 changes: 1 addition & 1 deletion dist/htmlminifier.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions sample-cli-config-file.conf
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
"minifyCSS": true,
"includeAutoGeneratedTags": false,
"ignoreCustomComments": [],
"processConditionalComments": true,
"processScripts": []
}
4 changes: 2 additions & 2 deletions src/htmlminifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,9 @@
}

function cleanConditionalComment(comment, options) {
return comment.replace(/^(\[if\s[^\]]+\]>)([\s\S]*?)(<!\[endif\])$/, function(match, prefix, text, suffix) {
return options.processConditionalComments ? comment.replace(/^(\[if\s[^\]]+\]>)([\s\S]*?)(<!\[endif\])$/, function(match, prefix, text, suffix) {
return prefix + minify(text, options, true) + suffix;
});
}) : comment;
}

function removeCDATASections(text) {
Expand Down
37 changes: 33 additions & 4 deletions tests/minifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,27 @@
' <body>\n' +
' </body>\n' +
'</html>';
output = '<head><!--[if lte IE 8]><script>alert("ie8!")</script><![endif]-->';
output = '<head><!--[if lte IE 8]>\n' +
' <script type="text/javascript">\n' +
' alert("ie8!");\n' +
' </script>\n' +
' <![endif]-->';
equal(minify(input, {
minifyJS: true,
removeComments: true,
collapseWhitespace: true,
removeOptionalTags: true,
removeScriptTypeAttributes: true
}), output);
output = '<head><!--[if lte IE 8]><script>alert("ie8!")</script><![endif]-->';
equal(minify(input, {
minifyJS: true,
removeComments: true,
collapseWhitespace: true,
removeOptionalTags: true,
removeScriptTypeAttributes: true,
processConditionalComments: true
}), output);

input = '<!DOCTYPE html>\n' +
'<html lang="en">\n' +
Expand Down Expand Up @@ -338,29 +351,45 @@
removeComments: true,
collapseWhitespace: true
}), output);
equal(minify(input, {
removeComments: true,
collapseWhitespace: true,
processConditionalComments: true
}), output);
});

test('collapsing space in conditional comments', function() {
input = '<!--[if IE 7]>\n\n \t\n \t\t ' +
'<link rel="stylesheet" href="/css/ie7-fixes.css" type="text/css" />\n\t' +
'<![endif]-->';
equal(minify(input, { removeComments: true }), input);
equal(minify(input, { removeComments: true, collapseWhitespace: true }), input);
output = '<!--[if IE 7]>\n\n \t\n \t\t ' +
'<link rel="stylesheet" href="/css/ie7-fixes.css" type="text/css">\n\t' +
'<![endif]-->';
equal(minify(input, { removeComments: true }), output);
equal(minify(input, { removeComments: true, processConditionalComments: true }), output);
output = '<!--[if IE 7]>' +
'<link rel="stylesheet" href="/css/ie7-fixes.css" type="text/css">' +
'<![endif]-->';
equal(minify(input, { removeComments: true, collapseWhitespace: true }), output);
equal(minify(input, {
removeComments: true,
collapseWhitespace: true,
processConditionalComments: true
}), output);

input = '<!--[if lte IE 6]>\n \n \n\n\n\t' +
'<p title=" sigificant whitespace ">blah blah</p>' +
'<![endif]-->';
equal(minify(input, { removeComments: true }), input);
equal(minify(input, { removeComments: true, collapseWhitespace: true }), input);
output = '<!--[if lte IE 6]>' +
'<p title=" sigificant whitespace ">blah blah</p>' +
'<![endif]-->';
equal(minify(input, { removeComments: true, collapseWhitespace: true }), output);
equal(minify(input, {
removeComments: true,
collapseWhitespace: true,
processConditionalComments: true
}), output);
});

test('remove comments from scripts', function() {
Expand Down

0 comments on commit 69034a7

Please sign in to comment.