compile_spec.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. _data: {},
  18. _directives: [],
  19. _bindDir: function (name) {
  20. this._directives.push({
  21. name: name,
  22. _teardown: directiveTeardown
  23. })
  24. },
  25. $eval: function (value) {
  26. return data[value]
  27. },
  28. $interpolate: function (value) {
  29. return data[value]
  30. },
  31. _context: {
  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 bindingModes = Vue.config._propBindingModes
  141. var props = [
  142. 'a',
  143. 'data-some-attr',
  144. 'some-other-attr',
  145. 'multiple-attrs',
  146. 'onetime',
  147. 'twoway',
  148. 'with-filter',
  149. 'camelCase',
  150. 'boolean-literal'
  151. ]
  152. var def = Vue.options.directives._prop
  153. el.setAttribute('a', '1')
  154. el.setAttribute('data-some-attr', '{{a}}')
  155. el.setAttribute('some-other-attr', '2')
  156. el.setAttribute('multiple-attrs', 'a {{b}} c')
  157. el.setAttribute('onetime', '{{*a}}')
  158. el.setAttribute('twoway', '{{@a}}')
  159. el.setAttribute('with-filter', '{{a | filter}}')
  160. el.setAttribute('boolean-literal', '{{true}}')
  161. compiler.compileAndLinkProps(vm, el, props)
  162. // should skip literals and one-time bindings
  163. expect(vm._bindDir.calls.count()).toBe(4)
  164. // data-some-attr
  165. var args = vm._bindDir.calls.argsFor(0)
  166. expect(args[0]).toBe('prop')
  167. expect(args[1]).toBe(null)
  168. expect(args[2].path).toBe('someAttr')
  169. expect(args[2].parentPath).toBe('a')
  170. expect(args[2].mode).toBe(bindingModes.ONE_WAY)
  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].path).toBe('multipleAttrs')
  177. expect(args[2].parentPath).toBe('"a "+(b)+" c"')
  178. expect(args[2].mode).toBe(bindingModes.ONE_WAY)
  179. expect(args[3]).toBe(def)
  180. // two way
  181. args = vm._bindDir.calls.argsFor(2)
  182. expect(args[0]).toBe('prop')
  183. expect(args[1]).toBe(null)
  184. expect(args[2].path).toBe('twoway')
  185. expect(args[2].mode).toBe(bindingModes.TWO_WAY)
  186. expect(args[2].parentPath).toBe('a')
  187. expect(args[3]).toBe(def)
  188. // with-filter
  189. args = vm._bindDir.calls.argsFor(3)
  190. expect(args[0]).toBe('prop')
  191. expect(args[1]).toBe(null)
  192. expect(args[2].path).toBe('withFilter')
  193. expect(args[2].parentPath).toBe('this._applyFilters(a,null,[{"name":"filter"}],false)')
  194. expect(args[2].mode).toBe(bindingModes.ONE_WAY)
  195. expect(args[3]).toBe(def)
  196. // literal and one time should've been set on the _data
  197. // and numbers should be casted
  198. expect(Object.keys(vm._data).length).toBe(5)
  199. expect(vm.a).toBe(1)
  200. expect(vm._data.a).toBe(1)
  201. expect(vm.someOtherAttr).toBe(2)
  202. expect(vm._data.someOtherAttr).toBe(2)
  203. expect(vm.onetime).toBe('from parent: a')
  204. expect(vm._data.onetime).toBe('from parent: a')
  205. expect(vm.booleanLiteral).toBe('from parent: true')
  206. expect(vm._data.booleanLiteral).toBe('from parent: true')
  207. expect(vm._data.camelCase).toBeUndefined()
  208. // camelCase should've warn
  209. expect(hasWarned(_, 'using camelCase')).toBe(true)
  210. })
  211. it('props on root instance', function () {
  212. // temporarily remove vm.$parent
  213. var context = vm._context
  214. vm._context = null
  215. var def = Vue.options.directives._prop
  216. el.setAttribute('a', 'hi')
  217. el.setAttribute('b', '{{hi}}')
  218. compiler.compileAndLinkProps(vm, el, ['a', 'b'])
  219. expect(vm._bindDir.calls.count()).toBe(0)
  220. expect(vm._data.a).toBe('hi')
  221. expect(hasWarned(_, 'Cannot bind dynamic prop on a root')).toBe(true)
  222. // restore parent mock
  223. vm._context = context
  224. })
  225. it('DocumentFragment', function () {
  226. var frag = document.createDocumentFragment()
  227. frag.appendChild(el)
  228. var el2 = document.createElement('div')
  229. frag.appendChild(el2)
  230. el.innerHTML = '{{*a}}'
  231. el2.innerHTML = '{{*b}}'
  232. data.a = 'A'
  233. data.b = 'B'
  234. var linker = compile(frag, Vue.options)
  235. linker(vm, frag)
  236. expect(el.innerHTML).toBe('A')
  237. expect(el2.innerHTML).toBe('B')
  238. })
  239. it('partial compilation', function () {
  240. el.innerHTML = '<div v-attr="test:abc">{{bcd}}<p v-show="ok"></p></div>'
  241. var linker = compile(el, Vue.options, true)
  242. var decompile = linker(vm, el)
  243. expect(vm._directives.length).toBe(3)
  244. decompile()
  245. expect(directiveTeardown.calls.count()).toBe(3)
  246. expect(vm._directives.length).toBe(0)
  247. })
  248. it('skip script tags', function () {
  249. el.innerHTML = '<script type="x/template">{{test}}</script>'
  250. var linker = compile(el, Vue.options)
  251. linker(vm, el)
  252. expect(vm._bindDir.calls.count()).toBe(0)
  253. })
  254. it('should handle container/replacer directives with same name', function () {
  255. var parentSpy = jasmine.createSpy()
  256. var childSpy = jasmine.createSpy()
  257. vm = new Vue({
  258. el: el,
  259. template:
  260. '<test class="a" v-on="click:test(1)"></test>',
  261. methods: {
  262. test: parentSpy
  263. },
  264. components: {
  265. test: {
  266. template: '<div class="b" v-on="click:test(2)"></div>',
  267. replace: true,
  268. methods: {
  269. test: childSpy
  270. }
  271. }
  272. }
  273. })
  274. expect(vm.$el.firstChild.className).toBe('b a')
  275. var e = document.createEvent('HTMLEvents')
  276. e.initEvent('click', true, true)
  277. vm.$el.firstChild.dispatchEvent(e)
  278. expect(parentSpy).toHaveBeenCalledWith(1)
  279. expect(childSpy).toHaveBeenCalledWith(2)
  280. })
  281. it('should teardown props and replacer directives when unlinking', function () {
  282. var vm = new Vue({
  283. el: el,
  284. template: '<test prop="{{msg}}"></test>',
  285. data: {
  286. msg: 'hi'
  287. },
  288. components: {
  289. test: {
  290. props: ['prop'],
  291. template: '<div v-show="true"></div>',
  292. replace: true
  293. }
  294. }
  295. })
  296. var dirs = vm.$children[0]._directives
  297. expect(dirs.length).toBe(2)
  298. vm.$children[0].$destroy()
  299. var i = dirs.length
  300. while (i--) {
  301. expect(dirs[i]._bound).toBe(false)
  302. }
  303. })
  304. it('should remove parent container directives from parent when unlinking', function () {
  305. var vm = new Vue({
  306. el: el,
  307. template:
  308. '<test v-show="ok"></test>',
  309. data: {
  310. ok: true
  311. },
  312. components: {
  313. test: {
  314. template: 'hi'
  315. }
  316. }
  317. })
  318. expect(el.firstChild.style.display).toBe('')
  319. expect(vm._directives.length).toBe(2)
  320. expect(vm.$children.length).toBe(1)
  321. vm.$children[0].$destroy()
  322. expect(vm._directives.length).toBe(1)
  323. expect(vm.$children.length).toBe(0)
  324. })
  325. it('should remove transcluded directives from parent when unlinking (component)', function () {
  326. var vm = new Vue({
  327. el: el,
  328. template:
  329. '<test>{{test}}</test>',
  330. data: {
  331. test: 'parent'
  332. },
  333. components: {
  334. test: {
  335. template: '<content></content>'
  336. }
  337. }
  338. })
  339. expect(vm.$el.textContent).toBe('parent')
  340. expect(vm._directives.length).toBe(2)
  341. expect(vm.$children.length).toBe(1)
  342. vm.$children[0].$destroy()
  343. expect(vm._directives.length).toBe(1)
  344. expect(vm.$children.length).toBe(0)
  345. })
  346. it('should remove transcluded directives from parent when unlinking (v-if + component)', function (done) {
  347. var vm = new Vue({
  348. el: el,
  349. template:
  350. '<div v-if="ok">' +
  351. '<test>{{test}}</test>' +
  352. '</div>',
  353. data: {
  354. test: 'parent',
  355. ok: true
  356. },
  357. components: {
  358. test: {
  359. template: '<content></content>'
  360. }
  361. }
  362. })
  363. expect(vm.$el.textContent).toBe('parent')
  364. expect(vm._directives.length).toBe(3)
  365. expect(vm.$children.length).toBe(1)
  366. vm.ok = false
  367. _.nextTick(function () {
  368. expect(vm.$el.textContent).toBe('')
  369. expect(vm._directives.length).toBe(1)
  370. expect(vm.$children.length).toBe(0)
  371. done()
  372. })
  373. })
  374. it('element directive', function () {
  375. var vm = new Vue({
  376. el: el,
  377. template: '<test>{{a}}</test>',
  378. elementDirectives: {
  379. test: {
  380. bind: function () {
  381. this.el.setAttribute('test', '1')
  382. }
  383. }
  384. }
  385. })
  386. // should be terminal
  387. expect(el.innerHTML).toBe('<test test="1">{{a}}</test>')
  388. })
  389. })
  390. }