Skip to content

Commit

Permalink
Merge pull request #135 from sethkinast/nested-truth
Browse files Browse the repository at this point in the history
Evaluate truth test bodies inside a @select before resolving the select.
  • Loading branch information
prashn64 committed Apr 28, 2015
2 parents fdf8392 + 411abc7 commit 5b363ab
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/dust-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function addSelectState(context, opts) {
}

var state = {
isPending: false,
isResolved: false,
isDeferredComplete: false,
deferreds: []
Expand Down Expand Up @@ -105,7 +106,7 @@ function filter(chunk, context, bodies, params, helperName, test) {
var body = bodies.block,
skip = bodies.else,
selectState = getSelectState(context) || {},
key, value, type;
willResolve, key, value, type;

// Once one truth test in a select passes, short-circuit the rest of the tests
if (selectState.isResolved) {
Expand All @@ -128,14 +129,20 @@ function filter(chunk, context, bodies, params, helperName, test) {
value = coerce(context.resolve(params.value), type);

if (test(key, value)) {
if (selectState) {
selectState.isResolved = true;
// Once a truth test passes, put the select into "pending" state. Now we can render the body of
// the truth test (which may contain truth tests) without altering the state of the select.
if (!selectState.isPending) {
willResolve = true;
selectState.isPending = true;
}
if(body) {
return chunk.render(body, context);
if (body) {
chunk = chunk.render(body, context);
}
if (willResolve) {
selectState.isResolved = true;
}
} else if (skip) {
return chunk.render(skip, context);
chunk = chunk.render(skip, context);
}
return chunk;
}
Expand Down
23 changes: 23 additions & 0 deletions test/jasmine-test/spec/helpersTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,29 @@
},
expected: "done done outside default default outside",
message: "should test select helper with params in an outer section"
},
{
name: "select with nested @eq",
source: ['{@select key=selectKey}',
'{@eq value=1}One',
'{@eq key=test value=5}Correct{/eq}',
'{@eq key=test value=5}!{/eq}',
'{@select}',
'{@eq value=1}Bug! No key specified{:else}Bug! No key specified{/eq}',
'{@eq key=test value=5}InnerCorrect!{/eq}',
'{@eq key=test value=5}Bug! True, but inner select is resolved.{/eq}',
'{/select}',
'{/eq}',
'{@eq value=1}Bug! True, but select is resolved.{:else}Bug! Not false{/eq}',
'{@eq key=key value=2}Bug! True, but select is resolved.{:else}Bug! Not false{/eq}',
'{/select}'].join(''),
context: {
selectKey: 1,
test: 5,
key: 2
},
expected: "OneCorrect!InnerCorrect!",
message: "Truth tests should only be skipped at the top level of a select"
}
]
},
Expand Down

0 comments on commit 5b363ab

Please sign in to comment.