Просмотр исходного кода

Coerce { token types to &

tags/0.7.0
Michael Jackson 13 лет назад
Родитель
Сommit
18e797d47e
2 измененных файлов: 21 добавлений и 27 удалений
  1. +20
    -26
      mustache.js
  2. +1
    -1
      test/parse_test.js

+ 20
- 26
mustache.js Просмотреть файл

@@ -291,28 +291,21 @@ var Mustache;


Renderer.prototype._partial = function (name, context) { Renderer.prototype._partial = function (name, context) {
var fn = this._partialCache[name]; var fn = this._partialCache[name];

if (fn) {
return fn(context);
}

return "";
return fn ? fn(context) : "";
}; };


Renderer.prototype._name = function (name, context, escape) {
Renderer.prototype._name = function (name, context) {
var value = context.lookup(name); var value = context.lookup(name);


if (typeof value === "function") { if (typeof value === "function") {
value = value.call(context.view); value = value.call(context.view);
} }


var string = (value == null) ? "" : String(value);

if (escape) {
return exports.escape(string);
}
return (value == null) ? "" : String(value);
};


return string;
Renderer.prototype._escaped = function (name, context) {
return exports.escape(this._name(name, context));
}; };


/** /**
@@ -340,36 +333,36 @@ var Mustache;
* `returnBody` is true. * `returnBody` is true.
*/ */
function compileTokens(tokens, returnBody) { function compileTokens(tokens, returnBody) {
var body = ['""'];
var token, escape, bounds, text;
var body = ['""'], token, quotedValue, bounds, text;


for (var i = 0, len = tokens.length; i < len; ++i) { for (var i = 0, len = tokens.length; i < len; ++i) {
token = tokens[i]; token = tokens[i];
quotedValue = quote(token[1]);


switch (token[0]) { switch (token[0]) {
case "#": case "#":
bounds = sectionBounds(token); bounds = sectionBounds(token);
text = "t.slice(" + bounds[0] + ", " + bounds[1] + ")"; text = "t.slice(" + bounds[0] + ", " + bounds[1] + ")";
body.push("r._section(" + quote(token[1]) + ", c, " + text + ", function (c, r) {\n" +
body.push("r._section(" + quotedValue + ", c, " + text + ", function (c, r) {\n" +
" " + compileTokens(token[4], true) + "\n" + " " + compileTokens(token[4], true) + "\n" +
"})"); "})");
break; break;
case "^": case "^":
body.push("r._inverted(" + quote(token[1]) + ", c, function (c, r) {\n" +
body.push("r._inverted(" + quotedValue + ", c, function (c, r) {\n" +
" " + compileTokens(token[4], true) + "\n" + " " + compileTokens(token[4], true) + "\n" +
"})"); "})");
break; break;
case "{":
case ">":
body.push("r._partial(" + quotedValue + ", c)");
break;
case "&": case "&":
case "name":
escape = String(token[0] === "name");
body.push("r._name(" + quote(token[1]) + ", c, " + escape + ")");
body.push("r._name(" + quotedValue + ", c)");
break; break;
case ">":
body.push("r._partial(" + quote(token[1]) + ", c)");
case "name":
body.push("r._escaped(" + quotedValue + ", c)");
break; break;
case "text": case "text":
body.push(quote(token[1]));
body.push(quotedValue);
break; break;
} }
} }
@@ -401,8 +394,8 @@ var Mustache;


/** /**
* Forms the given linear array of `tokens` into a nested tree structure * Forms the given linear array of `tokens` into a nested tree structure
* where tokens that represent a section have a "tokens" array property
* that contains all tokens that are in that section.
* where tokens that represent a section have a fifth item: an array that
* contains all tokens in that section.
*/ */
function nestTokens(tokens) { function nestTokens(tokens) {
var tree = []; var tree = [];
@@ -551,6 +544,7 @@ var Mustache;
value = scanner.scanUntil(closeRe); value = scanner.scanUntil(closeRe);
scanner.scan(curlyRe); scanner.scan(curlyRe);
scanner.scanUntil(tagRes[1]); scanner.scanUntil(tagRes[1]);
type = "&";
} else { } else {
value = scanner.scanUntil(tagRes[1]); value = scanner.scanUntil(tagRes[1]);
} }


+ 1
- 1
test/parse_test.js Просмотреть файл

@@ -11,7 +11,7 @@ var expectations = {
'{{ hi}}' : [ [ 'name', 'hi', 0, 7 ] ], '{{ hi}}' : [ [ 'name', 'hi', 0, 7 ] ],
'{{hi }}' : [ [ 'name', 'hi', 0, 7 ] ], '{{hi }}' : [ [ 'name', 'hi', 0, 7 ] ],
'{{ hi }}' : [ [ 'name', 'hi', 0, 8 ] ], '{{ hi }}' : [ [ 'name', 'hi', 0, 8 ] ],
'{{{hi}}}' : [ [ '{', 'hi', 0, 8 ] ],
'{{{hi}}}' : [ [ '&', 'hi', 0, 8 ] ],
'{{!hi}}' : [ [ '!', 'hi', 0, 7 ] ], '{{!hi}}' : [ [ '!', 'hi', 0, 7 ] ],
'{{! hi}}' : [ [ '!', 'hi', 0, 8 ] ], '{{! hi}}' : [ [ '!', 'hi', 0, 8 ] ],
'{{! hi }}' : [ [ '!', 'hi', 0, 9 ] ], '{{! hi }}' : [ [ '!', 'hi', 0, 9 ] ],


Загрузка…
Отмена
Сохранить