compile_spec.js 12 KB

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