feat(parser): allows users install custom AST transformers

Closes #5382
This commit is contained in:
Yegor Jbanov
2015-11-19 10:51:16 -08:00
committed by Yegor
parent 125fa3885e
commit a43ed79ee7
4 changed files with 80 additions and 26 deletions
@@ -13,7 +13,8 @@ import {
assertionsEnabled,
isBlank
} from 'angular2/src/facade/lang';
import {Injectable} from 'angular2/src/core/di';
import {Injectable, Inject, Injector, OpaqueToken, Optional} from 'angular2/core';
import {CONST_EXPR} from 'angular2/src/facade/lang';
import {BaseException} from 'angular2/src/facade/exceptions';
import {Parser, AST, ASTWithSource} from 'angular2/src/core/change_detection/change_detection';
import {TemplateBinding} from 'angular2/src/core/change_detection/parser/ast';
@@ -28,6 +29,8 @@ import {
BoundEventAst,
VariableAst,
TemplateAst,
TemplateAstVisitor,
templateVisitAll,
TextAst,
BoundTextAst,
EmbeddedTemplateAst,
@@ -78,6 +81,8 @@ const STYLE_PREFIX = 'style';
var TEXT_CSS_SELECTOR = CssSelector.parse('*')[0];
export const TEMPLATE_TRANSFORMS = CONST_EXPR(new OpaqueToken('TemplateTransforms'));
export class TemplateParseError extends ParseError {
constructor(message: string, location: ParseLocation) { super(location, message); }
}
@@ -85,7 +90,8 @@ export class TemplateParseError extends ParseError {
@Injectable()
export class TemplateParser {
constructor(private _exprParser: Parser, private _schemaRegistry: ElementSchemaRegistry,
private _htmlParser: HtmlParser) {}
private _htmlParser: HtmlParser,
@Optional() @Inject(TEMPLATE_TRANSFORMS) public transforms: TemplateAstVisitor[]) {}
parse(template: string, directives: CompileDirectiveMetadata[],
templateUrl: string): TemplateAst[] {
@@ -97,6 +103,10 @@ export class TemplateParser {
var errorString = errors.join('\n');
throw new BaseException(`Template parse errors:\n${errorString}`);
}
if (isPresent(this.transforms)) {
this.transforms.forEach(
(transform: TemplateAstVisitor) => { result = templateVisitAll(transform, result); });
}
return result;
}
}