Skip to content

Commit f93cd9c

Browse files
committed
feat(compiler): add full directive metadata and validation logic
With this, the new `TemplateParser` has feature/data parity with the `ProtoViewDto` of the `RenderCompiler`. Part of angular#3605 Closes angular#3880
1 parent 0f4eb1b commit f93cd9c

File tree

11 files changed

+900
-198
lines changed

11 files changed

+900
-198
lines changed

modules/angular2/src/compiler/api.ts

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import {isPresent} from 'angular2/src/core/facade/lang';
1+
import {isPresent, normalizeBool} from 'angular2/src/core/facade/lang';
22
import {HtmlAst} from './html_ast';
3+
import {ChangeDetectionStrategy} from 'angular2/src/core/change_detection/change_detection';
34

4-
export class TypeMeta {
5+
export class TypeMetadata {
56
type: any;
67
typeName: string;
78
typeUrl: string;
@@ -13,7 +14,28 @@ export class TypeMeta {
1314
}
1415
}
1516

16-
export class TemplateMeta {
17+
export class ChangeDetectionMetadata {
18+
changeDetection: ChangeDetectionStrategy;
19+
properties: string[];
20+
events: string[];
21+
hostListeners: StringMap<string, string>;
22+
hostProperties: StringMap<string, string>;
23+
constructor({changeDetection, properties, events, hostListeners, hostProperties}: {
24+
changeDetection?: ChangeDetectionStrategy,
25+
properties?: string[],
26+
events?: string[],
27+
hostListeners?: StringMap<string, string>,
28+
hostProperties?: StringMap<string, string>
29+
}) {
30+
this.changeDetection = changeDetection;
31+
this.properties = properties;
32+
this.events = events;
33+
this.hostListeners = hostListeners;
34+
this.hostProperties = hostProperties;
35+
}
36+
}
37+
38+
export class TemplateMetadata {
1739
encapsulation: ViewEncapsulation;
1840
nodes: HtmlAst[];
1941
styles: string[];
@@ -54,19 +76,25 @@ export enum ViewEncapsulation {
5476
}
5577

5678
export class DirectiveMetadata {
57-
type: TypeMeta;
79+
type: TypeMetadata;
5880
isComponent: boolean;
5981
selector: string;
60-
template: TemplateMeta;
61-
constructor({type, isComponent, selector, template}: {
62-
type?: TypeMeta,
82+
hostAttributes: Map<string, string>;
83+
changeDetection: ChangeDetectionMetadata;
84+
template: TemplateMetadata;
85+
constructor({type, isComponent, selector, hostAttributes, changeDetection, template}: {
86+
type?: TypeMetadata,
6387
isComponent?: boolean,
6488
selector?: string,
65-
template?: TemplateMeta
89+
hostAttributes?: Map<string, string>,
90+
changeDetection?: ChangeDetectionMetadata,
91+
template?: TemplateMetadata
6692
} = {}) {
6793
this.type = type;
68-
this.isComponent = isPresent(isComponent) ? isComponent : false;
94+
this.isComponent = normalizeBool(isComponent);
6995
this.selector = selector;
96+
this.hostAttributes = hostAttributes;
97+
this.changeDetection = changeDetection;
7098
this.template = template;
7199
}
72100
}

modules/angular2/src/compiler/template_ast.ts

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ export class AttrAst implements TemplateAst {
2222
visit(visitor: TemplateAstVisitor): any { return visitor.visitAttr(this); }
2323
}
2424

25-
export class BoundPropertyAst implements TemplateAst {
26-
constructor(public name: string, public value: AST, public sourceInfo: string) {}
27-
visit(visitor: TemplateAstVisitor): any { return visitor.visitProperty(this); }
25+
export class BoundElementPropertyAst implements TemplateAst {
26+
constructor(public name: string, public type: PropertyBindingType, public value: AST,
27+
public unit: string, public sourceInfo: string) {}
28+
visit(visitor: TemplateAstVisitor): any { return visitor.visitElementProperty(this); }
2829
}
2930

3031
export class BoundEventAst implements TemplateAst {
31-
constructor(public name: string, public handler: AST, public sourceInfo: string) {}
32+
constructor(public name: string, public target: string, public handler: AST,
33+
public sourceInfo: string) {}
3234
visit(visitor: TemplateAstVisitor): any { return visitor.visitEvent(this); }
3335
}
3436

@@ -38,35 +40,57 @@ export class VariableAst implements TemplateAst {
3840
}
3941

4042
export class ElementAst implements TemplateAst {
41-
constructor(public attrs: AttrAst[], public properties: BoundPropertyAst[],
43+
constructor(public attrs: AttrAst[], public properties: BoundElementPropertyAst[],
4244
public events: BoundEventAst[], public vars: VariableAst[],
43-
public directives: DirectiveMetadata[], public children: TemplateAst[],
45+
public directives: DirectiveAst[], public children: TemplateAst[],
4446
public sourceInfo: string) {}
4547
visit(visitor: TemplateAstVisitor): any { return visitor.visitElement(this); }
4648
}
4749

4850
export class EmbeddedTemplateAst implements TemplateAst {
49-
constructor(public attrs: AttrAst[], public properties: BoundPropertyAst[],
50-
public vars: VariableAst[], public directives: DirectiveMetadata[],
51-
public children: TemplateAst[], public sourceInfo: string) {}
51+
constructor(public attrs: AttrAst[], public vars: VariableAst[],
52+
public directives: DirectiveAst[], public children: TemplateAst[],
53+
public sourceInfo: string) {}
5254
visit(visitor: TemplateAstVisitor): any { return visitor.visitEmbeddedTemplate(this); }
5355
}
5456

57+
export class BoundDirectivePropertyAst implements TemplateAst {
58+
constructor(public directiveName: string, public templateName: string, public value: AST,
59+
public sourceInfo: string) {}
60+
visit(visitor: TemplateAstVisitor): any { return visitor.visitDirectiveProperty(this); }
61+
}
62+
63+
export class DirectiveAst implements TemplateAst {
64+
constructor(public directive: DirectiveMetadata, public properties: BoundDirectivePropertyAst[],
65+
public hostProperties: BoundElementPropertyAst[], public hostEvents: BoundEventAst[],
66+
public sourceInfo: string) {}
67+
visit(visitor: TemplateAstVisitor): any { return visitor.visitDirective(this); }
68+
}
69+
5570
export class NgContentAst implements TemplateAst {
5671
constructor(public select: string, public sourceInfo: string) {}
5772
visit(visitor: TemplateAstVisitor): any { return visitor.visitNgContent(this); }
5873
}
5974

75+
export enum PropertyBindingType {
76+
Property,
77+
Attribute,
78+
Class,
79+
Style
80+
}
81+
6082
export interface TemplateAstVisitor {
6183
visitNgContent(ast: NgContentAst): any;
6284
visitEmbeddedTemplate(ast: EmbeddedTemplateAst): any;
6385
visitElement(ast: ElementAst): any;
6486
visitVariable(ast: VariableAst): any;
6587
visitEvent(ast: BoundEventAst): any;
66-
visitProperty(ast: BoundPropertyAst): any;
88+
visitElementProperty(ast: BoundElementPropertyAst): any;
6789
visitAttr(ast: AttrAst): any;
6890
visitBoundText(ast: BoundTextAst): any;
6991
visitText(ast: TextAst): any;
92+
visitDirective(ast: DirectiveAst): any;
93+
visitDirectiveProperty(ast: BoundDirectivePropertyAst): any;
7094
}
7195

7296

modules/angular2/src/compiler/template_loader.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {TypeMeta, TemplateMeta, ViewEncapsulation} from './api';
1+
import {TypeMetadata, TemplateMetadata, ViewEncapsulation} from './api';
22
import {isPresent} from 'angular2/src/core/facade/lang';
33
import {Promise, PromiseWrapper} from 'angular2/src/core/facade/async';
44

@@ -28,8 +28,9 @@ export class TemplateLoader {
2828
constructor(private _xhr: XHR, private _urlResolver: UrlResolver,
2929
private _styleUrlResolver: StyleUrlResolver, private _domParser: HtmlParser) {}
3030

31-
loadTemplate(directiveType: TypeMeta, encapsulation: ViewEncapsulation, template: string,
32-
templateUrl: string, styles: string[], styleUrls: string[]): Promise<TemplateMeta> {
31+
loadTemplate(directiveType: TypeMetadata, encapsulation: ViewEncapsulation, template: string,
32+
templateUrl: string, styles: string[],
33+
styleUrls: string[]): Promise<TemplateMetadata> {
3334
if (isPresent(template)) {
3435
return PromiseWrapper.resolve(this.createTemplateFromString(
3536
directiveType, encapsulation, template, directiveType.typeUrl, styles, styleUrls));
@@ -42,9 +43,9 @@ export class TemplateLoader {
4243
}
4344
}
4445

45-
createTemplateFromString(directiveType: TypeMeta, encapsulation: ViewEncapsulation,
46+
createTemplateFromString(directiveType: TypeMetadata, encapsulation: ViewEncapsulation,
4647
template: string, templateSourceUrl: string, styles: string[],
47-
styleUrls: string[]): TemplateMeta {
48+
styleUrls: string[]): TemplateMetadata {
4849
var domNodes = this._domParser.parse(template, directiveType.typeName);
4950
var visitor = new TemplatePreparseVisitor();
5051
var remainingNodes = htmlVisitAll(visitor, domNodes);
@@ -60,7 +61,7 @@ export class TemplateLoader {
6061
allStyles.map(style => this._styleUrlResolver.resolveUrls(style, templateSourceUrl));
6162
var allStyleAbsUrls =
6263
allStyleUrls.map(styleUrl => this._urlResolver.resolve(templateSourceUrl, styleUrl));
63-
return new TemplateMeta({
64+
return new TemplateMetadata({
6465
encapsulation: encapsulation,
6566
nodes: remainingNodes,
6667
styles: allResolvedStyles,

0 commit comments

Comments
 (0)