exp-parser.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. // expressions with inline object literals
  85. exp: "sortRows({ column: 'name', test: 'haha', durrr: 123 })",
  86. vm: {
  87. sortRows: function (params) {
  88. return params.column + params.durrr
  89. }
  90. },
  91. expectedValue: 'name123'
  92. }
  93. ]
  94. testCases.forEach(describeCase)
  95. describe('XSS protection', function () {
  96. var cases = [
  97. {
  98. xss: true,
  99. exp: "constructor.constructor('alert(1)')()",
  100. vm: {}
  101. },
  102. {
  103. xss: true,
  104. exp: "\"\".toString.constructor.constructor('alert(1)')()",
  105. vm: {}
  106. },
  107. {
  108. xss: true,
  109. exp: "\"\".toString['cons' + 'tructor']['cons' + 'tructor']('alert(1)')()",
  110. vm: {}
  111. },
  112. {
  113. xss: true,
  114. exp: "\"\".toString['\\u0063ons' + 'tructor']['\\u0063ons' + 'tructor']('alert(1)')()",
  115. vm: {}
  116. }
  117. ]
  118. cases.forEach(describeCase)
  119. })
  120. describe('scope trace', function () {
  121. it('should determine the correct scope for variables', function () {
  122. var bindingsCreated = {}
  123. var getter = ExpParser.parse('a + b', mockCompiler({
  124. parent: {
  125. bindings: {},
  126. createBinding: function (key) {
  127. assert.strictEqual(key, 'a')
  128. bindingsCreated[key] = true
  129. },
  130. hasKey: function (key) {
  131. return key === 'a'
  132. },
  133. parent: {
  134. bindings: {},
  135. createBinding: function (key) {
  136. assert.strictEqual(key, 'b')
  137. bindingsCreated[key] = true
  138. },
  139. hasKey: function (key) {
  140. return key === 'b'
  141. }
  142. }
  143. }
  144. }))
  145. var getterString = getter.toString()
  146. assert.ok(getterString.indexOf('this.$parent.a') > -1)
  147. assert.ok(getterString.indexOf('this.$parent.$parent.b') > -1)
  148. })
  149. })
  150. // extra case for invalid expressions
  151. describe('invalid expression', function () {
  152. before(warnSpy.swapWarn)
  153. it('should capture the error and warn', function () {
  154. ExpParser.parse('a + "fsef', mockCompiler())
  155. assert.ok(warnSpy.warned)
  156. })
  157. after(warnSpy.resetWarn)
  158. })
  159. describe('.eval() with extra data', function () {
  160. it('should be able to eval an epxression with temporary additional data', function () {
  161. var res = ExpParser.eval('a + b', mockCompiler(), { a: 1, b: 2 })
  162. assert.strictEqual(res, 3)
  163. })
  164. })
  165. function describeCase (testCase) {
  166. describe(testCase.exp, function () {
  167. function createBinding (path) {
  168. caughtMissingPaths.push(path)
  169. }
  170. var caughtMissingPaths = [],
  171. compilerMock = {
  172. createBinding: createBinding,
  173. hasKey: function () {},
  174. vm: testCase.vm
  175. },
  176. vars = testCase.paths || Object.keys(testCase.vm),
  177. getter
  178. if (testCase.xss) {
  179. before(warnSpy.swapWarn)
  180. after(warnSpy.resetWarn)
  181. }
  182. before(function () {
  183. getter = ExpParser.parse(testCase.exp, compilerMock)
  184. })
  185. if (!testCase.xss) {
  186. it('should get correct paths', function () {
  187. if (!vars.length) return
  188. assert.strictEqual(caughtMissingPaths.length, vars.length)
  189. for (var i = 0; i < vars.length; i++) {
  190. assert.strictEqual(vars[i], caughtMissingPaths[i])
  191. }
  192. })
  193. it('getter function should return expected value', function () {
  194. var value = getter.call(testCase.vm)
  195. assert.strictEqual(value, testCase.expectedValue)
  196. })
  197. } else {
  198. it('should return undefined getter', function () {
  199. assert.strictEqual(getter, undefined)
  200. })
  201. it('should have warned', function () {
  202. assert.ok(warnSpy.warned)
  203. })
  204. }
  205. })
  206. }
  207. function noop () {}
  208. function mockCompiler (opts) {
  209. var mock = {
  210. createBinding: noop,
  211. hasKey: noop,
  212. vm: {
  213. $compiler: {
  214. bindings: {},
  215. createBinding: noop
  216. },
  217. $data: {}
  218. }
  219. }
  220. for (var key in opts) {
  221. mock[key] = opts[key]
  222. }
  223. return mock
  224. }
  225. })