exp-parser.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. describe('Expression Parser', function () {
  2. var ExpParser = require('vue/src/exp-parser'),
  3. utils = require('vue/src/utils'),
  4. oldWarn = utils.warn
  5. var warnSpy = {
  6. warned: false,
  7. swapWarn: function () {
  8. utils.warn = function () {
  9. warnSpy.warned = true
  10. }
  11. },
  12. resetWarn: function () {
  13. utils.warn = oldWarn
  14. warnSpy.warned = false
  15. }
  16. }
  17. var testCases = [
  18. {
  19. // string concat
  20. exp: 'a + b',
  21. vm: {
  22. a: 'hello',
  23. b: 'world'
  24. },
  25. expectedValue: 'helloworld'
  26. },
  27. {
  28. // math
  29. exp: 'a - b * 2 + 45',
  30. vm: {
  31. a: 100,
  32. b: 23
  33. },
  34. expectedValue: 100 - 23 * 2 + 45
  35. },
  36. {
  37. // boolean logic
  38. exp: '(a && b) ? c : d || e',
  39. vm: {
  40. a: true,
  41. b: false,
  42. c: null,
  43. d: false,
  44. e: 'worked'
  45. },
  46. expectedValue: 'worked'
  47. },
  48. {
  49. // inline string
  50. exp: "a + 'hello'",
  51. vm: {
  52. a: 'inline '
  53. },
  54. expectedValue: 'inline hello'
  55. },
  56. {
  57. // complex with nested values
  58. exp: "todo.title + ' : ' + (todo.done ? 'yep' : 'nope')",
  59. paths: ['todo.title', 'todo.done'],
  60. vm: {
  61. todo: {
  62. title: 'write tests',
  63. done: false
  64. }
  65. },
  66. expectedValue: 'write tests : nope'
  67. },
  68. {
  69. // expression with no data variables
  70. exp: "'a' + 'b'",
  71. vm: {},
  72. expectedValue: 'ab'
  73. },
  74. {
  75. // values with same variable name inside strings
  76. exp: "'\"test\"' + test + \"'hi'\" + hi",
  77. vm: {
  78. test: 1,
  79. hi: 2
  80. },
  81. expectedValue: '"test"1\'hi\'2'
  82. }
  83. ]
  84. testCases.forEach(describeCase)
  85. // extra case for invalid expressions
  86. describe('invalid expression', function () {
  87. before(warnSpy.swapWarn)
  88. it('should capture the error and warn', function () {
  89. function noop () {}
  90. ExpParser.parse('a + "fsef', {
  91. createBinding: noop,
  92. hasKey: noop,
  93. vm: {
  94. $compiler: {
  95. bindings: {},
  96. createBinding: noop
  97. },
  98. $data: {}
  99. }
  100. })
  101. assert.ok(warnSpy.warned)
  102. })
  103. after(warnSpy.resetWarn)
  104. })
  105. describe('XSS protection', function () {
  106. var cases = [
  107. {
  108. xss: true,
  109. exp: "constructor.constructor('alert(1)')()",
  110. vm: {}
  111. },
  112. {
  113. xss: true,
  114. exp: "\"\".toString.constructor.constructor('alert(1)')()",
  115. vm: {}
  116. },
  117. {
  118. xss: true,
  119. exp: "\"\".toString['cons' + 'tructor']['cons' + 'tructor']('alert(1)')()",
  120. vm: {}
  121. },
  122. {
  123. xss: true,
  124. exp: "\"\".toString['\\u0063ons' + 'tructor']['\\u0063ons' + 'tructor']('alert(1)')()",
  125. vm: {}
  126. }
  127. ]
  128. cases.forEach(describeCase)
  129. })
  130. function describeCase (testCase) {
  131. describe(testCase.exp, function () {
  132. function createBinding (path) {
  133. caughtMissingPaths.push(path)
  134. }
  135. var caughtMissingPaths = [],
  136. compilerMock = {
  137. createBinding: createBinding,
  138. hasKey: function () {},
  139. vm: testCase.vm
  140. },
  141. vars = testCase.paths || Object.keys(testCase.vm),
  142. getter
  143. if (testCase.xss) {
  144. before(warnSpy.swapWarn)
  145. after(warnSpy.resetWarn)
  146. }
  147. before(function () {
  148. getter = ExpParser.parse(testCase.exp, compilerMock)
  149. })
  150. if (!testCase.xss) {
  151. it('should get correct paths', function () {
  152. if (!vars.length) return
  153. assert.strictEqual(caughtMissingPaths.length, vars.length)
  154. for (var i = 0; i < vars.length; i++) {
  155. assert.strictEqual(vars[i], caughtMissingPaths[i])
  156. }
  157. })
  158. it('getter function should return expected value', function () {
  159. var value = getter.call(testCase.vm)
  160. assert.strictEqual(value, testCase.expectedValue)
  161. })
  162. } else {
  163. it('should return undefined getter', function () {
  164. assert.strictEqual(getter, undefined)
  165. })
  166. it('should have warned', function () {
  167. assert.ok(warnSpy.warned)
  168. })
  169. }
  170. })
  171. }
  172. })