Sfoglia il codice sorgente

JSLint hurt my feelings.

tags/0.3.0
Jan Lehnardt 16 anni fa
parent
commit
e3911963f3
1 ha cambiato i file con 31 aggiunte e 27 eliminazioni
  1. +31
    -27
      mustache.js

+ 31
- 27
mustache.js Vedi File

@@ -62,12 +62,13 @@ var Mustache = function() {
} }


var that = this; var that = this;
var regex = new RegExp(this.otag + "%([\\w_-]+) ?([\\w]+=[\\w]+)?"
+ this.ctag);
var regex = new RegExp(this.otag + "%([\\w_-]+) ?([\\w]+=[\\w]+)?" +
this.ctag);
return template.replace(regex, function(match, pragma, options) { return template.replace(regex, function(match, pragma, options) {
if(!that.pragmas_implemented[pragma]) { if(!that.pragmas_implemented[pragma]) {
throw({message: "This implementation of mustache doesn't understand the '"
+ pragma + "' pragma"});
throw({message:
"This implementation of mustache doesn't understand the '" +
pragma + "' pragma"});
} }
that.pragmas[pragma] = {}; that.pragmas[pragma] = {};
if(options) { if(options) {
@@ -111,7 +112,7 @@ var Mustache = function() {
return template.replace(regex, function(match, type, name, content) { return template.replace(regex, function(match, type, name, content) {
var value = that.find(name, context); var value = that.find(name, context);
if(type == "^") { // inverted section if(type == "^") { // inverted section
if(!value || that.is_array(value) && value.length == 0) {
if(!value || that.is_array(value) && value.length === 0) {
// false or empty list, render it // false or empty list, render it
return that.render(content, context, partials, true); return that.render(content, context, partials, true);
} else { } else {
@@ -148,29 +149,31 @@ var Mustache = function() {
var that = this; var that = this;


var new_regex = function() { var new_regex = function() {
return new RegExp(that.otag + "(=|!|>|\\{|%)?([^\/#\^]+?)\\1?" +
return new RegExp(that.otag + "(=|!|>|\\{|%)?([^\\/#\\^]+?)\\1?" +
that.ctag + "+", "g"); that.ctag + "+", "g");
}; };


var regex = new_regex(); var regex = new_regex();
var tag_replace_callback = function(match, operator, name) {
switch(operator) {
case "!": // ignore comments
return "";
case "=": // set new delimiters, rebuild the replace regexp
that.set_delimiters(name);
regex = new_regex();
return "";
case ">": // render partial
return that.render_partial(name, context, partials);
case "{": // the triple mustache is unescaped
return that.find(name, context);
default: // escape the value
return that.escape(that.find(name, context));
}
};
var lines = template.split("\n"); var lines = template.split("\n");

for (var i=0; i < lines.length; i++) { for (var i=0; i < lines.length; i++) {
lines[i] = lines[i].replace(regex, function(match, operator, name) {
switch(operator) {
case "!": // ignore comments
return "";
case "=": // set new delimiters, rebuild the replace regexp
that.set_delimiters(name);
regex = new_regex();
return "";
case ">": // render partial
return that.render_partial(name, context, partials);
case "{": // the triple mustache is unescaped
return that.find(name, context);
default: // escape the value
return that.escape(that.find(name, context));
}
}, this);
lines[i] = lines[i].replace(regex, tag_replace_callback, this);
if(!in_recursion) { if(!in_recursion) {
this.send(lines[i]); this.send(lines[i]);
} }
@@ -213,10 +216,11 @@ var Mustache = function() {
return bool === false || bool === 0 || bool; return bool === false || bool === 0 || bool;
} }


var value;
if(is_kinda_truthy(context[name])) { if(is_kinda_truthy(context[name])) {
var value = context[name];
value = context[name];
} else if(is_kinda_truthy(this.context[name])) { } else if(is_kinda_truthy(this.context[name])) {
var value = this.context[name];
value = this.context[name];
} }


if(typeof value === "function") { if(typeof value === "function") {
@@ -240,12 +244,12 @@ var Mustache = function() {
Does away with nasty characters Does away with nasty characters
*/ */
escape: function(s) { escape: function(s) {
s = String(s == null ? "" : s);
s = String(s === null ? "" : s);
return s.replace(/&(?!\w+;)|["<>\\]/g, function(s) { return s.replace(/&(?!\w+;)|["<>\\]/g, function(s) {
switch(s) { switch(s) {
case "&": return "&amp;"; case "&": return "&amp;";
case "\\": return "\\\\";;
case '"': return '\"';;
case "\\": return "\\\\";
case '"': return '\"';
case "<": return "&lt;"; case "<": return "&lt;";
case ">": return "&gt;"; case ">": return "&gt;";
default: return s; default: return s;


Loading…
Annulla
Salva