compile_spec.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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('inline html', function () {
  178. data.html = '<div>yoyoyo</div>'
  179. el.innerHTML = '{{{html}}} {{{*html}}}'
  180. var htmlDef = Vue.options.directives.html
  181. var linker = compile(el, Vue.options)
  182. linker(vm, el)
  183. expect(vm._bindDir.calls.count()).toBe(1)
  184. var htmlArgs = vm._bindDir.calls.argsFor(0)
  185. expect(htmlArgs[0].name).toBe('html')
  186. expect(htmlArgs[0].expression).toBe('html')
  187. expect(htmlArgs[0].def).toBe(htmlDef)
  188. // with placeholder comments & interpolated one-time html
  189. expect(el.innerHTML).toBe('<!--v-html--> <div>yoyoyo</div>')
  190. })
  191. it('terminal directives', function () {
  192. el.innerHTML =
  193. '<div v-for="item in items"><p v-a="b"></p></div>' + // v-for
  194. '<div v-pre><p v-a="b"></p></div>' // v-pre
  195. var def = Vue.options.directives.for
  196. var linker = compile(el, Vue.options)
  197. linker(vm, el)
  198. // expect 1 call because terminal should return early and let
  199. // the directive handle the rest.
  200. expect(vm._bindDir.calls.count()).toBe(1)
  201. var args = vm._bindDir.calls.argsFor(0)
  202. expect(args[0].name).toBe('for')
  203. expect(args[0].expression).toBe('item in items')
  204. expect(args[0].def).toBe(def)
  205. expect(args[1]).toBe(el.firstChild)
  206. })
  207. it('custom element components', function () {
  208. var options = _.mergeOptions(Vue.options, {
  209. components: {
  210. 'my-component': {}
  211. }
  212. })
  213. el.innerHTML = '<my-component><div v-a="b"></div></my-component>'
  214. var linker = compile(el, options)
  215. linker(vm, el)
  216. expect(vm._bindDir.calls.count()).toBe(1)
  217. var args = vm._bindDir.calls.argsFor(0)
  218. expect(args[0].name).toBe('component')
  219. expect(args[0].expression).toBe('my-component')
  220. expect(args[0].modifiers.literal).toBe(true)
  221. expect(args[0].def).toBe(internalDirectives.component)
  222. expect(_.warn).not.toHaveBeenCalled()
  223. })
  224. it('props', function () {
  225. var bindingModes = Vue.config._propBindingModes
  226. var props = {
  227. testNormal: null,
  228. testLiteral: null,
  229. testTwoWay: null,
  230. twoWayWarn: null,
  231. testOneTime: null,
  232. optimizeLiteral: null,
  233. optimizeLiteralStr: null
  234. }
  235. el.innerHTML = '<div ' +
  236. 'v-bind:test-normal="a" ' +
  237. 'test-literal="1" ' +
  238. ':optimize-literal="1" ' +
  239. ':optimize-literal-str="\'true\'"' +
  240. ':test-two-way.sync="a" ' +
  241. ':two-way-warn.sync="a + 1" ' +
  242. ':test-one-time.once="a"></div>'
  243. compiler.compileAndLinkProps(vm, el.firstChild, props)
  244. expect(vm._bindDir.calls.count()).toBe(3) // skip literal and one time
  245. // literal
  246. expect(vm.testLiteral).toBe('1')
  247. expect(vm._data.testLiteral).toBe('1')
  248. expect(vm.optimizeLiteral).toBe(1)
  249. expect(vm._data.optimizeLiteral).toBe(1)
  250. expect(vm.optimizeLiteralStr).toBe('true')
  251. expect(vm._data.optimizeLiteralStr).toBe('true')
  252. // one time
  253. expect(vm.testOneTime).toBe('from parent: a')
  254. expect(vm._data.testOneTime).toBe('from parent: a')
  255. // normal
  256. var args = vm._bindDir.calls.argsFor(0)
  257. var prop = args[0].prop
  258. expect(args[0].name).toBe('prop')
  259. expect(prop.path).toBe('testNormal')
  260. expect(prop.parentPath).toBe('a')
  261. expect(prop.mode).toBe(bindingModes.ONE_WAY)
  262. // two way
  263. args = vm._bindDir.calls.argsFor(1)
  264. prop = args[0].prop
  265. expect(args[0].name).toBe('prop')
  266. expect(prop.path).toBe('testTwoWay')
  267. expect(prop.parentPath).toBe('a')
  268. expect(prop.mode).toBe(bindingModes.TWO_WAY)
  269. // two way warn
  270. expect(hasWarned(_, 'non-settable parent path')).toBe(true)
  271. })
  272. it('props on root instance', function () {
  273. // temporarily remove vm.$parent
  274. var context = vm._context
  275. vm._context = null
  276. el.setAttribute('v-bind:a', '"hi"')
  277. el.setAttribute(':b', 'hi')
  278. compiler.compileAndLinkProps(vm, el, { a: null, b: null })
  279. expect(vm._bindDir.calls.count()).toBe(0)
  280. expect(vm._data.a).toBe('hi')
  281. expect(hasWarned(_, 'Cannot bind dynamic prop on a root')).toBe(true)
  282. // restore parent mock
  283. vm._context = context
  284. })
  285. it('DocumentFragment', function () {
  286. var frag = document.createDocumentFragment()
  287. frag.appendChild(el)
  288. var el2 = document.createElement('div')
  289. frag.appendChild(el2)
  290. el.innerHTML = '{{*a}}'
  291. el2.innerHTML = '{{*b}}'
  292. data.a = 'A'
  293. data.b = 'B'
  294. var linker = compile(frag, Vue.options)
  295. linker(vm, frag)
  296. expect(el.innerHTML).toBe('A')
  297. expect(el2.innerHTML).toBe('B')
  298. })
  299. it('partial compilation', function () {
  300. el.innerHTML = '<div v-bind:test="abc">{{bcd}}<p v-show="ok"></p></div>'
  301. var linker = compile(el, Vue.options, true)
  302. var decompile = linker(vm, el)
  303. expect(vm._directives.length).toBe(3)
  304. decompile()
  305. expect(directiveTeardown.calls.count()).toBe(3)
  306. expect(vm._directives.length).toBe(0)
  307. })
  308. it('skip script tags', function () {
  309. el.innerHTML = '<script type="x/template">{{test}}</script>'
  310. var linker = compile(el, Vue.options)
  311. linker(vm, el)
  312. expect(vm._bindDir.calls.count()).toBe(0)
  313. })
  314. it('should handle container/replacer directives with same name', function () {
  315. var parentSpy = jasmine.createSpy()
  316. var childSpy = jasmine.createSpy()
  317. vm = new Vue({
  318. el: el,
  319. template:
  320. '<test class="a" v-on:click="test(1)"></test>',
  321. methods: {
  322. test: parentSpy
  323. },
  324. components: {
  325. test: {
  326. template: '<div class="b" v-on:click="test(2)"></div>',
  327. replace: true,
  328. methods: {
  329. test: childSpy
  330. }
  331. }
  332. }
  333. })
  334. expect(vm.$el.firstChild.className).toBe('b a')
  335. var e = document.createEvent('HTMLEvents')
  336. e.initEvent('click', true, true)
  337. vm.$el.firstChild.dispatchEvent(e)
  338. expect(parentSpy).toHaveBeenCalledWith(1)
  339. expect(childSpy).toHaveBeenCalledWith(2)
  340. })
  341. it('should teardown props and replacer directives when unlinking', function () {
  342. var vm = new Vue({
  343. el: el,
  344. template: '<test :msg="msg"></test>',
  345. data: {
  346. msg: 'hi'
  347. },
  348. components: {
  349. test: {
  350. props: ['msg'],
  351. template: '<div v-show="true"></div>',
  352. replace: true
  353. }
  354. }
  355. })
  356. var dirs = vm.$children[0]._directives
  357. expect(dirs.length).toBe(2)
  358. vm.$children[0].$destroy()
  359. var i = dirs.length
  360. while (i--) {
  361. expect(dirs[i]._bound).toBe(false)
  362. }
  363. })
  364. it('should remove parent container directives from parent when unlinking', function () {
  365. var vm = new Vue({
  366. el: el,
  367. template:
  368. '<test v-show="ok"></test>',
  369. data: {
  370. ok: true
  371. },
  372. components: {
  373. test: {
  374. template: 'hi'
  375. }
  376. }
  377. })
  378. expect(el.firstChild.style.display).toBe('')
  379. expect(vm._directives.length).toBe(2)
  380. expect(vm.$children.length).toBe(1)
  381. vm.$children[0].$destroy()
  382. expect(vm._directives.length).toBe(1)
  383. expect(vm.$children.length).toBe(0)
  384. })
  385. it('should remove transcluded directives from parent when unlinking (component)', function () {
  386. var vm = new Vue({
  387. el: el,
  388. template:
  389. '<test>{{test}}</test>',
  390. data: {
  391. test: 'parent'
  392. },
  393. components: {
  394. test: {
  395. template: '<slot></slot>'
  396. }
  397. }
  398. })
  399. expect(vm.$el.textContent).toBe('parent')
  400. expect(vm._directives.length).toBe(2)
  401. expect(vm.$children.length).toBe(1)
  402. vm.$children[0].$destroy()
  403. expect(vm._directives.length).toBe(1)
  404. expect(vm.$children.length).toBe(0)
  405. })
  406. it('should remove transcluded directives from parent when unlinking (v-if + component)', function (done) {
  407. var vm = new Vue({
  408. el: el,
  409. template:
  410. '<div v-if="ok">' +
  411. '<test>{{test}}</test>' +
  412. '</div>',
  413. data: {
  414. test: 'parent',
  415. ok: true
  416. },
  417. components: {
  418. test: {
  419. template: '<slot></slot>'
  420. }
  421. }
  422. })
  423. expect(vm.$el.textContent).toBe('parent')
  424. expect(vm._directives.length).toBe(3)
  425. expect(vm.$children.length).toBe(1)
  426. vm.ok = false
  427. _.nextTick(function () {
  428. expect(vm.$el.textContent).toBe('')
  429. expect(vm._directives.length).toBe(1)
  430. expect(vm.$children.length).toBe(0)
  431. done()
  432. })
  433. })
  434. it('element directive', function () {
  435. new Vue({
  436. el: el,
  437. template: '<test>{{a}}</test>',
  438. elementDirectives: {
  439. test: {
  440. bind: function () {
  441. this.el.setAttribute('test', '1')
  442. }
  443. }
  444. }
  445. })
  446. // should be terminal
  447. expect(el.innerHTML).toBe('<test test="1">{{a}}</test>')
  448. })
  449. it('attribute interpolation', function (done) {
  450. var vm = new Vue({
  451. el: el,
  452. template: '<div id="{{a}}" class="b {{c}} d"></div>',
  453. data: {
  454. a: 'aaa',
  455. c: 'ccc'
  456. }
  457. })
  458. expect(el.firstChild.id).toBe('aaa')
  459. expect(el.firstChild.className).toBe('b ccc d')
  460. vm.a = 'aa'
  461. vm.c = 'cc'
  462. _.nextTick(function () {
  463. expect(el.firstChild.id).toBe('aa')
  464. expect(el.firstChild.className).toBe('b cc d')
  465. done()
  466. })
  467. })
  468. it('attribute interpolation: special cases', function () {
  469. new Vue({
  470. el: el,
  471. template: '<label for="{{a}}" data-test="{{b}}"></label><form accept-charset="{{c}}"></form>',
  472. data: {
  473. a: 'aaa',
  474. b: 'bbb',
  475. c: 'UTF-8'
  476. }
  477. })
  478. expect(el.innerHTML).toBe('<label for="aaa" data-test="bbb"></label><form accept-charset="UTF-8"></form>')
  479. })
  480. it('attribute interpolation: warn invalid', function () {
  481. new Vue({
  482. el: el,
  483. template: '<div v-text="{{a}}"></div>',
  484. data: {
  485. a: '123'
  486. }
  487. })
  488. expect(el.innerHTML).toBe('<div></div>')
  489. expect(hasWarned(_, 'attribute interpolation is not allowed in Vue.js directives')).toBe(true)
  490. })
  491. it('warn directives on fragment instances', function () {
  492. new Vue({
  493. el: el,
  494. template: '<test id="hi" class="ok" :prop="123"></test>',
  495. components: {
  496. test: {
  497. replace: true,
  498. props: ['prop'],
  499. template: '{{prop}}'
  500. }
  501. }
  502. })
  503. expect(_.warn.calls.count()).toBe(1)
  504. expect(
  505. hasWarned(_, 'Attributes "id", "class" are ignored on component <test>', true) ||
  506. hasWarned(_, 'Attributes "class", "id" are ignored on component <test>')
  507. ).toBe(true)
  508. })
  509. it('should compile component container directives using correct context', function () {
  510. new Vue({
  511. el: el,
  512. directives: {
  513. test: {
  514. bind: function () {
  515. this.el.textContent = 'worked!'
  516. }
  517. }
  518. },
  519. template: '<comp v-test></comp>',
  520. components: { comp: { template: '<div></div>'}}
  521. })
  522. expect(el.textContent).toBe('worked!')
  523. expect(_.warn).not.toHaveBeenCalled()
  524. })
  525. })
  526. }