Browse Source

Refactor escapeHTML

tags/0.4.0
Michael Jackson 14 years ago
parent
commit
9a8ff7638a
1 changed files with 10 additions and 20 deletions
  1. +10
    -20
      mustache.js

+ 10
- 20
mustache.js View File

@@ -35,6 +35,15 @@ var Mustache = function () {
}
}

function escapeHTML(string) {
return String(string)
.replace(/&(?!\w+;)/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#39;");
}

var regexCache = {};
var Renderer = function () {};

@@ -228,8 +237,6 @@ var Mustache = function () {
// tit for tat
var that = this;



var new_regex = function () {
return that.getCachedRegex("render_tags", function (otag, ctag) {
return new RegExp(otag + "(=|!|>|\\{|%)?([^\\/#\\^]+?)\\1?" + ctag + "+", "g");
@@ -250,7 +257,7 @@ var Mustache = function () {
case "{": // the triple mustache is unescaped
return that.find(name, context);
default: // escape the value
return that.escape(that.find(name, context));
return escapeHTML(that.find(name, context));
}
};
var lines = template.split("\n");
@@ -347,23 +354,6 @@ var Mustache = function () {
return haystack.indexOf(this.otag + needle) != -1;
},

/*
Does away with nasty characters
*/
escape: function (s) {
s = String(s === null ? "" : s);
return s.replace(/&(?!\w+;)|["'<>\\]/g, function (s) {
switch(s) {
case "&": return "&amp;";
case '"': return '&quot;';
case "'": return '&#39;';
case "<": return "&lt;";
case ">": return "&gt;";
default: return s;
}
});
},

// by @langalex, support for arrays of strings
create_context: function (_context) {
if (this.is_object(_context)) {


Loading…
Cancel
Save