compile_spec.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. var Vue = require('../../../../src/vue')
  2. var _ = require('../../../../src/util')
  3. var dirParser = require('../../../../src/parsers/directive')
  4. var compiler = require('../../../../src/compiler')
  5. var compile = compiler.compile
  6. var internalDirectives = require('../../../../src/directives/internal')
  7. if (_.inBrowser) {
  8. describe('Compile', function () {
  9. var vm, el, data, directiveBind, 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. directiveBind = jasmine.createSpy('bind')
  16. directiveTeardown = jasmine.createSpy('teardown')
  17. vm = {
  18. _data: {},
  19. _directives: [],
  20. _bindDir: function (name, node, desc, def) {
  21. this._directives.push({
  22. name: name,
  23. _def: def,
  24. _bind: function () {
  25. directiveBind(this.name)
  26. },
  27. _teardown: directiveTeardown
  28. })
  29. },
  30. $eval: function (value) {
  31. return data[value]
  32. },
  33. $interpolate: function (value) {
  34. return data[value]
  35. },
  36. _context: {
  37. _directives: [],
  38. $get: function (v) {
  39. return 'from parent: ' + v
  40. }
  41. }
  42. }
  43. spyOn(vm, '_bindDir').and.callThrough()
  44. spyOn(vm, '$eval').and.callThrough()
  45. spyOn(vm, '$interpolate').and.callThrough()
  46. spyOn(_, 'warn')
  47. })
  48. it('normal directives', function () {
  49. el.setAttribute('v-a', 'b')
  50. el.innerHTML = '<p v-a="a" v-b="b">hello</p><div v-b:="b"></div>'
  51. var defA = { priority: 1 }
  52. var defB = { priority: 2 }
  53. var descriptorA = dirParser.parse('a')
  54. var descriptorB = dirParser.parse('b')
  55. var options = _.mergeOptions(Vue.options, {
  56. directives: {
  57. a: defA,
  58. b: defB
  59. }
  60. })
  61. var linker = compile(el, options)
  62. expect(typeof linker).toBe('function')
  63. linker(vm, el)
  64. expect(directiveBind.calls.count()).toBe(4)
  65. expect(vm._bindDir.calls.count()).toBe(4)
  66. expect(vm._bindDir).toHaveBeenCalledWith('a', el, descriptorB, defA, undefined, undefined, undefined, undefined, false)
  67. expect(vm._bindDir).toHaveBeenCalledWith('a', el.firstChild, descriptorA, defA, undefined, undefined, undefined, undefined, false)
  68. expect(vm._bindDir).toHaveBeenCalledWith('b', el.firstChild, descriptorB, defB, undefined, undefined, undefined, undefined, false)
  69. expect(vm._bindDir).toHaveBeenCalledWith('b', el.lastChild, descriptorB, defB, undefined, undefined, undefined, undefined, true)
  70. // check the priority sorting
  71. // the "b"s should be called first!
  72. expect(directiveBind.calls.argsFor(0)[0]).toBe('b')
  73. expect(directiveBind.calls.argsFor(1)[0]).toBe('b')
  74. expect(directiveBind.calls.argsFor(2)[0]).toBe('a')
  75. expect(directiveBind.calls.argsFor(3)[0]).toBe('a')
  76. })
  77. it('bind- syntax', function () {
  78. el.setAttribute('bind-class', 'a')
  79. el.setAttribute('bind-style', 'b')
  80. el.setAttribute('bind-title', 'c')
  81. var descA = dirParser.parse('a')
  82. var descB = dirParser.parse('b')
  83. var descC = dirParser.parse('c')
  84. var linker = compile(el, Vue.options)
  85. linker(vm, el)
  86. expect(vm._bindDir.calls.count()).toBe(3)
  87. expect(vm._bindDir).toHaveBeenCalledWith('class', el, descA, Vue.options.directives.class, undefined, undefined, undefined, undefined, undefined)
  88. expect(vm._bindDir).toHaveBeenCalledWith('style', el, descB, Vue.options.directives.style, undefined, undefined, undefined, undefined, undefined)
  89. expect(vm._bindDir).toHaveBeenCalledWith('attr', el, descC, Vue.options.directives.attr, undefined, undefined, undefined, 'title', undefined)
  90. })
  91. it('on- syntax', function () {
  92. el.setAttribute('on-click', 'a++')
  93. var desc = dirParser.parse('a++')
  94. var linker = compile(el, Vue.options)
  95. linker(vm, el)
  96. expect(vm._bindDir.calls.count()).toBe(1)
  97. expect(vm._bindDir).toHaveBeenCalledWith('on', el, desc, Vue.options.directives.on, undefined, undefined, undefined, 'click', undefined)
  98. })
  99. it('text interpolation', function () {
  100. data.b = 'yeah'
  101. el.innerHTML = '{{a}} and {{*b}}'
  102. var def = Vue.options.directives.text
  103. var linker = compile(el, Vue.options)
  104. linker(vm, el)
  105. // expect 1 call because one-time bindings do not generate a directive.
  106. expect(vm._bindDir.calls.count()).toBe(1)
  107. var args = vm._bindDir.calls.argsFor(0)
  108. expect(args[0]).toBe('text')
  109. // skip the node because it's generated in the linker fn via cloneNode
  110. expect(args[2]).toBe(dirParser.parse('a'))
  111. expect(args[3]).toBe(def)
  112. // expect $eval to be called during onetime
  113. expect(vm.$eval).toHaveBeenCalledWith('b')
  114. // {{a}} is mocked so it's a space.
  115. // but we want to make sure {{*b}} worked.
  116. expect(el.innerHTML).toBe(' and yeah')
  117. })
  118. it('inline html', function () {
  119. data.html = '<div>yoyoyo</div>'
  120. el.innerHTML = '{{{html}}} {{{*html}}}'
  121. var htmlDef = Vue.options.directives.html
  122. var htmlDesc = dirParser.parse('html')
  123. var linker = compile(el, Vue.options)
  124. linker(vm, el)
  125. expect(vm._bindDir.calls.count()).toBe(1)
  126. var htmlArgs = vm._bindDir.calls.argsFor(0)
  127. expect(htmlArgs[0]).toBe('html')
  128. expect(htmlArgs[2]).toBe(htmlDesc)
  129. expect(htmlArgs[3]).toBe(htmlDef)
  130. // with placeholder comments & interpolated one-time html
  131. expect(el.innerHTML).toBe('<!--v-html--> <div>yoyoyo</div>')
  132. })
  133. it('terminal directives', function () {
  134. el.innerHTML =
  135. '<div v-for="item in items"><p v-a="b"></p></div>' + // v-for
  136. '<div v-pre><p v-a="b"></p></div>' // v-pre
  137. var def = Vue.options.directives.for
  138. var descriptor = dirParser.parse('item in items')
  139. var linker = compile(el, Vue.options)
  140. linker(vm, el)
  141. // expect 1 call because terminal should return early and let
  142. // the directive handle the rest.
  143. expect(vm._bindDir.calls.count()).toBe(1)
  144. expect(vm._bindDir).toHaveBeenCalledWith('for', el.firstChild, descriptor, def, undefined, undefined, undefined)
  145. })
  146. it('custom element components', function () {
  147. var options = _.mergeOptions(Vue.options, {
  148. components: {
  149. 'my-component': {}
  150. }
  151. })
  152. el.innerHTML = '<my-component><div v-a="b"></div></my-component>'
  153. var linker = compile(el, options)
  154. linker(vm, el)
  155. expect(vm._bindDir.calls.count()).toBe(1)
  156. expect(vm._bindDir.calls.argsFor(0)[0]).toBe('component')
  157. expect(_.warn).not.toHaveBeenCalled()
  158. })
  159. it('props', function () {
  160. var bindingModes = Vue.config._propBindingModes
  161. var props = [
  162. { name: 'testNormal' },
  163. { name: 'testLiteral' },
  164. { name: 'testTwoWay' },
  165. { name: 'twoWayWarn' },
  166. { name: 'testOneTime' },
  167. { name: 'optimizeLiteral' }
  168. ]
  169. el.setAttribute('bind-test-normal', 'a')
  170. el.setAttribute('test-literal', '1')
  171. el.setAttribute('bind-optimize-literal', '1')
  172. el.setAttribute('bind-test-two-way', '@a')
  173. el.setAttribute('bind-two-way-warn', '@a + 1')
  174. el.setAttribute('bind-test-one-time', '*a')
  175. compiler.compileAndLinkProps(vm, el, props)
  176. expect(vm._bindDir.calls.count()).toBe(3) // skip literal and one time
  177. // literal
  178. expect(vm.testLiteral).toBe('1')
  179. expect(vm._data.testLiteral).toBe('1')
  180. expect(vm.optimizeLiteral).toBe(1)
  181. expect(vm._data.optimizeLiteral).toBe(1)
  182. // one time
  183. expect(vm.testOneTime).toBe('from parent: a')
  184. expect(vm._data.testOneTime).toBe('from parent: a')
  185. // normal
  186. var args = vm._bindDir.calls.argsFor(0)
  187. expect(args[0]).toBe('prop')
  188. expect(args[1]).toBe(null)
  189. expect(args[2].path).toBe('testNormal')
  190. expect(args[2].parentPath).toBe('a')
  191. expect(args[2].mode).toBe(bindingModes.ONE_WAY)
  192. // two way
  193. args = vm._bindDir.calls.argsFor(1)
  194. expect(args[0]).toBe('prop')
  195. expect(args[1]).toBe(null)
  196. expect(args[2].path).toBe('testTwoWay')
  197. expect(args[2].parentPath).toBe('a')
  198. expect(args[2].mode).toBe(bindingModes.TWO_WAY)
  199. // two way warn
  200. expect(hasWarned(_, 'non-settable parent path')).toBe(true)
  201. })
  202. it('props on root instance', function () {
  203. // temporarily remove vm.$parent
  204. var context = vm._context
  205. vm._context = null
  206. el.setAttribute('bind-a', '"hi"')
  207. el.setAttribute('bind-b', 'hi')
  208. compiler.compileAndLinkProps(vm, el, [
  209. { name: 'a' },
  210. { name: 'b' }
  211. ])
  212. expect(vm._bindDir.calls.count()).toBe(0)
  213. expect(vm._data.a).toBe('hi')
  214. expect(hasWarned(_, 'Cannot bind dynamic prop on a root')).toBe(true)
  215. // restore parent mock
  216. vm._context = context
  217. })
  218. it('DocumentFragment', function () {
  219. var frag = document.createDocumentFragment()
  220. frag.appendChild(el)
  221. var el2 = document.createElement('div')
  222. frag.appendChild(el2)
  223. el.innerHTML = '{{*a}}'
  224. el2.innerHTML = '{{*b}}'
  225. data.a = 'A'
  226. data.b = 'B'
  227. var linker = compile(frag, Vue.options)
  228. linker(vm, frag)
  229. expect(el.innerHTML).toBe('A')
  230. expect(el2.innerHTML).toBe('B')
  231. })
  232. it('partial compilation', function () {
  233. el.innerHTML = '<div bind-test="abc">{{bcd}}<p v-show="ok"></p></div>'
  234. var linker = compile(el, Vue.options, true)
  235. var decompile = linker(vm, el)
  236. expect(vm._directives.length).toBe(3)
  237. decompile()
  238. expect(directiveTeardown.calls.count()).toBe(3)
  239. expect(vm._directives.length).toBe(0)
  240. })
  241. it('skip script tags', function () {
  242. el.innerHTML = '<script type="x/template">{{test}}</script>'
  243. var linker = compile(el, Vue.options)
  244. linker(vm, el)
  245. expect(vm._bindDir.calls.count()).toBe(0)
  246. })
  247. it('should handle container/replacer directives with same name', function () {
  248. var parentSpy = jasmine.createSpy()
  249. var childSpy = jasmine.createSpy()
  250. vm = new Vue({
  251. el: el,
  252. template:
  253. '<test class="a" on-click="test(1)"></test>',
  254. methods: {
  255. test: parentSpy
  256. },
  257. components: {
  258. test: {
  259. template: '<div class="b" on-click="test(2)"></div>',
  260. replace: true,
  261. methods: {
  262. test: childSpy
  263. }
  264. }
  265. }
  266. })
  267. expect(vm.$el.firstChild.className).toBe('b a')
  268. var e = document.createEvent('HTMLEvents')
  269. e.initEvent('click', true, true)
  270. vm.$el.firstChild.dispatchEvent(e)
  271. expect(parentSpy).toHaveBeenCalledWith(1)
  272. expect(childSpy).toHaveBeenCalledWith(2)
  273. })
  274. it('should teardown props and replacer directives when unlinking', function () {
  275. var vm = new Vue({
  276. el: el,
  277. template: '<test bind-msg="msg"></test>',
  278. data: {
  279. msg: 'hi'
  280. },
  281. components: {
  282. test: {
  283. props: ['msg'],
  284. template: '<div v-show="true"></div>',
  285. replace: true
  286. }
  287. }
  288. })
  289. var dirs = vm.$children[0]._directives
  290. expect(dirs.length).toBe(2)
  291. vm.$children[0].$destroy()
  292. var i = dirs.length
  293. while (i--) {
  294. expect(dirs[i]._bound).toBe(false)
  295. }
  296. })
  297. it('should remove parent container directives from parent when unlinking', function () {
  298. var vm = new Vue({
  299. el: el,
  300. template:
  301. '<test v-show="ok"></test>',
  302. data: {
  303. ok: true
  304. },
  305. components: {
  306. test: {
  307. template: 'hi'
  308. }
  309. }
  310. })
  311. expect(el.firstChild.style.display).toBe('')
  312. expect(vm._directives.length).toBe(2)
  313. expect(vm.$children.length).toBe(1)
  314. vm.$children[0].$destroy()
  315. expect(vm._directives.length).toBe(1)
  316. expect(vm.$children.length).toBe(0)
  317. })
  318. it('should remove transcluded directives from parent when unlinking (component)', function () {
  319. var vm = new Vue({
  320. el: el,
  321. template:
  322. '<test>{{test}}</test>',
  323. data: {
  324. test: 'parent'
  325. },
  326. components: {
  327. test: {
  328. template: '<slot></slot>'
  329. }
  330. }
  331. })
  332. expect(vm.$el.textContent).toBe('parent')
  333. expect(vm._directives.length).toBe(2)
  334. expect(vm.$children.length).toBe(1)
  335. vm.$children[0].$destroy()
  336. expect(vm._directives.length).toBe(1)
  337. expect(vm.$children.length).toBe(0)
  338. })
  339. it('should remove transcluded directives from parent when unlinking (v-if + component)', function (done) {
  340. var vm = new Vue({
  341. el: el,
  342. template:
  343. '<div v-if="ok">' +
  344. '<test>{{test}}</test>' +
  345. '</div>',
  346. data: {
  347. test: 'parent',
  348. ok: true
  349. },
  350. components: {
  351. test: {
  352. template: '<slot></slot>'
  353. }
  354. }
  355. })
  356. expect(vm.$el.textContent).toBe('parent')
  357. expect(vm._directives.length).toBe(3)
  358. expect(vm.$children.length).toBe(1)
  359. vm.ok = false
  360. _.nextTick(function () {
  361. expect(vm.$el.textContent).toBe('')
  362. expect(vm._directives.length).toBe(1)
  363. expect(vm.$children.length).toBe(0)
  364. done()
  365. })
  366. })
  367. it('element directive', function () {
  368. new Vue({
  369. el: el,
  370. template: '<test>{{a}}</test>',
  371. elementDirectives: {
  372. test: {
  373. bind: function () {
  374. this.el.setAttribute('test', '1')
  375. }
  376. }
  377. }
  378. })
  379. // should be terminal
  380. expect(el.innerHTML).toBe('<test test="1">{{a}}</test>')
  381. })
  382. })
  383. }