X Tutup
Skip to content

Commit b90de66

Browse files
committed
fix(parser): do not crash on untokenizable quote prefixes
Closes #5486
1 parent 1bec4f6 commit b90de66

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

modules/angular2/src/core/change_detection/parser/lexer.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,18 @@ function isIdentifierStart(code: number): boolean {
395395
return ($a <= code && code <= $z) || ($A <= code && code <= $Z) || (code == $_) || (code == $$);
396396
}
397397

398+
export function isIdentifier(input: string): boolean {
399+
if (input.length == 0) return false;
400+
var scanner = new _Scanner(input);
401+
if (!isIdentifierStart(scanner.peek)) return false;
402+
scanner.advance();
403+
while (scanner.peek !== $EOF) {
404+
if (!isIdentifierPart(scanner.peek)) return false;
405+
scanner.advance();
406+
}
407+
return true;
408+
}
409+
398410
function isIdentifierPart(code: number): boolean {
399411
return ($a <= code && code <= $z) || ($A <= code && code <= $Z) || ($0 <= code && code <= $9) ||
400412
(code == $_) || (code == $$);

modules/angular2/src/core/change_detection/parser/parser.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {ListWrapper} from 'angular2/src/facade/collection';
55
import {
66
Lexer,
77
EOF,
8+
isIdentifier,
89
Token,
910
$PERIOD,
1011
$COLON,
@@ -105,15 +106,10 @@ export class Parser {
105106
if (isBlank(input)) return null;
106107
var prefixSeparatorIndex = input.indexOf(':');
107108
if (prefixSeparatorIndex == -1) return null;
108-
var prefix = input.substring(0, prefixSeparatorIndex);
109+
var prefix = input.substring(0, prefixSeparatorIndex).trim();
110+
if (!isIdentifier(prefix)) return null;
109111
var uninterpretedExpression = input.substring(prefixSeparatorIndex + 1);
110-
111-
// while we do not interpret the expression, we do interpret the prefix
112-
var prefixTokens = this._lexer.tokenize(prefix);
113-
114-
// quote prefix must be a single legal identifier
115-
if (prefixTokens.length != 1 || !prefixTokens[0].isIdentifier()) return null;
116-
return new Quote(prefixTokens[0].strValue, uninterpretedExpression, location);
112+
return new Quote(prefix, uninterpretedExpression, location);
117113
}
118114

119115
parseTemplateBindings(input: string, location: any): TemplateBinding[] {

modules/angular2/test/core/change_detection/parser/parser_spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ export function main() {
233233

234234
it('should parse quoted expressions', () => { checkBinding('a:b', 'a:b'); });
235235

236+
it('should not crash when prefix part is not tokenizable',
237+
() => { checkBinding('"a:b"', '"a:b"'); });
238+
236239
it('should ignore whitespace around quote prefix', () => { checkBinding(' a :b', 'a:b'); });
237240

238241
it('should refuse prefixes that are not single identifiers', () => {

0 commit comments

Comments
 (0)
X Tutup