Fix problems with rmWhitespace

Make `rmWhitespace` safer by handling empty lines better and *not* removing newlines around EJS tags.
This commit is contained in:
Nathan Woltman
2018-11-16 17:04:25 -05:00
parent 720ab7f4d6
commit 4b1cbee458
3 changed files with 27 additions and 11 deletions

View File

@@ -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;
}

View File

@@ -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' %>
var d = 'd' %>
Another text. <%= c %>
After escaped
<% /* newline slurp */ -%>

View File

@@ -2,7 +2,19 @@
<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 oftext very long
adsffadsfadsfadfpiece of text.
tex
t very long piece oftext very long
adsffadsfadsfadf
piece of text.
Text again.
Another text. c
Raw output:
aa
Blank
Line
Another text. c
After escaped
Last line