compile_spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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', function () {
  88. data.html = '<div>yoyoyo</div>'
  89. el.innerHTML = '{{{html}}} {{{*html}}}'
  90. var htmlDef = Vue.options.directives.html
  91. var htmlDesc = dirParser.parse('html')[0]
  92. var linker = compile(el, Vue.options)
  93. linker(vm, el)
  94. expect(vm._bindDir.calls.count()).toBe(1)
  95. var htmlArgs = vm._bindDir.calls.argsFor(0)
  96. expect(htmlArgs[0]).toBe('html')
  97. expect(htmlArgs[2]).toBe(htmlDesc)
  98. expect(htmlArgs[3]).toBe(htmlDef)
  99. // with placeholder comments & interpolated one-time html
  100. expect(el.innerHTML).toBe('<!--v-html--> <div>yoyoyo</div>')
  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 linker = compile(el, options)
  123. linker(vm, el)
  124. expect(vm._bindDir.calls.count()).toBe(1)
  125. expect(vm._bindDir.calls.argsFor(0)[0]).toBe('component')
  126. expect(_.warn).not.toHaveBeenCalled()
  127. })
  128. it('attribute interpolation', function () {
  129. data['{{*b}}'] = 'B'
  130. el.innerHTML = '<div a="{{a}}" b="{{*b}}"></div>'
  131. var def = Vue.options.directives.attr
  132. var descriptor = dirParser.parse('a:a')[0]
  133. var linker = compile(el, Vue.options)
  134. linker(vm, el)
  135. expect(vm._bindDir.calls.count()).toBe(1)
  136. expect(vm._bindDir).toHaveBeenCalledWith('attr', el.firstChild, descriptor, def)
  137. expect(el.firstChild.getAttribute('b')).toBe('B')
  138. })
  139. it('props', function () {
  140. var options = merge(Vue.options, {
  141. _asComponent: true,
  142. props: [
  143. 'a',
  144. 'data-some-attr',
  145. 'some-other-attr',
  146. 'multiple-attrs',
  147. 'oneway',
  148. 'with-filter',
  149. 'camelCase'
  150. ]
  151. })
  152. var def = Vue.options.directives._prop
  153. el.setAttribute('a', '1')
  154. el.setAttribute('data-some-attr', '{{a}}')
  155. el.setAttribute('some-other-attr', '2')
  156. el.setAttribute('multiple-attrs', 'a {{b}} c')
  157. el.setAttribute('oneway', '{{*a}}')
  158. el.setAttribute('with-filter', '{{a | filter}}')
  159. transclude(el, options)
  160. var linker = compile(el, options)
  161. linker(vm, el)
  162. // should skip literals and one-time bindings
  163. expect(vm._bindDir.calls.count()).toBe(4)
  164. // data-some-attr
  165. var args = vm._bindDir.calls.argsFor(0)
  166. expect(args[0]).toBe('prop')
  167. expect(args[1]).toBe(null)
  168. expect(args[2].arg).toBe('someAttr')
  169. expect(args[2].expression).toBe('a')
  170. expect(args[3]).toBe(def)
  171. // multiple-attrs
  172. args = vm._bindDir.calls.argsFor(1)
  173. expect(args[0]).toBe('prop')
  174. expect(args[1]).toBe(null)
  175. expect(args[2].arg).toBe('multipleAttrs')
  176. expect(args[2].expression).toBe('"a "+(b)+" c"')
  177. expect(args[3]).toBe(def)
  178. // oneway
  179. args = vm._bindDir.calls.argsFor(2)
  180. expect(args[0]).toBe('prop')
  181. expect(args[1]).toBe(null)
  182. expect(args[2].arg).toBe('oneway')
  183. expect(args[2].oneWay).toBe(true)
  184. expect(args[2].expression).toBe('a')
  185. expect(args[3]).toBe(def)
  186. // with-filter
  187. args = vm._bindDir.calls.argsFor(3)
  188. expect(args[0]).toBe('prop')
  189. expect(args[1]).toBe(null)
  190. expect(args[2].arg).toBe('withFilter')
  191. expect(args[2].expression).toBe('this._applyFilter("filter",[a])')
  192. expect(args[3]).toBe(def)
  193. // camelCase should've warn
  194. expect(_.warn.calls.count()).toBe(1)
  195. // literal and one time should've called vm.$set
  196. expect(vm.$set).toHaveBeenCalledWith('a', '1')
  197. expect(vm.$set).toHaveBeenCalledWith('someOtherAttr', '2')
  198. })
  199. it('DocumentFragment', function () {
  200. var frag = document.createDocumentFragment()
  201. frag.appendChild(el)
  202. var el2 = document.createElement('div')
  203. frag.appendChild(el2)
  204. el.innerHTML = '{{*a}}'
  205. el2.innerHTML = '{{*b}}'
  206. data.a = 'A'
  207. data.b = 'B'
  208. var linker = compile(frag, Vue.options)
  209. linker(vm, frag)
  210. expect(el.innerHTML).toBe('A')
  211. expect(el2.innerHTML).toBe('B')
  212. })
  213. it('partial compilation', function () {
  214. el.innerHTML = '<div v-attr="test:abc">{{bcd}}<p v-show="ok"></p></div>'
  215. var linker = compile(el, Vue.options, true)
  216. var decompile = linker(vm, el)
  217. expect(vm._directives.length).toBe(3)
  218. decompile()
  219. expect(directiveTeardown.calls.count()).toBe(3)
  220. expect(vm._directives.length).toBe(0)
  221. })
  222. it('skip script tags', function () {
  223. el.innerHTML = '<script type="x/template">{{test}}</script>'
  224. var linker = compile(el, Vue.options)
  225. linker(vm, el)
  226. expect(vm._bindDir.calls.count()).toBe(0)
  227. })
  228. it('should handle nested transclusions', function (done) {
  229. vm = new Vue({
  230. el: el,
  231. template:
  232. '<testa>' +
  233. '<testb>' +
  234. '<div v-repeat="list">{{$value}}</div>' +
  235. '</testb>' +
  236. '</testa>',
  237. data: {
  238. list: [1,2]
  239. },
  240. components: {
  241. testa: { template: '<content></content>' },
  242. testb: { template: '<content></content>' }
  243. }
  244. })
  245. expect(el.innerHTML).toBe(
  246. '<testa><testb>' +
  247. '<div>1</div><div>2</div>' +
  248. '</testb></testa>'
  249. )
  250. vm.list.push(3)
  251. _.nextTick(function () {
  252. expect(el.innerHTML).toBe(
  253. '<testa><testb>' +
  254. '<div>1</div><div>2</div><div>3</div>' +
  255. '</testb></testa>'
  256. )
  257. done()
  258. })
  259. })
  260. it('should handle container/replacer directives with same name', function () {
  261. var parentSpy = jasmine.createSpy()
  262. var childSpy = jasmine.createSpy()
  263. vm = new Vue({
  264. el: el,
  265. template:
  266. '<test class="a" v-on="click:test(1)"></test>',
  267. methods: {
  268. test: parentSpy
  269. },
  270. components: {
  271. test: {
  272. template: '<div class="b" v-on="click:test(2)"></div>',
  273. replace: true,
  274. methods: {
  275. test: childSpy
  276. }
  277. }
  278. }
  279. })
  280. expect(vm.$el.firstChild.className).toBe('b a')
  281. var e = document.createEvent('HTMLEvents')
  282. e.initEvent('click', true, true)
  283. vm.$el.firstChild.dispatchEvent(e)
  284. expect(parentSpy).toHaveBeenCalledWith(1)
  285. expect(childSpy).toHaveBeenCalledWith(2)
  286. })
  287. it('should remove transcluded directives from parent when unlinking (v-component)', function () {
  288. var vm = new Vue({
  289. el: el,
  290. template:
  291. '<test>{{test}}</test>',
  292. data: {
  293. test: 'parent'
  294. },
  295. components: {
  296. test: {
  297. template: '<content></content>'
  298. }
  299. }
  300. })
  301. expect(vm.$el.textContent).toBe('parent')
  302. expect(vm._directives.length).toBe(2)
  303. expect(vm._children.length).toBe(1)
  304. vm._children[0].$destroy()
  305. expect(vm._directives.length).toBe(1)
  306. expect(vm._children.length).toBe(0)
  307. })
  308. it('should remove transcluded directives from parent when unlinking (v-if + v-component)', function (done) {
  309. var vm = new Vue({
  310. el: el,
  311. template:
  312. '<div v-if="ok">' +
  313. '<test>{{test}}</test>' +
  314. '</div>',
  315. data: {
  316. test: 'parent',
  317. ok: true
  318. },
  319. components: {
  320. test: {
  321. template: '<content></content>'
  322. }
  323. }
  324. })
  325. expect(vm.$el.textContent).toBe('parent')
  326. expect(vm._directives.length).toBe(3)
  327. expect(vm._children.length).toBe(1)
  328. vm.ok = false
  329. _.nextTick(function () {
  330. expect(vm.$el.textContent).toBe('')
  331. expect(vm._directives.length).toBe(1)
  332. expect(vm._children.length).toBe(0)
  333. done()
  334. })
  335. })
  336. it('element directive', function () {
  337. var vm = new Vue({
  338. el: el,
  339. template: '<test>{{a}}</test>',
  340. elementDirectives: {
  341. test: {
  342. bind: function () {
  343. this.el.setAttribute('test', '1')
  344. }
  345. }
  346. }
  347. })
  348. // should be terminal
  349. expect(el.innerHTML).toBe('<test test="1">{{a}}</test>')
  350. })
  351. })
  352. }