Skip to content

Commit

Permalink
feat(compiler): add full directive metadata and validation logic
Browse files Browse the repository at this point in the history
With this, the new `TemplateParser` has feature/data parity with the `ProtoViewDto` of the `RenderCompiler`.

Part of #3605

Closes #3880
  • Loading branch information
tbosch committed Aug 28, 2015
1 parent 0f4eb1b commit f93cd9c
Show file tree
Hide file tree
Showing 11 changed files with 900 additions and 198 deletions.
46 changes: 37 additions & 9 deletions modules/angular2/src/compiler/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {isPresent} from 'angular2/src/core/facade/lang';
import {isPresent, normalizeBool} from 'angular2/src/core/facade/lang';
import {HtmlAst} from './html_ast';
import {ChangeDetectionStrategy} from 'angular2/src/core/change_detection/change_detection';

export class TypeMeta {
export class TypeMetadata {
type: any;
typeName: string;
typeUrl: string;
Expand All @@ -13,7 +14,28 @@ export class TypeMeta {
}
}

export class TemplateMeta {
export class ChangeDetectionMetadata {
changeDetection: ChangeDetectionStrategy;
properties: string[];
events: string[];
hostListeners: StringMap<string, string>;
hostProperties: StringMap<string, string>;
constructor({changeDetection, properties, events, hostListeners, hostProperties}: {
changeDetection?: ChangeDetectionStrategy,
properties?: string[],
events?: string[],
hostListeners?: StringMap<string, string>,
hostProperties?: StringMap<string, string>
}) {
this.changeDetection = changeDetection;
this.properties = properties;
this.events = events;
this.hostListeners = hostListeners;
this.hostProperties = hostProperties;
}
}

export class TemplateMetadata {
encapsulation: ViewEncapsulation;
nodes: HtmlAst[];
styles: string[];
Expand Down Expand Up @@ -54,19 +76,25 @@ export enum ViewEncapsulation {
}

export class DirectiveMetadata {
type: TypeMeta;
type: TypeMetadata;
isComponent: boolean;
selector: string;
template: TemplateMeta;
constructor({type, isComponent, selector, template}: {
type?: TypeMeta,
hostAttributes: Map<string, string>;
changeDetection: ChangeDetectionMetadata;
template: TemplateMetadata;
constructor({type, isComponent, selector, hostAttributes, changeDetection, template}: {
type?: TypeMetadata,
isComponent?: boolean,
selector?: string,
template?: TemplateMeta
hostAttributes?: Map<string, string>,
changeDetection?: ChangeDetectionMetadata,
template?: TemplateMetadata
} = {}) {
this.type = type;
this.isComponent = isPresent(isComponent) ? isComponent : false;
this.isComponent = normalizeBool(isComponent);
this.selector = selector;
this.hostAttributes = hostAttributes;
this.changeDetection = changeDetection;
this.template = template;
}
}
44 changes: 34 additions & 10 deletions modules/angular2/src/compiler/template_ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ export class AttrAst implements TemplateAst {
visit(visitor: TemplateAstVisitor): any { return visitor.visitAttr(this); }
}

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

export class BoundEventAst implements TemplateAst {
constructor(public name: string, public handler: AST, public sourceInfo: string) {}
constructor(public name: string, public target: string, public handler: AST,
public sourceInfo: string) {}
visit(visitor: TemplateAstVisitor): any { return visitor.visitEvent(this); }
}

Expand All @@ -38,35 +40,57 @@ export class VariableAst implements TemplateAst {
}

export class ElementAst implements TemplateAst {
constructor(public attrs: AttrAst[], public properties: BoundPropertyAst[],
constructor(public attrs: AttrAst[], public properties: BoundElementPropertyAst[],
public events: BoundEventAst[], public vars: VariableAst[],
public directives: DirectiveMetadata[], public children: TemplateAst[],
public directives: DirectiveAst[], public children: TemplateAst[],
public sourceInfo: string) {}
visit(visitor: TemplateAstVisitor): any { return visitor.visitElement(this); }
}

export class EmbeddedTemplateAst implements TemplateAst {
constructor(public attrs: AttrAst[], public properties: BoundPropertyAst[],
public vars: VariableAst[], public directives: DirectiveMetadata[],
public children: TemplateAst[], public sourceInfo: string) {}
constructor(public attrs: AttrAst[], public vars: VariableAst[],
public directives: DirectiveAst[], public children: TemplateAst[],
public sourceInfo: string) {}
visit(visitor: TemplateAstVisitor): any { return visitor.visitEmbeddedTemplate(this); }
}

export class BoundDirectivePropertyAst implements TemplateAst {
constructor(public directiveName: string, public templateName: string, public value: AST,
public sourceInfo: string) {}
visit(visitor: TemplateAstVisitor): any { return visitor.visitDirectiveProperty(this); }
}

export class DirectiveAst implements TemplateAst {
constructor(public directive: DirectiveMetadata, public properties: BoundDirectivePropertyAst[],
public hostProperties: BoundElementPropertyAst[], public hostEvents: BoundEventAst[],
public sourceInfo: string) {}
visit(visitor: TemplateAstVisitor): any { return visitor.visitDirective(this); }
}

export class NgContentAst implements TemplateAst {
constructor(public select: string, public sourceInfo: string) {}
visit(visitor: TemplateAstVisitor): any { return visitor.visitNgContent(this); }
}

export enum PropertyBindingType {
Property,
Attribute,
Class,
Style
}

export interface TemplateAstVisitor {
visitNgContent(ast: NgContentAst): any;
visitEmbeddedTemplate(ast: EmbeddedTemplateAst): any;
visitElement(ast: ElementAst): any;
visitVariable(ast: VariableAst): any;
visitEvent(ast: BoundEventAst): any;
visitProperty(ast: BoundPropertyAst): any;
visitElementProperty(ast: BoundElementPropertyAst): any;
visitAttr(ast: AttrAst): any;
visitBoundText(ast: BoundTextAst): any;
visitText(ast: TextAst): any;
visitDirective(ast: DirectiveAst): any;
visitDirectiveProperty(ast: BoundDirectivePropertyAst): any;
}


Expand Down
13 changes: 7 additions & 6 deletions modules/angular2/src/compiler/template_loader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {TypeMeta, TemplateMeta, ViewEncapsulation} from './api';
import {TypeMetadata, TemplateMetadata, ViewEncapsulation} from './api';
import {isPresent} from 'angular2/src/core/facade/lang';
import {Promise, PromiseWrapper} from 'angular2/src/core/facade/async';

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

loadTemplate(directiveType: TypeMeta, encapsulation: ViewEncapsulation, template: string,
templateUrl: string, styles: string[], styleUrls: string[]): Promise<TemplateMeta> {
loadTemplate(directiveType: TypeMetadata, encapsulation: ViewEncapsulation, template: string,
templateUrl: string, styles: string[],
styleUrls: string[]): Promise<TemplateMetadata> {
if (isPresent(template)) {
return PromiseWrapper.resolve(this.createTemplateFromString(
directiveType, encapsulation, template, directiveType.typeUrl, styles, styleUrls));
Expand All @@ -42,9 +43,9 @@ export class TemplateLoader {
}
}

createTemplateFromString(directiveType: TypeMeta, encapsulation: ViewEncapsulation,
createTemplateFromString(directiveType: TypeMetadata, encapsulation: ViewEncapsulation,
template: string, templateSourceUrl: string, styles: string[],
styleUrls: string[]): TemplateMeta {
styleUrls: string[]): TemplateMetadata {
var domNodes = this._domParser.parse(template, directiveType.typeName);
var visitor = new TemplatePreparseVisitor();
var remainingNodes = htmlVisitAll(visitor, domNodes);
Expand All @@ -60,7 +61,7 @@ export class TemplateLoader {
allStyles.map(style => this._styleUrlResolver.resolveUrls(style, templateSourceUrl));
var allStyleAbsUrls =
allStyleUrls.map(styleUrl => this._urlResolver.resolve(templateSourceUrl, styleUrl));
return new TemplateMeta({
return new TemplateMetadata({
encapsulation: encapsulation,
nodes: remainingNodes,
styles: allResolvedStyles,
Expand Down
Loading

0 comments on commit f93cd9c

Please sign in to comment.