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

Commit a179a9a

Browse files
jtymesmhevery
authored andcommitted
feat($parse): allow strict equality in angular expressions
Allows the parser to parse strict equality and inequality in angular expressions. Closes #908
1 parent 610a5a0 commit a179a9a

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/ng/parse.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ var OPERATORS = {
2020
'%':function(self, locals, a,b){return a(self, locals)%b(self, locals);},
2121
'^':function(self, locals, a,b){return a(self, locals)^b(self, locals);},
2222
'=':noop,
23+
'===':function(self, locals, a, b){return a(self, locals)===b(self, locals);},
24+
'!==':function(self, locals, a, b){return a(self, locals)!==b(self, locals);},
2325
'==':function(self, locals, a,b){return a(self, locals)==b(self, locals);},
2426
'!=':function(self, locals, a,b){return a(self, locals)!=b(self, locals);},
2527
'<':function(self, locals, a,b){return a(self, locals)<b(self, locals);},
@@ -70,9 +72,14 @@ function lex(text, csp){
7072
continue;
7173
} else {
7274
var ch2 = ch + peek(),
75+
ch3 = ch2 + peek(2),
7376
fn = OPERATORS[ch],
74-
fn2 = OPERATORS[ch2];
75-
if (fn2) {
77+
fn2 = OPERATORS[ch2],
78+
fn3 = OPERATORS[ch3];
79+
if (fn3) {
80+
tokens.push({index:index, text:ch3, fn:fn3});
81+
index += 3;
82+
} else if (fn2) {
7683
tokens.push({index:index, text:ch2, fn:fn2});
7784
index += 2;
7885
} else if (fn) {
@@ -94,8 +101,9 @@ function lex(text, csp){
94101
return chars.indexOf(lastCh) != -1;
95102
}
96103

97-
function peek() {
98-
return index + 1 < text.length ? text.charAt(index + 1) : false;
104+
function peek(i) {
105+
var num = i || 1;
106+
return index + num < text.length ? text.charAt(index + num) : false;
99107
}
100108
function isNumber(ch) {
101109
return '0' <= ch && ch <= '9';
@@ -456,7 +464,7 @@ function parser(text, json, $filter, csp){
456464
function equality() {
457465
var left = relational();
458466
var token;
459-
if ((token = expect('==','!='))) {
467+
if ((token = expect('==','!=','===','!=='))) {
460468
left = binaryFn(left, token.fn, equality());
461469
}
462470
return left;

test/ng/parseSpec.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,17 @@ describe('parser', function() {
9191
expect(tokens[1].text).toEqual('b');
9292
});
9393

94-
it('should tokenize relation', function() {
95-
var tokens = lex("! == != < > <= >=");
94+
it('should tokenize relation and equality', function() {
95+
var tokens = lex("! == != < > <= >= === !==");
9696
expect(tokens[0].text).toEqual('!');
9797
expect(tokens[1].text).toEqual('==');
9898
expect(tokens[2].text).toEqual('!=');
9999
expect(tokens[3].text).toEqual('<');
100100
expect(tokens[4].text).toEqual('>');
101101
expect(tokens[5].text).toEqual('<=');
102102
expect(tokens[6].text).toEqual('>=');
103+
expect(tokens[7].text).toEqual('===');
104+
expect(tokens[8].text).toEqual('!==');
103105
});
104106

105107
it('should tokenize statements', function() {
@@ -197,12 +199,20 @@ describe('parser', function() {
197199
expect(scope.$eval("false")).toBeFalsy();
198200
expect(scope.$eval("!true")).toBeFalsy();
199201
expect(scope.$eval("1==1")).toBeTruthy();
202+
expect(scope.$eval("1==true")).toBeTruthy();
203+
expect(scope.$eval("1===1")).toBeTruthy();
204+
expect(scope.$eval("1==='1'")).toBeFalsy();
205+
expect(scope.$eval("1===true")).toBeFalsy();
206+
expect(scope.$eval("'true'===true")).toBeFalsy();
207+
expect(scope.$eval("1!==2")).toBeTruthy();
208+
expect(scope.$eval("1!=='1'")).toBeTruthy();
200209
expect(scope.$eval("1!=2")).toBeTruthy();
201210
expect(scope.$eval("1<2")).toBeTruthy();
202211
expect(scope.$eval("1<=1")).toBeTruthy();
203212
expect(scope.$eval("1>2")).toEqual(1>2);
204213
expect(scope.$eval("2>=1")).toEqual(2>=1);
205-
expect(scope.$eval("true==2<3")).toEqual(true === 2<3);
214+
expect(scope.$eval("true==2<3")).toEqual(true == 2<3);
215+
expect(scope.$eval("true===2<3")).toEqual(true === 2<3);
206216
});
207217

208218
it('should parse logical', function() {

0 commit comments

Comments
 (0)