Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Add parsing support for index binding
Browse files Browse the repository at this point in the history
This allows parsing of

  repeat="value, index in collection"

The binding is not yet hooked up so the test is disabled.

R=rafaelw@chromium.org

Review URL: https://codereview.appspot.com/14721043
  • Loading branch information
arv committed Oct 15, 2013
1 parent 75858bd commit fe23561
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/polymer-expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
this.depsList = [];
this.currentPath = undefined;
this.ident = undefined;
this.indexName = undefined;
}

ASTDelegate.prototype = {
Expand Down Expand Up @@ -314,9 +315,10 @@
this.ident = ident;
},

createInExpression: function(ident, expression) {
createInExpression: function(ident, indexName, expression) {
this.expression = expression;
this.ident = ident;
this.indexName = indexName;
},

createTopLevel: function(expression) {
Expand Down
25 changes: 25 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -862,4 +862,29 @@ suite('PolymerExpressions', function() {
Platform.performMicrotaskCheckpoint();
assert.equal('B', div.childNodes[1].textContent);
});

test('in expression with index scope', function() {
var div = createTestHtml(
'<template repeat="{{ user, index in array }}">' +
'{{ index }}. {{ user }}' +
'</template>');

var model = {
array: ['a', 'b']
};

recursivelySetTemplateModel(div, model);
Platform.performMicrotaskCheckpoint();

// TODO(arv): Enable the rest of this test when we have hooked up the
// binding for the index.
// assert.strictEqual('0. a', div.childNodes[1].textContent);
// assert.strictEqual('1. b', div.childNodes[2].textContent);

// model.array.splice(1, 0, 'c');
// Platform.performMicrotaskCheckpoint();
// assert.strictEqual('0. a', div.childNodes[1].textContent);
// assert.strictEqual('1. c', div.childNodes[2].textContent);
// assert.strictEqual('2. b', div.childNodes[3].textContent);
});
});
13 changes: 11 additions & 2 deletions third_party/esprima/esprima.js
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,7 @@
// Expression as Identifier

// InExpression ::
// Identifier, Identifier in Expression
// Identifier in Expression

function parseTopLevel() {
Expand All @@ -981,7 +982,7 @@
if (expr) {
if (lookahead.value === 'as') {
parseAsExpression(expr);
} else if (lookahead.value === 'in' &&
} else if (lookahead.value === ',' || lookahead.value == 'in' &&
expr.type === Syntax.Identifier) {
parseInExpression(expr);
} else if (match('|')) {
Expand Down Expand Up @@ -1034,9 +1035,17 @@
}

function parseInExpression(identifier) {
var indexName;
if (lookahead.value === ',') {
lex();
if (lookahead.type !== Token.Identifier)
throwUnexpected(lookahead);
indexName = lex().value;
}

lex(); // in
var expr = parseExpression();
delegate.createInExpression(identifier.name, expr);
delegate.createInExpression(identifier.name, indexName, expr);
}

function parse(code, inDelegate) {
Expand Down

0 comments on commit fe23561

Please sign in to comment.