Kaynağa Gözat

Fix inverted section rendering for missing keys

tags/0.5.1
Michael Jackson 14 yıl önce
ebeveyn
işleme
1846281683
4 değiştirilmiş dosya ile 26 ekleme ve 23 silme
  1. +20
    -19
      mustache.js
  2. +2
    -2
      spec/_files/inverted_section.js
  3. +3
    -2
      spec/_files/inverted_section.mustache
  4. +1
    -0
      spec/_files/inverted_section.txt

+ 20
- 19
mustache.js Dosyayı Görüntüle

@@ -115,7 +115,11 @@ var Mustache = (typeof module !== "undefined" && module.exports) || {};
/** /**
* Looks up the value of the given `name` in the given context `stack`. * Looks up the value of the given `name` in the given context `stack`.
*/ */
function findName(name, stack) {
function findName(name, stack, returnNull) {
if (name === ".") {
return stack[stack.length - 1];
}

var names = name.split("."); var names = name.split(".");
var lastIndex = names.length - 1; var lastIndex = names.length - 1;
var target = names[lastIndex]; var target = names[lastIndex];
@@ -147,10 +151,16 @@ var Mustache = (typeof module !== "undefined" && module.exports) || {};
value = value.call(localStack[localStack.length - 1]); value = value.call(localStack[localStack.length - 1]);
} }


return value == null ? "" : value;
if (value == null && !returnNull) {
return "";
}

return value;
} }


function sendSection(send, value, callback, stack, inverted) {
function sendSection(send, name, callback, stack, inverted) {
var value = findName(name, stack, true);

if (inverted) { if (inverted) {
// From the spec: inverted sections may render text once based on the // From the spec: inverted sections may render text once based on the
// inverse value of the key. That is, they will be rendered if the key // inverse value of the key. That is, they will be rendered if the key
@@ -223,11 +233,6 @@ var Mustache = (typeof module !== "undefined" && module.exports) || {};
nonSpace = false; nonSpace = false;
}; };


// Returns a bit of code that can be used to find the given `name`.
var findFor = function (name) {
return name === "." ? "stack[stack.length - 1]" : 'find("' + name + '")';
};

var sectionStack = [], updateLine, nextOpenTag, nextCloseTag; var sectionStack = [], updateLine, nextOpenTag, nextCloseTag;


var setTags = function (source) { var setTags = function (source) {
@@ -260,7 +265,7 @@ var Mustache = (typeof module !== "undefined" && module.exports) || {};
code.push( code.push(
'");', '");',
updateLine, updateLine,
'\nvar value = ' + findFor(name) + ';',
'\nvar name = "' + name + '";',
'\nvar callback = (function () {', '\nvar callback = (function () {',
'\n var buffer, send = function (chunk) { buffer.push(chunk); };', '\n var buffer, send = function (chunk) { buffer.push(chunk); };',
'\n return function () {', '\n return function () {',
@@ -291,9 +296,9 @@ var Mustache = (typeof module !== "undefined" && module.exports) || {};
); );


if (section.inverted) { if (section.inverted) {
code.push("\nsendSection(send,value,callback,stack,true);");
code.push("\nsendSection(send,name,callback,stack,true);");
} else { } else {
code.push("\nsendSection(send,value,callback,stack);");
code.push("\nsendSection(send,name,callback,stack);");
} }


code.push('\nsend("'); code.push('\nsend("');
@@ -303,7 +308,7 @@ var Mustache = (typeof module !== "undefined" && module.exports) || {};
code.push( code.push(
'");', '");',
updateLine, updateLine,
'\nsend(' + findFor(trim(source)) + ');',
'\nsend(findName("' + trim(source) + '", stack));',
'\nsend("' '\nsend("'
); );
}; };
@@ -312,7 +317,7 @@ var Mustache = (typeof module !== "undefined" && module.exports) || {};
code.push( code.push(
'");', '");',
updateLine, updateLine,
'\nsend(escapeHTML(' + findFor(trim(source)) + '));',
'\nsend(escapeHTML(findName("' + trim(source) + '", stack)));',
'\nsend("' '\nsend("'
); );
}; };
@@ -446,7 +451,7 @@ var Mustache = (typeof module !== "undefined" && module.exports) || {};
* Used by `compile` to generate a reusable function for the given `template`. * Used by `compile` to generate a reusable function for the given `template`.
*/ */
function _compile(template, options) { function _compile(template, options) {
var args = "view,partials,send,stack,find,escapeHTML,sendSection,render";
var args = "view,partials,send,stack,findName,escapeHTML,sendSection,render";
var body = parse(template, options); var body = parse(template, options);
var fn = new Function(args, body); var fn = new Function(args, body);


@@ -469,12 +474,8 @@ var Mustache = (typeof module !== "undefined" && module.exports) || {};


var stack = [view]; // context stack var stack = [view]; // context stack


var find = function (name) {
return findName(name, stack);
};

try { try {
fn(view, partials, send, stack, find, escapeHTML, sendSection, render);
fn(view, partials, send, stack, findName, escapeHTML, sendSection, render);
} catch (e) { } catch (e) {
throw debug(e.error, template, e.line, options.file); throw debug(e.error, template, e.line, options.file);
} }


+ 2
- 2
spec/_files/inverted_section.js Dosyayı Görüntüle

@@ -1,3 +1,3 @@
var inverted_section = { var inverted_section = {
"repo": []
}
"repos": []
};

+ 3
- 2
spec/_files/inverted_section.mustache Dosyayı Görüntüle

@@ -1,2 +1,3 @@
{{#repo}}<b>{{name}}</b>{{/repo}}
{{^repo}}No repos :({{/repo}}
{{#repos}}<b>{{name}}</b>{{/repos}}
{{^repos}}No repos :({{/repos}}
{{^nothin}}Hello!{{/nothin}}

+ 1
- 0
spec/_files/inverted_section.txt Dosyayı Görüntüle

@@ -1,2 +1,3 @@


No repos :( No repos :(
Hello!

Yükleniyor…
İptal
Kaydet