view.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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._el ).to.be.null;
  34. expect( view._template ).to.be.null;
  35. expect( () => {
  36. view.el;
  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.el ).to.be.equal( view.el.firstChild );
  70. expect( region2.el ).to.be.equal( view.el.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.el ).to.be.equal( view.el.firstChild );
  80. expect( region2.el ).to.be.equal( view.el.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.el ).to.be.null;
  90. expect( region2.el ).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.el ).to.be.an.instanceof( HTMLElement );
  152. expect( view.el.nodeName ).to.be.equal( 'A' );
  153. } );
  154. it( 'can be explicitly declared', () => {
  155. class CustomView extends View {
  156. constructor() {
  157. super();
  158. this.el = document.createElement( 'span' );
  159. }
  160. }
  161. view = new CustomView();
  162. expect( view.el ).to.be.an.instanceof( HTMLElement );
  163. } );
  164. } );
  165. describe( 'bindToAttribute', () => {
  166. beforeEach( createViewInstanceWithTemplate );
  167. it( 'returns a function that passes arguments', () => {
  168. setTestViewInstance( { a: 'foo' } );
  169. let spy = testUtils.sinon.spy();
  170. let callback = view.bindToAttribute( 'a', spy );
  171. expect( spy.called ).to.be.false;
  172. callback( 'el', 'updater' );
  173. sinon.assert.calledOnce( spy );
  174. sinon.assert.calledWithExactly( spy, 'el', 'foo' );
  175. view.model.a = 'bar';
  176. sinon.assert.calledTwice( spy );
  177. expect( spy.secondCall.calledWithExactly( 'el', 'bar' ) ).to.be.true;
  178. } );
  179. it( 'allows binding attribute to the model', () => {
  180. setTestViewClass( function() {
  181. return {
  182. tag: 'p',
  183. attrs: {
  184. 'class': this.bindToAttribute( 'foo' )
  185. },
  186. text: 'abc'
  187. };
  188. } );
  189. setTestViewInstance( { foo: 'bar' } );
  190. expect( view.el.outerHTML ).to.be.equal( '<p class="bar">abc</p>' );
  191. view.model.foo = 'baz';
  192. expect( view.el.outerHTML ).to.be.equal( '<p class="baz">abc</p>' );
  193. } );
  194. it( 'allows binding "text" to the model', () => {
  195. setTestViewClass( function() {
  196. return {
  197. tag: 'p',
  198. children: [
  199. {
  200. tag: 'b',
  201. text: 'baz'
  202. }
  203. ],
  204. text: this.bindToAttribute( 'foo' )
  205. };
  206. } );
  207. setTestViewInstance( { foo: 'bar' } );
  208. expect( view.el.outerHTML ).to.be.equal( '<p>bar<b>baz</b></p>' );
  209. // TODO: A solution to avoid nuking the children?
  210. view.model.foo = 'qux';
  211. expect( view.el.outerHTML ).to.be.equal( '<p>qux</p>' );
  212. } );
  213. it( 'allows binding to the model with value processing', () => {
  214. let callback = ( el, value ) =>
  215. ( value > 0 ? 'positive' : 'negative' );
  216. setTestViewClass( function() {
  217. return {
  218. tag: 'p',
  219. attrs: {
  220. 'class': this.bindToAttribute( 'foo', callback )
  221. },
  222. text: this.bindToAttribute( 'foo', callback )
  223. };
  224. } );
  225. setTestViewInstance( { foo: 3 } );
  226. expect( view.el.outerHTML ).to.be.equal( '<p class="positive">positive</p>' );
  227. view.model.foo = -7;
  228. expect( view.el.outerHTML ).to.be.equal( '<p class="negative">negative</p>' );
  229. } );
  230. it( 'allows binding to the model with custom callback', () => {
  231. setTestViewClass( function() {
  232. return {
  233. tag: 'p',
  234. attrs: {
  235. 'class': this.bindToAttribute( 'foo', ( el, value ) => {
  236. el.innerHTML = value;
  237. if ( value == 'changed' ) {
  238. return value;
  239. }
  240. } )
  241. },
  242. text: 'bar'
  243. };
  244. } );
  245. setTestViewInstance( { foo: 'moo' } );
  246. expect( view.el.outerHTML ).to.be.equal( '<p>moo</p>' );
  247. view.model.foo = 'changed';
  248. expect( view.el.outerHTML ).to.be.equal( '<p class="changed">changed</p>' );
  249. } );
  250. } );
  251. describe( 'on', () => {
  252. it( 'accepts plain binding', () => {
  253. let spy = testUtils.sinon.spy();
  254. setTestViewClass( function() {
  255. return {
  256. tag: 'p',
  257. on: {
  258. x: 'a',
  259. }
  260. };
  261. } );
  262. setTestViewInstance();
  263. view.on( 'a', spy );
  264. dispatchEvent( view.el, 'x' );
  265. sinon.assert.calledWithExactly( spy,
  266. sinon.match.has( 'name', 'a' ),
  267. sinon.match.has( 'target', view.el )
  268. );
  269. } );
  270. it( 'accepts an array of event bindings', () => {
  271. let spy1 = testUtils.sinon.spy();
  272. let spy2 = testUtils.sinon.spy();
  273. setTestViewClass( function() {
  274. return {
  275. tag: 'p',
  276. on: {
  277. x: [ 'a', 'b' ]
  278. }
  279. };
  280. } );
  281. setTestViewInstance();
  282. view.on( 'a', spy1 );
  283. view.on( 'b', spy2 );
  284. dispatchEvent( view.el, 'x' );
  285. sinon.assert.calledWithExactly( spy1,
  286. sinon.match.has( 'name', 'a' ),
  287. sinon.match.has( 'target', view.el )
  288. );
  289. sinon.assert.calledWithExactly( spy2,
  290. sinon.match.has( 'name', 'b' ),
  291. sinon.match.has( 'target', view.el )
  292. );
  293. } );
  294. it( 'accepts DOM selectors', () => {
  295. let spy1 = testUtils.sinon.spy();
  296. let spy2 = testUtils.sinon.spy();
  297. let spy3 = testUtils.sinon.spy();
  298. setTestViewClass( function() {
  299. return {
  300. tag: 'p',
  301. children: [
  302. {
  303. tag: 'span',
  304. attrs: {
  305. 'class': 'y',
  306. },
  307. on: {
  308. 'test@p': 'c'
  309. }
  310. },
  311. {
  312. tag: 'div',
  313. children: [
  314. {
  315. tag: 'span',
  316. attrs: {
  317. 'class': 'y',
  318. }
  319. }
  320. ],
  321. }
  322. ],
  323. on: {
  324. 'test@.y': 'a',
  325. 'test@div': 'b'
  326. }
  327. };
  328. } );
  329. setTestViewInstance();
  330. view.on( 'a', spy1 );
  331. view.on( 'b', spy2 );
  332. view.on( 'c', spy3 );
  333. // Test "test@p".
  334. dispatchEvent( view.el, 'test' );
  335. sinon.assert.callCount( spy1, 0 );
  336. sinon.assert.callCount( spy2, 0 );
  337. sinon.assert.callCount( spy3, 0 );
  338. // Test "test@.y".
  339. dispatchEvent( view.el.firstChild, 'test' );
  340. expect( spy1.firstCall.calledWithExactly(
  341. sinon.match.has( 'name', 'a' ),
  342. sinon.match.has( 'target', view.el.firstChild )
  343. ) ).to.be.true;
  344. sinon.assert.callCount( spy2, 0 );
  345. sinon.assert.callCount( spy3, 0 );
  346. // Test "test@div".
  347. dispatchEvent( view.el.lastChild, 'test' );
  348. sinon.assert.callCount( spy1, 1 );
  349. expect( spy2.firstCall.calledWithExactly(
  350. sinon.match.has( 'name', 'b' ),
  351. sinon.match.has( 'target', view.el.lastChild )
  352. ) ).to.be.true;
  353. sinon.assert.callCount( spy3, 0 );
  354. // Test "test@.y".
  355. dispatchEvent( view.el.lastChild.firstChild, 'test' );
  356. expect( spy1.secondCall.calledWithExactly(
  357. sinon.match.has( 'name', 'a' ),
  358. sinon.match.has( 'target', view.el.lastChild.firstChild )
  359. ) ).to.be.true;
  360. sinon.assert.callCount( spy2, 1 );
  361. sinon.assert.callCount( spy3, 0 );
  362. } );
  363. it( 'accepts function callbacks', () => {
  364. let spy1 = testUtils.sinon.spy();
  365. let spy2 = testUtils.sinon.spy();
  366. setTestViewClass( function() {
  367. return {
  368. tag: 'p',
  369. children: [
  370. {
  371. tag: 'span'
  372. }
  373. ],
  374. on: {
  375. x: spy1,
  376. 'y@span': [ spy2, 'c' ],
  377. }
  378. };
  379. } );
  380. setTestViewInstance();
  381. dispatchEvent( view.el, 'x' );
  382. dispatchEvent( view.el.firstChild, 'y' );
  383. sinon.assert.calledWithExactly( spy1,
  384. sinon.match.has( 'target', view.el )
  385. );
  386. sinon.assert.calledWithExactly( spy2,
  387. sinon.match.has( 'target', view.el.firstChild )
  388. );
  389. } );
  390. it( 'supports event delegation', () => {
  391. let spy = testUtils.sinon.spy();
  392. setTestViewClass( function() {
  393. return {
  394. tag: 'p',
  395. children: [
  396. {
  397. tag: 'span'
  398. }
  399. ],
  400. on: {
  401. x: 'a',
  402. }
  403. };
  404. } );
  405. setTestViewInstance();
  406. view.on( 'a', spy );
  407. dispatchEvent( view.el.firstChild, 'x' );
  408. sinon.assert.calledWithExactly( spy,
  409. sinon.match.has( 'name', 'a' ),
  410. sinon.match.has( 'target', view.el.firstChild )
  411. );
  412. } );
  413. it( 'works for future elements', () => {
  414. let spy = testUtils.sinon.spy();
  415. setTestViewClass( function() {
  416. return {
  417. tag: 'p',
  418. on: {
  419. 'test@div': 'a'
  420. }
  421. };
  422. } );
  423. setTestViewInstance();
  424. view.on( 'a', spy );
  425. let div = document.createElement( 'div' );
  426. view.el.appendChild( div );
  427. dispatchEvent( div, 'test' );
  428. sinon.assert.calledWithExactly( spy, sinon.match.has( 'name', 'a' ), sinon.match.has( 'target', div ) );
  429. } );
  430. } );
  431. describe( 'destroy', () => {
  432. beforeEach( createViewInstanceWithTemplate );
  433. it( 'should destroy the view', () => {
  434. view.destroy();
  435. expect( view.model ).to.be.null;
  436. expect( view.regions ).to.be.null;
  437. expect( view.template ).to.be.null;
  438. expect( view._regionsSelectors ).to.be.null;
  439. expect( view._el ).to.be.null;
  440. expect( view._template ).to.be.null;
  441. } );
  442. it( 'detaches the element from DOM', () => {
  443. const elRef = view.el;
  444. document.createElement( 'div' ).appendChild( view.el );
  445. view.destroy();
  446. expect( elRef.parentNode ).to.be.null;
  447. } );
  448. it( 'destroys child regions', () => {
  449. const region = new Region( 'x' );
  450. const spy = testUtils.sinon.spy( region, 'destroy' );
  451. const regionsRef = view.regions;
  452. const regionViewsRef = region.views;
  453. view.register( region, true );
  454. view.regions.get( 'x' ).views.add( new View() );
  455. view.destroy();
  456. expect( regionsRef.length ).to.be.equal( 0 );
  457. expect( regionViewsRef.length ).to.be.equal( 0 );
  458. expect( spy.calledOnce ).to.be.true;
  459. } );
  460. it( 'detaches bound model listeners', () => {
  461. setTestViewClass( function() {
  462. return {
  463. tag: 'p',
  464. text: this.bindToAttribute( 'foo' )
  465. };
  466. } );
  467. setTestViewInstance( { foo: 'bar' } );
  468. const modelRef = view.model;
  469. const elRef = view.el;
  470. expect( view.el.outerHTML ).to.be.equal( '<p>bar</p>' );
  471. modelRef.foo = 'baz';
  472. expect( view.el.outerHTML ).to.be.equal( '<p>baz</p>' );
  473. view.destroy();
  474. modelRef.foo = 'abc';
  475. expect( elRef.outerHTML ).to.be.equal( '<p>baz</p>' );
  476. } );
  477. it( 'destroy a template–less view', () => {
  478. view = new View();
  479. expect( () => {
  480. view.destroy();
  481. } ).to.not.throw();
  482. } );
  483. } );
  484. } );
  485. function createViewInstanceWithTemplate() {
  486. setTestViewClass( () => ( { tag: 'a' } ) );
  487. setTestViewInstance();
  488. }
  489. function setTestViewClass( templateFn, regionsFn ) {
  490. TestView = class V extends View {
  491. constructor( model ) {
  492. super( model );
  493. if ( templateFn ) {
  494. this.template = templateFn.call( this );
  495. }
  496. if ( templateFn && regionsFn ) {
  497. regionsFn.call( this );
  498. }
  499. }
  500. };
  501. }
  502. function setTestViewInstance( model ) {
  503. view = new TestView( new Model( model ) );
  504. if ( view.template ) {
  505. document.body.appendChild( view.el );
  506. }
  507. }
  508. function dispatchEvent( el, domEvtName ) {
  509. if ( !el.parentNode ) {
  510. throw new Error( 'To dispatch an event, element must be in DOM. Otherwise #target is null.' );
  511. }
  512. el.dispatchEvent( new Event( domEvtName, {
  513. bubbles: true
  514. } ) );
  515. }