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

feat($parse): Allow user-defined literals #14194

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,10 @@ AST.prototype = {
primary = this.arrayDeclaration();
} else if (this.expect('{')) {
primary = this.object();
} else if (this.constants.hasOwnProperty(this.peek().text)) {
primary = copy(this.constants[this.consume().text]);
} else if (this.selfReferential.hasOwnProperty(this.peek().text)) {
primary = copy(this.selfReferential[this.consume().text]);
} else if (this.options.literals.hasOwnProperty(this.peek().text)) {
primary = { type: AST.Literal, value: this.options.literals[this.consume().text]};
} else if (this.peek().identifier) {
primary = this.identifier();
} else if (this.peek().constant) {
Expand Down Expand Up @@ -616,15 +618,7 @@ AST.prototype = {
return false;
},


/* `undefined` is not a constant, it is an identifier,
* but using it as an identifier is not supported
*/
constants: {
'true': { type: AST.Literal, value: true },
'false': { type: AST.Literal, value: false },
'null': { type: AST.Literal, value: null },
'undefined': {type: AST.Literal, value: undefined },
selfReferential: {
'this': {type: AST.ThisExpression },
'$locals': {type: AST.LocalsExpression }
}
Expand Down Expand Up @@ -1669,7 +1663,7 @@ var Parser = function(lexer, $filter, options) {
this.lexer = lexer;
this.$filter = $filter;
this.options = options;
this.ast = new AST(this.lexer);
this.ast = new AST(lexer, options);
this.astCompiler = options.csp ? new ASTInterpreter(this.ast, $filter) :
new ASTCompiler(this.ast, $filter);
};
Expand Down Expand Up @@ -1746,16 +1740,39 @@ function getValueOf(value) {
function $ParseProvider() {
var cacheDefault = createMap();
var cacheExpensive = createMap();
var literals = {
'true': true,
'false': false,
'null': null,
'undefined': undefined
};

/**
* @ngdoc method
* @name $parseProvider#addLiteral
* @description
*
* Configure $parse service to add literal values that will be present as literal at expressions.
*
* @param {string} literalName Token for the literal value. The literal name value must be a valid literal name.
* @param {*} literalValue Value for this literal. All literal values must be primitives or `undefined`.
*
**/
this.addLiteral = function(literalName, literalValue) {
literals[literalName] = literalValue;
};

this.$get = ['$filter', function($filter) {
var noUnsafeEval = csp().noUnsafeEval;
var $parseOptions = {
csp: noUnsafeEval,
expensiveChecks: false
expensiveChecks: false,
literals: copy(literals)
},
$parseOptionsExpensive = {
csp: noUnsafeEval,
expensiveChecks: true
expensiveChecks: true,
literals: copy(literals)
};
var runningChecksEnabled = false;

Expand Down
15 changes: 14 additions & 1 deletion test/ng/parseSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ describe('parser', function() {
/* global AST: false */
createAst = function() {
var lexer = new Lexer({csp: false});
var ast = new AST(lexer, {csp: false});
var ast = new AST(lexer, {csp: false, literals: {'true': true, 'false': false, 'undefined': undefined, 'null': null}});
return ast.ast.apply(ast, arguments);
};
});
Expand Down Expand Up @@ -1681,6 +1681,19 @@ describe('parser', function() {
$filterProvider = filterProvider;
}]));

forEach([true, false], function(cspEnabled) {
beforeEach(module(['$parseProvider', function(parseProvider) {
parseProvider.addLiteral('Infinity', Infinity);
}]));

it('should allow extending literals with csp ' + cspEnabled, inject(function($rootScope) {
expect($rootScope.$eval("Infinity")).toEqual(Infinity);
expect($rootScope.$eval("-Infinity")).toEqual(-Infinity);
expect(function() {$rootScope.$eval("Infinity = 1");}).toThrow();
expect($rootScope.$eval("Infinity")).toEqual(Infinity);
}));
});

forEach([true, false], function(cspEnabled) {
describe('csp: ' + cspEnabled, function() {

Expand Down