mirror of
https://github.com/CryptKeeperZK/ejs.git
synced 2026-01-10 07:58:26 -05:00
Fix problems with rmWhitespace
Make `rmWhitespace` safer by handling empty lines better and *not* removing newlines around EJS tags.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
11
test/fixtures/rmWhitespace.ejs
vendored
11
test/fixtures/rmWhitespace.ejs
vendored
@@ -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 */ -%>
|
||||
|
||||
18
test/fixtures/rmWhitespace.html
vendored
18
test/fixtures/rmWhitespace.html
vendored
@@ -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
|
||||
Reference in New Issue
Block a user