Skip to content

Commit

Permalink
Merge pull request tj#50 from mde/optimize
Browse files Browse the repository at this point in the history
Optimize
  • Loading branch information
TimothyGu committed Feb 1, 2015
2 parents 8f861cc + 20fdcbd commit 0075fb6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
26 changes: 17 additions & 9 deletions lib/ejs.js
Expand Up @@ -252,15 +252,15 @@ Template.prototype = new function () {

if (!this.source) {
this.generateSource();
var prepended = 'var __output = "";';
var prepended = 'var __output = [];';
if (opts._with !== false) {
prepended += ' with (' + exports.localsName + ' || {}) { ';
}
this.source = prepended + this.source;
if (opts._with !== false) {
this.source += '}';
}
this.source += ';return __output.trim();';
this.source += ';return __output.join("").trim();';
}

if (opts.compileDebug) {
Expand All @@ -280,7 +280,15 @@ Template.prototype = new function () {
}

if (opts.client) {
src = 'escape = escape || ' + escape.toString() + ';\n' + src;
if (escape !== utils.escapeXML) {
src = 'escape = escape || ' + escape.toString() + ';\n' + src;
}
else {
src = utils.escapeFuncStr
+ 'escape = escape || '
+ escape.toString() + ';\n'
+ src;
}
if (opts.compileDebug) {
src = 'rethrow = rethrow || ' + rethrow.toString() + ';\n' + src;
}
Expand Down Expand Up @@ -404,7 +412,7 @@ Template.prototype = new function () {
// Escape double-quotes
// - this will be the delimiter during execution
line = line.replace(/"/g, '\\"');
self.source += ';__output += "' + line + '";';
self.source += ';__output.push("' + line + '");';
}

newLineCount = (line.split('\n').length - 1);
Expand All @@ -424,7 +432,7 @@ Template.prototype = new function () {
break;
case '<' + d + d:
this.mode = Template.modes.LITERAL;
this.source += ';__output += "' + line.replace('<' + d + d, '<' + d) + '";';
this.source += ';__output.push("' + line.replace('<' + d + d, '<' + d) + '");';
break;
case d + '>':
case '-' + d + '>':
Expand Down Expand Up @@ -457,16 +465,16 @@ Template.prototype = new function () {
// Add the exec'd, escaped result to the output
// Have to prevent the string-coercion of `undefined` and `null`
// in the `escape` function -- making a `join` call like below unnecessary
this.source += ';__output += escape(' +
line.replace(_TRAILING_SEMCOL, '').trim() + ')';
this.source += ';__output.push(escape(' +
line.replace(_TRAILING_SEMCOL, '').trim() + '))';
break;
// Exec and output
case Template.modes.RAW:
// Add the exec'd result to the output
// Using `join` here prevents string-coercion of `undefined` and `null`
// without filtering out falsey values like zero
this.source += ';__output = [__output, ' +
line.replace(_TRAILING_SEMCOL, '').trim() + '].join("")';
this.source += ';__output.push(' +
line.replace(_TRAILING_SEMCOL, '').trim() + ')';
break;
case Template.modes.COMMENT:
// Do nothing
Expand Down
37 changes: 25 additions & 12 deletions lib/utils.js
Expand Up @@ -28,19 +28,32 @@ exports.escapeRegExpChars = function (string) {
return String(string).replace(regExpChars, '\\$&');
};

var encodeHTMLRules = {
'&': '&amp;'
, '<': '&lt;'
, '>': '&gt;'
, '"': '&#34;'
, "'": '&#39;'
}
, matchHTML = /[&<>\'"]/g;

exports.escapeFuncStr =
'var encodeHTMLRules = {'
+ '"&": "&amp;"'
+ ', "<": "&lt;"'
+ ', ">": "&gt;"'
+ ', \'"\': "&#34;"'
+ ', "\'": "&#39;"'
+ '}'
+ ', matchHTML = /[&<>\'"]/g;';

exports.escapeXML = function (markup) {
// Handle stupid JS `undefined` and `null`
// Yes, we want double-equal here to catch both
// jshint eqnull: true
if (markup == null) {
return '';
}
return String(markup)
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/'/g, '&#39;')
.replace(/"/g, '&quot;');
return markup == undefined
? ''
: String(markup)
.replace(matchHTML, function(m) {
return encodeHTMLRules[m] || m;
});
};

exports.shallowCopy = function (to, from) {
Expand Down

0 comments on commit 0075fb6

Please sign in to comment.