Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed :else in section when iterating over empty array. #127

Merged
merged 1 commit into from
Sep 10, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions lib/dust.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,14 +390,18 @@ Chunk.prototype.section = function(elem, context, bodies, params) {
if (dust.isArray(elem)) {
if (body) {
var len = elem.length, chunk = this;
context.stack.head['$len'] = len;
for (var i=0; i<len; i++) {
context.stack.head['$idx'] = i;
chunk = body(chunk, context.push(elem[i], i, len));
if (len > 0) {
context.stack.head['$len'] = len;
for (var i=0; i<len; i++) {
context.stack.head['$idx'] = i;
chunk = body(chunk, context.push(elem[i], i, len));
}
context.stack.head['$idx'] = undefined;
context.stack.head['$len'] = undefined;
return chunk;
} else if (skip) {
return skip(this, context);
}
context.stack.head['$idx'] = undefined;
context.stack.head['$len'] = undefined;
return chunk;
}
} else if (elem === true) {
if (body) return body(this, context);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not do it by using the dust.isEmpty instead of len>0 that the exists section uses

dust.isEmpty = function(value) {
if (dust.isArray(value) && !value.length) return true;
if (value === 0) return false;
return (!value);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--len is needed in the loop anyway. no need to call another function.--
nevermind. totally agree let's keep consistent and use isEmpty.. let's put it before the isArray check so that is checked first and for everything type.

Expand Down
7 changes: 7 additions & 0 deletions test/jasmine-test/spec/grammarTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ var grammarTests = [
expected: "foo, not bar!",
message:"should test the else block"
},
{
name: "empty_else_block",
source: "{#foo}full foo{:else}empty foo{/foo}",
context: { foo: []},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a few more tests for completeness

to make sure both ? and # behave the same way

  • 0
  • "0"
  • true
  • false
  • []
  • {}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should fix the problem more upstream where dust seems to be treating numbers as string literals...i.e. 0 doesn't evaluate to false, which leads me to believe its treating it as "0". {} should evaluate to true in keeping with standard javascript.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for completeness then we should also check null and empty string

expected: "empty foo",
message: "should test else block when array empty"
},
{
name: "conditional",
source: "{?tags}\n" +
Expand Down
1 change: 1 addition & 0 deletions test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ <h1>{dust}</h1>
<option value="25">child_template</option>
<option value="26">recursion</option>
<option value="27">comments</option>
<option value="28">empty_else_block</option>
</select></span> or write your own:
</p>
<textarea id="input-source" class="console"></textarea>
Expand Down