view.js 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document, HTMLElement */
  6. /* bender-tags: ui */
  7. 'use strict';
  8. import testUtils from '/tests/_utils/utils.js';
  9. import View from '/ckeditor5/core/ui/view.js';
  10. import Region from '/ckeditor5/core/ui/region.js';
  11. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  12. import Model from '/ckeditor5/core/ui/model.js';
  13. let TestView, view;
  14. testUtils.createSinonSandbox();
  15. describe( 'View', () => {
  16. describe( 'constructor', () => {
  17. beforeEach( () => {
  18. setTestViewClass();
  19. setTestViewInstance();
  20. } );
  21. it( 'accepts the model', () => {
  22. setTestViewInstance( { a: 'foo', b: 42 } );
  23. expect( view.model ).to.be.an.instanceof( Model );
  24. expect( view ).to.have.deep.property( 'model.a', 'foo' );
  25. expect( view ).to.have.deep.property( 'model.b', 42 );
  26. } );
  27. it( 'defines basic view properties', () => {
  28. const model = new Model();
  29. view = new View( model );
  30. expect( view.model ).to.equal( model );
  31. expect( view.t ).to.be.undefined;
  32. expect( view.regions.length ).to.equal( 0 );
  33. } );
  34. it( 'defines the locale property and the t function', () => {
  35. const model = new Model();
  36. const locale = { t() {} };
  37. view = new View( model, locale );
  38. expect( view.locale ).to.equal( locale );
  39. expect( view.t ).to.equal( locale.t );
  40. } );
  41. } );
  42. describe( 'init', () => {
  43. beforeEach( () => {
  44. setTestViewClass( () => ( {
  45. tag: 'p',
  46. children: [
  47. { tag: 'span' },
  48. { tag: 'strong' }
  49. ]
  50. } ) );
  51. } );
  52. it( 'calls child regions #init', () => {
  53. setTestViewInstance();
  54. const region1 = new Region( 'x' );
  55. const region2 = new Region( 'y' );
  56. view.register( region1, el => el );
  57. view.register( region2, el => el );
  58. const spy1 = testUtils.sinon.spy( region1, 'init' );
  59. const spy2 = testUtils.sinon.spy( region2, 'init' );
  60. view.init();
  61. sinon.assert.calledOnce( spy1 );
  62. sinon.assert.calledOnce( spy2 );
  63. } );
  64. it( 'initializes view regions with string selector', () => {
  65. setTestViewInstance();
  66. const region1 = new Region( 'x' );
  67. const region2 = new Region( 'y' );
  68. view.register( region1, 'span' );
  69. view.register( region2, 'strong' );
  70. view.init();
  71. expect( region1.element ).to.equal( view.element.firstChild );
  72. expect( region2.element ).to.equal( view.element.lastChild );
  73. } );
  74. it( 'initializes view regions with function selector', () => {
  75. setTestViewInstance();
  76. const region1 = new Region( 'x' );
  77. const region2 = new Region( 'y' );
  78. view.register( region1, el => el.firstChild );
  79. view.register( region2, el => el.lastChild );
  80. view.init();
  81. expect( region1.element ).to.equal( view.element.firstChild );
  82. expect( region2.element ).to.equal( view.element.lastChild );
  83. } );
  84. it( 'initializes view regions with boolean selector', () => {
  85. setTestViewInstance();
  86. const region1 = new Region( 'x' );
  87. const region2 = new Region( 'y' );
  88. view.register( region1, true );
  89. view.register( region2, true );
  90. view.init();
  91. expect( region1.element ).to.be.null;
  92. expect( region2.element ).to.be.null;
  93. } );
  94. } );
  95. describe( 'register', () => {
  96. beforeEach( () => {
  97. setTestViewClass();
  98. setTestViewInstance();
  99. } );
  100. it( 'should throw when first argument is neither Region instance nor string', () => {
  101. expect( () => {
  102. view.register( new Date() );
  103. } ).to.throw( CKEditorError, /ui-view-register-wrongtype/ );
  104. } );
  105. it( 'should throw when missing the selector argument', () => {
  106. expect( () => {
  107. view.register( 'x' );
  108. } ).to.throw( CKEditorError, /ui-view-register-badselector/ );
  109. } );
  110. it( 'should throw when selector argument is of a wrong type', () => {
  111. expect( () => {
  112. view.register( 'x', new Date() );
  113. } ).to.throw( CKEditorError, /ui-view-register-badselector/ );
  114. expect( () => {
  115. view.register( 'x', false );
  116. } ).to.throw( CKEditorError, /ui-view-register-badselector/ );
  117. } );
  118. it( 'should throw when overriding an existing region but without override flag set', () => {
  119. expect( () => {
  120. view.register( 'x', true );
  121. view.register( new Region( 'x' ), true );
  122. } ).to.throw( CKEditorError, /ui-view-register-override/ );
  123. } );
  124. it( 'should register a new region with region name as a first argument', () => {
  125. view.register( 'x', true );
  126. expect( view.regions.get( 'x' ) ).to.be.an.instanceof( Region );
  127. } );
  128. it( 'should register a new region with Region instance as a first argument', () => {
  129. view.register( new Region( 'y' ), true );
  130. expect( view.regions.get( 'y' ) ).to.be.an.instanceof( Region );
  131. } );
  132. it( 'should override an existing region with override flag', () => {
  133. view.template = {
  134. tag: 'div',
  135. children: [
  136. { tag: 'span' }
  137. ]
  138. };
  139. const region1 = new Region( 'x' );
  140. const region2 = new Region( 'x' );
  141. view.register( region1, true );
  142. view.register( region2, true, true );
  143. view.register( 'x', 'span', true );
  144. view.init();
  145. expect( view.regions.get( 'x' ) ).to.equal( region2 );
  146. expect( view.regions.get( 'x' ).element ).to.equal( view.element.firstChild );
  147. } );
  148. it( 'should not override an existing region with the same region with override flag', () => {
  149. const region = new Region( 'x' );
  150. const spy = testUtils.sinon.spy( view.regions, 'remove' );
  151. view.register( region, true );
  152. view.register( region, true, true );
  153. sinon.assert.notCalled( spy );
  154. } );
  155. } );
  156. describe( 'el', () => {
  157. beforeEach( createViewInstanceWithTemplate );
  158. it( 'invokes out of #template', () => {
  159. setTestViewInstance( { a: 1 } );
  160. expect( view.element ).to.be.an.instanceof( HTMLElement );
  161. expect( view.element.nodeName ).to.equal( 'A' );
  162. } );
  163. it( 'can be explicitly declared', () => {
  164. class CustomView extends View {
  165. constructor() {
  166. super();
  167. this.element = document.createElement( 'span' );
  168. }
  169. }
  170. view = new CustomView();
  171. expect( view.element ).to.be.an.instanceof( HTMLElement );
  172. } );
  173. } );
  174. describe( 'attributeBinder', () => {
  175. it( 'provides "to" and "if" interface', () => {
  176. const bind = view.attributeBinder;
  177. expect( bind ).to.have.keys( 'to', 'if' );
  178. expect( typeof bind.to ).to.equal( 'function' );
  179. expect( typeof bind.if ).to.equal( 'function' );
  180. } );
  181. describe( 'to', () => {
  182. it( 'returns an object which describes the binding', () => {
  183. setTestViewInstance( { a: 1 } );
  184. const spy = testUtils.sinon.spy();
  185. const bind = view.attributeBinder;
  186. const binding = bind.to( 'a', spy );
  187. expect( spy.called ).to.be.false;
  188. expect( binding ).to.have.keys( [ 'type', 'model', 'attribute', 'callback' ] );
  189. expect( binding.model ).to.equal( view.model );
  190. expect( binding.callback ).to.equal( spy );
  191. expect( binding.attribute ).to.equal( 'a' );
  192. } );
  193. it( 'allows binding attribute to the model – simple (HTMLElement attribute)', () => {
  194. setTestViewClass( function() {
  195. const bind = this.attributeBinder;
  196. return {
  197. tag: 'p',
  198. attributes: {
  199. 'class': bind.to( 'foo' )
  200. },
  201. children: [ 'abc' ]
  202. };
  203. } );
  204. setTestViewInstance( { foo: 'bar' } );
  205. expect( view.element.outerHTML ).to.equal( '<p class="bar">abc</p>' );
  206. view.model.foo = 'baz';
  207. expect( view.element.outerHTML ).to.equal( '<p class="baz">abc</p>' );
  208. } );
  209. it( 'allows binding attribute to the model – simple (Text Node)', () => {
  210. setTestViewClass( function() {
  211. const bind = this.attributeBinder;
  212. return {
  213. tag: 'p',
  214. children: [
  215. {
  216. text: bind.to( 'foo' )
  217. }
  218. ]
  219. };
  220. } );
  221. setTestViewInstance( { foo: 'bar' } );
  222. expect( view.element.outerHTML ).to.equal( '<p>bar</p>' );
  223. view.model.foo = 'baz';
  224. expect( view.element.outerHTML ).to.equal( '<p>baz</p>' );
  225. } );
  226. it( 'allows binding attribute to the model – value processing', () => {
  227. setTestViewClass( function() {
  228. const bind = this.attributeBinder;
  229. const callback = value => value > 0 ? 'positive' : 'negative';
  230. return {
  231. tag: 'p',
  232. attributes: {
  233. 'class': bind.to( 'foo', callback )
  234. },
  235. children: [
  236. {
  237. text: bind.to( 'foo', callback )
  238. }
  239. ]
  240. };
  241. } );
  242. setTestViewInstance( { foo: 3 } );
  243. expect( view.element.outerHTML ).to.equal( '<p class="positive">positive</p>' );
  244. view.model.foo = -7;
  245. expect( view.element.outerHTML ).to.equal( '<p class="negative">negative</p>' );
  246. } );
  247. it( 'allows binding attribute to the model – value processing (use Node)', () => {
  248. setTestViewClass( function() {
  249. const bind = this.attributeBinder;
  250. const callback = ( value, node ) => {
  251. return ( !!node.tagName && value > 0 ) ? 'HTMLElement positive' : '';
  252. };
  253. return {
  254. tag: 'p',
  255. attributes: {
  256. 'class': bind.to( 'foo', callback )
  257. },
  258. children: [
  259. {
  260. text: bind.to( 'foo', callback )
  261. }
  262. ]
  263. };
  264. } );
  265. setTestViewInstance( { foo: 3 } );
  266. expect( view.element.outerHTML ).to.equal( '<p class="HTMLElement positive"></p>' );
  267. view.model.foo = -7;
  268. expect( view.element.outerHTML ).to.equal( '<p></p>' );
  269. } );
  270. it( 'allows binding attribute to the model – custom callback', () => {
  271. setTestViewClass( function() {
  272. const bind = this.attributeBinder;
  273. return {
  274. tag: 'p',
  275. attributes: {
  276. 'class': bind.to( 'foo', ( value, el ) => {
  277. el.innerHTML = value;
  278. if ( value == 'changed' ) {
  279. return value;
  280. }
  281. } )
  282. }
  283. };
  284. } );
  285. setTestViewInstance( { foo: 'moo' } );
  286. expect( view.element.outerHTML ).to.equal( '<p class="undefined">moo</p>' );
  287. view.model.foo = 'changed';
  288. expect( view.element.outerHTML ).to.equal( '<p class="changed">changed</p>' );
  289. } );
  290. it( 'allows binding attribute to the model – array of bindings (HTMLElement attribute)', () => {
  291. setTestViewClass( function() {
  292. const bind = this.attributeBinder;
  293. return {
  294. tag: 'p',
  295. attributes: {
  296. 'class': [
  297. 'ck-class',
  298. bind.to( 'foo' ),
  299. bind.to( 'bar' ),
  300. bind.to( 'foo', value => `foo-is-${value}` ),
  301. 'ck-end'
  302. ]
  303. },
  304. children: [ 'abc' ]
  305. };
  306. } );
  307. setTestViewInstance( { foo: 'a', bar: 'b' } );
  308. expect( view.element.outerHTML ).to.equal( '<p class="ck-class a b foo-is-a ck-end">abc</p>' );
  309. view.model.foo = 'c';
  310. view.model.bar = 'd';
  311. expect( view.element.outerHTML ).to.equal( '<p class="ck-class c d foo-is-c ck-end">abc</p>' );
  312. } );
  313. it( 'allows binding attribute to the model – array of bindings (Text Node)', () => {
  314. setTestViewClass( function() {
  315. const bind = this.attributeBinder;
  316. return {
  317. tag: 'p',
  318. attributes: {
  319. },
  320. children: [
  321. {
  322. text: [
  323. 'ck-class',
  324. bind.to( 'foo' ),
  325. bind.to( 'bar' ),
  326. bind.to( 'foo', value => `foo-is-${value}` ),
  327. 'ck-end'
  328. ]
  329. }
  330. ]
  331. };
  332. } );
  333. setTestViewInstance( { foo: 'a', bar: 'b' } );
  334. expect( view.element.outerHTML ).to.equal( '<p>ck-class a b foo-is-a ck-end</p>' );
  335. view.model.foo = 'c';
  336. view.model.bar = 'd';
  337. expect( view.element.outerHTML ).to.equal( '<p>ck-class c d foo-is-c ck-end</p>' );
  338. } );
  339. it( 'allows binding attribute to the model – falsy values', () => {
  340. setTestViewClass( function() {
  341. const bind = this.attributeBinder;
  342. return {
  343. tag: 'p',
  344. attributes: {
  345. 'class': bind.to( 'foo' )
  346. },
  347. children: [ 'abc' ]
  348. };
  349. } );
  350. setTestViewInstance( { foo: 'bar' } );
  351. expect( view.element.outerHTML ).to.equal( '<p class="bar">abc</p>' );
  352. view.model.foo = false;
  353. expect( view.element.outerHTML ).to.equal( '<p class="false">abc</p>' );
  354. view.model.foo = null;
  355. expect( view.element.outerHTML ).to.equal( '<p class="null">abc</p>' );
  356. view.model.foo = undefined;
  357. expect( view.element.outerHTML ).to.equal( '<p class="undefined">abc</p>' );
  358. view.model.foo = 0;
  359. expect( view.element.outerHTML ).to.equal( '<p class="0">abc</p>' );
  360. view.model.foo = '';
  361. expect( view.element.outerHTML ).to.equal( '<p>abc</p>' );
  362. } );
  363. } );
  364. describe( 'if', () => {
  365. it( 'returns an object which describes the binding', () => {
  366. setTestViewInstance( { a: 1 } );
  367. const spy = testUtils.sinon.spy();
  368. const bind = view.attributeBinder;
  369. const binding = bind.if( 'a', 'foo', spy );
  370. expect( spy.called ).to.be.false;
  371. expect( binding ).to.have.keys( [ 'type', 'model', 'attribute', 'callback', 'valueIfTrue' ] );
  372. expect( binding.model ).to.equal( view.model );
  373. expect( binding.callback ).to.equal( spy );
  374. expect( binding.attribute ).to.equal( 'a' );
  375. expect( binding.valueIfTrue ).to.equal( 'foo' );
  376. } );
  377. it( 'allows binding attribute to the model – presence of an attribute (HTMLElement attribute)', () => {
  378. setTestViewClass( function() {
  379. const bind = this.attributeBinder;
  380. return {
  381. tag: 'p',
  382. attributes: {
  383. 'class': bind.if( 'foo' )
  384. },
  385. children: [ 'abc' ]
  386. };
  387. } );
  388. setTestViewInstance( { foo: true } );
  389. expect( view.element.outerHTML ).to.equal( '<p class="">abc</p>' );
  390. view.model.foo = false;
  391. expect( view.element.outerHTML ).to.equal( '<p>abc</p>' );
  392. view.model.foo = 'bar';
  393. expect( view.element.outerHTML ).to.equal( '<p class="">abc</p>' );
  394. } );
  395. // TODO: Is this alright? It makes sense but it's pretty useless. Text Node cannot be
  396. // removed just like an attribute of some HTMLElement.
  397. it( 'allows binding attribute to the model – presence of an attribute (Text Node)', () => {
  398. setTestViewClass( function() {
  399. const bind = this.attributeBinder;
  400. return {
  401. tag: 'p',
  402. children: [
  403. {
  404. text: bind.if( 'foo' )
  405. }
  406. ]
  407. };
  408. } );
  409. setTestViewInstance( { foo: true } );
  410. expect( view.element.outerHTML ).to.equal( '<p></p>' );
  411. view.model.foo = false;
  412. expect( view.element.outerHTML ).to.equal( '<p></p>' );
  413. view.model.foo = 'bar';
  414. expect( view.element.outerHTML ).to.equal( '<p></p>' );
  415. } );
  416. it( 'allows binding attribute to the model – value of an attribute (HTMLElement attribute)', () => {
  417. setTestViewClass( function() {
  418. const bind = this.attributeBinder;
  419. return {
  420. tag: 'p',
  421. attributes: {
  422. 'class': bind.if( 'foo', 'bar' )
  423. },
  424. children: [ 'abc' ]
  425. };
  426. } );
  427. setTestViewInstance( { foo: 'bar' } );
  428. expect( view.element.outerHTML ).to.equal( '<p class="bar">abc</p>' );
  429. view.model.foo = false;
  430. expect( view.element.outerHTML ).to.equal( '<p>abc</p>' );
  431. view.model.foo = 64;
  432. expect( view.element.outerHTML ).to.equal( '<p class="bar">abc</p>' );
  433. } );
  434. it( 'allows binding attribute to the model – value of an attribute (Text Node)', () => {
  435. setTestViewClass( function() {
  436. const bind = this.attributeBinder;
  437. return {
  438. tag: 'p',
  439. children: [
  440. {
  441. text: bind.if( 'foo', 'bar' )
  442. }
  443. ]
  444. };
  445. } );
  446. setTestViewInstance( { foo: 'bar' } );
  447. expect( view.element.outerHTML ).to.equal( '<p>bar</p>' );
  448. view.model.foo = false;
  449. expect( view.element.outerHTML ).to.equal( '<p></p>' );
  450. view.model.foo = 64;
  451. expect( view.element.outerHTML ).to.equal( '<p>bar</p>' );
  452. } );
  453. it( 'allows binding attribute to the model – value of an attribute processed by a callback', () => {
  454. setTestViewClass( function() {
  455. const bind = this.attributeBinder;
  456. return {
  457. tag: 'p',
  458. attributes: {
  459. 'class': bind.if( 'foo', 'there–is–no–foo', value => !value )
  460. },
  461. children: [ 'abc' ]
  462. };
  463. } );
  464. setTestViewInstance( { foo: 'bar' } );
  465. expect( view.element.outerHTML ).to.equal( '<p>abc</p>' );
  466. view.model.foo = false;
  467. expect( view.element.outerHTML ).to.equal( '<p class="there–is–no–foo">abc</p>' );
  468. view.model.foo = 64;
  469. expect( view.element.outerHTML ).to.equal( '<p>abc</p>' );
  470. } );
  471. it( 'allows binding attribute to the model – value of an attribute processed by a callback (use Node)', () => {
  472. setTestViewClass( function() {
  473. const bind = this.attributeBinder;
  474. return {
  475. tag: 'p',
  476. attributes: {
  477. 'class': bind.if( 'foo', 'eqls-tag-name', ( value, el ) => el.tagName === value )
  478. },
  479. children: [ 'abc' ]
  480. };
  481. } );
  482. setTestViewInstance( { foo: 'bar' } );
  483. expect( view.element.outerHTML ).to.equal( '<p>abc</p>' );
  484. view.model.foo = 'P';
  485. expect( view.element.outerHTML ).to.equal( '<p class="eqls-tag-name">abc</p>' );
  486. view.model.foo = 64;
  487. expect( view.element.outerHTML ).to.equal( '<p>abc</p>' );
  488. } );
  489. it( 'allows binding attribute to the model – falsy values', () => {
  490. setTestViewClass( function() {
  491. const bind = this.attributeBinder;
  492. return {
  493. tag: 'p',
  494. attributes: {
  495. 'class': bind.if( 'foo', 'foo-is-set' )
  496. },
  497. children: [ 'abc' ]
  498. };
  499. } );
  500. setTestViewInstance( { foo: 'bar' } );
  501. expect( view.element.outerHTML ).to.equal( '<p class="foo-is-set">abc</p>' );
  502. view.model.foo = false;
  503. expect( view.element.outerHTML ).to.equal( '<p>abc</p>' );
  504. view.model.foo = null;
  505. expect( view.element.outerHTML ).to.equal( '<p>abc</p>' );
  506. view.model.foo = undefined;
  507. expect( view.element.outerHTML ).to.equal( '<p>abc</p>' );
  508. view.model.foo = '';
  509. expect( view.element.outerHTML ).to.equal( '<p>abc</p>' );
  510. view.model.foo = 0;
  511. expect( view.element.outerHTML ).to.equal( '<p>abc</p>' );
  512. } );
  513. } );
  514. } );
  515. describe( 'on', () => {
  516. it( 'accepts plain binding', () => {
  517. const spy = testUtils.sinon.spy();
  518. setTestViewClass( function() {
  519. return {
  520. tag: 'p',
  521. on: {
  522. x: 'a',
  523. }
  524. };
  525. } );
  526. setTestViewInstance();
  527. view.on( 'a', spy );
  528. dispatchEvent( view.element, 'x' );
  529. sinon.assert.calledWithExactly( spy,
  530. sinon.match.has( 'name', 'a' ),
  531. sinon.match.has( 'target', view.element )
  532. );
  533. } );
  534. it( 'accepts an array of event bindings', () => {
  535. const spy1 = testUtils.sinon.spy();
  536. const spy2 = testUtils.sinon.spy();
  537. setTestViewClass( function() {
  538. return {
  539. tag: 'p',
  540. on: {
  541. x: [ 'a', 'b' ]
  542. }
  543. };
  544. } );
  545. setTestViewInstance();
  546. view.on( 'a', spy1 );
  547. view.on( 'b', spy2 );
  548. dispatchEvent( view.element, 'x' );
  549. sinon.assert.calledWithExactly( spy1,
  550. sinon.match.has( 'name', 'a' ),
  551. sinon.match.has( 'target', view.element )
  552. );
  553. sinon.assert.calledWithExactly( spy2,
  554. sinon.match.has( 'name', 'b' ),
  555. sinon.match.has( 'target', view.element )
  556. );
  557. } );
  558. it( 'accepts DOM selectors', () => {
  559. const spy1 = testUtils.sinon.spy();
  560. const spy2 = testUtils.sinon.spy();
  561. const spy3 = testUtils.sinon.spy();
  562. setTestViewClass( function() {
  563. return {
  564. tag: 'p',
  565. children: [
  566. {
  567. tag: 'span',
  568. attributes: {
  569. 'class': 'y',
  570. },
  571. on: {
  572. 'test@p': 'c'
  573. }
  574. },
  575. {
  576. tag: 'div',
  577. children: [
  578. {
  579. tag: 'span',
  580. attributes: {
  581. 'class': 'y',
  582. }
  583. }
  584. ],
  585. }
  586. ],
  587. on: {
  588. 'test@.y': 'a',
  589. 'test@div': 'b'
  590. }
  591. };
  592. } );
  593. setTestViewInstance();
  594. view.on( 'a', spy1 );
  595. view.on( 'b', spy2 );
  596. view.on( 'c', spy3 );
  597. // Test "test@p".
  598. dispatchEvent( view.element, 'test' );
  599. sinon.assert.callCount( spy1, 0 );
  600. sinon.assert.callCount( spy2, 0 );
  601. sinon.assert.callCount( spy3, 0 );
  602. // Test "test@.y".
  603. dispatchEvent( view.element.firstChild, 'test' );
  604. expect( spy1.firstCall.calledWithExactly(
  605. sinon.match.has( 'name', 'a' ),
  606. sinon.match.has( 'target', view.element.firstChild )
  607. ) ).to.be.true;
  608. sinon.assert.callCount( spy2, 0 );
  609. sinon.assert.callCount( spy3, 0 );
  610. // Test "test@div".
  611. dispatchEvent( view.element.lastChild, 'test' );
  612. sinon.assert.callCount( spy1, 1 );
  613. expect( spy2.firstCall.calledWithExactly(
  614. sinon.match.has( 'name', 'b' ),
  615. sinon.match.has( 'target', view.element.lastChild )
  616. ) ).to.be.true;
  617. sinon.assert.callCount( spy3, 0 );
  618. // Test "test@.y".
  619. dispatchEvent( view.element.lastChild.firstChild, 'test' );
  620. expect( spy1.secondCall.calledWithExactly(
  621. sinon.match.has( 'name', 'a' ),
  622. sinon.match.has( 'target', view.element.lastChild.firstChild )
  623. ) ).to.be.true;
  624. sinon.assert.callCount( spy2, 1 );
  625. sinon.assert.callCount( spy3, 0 );
  626. } );
  627. it( 'accepts function callbacks', () => {
  628. const spy1 = testUtils.sinon.spy();
  629. const spy2 = testUtils.sinon.spy();
  630. setTestViewClass( function() {
  631. return {
  632. tag: 'p',
  633. children: [
  634. {
  635. tag: 'span'
  636. }
  637. ],
  638. on: {
  639. x: spy1,
  640. 'y@span': [ spy2, 'c' ],
  641. }
  642. };
  643. } );
  644. setTestViewInstance();
  645. dispatchEvent( view.element, 'x' );
  646. dispatchEvent( view.element.firstChild, 'y' );
  647. sinon.assert.calledWithExactly( spy1,
  648. sinon.match.has( 'target', view.element )
  649. );
  650. sinon.assert.calledWithExactly( spy2,
  651. sinon.match.has( 'target', view.element.firstChild )
  652. );
  653. } );
  654. it( 'supports event delegation', () => {
  655. const spy = testUtils.sinon.spy();
  656. setTestViewClass( function() {
  657. return {
  658. tag: 'p',
  659. children: [
  660. {
  661. tag: 'span'
  662. }
  663. ],
  664. on: {
  665. x: 'a',
  666. }
  667. };
  668. } );
  669. setTestViewInstance();
  670. view.on( 'a', spy );
  671. dispatchEvent( view.element.firstChild, 'x' );
  672. sinon.assert.calledWithExactly( spy,
  673. sinon.match.has( 'name', 'a' ),
  674. sinon.match.has( 'target', view.element.firstChild )
  675. );
  676. } );
  677. it( 'works for future elements', () => {
  678. const spy = testUtils.sinon.spy();
  679. setTestViewClass( function() {
  680. return {
  681. tag: 'p',
  682. on: {
  683. 'test@div': 'a'
  684. }
  685. };
  686. } );
  687. setTestViewInstance();
  688. view.on( 'a', spy );
  689. const div = document.createElement( 'div' );
  690. view.element.appendChild( div );
  691. dispatchEvent( div, 'test' );
  692. sinon.assert.calledWithExactly( spy, sinon.match.has( 'name', 'a' ), sinon.match.has( 'target', div ) );
  693. } );
  694. } );
  695. describe( 'destroy', () => {
  696. beforeEach( createViewInstanceWithTemplate );
  697. it( 'should destroy the view', () => {
  698. view.destroy();
  699. expect( view.model ).to.be.null;
  700. expect( view.regions ).to.be.null;
  701. expect( view.template ).to.be.null;
  702. expect( view.locale ).to.be.null;
  703. expect( view.t ).to.be.null;
  704. } );
  705. it( 'detaches the element from DOM', () => {
  706. const elRef = view.element;
  707. document.createElement( 'div' ).appendChild( view.element );
  708. view.destroy();
  709. expect( elRef.parentNode ).to.be.null;
  710. } );
  711. it( 'destroys child regions', () => {
  712. const region = new Region( 'x' );
  713. const spy = testUtils.sinon.spy( region, 'destroy' );
  714. const regionsRef = view.regions;
  715. const regionViewsRef = region.views;
  716. view.register( region, true );
  717. view.regions.get( 'x' ).views.add( new View() );
  718. view.destroy();
  719. expect( regionsRef.length ).to.equal( 0 );
  720. expect( regionViewsRef.length ).to.equal( 0 );
  721. expect( spy.calledOnce ).to.be.true;
  722. } );
  723. it( 'detaches bound model listeners', () => {
  724. setTestViewClass( function() {
  725. const bind = this.attributeBinder;
  726. return {
  727. tag: 'p',
  728. children: [
  729. { text: bind.to( 'foo' ) }
  730. ]
  731. };
  732. } );
  733. setTestViewInstance( { foo: 'bar' } );
  734. const modelRef = view.model;
  735. const elRef = view.element;
  736. expect( view.element.outerHTML ).to.equal( '<p>bar</p>' );
  737. modelRef.foo = 'baz';
  738. expect( view.element.outerHTML ).to.equal( '<p>baz</p>' );
  739. view.destroy();
  740. modelRef.foo = 'abc';
  741. expect( elRef.outerHTML ).to.equal( '<p>baz</p>' );
  742. } );
  743. it( 'destroy a template–less view', () => {
  744. view = new View();
  745. expect( () => {
  746. view.destroy();
  747. } ).to.not.throw();
  748. } );
  749. } );
  750. describe( 'applyTemplateToElement', () => {
  751. beforeEach( () => {
  752. setTestViewClass();
  753. setTestViewInstance( { a: 'foo', b: 42 } );
  754. } );
  755. it( 'should initialize attribute bindings', () => {
  756. const el = document.createElement( 'div' );
  757. const bind = view.attributeBinder;
  758. view.applyTemplateToElement( el, {
  759. tag: 'div',
  760. attributes: {
  761. class: bind.to( 'b' ),
  762. id: 'foo',
  763. checked: 'checked'
  764. }
  765. } );
  766. expect( el.outerHTML ).to.equal( '<div class="42" id="foo" checked="checked"></div>' );
  767. view.model.b = 64;
  768. expect( el.outerHTML ).to.equal( '<div class="64" id="foo" checked="checked"></div>' );
  769. } );
  770. it( 'should initialize DOM listeners', () => {
  771. const el = document.createElement( 'div' );
  772. const spy = testUtils.sinon.spy();
  773. view.applyTemplateToElement( el, {
  774. tag: 'div',
  775. on: {
  776. click: spy
  777. }
  778. } );
  779. document.body.appendChild( el );
  780. dispatchEvent( el, 'click' );
  781. sinon.assert.calledOnce( spy );
  782. view.stopListening( el, 'click' );
  783. dispatchEvent( el, 'click' );
  784. sinon.assert.calledOnce( spy );
  785. } );
  786. it( 'should work for deep DOM structure', () => {
  787. const el = document.createElement( 'div' );
  788. const childA = document.createElement( 'a' );
  789. const childB = document.createElement( 'b' );
  790. childA.textContent = 'anchor';
  791. childB.textContent = 'bold';
  792. el.appendChild( childA );
  793. el.appendChild( childB );
  794. expect( el.outerHTML ).to.equal( '<div><a>anchor</a><b>bold</b></div>' );
  795. const spy1 = testUtils.sinon.spy();
  796. const spy2 = testUtils.sinon.spy();
  797. const spy3 = testUtils.sinon.spy();
  798. const bind = view.attributeBinder;
  799. view.applyTemplateToElement( el, {
  800. tag: 'div',
  801. children: [
  802. {
  803. tag: 'a',
  804. on: {
  805. keyup: spy2
  806. },
  807. attributes: {
  808. class: bind.to( 'b', b => 'applied-A-' + b ),
  809. id: 'applied-A'
  810. },
  811. children: [ 'Text applied to childA.' ]
  812. },
  813. {
  814. tag: 'b',
  815. on: {
  816. keydown: spy3
  817. },
  818. attributes: {
  819. class: bind.to( 'b', b => 'applied-B-' + b ),
  820. id: 'applied-B'
  821. },
  822. children: [ 'Text applied to childB.' ]
  823. },
  824. 'Text which is not to be applied because it does NOT exist in original element.'
  825. ],
  826. on: {
  827. 'mouseover@a': spy1
  828. },
  829. attributes: {
  830. id: bind.to( 'a', a => a.toUpperCase() ),
  831. class: bind.to( 'b', b => 'applied-parent-' + b )
  832. }
  833. } );
  834. expect( el.outerHTML ).to.equal( '<div id="FOO" class="applied-parent-42">' +
  835. '<a class="applied-A-42" id="applied-A">Text applied to childA.</a>' +
  836. '<b class="applied-B-42" id="applied-B">Text applied to childB.</b>' +
  837. '</div>' );
  838. view.model.b = 16;
  839. expect( el.outerHTML ).to.equal( '<div id="FOO" class="applied-parent-16">' +
  840. '<a class="applied-A-16" id="applied-A">Text applied to childA.</a>' +
  841. '<b class="applied-B-16" id="applied-B">Text applied to childB.</b>' +
  842. '</div>' );
  843. document.body.appendChild( el );
  844. // Test "mouseover@a".
  845. dispatchEvent( el, 'mouseover' );
  846. dispatchEvent( childA, 'mouseover' );
  847. // Test "keyup".
  848. dispatchEvent( childA, 'keyup' );
  849. // Test "keydown".
  850. dispatchEvent( childB, 'keydown' );
  851. sinon.assert.calledOnce( spy1 );
  852. sinon.assert.calledOnce( spy2 );
  853. sinon.assert.calledOnce( spy3 );
  854. } );
  855. it( 're–uses a template definition object without redundant re–definition of "on" listener attachers', () => {
  856. const el1 = document.createElement( 'div' );
  857. const el2 = document.createElement( 'div' );
  858. const spy = testUtils.sinon.spy();
  859. const def = {
  860. tag: 'div',
  861. on: {
  862. click: spy
  863. }
  864. };
  865. view.applyTemplateToElement( el1, def );
  866. const attacherFn = def.on.click;
  867. view.applyTemplateToElement( el2, def );
  868. // Attacher function didn't change because it's still the same View instance.
  869. expect( attacherFn ).to.equal( def.on.click );
  870. expect( def.on._listenerView ).to.equal( view );
  871. document.body.appendChild( el1 );
  872. document.body.appendChild( el2 );
  873. dispatchEvent( el1, 'click' );
  874. sinon.assert.calledOnce( spy );
  875. dispatchEvent( el2, 'click' );
  876. sinon.assert.calledTwice( spy );
  877. } );
  878. it( 'shares a template definition between View instances – event listeners', () => {
  879. const el = document.createElement( 'div' );
  880. const spy = testUtils.sinon.spy();
  881. const view1 = new View();
  882. const view2 = new View();
  883. const def = {
  884. tag: 'div',
  885. on: {
  886. click: spy
  887. }
  888. };
  889. document.body.appendChild( el );
  890. view1.applyTemplateToElement( el, def );
  891. expect( def.on._listenerView ).to.equal( view1 );
  892. dispatchEvent( el, 'click' );
  893. sinon.assert.calledOnce( spy );
  894. // Use another view1 to see if attachers are re–defined.
  895. view2.applyTemplateToElement( el, def );
  896. expect( def.on._listenerView ).to.equal( view2 );
  897. dispatchEvent( el, 'click' );
  898. // Spy was called for view1 (1st dispatch), then for view1 and view2 (2nd dispatch).
  899. sinon.assert.calledThrice( spy );
  900. } );
  901. } );
  902. } );
  903. function createViewInstanceWithTemplate() {
  904. setTestViewClass( () => ( { tag: 'a' } ) );
  905. setTestViewInstance();
  906. }
  907. function setTestViewClass( templateFn, regionsFn ) {
  908. TestView = class V extends View {
  909. constructor( model ) {
  910. super( model );
  911. if ( templateFn ) {
  912. this.template = templateFn.call( this );
  913. }
  914. if ( templateFn && regionsFn ) {
  915. regionsFn.call( this );
  916. }
  917. }
  918. };
  919. }
  920. function setTestViewInstance( model ) {
  921. view = new TestView( new Model( model ) );
  922. if ( view.template ) {
  923. document.body.appendChild( view.element );
  924. }
  925. }
  926. function dispatchEvent( el, domEvtName ) {
  927. if ( !el.parentNode ) {
  928. throw new Error( 'To dispatch an event, element must be in DOM. Otherwise #target is null.' );
  929. }
  930. el.dispatchEvent( new Event( domEvtName, {
  931. bubbles: true
  932. } ) );
  933. }