From c745b46d7dba06cbf00b78219764b8d301038f57 Mon Sep 17 00:00:00 2001 From: Timothy Gu Date: Fri, 6 Feb 2015 21:08:41 -0500 Subject: [PATCH] Add rmWhitespace mode It is not a full-fledged HTML minifier, nor will it be. It is designed to be as "safe" as possible. Fixes #51. --- README.md | 4 ++++ docs/jsdoc/options.jsdoc | 6 ++++++ lib/ejs.js | 13 +++++++++++++ test/ejs.js | 7 +++++++ test/fixtures/rmWhitespace.ejs | 14 ++++++++++++++ test/fixtures/rmWhitespace.html | 8 ++++++++ 6 files changed, 52 insertions(+) create mode 100644 test/fixtures/rmWhitespace.ejs create mode 100644 test/fixtures/rmWhitespace.html diff --git a/README.md b/README.md index 7f0fd62a..48f98f0d 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,10 @@ for all the passed options. - `delimiter` Character to use with angle brackets for open/close - `debug` Output generated function body - `_with` Whether or not to use `with() {}` constructs. If `false` then the locals will be stored in the `locals` object. + - `rmWhitespace` Remove all safe-to-remove whitespace, including leading + and trailing whitespace. It also enables a safer version of `-%>` line + slurping for all scriptlet tags (it does not strip new lines of tags in + the middle of a line). ## Tags diff --git a/docs/jsdoc/options.jsdoc b/docs/jsdoc/options.jsdoc index 4fff8b95..a8fbd542 100644 --- a/docs/jsdoc/options.jsdoc +++ b/docs/jsdoc/options.jsdoc @@ -17,6 +17,12 @@ * whose name is specified by {@link module:ejs.localsName} (default to * `locals`). * + * @property {Boolean} [rmWhitespace=false] + * Remove all safe-to-remove whitespace, including leading and trailing + * whitespace. It also enables a safer version of `-%>` line slurping for all + * scriptlet tags (it does not strip new lines of tags in the middle of a + * line). + * * @property {Boolean} [client=false] * Whether or not to compile functions that are to be included in the browser. * diff --git a/lib/ejs.js b/lib/ejs.js index 6f3c1f66..d9867fc5 100644 --- a/lib/ejs.js +++ b/lib/ejs.js @@ -386,6 +386,7 @@ 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; this.opts = options; this.regex = this.createRegex(); @@ -413,6 +414,12 @@ Template.prototype = { , opts = this.opts , escape = opts.escapeFunction; + if (opts.rmWhitespace) { + // Have to use two separate replace here as `^` and `$` operators don't + // work well with `\r`. + this.templateText = + this.templateText.replace(/\r/g, '').replace(/^\s+|\s+$/gm, ''); + } if (!this.source) { this.generateSource(); var prepended = ' var __output = [];' + '\n'; @@ -567,6 +574,12 @@ Template.prototype = { line = line.replace('\n', ''); self.truncate = false; } + else if (self.opts.rmWhitespace) { + // Gotta me more careful here. + // .replace(/^(\s*)\n/, '$1') might be more appropriate here but as + // rmWhitespace already removes trailing spaces anyway so meh. + line = line.replace(/^\n/, ''); + } if (!line) { return; } diff --git a/test/ejs.js b/test/ejs.js index 39b6cba0..1bba889c 100644 --- a/test/ejs.js +++ b/test/ejs.js @@ -602,6 +602,13 @@ suite('exceptions', function () { }); }); +suite('rmWhitespace', function () { + test('works', function () { + assert.equal(ejs.render(fixture('rmWhitespace.ejs'), {}, {rmWhitespace: true}), + fixture('rmWhitespace.html')); + }); +}); + suite('include()', function () { test('include ejs', function () { var file = 'test/fixtures/include-simple.ejs'; diff --git a/test/fixtures/rmWhitespace.ejs b/test/fixtures/rmWhitespace.ejs new file mode 100644 index 00000000..5da0d896 --- /dev/null +++ b/test/fixtures/rmWhitespace.ejs @@ -0,0 +1,14 @@ + + +A very long piece of text very long piece of text very long piece of +text very long piece <% var f = 'f' %>of text very long piece of +tex t very long piece of<% %>text very long +adsffadsfadsfad<%= f %> + +piece of text. +<% var a = 'a' %> +Text again. +<% var b = 'b' %> +<% var c = 'c' +var d = 'd' %> +Another text. <%= c %> diff --git a/test/fixtures/rmWhitespace.html b/test/fixtures/rmWhitespace.html new file mode 100644 index 00000000..4beb5fa9 --- /dev/null +++ b/test/fixtures/rmWhitespace.html @@ -0,0 +1,8 @@ + + +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 oftext very long +adsffadsfadsfadfpiece of text. +Text again. +Another text. c \ No newline at end of file