diff --git a/lib/ejs.js b/lib/ejs.js index d65b8aae..0044d8f1 100755 --- a/lib/ejs.js +++ b/lib/ejs.js @@ -669,9 +669,9 @@ Template.prototype = { if (opts.rmWhitespace) { // Have to use two separate replace here as `^` and `$` operators don't - // work well with `\r`. + // work well with `\r` and empty lines don't work well with the `m` flag. this.templateText = - this.templateText.replace(/\r/g, '').replace(/^\s+|\s+$/gm, ''); + this.templateText.replace(/[\r\n]+/g, '\n').replace(/^\s+|\s+$/gm, ''); } // Slurp spaces and tabs before <%_ and after _%> @@ -775,11 +775,6 @@ Template.prototype = { line = line.replace(/^(?:\r\n|\r|\n)/, ''); this.truncate = false; } - else if (this.opts.rmWhitespace) { - // rmWhitespace has already removed trailing spaces, just need - // to remove linebreaks - line = line.replace(/^\n/, ''); - } if (!line) { return line; } diff --git a/test/fixtures/rmWhitespace.ejs b/test/fixtures/rmWhitespace.ejs index 5da0d896..ff2f78f8 100644 --- a/test/fixtures/rmWhitespace.ejs +++ b/test/fixtures/rmWhitespace.ejs @@ -8,7 +8,16 @@ adsffadsfadsfad<%= f %> piece of text. <% var a = 'a' %> Text again. +<% var aa = a + 'a' %> +Raw output: +<%- aa %> +Blank + +Line <% var b = 'b' %> <% var c = 'c' var d = 'd' %> -Another text. <%= c %> +Another text. <%= c %> +After escaped +<% /* newline slurp */ -%> +Last line diff --git a/test/fixtures/rmWhitespace.html b/test/fixtures/rmWhitespace.html index 4beb5fa9..85bc77ae 100644 --- a/test/fixtures/rmWhitespace.html +++ b/test/fixtures/rmWhitespace.html @@ -2,7 +2,19 @@ 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. +tex +t very long piece oftext very long +adsffadsfadsfadf +piece of text. + Text again. -Another text. c \ No newline at end of file + +Raw output: +aa +Blank +Line + + +Another text. c +After escaped +Last line \ No newline at end of file