2015-11-06 17:34:07 -08:00
|
|
|
import {isPresent} from 'angular2/src/facade/lang';
|
2015-08-25 15:36:02 -07:00
|
|
|
|
|
|
|
export interface HtmlAst {
|
2015-11-16 14:37:00 -08:00
|
|
|
sourceInfo: string;
|
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-11-16 14:37:00 -08:00
|
|
|
constructor(public value: string, public sourceInfo: string) {}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
export class HtmlAttrAst implements HtmlAst {
|
2015-11-16 14:37:00 -08:00
|
|
|
constructor(public name: string, public value: string, public sourceInfo: string) {}
|
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 {
|
|
|
|
constructor(public name: string, public attrs: HtmlAttrAst[], public children: HtmlAst[],
|
2015-11-16 14:37:00 -08:00
|
|
|
public sourceInfo: string) {}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
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[] {
|
2015-08-25 15:36:02 -07:00
|
|
|
var result = [];
|
|
|
|
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;
|
|
|
|
}
|