mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-20 12:21:52 +00:00
feat($parse): allow strict equality in angular expressions
Allows the parser to parse strict equality and inequality in angular expressions. Closes #908
This commit is contained in:
parent
610a5a0c14
commit
a179a9a96e
2 changed files with 26 additions and 8 deletions
|
|
@ -20,6 +20,8 @@ var OPERATORS = {
|
||||||
'%':function(self, locals, a,b){return a(self, locals)%b(self, locals);},
|
'%':function(self, locals, a,b){return a(self, locals)%b(self, locals);},
|
||||||
'^':function(self, locals, a,b){return a(self, locals)^b(self, locals);},
|
'^':function(self, locals, a,b){return a(self, locals)^b(self, locals);},
|
||||||
'=':noop,
|
'=':noop,
|
||||||
|
'===':function(self, locals, a, b){return a(self, locals)===b(self, locals);},
|
||||||
|
'!==':function(self, locals, a, b){return a(self, locals)!==b(self, locals);},
|
||||||
'==':function(self, locals, a,b){return a(self, locals)==b(self, locals);},
|
'==':function(self, locals, a,b){return a(self, locals)==b(self, locals);},
|
||||||
'!=':function(self, locals, a,b){return a(self, locals)!=b(self, locals);},
|
'!=':function(self, locals, a,b){return a(self, locals)!=b(self, locals);},
|
||||||
'<':function(self, locals, a,b){return a(self, locals)<b(self, locals);},
|
'<':function(self, locals, a,b){return a(self, locals)<b(self, locals);},
|
||||||
|
|
@ -70,9 +72,14 @@ function lex(text, csp){
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
var ch2 = ch + peek(),
|
var ch2 = ch + peek(),
|
||||||
|
ch3 = ch2 + peek(2),
|
||||||
fn = OPERATORS[ch],
|
fn = OPERATORS[ch],
|
||||||
fn2 = OPERATORS[ch2];
|
fn2 = OPERATORS[ch2],
|
||||||
if (fn2) {
|
fn3 = OPERATORS[ch3];
|
||||||
|
if (fn3) {
|
||||||
|
tokens.push({index:index, text:ch3, fn:fn3});
|
||||||
|
index += 3;
|
||||||
|
} else if (fn2) {
|
||||||
tokens.push({index:index, text:ch2, fn:fn2});
|
tokens.push({index:index, text:ch2, fn:fn2});
|
||||||
index += 2;
|
index += 2;
|
||||||
} else if (fn) {
|
} else if (fn) {
|
||||||
|
|
@ -94,8 +101,9 @@ function lex(text, csp){
|
||||||
return chars.indexOf(lastCh) != -1;
|
return chars.indexOf(lastCh) != -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
function peek() {
|
function peek(i) {
|
||||||
return index + 1 < text.length ? text.charAt(index + 1) : false;
|
var num = i || 1;
|
||||||
|
return index + num < text.length ? text.charAt(index + num) : false;
|
||||||
}
|
}
|
||||||
function isNumber(ch) {
|
function isNumber(ch) {
|
||||||
return '0' <= ch && ch <= '9';
|
return '0' <= ch && ch <= '9';
|
||||||
|
|
@ -456,7 +464,7 @@ function parser(text, json, $filter, csp){
|
||||||
function equality() {
|
function equality() {
|
||||||
var left = relational();
|
var left = relational();
|
||||||
var token;
|
var token;
|
||||||
if ((token = expect('==','!='))) {
|
if ((token = expect('==','!=','===','!=='))) {
|
||||||
left = binaryFn(left, token.fn, equality());
|
left = binaryFn(left, token.fn, equality());
|
||||||
}
|
}
|
||||||
return left;
|
return left;
|
||||||
|
|
|
||||||
|
|
@ -91,8 +91,8 @@ describe('parser', function() {
|
||||||
expect(tokens[1].text).toEqual('b');
|
expect(tokens[1].text).toEqual('b');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should tokenize relation', function() {
|
it('should tokenize relation and equality', function() {
|
||||||
var tokens = lex("! == != < > <= >=");
|
var tokens = lex("! == != < > <= >= === !==");
|
||||||
expect(tokens[0].text).toEqual('!');
|
expect(tokens[0].text).toEqual('!');
|
||||||
expect(tokens[1].text).toEqual('==');
|
expect(tokens[1].text).toEqual('==');
|
||||||
expect(tokens[2].text).toEqual('!=');
|
expect(tokens[2].text).toEqual('!=');
|
||||||
|
|
@ -100,6 +100,8 @@ describe('parser', function() {
|
||||||
expect(tokens[4].text).toEqual('>');
|
expect(tokens[4].text).toEqual('>');
|
||||||
expect(tokens[5].text).toEqual('<=');
|
expect(tokens[5].text).toEqual('<=');
|
||||||
expect(tokens[6].text).toEqual('>=');
|
expect(tokens[6].text).toEqual('>=');
|
||||||
|
expect(tokens[7].text).toEqual('===');
|
||||||
|
expect(tokens[8].text).toEqual('!==');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should tokenize statements', function() {
|
it('should tokenize statements', function() {
|
||||||
|
|
@ -197,12 +199,20 @@ describe('parser', function() {
|
||||||
expect(scope.$eval("false")).toBeFalsy();
|
expect(scope.$eval("false")).toBeFalsy();
|
||||||
expect(scope.$eval("!true")).toBeFalsy();
|
expect(scope.$eval("!true")).toBeFalsy();
|
||||||
expect(scope.$eval("1==1")).toBeTruthy();
|
expect(scope.$eval("1==1")).toBeTruthy();
|
||||||
|
expect(scope.$eval("1==true")).toBeTruthy();
|
||||||
|
expect(scope.$eval("1===1")).toBeTruthy();
|
||||||
|
expect(scope.$eval("1==='1'")).toBeFalsy();
|
||||||
|
expect(scope.$eval("1===true")).toBeFalsy();
|
||||||
|
expect(scope.$eval("'true'===true")).toBeFalsy();
|
||||||
|
expect(scope.$eval("1!==2")).toBeTruthy();
|
||||||
|
expect(scope.$eval("1!=='1'")).toBeTruthy();
|
||||||
expect(scope.$eval("1!=2")).toBeTruthy();
|
expect(scope.$eval("1!=2")).toBeTruthy();
|
||||||
expect(scope.$eval("1<2")).toBeTruthy();
|
expect(scope.$eval("1<2")).toBeTruthy();
|
||||||
expect(scope.$eval("1<=1")).toBeTruthy();
|
expect(scope.$eval("1<=1")).toBeTruthy();
|
||||||
expect(scope.$eval("1>2")).toEqual(1>2);
|
expect(scope.$eval("1>2")).toEqual(1>2);
|
||||||
expect(scope.$eval("2>=1")).toEqual(2>=1);
|
expect(scope.$eval("2>=1")).toEqual(2>=1);
|
||||||
expect(scope.$eval("true==2<3")).toEqual(true === 2<3);
|
expect(scope.$eval("true==2<3")).toEqual(true == 2<3);
|
||||||
|
expect(scope.$eval("true===2<3")).toEqual(true === 2<3);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should parse logical', function() {
|
it('should parse logical', function() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue