Skip to content

Commit

Permalink
handling expressions like {{#eq foo[bar].baz xyz}}
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipskevin committed Jan 11, 2017
1 parent 9c602fc commit 2c5f353
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/expression.js
Expand Up @@ -153,6 +153,10 @@ var ScopeLookup = function(key, root) {
Lookup.apply(this, arguments);
};
ScopeLookup.prototype.value = function(scope, helperOptions){
if (this.rootExpr) {
return lookupValueInResult(this.key, this.rootExpr, scope, {}, {}).value;
}

return lookupValue(this.key, scope, helperOptions).value;
};

Expand Down Expand Up @@ -802,13 +806,12 @@ var expression = {
if (!(firstParent.children && firstParent.children.length > 0)) {
stack.addToAndPush(["Bracket"], {type: "Lookup", key: token});
} else {
// This `=== 'Helper` check is to make sure expressions
// like `helper foo[bar] car` are parsed as helpers
// instead of like `helper foo[bar].car`
if(stack.first(["Helper", "Call", "Hash", "Arg"]).type === 'Helper') {
// check if we are adding to a helper like `eq foo[bar] baz`
// but not at the `.baz` of `eq foo[bar].baz xyz`
if(stack.first(["Helper", "Call", "Hash", "Arg"]).type === 'Helper' && token[0] !== '.') {
stack.addToAndPush(["Helper"], {type: "Lookup", key: token});
} else {
// This handles the `.baz` in expressions like `foo[bar].baz`
// otherwise, handle the `.baz` in expressions like `foo[bar].baz`
stack.replaceTopAndPush({
type: "Lookup",
key: token.slice(1),
Expand Down
21 changes: 21 additions & 0 deletions test/expression-test.js
Expand Up @@ -432,6 +432,27 @@ test("expression.ast - [] operator", function(){
children: [{type: "Lookup", key: "bar"}]
}
}, "foo[bar].baz");

deepEqual(expression.ast("eq foo[bar].baz xyz"), {
type: "Helper",
method: {
type: "Lookup",
key: "eq"
},
children: [{
type: "Lookup",
key: "baz",
root: {
type: "Bracket",
root: {type: "Lookup", key: "foo"},
children: [{type: "Lookup", key: "bar"}]
}
},
{
type: "Lookup",
key: "xyz"
}]
}, "eq foo[bar].baz xyz");
});

test("expression.parse - [] operator", function(){
Expand Down
4 changes: 3 additions & 1 deletion test/stache-test.js
Expand Up @@ -5336,7 +5336,7 @@ function makeTest(name, doc, mutation) {
var template;
var div = doc.createElement('div');

template = stache("<p>{{ foo[bar].first }}</p>");
template = stache('<p>{{ foo[bar].first }}</p><p>{{#is foo[bar].first "K"}}short{{else}}long{{/is}}</p>');

var data = new CanMap({
baz: 'first',
Expand All @@ -5355,10 +5355,12 @@ function makeTest(name, doc, mutation) {
var p = div.getElementsByTagName('p');

equal(innerHTML(p[0]), 'K', 'correct value for foo[bar].first');
equal(innerHTML(p[1]), 'short', 'correct value for `is foo[bar].first "K"`');

data.attr('bar', 'fullName');

equal(innerHTML(p[0]), 'Kevin', 'updated value for foo[bar].first');
equal(innerHTML(p[1]), 'long', 'updated value for `is foo[bar].first "K"`');
});

// PUT NEW TESTS RIGHT BEFORE THIS!
Expand Down

0 comments on commit 2c5f353

Please sign in to comment.