exp-parser.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. describe('computed filters', function () {
  156. it('should wrap expression with computed filters', function () {
  157. var filters = [
  158. { name: 'test', args: ['a', 'b'] },
  159. { name: 'wrap', args: ['c', 'd'] }
  160. ],
  161. filterFns = {
  162. test: function (v, a, b) {
  163. return v + a + b
  164. },
  165. wrap: function (v, c, d) {
  166. return v + c + d
  167. }
  168. }
  169. var compiler = mockCompiler({
  170. getOption: function (type, id) {
  171. return filterFns[id]
  172. }
  173. })
  174. var getter = ExpParser.parse('a + b', compiler, null, filters)
  175. var res = getter.call({
  176. $compiler: compiler,
  177. a: 1,
  178. b: 2
  179. })
  180. assert.strictEqual(res, '3abcd')
  181. })
  182. })
  183. function describeCase (testCase) {
  184. describe(testCase.exp, function () {
  185. function createBinding (path) {
  186. caughtMissingPaths.push(path)
  187. }
  188. var caughtMissingPaths = [],
  189. compilerMock = {
  190. createBinding: createBinding,
  191. hasKey: function () {},
  192. vm: testCase.vm
  193. },
  194. vars = testCase.paths || Object.keys(testCase.vm),
  195. getter
  196. if (testCase.xss) {
  197. before(warnSpy.swapWarn)
  198. after(warnSpy.resetWarn)
  199. }
  200. before(function () {
  201. getter = ExpParser.parse(testCase.exp, compilerMock)
  202. })
  203. if (!testCase.xss) {
  204. it('should get correct paths', function () {
  205. if (!vars.length) return
  206. assert.strictEqual(caughtMissingPaths.length, vars.length)
  207. for (var i = 0; i < vars.length; i++) {
  208. assert.strictEqual(vars[i], caughtMissingPaths[i])
  209. }
  210. })
  211. it('getter function should return expected value', function () {
  212. var value = getter.call(testCase.vm)
  213. assert.strictEqual(value, testCase.expectedValue)
  214. })
  215. } else {
  216. it('should return undefined getter', function () {
  217. assert.strictEqual(getter, undefined)
  218. })
  219. it('should have warned', function () {
  220. assert.ok(warnSpy.warned)
  221. })
  222. }
  223. })
  224. }
  225. function noop () {}
  226. function mockCompiler (opts) {
  227. var mock = {
  228. createBinding: noop,
  229. hasKey: noop,
  230. vm: {
  231. $compiler: {
  232. bindings: {},
  233. createBinding: noop
  234. },
  235. $data: {}
  236. }
  237. }
  238. for (var key in opts) {
  239. mock[key] = opts[key]
  240. }
  241. return mock
  242. }
  243. })