compile_spec.js 15 KB

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