compile_spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. var transclude = require('../../../../src/compiler/transclude')
  7. if (_.inBrowser) {
  8. describe('Compile', function () {
  9. var vm, el, data, directiveTeardown
  10. beforeEach(function () {
  11. // We mock vms here so we can assert what the generated
  12. // linker functions do.
  13. el = document.createElement('div')
  14. data = {}
  15. directiveTeardown = jasmine.createSpy()
  16. vm = {
  17. _directives: [],
  18. _bindDir: function (name) {
  19. this._directives.push({
  20. name: name,
  21. _teardown: directiveTeardown
  22. })
  23. },
  24. $set: jasmine.createSpy(),
  25. $eval: function (value) {
  26. return data[value]
  27. },
  28. $interpolate: function (value) {
  29. return data[value]
  30. },
  31. $parent: {
  32. _directives: [],
  33. $get: function (v) {
  34. return 'from parent: ' + v
  35. }
  36. }
  37. }
  38. spyOn(vm, '_bindDir').and.callThrough()
  39. spyOn(vm, '$eval').and.callThrough()
  40. spyOn(vm, '$interpolate').and.callThrough()
  41. spyOn(_, 'warn')
  42. })
  43. it('normal directives', function () {
  44. el.setAttribute('v-a', 'b')
  45. el.innerHTML = '<p v-a="a" v-b="b">hello</p><div v-b="b"></div>'
  46. var defA = { priority: 1 }
  47. var defB = { priority: 2 }
  48. var descriptorA = dirParser.parse('a')[0]
  49. var descriptorB = dirParser.parse('b')[0]
  50. var options = merge(Vue.options, {
  51. directives: {
  52. a: defA,
  53. b: defB
  54. }
  55. })
  56. var linker = compile(el, options)
  57. expect(typeof linker).toBe('function')
  58. linker(vm, el)
  59. expect(vm._bindDir.calls.count()).toBe(4)
  60. expect(vm._bindDir).toHaveBeenCalledWith('a', el, descriptorB, defA, undefined)
  61. expect(vm._bindDir).toHaveBeenCalledWith('a', el.firstChild, descriptorA, defA, undefined)
  62. expect(vm._bindDir).toHaveBeenCalledWith('b', el.firstChild, descriptorB, defB, undefined)
  63. expect(vm._bindDir).toHaveBeenCalledWith('b', el.lastChild, descriptorB, defB, undefined)
  64. // check the priority sorting
  65. // the "b" on the firstNode should be called first!
  66. expect(vm._bindDir.calls.argsFor(1)[0]).toBe('b')
  67. })
  68. it('text interpolation', function () {
  69. data.b = 'yeah'
  70. el.innerHTML = '{{a}} and {{*b}}'
  71. var def = Vue.options.directives.text
  72. var linker = compile(el, Vue.options)
  73. linker(vm, el)
  74. // expect 1 call because one-time bindings do not generate a directive.
  75. expect(vm._bindDir.calls.count()).toBe(1)
  76. var args = vm._bindDir.calls.argsFor(0)
  77. expect(args[0]).toBe('text')
  78. // skip the node because it's generated in the linker fn via cloneNode
  79. expect(args[2]).toBe(dirParser.parse('a')[0])
  80. expect(args[3]).toBe(def)
  81. // expect $eval to be called during onetime
  82. expect(vm.$eval).toHaveBeenCalledWith('b')
  83. // {{a}} is mocked so it's a space.
  84. // but we want to make sure {{*b}} worked.
  85. expect(el.innerHTML).toBe(' and yeah')
  86. })
  87. it('inline html and partial', function () {
  88. data.html = 'yoyoyo'
  89. el.innerHTML = '{{{html}}} {{{*html}}} {{>partial}}'
  90. var htmlDef = Vue.options.directives.html
  91. var partialDef = Vue.options.directives.partial
  92. var htmlDesc = dirParser.parse('html')[0]
  93. var partialDesc = dirParser.parse('partial')[0]
  94. var linker = compile(el, Vue.options)
  95. linker(vm, el)
  96. expect(vm._bindDir.calls.count()).toBe(2)
  97. var htmlArgs = vm._bindDir.calls.argsFor(0)
  98. expect(htmlArgs[0]).toBe('html')
  99. expect(htmlArgs[2]).toBe(htmlDesc)
  100. expect(htmlArgs[3]).toBe(htmlDef)
  101. var partialArgs = vm._bindDir.calls.argsFor(1)
  102. expect(partialArgs[0]).toBe('partial')
  103. expect(partialArgs[2]).toBe(partialDesc)
  104. expect(partialArgs[3]).toBe(partialDef)
  105. expect(vm.$eval).toHaveBeenCalledWith('html')
  106. // with placeholder comments & interpolated one-time html
  107. expect(el.innerHTML).toBe('<!--v-html--> yoyoyo <!--v-partial-->')
  108. })
  109. it('terminal directives', function () {
  110. el.innerHTML =
  111. '<div v-repeat="items"><p v-a="b"></p></div>' + // v-repeat
  112. '<div v-pre><p v-a="b"></p></div>' // v-pre
  113. var def = Vue.options.directives.repeat
  114. var descriptor = dirParser.parse('items')[0]
  115. var linker = compile(el, Vue.options)
  116. linker(vm, el)
  117. // expect 1 call because terminal should return early and let
  118. // the directive handle the rest.
  119. expect(vm._bindDir.calls.count()).toBe(1)
  120. expect(vm._bindDir).toHaveBeenCalledWith('repeat', el.firstChild, descriptor, def, undefined)
  121. })
  122. it('custom element components', function () {
  123. var options = merge(Vue.options, {
  124. components: {
  125. 'my-component': {}
  126. }
  127. })
  128. el.innerHTML = '<my-component><div v-a="b"></div></my-component>'
  129. var def = Vue.options.directives.component
  130. var descriptor = dirParser.parse('my-component')[0]
  131. var linker = compile(el, options)
  132. linker(vm, el)
  133. expect(vm._bindDir.calls.count()).toBe(1)
  134. expect(vm._bindDir).toHaveBeenCalledWith('component', el.firstChild, descriptor, def, undefined)
  135. expect(_.warn).not.toHaveBeenCalled()
  136. })
  137. it('attribute interpolation', function () {
  138. data['{{*b}}'] = 'B'
  139. el.innerHTML = '<div a="{{a}}" b="{{*b}}"></div>'
  140. var def = Vue.options.directives.attr
  141. var descriptor = dirParser.parse('a:a')[0]
  142. var linker = compile(el, Vue.options)
  143. linker(vm, el)
  144. expect(vm._bindDir.calls.count()).toBe(1)
  145. expect(vm._bindDir).toHaveBeenCalledWith('attr', el.firstChild, descriptor, def)
  146. expect(el.firstChild.getAttribute('b')).toBe('B')
  147. })
  148. it('param attributes', function () {
  149. var options = merge(Vue.options, {
  150. _asComponent: true,
  151. props: [
  152. 'a',
  153. 'data-some-attr',
  154. 'some-other-attr',
  155. 'multiple-attrs',
  156. 'onetime',
  157. 'with-filter',
  158. 'camelCase'
  159. ]
  160. })
  161. var def = Vue.options.directives['with']
  162. el.setAttribute('a', '1')
  163. el.setAttribute('data-some-attr', '{{a}}')
  164. el.setAttribute('some-other-attr', '2')
  165. el.setAttribute('multiple-attrs', 'a {{b}} c')
  166. el.setAttribute('onetime', '{{*a}}')
  167. el.setAttribute('with-filter', '{{a | filter}}')
  168. transclude(el, options)
  169. var linker = compile(el, options)
  170. linker(vm, el)
  171. // should skip literals and one-time bindings
  172. expect(vm._bindDir.calls.count()).toBe(3)
  173. // data-some-attr
  174. var args = vm._bindDir.calls.argsFor(0)
  175. expect(args[0]).toBe('with')
  176. expect(args[1]).toBe(null)
  177. expect(args[2].arg).toBe('someAttr')
  178. expect(args[2].expression).toBe('a')
  179. expect(args[3]).toBe(def)
  180. // multiple-attrs
  181. args = vm._bindDir.calls.argsFor(1)
  182. expect(args[0]).toBe('with')
  183. expect(args[1]).toBe(null)
  184. expect(args[2].arg).toBe('multipleAttrs')
  185. expect(args[2].expression).toBe('"a "+(b)+" c"')
  186. expect(args[3]).toBe(def)
  187. // with-filter
  188. args = vm._bindDir.calls.argsFor(2)
  189. expect(args[0]).toBe('with')
  190. expect(args[1]).toBe(null)
  191. expect(args[2].arg).toBe('withFilter')
  192. expect(args[2].expression).toBe('this._applyFilter("filter",[a])')
  193. expect(args[3]).toBe(def)
  194. // camelCase should've warn
  195. expect(_.warn.calls.count()).toBe(1)
  196. // literal and one time should've called vm.$set
  197. expect(vm.$set).toHaveBeenCalledWith('a', '1')
  198. expect(vm.$set).toHaveBeenCalledWith('onetime', 'from parent: a')
  199. expect(vm.$set).toHaveBeenCalledWith('someOtherAttr', '2')
  200. })
  201. it('DocumentFragment', function () {
  202. var frag = document.createDocumentFragment()
  203. frag.appendChild(el)
  204. var el2 = document.createElement('div')
  205. frag.appendChild(el2)
  206. el.innerHTML = '{{*a}}'
  207. el2.innerHTML = '{{*b}}'
  208. data.a = 'A'
  209. data.b = 'B'
  210. var linker = compile(frag, Vue.options)
  211. linker(vm, frag)
  212. expect(el.innerHTML).toBe('A')
  213. expect(el2.innerHTML).toBe('B')
  214. })
  215. it('partial compilation', function () {
  216. el.innerHTML = '<div v-attr="test:abc">{{bcd}}<p v-show="ok"></p></div>'
  217. var linker = compile(el, Vue.options, true)
  218. var decompile = linker(vm, el)
  219. expect(vm._directives.length).toBe(3)
  220. decompile()
  221. expect(directiveTeardown.calls.count()).toBe(3)
  222. expect(vm._directives.length).toBe(0)
  223. })
  224. it('skip script tags', function () {
  225. el.innerHTML = '<script type="x/template">{{test}}</script>'
  226. var linker = compile(el, Vue.options)
  227. linker(vm, el)
  228. expect(vm._bindDir.calls.count()).toBe(0)
  229. })
  230. it('should handle nested transclusions', function (done) {
  231. vm = new Vue({
  232. el: el,
  233. template:
  234. '<div v-component="a">' +
  235. '<div v-component="b">' +
  236. '<div v-repeat="list">{{$value}}</div>' +
  237. '</div>' +
  238. '</div>',
  239. data: {
  240. list: [1,2]
  241. },
  242. components: {
  243. a: { template: '<content></content>' },
  244. b: { template: '<content></content>' }
  245. }
  246. })
  247. expect(el.innerHTML).toBe(
  248. '<div><div>' +
  249. '<div>1</div><div>2</div><!--v-repeat-->' +
  250. '</div><!--v-component-->' +
  251. '</div><!--v-component-->'
  252. )
  253. vm.list.push(3)
  254. _.nextTick(function () {
  255. expect(el.innerHTML).toBe(
  256. '<div><div>' +
  257. '<div>1</div><div>2</div><div>3</div><!--v-repeat-->' +
  258. '</div><!--v-component-->' +
  259. '</div><!--v-component-->'
  260. )
  261. done()
  262. })
  263. })
  264. it('should handle container/replacer directives with same name', function () {
  265. var parentSpy = jasmine.createSpy()
  266. var childSpy = jasmine.createSpy()
  267. vm = new Vue({
  268. el: el,
  269. template:
  270. '<div v-component="a" class="a" v-on="click:test(1)"></div>',
  271. methods: {
  272. test: parentSpy
  273. },
  274. components: {
  275. a: {
  276. template: '<div class="b" v-on="click:test(2)"></div>',
  277. replace: true,
  278. methods: {
  279. test: childSpy
  280. }
  281. }
  282. }
  283. })
  284. expect(vm.$el.firstChild.className).toBe('b a')
  285. var e = document.createEvent('HTMLEvents')
  286. e.initEvent('click', true, true)
  287. vm.$el.firstChild.dispatchEvent(e)
  288. expect(parentSpy).toHaveBeenCalledWith(1)
  289. expect(childSpy).toHaveBeenCalledWith(2)
  290. })
  291. it('should remove transcluded directives from parent when unlinking (v-component)', function () {
  292. var vm = new Vue({
  293. el: el,
  294. template:
  295. '<div v-component="test">{{test}}</div>',
  296. data: {
  297. test: 'parent'
  298. },
  299. components: {
  300. test: {
  301. template: '<content></content>'
  302. }
  303. }
  304. })
  305. expect(vm.$el.textContent).toBe('parent')
  306. expect(vm._directives.length).toBe(2)
  307. expect(vm._children.length).toBe(1)
  308. vm._children[0].$destroy()
  309. expect(vm._directives.length).toBe(1)
  310. expect(vm._children.length).toBe(0)
  311. })
  312. it('should remove transcluded directives from parent when unlinking (v-if + v-component)', function (done) {
  313. var vm = new Vue({
  314. el: el,
  315. template:
  316. '<div v-if="ok">' +
  317. '<div v-component="test">{{test}}</div>' +
  318. '</div>',
  319. data: {
  320. test: 'parent',
  321. ok: true
  322. },
  323. components: {
  324. test: {
  325. template: '<content></content>'
  326. }
  327. }
  328. })
  329. expect(vm.$el.textContent).toBe('parent')
  330. expect(vm._directives.length).toBe(3)
  331. expect(vm._children.length).toBe(1)
  332. vm.ok = false
  333. _.nextTick(function () {
  334. expect(vm.$el.textContent).toBe('')
  335. expect(vm._directives.length).toBe(1)
  336. expect(vm._children.length).toBe(0)
  337. done()
  338. })
  339. })
  340. })
  341. }