compile_spec.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. var Vue = require('../../../../src/vue')
  2. var _ = require('../../../../src/util')
  3. var compiler = require('../../../../src/compiler')
  4. var compile = compiler.compile
  5. var publicDirectives = require('../../../../src/directives/public')
  6. var internalDirectives = require('../../../../src/directives/internal')
  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 (descriptor, node) {
  21. this._directives.push({
  22. name: descriptor.name,
  23. descriptor: descriptor,
  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:hello="a" v-b="1">hello</p><div v-b.literal="hi"></div>'
  51. var defA = { priority: 1 }
  52. var defB = { priority: 2 }
  53. var options = _.mergeOptions(Vue.options, {
  54. directives: {
  55. a: defA,
  56. b: defB
  57. }
  58. })
  59. var linker = compile(el, options)
  60. expect(typeof linker).toBe('function')
  61. linker(vm, el)
  62. expect(directiveBind.calls.count()).toBe(4)
  63. expect(vm._bindDir.calls.count()).toBe(4)
  64. // check if we are in firefox, which has different
  65. // attribute interation order
  66. var isAttrReversed = el.firstChild.attributes[0].name === 'v-b'
  67. // 1
  68. var args = vm._bindDir.calls.argsFor(0)
  69. expect(args[0].name).toBe('a')
  70. expect(args[0].expression).toBe('b')
  71. expect(args[0].def).toBe(defA)
  72. expect(args[1]).toBe(el)
  73. // 2
  74. args = vm._bindDir.calls.argsFor(isAttrReversed ? 2 : 1)
  75. expect(args[0].name).toBe('a')
  76. expect(args[0].expression).toBe('a')
  77. expect(args[0].def).toBe(defA)
  78. expect(args[0].arg).toBe('hello')
  79. expect(args[1]).toBe(el.firstChild)
  80. // 3 (expression literal)
  81. args = vm._bindDir.calls.argsFor(isAttrReversed ? 1 : 2)
  82. expect(args[0].name).toBe('b')
  83. expect(args[0].expression).toBe('1')
  84. expect(args[0].def).toBe(defB)
  85. expect(args[0].literal).toBe(true)
  86. expect(args[1]).toBe(el.firstChild)
  87. // 4 (explicit literal)
  88. args = vm._bindDir.calls.argsFor(3)
  89. expect(args[0].name).toBe('b')
  90. expect(args[0].expression).toBe('hi')
  91. expect(args[0].def).toBe(defB)
  92. expect(args[0].literal).toBe(true)
  93. expect(args[1]).toBe(el.lastChild)
  94. // check the priority sorting
  95. // the "b"s should be called first!
  96. expect(directiveBind.calls.argsFor(0)[0]).toBe('b')
  97. expect(directiveBind.calls.argsFor(1)[0]).toBe('b')
  98. expect(directiveBind.calls.argsFor(2)[0]).toBe('a')
  99. expect(directiveBind.calls.argsFor(3)[0]).toBe('a')
  100. })
  101. it('v-bind shorthand', function () {
  102. el.setAttribute(':class', 'a')
  103. el.setAttribute(':style', 'b')
  104. el.setAttribute(':title', 'c')
  105. // The order of setAttribute is not guaranteed to be the same with
  106. // the order of attribute enumberation, therefore we need to save
  107. // it here!
  108. var descriptors = {
  109. ':class': {
  110. name: 'class',
  111. attr: ':class',
  112. expression: 'a',
  113. def: internalDirectives.class
  114. },
  115. ':style': {
  116. name: 'style',
  117. attr: ':style',
  118. expression: 'b',
  119. def: internalDirectives.style
  120. },
  121. ':title': {
  122. name: 'bind',
  123. attr: ':title',
  124. expression: 'c',
  125. arg: 'title',
  126. def: publicDirectives.bind
  127. }
  128. }
  129. var expects = [].map.call(el.attributes, function (attr) {
  130. return descriptors[attr.name]
  131. })
  132. var linker = compile(el, Vue.options)
  133. linker(vm, el)
  134. expect(vm._bindDir.calls.count()).toBe(3)
  135. expects.forEach(function (e, i) {
  136. var args = vm._bindDir.calls.argsFor(i)
  137. for (var key in e) {
  138. expect(args[0][key]).toBe(e[key])
  139. }
  140. expect(args[1]).toBe(el)
  141. })
  142. })
  143. it('v-on shorthand', function () {
  144. el.innerHTML = '<div @click="a++"></div>'
  145. el = el.firstChild
  146. var linker = compile(el, Vue.options)
  147. linker(vm, el)
  148. expect(vm._bindDir.calls.count()).toBe(1)
  149. var args = vm._bindDir.calls.argsFor(0)
  150. expect(args[0].name).toBe('on')
  151. expect(args[0].expression).toBe('a++')
  152. expect(args[0].arg).toBe('click')
  153. expect(args[0].def).toBe(publicDirectives.on)
  154. expect(args[1]).toBe(el)
  155. })
  156. it('text interpolation', function () {
  157. data.b = 'yeah'
  158. el.innerHTML = '{{a}} and {{*b}}'
  159. var def = Vue.options.directives.text
  160. var linker = compile(el, Vue.options)
  161. linker(vm, el)
  162. // expect 1 call because one-time bindings do not generate a directive.
  163. expect(vm._bindDir.calls.count()).toBe(1)
  164. var args = vm._bindDir.calls.argsFor(0)
  165. expect(args[0].name).toBe('text')
  166. expect(args[0].expression).toBe('a')
  167. expect(args[0].def).toBe(def)
  168. // skip the node because it's generated in the linker fn via cloneNode
  169. // expect $eval to be called during onetime
  170. expect(vm.$eval).toHaveBeenCalledWith('b')
  171. // {{a}} is mocked so it's a space.
  172. // but we want to make sure {{*b}} worked.
  173. expect(el.innerHTML).toBe(' and yeah')
  174. })
  175. it('inline html', function () {
  176. data.html = '<div>yoyoyo</div>'
  177. el.innerHTML = '{{{html}}} {{{*html}}}'
  178. var htmlDef = Vue.options.directives.html
  179. var linker = compile(el, Vue.options)
  180. linker(vm, el)
  181. expect(vm._bindDir.calls.count()).toBe(1)
  182. var htmlArgs = vm._bindDir.calls.argsFor(0)
  183. expect(htmlArgs[0].name).toBe('html')
  184. expect(htmlArgs[0].expression).toBe('html')
  185. expect(htmlArgs[0].def).toBe(htmlDef)
  186. // with placeholder comments & interpolated one-time html
  187. expect(el.innerHTML).toBe('<!--v-html--> <div>yoyoyo</div>')
  188. })
  189. it('terminal directives', function () {
  190. el.innerHTML =
  191. '<div v-for="item in items"><p v-a="b"></p></div>' + // v-for
  192. '<div v-pre><p v-a="b"></p></div>' // v-pre
  193. var def = Vue.options.directives.for
  194. var linker = compile(el, Vue.options)
  195. linker(vm, el)
  196. // expect 1 call because terminal should return early and let
  197. // the directive handle the rest.
  198. expect(vm._bindDir.calls.count()).toBe(1)
  199. var args = vm._bindDir.calls.argsFor(0)
  200. expect(args[0].name).toBe('for')
  201. expect(args[0].expression).toBe('item in items')
  202. expect(args[0].def).toBe(def)
  203. expect(args[1]).toBe(el.firstChild)
  204. })
  205. it('custom element components', function () {
  206. var options = _.mergeOptions(Vue.options, {
  207. components: {
  208. 'my-component': {}
  209. }
  210. })
  211. el.innerHTML = '<my-component><div v-a="b"></div></my-component>'
  212. var linker = compile(el, options)
  213. linker(vm, el)
  214. expect(vm._bindDir.calls.count()).toBe(1)
  215. var args = vm._bindDir.calls.argsFor(0)
  216. expect(args[0].name).toBe('component')
  217. expect(args[0].expression).toBe('my-component')
  218. expect(args[0].literal).toBe(true)
  219. expect(args[0].def).toBe(internalDirectives.component)
  220. expect(_.warn).not.toHaveBeenCalled()
  221. })
  222. it('props', function () {
  223. var bindingModes = Vue.config._propBindingModes
  224. var props = [
  225. { name: 'testNormal' },
  226. { name: 'testLiteral' },
  227. { name: 'testTwoWay' },
  228. { name: 'twoWayWarn' },
  229. { name: 'testOneTime' },
  230. { name: 'optimizeLiteral' }
  231. ]
  232. el.innerHTML = '<div ' +
  233. 'v-bind:test-normal="a" ' +
  234. 'test-literal="1" ' +
  235. ':optimize-literal="1" ' +
  236. ':test-two-way.sync="a" ' +
  237. ':two-way-warn.sync="a + 1" ' +
  238. ':test-one-time.once="a"></div>'
  239. compiler.compileAndLinkProps(vm, el.firstChild, props)
  240. expect(vm._bindDir.calls.count()).toBe(3) // skip literal and one time
  241. // literal
  242. expect(vm.testLiteral).toBe('1')
  243. expect(vm._data.testLiteral).toBe('1')
  244. expect(vm.optimizeLiteral).toBe(1)
  245. expect(vm._data.optimizeLiteral).toBe(1)
  246. // one time
  247. expect(vm.testOneTime).toBe('from parent: a')
  248. expect(vm._data.testOneTime).toBe('from parent: a')
  249. // normal
  250. var args = vm._bindDir.calls.argsFor(0)
  251. var prop = args[0].prop
  252. expect(args[0].name).toBe('prop')
  253. expect(prop.path).toBe('testNormal')
  254. expect(prop.parentPath).toBe('a')
  255. expect(prop.mode).toBe(bindingModes.ONE_WAY)
  256. // two way
  257. args = vm._bindDir.calls.argsFor(1)
  258. prop = args[0].prop
  259. expect(args[0].name).toBe('prop')
  260. expect(prop.path).toBe('testTwoWay')
  261. expect(prop.parentPath).toBe('a')
  262. expect(prop.mode).toBe(bindingModes.TWO_WAY)
  263. // two way warn
  264. expect(hasWarned(_, 'non-settable parent path')).toBe(true)
  265. })
  266. it('props on root instance', function () {
  267. // temporarily remove vm.$parent
  268. var context = vm._context
  269. vm._context = null
  270. el.setAttribute('v-bind:a', '"hi"')
  271. el.setAttribute(':b', 'hi')
  272. compiler.compileAndLinkProps(vm, el, [
  273. { name: 'a' },
  274. { name: 'b' }
  275. ])
  276. expect(vm._bindDir.calls.count()).toBe(0)
  277. expect(vm._data.a).toBe('hi')
  278. expect(hasWarned(_, 'Cannot bind dynamic prop on a root')).toBe(true)
  279. // restore parent mock
  280. vm._context = context
  281. })
  282. it('DocumentFragment', function () {
  283. var frag = document.createDocumentFragment()
  284. frag.appendChild(el)
  285. var el2 = document.createElement('div')
  286. frag.appendChild(el2)
  287. el.innerHTML = '{{*a}}'
  288. el2.innerHTML = '{{*b}}'
  289. data.a = 'A'
  290. data.b = 'B'
  291. var linker = compile(frag, Vue.options)
  292. linker(vm, frag)
  293. expect(el.innerHTML).toBe('A')
  294. expect(el2.innerHTML).toBe('B')
  295. })
  296. it('partial compilation', function () {
  297. el.innerHTML = '<div v-bind:test="abc">{{bcd}}<p v-show="ok"></p></div>'
  298. var linker = compile(el, Vue.options, true)
  299. var decompile = linker(vm, el)
  300. expect(vm._directives.length).toBe(3)
  301. decompile()
  302. expect(directiveTeardown.calls.count()).toBe(3)
  303. expect(vm._directives.length).toBe(0)
  304. })
  305. it('skip script tags', function () {
  306. el.innerHTML = '<script type="x/template">{{test}}</script>'
  307. var linker = compile(el, Vue.options)
  308. linker(vm, el)
  309. expect(vm._bindDir.calls.count()).toBe(0)
  310. })
  311. it('should handle container/replacer directives with same name', function () {
  312. var parentSpy = jasmine.createSpy()
  313. var childSpy = jasmine.createSpy()
  314. vm = new Vue({
  315. el: el,
  316. template:
  317. '<test class="a" v-on:click="test(1)"></test>',
  318. methods: {
  319. test: parentSpy
  320. },
  321. components: {
  322. test: {
  323. template: '<div class="b" v-on:click="test(2)"></div>',
  324. replace: true,
  325. methods: {
  326. test: childSpy
  327. }
  328. }
  329. }
  330. })
  331. expect(vm.$el.firstChild.className).toBe('b a')
  332. var e = document.createEvent('HTMLEvents')
  333. e.initEvent('click', true, true)
  334. vm.$el.firstChild.dispatchEvent(e)
  335. expect(parentSpy).toHaveBeenCalledWith(1)
  336. expect(childSpy).toHaveBeenCalledWith(2)
  337. })
  338. it('should teardown props and replacer directives when unlinking', function () {
  339. var vm = new Vue({
  340. el: el,
  341. template: '<test :msg="msg"></test>',
  342. data: {
  343. msg: 'hi'
  344. },
  345. components: {
  346. test: {
  347. props: ['msg'],
  348. template: '<div v-show="true"></div>',
  349. replace: true
  350. }
  351. }
  352. })
  353. var dirs = vm.$children[0]._directives
  354. expect(dirs.length).toBe(2)
  355. vm.$children[0].$destroy()
  356. var i = dirs.length
  357. while (i--) {
  358. expect(dirs[i]._bound).toBe(false)
  359. }
  360. })
  361. it('should remove parent container directives from parent when unlinking', function () {
  362. var vm = new Vue({
  363. el: el,
  364. template:
  365. '<test v-show="ok"></test>',
  366. data: {
  367. ok: true
  368. },
  369. components: {
  370. test: {
  371. template: 'hi'
  372. }
  373. }
  374. })
  375. expect(el.firstChild.style.display).toBe('')
  376. expect(vm._directives.length).toBe(2)
  377. expect(vm.$children.length).toBe(1)
  378. vm.$children[0].$destroy()
  379. expect(vm._directives.length).toBe(1)
  380. expect(vm.$children.length).toBe(0)
  381. })
  382. it('should remove transcluded directives from parent when unlinking (component)', function () {
  383. var vm = new Vue({
  384. el: el,
  385. template:
  386. '<test>{{test}}</test>',
  387. data: {
  388. test: 'parent'
  389. },
  390. components: {
  391. test: {
  392. template: '<slot></slot>'
  393. }
  394. }
  395. })
  396. expect(vm.$el.textContent).toBe('parent')
  397. expect(vm._directives.length).toBe(2)
  398. expect(vm.$children.length).toBe(1)
  399. vm.$children[0].$destroy()
  400. expect(vm._directives.length).toBe(1)
  401. expect(vm.$children.length).toBe(0)
  402. })
  403. it('should remove transcluded directives from parent when unlinking (v-if + component)', function (done) {
  404. var vm = new Vue({
  405. el: el,
  406. template:
  407. '<div v-if="ok">' +
  408. '<test>{{test}}</test>' +
  409. '</div>',
  410. data: {
  411. test: 'parent',
  412. ok: true
  413. },
  414. components: {
  415. test: {
  416. template: '<slot></slot>'
  417. }
  418. }
  419. })
  420. expect(vm.$el.textContent).toBe('parent')
  421. expect(vm._directives.length).toBe(3)
  422. expect(vm.$children.length).toBe(1)
  423. vm.ok = false
  424. _.nextTick(function () {
  425. expect(vm.$el.textContent).toBe('')
  426. expect(vm._directives.length).toBe(1)
  427. expect(vm.$children.length).toBe(0)
  428. done()
  429. })
  430. })
  431. it('element directive', function () {
  432. new Vue({
  433. el: el,
  434. template: '<test>{{a}}</test>',
  435. elementDirectives: {
  436. test: {
  437. bind: function () {
  438. this.el.setAttribute('test', '1')
  439. }
  440. }
  441. }
  442. })
  443. // should be terminal
  444. expect(el.innerHTML).toBe('<test test="1">{{a}}</test>')
  445. })
  446. it('attribute interpolation', function (done) {
  447. var vm = new Vue({
  448. el: el,
  449. template: '<div id="{{a}}" class="b {{c}} d"></div>',
  450. data: {
  451. a: 'aaa',
  452. c: 'ccc'
  453. }
  454. })
  455. expect(el.firstChild.id).toBe('aaa')
  456. expect(el.firstChild.className).toBe('b ccc d')
  457. vm.a = 'aa'
  458. vm.c = 'cc'
  459. _.nextTick(function () {
  460. expect(el.firstChild.id).toBe('aa')
  461. expect(el.firstChild.className).toBe('b cc d')
  462. done()
  463. })
  464. })
  465. it('attribute interpolation: special cases', function () {
  466. new Vue({
  467. el: el,
  468. template: '<label for="{{a}}" data-test="{{b}}"></label><form accept-charset="{{c}}"></form>',
  469. data: {
  470. a: 'aaa',
  471. b: 'bbb',
  472. c: 'UTF-8'
  473. }
  474. })
  475. expect(el.innerHTML).toBe('<label for="aaa" data-test="bbb"></label><form accept-charset="UTF-8"></form>')
  476. })
  477. it('attribute interpolation: warn invalid', function () {
  478. new Vue({
  479. el: el,
  480. template: '<div hello="{{a}}"></div>',
  481. data: {
  482. a: '123'
  483. }
  484. })
  485. expect(el.innerHTML).toBe('<div></div>')
  486. expect(hasWarned(_, 'attribute interpolation is allowed only in valid native attributes')).toBe(true)
  487. })
  488. })
  489. }