-
Notifications
You must be signed in to change notification settings - Fork 847
Commit
It is not a full-fledged HTML minifier, nor will it be. It is designed to be as "safe" as possible. Fixes #51.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ var fs = require('fs') | |
, _TRAILING_SEMCOL = /;\s*$/; | ||
|
||
exports.localsName = _DEFAULT_LOCALS_NAME; | ||
exports.rmWhitespace = false; | ||
|
||
function resolveInclude(name, filename) { | ||
var path = require('path') | ||
|
@@ -223,6 +224,9 @@ function Template(text, opts) { | |
options.delimiter = opts.delimiter || exports.delimiter || _DEFAULT_DELIMITER; | ||
options._with = typeof opts._with != 'undefined' ? opts._with : true; | ||
options.cache = opts.cache || false; | ||
options.rmWhitespace = opts.rmWhitespace !== undefined | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
TimothyGu
Author
Collaborator
|
||
? opts.rmWhitespace | ||
: exports.rmWhitespace; | ||
this.opts = options; | ||
|
||
this.regex = this.createRegex(); | ||
|
@@ -250,6 +254,9 @@ Template.prototype = new function () { | |
, opts = this.opts | ||
, escape = opts.escapeFunction; | ||
|
||
if (opts.rmWhitespace) { | ||
this.templateText = this.templateText.replace(/^\s+|\s+$|\r/gm, ''); | ||
} | ||
if (!this.source) { | ||
this.generateSource(); | ||
var prepended = 'var __output = "";'; | ||
|
@@ -392,6 +399,9 @@ Template.prototype = new function () { | |
function _addOutput() { | ||
if (self.truncate) { | ||
line = line.replace('\n', ''); | ||
if (!line) { | ||
return; | ||
} | ||
} | ||
|
||
// Preserve literal slashes | ||
|
@@ -433,7 +443,7 @@ Template.prototype = new function () { | |
} | ||
|
||
this.mode = null; | ||
this.truncate = line.indexOf('-') === 0; | ||
this.truncate = line.indexOf('-') === 0 || self.opts.rmWhitespace; | ||
break; | ||
default: | ||
// In script mode, depends on type of tag | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
This document does not use semicolons in scriptlets. | ||
The value of c is: c |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<tag1> | ||
<tag2> | ||
A very long piece of text very long piece of text very long piece of text very | ||
long piece of text very long piece of text very long piece of text very long | ||
piece of text. | ||
<% var a = 'a' %> | ||
Text again. | ||
<% var b = 'b' %> | ||
<% var c = 'c' | ||
var d = 'd' %> | ||
Another text. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<tag1> | ||
<tag2> | ||
A very long piece of text very long piece of text very long piece of text very | ||
long piece of text very long piece of text very long piece of text very long | ||
piece of text. | ||
Text again. | ||
Another text. |
This is generally okay, but I prefer the
typeof
check (e.g. with_with
, above) to see if an option or var has been set. Given all these settings, a global config might have been nicer than hanging all this stuff right on the export. This bunch of massaging of values might be better as its own function too, rather than just inlined here.