compile_spec.js 14 KB

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