compile_spec.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. var Vue = require('../../../../src/vue')
  2. var _ = require('../../../../src/util')
  3. var dirParser = require('../../../../src/parsers/directive')
  4. var merge = require('../../../../src/util/merge-option')
  5. var compile = require('../../../../src/compiler/compile')
  6. if (_.inBrowser) {
  7. describe('Compile', function () {
  8. var vm, el, data, directiveTeardown
  9. beforeEach(function () {
  10. // We mock vms here so we can assert what the generated
  11. // linker functions do.
  12. el = document.createElement('div')
  13. data = {}
  14. directiveTeardown = jasmine.createSpy()
  15. vm = {
  16. _directives: [],
  17. _bindDir: function (name) {
  18. this._directives.push({
  19. name: name,
  20. _teardown: directiveTeardown
  21. })
  22. },
  23. $set: jasmine.createSpy(),
  24. $eval: function (value) {
  25. return data[value]
  26. },
  27. $interpolate: function (value) {
  28. return data[value]
  29. }
  30. }
  31. spyOn(vm, '_bindDir').and.callThrough()
  32. spyOn(vm, '$eval').and.callThrough()
  33. spyOn(vm, '$interpolate').and.callThrough()
  34. spyOn(_, 'warn')
  35. })
  36. it('normal directives', function () {
  37. el.setAttribute('v-a', 'b')
  38. el.innerHTML = '<p v-a="a" v-b="b">hello</p><div v-b="b"></div>'
  39. var defA = { priority: 1 }
  40. var defB = { priority: 2 }
  41. var descriptorA = dirParser.parse('a')[0]
  42. var descriptorB = dirParser.parse('b')[0]
  43. var options = merge(Vue.options, {
  44. directives: {
  45. a: defA,
  46. b: defB
  47. }
  48. })
  49. var linker = compile(el, options)
  50. expect(typeof linker).toBe('function')
  51. linker(vm, el)
  52. expect(vm._bindDir.calls.count()).toBe(4)
  53. expect(vm._bindDir).toHaveBeenCalledWith('a', el, descriptorB, defA, undefined)
  54. expect(vm._bindDir).toHaveBeenCalledWith('a', el.firstChild, descriptorA, defA, undefined)
  55. expect(vm._bindDir).toHaveBeenCalledWith('b', el.firstChild, descriptorB, defB, undefined)
  56. expect(vm._bindDir).toHaveBeenCalledWith('b', el.lastChild, descriptorB, defB, undefined)
  57. // check the priority sorting
  58. // the "b" on the firstNode should be called first!
  59. expect(vm._bindDir.calls.argsFor(1)[0]).toBe('b')
  60. })
  61. it('text interpolation', function () {
  62. data.b = 'yeah'
  63. el.innerHTML = '{{a}} and {{*b}}'
  64. var def = Vue.options.directives.text
  65. var linker = compile(el, Vue.options)
  66. linker(vm, el)
  67. // expect 1 call because one-time bindings do not generate a directive.
  68. expect(vm._bindDir.calls.count()).toBe(1)
  69. var args = vm._bindDir.calls.argsFor(0)
  70. expect(args[0]).toBe('text')
  71. // skip the node because it's generated in the linker fn via cloneNode
  72. expect(args[2]).toBe(dirParser.parse('a')[0])
  73. expect(args[3]).toBe(def)
  74. // expect $eval to be called during onetime
  75. expect(vm.$eval).toHaveBeenCalledWith('b')
  76. // {{a}} is mocked so it's a space.
  77. // but we want to make sure {{*b}} worked.
  78. expect(el.innerHTML).toBe(' and yeah')
  79. })
  80. it('inline html and partial', function () {
  81. data.html = 'yoyoyo'
  82. el.innerHTML = '{{{html}}} {{{*html}}} {{>partial}}'
  83. var htmlDef = Vue.options.directives.html
  84. var partialDef = Vue.options.directives.partial
  85. var htmlDesc = dirParser.parse('html')[0]
  86. var partialDesc = dirParser.parse('partial')[0]
  87. var linker = compile(el, Vue.options)
  88. linker(vm, el)
  89. expect(vm._bindDir.calls.count()).toBe(2)
  90. var htmlArgs = vm._bindDir.calls.argsFor(0)
  91. expect(htmlArgs[0]).toBe('html')
  92. expect(htmlArgs[2]).toBe(htmlDesc)
  93. expect(htmlArgs[3]).toBe(htmlDef)
  94. var partialArgs = vm._bindDir.calls.argsFor(1)
  95. expect(partialArgs[0]).toBe('partial')
  96. expect(partialArgs[2]).toBe(partialDesc)
  97. expect(partialArgs[3]).toBe(partialDef)
  98. expect(vm.$eval).toHaveBeenCalledWith('html')
  99. // with placeholder comments & interpolated one-time html
  100. expect(el.innerHTML).toBe('<!--v-html--> yoyoyo <!--v-partial-->')
  101. })
  102. it('terminal directives', function () {
  103. el.innerHTML =
  104. '<div v-repeat="items"><p v-a="b"></p></div>' + // v-repeat
  105. '<div v-pre><p v-a="b"></p></div>' // v-pre
  106. var def = Vue.options.directives.repeat
  107. var descriptor = dirParser.parse('items')[0]
  108. var linker = compile(el, Vue.options)
  109. linker(vm, el)
  110. // expect 1 call because terminal should return early and let
  111. // the directive handle the rest.
  112. expect(vm._bindDir.calls.count()).toBe(1)
  113. expect(vm._bindDir).toHaveBeenCalledWith('repeat', el.firstChild, descriptor, def, undefined)
  114. })
  115. it('custom element components', function () {
  116. var options = merge(Vue.options, {
  117. components: {
  118. 'my-component': {}
  119. }
  120. })
  121. el.innerHTML = '<my-component><div v-a="b"></div></my-component>'
  122. var def = Vue.options.directives.component
  123. var descriptor = dirParser.parse('my-component')[0]
  124. var linker = compile(el, options)
  125. linker(vm, el)
  126. expect(vm._bindDir.calls.count()).toBe(1)
  127. expect(vm._bindDir).toHaveBeenCalledWith('component', el.firstChild, descriptor, def, undefined)
  128. expect(_.warn).not.toHaveBeenCalled()
  129. })
  130. it('attribute interpolation', function () {
  131. data['{{*b}}'] = 'B'
  132. el.innerHTML = '<div a="{{a}}" b="{{*b}}"></div>'
  133. var def = Vue.options.directives.attr
  134. var descriptor = dirParser.parse('a:a')[0]
  135. var linker = compile(el, Vue.options)
  136. linker(vm, el)
  137. expect(vm._bindDir.calls.count()).toBe(1)
  138. expect(vm._bindDir).toHaveBeenCalledWith('attr', el.firstChild, descriptor, def)
  139. expect(el.firstChild.getAttribute('b')).toBe('B')
  140. })
  141. it('param attributes', function () {
  142. var options = merge(Vue.options, {
  143. paramAttributes: ['a', 'data-some-attr', 'some-other-attr', 'invalid', 'camelCase']
  144. })
  145. var def = Vue.options.directives['with']
  146. el.setAttribute('a', '1')
  147. el.setAttribute('data-some-attr', '{{a}}')
  148. el.setAttribute('some-other-attr', '2')
  149. el.setAttribute('invalid', 'a {{b}} c') // invalid
  150. var linker = compile(el, options)
  151. linker(vm, el)
  152. // should skip literal & invliad
  153. expect(vm._bindDir.calls.count()).toBe(1)
  154. var args = vm._bindDir.calls.argsFor(0)
  155. expect(args[0]).toBe('with')
  156. expect(args[1]).toBe(el)
  157. expect(args[2].arg).toBe('someAttr')
  158. expect(args[3]).toBe(def)
  159. // invalid and camelCase should've warn
  160. expect(_.warn.calls.count()).toBe(2)
  161. // literal should've called vm.$set
  162. expect(vm.$set).toHaveBeenCalledWith('a', '1')
  163. expect(vm.$set).toHaveBeenCalledWith('someOtherAttr', '2')
  164. })
  165. it('DocumentFragment', function () {
  166. var frag = document.createDocumentFragment()
  167. frag.appendChild(el)
  168. var el2 = document.createElement('div')
  169. frag.appendChild(el2)
  170. el.innerHTML = '{{*a}}'
  171. el2.innerHTML = '{{*b}}'
  172. data.a = 'A'
  173. data.b = 'B'
  174. var linker = compile(frag, Vue.options)
  175. linker(vm, frag)
  176. expect(el.innerHTML).toBe('A')
  177. expect(el2.innerHTML).toBe('B')
  178. })
  179. it('partial compilation', function () {
  180. el.innerHTML = '<div v-attr="test:abc">{{bcd}}<p v-show="ok"></p></div>'
  181. var linker = compile(el, Vue.options, true)
  182. var decompile = linker(vm, el)
  183. expect(vm._directives.length).toBe(3)
  184. decompile()
  185. expect(directiveTeardown.calls.count()).toBe(3)
  186. expect(vm._directives.length).toBe(0)
  187. })
  188. it('skip script tags', function () {
  189. el.innerHTML = '<script type="x/template">{{test}}</script>'
  190. var linker = compile(el, Vue.options)
  191. linker(vm, el)
  192. expect(vm._bindDir.calls.count()).toBe(0)
  193. })
  194. it('should handle nested transclusions', function (done) {
  195. vm = new Vue({
  196. el: el,
  197. template:
  198. '<div v-component="a">' +
  199. '<div v-component="b">' +
  200. '<div v-repeat="list">{{$value}}</div>' +
  201. '</div>' +
  202. '</div>',
  203. data: {
  204. list: [1,2]
  205. },
  206. components: {
  207. a: { template: '<content></content>' },
  208. b: { template: '<content></content>' }
  209. }
  210. })
  211. expect(el.innerHTML).toBe(
  212. '<div><div>' +
  213. '<div>1</div><div>2</div><!--v-repeat-->' +
  214. '</div><!--v-component-->' +
  215. '</div><!--v-component-->'
  216. )
  217. vm.list.push(3)
  218. _.nextTick(function () {
  219. expect(el.innerHTML).toBe(
  220. '<div><div>' +
  221. '<div>1</div><div>2</div><div>3</div><!--v-repeat-->' +
  222. '</div><!--v-component-->' +
  223. '</div><!--v-component-->'
  224. )
  225. done()
  226. })
  227. })
  228. })
  229. }