fix: corrected var/# parsing in template

Closes #2084
This commit is contained in:
Misko Hevery 2015-05-21 12:17:46 -07:00
parent ab8eb4f652
commit a418397174
2 changed files with 21 additions and 17 deletions

View File

@ -538,20 +538,18 @@ class _ParseAST {
this.optionalCharacter($COLON); this.optionalCharacter($COLON);
var name = null; var name = null;
var expression = null; var expression = null;
if (this.next !== EOF) {
if (keyIsVar) { if (keyIsVar) {
if (this.optionalOperator("=")) { if (this.optionalOperator("=")) {
name = this.expectTemplateBindingKey(); name = this.expectTemplateBindingKey();
} else { } else {
name = '\$implicit'; name = '\$implicit';
} }
} else if (!this.peekKeywordVar()) { } else if (this.next !== EOF && !this.peekKeywordVar()) {
var start = this.inputIndex; var start = this.inputIndex;
var ast = this.parsePipe(); var ast = this.parsePipe();
var source = this.input.substring(start, this.inputIndex); var source = this.input.substring(start, this.inputIndex);
expression = new ASTWithSource(ast, source, this.location); expression = new ASTWithSource(ast, source, this.location);
} }
}
ListWrapper.push(bindings, new TemplateBinding(key, keyIsVar, name, expression)); ListWrapper.push(bindings, new TemplateBinding(key, keyIsVar, name, expression));
if (!this.optionalCharacter($SEMICOLON)) { if (!this.optionalCharacter($SEMICOLON)) {
this.optionalCharacter($COMMA); this.optionalCharacter($COMMA);

View File

@ -447,7 +447,7 @@ export function main() {
function keyValues(templateBindings) { function keyValues(templateBindings) {
return ListWrapper.map(templateBindings, (binding) => { return ListWrapper.map(templateBindings, (binding) => {
if (binding.keyIsVar) { if (binding.keyIsVar) {
return '#' + binding.key + (isBlank(binding.name) ? '' : '=' + binding.name); return '#' + binding.key + (isBlank(binding.name) ? '=null' : '=' + binding.name);
} else { } else {
return binding.key + (isBlank(binding.expression) ? '' : `=${binding.expression}`) return binding.key + (isBlank(binding.expression) ? '' : `=${binding.expression}`)
} }
@ -471,7 +471,7 @@ export function main() {
expect(bindings).toEqual([]); expect(bindings).toEqual([]);
}); });
iit('should parse a string without a value', () => { it('should parse a string without a value', () => {
var bindings = parseTemplateBindings('a'); var bindings = parseTemplateBindings('a');
expect(keys(bindings)).toEqual(['a']); expect(keys(bindings)).toEqual(['a']);
}); });
@ -508,7 +508,7 @@ export function main() {
it('should detect names as value', () => { it('should detect names as value', () => {
var bindings = parseTemplateBindings("a:#b"); var bindings = parseTemplateBindings("a:#b");
expect(keyValues(bindings)).toEqual(['a', '#b']); expect(keyValues(bindings)).toEqual(['a', '#b=\$implicit']);
}); });
it('should allow space and colon as separators', () => { it('should allow space and colon as separators', () => {
@ -540,10 +540,16 @@ export function main() {
it('should support var/# notation', () => { it('should support var/# notation', () => {
var bindings = parseTemplateBindings("var i"); var bindings = parseTemplateBindings("var i");
expect(keyValues(bindings)).toEqual(['#i']); expect(keyValues(bindings)).toEqual(['#i=\$implicit']);
bindings = parseTemplateBindings("#i"); bindings = parseTemplateBindings("#i");
expect(keyValues(bindings)).toEqual(['#i']); expect(keyValues(bindings)).toEqual(['#i=\$implicit']);
bindings = parseTemplateBindings("var a; var b");
expect(keyValues(bindings)).toEqual(['#a=\$implicit', '#b=\$implicit']);
bindings = parseTemplateBindings("#a; #b;");
expect(keyValues(bindings)).toEqual(['#a=\$implicit', '#b=\$implicit']);
bindings = parseTemplateBindings("var i-a = k-a"); bindings = parseTemplateBindings("var i-a = k-a");
expect(keyValues(bindings)).toEqual(['#i-a=k-a']); expect(keyValues(bindings)).toEqual(['#i-a=k-a']);