feat(i18n): implement an i18n-aware html parser

Closes #7738
This commit is contained in:
vsavkin
2016-03-23 13:45:04 -07:00
committed by Victor Savkin
parent 73a84a7098
commit d272f96e23
3 changed files with 577 additions and 4 deletions

View File

@ -57,6 +57,10 @@ class ParseException extends BaseException {
}
}
export class SplitInterpolation {
constructor(public strings: string[], public expressions: string[]) {}
}
@Injectable()
export class Parser {
/** @internal */
@ -118,6 +122,21 @@ export class Parser {
}
parseInterpolation(input: string, location: any): ASTWithSource {
let split = this.splitInterpolation(input, location);
if (split == null) return null;
let expressions = [];
for (let i = 0; i < split.expressions.length; ++i) {
var tokens = this._lexer.tokenize(split.expressions[i]);
var ast = new _ParseAST(input, location, tokens, this._reflector, false).parseChain();
expressions.push(ast);
}
return new ASTWithSource(new Interpolation(split.strings, expressions), input, location);
}
splitInterpolation(input: string, location: string): SplitInterpolation {
var parts = StringWrapper.split(input, INTERPOLATION_REGEXP);
if (parts.length <= 1) {
return null;
@ -131,16 +150,14 @@ export class Parser {
// fixed string
strings.push(part);
} else if (part.trim().length > 0) {
var tokens = this._lexer.tokenize(part);
var ast = new _ParseAST(input, location, tokens, this._reflector, false).parseChain();
expressions.push(ast);
expressions.push(part);
} else {
throw new ParseException('Blank expressions are not allowed in interpolated strings', input,
`at column ${this._findInterpolationErrorColumn(parts, i)} in`,
location);
}
}
return new ASTWithSource(new Interpolation(strings, expressions), input, location);
return new SplitInterpolation(strings, expressions);
}
wrapLiteralPrimitive(input: string, location: any): ASTWithSource {