view.js 28 KB

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