refactor(compiler): misc + add a test for directives on inline templates

This commit is contained in:
Victor Berchet 2017-01-09 13:16:37 -08:00 committed by Igor Minar
parent ee747f7d0c
commit 3f519207a4
2 changed files with 40 additions and 22 deletions

View File

@ -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 {

View File

@ -667,6 +667,24 @@ Binding to attribute 'onEvent' is disallowed for security reasons ("<my-componen
]);
});
it('should locate directives in inline templates', () => {
const dirTemplate =
CompileDirectiveMetadata
.create({
selector: 'template',
type:
createTypeMeta({reference: {filePath: someModuleUrl, name: 'onTemplate'}})
})
.toSummary();
expect(humanizeTplAst(parse('<div *ngIf="cond">', [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 ("<my-componen
expect(elAst.providers[2].eager).toBe(false);
});
it('should not mark dependencies accross embedded views as eager', () => {
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 ("<div #a></div><div [ERROR ->]#a></div>
it('should wrap the element with data-template attribute into an EmbeddedTemplateAST ',
() => {
expect(humanizeTplAst(parse('<div data-template>', [
]))).toEqual([[EmbeddedTemplateAst], [ElementAst, 'div']]);
expect(humanizeTplAst(parse('<div data-template>', []))).toEqual([
[EmbeddedTemplateAst],
[ElementAst, 'div'],
]);
});
it('should parse bound properties', () => {
expect(humanizeTplAst(parse('<div template="ngIf test">', [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 ("<div #a></div><div [ERROR ->]#a></div>
[DirectiveAst, ngIf],
[BoundDirectivePropertyAst, 'ngIf', 'test'],
[ElementAst, 'div'],
]);
// https://github.com/angular/angular/issues/13800