Skip to content

Commit

Permalink
[fix] allow keeping conditionals while removing comments, fixes #34
Browse files Browse the repository at this point in the history
  • Loading branch information
Swaagie committed Mar 7, 2015
1 parent 574aa5d commit 75e7f28
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -96,8 +96,9 @@ minimize.parse(

Comments inside HTML are usually beneficial while developing. Hiding your
comments in production is sane, safe and will reduce data transfer. If you
ensist on keeping them, for instance to show a nice easter egg, set the option
to true.
ensist on keeping them, fo1r instance to show a nice easter egg, set the option
to true. Keeping comments will also retain any Server Side Includes or
conditional IE statements.

```javascript
var Minimize = require('minimize')
Expand Down
12 changes: 10 additions & 2 deletions lib/helpers.js
Expand Up @@ -249,8 +249,16 @@ Helpers.prototype.text = function text(element) {
Helpers.prototype.comment = function comment(element) {
var cfg = this.config;

if (!(cfg.comments || cfg.conditionals || cfg.ssi)) return '';
return '<!--' + compact(element.data, true).trim() + '-->';
function io() {
return '<!--' + compact(element.data, true).trim() + '-->';
}

if (cfg.ssi && ~element.data.indexOf('#include')) return io();
if (cfg.conditionals && (~element.data.indexOf('[if')
|| ~element.data.indexOf('<![endif'))) return io();
if (cfg.comments) return io();

return '';
};

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "minimize",
"version": "1.3.0",
"version": "1.3.1",
"description": "Minimize HTML",
"main": "./lib/minimize",
"bin": {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/html.json
Expand Up @@ -23,6 +23,7 @@
"inlineelements": "<form><label>Last:</label> <input>, <label>First:</label> <input> <button>Submit</button></form>",
"multilineattribs": "<h1 class=\"\n big\n title\n\">minimize</h1>",
"complexconditional": "<head><!--[if lt IE 9]> <html class=\"no-js lt-ie9\"> <![endif]--><!--[if gt IE 8]><!--> <link rel=\"some.file.com\"> <!--<![endif]--></head>",
"conditionalcomments": "<head><!--[if IE 6]>Special instructions for IE 6 here<![endif]--></head><h1><!-- Some comments thats either removed or kept --></h1>",
"doctype": {
"data": "!doctype html",
"type": "directive",
Expand Down
8 changes: 8 additions & 0 deletions test/minimize-test.js
Expand Up @@ -85,6 +85,14 @@ describe('Minimize', function () {
});
});

it('should be able to remove comments but not conditionals', function (done) {
var eitheror = new Minimize({ comments: false, conditionals: true });
eitheror.parse(html.conditionalcomments, function (error, result) {
expect(result).to.equal('<head><!--[if IE 6]>Special instructions for IE 6 here<![endif]--></head><h1></h1>');
done();
});
});

it('should be configurable to retain server side includes', function (done) {
var commentable = new Minimize({ ssi: true });
commentable.parse(html.ssi, function (error, result) {
Expand Down

0 comments on commit 75e7f28

Please sign in to comment.