template.js 41 KB

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