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

Commit

Permalink
Refactor mustache token parsing in advance of building instance-bindi…
Browse files Browse the repository at this point in the history
…ng-map

R=arv
BUG=

Review URL: https://codereview.appspot.com/10858044
  • Loading branch information
rafaelw committed Jul 2, 2013
1 parent 8843b72 commit f1af6fe
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions src/template_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -968,15 +968,18 @@
}
});

var TEXT = 0;
var BINDING = 1;

function Token(type, value) {
this.type = type;
this.value = value;
function isSimpleBinding(tokens) {
// tokens ==? ['', path, '']
return tokens.length == 3 && tokens[0].length == 0 && tokens[2].length == 0;
}

// Returns
// a) undefined if there are no mustaches.
// b) [TEXT, (PATH, TEXT)+] if there is at least one mustache.
function parseMustacheTokens(s) {
if (!s || s.length === 0)
return;

var tokens = undefined;
var length = s.length;
var startIndex = 0, lastIndex = 0, endIndex = 0;
Expand All @@ -985,22 +988,22 @@
endIndex = startIndex < 0 ? -1 : s.indexOf('}}', startIndex + 2);

if (endIndex < 0) {
if (tokens)
tokens.push(new Token(TEXT, s.slice(lastIndex)));
break;
}
if (!tokens)
return;

// There is a non-empty text run before the next path token.
if (startIndex - lastIndex > 0) {
tokens = tokens || [];
tokens.push(new Token(TEXT, s.slice(lastIndex, startIndex)));
tokens.push(s.slice(lastIndex)); // TEXT
break;
}

tokens = tokens || [];
tokens.push(new Token(BINDING, s.slice(startIndex + 2, endIndex).trim()));
tokens.push(s.slice(lastIndex, startIndex)); // TEXT
tokens.push(s.slice(startIndex + 2, endIndex).trim()); // PATH
lastIndex = endIndex + 2;
}

if (lastIndex === length)
tokens.push(''); // TEXT

return tokens;
}

Expand All @@ -1023,25 +1026,22 @@
if (!tokens)
return;

if (tokens.length == 1) {
bindOrDelegate(node, name, model, tokens[0].value, delegate);
if (isSimpleBinding(tokens)) {
bindOrDelegate(node, name, model, tokens[1], delegate);
return;
}

var replacementBinding = new CompoundBinding();
for (var i = 0; i < tokens.length; i++) {
var token = tokens[i];
if (token.type == BINDING)
bindOrDelegate(replacementBinding, i, model, token.value, delegate);
for (var i = 1; i < tokens.length; i = i + 2) {
bindOrDelegate(replacementBinding, i, model, tokens[i], delegate);

This comment has been minimized.

Copy link
@jmesserly

jmesserly Jul 23, 2013

Contributor

hey @rafaelw -- I noticed a somewhat subtle change this causes. bindOrDelegate will call the bindingDelegate and pass (model, path, name, node). In this case node is the replacementBinding and name is the index i. Previously, the indexes were 0, 1, 2, 3, ... corresponding to the index of the mustache expr. Now they're 1, 3, 5, 9, ... which is a little bit different. I'm curious if this API change was intended or not.

}

replacementBinding.combinator = function(values) {
var newValue = '';

for (var i = 0; i < tokens.length; i++) {
var token = tokens[i];
if (token.type === TEXT) {
newValue += token.value;
for (var i = 0, text = true; i < tokens.length; i++, text = !text) {
if (text) {
newValue += tokens[i];
} else {
var value = values[i];
if (value !== undefined)
Expand Down

0 comments on commit f1af6fe

Please sign in to comment.