mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-17 12:15:56 +08:00
feat($parse): support trailing commas in object & array literals
Per ECMAScript 5.1 specification trailing commas are allowed in object and array literals. All modern browsers as well as IE>8 support this syntax. This commit adds support for such syntax to Angular expressions.
This commit is contained in:
@@ -785,6 +785,10 @@ Parser.prototype = {
|
||||
var allConstant = true;
|
||||
if (this.peekToken().text !== ']') {
|
||||
do {
|
||||
if (this.peek(']')) {
|
||||
// Support trailing commas per ES5.1.
|
||||
break;
|
||||
}
|
||||
var elementFn = this.expression();
|
||||
elementFns.push(elementFn);
|
||||
if (!elementFn.constant) {
|
||||
@@ -811,6 +815,10 @@ Parser.prototype = {
|
||||
var allConstant = true;
|
||||
if (this.peekToken().text !== '}') {
|
||||
do {
|
||||
if (this.peek('}')) {
|
||||
// Support trailing commas per ES5.1.
|
||||
break;
|
||||
}
|
||||
var token = this.expect(),
|
||||
key = token.string || token.text;
|
||||
this.consume(':');
|
||||
|
||||
@@ -460,6 +460,8 @@ describe('parser', function() {
|
||||
expect(scope.$eval("[1, 2]").length).toEqual(2);
|
||||
expect(scope.$eval("[1, 2]")[0]).toEqual(1);
|
||||
expect(scope.$eval("[1, 2]")[1]).toEqual(2);
|
||||
expect(scope.$eval("[1, 2,]")[1]).toEqual(2);
|
||||
expect(scope.$eval("[1, 2,]").length).toEqual(2);
|
||||
});
|
||||
|
||||
it('should evaluate array access', function() {
|
||||
@@ -474,6 +476,9 @@ describe('parser', function() {
|
||||
expect(toJson(scope.$eval("{a:'b'}"))).toEqual('{"a":"b"}');
|
||||
expect(toJson(scope.$eval("{'a':'b'}"))).toEqual('{"a":"b"}');
|
||||
expect(toJson(scope.$eval("{\"a\":'b'}"))).toEqual('{"a":"b"}');
|
||||
expect(toJson(scope.$eval("{a:'b',}"))).toEqual('{"a":"b"}');
|
||||
expect(toJson(scope.$eval("{'a':'b',}"))).toEqual('{"a":"b"}');
|
||||
expect(toJson(scope.$eval("{\"a\":'b',}"))).toEqual('{"a":"b"}');
|
||||
});
|
||||
|
||||
it('should evaluate object access', function() {
|
||||
|
||||
Reference in New Issue
Block a user