template.js 43 KB

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