compile_spec.js 15 KB

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