浏览代码

Use JavaScript's definition of falsy

Fixes #186
tags/0.6.0
Michael Jackson 13 年前
父节点
当前提交
5ab345eddf
共有 4 个文件被更改,包括 35 次插入4 次删除
  1. +3
    -4
      mustache.js
  2. +8
    -0
      test/_files/falsy.js
  3. +12
    -0
      test/_files/falsy.mustache
  4. +12
    -0
      test/_files/falsy.txt

+ 3
- 4
mustache.js 查看文件

@@ -286,10 +286,9 @@ var Mustache;
Renderer.prototype._inverted = function (name, context, callback) {
var value = context.lookup(name);

// 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
// doesn't exist, is false, or is an empty list.
if (value == null || value === false || (isArray(value) && value.length === 0)) {
// Use JavaScript's definition of falsy. Include empty arrays.
// See https://github.com/janl/mustache.js/issues/186
if (!value || (isArray(value) && value.length === 0)) {
return callback(context, this);
}



+ 8
- 0
test/_files/falsy.js 查看文件

@@ -0,0 +1,8 @@
({
"emptyString": "",
"emptyArray": [],
"zero": 0,
"null": null,
"undefined": undefined,
"NaN": 0/0
})

+ 12
- 0
test/_files/falsy.mustache 查看文件

@@ -0,0 +1,12 @@
{{#emptyString}}empty string{{/emptyString}}
{{^emptyString}}inverted empty string{{/emptyString}}
{{#emptyArray}}empty array{{/emptyArray}}
{{^emptyArray}}inverted empty array{{/emptyArray}}
{{#zero}}zero{{/zero}}
{{^zero}}inverted zero{{/zero}}
{{#null}}null{{/null}}
{{^null}}inverted null{{/null}}
{{#undefined}}undefined{{/undefined}}
{{^undefined}}inverted undefined{{/undefined}}
{{#NaN}}NaN{{/NaN}}
{{^NaN}}inverted NaN{{/NaN}}

+ 12
- 0
test/_files/falsy.txt 查看文件

@@ -0,0 +1,12 @@

inverted empty string

inverted empty array

inverted zero

inverted null

inverted undefined

inverted NaN

正在加载...
取消
保存