compile_spec.js 14 KB

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