compile_spec.js 19 KB

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