Skip to content

Commit

Permalink
faster escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
satchmorun committed Feb 9, 2012
1 parent 20e8fdc commit 6ecf295
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions mote.js
Expand Up @@ -441,22 +441,21 @@ Compiler.prototype.compile_exists = function(token) {
var Writer = {};

Writer.noop = function() { return ''; };
Writer.stringify = function(obj) { return obj ? ('' + obj) : ''; }
Writer.isArray = isArray;
Writer.Amp = /&/g;
Writer.Lt = /</g;
Writer.Gt = />/g;
Writer.Quot = /"/g;
Writer.escapeRE = /[&"<>]/g;
Writer.escaper = function(ch) {
switch (ch) {
case '&': return '&amp;'
case '<': return '&lt;'
case '>': return '&gt;'
case '"': return '&quot;'
default : return ch;
}
};

Writer.escapeHTML = function(str) {
return this.escapeRE.test(str)
? str
.replace(this.Amp,'&amp;')
.replace(this.Lt,'&lt;')
.replace(this.Gt,'&gt;')
.replace(this.Quot, '&quot;')
: str;
if (!this.escapeRE.test(str)) return str;
return str.replace(this.escapeRE, this.escaper);
};

Writer.cache = {};
Expand Down Expand Up @@ -539,7 +538,7 @@ Writer.exists = function(value, context, fn, indent) {
*/

function Context(obj, tail, root) {
this.head = obj;
this.obj = obj;
this.tail = tail;
this.root = root || obj;
}
Expand All @@ -560,8 +559,8 @@ Context.prototype.lookup = function(key) {

while (node) {
value = this.isArray(key)
? this.getPath(node.head, key)
: (key === '.') ? node.head : node.head[key]
? this.getPath(node.obj, key)
: (key === '.') ? node.obj : node.obj[key]
if (value) return value;
node = node.tail;
}
Expand Down

0 comments on commit 6ecf295

Please sign in to comment.