8
0

view.js 30 KB

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