template.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: ui */
  6. /* global HTMLElement */
  7. 'use strict';
  8. import testUtils from '/tests/ckeditor5/_utils/utils.js';
  9. import Template from '/ckeditor5/ui/template.js';
  10. import Model from '/ckeditor5/ui/model.js';
  11. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  12. import EmitterMixin from '/ckeditor5/utils/emittermixin.js';
  13. import DOMEmitterMixin from '/ckeditor5/ui/domemittermixin.js';
  14. import extend from '/ckeditor5/utils/lib/lodash/extend.js';
  15. testUtils.createSinonSandbox();
  16. let el, text;
  17. describe( 'Template', () => {
  18. describe( 'constructor', () => {
  19. it( 'accepts template definition', () => {
  20. const def = {
  21. tag: 'p'
  22. };
  23. expect( new Template( def ).definition ).to.equal( def );
  24. } );
  25. it( 'normalizes template definition', () => {
  26. const bind = Template.bind( new Model( {} ), extend( {}, DOMEmitterMixin ) );
  27. const tpl = new Template( {
  28. tag: 'p',
  29. attributes: {
  30. a: 'foo',
  31. b: [ 'bar', 'baz' ],
  32. c: {
  33. ns: 'abc',
  34. value: bind.to( 'qux' )
  35. }
  36. },
  37. children: [
  38. {
  39. text: 'content'
  40. },
  41. {
  42. text: bind.to( 'x' )
  43. },
  44. 'abc',
  45. {
  46. text: [ 'a', 'b' ]
  47. }
  48. ],
  49. on: {
  50. 'a@span': bind.to( 'b' ),
  51. 'b@span': bind.to( () => {} ),
  52. 'c@span': [
  53. bind.to( 'c' ),
  54. bind.to( () => {} )
  55. ]
  56. }
  57. } );
  58. const def = tpl.definition;
  59. expect( def.attributes.a[ 0 ] ).to.equal( 'foo' );
  60. expect( def.attributes.b[ 0 ] ).to.equal( 'bar' );
  61. expect( def.attributes.b[ 1 ] ).to.equal( 'baz' );
  62. expect( def.attributes.c[ 0 ].value[ 0 ].type ).to.be.a( 'symbol' );
  63. expect( def.children[ 0 ].text[ 0 ] ).to.equal( 'content' );
  64. expect( def.children[ 1 ].text[ 0 ].type ).to.be.a( 'symbol' );
  65. expect( def.children[ 2 ].text[ 0 ] ).to.equal( 'abc' );
  66. expect( def.children[ 3 ].text[ 0 ] ).to.equal( 'a' );
  67. expect( def.children[ 3 ].text[ 1 ] ).to.equal( 'b' );
  68. expect( def.on[ 'a@span' ][ 0 ].type ).to.be.a( 'symbol' );
  69. expect( def.on[ 'b@span' ][ 0 ].type ).to.be.a( 'symbol' );
  70. expect( def.on[ 'c@span' ][ 0 ].type ).to.be.a( 'symbol' );
  71. expect( def.on[ 'c@span' ][ 1 ].type ).to.be.a( 'symbol' );
  72. } );
  73. } );
  74. describe( 'render', () => {
  75. it( 'throws when wrong template definition', () => {
  76. expect( () => {
  77. new Template( {} ).render();
  78. } ).to.throw( CKEditorError, /ui-template-wrong-syntax/ );
  79. expect( () => {
  80. new Template( {
  81. tag: 'p',
  82. text: 'foo'
  83. } ).render();
  84. } ).to.throw( CKEditorError, /ui-template-wrong-syntax/ );
  85. } );
  86. it( 'creates HTMLElement', () => {
  87. const el = new Template( {
  88. tag: 'p',
  89. } ).render();
  90. expect( el ).to.be.instanceof( HTMLElement );
  91. expect( el.parentNode ).to.be.null;
  92. expect( el.outerHTML ).to.be.equal( '<p></p>' );
  93. expect( el.namespaceURI ).to.be.equal( 'http://www.w3.org/1999/xhtml' );
  94. } );
  95. it( 'creates an element in a custom namespace', () => {
  96. const el = new Template( {
  97. tag: 'p',
  98. ns: 'foo'
  99. } ).render();
  100. expect( el.namespaceURI ).to.be.equal( 'foo' );
  101. } );
  102. it( 'renders HTMLElement attributes', () => {
  103. const el = new Template( {
  104. tag: 'p',
  105. attributes: {
  106. 'class': [ 'a', 'b' ],
  107. x: 'bar'
  108. },
  109. children: [ 'foo' ]
  110. } ).render();
  111. expect( el ).to.be.instanceof( HTMLElement );
  112. expect( el.parentNode ).to.be.null;
  113. expect( el.outerHTML ).to.be.equal( '<p class="a b" x="bar">foo</p>' );
  114. expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.be.null;
  115. } );
  116. it( 'renders HTMLElement attributes – empty', () => {
  117. const el = new Template( {
  118. tag: 'p',
  119. attributes: {
  120. class: '',
  121. x: [ '', '' ]
  122. },
  123. children: [ 'foo' ]
  124. } ).render();
  125. expect( el.outerHTML ).to.be.equal( '<p class="" x="">foo</p>' );
  126. } );
  127. it( 'renders HTMLElement attributes – falsy values', () => {
  128. const el = new Template( {
  129. tag: 'p',
  130. attributes: {
  131. class: false,
  132. x: [ '', null ]
  133. },
  134. children: [ 'foo' ]
  135. } ).render();
  136. expect( el.outerHTML ).to.be.equal( '<p class="false" x="null">foo</p>' );
  137. } );
  138. it( 'renders HTMLElement attributes in a custom namespace', () => {
  139. const el = new Template( {
  140. tag: 'p',
  141. attributes: {
  142. class: {
  143. ns: 'foo',
  144. value: [ 'a', 'b' ]
  145. },
  146. x: {
  147. ns: 'abc',
  148. value: 'bar'
  149. }
  150. },
  151. children: [ 'foo' ]
  152. } ).render();
  153. expect( el.outerHTML ).to.be.equal( '<p class="a b" x="bar">foo</p>' );
  154. expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.equal( 'foo' );
  155. expect( el.attributes.getNamedItem( 'x' ).namespaceURI ).to.equal( 'abc' );
  156. } );
  157. it( 'creates HTMLElement children', () => {
  158. const el = new Template( {
  159. tag: 'p',
  160. attributes: {
  161. a: 'A'
  162. },
  163. children: [
  164. {
  165. tag: 'b',
  166. children: [ 'B' ]
  167. },
  168. {
  169. tag: 'i',
  170. children: [
  171. 'C',
  172. {
  173. tag: 'b',
  174. children: [ 'D' ]
  175. }
  176. ]
  177. }
  178. ]
  179. } ).render();
  180. expect( el.outerHTML ).to.be.equal( '<p a="A"><b>B</b><i>C<b>D</b></i></p>' );
  181. } );
  182. it( 'creates a Text node', () => {
  183. const node = new Template( { text: 'foo' } ).render();
  184. expect( node.nodeType ).to.be.equal( 3 );
  185. expect( node.textContent ).to.be.equal( 'foo' );
  186. } );
  187. it( 'creates a child Text Node (different syntaxes)', () => {
  188. const el = new Template( {
  189. tag: 'p',
  190. children: [
  191. 'foo',
  192. { text: 'bar' }
  193. ]
  194. } ).render();
  195. expect( el.outerHTML ).to.be.equal( '<p>foobar</p>' );
  196. } );
  197. it( 'creates multiple child Text Nodes', () => {
  198. const el = new Template( {
  199. tag: 'p',
  200. children: [
  201. 'a',
  202. 'b',
  203. { text: 'c' },
  204. 'd',
  205. { text: [ 'e', 'f' ] }
  206. ]
  207. } ).render();
  208. expect( el.childNodes ).to.have.length( 5 );
  209. expect( el.outerHTML ).to.be.equal( '<p>abcdef</p>' );
  210. } );
  211. it( 'activates model bindings – root', () => {
  212. const observable = new Model( {
  213. foo: 'bar'
  214. } );
  215. const emitter = extend( {}, EmitterMixin );
  216. const bind = Template.bind( observable, emitter );
  217. const el = new Template( {
  218. tag: 'div',
  219. attributes: {
  220. class: bind.to( 'foo' )
  221. }
  222. } ).render();
  223. expect( el.getAttribute( 'class' ) ).to.equal( 'bar' );
  224. observable.foo = 'baz';
  225. expect( el.getAttribute( 'class' ) ).to.equal( 'baz' );
  226. } );
  227. it( 'activates model bindings – children', () => {
  228. const observable = new Model( {
  229. foo: 'bar'
  230. } );
  231. const emitter = extend( {}, EmitterMixin );
  232. const bind = Template.bind( observable, emitter );
  233. const el = new Template( {
  234. tag: 'div',
  235. children: [
  236. {
  237. tag: 'span',
  238. children: [
  239. {
  240. text: [
  241. bind.to( 'foo' ),
  242. 'static'
  243. ]
  244. }
  245. ]
  246. }
  247. ]
  248. } ).render();
  249. expect( el.firstChild.textContent ).to.equal( 'bar static' );
  250. observable.foo = 'baz';
  251. expect( el.firstChild.textContent ).to.equal( 'baz static' );
  252. } );
  253. } );
  254. describe( 'apply', () => {
  255. beforeEach( () => {
  256. el = document.createElement( 'div' );
  257. text = document.createTextNode( '' );
  258. } );
  259. it( 'throws when wrong template definition', () => {
  260. expect( () => {
  261. new Template( {
  262. tag: 'p',
  263. text: 'foo'
  264. } ).apply( el );
  265. } ).to.throw( CKEditorError, /ui-template-wrong-syntax/ );
  266. } );
  267. it( 'throws when no HTMLElement passed', () => {
  268. expect( () => {
  269. new Template( {
  270. tag: 'p'
  271. } ).apply();
  272. } ).to.throw( CKEditorError, /ui-template-wrong-node/ );
  273. } );
  274. it( 'accepts empty template definition', () => {
  275. new Template( {} ).apply( el );
  276. new Template( {} ).apply( text );
  277. expect( el.outerHTML ).to.be.equal( '<div></div>' );
  278. expect( text.textContent ).to.be.equal( '' );
  279. } );
  280. it( 'applies textContent to a Text Node', () => {
  281. new Template( {
  282. text: 'abc'
  283. } ).apply( text );
  284. expect( text.textContent ).to.be.equal( 'abc' );
  285. } );
  286. it( 'applies attributes to an HTMLElement', () => {
  287. new Template( {
  288. tag: 'div',
  289. attributes: {
  290. 'class': [ 'a', 'b' ],
  291. x: 'bar'
  292. }
  293. } ).apply( el );
  294. expect( el.outerHTML ).to.be.equal( '<div class="a b" x="bar"></div>' );
  295. } );
  296. it( 'doesn\'t apply new child to an HTMLElement – Text Node', () => {
  297. new Template( {
  298. tag: 'div',
  299. children: [ 'foo' ]
  300. } ).apply( el );
  301. expect( el.outerHTML ).to.be.equal( '<div></div>' );
  302. } );
  303. it( 'doesn\'t apply new child to an HTMLElement – HTMLElement', () => {
  304. new Template( {
  305. tag: 'div',
  306. children: [
  307. {
  308. tag: 'span'
  309. }
  310. ]
  311. } ).apply( el );
  312. expect( el.outerHTML ).to.be.equal( '<div></div>' );
  313. } );
  314. it( 'applies new textContent to an existing Text Node of an HTMLElement', () => {
  315. el.textContent = 'bar';
  316. new Template( {
  317. tag: 'div',
  318. children: [ 'foo' ]
  319. } ).apply( el );
  320. expect( el.outerHTML ).to.be.equal( '<div>foo</div>' );
  321. } );
  322. it( 'applies attributes and TextContent to a DOM tree', () => {
  323. el.textContent = 'abc';
  324. el.appendChild( document.createElement( 'span' ) );
  325. new Template( {
  326. tag: 'div',
  327. attributes: {
  328. 'class': 'parent'
  329. },
  330. children: [
  331. 'Children: ',
  332. {
  333. tag: 'span',
  334. attributes: {
  335. class: 'child'
  336. }
  337. }
  338. ]
  339. } ).apply( el );
  340. expect( el.outerHTML ).to.be.equal( '<div class="parent">Children: <span class="child"></span></div>' );
  341. } );
  342. } );
  343. describe( 'bind', () => {
  344. it( 'returns object', () => {
  345. expect( Template.bind() ).to.be.an( 'object' );
  346. } );
  347. it( 'provides "to" and "if" interface', () => {
  348. const bind = Template.bind();
  349. expect( bind ).to.have.keys( 'to', 'if' );
  350. expect( bind.to ).to.be.a( 'function' );
  351. expect( bind.if ).to.be.a( 'function' );
  352. } );
  353. describe( 'event', () => {
  354. let observable, domEmitter, bind;
  355. beforeEach( () => {
  356. observable = new Model( {
  357. foo: 'bar',
  358. baz: 'qux'
  359. } );
  360. domEmitter = extend( {}, DOMEmitterMixin );
  361. bind = Template.bind( observable, domEmitter );
  362. } );
  363. it( 'accepts plain binding', () => {
  364. const spy = testUtils.sinon.spy();
  365. setElement( {
  366. tag: 'p',
  367. on: {
  368. x: bind.to( 'a' ),
  369. }
  370. } );
  371. observable.on( 'a', spy );
  372. dispatchEvent( el, 'x' );
  373. sinon.assert.calledWithExactly( spy,
  374. sinon.match.has( 'name', 'a' ),
  375. sinon.match.has( 'target', el )
  376. );
  377. } );
  378. it( 'accepts an array of event bindings', () => {
  379. const spy1 = testUtils.sinon.spy();
  380. const spy2 = testUtils.sinon.spy();
  381. setElement( {
  382. tag: 'p',
  383. on: {
  384. x: [
  385. bind.to( 'a' ),
  386. bind.to( 'b' )
  387. ]
  388. }
  389. } );
  390. observable.on( 'a', spy1 );
  391. observable.on( 'b', spy2 );
  392. dispatchEvent( el, 'x' );
  393. sinon.assert.calledWithExactly( spy1,
  394. sinon.match.has( 'name', 'a' ),
  395. sinon.match.has( 'target', el )
  396. );
  397. sinon.assert.calledWithExactly( spy2,
  398. sinon.match.has( 'name', 'b' ),
  399. sinon.match.has( 'target', el )
  400. );
  401. } );
  402. it( 'accepts DOM selectors', () => {
  403. const spy1 = testUtils.sinon.spy();
  404. const spy2 = testUtils.sinon.spy();
  405. const spy3 = testUtils.sinon.spy();
  406. setElement( {
  407. tag: 'p',
  408. children: [
  409. {
  410. tag: 'span',
  411. attributes: {
  412. 'class': 'y',
  413. },
  414. on: {
  415. 'test@p': bind.to( 'c' )
  416. }
  417. },
  418. {
  419. tag: 'div',
  420. children: [
  421. {
  422. tag: 'span',
  423. attributes: {
  424. 'class': 'y',
  425. }
  426. }
  427. ],
  428. }
  429. ],
  430. on: {
  431. 'test@.y': bind.to( 'a' ),
  432. 'test@div': bind.to( 'b' )
  433. }
  434. } );
  435. observable.on( 'a', spy1 );
  436. observable.on( 'b', spy2 );
  437. observable.on( 'c', spy3 );
  438. // Test "test@p".
  439. dispatchEvent( el, 'test' );
  440. sinon.assert.callCount( spy1, 0 );
  441. sinon.assert.callCount( spy2, 0 );
  442. sinon.assert.callCount( spy3, 0 );
  443. // Test "test@.y".
  444. dispatchEvent( el.firstChild, 'test' );
  445. expect( spy1.firstCall.calledWithExactly(
  446. sinon.match.has( 'name', 'a' ),
  447. sinon.match.has( 'target', el.firstChild )
  448. ) ).to.be.true;
  449. sinon.assert.callCount( spy2, 0 );
  450. sinon.assert.callCount( spy3, 0 );
  451. // Test "test@div".
  452. dispatchEvent( el.lastChild, 'test' );
  453. sinon.assert.callCount( spy1, 1 );
  454. expect( spy2.firstCall.calledWithExactly(
  455. sinon.match.has( 'name', 'b' ),
  456. sinon.match.has( 'target', el.lastChild )
  457. ) ).to.be.true;
  458. sinon.assert.callCount( spy3, 0 );
  459. // Test "test@.y".
  460. dispatchEvent( el.lastChild.firstChild, 'test' );
  461. expect( spy1.secondCall.calledWithExactly(
  462. sinon.match.has( 'name', 'a' ),
  463. sinon.match.has( 'target', el.lastChild.firstChild )
  464. ) ).to.be.true;
  465. sinon.assert.callCount( spy2, 1 );
  466. sinon.assert.callCount( spy3, 0 );
  467. } );
  468. it( 'accepts function callbacks', () => {
  469. const spy1 = testUtils.sinon.spy();
  470. const spy2 = testUtils.sinon.spy();
  471. setElement( {
  472. tag: 'p',
  473. children: [
  474. {
  475. tag: 'span'
  476. }
  477. ],
  478. on: {
  479. x: bind.to( spy1 ),
  480. 'y@span': [
  481. bind.to( spy2 ),
  482. bind.to( 'c' )
  483. ]
  484. }
  485. } );
  486. dispatchEvent( el, 'x' );
  487. dispatchEvent( el.firstChild, 'y' );
  488. sinon.assert.calledWithExactly( spy1,
  489. sinon.match.has( 'target', el )
  490. );
  491. sinon.assert.calledWithExactly( spy2,
  492. sinon.match.has( 'target', el.firstChild )
  493. );
  494. } );
  495. it( 'supports event delegation', () => {
  496. const spy = testUtils.sinon.spy();
  497. setElement( {
  498. tag: 'p',
  499. children: [
  500. {
  501. tag: 'span'
  502. }
  503. ],
  504. on: {
  505. x: bind.to( 'a' ),
  506. }
  507. } );
  508. observable.on( 'a', spy );
  509. dispatchEvent( el.firstChild, 'x' );
  510. sinon.assert.calledWithExactly( spy,
  511. sinon.match.has( 'name', 'a' ),
  512. sinon.match.has( 'target', el.firstChild )
  513. );
  514. } );
  515. it( 'works for future elements', () => {
  516. const spy = testUtils.sinon.spy();
  517. setElement( {
  518. tag: 'p',
  519. on: {
  520. 'test@div': bind.to( 'a' )
  521. }
  522. } );
  523. observable.on( 'a', spy );
  524. const div = document.createElement( 'div' );
  525. el.appendChild( div );
  526. dispatchEvent( div, 'test' );
  527. sinon.assert.calledWithExactly( spy, sinon.match.has( 'name', 'a' ), sinon.match.has( 'target', div ) );
  528. } );
  529. } );
  530. describe( 'model', () => {
  531. let observable, emitter, bind;
  532. beforeEach( () => {
  533. observable = new Model( {
  534. foo: 'bar',
  535. baz: 'qux'
  536. } );
  537. emitter = extend( {}, EmitterMixin );
  538. bind = Template.bind( observable, emitter );
  539. } );
  540. describe( 'to', () => {
  541. it( 'returns an object which describes the binding', () => {
  542. const spy = testUtils.sinon.spy();
  543. const binding = bind.to( 'foo', spy );
  544. expect( spy.called ).to.be.false;
  545. expect( binding ).to.have.keys( [ 'type', 'observable', 'eventNameOrFuncion', 'emitter', 'attribute', 'callback' ] );
  546. expect( binding.observable ).to.equal( observable );
  547. expect( binding.callback ).to.equal( spy );
  548. expect( binding.attribute ).to.equal( 'foo' );
  549. } );
  550. it( 'allows binding attribute to the observable – simple (HTMLElement attribute)', () => {
  551. setElement( {
  552. tag: 'p',
  553. attributes: {
  554. 'class': bind.to( 'foo' )
  555. },
  556. children: [ 'abc' ]
  557. } );
  558. expect( el.outerHTML ).to.equal( '<p class="bar">abc</p>' );
  559. observable.foo = 'baz';
  560. expect( el.outerHTML ).to.equal( '<p class="baz">abc</p>' );
  561. expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.be.null;
  562. } );
  563. it( 'allows binding attribute to the observable – simple (Text Node)', () => {
  564. setElement( {
  565. tag: 'p',
  566. children: [
  567. {
  568. text: bind.to( 'foo' )
  569. }
  570. ]
  571. } );
  572. expect( el.outerHTML ).to.equal( '<p>bar</p>' );
  573. observable.foo = 'baz';
  574. expect( el.outerHTML ).to.equal( '<p>baz</p>' );
  575. } );
  576. it( 'allows binding attribute to the observable – value processing', () => {
  577. const callback = value => value > 0 ? 'positive' : 'negative';
  578. setElement( {
  579. tag: 'p',
  580. attributes: {
  581. 'class': bind.to( 'foo', callback )
  582. },
  583. children: [
  584. {
  585. text: bind.to( 'foo', callback )
  586. }
  587. ]
  588. } );
  589. observable.foo = 3;
  590. expect( el.outerHTML ).to.equal( '<p class="positive">positive</p>' );
  591. observable.foo = -7;
  592. expect( el.outerHTML ).to.equal( '<p class="negative">negative</p>' );
  593. } );
  594. it( 'allows binding attribute to the observable – value processing (use Node)', () => {
  595. const callback = ( value, node ) => {
  596. return ( !!node.tagName && value > 0 ) ? 'HTMLElement positive' : '';
  597. };
  598. setElement( {
  599. tag: 'p',
  600. attributes: {
  601. 'class': bind.to( 'foo', callback )
  602. },
  603. children: [
  604. {
  605. text: bind.to( 'foo', callback )
  606. }
  607. ]
  608. } );
  609. observable.foo = 3;
  610. expect( el.outerHTML ).to.equal( '<p class="HTMLElement positive"></p>' );
  611. observable.foo = -7;
  612. expect( el.outerHTML ).to.equal( '<p></p>' );
  613. } );
  614. it( 'allows binding attribute to the observable – custom callback', () => {
  615. setElement( {
  616. tag: 'p',
  617. attributes: {
  618. 'class': bind.to( 'foo', ( value, el ) => {
  619. el.innerHTML = value;
  620. if ( value == 'changed' ) {
  621. return value;
  622. }
  623. } )
  624. }
  625. } );
  626. observable.foo = 'moo';
  627. expect( el.outerHTML ).to.equal( '<p class="undefined">moo</p>' );
  628. observable.foo = 'changed';
  629. expect( el.outerHTML ).to.equal( '<p class="changed">changed</p>' );
  630. } );
  631. it( 'allows binding attribute to the observable – array of bindings (HTMLElement attribute)', () => {
  632. setElement( {
  633. tag: 'p',
  634. attributes: {
  635. 'class': [
  636. 'ck-class',
  637. bind.to( 'foo' ),
  638. bind.to( 'baz' ),
  639. bind.to( 'foo', value => `foo-is-${value}` ),
  640. 'ck-end'
  641. ]
  642. },
  643. children: [ 'abc' ]
  644. } );
  645. observable.foo = 'a';
  646. observable.baz = 'b';
  647. expect( el.outerHTML ).to.equal( '<p class="ck-class a b foo-is-a ck-end">abc</p>' );
  648. observable.foo = 'c';
  649. observable.baz = 'd';
  650. expect( el.outerHTML ).to.equal( '<p class="ck-class c d foo-is-c ck-end">abc</p>' );
  651. } );
  652. it( 'allows binding attribute to the observable – array of bindings (Text Node)', () => {
  653. setElement( {
  654. tag: 'p',
  655. attributes: {
  656. },
  657. children: [
  658. {
  659. text: [
  660. 'ck-class',
  661. bind.to( 'foo' ),
  662. bind.to( 'baz' ),
  663. bind.to( 'foo', value => `foo-is-${value}` ),
  664. 'ck-end'
  665. ]
  666. }
  667. ]
  668. } );
  669. observable.foo = 'a';
  670. observable.baz = 'b';
  671. expect( el.outerHTML ).to.equal( '<p>ck-class a b foo-is-a ck-end</p>' );
  672. observable.foo = 'c';
  673. observable.baz = 'd';
  674. expect( el.outerHTML ).to.equal( '<p>ck-class c d foo-is-c ck-end</p>' );
  675. } );
  676. it( 'allows binding attribute to the observable – falsy values', () => {
  677. setElement( {
  678. tag: 'p',
  679. attributes: {
  680. 'class': bind.to( 'foo' )
  681. },
  682. children: [ 'abc' ]
  683. } );
  684. observable.foo = 'bar';
  685. expect( el.outerHTML ).to.equal( '<p class="bar">abc</p>' );
  686. observable.foo = false;
  687. expect( el.outerHTML ).to.equal( '<p class="false">abc</p>' );
  688. observable.foo = null;
  689. expect( el.outerHTML ).to.equal( '<p class="null">abc</p>' );
  690. observable.foo = undefined;
  691. expect( el.outerHTML ).to.equal( '<p class="undefined">abc</p>' );
  692. observable.foo = 0;
  693. expect( el.outerHTML ).to.equal( '<p class="0">abc</p>' );
  694. observable.foo = '';
  695. expect( el.outerHTML ).to.equal( '<p>abc</p>' );
  696. } );
  697. it( 'allows binding attribute to the observable – a custom namespace', () => {
  698. setElement( {
  699. tag: 'p',
  700. attributes: {
  701. class: {
  702. ns: 'foo',
  703. value: bind.to( 'foo' )
  704. },
  705. custom: {
  706. ns: 'foo',
  707. value: [
  708. bind.to( 'foo' ),
  709. bind.to( 'baz' )
  710. ]
  711. }
  712. },
  713. children: [ 'abc' ]
  714. } );
  715. observable.foo = 'bar';
  716. expect( el.outerHTML ).to.equal( '<p class="bar" custom="bar qux">abc</p>' );
  717. expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.equal( 'foo' );
  718. observable.foo = 'baz';
  719. expect( el.outerHTML ).to.equal( '<p class="baz" custom="baz qux">abc</p>' );
  720. expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.equal( 'foo' );
  721. } );
  722. } );
  723. describe( 'if', () => {
  724. it( 'returns an object which describes the binding', () => {
  725. const spy = testUtils.sinon.spy();
  726. const binding = bind.if( 'foo', 'whenTrue', spy );
  727. expect( spy.called ).to.be.false;
  728. expect( binding ).to.have.keys( [ 'type', 'observable', 'emitter', 'attribute', 'callback', 'valueIfTrue' ] );
  729. expect( binding.observable ).to.equal( observable );
  730. expect( binding.callback ).to.equal( spy );
  731. expect( binding.attribute ).to.equal( 'foo' );
  732. expect( binding.valueIfTrue ).to.equal( 'whenTrue' );
  733. } );
  734. it( 'allows binding attribute to the observable – presence of an attribute (HTMLElement attribute)', () => {
  735. setElement( {
  736. tag: 'p',
  737. attributes: {
  738. 'class': bind.if( 'foo' )
  739. },
  740. children: [ 'abc' ]
  741. } );
  742. observable.foo = true;
  743. expect( el.outerHTML ).to.equal( '<p class="">abc</p>' );
  744. observable.foo = false;
  745. expect( el.outerHTML ).to.equal( '<p>abc</p>' );
  746. observable.foo = 'bar';
  747. expect( el.outerHTML ).to.equal( '<p class="">abc</p>' );
  748. } );
  749. // TODO: Is this alright? It makes sense but it's pretty useless. Text Node cannot be
  750. // removed just like an attribute of some HTMLElement.
  751. it( 'allows binding attribute to the observable – presence of an attribute (Text Node)', () => {
  752. setElement( {
  753. tag: 'p',
  754. children: [
  755. {
  756. text: bind.if( 'foo' )
  757. }
  758. ]
  759. } );
  760. observable.foo = true;
  761. expect( el.outerHTML ).to.equal( '<p></p>' );
  762. observable.foo = false;
  763. expect( el.outerHTML ).to.equal( '<p></p>' );
  764. observable.foo = 'bar';
  765. expect( el.outerHTML ).to.equal( '<p></p>' );
  766. } );
  767. it( 'allows binding attribute to the observable – value of an attribute (HTMLElement attribute)', () => {
  768. setElement( {
  769. tag: 'p',
  770. attributes: {
  771. 'class': bind.if( 'foo', 'bar' )
  772. },
  773. children: [ 'abc' ]
  774. } );
  775. observable.foo = 'bar';
  776. expect( el.outerHTML ).to.equal( '<p class="bar">abc</p>' );
  777. observable.foo = false;
  778. expect( el.outerHTML ).to.equal( '<p>abc</p>' );
  779. observable.foo = 64;
  780. expect( el.outerHTML ).to.equal( '<p class="bar">abc</p>' );
  781. } );
  782. it( 'allows binding attribute to the observable – value of an attribute (Text Node)', () => {
  783. setElement( {
  784. tag: 'p',
  785. children: [
  786. {
  787. text: bind.if( 'foo', 'bar' )
  788. }
  789. ]
  790. } );
  791. observable.foo = 'bar';
  792. expect( el.outerHTML ).to.equal( '<p>bar</p>' );
  793. observable.foo = false;
  794. expect( el.outerHTML ).to.equal( '<p></p>' );
  795. observable.foo = 64;
  796. expect( el.outerHTML ).to.equal( '<p>bar</p>' );
  797. } );
  798. it( 'allows binding attribute to the observable – array of bindings (HTMLElement attribute)', () => {
  799. setElement( {
  800. tag: 'p',
  801. attributes: {
  802. 'class': [
  803. 'ck-class',
  804. bind.if( 'foo', 'foo-set' ),
  805. bind.if( 'bar', 'bar-not-set', ( value ) => !value ),
  806. 'ck-end'
  807. ]
  808. },
  809. children: [ 'abc' ]
  810. } );
  811. observable.foo = observable.bar = true;
  812. expect( el.outerHTML ).to.equal( '<p class="ck-class foo-set ck-end">abc</p>' );
  813. observable.foo = observable.bar = false;
  814. expect( el.outerHTML ).to.equal( '<p class="ck-class bar-not-set ck-end">abc</p>' );
  815. } );
  816. it( 'allows binding attribute to the observable – value of an attribute processed by a callback', () => {
  817. setElement( {
  818. tag: 'p',
  819. attributes: {
  820. 'class': bind.if( 'foo', 'there–is–no–foo', value => !value )
  821. },
  822. children: [ 'abc' ]
  823. } );
  824. observable.foo = 'bar';
  825. expect( el.outerHTML ).to.equal( '<p>abc</p>' );
  826. observable.foo = false;
  827. expect( el.outerHTML ).to.equal( '<p class="there–is–no–foo">abc</p>' );
  828. observable.foo = 64;
  829. expect( el.outerHTML ).to.equal( '<p>abc</p>' );
  830. } );
  831. it( 'allows binding attribute to the observable – value of an attribute processed by a callback (use Node)', () => {
  832. setElement( {
  833. tag: 'p',
  834. attributes: {
  835. 'class': bind.if( 'foo', 'eqls-tag-name', ( value, el ) => el.tagName === value )
  836. },
  837. children: [ 'abc' ]
  838. } );
  839. observable.foo = 'bar';
  840. expect( el.outerHTML ).to.equal( '<p>abc</p>' );
  841. observable.foo = 'P';
  842. expect( el.outerHTML ).to.equal( '<p class="eqls-tag-name">abc</p>' );
  843. observable.foo = 64;
  844. expect( el.outerHTML ).to.equal( '<p>abc</p>' );
  845. } );
  846. it( 'allows binding attribute to the observable – falsy values', () => {
  847. setElement( {
  848. tag: 'p',
  849. attributes: {
  850. 'class': bind.if( 'foo', 'foo-is-set' )
  851. },
  852. children: [ 'abc' ]
  853. } );
  854. observable.foo = 'bar';
  855. expect( el.outerHTML ).to.equal( '<p class="foo-is-set">abc</p>' );
  856. observable.foo = false;
  857. expect( el.outerHTML ).to.equal( '<p>abc</p>' );
  858. observable.foo = null;
  859. expect( el.outerHTML ).to.equal( '<p>abc</p>' );
  860. observable.foo = undefined;
  861. expect( el.outerHTML ).to.equal( '<p>abc</p>' );
  862. observable.foo = '';
  863. expect( el.outerHTML ).to.equal( '<p>abc</p>' );
  864. observable.foo = 0;
  865. expect( el.outerHTML ).to.equal( '<p>abc</p>' );
  866. } );
  867. } );
  868. it( 'works with Template#apply() – root element', () => {
  869. new Template( {
  870. tag: 'div',
  871. attributes: {
  872. class: bind.to( 'foo' )
  873. }
  874. } ).apply( el );
  875. expect( el.getAttribute( 'class' ) ).to.equal( 'bar' );
  876. observable.foo = 'baz';
  877. expect( el.getAttribute( 'class' ) ).to.equal( 'baz' );
  878. } );
  879. it( 'works with Template#apply() – children', () => {
  880. const el = document.createElement( 'div' );
  881. const child = document.createElement( 'span' );
  882. child.textContent = 'foo';
  883. el.appendChild( child );
  884. new Template( {
  885. tag: 'div',
  886. children: [
  887. {
  888. tag: 'span',
  889. children: [
  890. {
  891. text: bind.to( 'foo' )
  892. }
  893. ]
  894. }
  895. ]
  896. } ).apply( el );
  897. expect( child.textContent ).to.equal( 'bar' );
  898. observable.foo = 'baz';
  899. expect( child.textContent ).to.equal( 'baz' );
  900. } );
  901. } );
  902. } );
  903. describe( 'extend', () => {
  904. let observable, emitter, bind;
  905. beforeEach( () => {
  906. observable = new Model( {
  907. foo: 'bar',
  908. baz: 'qux'
  909. } );
  910. emitter = extend( {}, DOMEmitterMixin );
  911. bind = Template.bind( observable, emitter );
  912. } );
  913. describe( 'attributes', () => {
  914. it( 'extends existing - simple', () => {
  915. extensionTest(
  916. {
  917. tag: 'p',
  918. attributes: {
  919. a: 'b'
  920. }
  921. },
  922. {
  923. attributes: {
  924. a: 'c'
  925. }
  926. },
  927. '<p a="b c"></p>'
  928. );
  929. } );
  930. it( 'extends existing - complex #1', () => {
  931. extensionTest(
  932. {
  933. tag: 'p',
  934. attributes: {
  935. a: [ 'b', 'c' ]
  936. }
  937. },
  938. {
  939. attributes: {
  940. a: 'd'
  941. }
  942. },
  943. '<p a="b c d"></p>'
  944. );
  945. } );
  946. it( 'extends existing - complex #2', () => {
  947. extensionTest(
  948. {
  949. tag: 'p',
  950. attributes: {
  951. a: {
  952. value: 'b'
  953. }
  954. }
  955. },
  956. {
  957. attributes: {
  958. a: [ 'c', 'd' ]
  959. }
  960. },
  961. '<p a="b c d"></p>'
  962. );
  963. } );
  964. it( 'extends existing - complex #3', () => {
  965. extensionTest(
  966. {
  967. tag: 'p',
  968. attributes: {
  969. a: [ 'b' ]
  970. }
  971. },
  972. {
  973. attributes: {
  974. a: [ 'c', 'd' ]
  975. }
  976. },
  977. '<p a="b c d"></p>'
  978. );
  979. } );
  980. it( 'extends existing - bindings #1', () => {
  981. const el = extensionTest(
  982. {
  983. tag: 'p',
  984. attributes: {
  985. a: bind.to( 'foo' )
  986. }
  987. },
  988. {
  989. attributes: {
  990. a: [ 'c', 'd' ]
  991. }
  992. },
  993. '<p a="bar c d"></p>'
  994. );
  995. observable.foo = 'baz';
  996. expect( el.outerHTML ).to.equal( '<p a="baz c d"></p>' );
  997. } );
  998. it( 'extends existing - bindings #2', () => {
  999. const el = extensionTest(
  1000. {
  1001. tag: 'p',
  1002. attributes: {
  1003. a: [ 'b', bind.to( 'foo' ) ]
  1004. }
  1005. },
  1006. {
  1007. attributes: {
  1008. a: [ 'c', bind.to( 'baz' ) ]
  1009. }
  1010. },
  1011. '<p a="b bar c qux"></p>'
  1012. );
  1013. observable.foo = 'abc';
  1014. observable.baz = 'def';
  1015. expect( el.outerHTML ).to.equal( '<p a="b abc c def"></p>' );
  1016. } );
  1017. it( 'creates new - no attributes', () => {
  1018. extensionTest(
  1019. {
  1020. tag: 'p',
  1021. },
  1022. {
  1023. attributes: {
  1024. c: 'd'
  1025. }
  1026. },
  1027. '<p c="d"></p>'
  1028. );
  1029. } );
  1030. it( 'creates new - simple', () => {
  1031. extensionTest(
  1032. {
  1033. tag: 'p',
  1034. attributes: {
  1035. a: 'b'
  1036. }
  1037. },
  1038. {
  1039. attributes: {
  1040. c: 'd'
  1041. }
  1042. },
  1043. '<p a="b" c="d"></p>'
  1044. );
  1045. } );
  1046. it( 'creates new - array', () => {
  1047. extensionTest(
  1048. {
  1049. tag: 'p',
  1050. attributes: {
  1051. a: 'b'
  1052. }
  1053. },
  1054. {
  1055. attributes: {
  1056. c: [ 'd', 'e' ]
  1057. }
  1058. },
  1059. '<p a="b" c="d e"></p>'
  1060. );
  1061. } );
  1062. it( 'creates new - bindings #1', () => {
  1063. const el = extensionTest(
  1064. {
  1065. tag: 'p',
  1066. attributes: {
  1067. a: 'b'
  1068. }
  1069. },
  1070. {
  1071. attributes: {
  1072. c: bind.to( 'foo' )
  1073. }
  1074. },
  1075. '<p a="b" c="bar"></p>'
  1076. );
  1077. observable.foo = 'abc';
  1078. expect( el.outerHTML ).to.equal( '<p a="b" c="abc"></p>' );
  1079. } );
  1080. it( 'creates new - bindings #2', () => {
  1081. const el = extensionTest(
  1082. {
  1083. tag: 'p',
  1084. attributes: {
  1085. a: 'b'
  1086. }
  1087. },
  1088. {
  1089. attributes: {
  1090. c: [ 'd', bind.to( 'foo' ) ]
  1091. }
  1092. },
  1093. '<p a="b" c="d bar"></p>'
  1094. );
  1095. observable.foo = 'abc';
  1096. expect( el.outerHTML ).to.equal( '<p a="b" c="d abc"></p>' );
  1097. } );
  1098. } );
  1099. describe( 'text', () => {
  1100. it( 'extends existing - simple', () => {
  1101. const el = extensionTest(
  1102. {
  1103. tag: 'p',
  1104. children: [
  1105. 'foo'
  1106. ]
  1107. },
  1108. {
  1109. children: [
  1110. 'bar'
  1111. ]
  1112. },
  1113. '<p>foobar</p>'
  1114. );
  1115. expect( el.childNodes ).to.have.length( 1 );
  1116. } );
  1117. it( 'extends existing - complex #1', () => {
  1118. const el = extensionTest(
  1119. {
  1120. tag: 'p',
  1121. children: [
  1122. { text: 'foo' }
  1123. ]
  1124. },
  1125. {
  1126. children: [
  1127. 'bar'
  1128. ]
  1129. },
  1130. '<p>foobar</p>'
  1131. );
  1132. expect( el.childNodes ).to.have.length( 1 );
  1133. } );
  1134. it( 'extends existing - complex #2', () => {
  1135. const el = extensionTest(
  1136. {
  1137. tag: 'p',
  1138. children: [
  1139. { text: 'foo' }
  1140. ]
  1141. },
  1142. {
  1143. children: [
  1144. { text: 'bar' }
  1145. ]
  1146. },
  1147. '<p>foobar</p>'
  1148. );
  1149. expect( el.childNodes ).to.have.length( 1 );
  1150. } );
  1151. it( 'extends existing - bindings #1', () => {
  1152. const el = extensionTest(
  1153. {
  1154. tag: 'p',
  1155. children: [
  1156. { text: bind.to( 'foo' ) }
  1157. ]
  1158. },
  1159. {
  1160. children: [
  1161. 'abc'
  1162. ]
  1163. },
  1164. '<p>bar abc</p>'
  1165. );
  1166. observable.foo = 'asd';
  1167. expect( el.outerHTML ).to.equal( '<p>asd abc</p>' );
  1168. expect( el.childNodes ).to.have.length( 1 );
  1169. } );
  1170. it( 'extends existing - bindings #2', () => {
  1171. const el = extensionTest(
  1172. {
  1173. tag: 'p',
  1174. children: [
  1175. 'abc'
  1176. ]
  1177. },
  1178. {
  1179. children: [
  1180. { text: bind.to( 'foo' ) }
  1181. ]
  1182. },
  1183. '<p>abc bar</p>'
  1184. );
  1185. observable.foo = 'asd';
  1186. expect( el.outerHTML ).to.equal( '<p>abc asd</p>' );
  1187. expect( el.childNodes ).to.have.length( 1 );
  1188. } );
  1189. it( 'extends existing - bindings #3', () => {
  1190. const el = extensionTest(
  1191. {
  1192. tag: 'p',
  1193. children: [
  1194. { text: bind.to( 'foo' ) },
  1195. 'X'
  1196. ]
  1197. },
  1198. {
  1199. children: [
  1200. { text: bind.to( 'baz' ) },
  1201. 'Y'
  1202. ]
  1203. },
  1204. '<p>bar quxXY</p>'
  1205. );
  1206. observable.foo = 'A';
  1207. observable.baz = 'B';
  1208. expect( el.outerHTML ).to.equal( '<p>A BXY</p>' );
  1209. expect( el.childNodes ).to.have.length( 2 );
  1210. } );
  1211. } );
  1212. describe( 'children', () => {
  1213. it( 'should throw when the number of children does not correspond', () => {
  1214. expect( () => {
  1215. extensionTest(
  1216. {
  1217. tag: 'p',
  1218. children: [
  1219. 'foo'
  1220. ]
  1221. },
  1222. {
  1223. children: [
  1224. 'foo',
  1225. 'bar'
  1226. ]
  1227. },
  1228. 'it should fail'
  1229. );
  1230. } ).to.throw( CKEditorError, /ui-template-extend-children-mismatch/ );
  1231. } );
  1232. it( 'should throw when no children in target but extending one', () => {
  1233. expect( () => {
  1234. extensionTest(
  1235. {
  1236. tag: 'p',
  1237. },
  1238. {
  1239. children: [
  1240. {
  1241. tag: 'b'
  1242. }
  1243. ]
  1244. },
  1245. 'it should fail'
  1246. );
  1247. } ).to.throw( CKEditorError, /ui-template-extend-children-mismatch/ );
  1248. } );
  1249. it( 'should throw when the number of children does not correspond on some deeper level', () => {
  1250. expect( () => {
  1251. extensionTest(
  1252. {
  1253. tag: 'p',
  1254. children: [
  1255. {
  1256. tag: 'span',
  1257. attributes: {
  1258. class: 'A'
  1259. },
  1260. children: [
  1261. 'A',
  1262. {
  1263. tag: 'span',
  1264. attributes: {
  1265. class: 'AA'
  1266. },
  1267. children: [
  1268. 'AA'
  1269. ]
  1270. }
  1271. ]
  1272. }
  1273. ]
  1274. },
  1275. {
  1276. children: [
  1277. {
  1278. attributes: {
  1279. class: 'B'
  1280. },
  1281. children: [
  1282. 'B'
  1283. ]
  1284. }
  1285. ]
  1286. },
  1287. 'it should fail'
  1288. );
  1289. } ).to.throw( CKEditorError, /ui-template-extend-children-mismatch/ );
  1290. } );
  1291. it( 'extends existing - simple', () => {
  1292. extensionTest(
  1293. {
  1294. tag: 'p',
  1295. children: [
  1296. {
  1297. tag: 'span',
  1298. attributes: {
  1299. class: 'foo'
  1300. }
  1301. }
  1302. ]
  1303. },
  1304. {
  1305. children: [
  1306. {
  1307. tag: 'span',
  1308. attributes: {
  1309. class: 'bar'
  1310. }
  1311. }
  1312. ]
  1313. },
  1314. '<p><span class="foo bar"></span></p>'
  1315. );
  1316. } );
  1317. it( 'extends existing - complex', () => {
  1318. extensionTest(
  1319. {
  1320. tag: 'p',
  1321. children: [
  1322. {
  1323. tag: 'span',
  1324. attributes: {
  1325. class: 'A'
  1326. },
  1327. children: [
  1328. 'A',
  1329. {
  1330. tag: 'span',
  1331. attributes: {
  1332. class: 'AA'
  1333. },
  1334. children: [
  1335. 'AA'
  1336. ]
  1337. }
  1338. ]
  1339. }
  1340. ]
  1341. },
  1342. {
  1343. children: [
  1344. {
  1345. tag: 'span',
  1346. attributes: {
  1347. class: 'B'
  1348. },
  1349. children: [
  1350. 'B',
  1351. {
  1352. tag: 'span',
  1353. attributes: {
  1354. class: 'BB'
  1355. },
  1356. children: [
  1357. 'BB'
  1358. ]
  1359. }
  1360. ]
  1361. }
  1362. ]
  1363. },
  1364. '<p><span class="A B">AB<span class="AA BB">AABB</span></span></p>'
  1365. );
  1366. } );
  1367. it( 'allows extending a particular child', () => {
  1368. const template = new Template( {
  1369. tag: 'p',
  1370. children: [
  1371. {
  1372. tag: 'span',
  1373. attributes: {
  1374. class: 'foo'
  1375. }
  1376. }
  1377. ]
  1378. } );
  1379. Template.extend( template.definition.children[ 0 ], {
  1380. attributes: {
  1381. class: 'bar'
  1382. }
  1383. } );
  1384. expect( template.render().outerHTML ).to.equal( '<p><span class="foo bar"></span></p>' );
  1385. } );
  1386. it( 'allows extending a particular child – recursively', () => {
  1387. const template = new Template( {
  1388. tag: 'p',
  1389. children: [
  1390. {
  1391. tag: 'span',
  1392. attributes: {
  1393. class: 'A'
  1394. },
  1395. children: [
  1396. 'A',
  1397. {
  1398. tag: 'span',
  1399. attributes: {
  1400. class: 'AA'
  1401. },
  1402. children: [
  1403. 'AA'
  1404. ]
  1405. }
  1406. ]
  1407. }
  1408. ]
  1409. } );
  1410. Template.extend( template.definition.children[ 0 ], {
  1411. attributes: {
  1412. class: 'B',
  1413. },
  1414. children: [
  1415. 'B',
  1416. {
  1417. attributes: {
  1418. class: 'BB'
  1419. }
  1420. }
  1421. ]
  1422. } );
  1423. expect( template.render().outerHTML ).to.equal( '<p><span class="A B">AB<span class="AA BB">AA</span></span></p>' );
  1424. } );
  1425. } );
  1426. describe( 'listeners', () => {
  1427. it( 'extends existing', () => {
  1428. const spy1 = testUtils.sinon.spy();
  1429. const spy2 = testUtils.sinon.spy();
  1430. const spy3 = testUtils.sinon.spy();
  1431. const spy4 = testUtils.sinon.spy();
  1432. const spy5 = testUtils.sinon.spy();
  1433. observable.on( 'A', spy1 );
  1434. observable.on( 'C', spy2 );
  1435. observable.on( 'B', spy3 );
  1436. observable.on( 'D', spy4 );
  1437. const el = extensionTest(
  1438. {
  1439. tag: 'p',
  1440. children: [
  1441. {
  1442. tag: 'span'
  1443. }
  1444. ],
  1445. on: {
  1446. click: bind.to( 'A' ),
  1447. 'click@span': [
  1448. bind.to( 'B' ),
  1449. bind.to( spy5 )
  1450. ]
  1451. }
  1452. },
  1453. {
  1454. on: {
  1455. click: bind.to( 'C' ),
  1456. 'click@span': bind.to( 'D' )
  1457. }
  1458. },
  1459. '<p><span></span></p>'
  1460. );
  1461. dispatchEvent( el, 'click' );
  1462. expect( spy1.calledOnce ).to.be.true;
  1463. expect( spy2.calledOnce ).to.be.true;
  1464. expect( spy3.called ).to.be.false;
  1465. expect( spy4.called ).to.be.false;
  1466. expect( spy5.called ).to.be.false;
  1467. dispatchEvent( el.firstChild, 'click' );
  1468. expect( spy1.calledTwice ).to.be.true;
  1469. expect( spy2.calledTwice ).to.be.true;
  1470. expect( spy3.calledOnce ).to.be.true;
  1471. expect( spy4.calledOnce ).to.be.true;
  1472. expect( spy5.calledOnce ).to.be.true;
  1473. } );
  1474. it( 'creates new', () => {
  1475. const spy1 = testUtils.sinon.spy();
  1476. const spy2 = testUtils.sinon.spy();
  1477. const spy3 = testUtils.sinon.spy();
  1478. observable.on( 'A', spy1 );
  1479. observable.on( 'B', spy2 );
  1480. const el = extensionTest(
  1481. {
  1482. tag: 'p',
  1483. children: [
  1484. {
  1485. tag: 'span'
  1486. }
  1487. ],
  1488. },
  1489. {
  1490. on: {
  1491. click: bind.to( 'A' ),
  1492. 'click@span': [
  1493. bind.to( 'B' ),
  1494. bind.to( spy3 )
  1495. ]
  1496. }
  1497. },
  1498. '<p><span></span></p>'
  1499. );
  1500. dispatchEvent( el, 'click' );
  1501. expect( spy1.calledOnce ).to.be.true;
  1502. expect( spy2.called ).to.be.false;
  1503. expect( spy3.called ).to.be.false;
  1504. dispatchEvent( el.firstChild, 'click' );
  1505. expect( spy1.calledTwice ).to.be.true;
  1506. expect( spy2.calledOnce ).to.be.true;
  1507. expect( spy3.calledOnce ).to.be.true;
  1508. } );
  1509. } );
  1510. } );
  1511. } );
  1512. function setElement( template ) {
  1513. el = new Template( template ).render();
  1514. document.body.appendChild( el );
  1515. }
  1516. function extensionTest( base, extension, expectedHtml ) {
  1517. const template = new Template( base );
  1518. Template.extend( template, extension );
  1519. const el = template.render();
  1520. document.body.appendChild( el );
  1521. expect( el.outerHTML ).to.equal( expectedHtml );
  1522. return el;
  1523. }
  1524. function dispatchEvent( el, domEvtName ) {
  1525. if ( !el.parentNode ) {
  1526. throw new Error( 'To dispatch an event, element must be in DOM. Otherwise #target is null.' );
  1527. }
  1528. el.dispatchEvent( new Event( domEvtName, {
  1529. bubbles: true
  1530. } ) );
  1531. }