compile_spec.js 18 KB

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