| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | import {AST} from 'angular2/src/core/change_detection/change_detection'; | 
					
						
							| 
									
										
										
										
											2015-11-06 17:34:07 -08:00
										 |  |  | import {isPresent} from 'angular2/src/facade/lang'; | 
					
						
							| 
									
										
										
										
											2015-09-18 10:33:23 -07:00
										 |  |  | import {CompileDirectiveMetadata} from './directive_metadata'; | 
					
						
							| 
									
										
										
										
											2015-10-07 09:34:21 -07:00
										 |  |  | import {ParseSourceSpan} from './parse_util'; | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * An Abstract Syntax Tree node representing part of a parsed Angular template. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | export interface TemplateAst { | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * The source span from which this node was parsed. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2015-10-07 09:34:21 -07:00
										 |  |  |   sourceSpan: ParseSourceSpan; | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Visit this node and possibly transform it. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2015-09-01 16:24:23 -07:00
										 |  |  |   visit(visitor: TemplateAstVisitor, context: any): any; | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * A segment of text within the template. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | export class TextAst implements TemplateAst { | 
					
						
							| 
									
										
										
										
											2016-04-07 17:17:50 -07:00
										 |  |  |   constructor( | 
					
						
							|  |  |  |       public value: string, public ngContentIndex: number, public sourceSpan: ParseSourceSpan) {} | 
					
						
							| 
									
										
										
										
											2015-09-01 16:24:23 -07:00
										 |  |  |   visit(visitor: TemplateAstVisitor, context: any): any { return visitor.visitText(this, context); } | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * A bound expression within the text of a template. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | export class BoundTextAst implements TemplateAst { | 
					
						
							| 
									
										
										
										
											2016-04-07 17:17:50 -07:00
										 |  |  |   constructor( | 
					
						
							|  |  |  |       public value: AST, public ngContentIndex: number, public sourceSpan: ParseSourceSpan) {} | 
					
						
							| 
									
										
										
										
											2015-09-01 16:24:23 -07:00
										 |  |  |   visit(visitor: TemplateAstVisitor, context: any): any { | 
					
						
							|  |  |  |     return visitor.visitBoundText(this, context); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * A plain attribute on an element. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | export class AttrAst implements TemplateAst { | 
					
						
							| 
									
										
										
										
											2015-10-07 09:34:21 -07:00
										 |  |  |   constructor(public name: string, public value: string, public sourceSpan: ParseSourceSpan) {} | 
					
						
							| 
									
										
										
										
											2015-09-01 16:24:23 -07:00
										 |  |  |   visit(visitor: TemplateAstVisitor, context: any): any { return visitor.visitAttr(this, context); } | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * A binding for an element property (e.g. `[property]="expression"`). | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-08-27 16:29:02 -07:00
										 |  |  | export class BoundElementPropertyAst implements TemplateAst { | 
					
						
							| 
									
										
										
										
											2016-04-07 17:17:50 -07:00
										 |  |  |   constructor( | 
					
						
							|  |  |  |       public name: string, public type: PropertyBindingType, public value: AST, public unit: string, | 
					
						
							|  |  |  |       public sourceSpan: ParseSourceSpan) {} | 
					
						
							| 
									
										
										
										
											2015-09-01 16:24:23 -07:00
										 |  |  |   visit(visitor: TemplateAstVisitor, context: any): any { | 
					
						
							|  |  |  |     return visitor.visitElementProperty(this, context); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * A binding for an element event (e.g. `(event)="handler()"`). | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | export class BoundEventAst implements TemplateAst { | 
					
						
							| 
									
										
										
										
											2016-04-07 17:17:50 -07:00
										 |  |  |   constructor( | 
					
						
							|  |  |  |       public name: string, public target: string, public handler: AST, | 
					
						
							|  |  |  |       public sourceSpan: ParseSourceSpan) {} | 
					
						
							| 
									
										
										
										
											2015-09-01 16:24:23 -07:00
										 |  |  |   visit(visitor: TemplateAstVisitor, context: any): any { | 
					
						
							|  |  |  |     return visitor.visitEvent(this, context); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-09-18 10:33:23 -07:00
										 |  |  |   get fullName() { | 
					
						
							| 
									
										
										
										
											2015-09-11 13:37:05 -07:00
										 |  |  |     if (isPresent(this.target)) { | 
					
						
							|  |  |  |       return `${this.target}:${this.name}`; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       return this.name; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * A variable declaration on an element (e.g. `#var="expression"`). | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | export class VariableAst implements TemplateAst { | 
					
						
							| 
									
										
										
										
											2015-10-07 09:34:21 -07:00
										 |  |  |   constructor(public name: string, public value: string, public sourceSpan: ParseSourceSpan) {} | 
					
						
							| 
									
										
										
										
											2015-09-01 16:24:23 -07:00
										 |  |  |   visit(visitor: TemplateAstVisitor, context: any): any { | 
					
						
							|  |  |  |     return visitor.visitVariable(this, context); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * An element declaration in a template. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | export class ElementAst implements TemplateAst { | 
					
						
							| 
									
										
										
										
											2016-04-07 17:17:50 -07:00
										 |  |  |   constructor( | 
					
						
							|  |  |  |       public name: string, public attrs: AttrAst[], public inputs: BoundElementPropertyAst[], | 
					
						
							|  |  |  |       public outputs: BoundEventAst[], public exportAsVars: VariableAst[], | 
					
						
							|  |  |  |       public directives: DirectiveAst[], public children: TemplateAst[], | 
					
						
							|  |  |  |       public ngContentIndex: number, public sourceSpan: ParseSourceSpan) {} | 
					
						
							| 
									
										
										
										
											2015-09-01 16:24:23 -07:00
										 |  |  |   visit(visitor: TemplateAstVisitor, context: any): any { | 
					
						
							|  |  |  |     return visitor.visitElement(this, context); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Whether the element has any active bindings (inputs, outputs, vars, or directives). | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2015-09-01 16:24:23 -07:00
										 |  |  |   isBound(): boolean { | 
					
						
							| 
									
										
										
										
											2016-04-07 17:17:50 -07:00
										 |  |  |     return ( | 
					
						
							|  |  |  |         this.inputs.length > 0 || this.outputs.length > 0 || this.exportAsVars.length > 0 || | 
					
						
							|  |  |  |         this.directives.length > 0); | 
					
						
							| 
									
										
										
										
											2015-09-01 16:24:23 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-09-11 13:37:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Get the component associated with this element, if any. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2015-09-18 10:33:23 -07:00
										 |  |  |   getComponent(): CompileDirectiveMetadata { | 
					
						
							| 
									
										
										
										
											2015-09-11 13:37:05 -07:00
										 |  |  |     return this.directives.length > 0 && this.directives[0].directive.isComponent ? | 
					
						
							| 
									
										
										
										
											2016-04-07 17:17:50 -07:00
										 |  |  |         this.directives[0].directive : | 
					
						
							|  |  |  |         null; | 
					
						
							| 
									
										
										
										
											2015-09-11 13:37:05 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * A `<template>` element included in an Angular template. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | export class EmbeddedTemplateAst implements TemplateAst { | 
					
						
							| 
									
										
										
										
											2016-04-07 17:17:50 -07:00
										 |  |  |   constructor( | 
					
						
							|  |  |  |       public attrs: AttrAst[], public outputs: BoundEventAst[], public vars: VariableAst[], | 
					
						
							|  |  |  |       public directives: DirectiveAst[], public children: TemplateAst[], | 
					
						
							|  |  |  |       public ngContentIndex: number, public sourceSpan: ParseSourceSpan) {} | 
					
						
							| 
									
										
										
										
											2015-09-01 16:24:23 -07:00
										 |  |  |   visit(visitor: TemplateAstVisitor, context: any): any { | 
					
						
							|  |  |  |     return visitor.visitEmbeddedTemplate(this, context); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * A directive property with a bound value (e.g. `*ngIf="condition").
 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-08-27 16:29:02 -07:00
										 |  |  | export class BoundDirectivePropertyAst implements TemplateAst { | 
					
						
							| 
									
										
										
										
											2016-04-07 17:17:50 -07:00
										 |  |  |   constructor( | 
					
						
							|  |  |  |       public directiveName: string, public templateName: string, public value: AST, | 
					
						
							|  |  |  |       public sourceSpan: ParseSourceSpan) {} | 
					
						
							| 
									
										
										
										
											2015-09-01 16:24:23 -07:00
										 |  |  |   visit(visitor: TemplateAstVisitor, context: any): any { | 
					
						
							|  |  |  |     return visitor.visitDirectiveProperty(this, context); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-27 16:29:02 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * A directive declared on an element. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-08-27 16:29:02 -07:00
										 |  |  | export class DirectiveAst implements TemplateAst { | 
					
						
							| 
									
										
										
										
											2016-04-07 17:17:50 -07:00
										 |  |  |   constructor( | 
					
						
							|  |  |  |       public directive: CompileDirectiveMetadata, public inputs: BoundDirectivePropertyAst[], | 
					
						
							|  |  |  |       public hostProperties: BoundElementPropertyAst[], public hostEvents: BoundEventAst[], | 
					
						
							|  |  |  |       public exportAsVars: VariableAst[], public sourceSpan: ParseSourceSpan) {} | 
					
						
							| 
									
										
										
										
											2015-09-01 16:24:23 -07:00
										 |  |  |   visit(visitor: TemplateAstVisitor, context: any): any { | 
					
						
							|  |  |  |     return visitor.visitDirective(this, context); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-27 16:29:02 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Position where content is to be projected (instance of `<ng-content>` in a template). | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | export class NgContentAst implements TemplateAst { | 
					
						
							| 
									
										
										
										
											2016-04-07 17:17:50 -07:00
										 |  |  |   constructor( | 
					
						
							|  |  |  |       public index: number, public ngContentIndex: number, public sourceSpan: ParseSourceSpan) {} | 
					
						
							| 
									
										
										
										
											2015-09-01 16:24:23 -07:00
										 |  |  |   visit(visitor: TemplateAstVisitor, context: any): any { | 
					
						
							|  |  |  |     return visitor.visitNgContent(this, context); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Enumeration of types of property bindings. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-08-27 16:29:02 -07:00
										 |  |  | export enum PropertyBindingType { | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * A normal binding to a property (e.g. `[property]="expression"`). | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2015-08-27 16:29:02 -07:00
										 |  |  |   Property, | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * A binding to an element attribute (e.g. `[attr.name]="expression"`). | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2015-08-27 16:29:02 -07:00
										 |  |  |   Attribute, | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * A binding to a CSS class (e.g. `[class.name]="condition"`). | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2015-08-27 16:29:02 -07:00
										 |  |  |   Class, | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * A binding to a style rule (e.g. `[style.rule]="expression"`). | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2015-08-27 16:29:02 -07:00
										 |  |  |   Style | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * A visitor for {@link TemplateAst} trees that will process each node. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | export interface TemplateAstVisitor { | 
					
						
							| 
									
										
										
										
											2015-09-01 16:24:23 -07:00
										 |  |  |   visitNgContent(ast: NgContentAst, context: any): any; | 
					
						
							|  |  |  |   visitEmbeddedTemplate(ast: EmbeddedTemplateAst, context: any): any; | 
					
						
							|  |  |  |   visitElement(ast: ElementAst, context: any): any; | 
					
						
							|  |  |  |   visitVariable(ast: VariableAst, context: any): any; | 
					
						
							|  |  |  |   visitEvent(ast: BoundEventAst, context: any): any; | 
					
						
							|  |  |  |   visitElementProperty(ast: BoundElementPropertyAst, context: any): any; | 
					
						
							|  |  |  |   visitAttr(ast: AttrAst, context: any): any; | 
					
						
							|  |  |  |   visitBoundText(ast: BoundTextAst, context: any): any; | 
					
						
							|  |  |  |   visitText(ast: TextAst, context: any): any; | 
					
						
							|  |  |  |   visitDirective(ast: DirectiveAst, context: any): any; | 
					
						
							|  |  |  |   visitDirectiveProperty(ast: BoundDirectivePropertyAst, context: any): any; | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Visit every node in a list of {@link TemplateAst}s with the given {@link TemplateAstVisitor}. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-04-07 17:17:50 -07:00
										 |  |  | export function templateVisitAll( | 
					
						
							|  |  |  |     visitor: TemplateAstVisitor, asts: TemplateAst[], context: any = null): any[] { | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  |   var result = []; | 
					
						
							|  |  |  |   asts.forEach(ast => { | 
					
						
							| 
									
										
										
										
											2015-09-01 16:24:23 -07:00
										 |  |  |     var astResult = ast.visit(visitor, context); | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  |     if (isPresent(astResult)) { | 
					
						
							|  |  |  |       result.push(astResult); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } |