Skip to content

Commit

Permalink
feat(i18n): implement an i18n-aware html parser
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin authored and mircoba committed Apr 10, 2016
1 parent 621afe7 commit 4542fdb
Show file tree
Hide file tree
Showing 3 changed files with 577 additions and 4 deletions.
25 changes: 21 additions & 4 deletions modules/angular2/src/core/change_detection/parser/parser.ts
Expand Up @@ -57,6 +57,10 @@ class ParseException extends BaseException {
}
}

export class SplitInterpolation {
constructor(public strings: string[], public expressions: string[]) {}
}

@Injectable()
export class Parser {
/** @internal */
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand Down

0 comments on commit 4542fdb

Please sign in to comment.