@@ -463,8 +463,10 @@ AST.prototype = {
463
463
primary = this . arrayDeclaration ( ) ;
464
464
} else if ( this . expect ( '{' ) ) {
465
465
primary = this . object ( ) ;
466
- } else if ( this . constants . hasOwnProperty ( this . peek ( ) . text ) ) {
467
- primary = copy ( this . constants [ this . consume ( ) . text ] ) ;
466
+ } else if ( this . selfReferential . hasOwnProperty ( this . peek ( ) . text ) ) {
467
+ primary = copy ( this . selfReferential [ this . consume ( ) . text ] ) ;
468
+ } else if ( this . options . literals . hasOwnProperty ( this . peek ( ) . text ) ) {
469
+ primary = { type : AST . Literal , value : this . options . literals [ this . consume ( ) . text ] } ;
468
470
} else if ( this . peek ( ) . identifier ) {
469
471
primary = this . identifier ( ) ;
470
472
} else if ( this . peek ( ) . constant ) {
@@ -616,15 +618,7 @@ AST.prototype = {
616
618
return false ;
617
619
} ,
618
620
619
-
620
- /* `undefined` is not a constant, it is an identifier,
621
- * but using it as an identifier is not supported
622
- */
623
- constants : {
624
- 'true' : { type : AST . Literal , value : true } ,
625
- 'false' : { type : AST . Literal , value : false } ,
626
- 'null' : { type : AST . Literal , value : null } ,
627
- 'undefined' : { type : AST . Literal , value : undefined } ,
621
+ selfReferential : {
628
622
'this' : { type : AST . ThisExpression } ,
629
623
'$locals' : { type : AST . LocalsExpression }
630
624
}
@@ -1669,7 +1663,7 @@ var Parser = function(lexer, $filter, options) {
1669
1663
this . lexer = lexer ;
1670
1664
this . $filter = $filter ;
1671
1665
this . options = options ;
1672
- this . ast = new AST ( this . lexer ) ;
1666
+ this . ast = new AST ( lexer , options ) ;
1673
1667
this . astCompiler = options . csp ? new ASTInterpreter ( this . ast , $filter ) :
1674
1668
new ASTCompiler ( this . ast , $filter ) ;
1675
1669
} ;
@@ -1746,16 +1740,39 @@ function getValueOf(value) {
1746
1740
function $ParseProvider ( ) {
1747
1741
var cacheDefault = createMap ( ) ;
1748
1742
var cacheExpensive = createMap ( ) ;
1743
+ var literals = {
1744
+ 'true' : true ,
1745
+ 'false' : false ,
1746
+ 'null' : null ,
1747
+ 'undefined' : undefined
1748
+ } ;
1749
+
1750
+ /**
1751
+ * @ngdoc method
1752
+ * @name $parseProvider#addLiteral
1753
+ * @description
1754
+ *
1755
+ * Configure $parse service to add literal values that will be present as literal at expressions.
1756
+ *
1757
+ * @param {string } literalName Token for the literal value. The literal name value must be a valid literal name.
1758
+ * @param {* } literalValue Value for this literal. All literal values must be primitives or `undefined`.
1759
+ *
1760
+ **/
1761
+ this . addLiteral = function ( literalName , literalValue ) {
1762
+ literals [ literalName ] = literalValue ;
1763
+ } ;
1749
1764
1750
1765
this . $get = [ '$filter' , function ( $filter ) {
1751
1766
var noUnsafeEval = csp ( ) . noUnsafeEval ;
1752
1767
var $parseOptions = {
1753
1768
csp : noUnsafeEval ,
1754
- expensiveChecks : false
1769
+ expensiveChecks : false ,
1770
+ literals : copy ( literals )
1755
1771
} ,
1756
1772
$parseOptionsExpensive = {
1757
1773
csp : noUnsafeEval ,
1758
- expensiveChecks : true
1774
+ expensiveChecks : true ,
1775
+ literals : copy ( literals )
1759
1776
} ;
1760
1777
var runningChecksEnabled = false ;
1761
1778
0 commit comments