2016-06-23 09:47:54 -07:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-07-21 11:41:25 -07:00
|
|
|
import {isPresent} from '../facade/lang';
|
2015-08-25 15:36:02 -07:00
|
|
|
|
2016-07-21 11:41:25 -07:00
|
|
|
import {ParseSourceSpan} from '../parse_util';
|
2015-10-07 09:34:21 -07:00
|
|
|
|
2015-08-25 15:36:02 -07:00
|
|
|
export interface HtmlAst {
|
2015-10-07 09:34:21 -07:00
|
|
|
sourceSpan: ParseSourceSpan;
|
2015-09-11 13:35:46 -07:00
|
|
|
visit(visitor: HtmlAstVisitor, context: any): any;
|
2015-08-25 15:36:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export class HtmlTextAst implements HtmlAst {
|
2015-10-07 09:34:21 -07:00
|
|
|
constructor(public value: string, public sourceSpan: ParseSourceSpan) {}
|
2015-09-11 13:35:46 -07:00
|
|
|
visit(visitor: HtmlAstVisitor, context: any): any { return visitor.visitText(this, context); }
|
2015-08-25 15:36:02 -07:00
|
|
|
}
|
|
|
|
|
2016-04-12 11:46:49 -07:00
|
|
|
export class HtmlExpansionAst implements HtmlAst {
|
2016-06-08 16:38:52 -07:00
|
|
|
constructor(
|
|
|
|
public switchValue: string, public type: string, public cases: HtmlExpansionCaseAst[],
|
|
|
|
public sourceSpan: ParseSourceSpan, public switchValueSourceSpan: ParseSourceSpan) {}
|
2016-04-12 11:46:49 -07:00
|
|
|
visit(visitor: HtmlAstVisitor, context: any): any {
|
|
|
|
return visitor.visitExpansion(this, context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class HtmlExpansionCaseAst implements HtmlAst {
|
2016-06-08 16:38:52 -07:00
|
|
|
constructor(
|
|
|
|
public value: string, public expression: HtmlAst[], public sourceSpan: ParseSourceSpan,
|
|
|
|
public valueSourceSpan: ParseSourceSpan, public expSourceSpan: ParseSourceSpan) {}
|
2016-04-12 11:46:49 -07:00
|
|
|
|
|
|
|
visit(visitor: HtmlAstVisitor, context: any): any {
|
|
|
|
return visitor.visitExpansionCase(this, context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-25 15:36:02 -07:00
|
|
|
export class HtmlAttrAst implements HtmlAst {
|
2015-10-07 09:34:21 -07:00
|
|
|
constructor(public name: string, public value: string, public sourceSpan: ParseSourceSpan) {}
|
2015-09-11 13:35:46 -07:00
|
|
|
visit(visitor: HtmlAstVisitor, context: any): any { return visitor.visitAttr(this, context); }
|
2015-08-25 15:36:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export class HtmlElementAst implements HtmlAst {
|
2016-06-08 16:38:52 -07:00
|
|
|
constructor(
|
|
|
|
public name: string, public attrs: HtmlAttrAst[], public children: HtmlAst[],
|
|
|
|
public sourceSpan: ParseSourceSpan, public startSourceSpan: ParseSourceSpan,
|
|
|
|
public endSourceSpan: ParseSourceSpan) {}
|
2015-09-11 13:35:46 -07:00
|
|
|
visit(visitor: HtmlAstVisitor, context: any): any { return visitor.visitElement(this, context); }
|
2015-08-25 15:36:02 -07:00
|
|
|
}
|
|
|
|
|
2016-03-06 20:21:20 -08:00
|
|
|
export class HtmlCommentAst implements HtmlAst {
|
|
|
|
constructor(public value: string, public sourceSpan: ParseSourceSpan) {}
|
|
|
|
visit(visitor: HtmlAstVisitor, context: any): any { return visitor.visitComment(this, context); }
|
|
|
|
}
|
|
|
|
|
2015-08-25 15:36:02 -07:00
|
|
|
export interface HtmlAstVisitor {
|
2015-09-11 13:35:46 -07:00
|
|
|
visitElement(ast: HtmlElementAst, context: any): any;
|
|
|
|
visitAttr(ast: HtmlAttrAst, context: any): any;
|
|
|
|
visitText(ast: HtmlTextAst, context: any): any;
|
2016-03-06 20:21:20 -08:00
|
|
|
visitComment(ast: HtmlCommentAst, context: any): any;
|
2016-04-12 11:46:49 -07:00
|
|
|
visitExpansion(ast: HtmlExpansionAst, context: any): any;
|
|
|
|
visitExpansionCase(ast: HtmlExpansionCaseAst, context: any): any;
|
2015-08-25 15:36:02 -07:00
|
|
|
}
|
|
|
|
|
2015-09-11 13:35:46 -07:00
|
|
|
export function htmlVisitAll(visitor: HtmlAstVisitor, asts: HtmlAst[], context: any = null): any[] {
|
2016-06-11 21:23:37 -07:00
|
|
|
var result: any[] = [];
|
2015-08-25 15:36:02 -07:00
|
|
|
asts.forEach(ast => {
|
2015-09-11 13:35:46 -07:00
|
|
|
var astResult = ast.visit(visitor, context);
|
2015-08-25 15:36:02 -07:00
|
|
|
if (isPresent(astResult)) {
|
|
|
|
result.push(astResult);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|