Skip to content

Commit

Permalink
Merge pull request #407 from nwoltman/fix-rmwhitespace
Browse files Browse the repository at this point in the history
Fix problems with rmWhitespace
  • Loading branch information
mde committed Nov 19, 2018
2 parents 720ab7f + 4b1cbee commit 59ac6ef
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
9 changes: 2 additions & 7 deletions lib/ejs.js
Expand Up @@ -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 _%>
Expand Down Expand Up @@ -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;
}
Expand Down
11 changes: 10 additions & 1 deletion test/fixtures/rmWhitespace.ejs
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions test/fixtures/rmWhitespace.html
Expand Up @@ -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

0 comments on commit 59ac6ef

Please sign in to comment.