view.js 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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 = value => value > 0 ? 'positive' : 'negative';
  221. return {
  222. tag: 'p',
  223. attributes: {
  224. 'class': bind.to( 'foo', callback )
  225. },
  226. children: [
  227. {
  228. text: bind.to( 'foo', callback )
  229. }
  230. ]
  231. };
  232. } );
  233. setTestViewInstance( { foo: 3 } );
  234. expect( view.element.outerHTML ).to.be.equal( '<p class="positive">positive</p>' );
  235. view.model.foo = -7;
  236. expect( view.element.outerHTML ).to.be.equal( '<p class="negative">negative</p>' );
  237. } );
  238. it( 'allows binding attribute to the model – value processing (use Node)', () => {
  239. setTestViewClass( function() {
  240. const bind = this.attributeBinder;
  241. const callback = ( value, node ) => {
  242. return ( !!node.tagName && value > 0 ) ? 'HTMLElement positive' : '';
  243. };
  244. return {
  245. tag: 'p',
  246. attributes: {
  247. 'class': bind.to( 'foo', callback )
  248. },
  249. children: [
  250. {
  251. text: bind.to( 'foo', callback )
  252. }
  253. ]
  254. };
  255. } );
  256. setTestViewInstance( { foo: 3 } );
  257. expect( view.element.outerHTML ).to.be.equal( '<p class="HTMLElement positive"></p>' );
  258. view.model.foo = -7;
  259. expect( view.element.outerHTML ).to.be.equal( '<p></p>' );
  260. } );
  261. it( 'allows binding attribute to the model – custom callback', () => {
  262. setTestViewClass( function() {
  263. const bind = this.attributeBinder;
  264. return {
  265. tag: 'p',
  266. attributes: {
  267. 'class': bind.to( 'foo', ( value, el ) => {
  268. el.innerHTML = value;
  269. if ( value == 'changed' ) {
  270. return value;
  271. }
  272. } )
  273. }
  274. };
  275. } );
  276. setTestViewInstance( { foo: 'moo' } );
  277. expect( view.element.outerHTML ).to.be.equal( '<p class="undefined">moo</p>' );
  278. view.model.foo = 'changed';
  279. expect( view.element.outerHTML ).to.be.equal( '<p class="changed">changed</p>' );
  280. } );
  281. it( 'allows binding attribute to the model – array of bindings (HTMLElement attribute)', () => {
  282. setTestViewClass( function() {
  283. const bind = this.attributeBinder;
  284. return {
  285. tag: 'p',
  286. attributes: {
  287. 'class': [
  288. 'ck-class',
  289. bind.to( 'foo' ),
  290. bind.to( 'bar' ),
  291. bind.to( 'foo', value => `foo-is-${value}` ),
  292. 'ck-end'
  293. ]
  294. },
  295. children: [ 'abc' ]
  296. };
  297. } );
  298. setTestViewInstance( { foo: 'a', bar: 'b' } );
  299. expect( view.element.outerHTML ).to.be.equal( '<p class="ck-class a b foo-is-a ck-end">abc</p>' );
  300. view.model.foo = 'c';
  301. view.model.bar = 'd';
  302. expect( view.element.outerHTML ).to.be.equal( '<p class="ck-class c d foo-is-c ck-end">abc</p>' );
  303. } );
  304. it( 'allows binding attribute to the model – array of bindings (Text Node)', () => {
  305. setTestViewClass( function() {
  306. const bind = this.attributeBinder;
  307. return {
  308. tag: 'p',
  309. attributes: {
  310. },
  311. children: [
  312. {
  313. text: [
  314. 'ck-class',
  315. bind.to( 'foo' ),
  316. bind.to( 'bar' ),
  317. bind.to( 'foo', value => `foo-is-${value}` ),
  318. 'ck-end'
  319. ]
  320. }
  321. ]
  322. };
  323. } );
  324. setTestViewInstance( { foo: 'a', bar: 'b' } );
  325. expect( view.element.outerHTML ).to.be.equal( '<p>ck-class a b foo-is-a ck-end</p>' );
  326. view.model.foo = 'c';
  327. view.model.bar = 'd';
  328. expect( view.element.outerHTML ).to.be.equal( '<p>ck-class c d foo-is-c ck-end</p>' );
  329. } );
  330. it( 'allows binding attribute to the model – falsy values', () => {
  331. setTestViewClass( function() {
  332. const bind = this.attributeBinder;
  333. return {
  334. tag: 'p',
  335. attributes: {
  336. 'class': bind.to( 'foo' )
  337. },
  338. children: [ 'abc' ]
  339. };
  340. } );
  341. setTestViewInstance( { foo: 'bar' } );
  342. expect( view.element.outerHTML ).to.be.equal( '<p class="bar">abc</p>' );
  343. view.model.foo = false;
  344. expect( view.element.outerHTML ).to.be.equal( '<p class="false">abc</p>' );
  345. view.model.foo = null;
  346. expect( view.element.outerHTML ).to.be.equal( '<p class="null">abc</p>' );
  347. view.model.foo = undefined;
  348. expect( view.element.outerHTML ).to.be.equal( '<p class="undefined">abc</p>' );
  349. view.model.foo = 0;
  350. expect( view.element.outerHTML ).to.be.equal( '<p class="0">abc</p>' );
  351. view.model.foo = '';
  352. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  353. } );
  354. } );
  355. describe( 'if', () => {
  356. it( 'returns an object which describes the binding', () => {
  357. setTestViewInstance( { a: 1 } );
  358. const spy = testUtils.sinon.spy();
  359. const bind = view.attributeBinder;
  360. const binding = bind.if( 'a', 'foo', spy );
  361. expect( spy.called ).to.be.false;
  362. expect( binding ).to.have.keys( [ 'type', 'model', 'attribute', 'callback', 'valueIfTrue' ] );
  363. expect( binding.model ).to.equal( view.model );
  364. expect( binding.callback ).to.equal( spy );
  365. expect( binding.attribute ).to.equal( 'a' );
  366. expect( binding.valueIfTrue ).to.equal( 'foo' );
  367. } );
  368. it( 'allows binding attribute to the model – presence of an attribute (HTMLElement attribute)', () => {
  369. setTestViewClass( function() {
  370. const bind = this.attributeBinder;
  371. return {
  372. tag: 'p',
  373. attributes: {
  374. 'class': bind.if( 'foo' )
  375. },
  376. children: [ 'abc' ]
  377. };
  378. } );
  379. setTestViewInstance( { foo: true } );
  380. expect( view.element.outerHTML ).to.be.equal( '<p class="">abc</p>' );
  381. view.model.foo = false;
  382. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  383. view.model.foo = 'bar';
  384. expect( view.element.outerHTML ).to.be.equal( '<p class="">abc</p>' );
  385. } );
  386. // TODO: Is this alright? It makes sense but it's pretty useless. Text Node cannot be
  387. // removed just like an attribute of some HTMLElement.
  388. it( 'allows binding attribute to the model – presence of an attribute (Text Node)', () => {
  389. setTestViewClass( function() {
  390. const bind = this.attributeBinder;
  391. return {
  392. tag: 'p',
  393. children: [
  394. {
  395. text: bind.if( 'foo' )
  396. }
  397. ]
  398. };
  399. } );
  400. setTestViewInstance( { foo: true } );
  401. expect( view.element.outerHTML ).to.be.equal( '<p></p>' );
  402. view.model.foo = false;
  403. expect( view.element.outerHTML ).to.be.equal( '<p></p>' );
  404. view.model.foo = 'bar';
  405. expect( view.element.outerHTML ).to.be.equal( '<p></p>' );
  406. } );
  407. it( 'allows binding attribute to the model – value of an attribute (HTMLElement attribute)', () => {
  408. setTestViewClass( function() {
  409. const bind = this.attributeBinder;
  410. return {
  411. tag: 'p',
  412. attributes: {
  413. 'class': bind.if( 'foo', 'bar' )
  414. },
  415. children: [ 'abc' ]
  416. };
  417. } );
  418. setTestViewInstance( { foo: 'bar' } );
  419. expect( view.element.outerHTML ).to.be.equal( '<p class="bar">abc</p>' );
  420. view.model.foo = false;
  421. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  422. view.model.foo = 64;
  423. expect( view.element.outerHTML ).to.be.equal( '<p class="bar">abc</p>' );
  424. } );
  425. it( 'allows binding attribute to the model – value of an attribute (Text Node)', () => {
  426. setTestViewClass( function() {
  427. const bind = this.attributeBinder;
  428. return {
  429. tag: 'p',
  430. children: [
  431. {
  432. text: bind.if( 'foo', 'bar' )
  433. }
  434. ]
  435. };
  436. } );
  437. setTestViewInstance( { foo: 'bar' } );
  438. expect( view.element.outerHTML ).to.be.equal( '<p>bar</p>' );
  439. view.model.foo = false;
  440. expect( view.element.outerHTML ).to.be.equal( '<p></p>' );
  441. view.model.foo = 64;
  442. expect( view.element.outerHTML ).to.be.equal( '<p>bar</p>' );
  443. } );
  444. it( 'allows binding attribute to the model – value of an attribute processed by a callback', () => {
  445. setTestViewClass( function() {
  446. const bind = this.attributeBinder;
  447. return {
  448. tag: 'p',
  449. attributes: {
  450. 'class': bind.if( 'foo', 'there–is–no–foo', value => !value )
  451. },
  452. children: [ 'abc' ]
  453. };
  454. } );
  455. setTestViewInstance( { foo: 'bar' } );
  456. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  457. view.model.foo = false;
  458. expect( view.element.outerHTML ).to.be.equal( '<p class="there–is–no–foo">abc</p>' );
  459. view.model.foo = 64;
  460. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  461. } );
  462. it( 'allows binding attribute to the model – value of an attribute processed by a callback (use Node)', () => {
  463. setTestViewClass( function() {
  464. const bind = this.attributeBinder;
  465. return {
  466. tag: 'p',
  467. attributes: {
  468. 'class': bind.if( 'foo', 'eqls-tag-name', ( value, el ) => el.tagName === value )
  469. },
  470. children: [ 'abc' ]
  471. };
  472. } );
  473. setTestViewInstance( { foo: 'bar' } );
  474. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  475. view.model.foo = 'P';
  476. expect( view.element.outerHTML ).to.be.equal( '<p class="eqls-tag-name">abc</p>' );
  477. view.model.foo = 64;
  478. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  479. } );
  480. it( 'allows binding attribute to the model – falsy values', () => {
  481. setTestViewClass( function() {
  482. const bind = this.attributeBinder;
  483. return {
  484. tag: 'p',
  485. attributes: {
  486. 'class': bind.if( 'foo', 'foo-is-set' )
  487. },
  488. children: [ 'abc' ]
  489. };
  490. } );
  491. setTestViewInstance( { foo: 'bar' } );
  492. expect( view.element.outerHTML ).to.be.equal( '<p class="foo-is-set">abc</p>' );
  493. view.model.foo = false;
  494. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  495. view.model.foo = null;
  496. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  497. view.model.foo = undefined;
  498. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  499. view.model.foo = '';
  500. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  501. view.model.foo = 0;
  502. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  503. } );
  504. } );
  505. } );
  506. describe( 'on', () => {
  507. it( 'accepts plain binding', () => {
  508. const spy = testUtils.sinon.spy();
  509. setTestViewClass( function() {
  510. return {
  511. tag: 'p',
  512. on: {
  513. x: 'a',
  514. }
  515. };
  516. } );
  517. setTestViewInstance();
  518. view.on( 'a', spy );
  519. dispatchEvent( view.element, 'x' );
  520. sinon.assert.calledWithExactly( spy,
  521. sinon.match.has( 'name', 'a' ),
  522. sinon.match.has( 'target', view.element )
  523. );
  524. } );
  525. it( 'accepts an array of event bindings', () => {
  526. const spy1 = testUtils.sinon.spy();
  527. const spy2 = testUtils.sinon.spy();
  528. setTestViewClass( function() {
  529. return {
  530. tag: 'p',
  531. on: {
  532. x: [ 'a', 'b' ]
  533. }
  534. };
  535. } );
  536. setTestViewInstance();
  537. view.on( 'a', spy1 );
  538. view.on( 'b', spy2 );
  539. dispatchEvent( view.element, 'x' );
  540. sinon.assert.calledWithExactly( spy1,
  541. sinon.match.has( 'name', 'a' ),
  542. sinon.match.has( 'target', view.element )
  543. );
  544. sinon.assert.calledWithExactly( spy2,
  545. sinon.match.has( 'name', 'b' ),
  546. sinon.match.has( 'target', view.element )
  547. );
  548. } );
  549. it( 'accepts DOM selectors', () => {
  550. const spy1 = testUtils.sinon.spy();
  551. const spy2 = testUtils.sinon.spy();
  552. const spy3 = testUtils.sinon.spy();
  553. setTestViewClass( function() {
  554. return {
  555. tag: 'p',
  556. children: [
  557. {
  558. tag: 'span',
  559. attributes: {
  560. 'class': 'y',
  561. },
  562. on: {
  563. 'test@p': 'c'
  564. }
  565. },
  566. {
  567. tag: 'div',
  568. children: [
  569. {
  570. tag: 'span',
  571. attributes: {
  572. 'class': 'y',
  573. }
  574. }
  575. ],
  576. }
  577. ],
  578. on: {
  579. 'test@.y': 'a',
  580. 'test@div': 'b'
  581. }
  582. };
  583. } );
  584. setTestViewInstance();
  585. view.on( 'a', spy1 );
  586. view.on( 'b', spy2 );
  587. view.on( 'c', spy3 );
  588. // Test "test@p".
  589. dispatchEvent( view.element, 'test' );
  590. sinon.assert.callCount( spy1, 0 );
  591. sinon.assert.callCount( spy2, 0 );
  592. sinon.assert.callCount( spy3, 0 );
  593. // Test "test@.y".
  594. dispatchEvent( view.element.firstChild, 'test' );
  595. expect( spy1.firstCall.calledWithExactly(
  596. sinon.match.has( 'name', 'a' ),
  597. sinon.match.has( 'target', view.element.firstChild )
  598. ) ).to.be.true;
  599. sinon.assert.callCount( spy2, 0 );
  600. sinon.assert.callCount( spy3, 0 );
  601. // Test "test@div".
  602. dispatchEvent( view.element.lastChild, 'test' );
  603. sinon.assert.callCount( spy1, 1 );
  604. expect( spy2.firstCall.calledWithExactly(
  605. sinon.match.has( 'name', 'b' ),
  606. sinon.match.has( 'target', view.element.lastChild )
  607. ) ).to.be.true;
  608. sinon.assert.callCount( spy3, 0 );
  609. // Test "test@.y".
  610. dispatchEvent( view.element.lastChild.firstChild, 'test' );
  611. expect( spy1.secondCall.calledWithExactly(
  612. sinon.match.has( 'name', 'a' ),
  613. sinon.match.has( 'target', view.element.lastChild.firstChild )
  614. ) ).to.be.true;
  615. sinon.assert.callCount( spy2, 1 );
  616. sinon.assert.callCount( spy3, 0 );
  617. } );
  618. it( 'accepts function callbacks', () => {
  619. const spy1 = testUtils.sinon.spy();
  620. const spy2 = testUtils.sinon.spy();
  621. setTestViewClass( function() {
  622. return {
  623. tag: 'p',
  624. children: [
  625. {
  626. tag: 'span'
  627. }
  628. ],
  629. on: {
  630. x: spy1,
  631. 'y@span': [ spy2, 'c' ],
  632. }
  633. };
  634. } );
  635. setTestViewInstance();
  636. dispatchEvent( view.element, 'x' );
  637. dispatchEvent( view.element.firstChild, 'y' );
  638. sinon.assert.calledWithExactly( spy1,
  639. sinon.match.has( 'target', view.element )
  640. );
  641. sinon.assert.calledWithExactly( spy2,
  642. sinon.match.has( 'target', view.element.firstChild )
  643. );
  644. } );
  645. it( 'supports event delegation', () => {
  646. const spy = testUtils.sinon.spy();
  647. setTestViewClass( function() {
  648. return {
  649. tag: 'p',
  650. children: [
  651. {
  652. tag: 'span'
  653. }
  654. ],
  655. on: {
  656. x: 'a',
  657. }
  658. };
  659. } );
  660. setTestViewInstance();
  661. view.on( 'a', spy );
  662. dispatchEvent( view.element.firstChild, 'x' );
  663. sinon.assert.calledWithExactly( spy,
  664. sinon.match.has( 'name', 'a' ),
  665. sinon.match.has( 'target', view.element.firstChild )
  666. );
  667. } );
  668. it( 'works for future elements', () => {
  669. const spy = testUtils.sinon.spy();
  670. setTestViewClass( function() {
  671. return {
  672. tag: 'p',
  673. on: {
  674. 'test@div': 'a'
  675. }
  676. };
  677. } );
  678. setTestViewInstance();
  679. view.on( 'a', spy );
  680. const div = document.createElement( 'div' );
  681. view.element.appendChild( div );
  682. dispatchEvent( div, 'test' );
  683. sinon.assert.calledWithExactly( spy, sinon.match.has( 'name', 'a' ), sinon.match.has( 'target', div ) );
  684. } );
  685. } );
  686. describe( 'destroy', () => {
  687. beforeEach( createViewInstanceWithTemplate );
  688. it( 'should destroy the view', () => {
  689. view.destroy();
  690. expect( view.model ).to.be.null;
  691. expect( view.regions ).to.be.null;
  692. expect( view.template ).to.be.null;
  693. expect( view._regionsSelectors ).to.be.null;
  694. expect( view._element ).to.be.null;
  695. expect( view._template ).to.be.null;
  696. } );
  697. it( 'detaches the element from DOM', () => {
  698. const elRef = view.element;
  699. document.createElement( 'div' ).appendChild( view.element );
  700. view.destroy();
  701. expect( elRef.parentNode ).to.be.null;
  702. } );
  703. it( 'destroys child regions', () => {
  704. const region = new Region( 'x' );
  705. const spy = testUtils.sinon.spy( region, 'destroy' );
  706. const regionsRef = view.regions;
  707. const regionViewsRef = region.views;
  708. view.register( region, true );
  709. view.regions.get( 'x' ).views.add( new View() );
  710. view.destroy();
  711. expect( regionsRef.length ).to.be.equal( 0 );
  712. expect( regionViewsRef.length ).to.be.equal( 0 );
  713. expect( spy.calledOnce ).to.be.true;
  714. } );
  715. it( 'detaches bound model listeners', () => {
  716. setTestViewClass( function() {
  717. const bind = this.attributeBinder;
  718. return {
  719. tag: 'p',
  720. children: [
  721. { text: bind.to( 'foo' ) }
  722. ]
  723. };
  724. } );
  725. setTestViewInstance( { foo: 'bar' } );
  726. const modelRef = view.model;
  727. const elRef = view.element;
  728. expect( view.element.outerHTML ).to.be.equal( '<p>bar</p>' );
  729. modelRef.foo = 'baz';
  730. expect( view.element.outerHTML ).to.be.equal( '<p>baz</p>' );
  731. view.destroy();
  732. modelRef.foo = 'abc';
  733. expect( elRef.outerHTML ).to.be.equal( '<p>baz</p>' );
  734. } );
  735. it( 'destroy a template–less view', () => {
  736. view = new View();
  737. expect( () => {
  738. view.destroy();
  739. } ).to.not.throw();
  740. } );
  741. } );
  742. describe( 'applyTemplateToElement', () => {
  743. beforeEach( () => {
  744. setTestViewClass();
  745. setTestViewInstance( { a: 'foo', b: 42 } );
  746. } );
  747. it( 'should initialize attribute bindings', () => {
  748. const el = document.createElement( 'div' );
  749. const bind = view.attributeBinder;
  750. view.applyTemplateToElement( el, {
  751. tag: 'div',
  752. attributes: {
  753. class: bind.to( 'b' ),
  754. id: 'foo',
  755. checked: 'checked'
  756. }
  757. } );
  758. expect( el.outerHTML ).to.be.equal( '<div class="42" id="foo" checked="checked"></div>' );
  759. view.model.b = 64;
  760. expect( el.outerHTML ).to.be.equal( '<div class="64" id="foo" checked="checked"></div>' );
  761. } );
  762. it( 'should initialize DOM listeners', () => {
  763. const el = document.createElement( 'div' );
  764. const spy = testUtils.sinon.spy();
  765. view.applyTemplateToElement( el, {
  766. tag: 'div',
  767. on: {
  768. click: spy
  769. }
  770. } );
  771. document.body.appendChild( el );
  772. dispatchEvent( el, 'click' );
  773. sinon.assert.calledOnce( spy );
  774. view.stopListening( el, 'click' );
  775. dispatchEvent( el, 'click' );
  776. sinon.assert.calledOnce( spy );
  777. } );
  778. it( 'should work for deep DOM structure', () => {
  779. const el = document.createElement( 'div' );
  780. const childA = document.createElement( 'a' );
  781. const childB = document.createElement( 'b' );
  782. childA.textContent = 'anchor';
  783. childB.textContent = 'bold';
  784. el.appendChild( childA );
  785. el.appendChild( childB );
  786. expect( el.outerHTML ).to.be.equal( '<div><a>anchor</a><b>bold</b></div>' );
  787. const spy1 = testUtils.sinon.spy();
  788. const spy2 = testUtils.sinon.spy();
  789. const spy3 = testUtils.sinon.spy();
  790. const bind = view.attributeBinder;
  791. view.applyTemplateToElement( el, {
  792. tag: 'div',
  793. children: [
  794. {
  795. tag: 'a',
  796. on: {
  797. keyup: spy2
  798. },
  799. attributes: {
  800. class: bind.to( 'b', b => 'applied-A-' + b ),
  801. id: 'applied-A'
  802. },
  803. children: [ 'Text applied to childA.' ]
  804. },
  805. {
  806. tag: 'b',
  807. on: {
  808. keydown: spy3
  809. },
  810. attributes: {
  811. class: bind.to( 'b', b => 'applied-B-' + b ),
  812. id: 'applied-B'
  813. },
  814. children: [ 'Text applied to childB.' ]
  815. },
  816. 'Text which is not to be applied because it does NOT exist in original element.'
  817. ],
  818. on: {
  819. 'mouseover@a': spy1
  820. },
  821. attributes: {
  822. id: bind.to( 'a', a => a.toUpperCase() ),
  823. class: bind.to( 'b', b => 'applied-parent-' + b )
  824. }
  825. } );
  826. expect( el.outerHTML ).to.be.equal( '<div id="FOO" class="applied-parent-42">' +
  827. '<a class="applied-A-42" id="applied-A">Text applied to childA.</a>' +
  828. '<b class="applied-B-42" id="applied-B">Text applied to childB.</b>' +
  829. '</div>' );
  830. view.model.b = 16;
  831. expect( el.outerHTML ).to.be.equal( '<div id="FOO" class="applied-parent-16">' +
  832. '<a class="applied-A-16" id="applied-A">Text applied to childA.</a>' +
  833. '<b class="applied-B-16" id="applied-B">Text applied to childB.</b>' +
  834. '</div>' );
  835. document.body.appendChild( el );
  836. // Test "mouseover@a".
  837. dispatchEvent( el, 'mouseover' );
  838. dispatchEvent( childA, 'mouseover' );
  839. // Test "keyup".
  840. dispatchEvent( childA, 'keyup' );
  841. // Test "keydown".
  842. dispatchEvent( childB, 'keydown' );
  843. sinon.assert.calledOnce( spy1 );
  844. sinon.assert.calledOnce( spy2 );
  845. sinon.assert.calledOnce( spy3 );
  846. } );
  847. it( 're–uses a template definition object without redundant re–definition of "on" listener attachers', () => {
  848. const el1 = document.createElement( 'div' );
  849. const el2 = document.createElement( 'div' );
  850. const spy = testUtils.sinon.spy();
  851. const def = {
  852. tag: 'div',
  853. on: {
  854. click: spy
  855. }
  856. };
  857. view.applyTemplateToElement( el1, def );
  858. const attacherFn = def.on.click;
  859. view.applyTemplateToElement( el2, def );
  860. // Attacher function didn't change because it's still the same View instance.
  861. expect( attacherFn ).to.be.equal( def.on.click );
  862. expect( def.on._listenerView ).to.be.equal( view );
  863. document.body.appendChild( el1 );
  864. document.body.appendChild( el2 );
  865. dispatchEvent( el1, 'click' );
  866. sinon.assert.calledOnce( spy );
  867. dispatchEvent( el2, 'click' );
  868. sinon.assert.calledTwice( spy );
  869. } );
  870. it( 'shares a template definition between View instances – event listeners', () => {
  871. const el = document.createElement( 'div' );
  872. const spy = testUtils.sinon.spy();
  873. const view1 = new View();
  874. const view2 = new View();
  875. const def = {
  876. tag: 'div',
  877. on: {
  878. click: spy
  879. }
  880. };
  881. document.body.appendChild( el );
  882. view1.applyTemplateToElement( el, def );
  883. expect( def.on._listenerView ).to.be.equal( view1 );
  884. dispatchEvent( el, 'click' );
  885. sinon.assert.calledOnce( spy );
  886. // Use another view1 to see if attachers are re–defined.
  887. view2.applyTemplateToElement( el, def );
  888. expect( def.on._listenerView ).to.be.equal( view2 );
  889. dispatchEvent( el, 'click' );
  890. // Spy was called for view1 (1st dispatch), then for view1 and view2 (2nd dispatch).
  891. sinon.assert.calledThrice( spy );
  892. } );
  893. } );
  894. } );
  895. function createViewInstanceWithTemplate() {
  896. setTestViewClass( () => ( { tag: 'a' } ) );
  897. setTestViewInstance();
  898. }
  899. function setTestViewClass( templateFn, regionsFn ) {
  900. TestView = class V extends View {
  901. constructor( model ) {
  902. super( model );
  903. if ( templateFn ) {
  904. this.template = templateFn.call( this );
  905. }
  906. if ( templateFn && regionsFn ) {
  907. regionsFn.call( this );
  908. }
  909. }
  910. };
  911. }
  912. function setTestViewInstance( model ) {
  913. view = new TestView( new Model( model ) );
  914. if ( view.template ) {
  915. document.body.appendChild( view.element );
  916. }
  917. }
  918. function dispatchEvent( el, domEvtName ) {
  919. if ( !el.parentNode ) {
  920. throw new Error( 'To dispatch an event, element must be in DOM. Otherwise #target is null.' );
  921. }
  922. el.dispatchEvent( new Event( domEvtName, {
  923. bubbles: true
  924. } ) );
  925. }