From 3f519207a488537324e516184cd4e26d4ba78d81 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Mon, 9 Jan 2017 13:16:37 -0800 Subject: [PATCH] refactor(compiler): misc + add a test for directives on inline templates --- .../src/template_parser/template_parser.ts | 29 +++++++--------- .../template_parser/template_parser_spec.ts | 33 ++++++++++++++++--- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/modules/@angular/compiler/src/template_parser/template_parser.ts b/modules/@angular/compiler/src/template_parser/template_parser.ts index 5810496e49..121f8fa561 100644 --- a/modules/@angular/compiler/src/template_parser/template_parser.ts +++ b/modules/@angular/compiler/src/template_parser/template_parser.ts @@ -30,30 +30,28 @@ import {BindingParser, BoundProperty} from './binding_parser'; import {AttrAst, BoundDirectivePropertyAst, BoundElementPropertyAst, BoundEventAst, BoundTextAst, DirectiveAst, ElementAst, EmbeddedTemplateAst, NgContentAst, PropertyBindingType, ReferenceAst, TemplateAst, TemplateAstVisitor, TextAst, VariableAst, templateVisitAll} from './template_ast'; import {PreparsedElementType, preparseElement} from './template_preparser'; - - -// Group 1 = "bind-" -// Group 2 = "let-" -// Group 3 = "ref-/#" -// Group 4 = "on-" -// Group 5 = "bindon-" -// Group 6 = "@" -// Group 7 = the identifier after "bind-", "let-", "ref-/#", "on-", "bindon-" or "@" -// Group 8 = identifier inside [()] -// Group 9 = identifier inside [] -// Group 10 = identifier inside () const BIND_NAME_REGEXP = /^(?:(?:(?:(bind-)|(let-)|(ref-|#)|(on-)|(bindon-)|(@))(.+))|\[\(([^\)]+)\)\]|\[([^\]]+)\]|\(([^\)]+)\))$/; +// Group 1 = "bind-" const KW_BIND_IDX = 1; +// Group 2 = "let-" const KW_LET_IDX = 2; +// Group 3 = "ref-/#" const KW_REF_IDX = 3; +// Group 4 = "on-" const KW_ON_IDX = 4; +// Group 5 = "bindon-" const KW_BINDON_IDX = 5; +// Group 6 = "@" const KW_AT_IDX = 6; +// Group 7 = the identifier after "bind-", "let-", "ref-/#", "on-", "bindon-" or "@" const IDENT_KW_IDX = 7; +// Group 8 = identifier inside [()] const IDENT_BANANA_BOX_IDX = 8; +// Group 9 = identifier inside [] const IDENT_PROPERTY_IDX = 9; +// Group 10 = identifier inside () const IDENT_EVENT_IDX = 10; const TEMPLATE_ELEMENT = 'template'; @@ -231,11 +229,8 @@ class TemplateParseVisitor implements html.Visitor { visitText(text: html.Text, parent: ElementContext): any { const ngContentIndex = parent.findNgContentIndex(TEXT_CSS_SELECTOR); const expr = this._bindingParser.parseInterpolation(text.value, text.sourceSpan); - if (expr) { - return new BoundTextAst(expr, ngContentIndex, text.sourceSpan); - } else { - return new TextAst(text.value, ngContentIndex, text.sourceSpan); - } + return expr ? new BoundTextAst(expr, ngContentIndex, text.sourceSpan) : + new TextAst(text.value, ngContentIndex, text.sourceSpan); } visitAttribute(attribute: html.Attribute, context: any): any { diff --git a/modules/@angular/compiler/test/template_parser/template_parser_spec.ts b/modules/@angular/compiler/test/template_parser/template_parser_spec.ts index f84554dbdf..68cf09b423 100644 --- a/modules/@angular/compiler/test/template_parser/template_parser_spec.ts +++ b/modules/@angular/compiler/test/template_parser/template_parser_spec.ts @@ -667,6 +667,24 @@ Binding to attribute 'onEvent' is disallowed for security reasons (" { + const dirTemplate = + CompileDirectiveMetadata + .create({ + selector: 'template', + type: + createTypeMeta({reference: {filePath: someModuleUrl, name: 'onTemplate'}}) + }) + .toSummary(); + expect(humanizeTplAst(parse('
', [ngIf, dirTemplate]))).toEqual([ + [EmbeddedTemplateAst], + [DirectiveAst, ngIf], + [BoundDirectivePropertyAst, 'ngIf', 'cond'], + [DirectiveAst, dirTemplate], + [ElementAst, 'div'], + ]); + }); + it('should locate directives in event bindings', () => { const dirA = CompileDirectiveMetadata @@ -1042,7 +1060,7 @@ Binding to attribute 'onEvent' is disallowed for security reasons (" { + it('should not mark dependencies across embedded views as eager', () => { const provider0 = createProvider('service0'); const dirA = createDir('[dirA]', {providers: [provider0]}); const dirB = createDir('[dirB]', {deps: ['service0']}); @@ -1243,14 +1261,18 @@ Reference "#a" is defined several times ("
]#a>
it('should wrap the element with data-template attribute into an EmbeddedTemplateAST ', () => { - expect(humanizeTplAst(parse('
', [ - ]))).toEqual([[EmbeddedTemplateAst], [ElementAst, 'div']]); + expect(humanizeTplAst(parse('
', []))).toEqual([ + [EmbeddedTemplateAst], + [ElementAst, 'div'], + ]); }); it('should parse bound properties', () => { expect(humanizeTplAst(parse('
', [ngIf]))).toEqual([ - [EmbeddedTemplateAst], [DirectiveAst, ngIf], - [BoundDirectivePropertyAst, 'ngIf', 'test'], [ElementAst, 'div'] + [EmbeddedTemplateAst], + [DirectiveAst, ngIf], + [BoundDirectivePropertyAst, 'ngIf', 'test'], + [ElementAst, 'div'], ]); }); @@ -1329,6 +1351,7 @@ Reference "#a" is defined several times ("
]#a>
[DirectiveAst, ngIf], [BoundDirectivePropertyAst, 'ngIf', 'test'], [ElementAst, 'div'], + ]); // https://github.com/angular/angular/issues/13800