compile_spec.js 13 KB

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