Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 8ac9035

Browse files
fix($parse): properly handle dots at the end of identifiers
Fixes #4613 Fixes #4912 Closes #8559
1 parent 525a8f8 commit 8ac9035

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/ng/parse.js

+10
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,16 @@ Lexer.prototype = {
276276
this.index++;
277277
}
278278

279+
//check if the identifier ends with . and if so move back one char
280+
if (lastDot && ident[ident.length - 1] === '.') {
281+
this.index--;
282+
ident = ident.slice(0, -1);
283+
lastDot = ident.lastIndexOf('.');
284+
if (lastDot === -1) {
285+
lastDot = undefined;
286+
}
287+
}
288+
279289
//check if this is not a method invocation and if it is back out to last dot
280290
if (lastDot) {
281291
peekIndex = this.index;

test/ng/parseSpec.js

+29
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ describe('parser', function() {
7171
expect(tokens[i].string).toEqual('d"e');
7272
});
7373

74+
it('should tokenize identifiers with spaces after dots', function () {
75+
var tokens = lex('foo. bar');
76+
expect(tokens[0].text).toEqual('foo');
77+
expect(tokens[1].text).toEqual('.');
78+
expect(tokens[2].text).toEqual('bar');
79+
});
80+
7481
it('should tokenize undefined', function() {
7582
var tokens = lex("undefined");
7683
var i = 0;
@@ -349,6 +356,28 @@ describe('parser', function() {
349356
expect(scope.$eval("x.y.z", scope)).not.toBeDefined();
350357
});
351358

359+
it('should handle white-spaces around dots in paths', function () {
360+
scope.a = {b: 4};
361+
expect(scope.$eval("a . b", scope)).toEqual(4);
362+
expect(scope.$eval("a. b", scope)).toEqual(4);
363+
expect(scope.$eval("a .b", scope)).toEqual(4);
364+
expect(scope.$eval("a . \nb", scope)).toEqual(4);
365+
});
366+
367+
it('should throw syntax error exception for identifiers ending with a dot', function () {
368+
scope.a = {b: 4};
369+
370+
expect(function() {
371+
scope.$eval("a.", scope);
372+
}).toThrowMinErr('$parse', 'syntax',
373+
"Token 'null' is an unexpected token at column 2 of the expression [a.] starting at [.].");
374+
375+
expect(function() {
376+
scope.$eval("a .", scope);
377+
}).toThrowMinErr('$parse', 'syntax',
378+
"Token 'null' is an unexpected token at column 3 of the expression [a .] starting at [.].");
379+
});
380+
352381
it('should resolve deeply nested paths (important for CSP mode)', function() {
353382
scope.a = {b: {c: {d: {e: {f: {g: {h: {i: {j: {k: {l: {m: {n: 'nooo!'}}}}}}}}}}}}};
354383
expect(scope.$eval("a.b.c.d.e.f.g.h.i.j.k.l.m.n", scope)).toBe('nooo!');

0 commit comments

Comments
 (0)