From 231773ea762ad45df1612efaeeec29d9b4ebca68 Mon Sep 17 00:00:00 2001 From: vsavkin Date: Wed, 3 Feb 2016 11:35:42 -0800 Subject: [PATCH] fix(compiler): use event names for matching directives Closes #6870 --- .../angular2/src/compiler/template_parser.ts | 5 +++-- .../test/compiler/template_parser_spec.ts | 9 ++++++++ .../test/core/linker/integration_spec.ts | 22 +++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/modules/angular2/src/compiler/template_parser.ts b/modules/angular2/src/compiler/template_parser.ts index 865532ebd7..dd3c970d8a 100644 --- a/modules/angular2/src/compiler/template_parser.ts +++ b/modules/angular2/src/compiler/template_parser.ts @@ -433,8 +433,9 @@ class TemplateParseVisitor implements HtmlAstVisitor { var parts = splitAtColon(name, [null, name]); var target = parts[0]; var eventName = parts[1]; - targetEvents.push(new BoundEventAst(eventName, target, - this._parseAction(expression, sourceSpan), sourceSpan)); + var ast = this._parseAction(expression, sourceSpan); + targetMatchableAttrs.push([name, ast.source]); + targetEvents.push(new BoundEventAst(eventName, target, ast, sourceSpan)); // Don't detect directives for event names for now, // so don't add the event name to the matchableAttrs } diff --git a/modules/angular2/test/compiler/template_parser_spec.ts b/modules/angular2/test/compiler/template_parser_spec.ts index 55a42d6d55..6c37b5c783 100644 --- a/modules/angular2/test/compiler/template_parser_spec.ts +++ b/modules/angular2/test/compiler/template_parser_spec.ts @@ -327,6 +327,15 @@ export function main() { ]); }); + it('should locate directives in event bindings', () => { + var dirA = CompileDirectiveMetadata.create( + {selector: '[a]', type: new CompileTypeMetadata({name: 'DirB'})}); + + expect(humanizeTplAst(parse('
', [dirA]))) + .toEqual( + [[ElementAst, 'div'], [BoundEventAst, 'a', null, 'b'], [DirectiveAst, dirA]]); + }); + it('should parse directive host properties', () => { var dirA = CompileDirectiveMetadata.create({ selector: 'div', diff --git a/modules/angular2/test/core/linker/integration_spec.ts b/modules/angular2/test/core/linker/integration_spec.ts index 29cb28e709..6eca60cb15 100644 --- a/modules/angular2/test/core/linker/integration_spec.ts +++ b/modules/angular2/test/core/linker/integration_spec.ts @@ -448,6 +448,21 @@ function declareTests() { }); })); + it('should support directives where a selector matches event binding', + inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { + tcb.overrideView( + MyComp, + new ViewMetadata( + {template: '

', directives: [EventDir]})) + + .createAsync(MyComp) + .then((fixture) => { + var tc = fixture.debugElement.children[0]; + expect(tc.inject(EventDir)).not.toBe(null); + async.done(); + }); + })); + it('should read directives metadata from their binding token', inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { tcb.overrideView(MyComp, new ViewMetadata({ @@ -2133,6 +2148,13 @@ class IdDir { id: string; } +@Directive({selector: '[customEvent]'}) +@Injectable() +class EventDir { + @Output() customEvent = new EventEmitter(); + doSomething() {} +} + @Directive({selector: '[static]'}) @Injectable() class NeedsAttribute {