PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/test/ng/parseSpec.js

https://github.com/xsurge83/angular.js
JavaScript | 1052 lines | 1038 code | 14 blank | 0 comment | 5 complexity | b9342073b135f0d24f4dbe8cc37e799d MD5 | raw file
Possible License(s): JSON
  1. 'use strict';
  2. describe('parser', function() {
  3. describe('lexer', function() {
  4. it('should tokenize a string', function() {
  5. var tokens = lex("a.bc[22]+1.3|f:'a\\\'c':\"d\\\"e\"");
  6. var i = 0;
  7. expect(tokens[i].index).toEqual(0);
  8. expect(tokens[i].text).toEqual('a.bc');
  9. i++;
  10. expect(tokens[i].index).toEqual(4);
  11. expect(tokens[i].text).toEqual('[');
  12. i++;
  13. expect(tokens[i].index).toEqual(5);
  14. expect(tokens[i].text).toEqual(22);
  15. i++;
  16. expect(tokens[i].index).toEqual(7);
  17. expect(tokens[i].text).toEqual(']');
  18. i++;
  19. expect(tokens[i].index).toEqual(8);
  20. expect(tokens[i].text).toEqual('+');
  21. i++;
  22. expect(tokens[i].index).toEqual(9);
  23. expect(tokens[i].text).toEqual(1.3);
  24. i++;
  25. expect(tokens[i].index).toEqual(12);
  26. expect(tokens[i].text).toEqual('|');
  27. i++;
  28. expect(tokens[i].index).toEqual(13);
  29. expect(tokens[i].text).toEqual('f');
  30. i++;
  31. expect(tokens[i].index).toEqual(14);
  32. expect(tokens[i].text).toEqual(':');
  33. i++;
  34. expect(tokens[i].index).toEqual(15);
  35. expect(tokens[i].string).toEqual("a'c");
  36. i++;
  37. expect(tokens[i].index).toEqual(21);
  38. expect(tokens[i].text).toEqual(':');
  39. i++;
  40. expect(tokens[i].index).toEqual(22);
  41. expect(tokens[i].string).toEqual('d"e');
  42. });
  43. it('should tokenize undefined', function() {
  44. var tokens = lex("undefined");
  45. var i = 0;
  46. expect(tokens[i].index).toEqual(0);
  47. expect(tokens[i].text).toEqual('undefined');
  48. expect(undefined).toEqual(tokens[i].fn());
  49. });
  50. it('should tokenize quoted string', function() {
  51. var str = "['\\'', \"\\\"\"]";
  52. var tokens = lex(str);
  53. expect(tokens[1].index).toEqual(1);
  54. expect(tokens[1].string).toEqual("'");
  55. expect(tokens[3].index).toEqual(7);
  56. expect(tokens[3].string).toEqual('"');
  57. });
  58. it('should tokenize escaped quoted string', function() {
  59. var str = '"\\"\\n\\f\\r\\t\\v\\u00A0"';
  60. var tokens = lex(str);
  61. expect(tokens[0].string).toEqual('"\n\f\r\t\v\u00A0');
  62. });
  63. it('should tokenize unicode', function() {
  64. var tokens = lex('"\\u00A0"');
  65. expect(tokens.length).toEqual(1);
  66. expect(tokens[0].string).toEqual('\u00a0');
  67. });
  68. it('should ignore whitespace', function() {
  69. var tokens = lex("a \t \n \r b");
  70. expect(tokens[0].text).toEqual('a');
  71. expect(tokens[1].text).toEqual('b');
  72. });
  73. it('should tokenize relation and equality', function() {
  74. var tokens = lex("! == != < > <= >= === !==");
  75. expect(tokens[0].text).toEqual('!');
  76. expect(tokens[1].text).toEqual('==');
  77. expect(tokens[2].text).toEqual('!=');
  78. expect(tokens[3].text).toEqual('<');
  79. expect(tokens[4].text).toEqual('>');
  80. expect(tokens[5].text).toEqual('<=');
  81. expect(tokens[6].text).toEqual('>=');
  82. expect(tokens[7].text).toEqual('===');
  83. expect(tokens[8].text).toEqual('!==');
  84. });
  85. it('should tokenize logical and ternary', function() {
  86. var tokens = lex("&& || ? :");
  87. expect(tokens[0].text).toEqual('&&');
  88. expect(tokens[1].text).toEqual('||');
  89. expect(tokens[2].text).toEqual('?');
  90. expect(tokens[3].text).toEqual(':');
  91. });
  92. it('should tokenize statements', function() {
  93. var tokens = lex("a;b;");
  94. expect(tokens[0].text).toEqual('a');
  95. expect(tokens[1].text).toEqual(';');
  96. expect(tokens[2].text).toEqual('b');
  97. expect(tokens[3].text).toEqual(';');
  98. });
  99. it('should tokenize function invocation', function() {
  100. var tokens = lex("a()")
  101. expect(map(tokens, function(t) { return t.text;})).toEqual(['a', '(', ')']);
  102. });
  103. it('should tokenize method invocation', function() {
  104. var tokens = lex("a.b.c (d) - e.f()");
  105. expect(map(tokens, function(t) { return t.text;})).
  106. toEqual(['a.b', '.', 'c', '(', 'd', ')', '-', 'e', '.', 'f', '(', ')']);
  107. });
  108. it('should tokenize number', function() {
  109. var tokens = lex("0.5");
  110. expect(tokens[0].text).toEqual(0.5);
  111. });
  112. it('should tokenize negative number', inject(function($rootScope) {
  113. var value = $rootScope.$eval("-0.5");
  114. expect(value).toEqual(-0.5);
  115. value = $rootScope.$eval("{a:-0.5}");
  116. expect(value).toEqual({a:-0.5});
  117. }));
  118. it('should tokenize number with exponent', inject(function($rootScope) {
  119. var tokens = lex("0.5E-10");
  120. expect(tokens[0].text).toEqual(0.5E-10);
  121. expect($rootScope.$eval("0.5E-10")).toEqual(0.5E-10);
  122. tokens = lex("0.5E+10");
  123. expect(tokens[0].text).toEqual(0.5E+10);
  124. }));
  125. it('should throws exception for invalid exponent', function() {
  126. expect(function() {
  127. lex("0.5E-");
  128. }).toThrow(new Error('[$parse:lexerr] Lexer Error: Invalid exponent at column 4 in expression [0.5E-].'));
  129. expect(function() {
  130. lex("0.5E-A");
  131. }).toThrow(new Error('[$parse:lexerr] Lexer Error: Invalid exponent at column 4 in expression [0.5E-A].'));
  132. });
  133. it('should tokenize number starting with a dot', function() {
  134. var tokens = lex(".5");
  135. expect(tokens[0].text).toEqual(0.5);
  136. });
  137. it('should throw error on invalid unicode', function() {
  138. expect(function() {
  139. lex("'\\u1''bla'");
  140. }).toThrow(new Error("[$parse:lexerr] Lexer Error: Invalid unicode escape [\\u1''b] at column 2 in expression ['\\u1''bla']."));
  141. });
  142. });
  143. var $filterProvider, scope;
  144. beforeEach(module(['$filterProvider', function (filterProvider) {
  145. $filterProvider = filterProvider;
  146. }]));
  147. forEach([true, false], function(cspEnabled) {
  148. describe('csp ' + cspEnabled, function() {
  149. beforeEach(inject(function ($rootScope, $sniffer) {
  150. scope = $rootScope;
  151. $sniffer.csp = cspEnabled;
  152. getterFnCache = {}; // clear cache
  153. }));
  154. it('should parse expressions', function() {
  155. expect(scope.$eval("-1")).toEqual(-1);
  156. expect(scope.$eval("1 + 2.5")).toEqual(3.5);
  157. expect(scope.$eval("1 + -2.5")).toEqual(-1.5);
  158. expect(scope.$eval("1+2*3/4")).toEqual(1+2*3/4);
  159. expect(scope.$eval("0--1+1.5")).toEqual(0- -1 + 1.5);
  160. expect(scope.$eval("-0--1++2*-3/-4")).toEqual(-0- -1+ +2*-3/-4);
  161. expect(scope.$eval("1/2*3")).toEqual(1/2*3);
  162. });
  163. it('should parse comparison', function() {
  164. expect(scope.$eval("false")).toBeFalsy();
  165. expect(scope.$eval("!true")).toBeFalsy();
  166. expect(scope.$eval("1==1")).toBeTruthy();
  167. expect(scope.$eval("1==true")).toBeTruthy();
  168. expect(scope.$eval("1===1")).toBeTruthy();
  169. expect(scope.$eval("1==='1'")).toBeFalsy();
  170. expect(scope.$eval("1===true")).toBeFalsy();
  171. expect(scope.$eval("'true'===true")).toBeFalsy();
  172. expect(scope.$eval("1!==2")).toBeTruthy();
  173. expect(scope.$eval("1!=='1'")).toBeTruthy();
  174. expect(scope.$eval("1!=2")).toBeTruthy();
  175. expect(scope.$eval("1<2")).toBeTruthy();
  176. expect(scope.$eval("1<=1")).toBeTruthy();
  177. expect(scope.$eval("1>2")).toEqual(1>2);
  178. expect(scope.$eval("2>=1")).toEqual(2>=1);
  179. expect(scope.$eval("true==2<3")).toEqual(true == 2<3);
  180. expect(scope.$eval("true===2<3")).toEqual(true === 2<3);
  181. });
  182. it('should parse logical', function() {
  183. expect(scope.$eval("0&&2")).toEqual(0&&2);
  184. expect(scope.$eval("0||2")).toEqual(0||2);
  185. expect(scope.$eval("0||1&&2")).toEqual(0||1&&2);
  186. });
  187. it('should parse ternary', function(){
  188. var returnTrue = scope.returnTrue = function(){ return true; };
  189. var returnFalse = scope.returnFalse = function(){ return false; };
  190. var returnString = scope.returnString = function(){ return 'asd'; };
  191. var returnInt = scope.returnInt = function(){ return 123; };
  192. var identity = scope.identity = function(x){ return x; };
  193. // Simple.
  194. expect(scope.$eval('0?0:2')).toEqual(0?0:2);
  195. expect(scope.$eval('1?0:2')).toEqual(1?0:2);
  196. // Nested on the left.
  197. expect(scope.$eval('0?0?0:0:2')).toEqual(0?0?0:0:2);
  198. expect(scope.$eval('1?0?0:0:2')).toEqual(1?0?0:0:2);
  199. expect(scope.$eval('0?1?0:0:2')).toEqual(0?1?0:0:2);
  200. expect(scope.$eval('0?0?1:0:2')).toEqual(0?0?1:0:2);
  201. expect(scope.$eval('0?0?0:2:3')).toEqual(0?0?0:2:3);
  202. expect(scope.$eval('1?1?0:0:2')).toEqual(1?1?0:0:2);
  203. expect(scope.$eval('1?1?1:0:2')).toEqual(1?1?1:0:2);
  204. expect(scope.$eval('1?1?1:2:3')).toEqual(1?1?1:2:3);
  205. expect(scope.$eval('1?1?1:2:3')).toEqual(1?1?1:2:3);
  206. // Nested on the right.
  207. expect(scope.$eval('0?0:0?0:2')).toEqual(0?0:0?0:2);
  208. expect(scope.$eval('1?0:0?0:2')).toEqual(1?0:0?0:2);
  209. expect(scope.$eval('0?1:0?0:2')).toEqual(0?1:0?0:2);
  210. expect(scope.$eval('0?0:1?0:2')).toEqual(0?0:1?0:2);
  211. expect(scope.$eval('0?0:0?2:3')).toEqual(0?0:0?2:3);
  212. expect(scope.$eval('1?1:0?0:2')).toEqual(1?1:0?0:2);
  213. expect(scope.$eval('1?1:1?0:2')).toEqual(1?1:1?0:2);
  214. expect(scope.$eval('1?1:1?2:3')).toEqual(1?1:1?2:3);
  215. expect(scope.$eval('1?1:1?2:3')).toEqual(1?1:1?2:3);
  216. // Precedence with respect to logical operators.
  217. expect(scope.$eval('0&&1?0:1')).toEqual(0&&1?0:1);
  218. expect(scope.$eval('1||0?0:0')).toEqual(1||0?0:0);
  219. expect(scope.$eval('0?0&&1:2')).toEqual(0?0&&1:2);
  220. expect(scope.$eval('0?1&&1:2')).toEqual(0?1&&1:2);
  221. expect(scope.$eval('0?0||0:1')).toEqual(0?0||0:1);
  222. expect(scope.$eval('0?0||1:2')).toEqual(0?0||1:2);
  223. expect(scope.$eval('1?0&&1:2')).toEqual(1?0&&1:2);
  224. expect(scope.$eval('1?1&&1:2')).toEqual(1?1&&1:2);
  225. expect(scope.$eval('1?0||0:1')).toEqual(1?0||0:1);
  226. expect(scope.$eval('1?0||1:2')).toEqual(1?0||1:2);
  227. expect(scope.$eval('0?1:0&&1')).toEqual(0?1:0&&1);
  228. expect(scope.$eval('0?2:1&&1')).toEqual(0?2:1&&1);
  229. expect(scope.$eval('0?1:0||0')).toEqual(0?1:0||0);
  230. expect(scope.$eval('0?2:0||1')).toEqual(0?2:0||1);
  231. expect(scope.$eval('1?1:0&&1')).toEqual(1?1:0&&1);
  232. expect(scope.$eval('1?2:1&&1')).toEqual(1?2:1&&1);
  233. expect(scope.$eval('1?1:0||0')).toEqual(1?1:0||0);
  234. expect(scope.$eval('1?2:0||1')).toEqual(1?2:0||1);
  235. // Function calls.
  236. expect(scope.$eval('returnTrue() ? returnString() : returnInt()')).toEqual(returnTrue() ? returnString() : returnInt());
  237. expect(scope.$eval('returnFalse() ? returnString() : returnInt()')).toEqual(returnFalse() ? returnString() : returnInt());
  238. expect(scope.$eval('returnTrue() ? returnString() : returnInt()')).toEqual(returnTrue() ? returnString() : returnInt());
  239. expect(scope.$eval('identity(returnFalse() ? returnString() : returnInt())')).toEqual(identity(returnFalse() ? returnString() : returnInt()));
  240. });
  241. it('should parse string', function() {
  242. expect(scope.$eval("'a' + 'b c'")).toEqual("ab c");
  243. });
  244. it('should parse filters', function() {
  245. $filterProvider.register('substring', valueFn(function(input, start, end) {
  246. return input.substring(start, end);
  247. }));
  248. expect(function() {
  249. scope.$eval("1|nonexistent");
  250. }).toThrow(new Error("[$injector:unpr] Unknown provider: nonexistentFilterProvider <- nonexistentFilter"));
  251. scope.offset = 3;
  252. expect(scope.$eval("'abcd'|substring:1:offset")).toEqual("bc");
  253. expect(scope.$eval("'abcd'|substring:1:3|uppercase")).toEqual("BC");
  254. });
  255. it('should access scope', function() {
  256. scope.a = 123;
  257. scope.b = {c: 456};
  258. expect(scope.$eval("a", scope)).toEqual(123);
  259. expect(scope.$eval("b.c", scope)).toEqual(456);
  260. expect(scope.$eval("x.y.z", scope)).not.toBeDefined();
  261. });
  262. it('should resolve deeply nested paths (important for CSP mode)', function() {
  263. scope.a = {b: {c: {d: {e: {f: {g: {h: {i: {j: {k: {l: {m: {n: 'nooo!'}}}}}}}}}}}}};
  264. expect(scope.$eval("a.b.c.d.e.f.g.h.i.j.k.l.m.n", scope)).toBe('nooo!');
  265. });
  266. it('should be forgiving', function() {
  267. scope.a = {b: 23};
  268. expect(scope.$eval('b')).toBeUndefined();
  269. expect(scope.$eval('a.x')).toBeUndefined();
  270. expect(scope.$eval('a.b.c.d')).toBeUndefined();
  271. });
  272. it('should support property names that collide with native object properties', function() {
  273. // regression
  274. scope.watch = 1;
  275. scope.toString = function toString() {
  276. return "custom toString";
  277. };
  278. expect(scope.$eval('watch', scope)).toBe(1);
  279. expect(scope.$eval('toString()', scope)).toBe('custom toString');
  280. });
  281. it('should evaluate grouped expressions', function() {
  282. expect(scope.$eval("(1+2)*3")).toEqual((1+2)*3);
  283. });
  284. it('should evaluate assignments', function() {
  285. expect(scope.$eval("a=12")).toEqual(12);
  286. expect(scope.a).toEqual(12);
  287. expect(scope.$eval("x.y.z=123;")).toEqual(123);
  288. expect(scope.x.y.z).toEqual(123);
  289. expect(scope.$eval("a=123; b=234")).toEqual(234);
  290. expect(scope.a).toEqual(123);
  291. expect(scope.b).toEqual(234);
  292. });
  293. it('should evaluate function call without arguments', function() {
  294. scope['const'] = function(a,b){return 123;};
  295. expect(scope.$eval("const()")).toEqual(123);
  296. });
  297. it('should evaluate function call with arguments', function() {
  298. scope.add = function(a,b) {
  299. return a+b;
  300. };
  301. expect(scope.$eval("add(1,2)")).toEqual(3);
  302. });
  303. it('should evaluate function call from a return value', function() {
  304. scope.val = 33;
  305. scope.getter = function() { return function() { return this.val; }};
  306. expect(scope.$eval("getter()()")).toBe(33);
  307. });
  308. it('should evaluate multiplication and division', function() {
  309. scope.taxRate = 8;
  310. scope.subTotal = 100;
  311. expect(scope.$eval("taxRate / 100 * subTotal")).toEqual(8);
  312. expect(scope.$eval("subTotal * taxRate / 100")).toEqual(8);
  313. });
  314. it('should evaluate array', function() {
  315. expect(scope.$eval("[]").length).toEqual(0);
  316. expect(scope.$eval("[1, 2]").length).toEqual(2);
  317. expect(scope.$eval("[1, 2]")[0]).toEqual(1);
  318. expect(scope.$eval("[1, 2]")[1]).toEqual(2);
  319. });
  320. it('should evaluate array access', function() {
  321. expect(scope.$eval("[1][0]")).toEqual(1);
  322. expect(scope.$eval("[[1]][0][0]")).toEqual(1);
  323. expect(scope.$eval("[].length")).toEqual(0);
  324. expect(scope.$eval("[1, 2].length")).toEqual(2);
  325. });
  326. it('should evaluate object', function() {
  327. expect(toJson(scope.$eval("{}"))).toEqual("{}");
  328. expect(toJson(scope.$eval("{a:'b'}"))).toEqual('{"a":"b"}');
  329. expect(toJson(scope.$eval("{'a':'b'}"))).toEqual('{"a":"b"}');
  330. expect(toJson(scope.$eval("{\"a\":'b'}"))).toEqual('{"a":"b"}');
  331. });
  332. it('should evaluate object access', function() {
  333. expect(scope.$eval("{false:'WC', true:'CC'}[false]")).toEqual("WC");
  334. });
  335. it('should evaluate JSON', function() {
  336. expect(toJson(scope.$eval("[{}]"))).toEqual("[{}]");
  337. expect(toJson(scope.$eval("[{a:[]}, {b:1}]"))).toEqual('[{"a":[]},{"b":1}]');
  338. });
  339. it('should evaluate multiple statements', function() {
  340. expect(scope.$eval("a=1;b=3;a+b")).toEqual(4);
  341. expect(scope.$eval(";;1;;")).toEqual(1);
  342. });
  343. it('should evaluate object methods in correct context (this)', function() {
  344. var C = function () {
  345. this.a = 123;
  346. };
  347. C.prototype.getA = function() {
  348. return this.a;
  349. };
  350. scope.obj = new C();
  351. expect(scope.$eval("obj.getA()")).toEqual(123);
  352. expect(scope.$eval("obj['getA']()")).toEqual(123);
  353. });
  354. it('should evaluate methods in correct context (this) in argument', function() {
  355. var C = function () {
  356. this.a = 123;
  357. };
  358. C.prototype.sum = function(value) {
  359. return this.a + value;
  360. };
  361. C.prototype.getA = function() {
  362. return this.a;
  363. };
  364. scope.obj = new C();
  365. expect(scope.$eval("obj.sum(obj.getA())")).toEqual(246);
  366. expect(scope.$eval("obj['sum'](obj.getA())")).toEqual(246);
  367. });
  368. it('should evaluate objects on scope context', function() {
  369. scope.a = "abc";
  370. expect(scope.$eval("{a:a}").a).toEqual("abc");
  371. });
  372. it('should evaluate field access on function call result', function() {
  373. scope.a = function() {
  374. return {name:'misko'};
  375. };
  376. expect(scope.$eval("a().name")).toEqual("misko");
  377. });
  378. it('should evaluate field access after array access', function () {
  379. scope.items = [{}, {name:'misko'}];
  380. expect(scope.$eval('items[1].name')).toEqual("misko");
  381. });
  382. it('should evaluate array assignment', function() {
  383. scope.items = [];
  384. expect(scope.$eval('items[1] = "abc"')).toEqual("abc");
  385. expect(scope.$eval('items[1]')).toEqual("abc");
  386. // Dont know how to make this work....
  387. // expect(scope.$eval('books[1] = "moby"')).toEqual("moby");
  388. // expect(scope.$eval('books[1]')).toEqual("moby");
  389. });
  390. it('should evaluate grouped filters', function() {
  391. scope.name = 'MISKO';
  392. expect(scope.$eval('n = (name|lowercase)')).toEqual('misko');
  393. expect(scope.$eval('n')).toEqual('misko');
  394. });
  395. it('should evaluate remainder', function() {
  396. expect(scope.$eval('1%2')).toEqual(1);
  397. });
  398. it('should evaluate sum with undefined', function() {
  399. expect(scope.$eval('1+undefined')).toEqual(1);
  400. expect(scope.$eval('undefined+1')).toEqual(1);
  401. });
  402. it('should throw exception on non-closed bracket', function() {
  403. expect(function() {
  404. scope.$eval('[].count(');
  405. }).toThrow('[$parse:ueoe] Unexpected end of expression: [].count(');
  406. });
  407. it('should evaluate double negation', function() {
  408. expect(scope.$eval('true')).toBeTruthy();
  409. expect(scope.$eval('!true')).toBeFalsy();
  410. expect(scope.$eval('!!true')).toBeTruthy();
  411. expect(scope.$eval('{true:"a", false:"b"}[!!true]')).toEqual('a');
  412. });
  413. it('should evaluate negation', function() {
  414. expect(scope.$eval("!false || true")).toEqual(!false || true);
  415. expect(scope.$eval("!11 == 10")).toEqual(!11 == 10);
  416. expect(scope.$eval("12/6/2")).toEqual(12/6/2);
  417. });
  418. it('should evaluate exclamation mark', function() {
  419. expect(scope.$eval('suffix = "!"')).toEqual('!');
  420. });
  421. it('should evaluate minus', function() {
  422. expect(scope.$eval("{a:'-'}")).toEqual({a: "-"});
  423. });
  424. it('should evaluate undefined', function() {
  425. expect(scope.$eval("undefined")).not.toBeDefined();
  426. expect(scope.$eval("a=undefined")).not.toBeDefined();
  427. expect(scope.a).not.toBeDefined();
  428. });
  429. it('should allow assignment after array dereference', function() {
  430. scope.obj = [{}];
  431. scope.$eval('obj[0].name=1');
  432. expect(scope.obj.name).toBeUndefined();
  433. expect(scope.obj[0].name).toEqual(1);
  434. });
  435. it('should short-circuit AND operator', function() {
  436. scope.run = function() {
  437. throw "IT SHOULD NOT HAVE RUN";
  438. };
  439. expect(scope.$eval('false && run()')).toBe(false);
  440. });
  441. it('should short-circuit OR operator', function() {
  442. scope.run = function() {
  443. throw "IT SHOULD NOT HAVE RUN";
  444. };
  445. expect(scope.$eval('true || run()')).toBe(true);
  446. });
  447. it('should support method calls on primitive types', function() {
  448. scope.empty = '';
  449. scope.zero = 0;
  450. scope.bool = false;
  451. expect(scope.$eval('empty.substr(0)')).toBe('');
  452. expect(scope.$eval('zero.toString()')).toBe('0');
  453. expect(scope.$eval('bool.toString()')).toBe('false');
  454. });
  455. describe('sandboxing', function() {
  456. it('should NOT allow access to Function constructor in getter', function() {
  457. expect(function() {
  458. scope.$eval('{}.toString.constructor');
  459. }).toThrow(new Error(
  460. '[$parse:isecfld] Referencing "constructor" field in Angular expressions is disallowed! ' +
  461. 'Expression: {}.toString.constructor'));
  462. expect(function() {
  463. scope.$eval('{}.toString.constructor("alert(1)")');
  464. }).toThrow(new Error(
  465. '[$parse:isecfld] Referencing "constructor" field in Angular expressions is disallowed! ' +
  466. 'Expression: {}.toString.constructor("alert(1)")'));
  467. expect(function() {
  468. scope.$eval('[].toString.constructor.foo');
  469. }).toThrow(new Error(
  470. '[$parse:isecfld] Referencing "constructor" field in Angular expressions is disallowed! ' +
  471. 'Expression: [].toString.constructor.foo'));
  472. expect(function() {
  473. scope.$eval('{}.toString["constructor"]');
  474. }).toThrow(new Error(
  475. '[$parse:isecfn] Referencing Function in Angular expressions is disallowed! ' +
  476. 'Expression: {}.toString["constructor"]'));
  477. expect(function() {
  478. scope.$eval('{}["toString"]["constructor"]');
  479. }).toThrow(new Error(
  480. '[$parse:isecfn] Referencing Function in Angular expressions is disallowed! ' +
  481. 'Expression: {}["toString"]["constructor"]'));
  482. scope.a = [];
  483. expect(function() {
  484. scope.$eval('a.toString.constructor', scope);
  485. }).toThrow(new Error(
  486. '[$parse:isecfld] Referencing "constructor" field in Angular expressions is disallowed! ' +
  487. 'Expression: a.toString.constructor'));
  488. expect(function() {
  489. scope.$eval('a.toString["constructor"]', scope);
  490. }).toThrow(new Error(
  491. '[$parse:isecfn] Referencing Function in Angular expressions is disallowed! ' +
  492. 'Expression: a.toString["constructor"]'));
  493. });
  494. it('should NOT allow access to Function constructor in setter', function() {
  495. expect(function() {
  496. scope.$eval('{}.toString.constructor = 1');
  497. }).toThrow(new Error(
  498. '[$parse:isecfld] Referencing "constructor" field in Angular expressions is disallowed! ' +
  499. 'Expression: {}.toString.constructor = 1'));
  500. expect(function() {
  501. scope.$eval('{}.toString.constructor.a = 1');
  502. }).toThrow(new Error(
  503. '[$parse:isecfld] Referencing "constructor" field in Angular expressions is disallowed! ' +
  504. 'Expression: {}.toString.constructor.a = 1'));
  505. expect(function() {
  506. scope.$eval('{}.toString["constructor"]["constructor"] = 1');
  507. }).toThrow(new Error(
  508. '[$parse:isecfn] Referencing Function in Angular expressions is disallowed! ' +
  509. 'Expression: {}.toString["constructor"]["constructor"] = 1'));
  510. scope.key1 = "const";
  511. scope.key2 = "ructor";
  512. expect(function() {
  513. scope.$eval('{}.toString[key1 + key2].foo = 1');
  514. }).toThrow(new Error(
  515. '[$parse:isecfn] Referencing Function in Angular expressions is disallowed! ' +
  516. 'Expression: {}.toString[key1 + key2].foo = 1'));
  517. expect(function() {
  518. scope.$eval('{}.toString["constructor"]["a"] = 1');
  519. }).toThrow(new Error(
  520. '[$parse:isecfn] Referencing Function in Angular expressions is disallowed! ' +
  521. 'Expression: {}.toString["constructor"]["a"] = 1'));
  522. scope.a = [];
  523. expect(function() {
  524. scope.$eval('a.toString.constructor = 1', scope);
  525. }).toThrow(new Error(
  526. '[$parse:isecfld] Referencing "constructor" field in Angular expressions is disallowed! ' +
  527. 'Expression: a.toString.constructor = 1'));
  528. });
  529. it('should NOT allow access to Function constructor that has been aliased', function() {
  530. scope.foo = { "bar": Function };
  531. expect(function() {
  532. scope.$eval('foo["bar"]');
  533. }).toThrow(new Error(
  534. '[$parse:isecfn] Referencing Function in Angular expressions is disallowed! ' +
  535. 'Expression: foo["bar"]'));
  536. });
  537. });
  538. describe('overriding constructor', function() {
  539. it('should evaluate grouped expressions', function() {
  540. scope.foo = function foo() {
  541. return "foo";
  542. };
  543. // When not overridden, access should be restricted both by the dot operator and by the
  544. // index operator.
  545. expect(function() {
  546. scope.$eval('foo.constructor()', scope)
  547. }).toThrow(new Error(
  548. '[$parse:isecfld] Referencing "constructor" field in Angular expressions is disallowed! ' +
  549. 'Expression: foo.constructor()'));
  550. expect(function() {
  551. scope.$eval('foo["constructor"]()', scope)
  552. }).toThrow(new Error(
  553. '[$parse:isecfn] Referencing Function in Angular expressions is disallowed! ' +
  554. 'Expression: foo["constructor"]()'));
  555. // User defined value assigned to constructor.
  556. scope.foo.constructor = function constructor() {
  557. return "custom constructor";
  558. }
  559. // Dot operator should still block it.
  560. expect(function() {
  561. scope.$eval('foo.constructor()', scope)
  562. }).toThrow(new Error(
  563. '[$parse:isecfld] Referencing "constructor" field in Angular expressions is disallowed! ' +
  564. 'Expression: foo.constructor()'));
  565. // However, the index operator should allow it.
  566. expect(scope.$eval('foo["constructor"]()', scope)).toBe('custom constructor');
  567. });
  568. });
  569. it('should call the function from the received instance and not from a new one', function() {
  570. var n = 0;
  571. scope.fn = function() {
  572. var c = n++;
  573. return { c: c, anotherFn: function() { return this.c == c; } };
  574. };
  575. expect(scope.$eval('fn().anotherFn()')).toBe(true);
  576. });
  577. it('should call the function once when it is part of the context', function() {
  578. var count = 0;
  579. scope.fn = function() {
  580. count++;
  581. return { anotherFn: function() { return "lucas"; } };
  582. };
  583. expect(scope.$eval('fn().anotherFn()')).toBe('lucas');
  584. expect(count).toBe(1);
  585. });
  586. it('should call the function once when it is not part of the context', function() {
  587. var count = 0;
  588. scope.fn = function() {
  589. count++;
  590. return function() { return 'lucas'; };
  591. };
  592. expect(scope.$eval('fn()()')).toBe('lucas');
  593. expect(count).toBe(1);
  594. });
  595. it('should call the function once when it is not part of the context', function() {
  596. var count = 0;
  597. scope.fn = function() {
  598. count++;
  599. return function() { return 'lucas'; };
  600. };
  601. expect(scope.$eval('fn()()')).toBe('lucas');
  602. expect(count).toBe(1);
  603. });
  604. it('should call the function once when it is part of the context on assignments', function() {
  605. var count = 0;
  606. var element = {};
  607. scope.fn = function() {
  608. count++;
  609. return element;
  610. };
  611. expect(scope.$eval('fn().name = "lucas"')).toBe('lucas');
  612. expect(element.name).toBe('lucas');
  613. expect(count).toBe(1);
  614. });
  615. it('should call the function once when it is part of the context on array lookups', function() {
  616. var count = 0;
  617. var element = [];
  618. scope.fn = function() {
  619. count++;
  620. return element;
  621. };
  622. expect(scope.$eval('fn()[0] = "lucas"')).toBe('lucas');
  623. expect(element[0]).toBe('lucas');
  624. expect(count).toBe(1);
  625. });
  626. it('should call the function once when it is part of the context on array lookup function', function() {
  627. var count = 0;
  628. var element = [{anotherFn: function() { return 'lucas';} }];
  629. scope.fn = function() {
  630. count++;
  631. return element;
  632. };
  633. expect(scope.$eval('fn()[0].anotherFn()')).toBe('lucas');
  634. expect(count).toBe(1);
  635. });
  636. it('should call the function once when it is part of the context on array lookup function', function() {
  637. var count = 0;
  638. var element = {name: {anotherFn: function() { return 'lucas';} } };
  639. scope.fn = function() {
  640. count++;
  641. return element;
  642. };
  643. expect(scope.$eval('fn().name.anotherFn()')).toBe('lucas');
  644. expect(count).toBe(1);
  645. });
  646. it('should call the function once when it is part of a sub-expression', function() {
  647. var count = 0;
  648. scope.element = [{}];
  649. scope.fn = function() {
  650. count++;
  651. return 0;
  652. };
  653. expect(scope.$eval('element[fn()].name = "lucas"')).toBe('lucas');
  654. expect(scope.element[0].name).toBe('lucas');
  655. expect(count).toBe(1);
  656. });
  657. describe('promises', function() {
  658. var deferred, promise, q;
  659. beforeEach(inject(function($q) {
  660. q = $q;
  661. deferred = q.defer();
  662. promise = deferred.promise;
  663. }));
  664. describe('{{promise}}', function() {
  665. it('should evaluated resolved promise and get its value', function() {
  666. deferred.resolve('hello!');
  667. scope.greeting = promise;
  668. expect(scope.$eval('greeting')).toBe(undefined);
  669. scope.$digest();
  670. expect(scope.$eval('greeting')).toBe('hello!');
  671. });
  672. it('should evaluated rejected promise and ignore the rejection reason', function() {
  673. deferred.reject('sorry');
  674. scope.greeting = promise;
  675. expect(scope.$eval('gretting')).toBe(undefined);
  676. scope.$digest();
  677. expect(scope.$eval('greeting')).toBe(undefined);
  678. });
  679. it('should evaluate a promise and eventualy get its value', function() {
  680. scope.greeting = promise;
  681. expect(scope.$eval('greeting')).toBe(undefined);
  682. scope.$digest();
  683. expect(scope.$eval('greeting')).toBe(undefined);
  684. deferred.resolve('hello!');
  685. expect(scope.$eval('greeting')).toBe(undefined);
  686. scope.$digest();
  687. expect(scope.$eval('greeting')).toBe('hello!');
  688. });
  689. it('should evaluate a promise and eventualy ignore its rejection', function() {
  690. scope.greeting = promise;
  691. expect(scope.$eval('greeting')).toBe(undefined);
  692. scope.$digest();
  693. expect(scope.$eval('greeting')).toBe(undefined);
  694. deferred.reject('sorry');
  695. expect(scope.$eval('greeting')).toBe(undefined);
  696. scope.$digest();
  697. expect(scope.$eval('greeting')).toBe(undefined);
  698. });
  699. });
  700. describe('dereferencing', function() {
  701. it('should evaluate and dereference properties leading to and from a promise', function() {
  702. scope.obj = {greeting: promise};
  703. expect(scope.$eval('obj.greeting')).toBe(undefined);
  704. expect(scope.$eval('obj.greeting.polite')).toBe(undefined);
  705. scope.$digest();
  706. expect(scope.$eval('obj.greeting')).toBe(undefined);
  707. expect(scope.$eval('obj.greeting.polite')).toBe(undefined);
  708. deferred.resolve({polite: 'Good morning!'});
  709. scope.$digest();
  710. expect(scope.$eval('obj.greeting')).toEqual({polite: 'Good morning!'});
  711. expect(scope.$eval('obj.greeting.polite')).toBe('Good morning!');
  712. });
  713. it('should evaluate and dereference properties leading to and from a promise via bracket ' +
  714. 'notation', function() {
  715. scope.obj = {greeting: promise};
  716. expect(scope.$eval('obj["greeting"]')).toBe(undefined);
  717. expect(scope.$eval('obj["greeting"]["polite"]')).toBe(undefined);
  718. scope.$digest();
  719. expect(scope.$eval('obj["greeting"]')).toBe(undefined);
  720. expect(scope.$eval('obj["greeting"]["polite"]')).toBe(undefined);
  721. deferred.resolve({polite: 'Good morning!'});
  722. scope.$digest();
  723. expect(scope.$eval('obj["greeting"]')).toEqual({polite: 'Good morning!'});
  724. expect(scope.$eval('obj["greeting"]["polite"]')).toBe('Good morning!');
  725. });
  726. it('should evaluate and dereference array references leading to and from a promise',
  727. function() {
  728. scope.greetings = [promise];
  729. expect(scope.$eval('greetings[0]')).toBe(undefined);
  730. expect(scope.$eval('greetings[0][0]')).toBe(undefined);
  731. scope.$digest();
  732. expect(scope.$eval('greetings[0]')).toBe(undefined);
  733. expect(scope.$eval('greetings[0][0]')).toBe(undefined);
  734. deferred.resolve(['Hi!', 'Cau!']);
  735. scope.$digest();
  736. expect(scope.$eval('greetings[0]')).toEqual(['Hi!', 'Cau!']);
  737. expect(scope.$eval('greetings[0][0]')).toBe('Hi!');
  738. });
  739. it('should evaluate and dereference promises used as function arguments', function() {
  740. scope.greet = function(name) { return 'Hi ' + name + '!'; };
  741. scope.name = promise;
  742. expect(scope.$eval('greet(name)')).toBe('Hi undefined!');
  743. scope.$digest();
  744. expect(scope.$eval('greet(name)')).toBe('Hi undefined!');
  745. deferred.resolve('Veronica');
  746. expect(scope.$eval('greet(name)')).toBe('Hi undefined!');
  747. scope.$digest();
  748. expect(scope.$eval('greet(name)')).toBe('Hi Veronica!');
  749. });
  750. it('should evaluate and dereference promises used as array indexes', function() {
  751. scope.childIndex = promise;
  752. scope.kids = ['Adam', 'Veronica', 'Elisa'];
  753. expect(scope.$eval('kids[childIndex]')).toBe(undefined);
  754. scope.$digest();
  755. expect(scope.$eval('kids[childIndex]')).toBe(undefined);
  756. deferred.resolve(1);
  757. expect(scope.$eval('kids[childIndex]')).toBe(undefined);
  758. scope.$digest();
  759. expect(scope.$eval('kids[childIndex]')).toBe('Veronica');
  760. });
  761. it('should evaluate and dereference promises used as keys in bracket notation', function() {
  762. scope.childKey = promise;
  763. scope.kids = {'a': 'Adam', 'v': 'Veronica', 'e': 'Elisa'};
  764. expect(scope.$eval('kids[childKey]')).toBe(undefined);
  765. scope.$digest();
  766. expect(scope.$eval('kids[childKey]')).toBe(undefined);
  767. deferred.resolve('v');
  768. expect(scope.$eval('kids[childKey]')).toBe(undefined);
  769. scope.$digest();
  770. expect(scope.$eval('kids[childKey]')).toBe('Veronica');
  771. });
  772. it('should not mess with the promise if it was not directly evaluated', function() {
  773. scope.obj = {greeting: promise, username: 'hi'};
  774. var obj = scope.$eval('obj');
  775. expect(obj.username).toEqual('hi');
  776. expect(typeof obj.greeting.then).toBe('function');
  777. });
  778. });
  779. });
  780. describe('assignable', function() {
  781. it('should expose assignment function', inject(function($parse) {
  782. var fn = $parse('a');
  783. expect(fn.assign).toBeTruthy();
  784. var scope = {};
  785. fn.assign(scope, 123);
  786. expect(scope).toEqual({a:123});
  787. }));
  788. });
  789. describe('locals', function() {
  790. it('should expose local variables', inject(function($parse) {
  791. expect($parse('a')({a: 0}, {a: 1})).toEqual(1);
  792. expect($parse('add(a,b)')({b: 1, add: function(a, b) { return a + b; }}, {a: 2})).toEqual(3);
  793. }));
  794. it('should expose traverse locals', inject(function($parse) {
  795. expect($parse('a.b')({a: {b: 0}}, {a: {b:1}})).toEqual(1);
  796. expect($parse('a.b')({a: null}, {a: {b:1}})).toEqual(1);
  797. expect($parse('a.b')({a: {b: 0}}, {a: null})).toEqual(undefined);
  798. }));
  799. });
  800. describe('literal', function() {
  801. it('should mark scalar value expressions as literal', inject(function($parse) {
  802. expect($parse('0').literal).toBe(true);
  803. expect($parse('"hello"').literal).toBe(true);
  804. expect($parse('true').literal).toBe(true);
  805. expect($parse('false').literal).toBe(true);
  806. expect($parse('null').literal).toBe(true);
  807. expect($parse('undefined').literal).toBe(true);
  808. }));
  809. it('should mark array expressions as literal', inject(function($parse) {
  810. expect($parse('[]').literal).toBe(true);
  811. expect($parse('[1, 2, 3]').literal).toBe(true);
  812. expect($parse('[1, identifier]').literal).toBe(true);
  813. }));
  814. it('should mark object expressions as literal', inject(function($parse) {
  815. expect($parse('{}').literal).toBe(true);
  816. expect($parse('{x: 1}').literal).toBe(true);
  817. expect($parse('{foo: bar}').literal).toBe(true);
  818. }));
  819. it('should not mark function calls or operator expressions as literal', inject(function($parse) {
  820. expect($parse('1 + 1').literal).toBe(false);
  821. expect($parse('call()').literal).toBe(false);
  822. expect($parse('[].length').literal).toBe(false);
  823. }));
  824. });
  825. describe('constant', function() {
  826. it('should mark scalar value expressions as constant', inject(function($parse) {
  827. expect($parse('12.3').constant).toBe(true);
  828. expect($parse('"string"').constant).toBe(true);
  829. expect($parse('true').constant).toBe(true);
  830. expect($parse('false').constant).toBe(true);
  831. expect($parse('null').constant).toBe(true);
  832. expect($parse('undefined').constant).toBe(true);
  833. }));
  834. it('should mark arrays as constant if they only contain constant elements', inject(function($parse) {
  835. expect($parse('[]').constant).toBe(true);
  836. expect($parse('[1, 2, 3]').constant).toBe(true);
  837. expect($parse('["string", null]').constant).toBe(true);
  838. expect($parse('[[]]').constant).toBe(true);
  839. expect($parse('[1, [2, 3], {4: 5}]').constant).toBe(true);
  840. }));
  841. it('should not mark arrays as constant if they contain any non-constant elements', inject(function($parse) {
  842. expect($parse('[foo]').constant).toBe(false);
  843. expect($parse('[x + 1]').constant).toBe(false);
  844. expect($parse('[bar[0]]').constant).toBe(false);
  845. }));
  846. it('should mark complex expressions involving constant values as constant', inject(function($parse) {
  847. expect($parse('!true').constant).toBe(true);
  848. expect($parse('1 - 1').constant).toBe(true);
  849. expect($parse('"foo" + "bar"').constant).toBe(true);
  850. expect($parse('5 != null').constant).toBe(true);
  851. expect($parse('{standard: 4/3, wide: 16/9}').constant).toBe(true);
  852. }));
  853. it('should not mark any expression involving variables or function calls as constant', inject(function($parse) {
  854. expect($parse('true.toString()').constant).toBe(false);
  855. expect($parse('foo(1, 2, 3)').constant).toBe(false);
  856. expect($parse('"name" + id').constant).toBe(false);
  857. }));
  858. });
  859. });
  860. });
  861. });