exp-parser.js 6.8 KB

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