compile_spec.js 15 KB

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