Skip to content

Commit

Permalink
Removed needless escaping, handle trailing comments in output modes
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Nov 30, 2016
1 parent 53ffe01 commit 7eaba6a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ var _REGEX_STRING = '(<%%|%%>|<%=|<%-|<%_|<%#|<%|%>|-%>|_%>)';
var _OPTS = [ 'cache', 'filename', 'delimiter', 'scope', 'context',
'debug', 'compileDebug', 'client', '_with', 'root', 'rmWhitespace',
'strict', 'localsName'];
var _TRAILING_SEMCOL = /;\s*$/;
var _BOM = /^\uFEFF/;

/**
Expand Down Expand Up @@ -281,6 +280,10 @@ function cpOptsInData(data, opts) {
});
}

function stripSemi(str) {
return str.replace(/;(\s*$)/, '$1');
}

/**
* Compile the given `str` of ejs into a template function.
*
Expand Down Expand Up @@ -647,9 +650,8 @@ Template.prototype = {
self.truncate = false;
}
else if (self.opts.rmWhitespace) {
// Gotta be more careful here.
// .replace(/^(\s*)\n/, '$1') might be more appropriate here but as
// rmWhitespace already removes trailing spaces anyway so meh.
// rmWhitespace has already removed trailing spaces, just need
// to remove linebreaks
line = line.replace(/^\n/, '');
}
if (!line) {
Expand Down Expand Up @@ -722,13 +724,11 @@ Template.prototype = {
break;
// Exec, esc, and output
case Template.modes.ESCAPED:
this.source += ' ; __append(escape(' +
line.replace(_TRAILING_SEMCOL, '').trim() + '))' + '\n';
this.source += ' ; __append(escape(' + stripSemi(line) + '))' + '\n';
break;
// Exec and output
case Template.modes.RAW:
this.source += ' ; __append(' +
line.replace(_TRAILING_SEMCOL, '').trim() + ')' + '\n';
this.source += ' ; __append(' + stripSemi(line) + ')' + '\n';
break;
case Template.modes.COMMENT:
// Do nothing
Expand Down
10 changes: 10 additions & 0 deletions test/ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,11 @@ suite('<%', function () {
});

suite('<%=', function () {
test('should not throw an error with a // comment on the final line', function () {
assert.equal(ejs.render('<%=\n// a comment\nname\n// another comment %>', {name: '&nbsp;<script>'}),
'&amp;nbsp;&lt;script&gt;');
});

test('escape &amp;<script>', function () {
assert.equal(ejs.render('<%= name %>', {name: '&nbsp;<script>'}),
'&amp;nbsp;&lt;script&gt;');
Expand Down Expand Up @@ -503,6 +508,11 @@ suite('<%=', function () {
});

suite('<%-', function () {
test('should not throw an error with a // comment on the final line', function () {
assert.equal(ejs.render('<%-\n// a comment\nname\n// another comment %>', {name: '&nbsp;<script>'}),
'&nbsp;<script>');
});

test('not escape', function () {
assert.equal(ejs.render('<%- name %>', {name: '<script>'}),
'<script>');
Expand Down

0 comments on commit 7eaba6a

Please sign in to comment.