template.js 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  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 - simple', () => {
  1018. extensionTest(
  1019. {
  1020. tag: 'p',
  1021. attributes: {
  1022. a: 'b'
  1023. }
  1024. },
  1025. {
  1026. attributes: {
  1027. c: 'd'
  1028. }
  1029. },
  1030. '<p a="b" c="d"></p>'
  1031. );
  1032. } );
  1033. it( 'creates new - array', () => {
  1034. extensionTest(
  1035. {
  1036. tag: 'p',
  1037. attributes: {
  1038. a: 'b'
  1039. }
  1040. },
  1041. {
  1042. attributes: {
  1043. c: [ 'd', 'e' ]
  1044. }
  1045. },
  1046. '<p a="b" c="d e"></p>'
  1047. );
  1048. } );
  1049. it( 'creates new - bindings #1', () => {
  1050. const el = extensionTest(
  1051. {
  1052. tag: 'p',
  1053. attributes: {
  1054. a: 'b'
  1055. }
  1056. },
  1057. {
  1058. attributes: {
  1059. c: bind.to( 'foo' )
  1060. }
  1061. },
  1062. '<p a="b" c="bar"></p>'
  1063. );
  1064. observable.foo = 'abc';
  1065. expect( el.outerHTML ).to.equal( '<p a="b" c="abc"></p>' );
  1066. } );
  1067. it( 'creates new - bindings #2', () => {
  1068. const el = extensionTest(
  1069. {
  1070. tag: 'p',
  1071. attributes: {
  1072. a: 'b'
  1073. }
  1074. },
  1075. {
  1076. attributes: {
  1077. c: [ 'd', bind.to( 'foo' ) ]
  1078. }
  1079. },
  1080. '<p a="b" c="d bar"></p>'
  1081. );
  1082. observable.foo = 'abc';
  1083. expect( el.outerHTML ).to.equal( '<p a="b" c="d abc"></p>' );
  1084. } );
  1085. } );
  1086. describe( 'text', () => {
  1087. it( 'extends existing - simple', () => {
  1088. const el = extensionTest(
  1089. {
  1090. tag: 'p',
  1091. children: [
  1092. 'foo'
  1093. ]
  1094. },
  1095. {
  1096. children: [
  1097. 'bar'
  1098. ]
  1099. },
  1100. '<p>foobar</p>'
  1101. );
  1102. expect( el.childNodes ).to.have.length( 1 );
  1103. } );
  1104. it( 'extends existing - complex #1', () => {
  1105. const el = extensionTest(
  1106. {
  1107. tag: 'p',
  1108. children: [
  1109. { text: 'foo' }
  1110. ]
  1111. },
  1112. {
  1113. children: [
  1114. 'bar'
  1115. ]
  1116. },
  1117. '<p>foobar</p>'
  1118. );
  1119. expect( el.childNodes ).to.have.length( 1 );
  1120. } );
  1121. it( 'extends existing - complex #2', () => {
  1122. const el = extensionTest(
  1123. {
  1124. tag: 'p',
  1125. children: [
  1126. { text: 'foo' }
  1127. ]
  1128. },
  1129. {
  1130. children: [
  1131. { text: 'bar' }
  1132. ]
  1133. },
  1134. '<p>foobar</p>'
  1135. );
  1136. expect( el.childNodes ).to.have.length( 1 );
  1137. } );
  1138. it( 'extends existing - bindings #1', () => {
  1139. const el = extensionTest(
  1140. {
  1141. tag: 'p',
  1142. children: [
  1143. { text: bind.to( 'foo' ) }
  1144. ]
  1145. },
  1146. {
  1147. children: [
  1148. 'abc'
  1149. ]
  1150. },
  1151. '<p>bar abc</p>'
  1152. );
  1153. observable.foo = 'asd';
  1154. expect( el.outerHTML ).to.equal( '<p>asd abc</p>' );
  1155. expect( el.childNodes ).to.have.length( 1 );
  1156. } );
  1157. it( 'extends existing - bindings #2', () => {
  1158. const el = extensionTest(
  1159. {
  1160. tag: 'p',
  1161. children: [
  1162. 'abc'
  1163. ]
  1164. },
  1165. {
  1166. children: [
  1167. { text: bind.to( 'foo' ) }
  1168. ]
  1169. },
  1170. '<p>abc bar</p>'
  1171. );
  1172. observable.foo = 'asd';
  1173. expect( el.outerHTML ).to.equal( '<p>abc asd</p>' );
  1174. expect( el.childNodes ).to.have.length( 1 );
  1175. } );
  1176. it( 'extends existing - bindings #3', () => {
  1177. const el = extensionTest(
  1178. {
  1179. tag: 'p',
  1180. children: [
  1181. { text: bind.to( 'foo' ) },
  1182. 'X'
  1183. ]
  1184. },
  1185. {
  1186. children: [
  1187. { text: bind.to( 'baz' ) },
  1188. 'Y'
  1189. ]
  1190. },
  1191. '<p>bar quxXY</p>'
  1192. );
  1193. observable.foo = 'A';
  1194. observable.baz = 'B';
  1195. expect( el.outerHTML ).to.equal( '<p>A BXY</p>' );
  1196. expect( el.childNodes ).to.have.length( 2 );
  1197. } );
  1198. } );
  1199. describe( 'children', () => {
  1200. it( 'should throw when the number of children does not correspond', () => {
  1201. expect( () => {
  1202. extensionTest(
  1203. {
  1204. tag: 'p',
  1205. children: [
  1206. 'foo'
  1207. ]
  1208. },
  1209. {
  1210. children: [
  1211. 'foo',
  1212. 'bar'
  1213. ]
  1214. },
  1215. 'it should fail'
  1216. );
  1217. } ).to.throw( CKEditorError, /ui-template-extend-children-mismatch/ );
  1218. } );
  1219. it( 'should throw when no children in target but extending one', () => {
  1220. expect( () => {
  1221. extensionTest(
  1222. {
  1223. tag: 'p',
  1224. },
  1225. {
  1226. children: [
  1227. {
  1228. tag: 'b'
  1229. }
  1230. ]
  1231. },
  1232. 'it should fail'
  1233. );
  1234. } ).to.throw( CKEditorError, /ui-template-extend-children-mismatch/ );
  1235. } );
  1236. it( 'should throw when the number of children does not correspond on some deeper level', () => {
  1237. expect( () => {
  1238. extensionTest(
  1239. {
  1240. tag: 'p',
  1241. children: [
  1242. {
  1243. tag: 'span',
  1244. attributes: {
  1245. class: 'A'
  1246. },
  1247. children: [
  1248. 'A',
  1249. {
  1250. tag: 'span',
  1251. attributes: {
  1252. class: 'AA'
  1253. },
  1254. children: [
  1255. 'AA'
  1256. ]
  1257. }
  1258. ]
  1259. }
  1260. ]
  1261. },
  1262. {
  1263. children: [
  1264. {
  1265. attributes: {
  1266. class: 'B'
  1267. },
  1268. children: [
  1269. 'B'
  1270. ]
  1271. }
  1272. ]
  1273. },
  1274. 'it should fail'
  1275. );
  1276. } ).to.throw( CKEditorError, /ui-template-extend-children-mismatch/ );
  1277. } );
  1278. it( 'extends existing - simple', () => {
  1279. extensionTest(
  1280. {
  1281. tag: 'p',
  1282. children: [
  1283. {
  1284. tag: 'span',
  1285. attributes: {
  1286. class: 'foo'
  1287. }
  1288. }
  1289. ]
  1290. },
  1291. {
  1292. children: [
  1293. {
  1294. tag: 'span',
  1295. attributes: {
  1296. class: 'bar'
  1297. }
  1298. }
  1299. ]
  1300. },
  1301. '<p><span class="foo bar"></span></p>'
  1302. );
  1303. } );
  1304. it( 'extends existing - complex', () => {
  1305. extensionTest(
  1306. {
  1307. tag: 'p',
  1308. children: [
  1309. {
  1310. tag: 'span',
  1311. attributes: {
  1312. class: 'A'
  1313. },
  1314. children: [
  1315. 'A',
  1316. {
  1317. tag: 'span',
  1318. attributes: {
  1319. class: 'AA'
  1320. },
  1321. children: [
  1322. 'AA'
  1323. ]
  1324. }
  1325. ]
  1326. }
  1327. ]
  1328. },
  1329. {
  1330. children: [
  1331. {
  1332. tag: 'span',
  1333. attributes: {
  1334. class: 'B'
  1335. },
  1336. children: [
  1337. 'B',
  1338. {
  1339. tag: 'span',
  1340. attributes: {
  1341. class: 'BB'
  1342. },
  1343. children: [
  1344. 'BB'
  1345. ]
  1346. }
  1347. ]
  1348. }
  1349. ]
  1350. },
  1351. '<p><span class="A B">AB<span class="AA BB">AABB</span></span></p>'
  1352. );
  1353. } );
  1354. it( 'allows extending a particular child', () => {
  1355. const template = new Template( {
  1356. tag: 'p',
  1357. children: [
  1358. {
  1359. tag: 'span',
  1360. attributes: {
  1361. class: 'foo'
  1362. }
  1363. }
  1364. ]
  1365. } );
  1366. Template.extend( template.definition.children[ 0 ], {
  1367. attributes: {
  1368. class: 'bar'
  1369. }
  1370. } );
  1371. expect( template.render().outerHTML ).to.equal( '<p><span class="foo bar"></span></p>' );
  1372. } );
  1373. it( 'allows extending a particular child – recursively', () => {
  1374. const template = new Template( {
  1375. tag: 'p',
  1376. children: [
  1377. {
  1378. tag: 'span',
  1379. attributes: {
  1380. class: 'A'
  1381. },
  1382. children: [
  1383. 'A',
  1384. {
  1385. tag: 'span',
  1386. attributes: {
  1387. class: 'AA'
  1388. },
  1389. children: [
  1390. 'AA'
  1391. ]
  1392. }
  1393. ]
  1394. }
  1395. ]
  1396. } );
  1397. Template.extend( template.definition.children[ 0 ], {
  1398. attributes: {
  1399. class: 'B',
  1400. },
  1401. children: [
  1402. 'B',
  1403. {
  1404. attributes: {
  1405. class: 'BB'
  1406. }
  1407. }
  1408. ]
  1409. } );
  1410. expect( template.render().outerHTML ).to.equal( '<p><span class="A B">AB<span class="AA BB">AA</span></span></p>' );
  1411. } );
  1412. } );
  1413. describe( 'listeners', () => {
  1414. it( 'extends existing', () => {
  1415. const spy1 = testUtils.sinon.spy();
  1416. const spy2 = testUtils.sinon.spy();
  1417. const spy3 = testUtils.sinon.spy();
  1418. const spy4 = testUtils.sinon.spy();
  1419. const spy5 = testUtils.sinon.spy();
  1420. observable.on( 'A', spy1 );
  1421. observable.on( 'C', spy2 );
  1422. observable.on( 'B', spy3 );
  1423. observable.on( 'D', spy4 );
  1424. const el = extensionTest(
  1425. {
  1426. tag: 'p',
  1427. children: [
  1428. {
  1429. tag: 'span'
  1430. }
  1431. ],
  1432. on: {
  1433. click: bind.to( 'A' ),
  1434. 'click@span': [
  1435. bind.to( 'B' ),
  1436. bind.to( spy5 )
  1437. ]
  1438. }
  1439. },
  1440. {
  1441. on: {
  1442. click: bind.to( 'C' ),
  1443. 'click@span': bind.to( 'D' )
  1444. }
  1445. },
  1446. '<p><span></span></p>'
  1447. );
  1448. dispatchEvent( el, 'click' );
  1449. expect( spy1.calledOnce ).to.be.true;
  1450. expect( spy2.calledOnce ).to.be.true;
  1451. expect( spy3.called ).to.be.false;
  1452. expect( spy4.called ).to.be.false;
  1453. expect( spy5.called ).to.be.false;
  1454. dispatchEvent( el.firstChild, 'click' );
  1455. expect( spy1.calledTwice ).to.be.true;
  1456. expect( spy2.calledTwice ).to.be.true;
  1457. expect( spy3.calledOnce ).to.be.true;
  1458. expect( spy4.calledOnce ).to.be.true;
  1459. expect( spy5.calledOnce ).to.be.true;
  1460. } );
  1461. it( 'creates new', () => {
  1462. const spy1 = testUtils.sinon.spy();
  1463. const spy2 = testUtils.sinon.spy();
  1464. const spy3 = testUtils.sinon.spy();
  1465. observable.on( 'A', spy1 );
  1466. observable.on( 'B', spy2 );
  1467. const el = extensionTest(
  1468. {
  1469. tag: 'p',
  1470. children: [
  1471. {
  1472. tag: 'span'
  1473. }
  1474. ],
  1475. },
  1476. {
  1477. on: {
  1478. click: bind.to( 'A' ),
  1479. 'click@span': [
  1480. bind.to( 'B' ),
  1481. bind.to( spy3 )
  1482. ]
  1483. }
  1484. },
  1485. '<p><span></span></p>'
  1486. );
  1487. dispatchEvent( el, 'click' );
  1488. expect( spy1.calledOnce ).to.be.true;
  1489. expect( spy2.called ).to.be.false;
  1490. expect( spy3.called ).to.be.false;
  1491. dispatchEvent( el.firstChild, 'click' );
  1492. expect( spy1.calledTwice ).to.be.true;
  1493. expect( spy2.calledOnce ).to.be.true;
  1494. expect( spy3.calledOnce ).to.be.true;
  1495. } );
  1496. } );
  1497. } );
  1498. } );
  1499. function setElement( template ) {
  1500. el = new Template( template ).render();
  1501. document.body.appendChild( el );
  1502. }
  1503. function extensionTest( base, extension, expectedHtml ) {
  1504. const template = new Template( base );
  1505. Template.extend( template, extension );
  1506. const el = template.render();
  1507. document.body.appendChild( el );
  1508. expect( el.outerHTML ).to.equal( expectedHtml );
  1509. return el;
  1510. }
  1511. function dispatchEvent( el, domEvtName ) {
  1512. if ( !el.parentNode ) {
  1513. throw new Error( 'To dispatch an event, element must be in DOM. Otherwise #target is null.' );
  1514. }
  1515. el.dispatchEvent( new Event( domEvtName, {
  1516. bubbles: true
  1517. } ) );
  1518. }