template.js 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals HTMLElement, Event, document */
  6. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import { default as Template, TemplateToBinding, TemplateIfBinding } from '../src/template';
  8. import View from '../src/view';
  9. import ViewCollection from '../src/viewcollection';
  10. import Model from '../src/model';
  11. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  12. import DomEmitterMixin from '@ckeditor/ckeditor5-utils/src/dom/emittermixin';
  13. import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
  14. import log from '@ckeditor/ckeditor5-utils/src/log';
  15. let el, text;
  16. const injectedElements = [];
  17. describe( 'Template', () => {
  18. testUtils.createSinonSandbox();
  19. // Clean-up document.body from the rendered elements.
  20. afterEach( () => {
  21. for ( const el of injectedElements ) {
  22. el.remove();
  23. }
  24. } );
  25. describe( 'constructor()', () => {
  26. it( 'sets #_isRendered property', () => {
  27. expect( new Template( { tag: 'p' } )._isRendered ).to.be.false;
  28. } );
  29. it( 'accepts and normalizes the definition', () => {
  30. const bind = Template.bind( new Model( {} ), Object.create( DomEmitterMixin ) );
  31. const childNode = document.createElement( 'div' );
  32. const childTemplate = new Template( {
  33. tag: 'b'
  34. } );
  35. const tpl = new Template( {
  36. tag: 'p',
  37. attributes: {
  38. a: 'foo',
  39. b: [ 'bar', 'baz' ],
  40. c: {
  41. ns: 'abc',
  42. value: bind.to( 'qux' )
  43. }
  44. },
  45. children: [
  46. {
  47. text: 'content'
  48. },
  49. {
  50. text: bind.to( 'x' )
  51. },
  52. 'abc',
  53. {
  54. text: [ 'a', 'b' ]
  55. },
  56. childNode,
  57. childTemplate
  58. ],
  59. on: {
  60. 'a@span': bind.to( 'b' ),
  61. 'b@span': bind.to( () => {} ),
  62. 'c@span': [
  63. bind.to( 'c' ),
  64. bind.to( () => {} )
  65. ]
  66. }
  67. } );
  68. expect( tpl.attributes.a[ 0 ] ).to.equal( 'foo' );
  69. expect( tpl.attributes.b[ 0 ] ).to.equal( 'bar' );
  70. expect( tpl.attributes.b[ 1 ] ).to.equal( 'baz' );
  71. expect( tpl.attributes.c[ 0 ].value[ 0 ] ).to.be.instanceof( TemplateToBinding );
  72. expect( tpl.children ).to.have.length( 6 );
  73. expect( tpl.children[ 0 ].text[ 0 ] ).to.equal( 'content' );
  74. expect( tpl.children[ 1 ].text[ 0 ] ).to.be.instanceof( TemplateToBinding );
  75. expect( tpl.children[ 2 ].text[ 0 ] ).to.equal( 'abc' );
  76. expect( tpl.children[ 3 ].text[ 0 ] ).to.equal( 'a' );
  77. expect( tpl.children[ 3 ].text[ 1 ] ).to.equal( 'b' );
  78. expect( tpl.children[ 4 ] ).to.equal( childNode );
  79. expect( tpl.children[ 5 ] ).to.equal( childTemplate );
  80. expect( tpl.eventListeners[ 'a@span' ][ 0 ] ).to.be.instanceof( TemplateToBinding );
  81. expect( tpl.eventListeners[ 'b@span' ][ 0 ] ).to.be.instanceof( TemplateToBinding );
  82. expect( tpl.eventListeners[ 'c@span' ][ 0 ] ).to.be.instanceof( TemplateToBinding );
  83. expect( tpl.eventListeners[ 'c@span' ][ 1 ] ).to.be.instanceof( TemplateToBinding );
  84. // Note that Template mixes EmitterMixin.
  85. expect( tpl.on ).to.be.a( 'function' );
  86. expect( tpl.on[ 'a@span' ] ).to.be.undefined;
  87. } );
  88. it( 'defines #children collection', () => {
  89. const elementTpl = new Template( {
  90. tag: 'p',
  91. } );
  92. const textTpl = new Template( {
  93. text: 'foo'
  94. } );
  95. expect( elementTpl.children ).to.be.an( 'array' );
  96. expect( elementTpl.children ).to.have.length( 0 );
  97. // Text will never have children.
  98. expect( textTpl.children ).to.be.undefined;
  99. } );
  100. it( 'does not modify passed definition', () => {
  101. const def = {
  102. tag: 'p',
  103. attributes: {
  104. a: 'foo',
  105. },
  106. children: [
  107. {
  108. tag: 'span'
  109. }
  110. ]
  111. };
  112. const tpl = new Template( def );
  113. expect( def.attributes ).to.not.equal( tpl.attributes );
  114. expect( def.children ).to.not.equal( tpl.children );
  115. expect( def.children[ 0 ] ).to.not.equal( tpl.children[ 0 ] );
  116. expect( def.attributes.a ).to.equal( 'foo' );
  117. expect( def.children[ 0 ].tag ).to.equal( 'span' );
  118. expect( tpl.attributes.a[ 0 ] ).to.equal( 'foo' );
  119. expect( tpl.children[ 0 ].tag ).to.equal( 'span' );
  120. } );
  121. } );
  122. describe( 'render()', () => {
  123. it( 'throws when the template definition is wrong', () => {
  124. expectToThrowCKEditorError( () => {
  125. new Template( {} ).render();
  126. }, /ui-template-wrong-syntax/ );
  127. expectToThrowCKEditorError( () => {
  128. new Template( {
  129. tag: 'p',
  130. text: 'foo'
  131. } ).render();
  132. }, /ui-template-wrong-syntax/ );
  133. } );
  134. it( 'sets #_isRendered true', () => {
  135. const tpl = new Template( { tag: 'p' } );
  136. expect( tpl._isRendered ).to.be.false;
  137. tpl.render();
  138. expect( tpl._isRendered ).to.be.true;
  139. } );
  140. describe( 'DOM Node', () => {
  141. it( 'creates HTMLElement', () => {
  142. const el = new Template( {
  143. tag: 'p',
  144. } ).render();
  145. expect( el ).to.be.instanceof( HTMLElement );
  146. expect( el.parentNode ).to.be.null;
  147. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  148. expect( el.namespaceURI ).to.equal( 'http://www.w3.org/1999/xhtml' );
  149. } );
  150. it( 'creates an element in a custom namespace', () => {
  151. const el = new Template( {
  152. tag: 'p',
  153. ns: 'foo'
  154. } ).render();
  155. expect( el.namespaceURI ).to.equal( 'foo' );
  156. } );
  157. it( 'creates a Text node', () => {
  158. const node = new Template( { text: 'foo' } ).render();
  159. expect( node.nodeType ).to.equal( 3 );
  160. expect( node.textContent ).to.equal( 'foo' );
  161. } );
  162. } );
  163. describe( 'attributes', () => {
  164. it( 'renders HTMLElement attributes', () => {
  165. const el = new Template( {
  166. tag: 'p',
  167. attributes: {
  168. 'class': [ 'a', 'b' ],
  169. x: 'bar'
  170. },
  171. children: [ 'foo' ]
  172. } ).render();
  173. expect( el ).to.be.instanceof( HTMLElement );
  174. expect( el.parentNode ).to.be.null;
  175. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="a b" x="bar">foo</p>' );
  176. expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.be.null;
  177. } );
  178. it( 'renders HTMLElement attributes – empty', () => {
  179. const el = new Template( {
  180. tag: 'p',
  181. attributes: {
  182. class: '',
  183. x: [ '', '' ]
  184. },
  185. children: [ 'foo' ]
  186. } ).render();
  187. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>foo</p>' );
  188. } );
  189. it( 'renders HTMLElement attributes – falsy values', () => {
  190. const el = new Template( {
  191. tag: 'p',
  192. attributes: {
  193. class: false,
  194. x: [ '', null, undefined, false ],
  195. y: [ 'foo', null, undefined, false ]
  196. },
  197. children: [ 'foo' ]
  198. } ).render();
  199. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p y="foo">foo</p>' );
  200. } );
  201. it( 'renders HTMLElement attributes in a custom namespace', () => {
  202. const el = new Template( {
  203. tag: 'p',
  204. attributes: {
  205. class: {
  206. ns: 'foo',
  207. value: [ 'a', 'b' ]
  208. },
  209. x: {
  210. ns: 'abc',
  211. value: 'bar'
  212. }
  213. },
  214. children: [ 'foo' ]
  215. } ).render();
  216. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="a b" x="bar">foo</p>' );
  217. expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.equal( 'foo' );
  218. expect( el.attributes.getNamedItem( 'x' ).namespaceURI ).to.equal( 'abc' );
  219. } );
  220. describe( 'style', () => {
  221. let observable, emitter, bind;
  222. beforeEach( () => {
  223. observable = new Model( {
  224. width: '10px',
  225. backgroundColor: 'yellow'
  226. } );
  227. emitter = Object.create( EmitterMixin );
  228. bind = Template.bind( observable, emitter );
  229. } );
  230. it( 'renders as a static value', () => {
  231. setElement( {
  232. tag: 'p',
  233. attributes: {
  234. style: 'color: red'
  235. }
  236. } );
  237. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="color:red"></p>' );
  238. } );
  239. it( 'renders as a static value (Array of values)', () => {
  240. setElement( {
  241. tag: 'p',
  242. attributes: {
  243. style: {
  244. color: 'red',
  245. display: 'block'
  246. }
  247. }
  248. } );
  249. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="color:red;display:block"></p>' );
  250. } );
  251. it( 'renders as a value bound to the model', () => {
  252. setElement( {
  253. tag: 'p',
  254. attributes: {
  255. style: bind.to( 'width', w => `width: ${ w }` )
  256. }
  257. } );
  258. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="width:10px"></p>' );
  259. observable.width = '1em';
  260. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="width:1em"></p>' );
  261. } );
  262. it( 'renders as a value bound to the model (Array of bindings)', () => {
  263. setElement( {
  264. tag: 'p',
  265. attributes: {
  266. style: [
  267. bind.to( 'width', w => `width: ${ w };` ),
  268. bind.to( 'backgroundColor', c => `background-color: ${ c };` )
  269. ]
  270. }
  271. } );
  272. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="background-color:yellow;width:10px"></p>' );
  273. observable.width = '1em';
  274. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="background-color:yellow;width:1em"></p>' );
  275. } );
  276. describe( 'object', () => {
  277. it( 'renders with static and bound attributes', () => {
  278. setElement( {
  279. tag: 'p',
  280. attributes: {
  281. style: {
  282. width: bind.to( 'width' ),
  283. height: '10px',
  284. backgroundColor: bind.to( 'backgroundColor' )
  285. }
  286. }
  287. } );
  288. expect( normalizeHtml( el.outerHTML ) )
  289. .to.equal( '<p style="background-color:yellow;height:10px;width:10px"></p>' );
  290. observable.width = '20px';
  291. observable.backgroundColor = 'green';
  292. expect( normalizeHtml( el.outerHTML ) )
  293. .to.equal( '<p style="background-color:green;height:10px;width:20px"></p>' );
  294. } );
  295. it( 'renders with empty string attributes', () => {
  296. setElement( {
  297. tag: 'p',
  298. attributes: {
  299. style: {
  300. width: '',
  301. backgroundColor: bind.to( 'backgroundColor' )
  302. }
  303. }
  304. } );
  305. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="background-color:yellow"></p>' );
  306. observable.backgroundColor = '';
  307. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  308. } );
  309. it( 'renders with falsy values', () => {
  310. setElement( {
  311. tag: 'p',
  312. attributes: {
  313. style: {
  314. width: null,
  315. height: false,
  316. color: undefined
  317. }
  318. }
  319. } );
  320. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  321. } );
  322. } );
  323. } );
  324. } );
  325. describe( 'children', () => {
  326. it( 'creates HTMLElement children', () => {
  327. const el = new Template( {
  328. tag: 'p',
  329. attributes: {
  330. a: 'A'
  331. },
  332. children: [
  333. {
  334. tag: 'b',
  335. children: [ 'B' ]
  336. },
  337. {
  338. tag: 'i',
  339. children: [
  340. 'C',
  341. {
  342. tag: 'b',
  343. children: [ 'D' ]
  344. }
  345. ]
  346. }
  347. ]
  348. } ).render();
  349. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p a="A"><b>B</b><i>C<b>D</b></i></p>' );
  350. } );
  351. it( 'creates a child Text Node (different syntaxes)', () => {
  352. const el = new Template( {
  353. tag: 'p',
  354. children: [
  355. 'foo',
  356. { text: 'bar' }
  357. ]
  358. } ).render();
  359. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>foobar</p>' );
  360. } );
  361. it( 'creates multiple child Text Nodes', () => {
  362. const el = new Template( {
  363. tag: 'p',
  364. children: [
  365. 'a',
  366. 'b',
  367. { text: 'c' },
  368. 'd',
  369. { text: [ 'e', 'f' ] }
  370. ]
  371. } ).render();
  372. expect( el.childNodes ).to.have.length( 5 );
  373. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abcdef</p>' );
  374. } );
  375. it( 'renders view children', () => {
  376. const v1 = getView( {
  377. tag: 'b',
  378. attributes: {
  379. class: [
  380. 'v1'
  381. ]
  382. }
  383. } );
  384. const v2 = getView( {
  385. tag: 'b',
  386. attributes: {
  387. class: [
  388. 'v2'
  389. ]
  390. }
  391. } );
  392. const v3 = getView( {
  393. tag: 'b',
  394. attributes: {
  395. class: [
  396. 'v3'
  397. ]
  398. }
  399. } );
  400. v3.render();
  401. const tpl = new Template( {
  402. tag: 'p',
  403. children: [ v1, v2, v3 ]
  404. } );
  405. expect( tpl.children[ 0 ] ).to.equal( v1 );
  406. expect( tpl.children[ 1 ] ).to.equal( v2 );
  407. expect( tpl.children[ 2 ] ).to.equal( v3 );
  408. const rendered = tpl.render();
  409. expect( normalizeHtml( rendered.outerHTML ) )
  410. .to.equal( '<p><b class="v1"></b><b class="v2"></b><b class="v3"></b></p>' );
  411. // Make sure the child views will not re–render their elements but
  412. // use ones rendered by the template instance above.
  413. expect( v1.element ).to.equal( rendered.firstChild );
  414. expect( v2.element ).to.equal( rendered.children[ 1 ] );
  415. expect( v3.element ).to.equal( rendered.lastChild );
  416. } );
  417. it( 'renders view collection', () => {
  418. const collection = new ViewCollection();
  419. const v1 = getView( {
  420. tag: 'span',
  421. attributes: {
  422. class: [
  423. 'v1'
  424. ]
  425. }
  426. } );
  427. const v2 = getView( {
  428. tag: 'span',
  429. attributes: {
  430. class: [
  431. 'v2'
  432. ]
  433. }
  434. } );
  435. collection.add( v1 );
  436. collection.add( v2 );
  437. const rendered = new Template( {
  438. tag: 'p',
  439. children: collection
  440. } ).render();
  441. expect( normalizeHtml( rendered.outerHTML ) ).to.equal( '<p><span class="v1"></span><span class="v2"></span></p>' );
  442. // Make sure the child views will not re–render their elements but
  443. // use ones rendered by the template instance above.
  444. expect( v1.element ).to.equal( rendered.firstChild );
  445. expect( v2.element ).to.equal( rendered.lastChild );
  446. expect( collection._parentElement ).to.equal( rendered );
  447. } );
  448. it( 'renders DOM nodes', () => {
  449. const view = new View();
  450. view.set( {
  451. foo: 'bar',
  452. bar: 'baz'
  453. } );
  454. const bind = Template.bind( view, view );
  455. const childA = new Template( {
  456. tag: 'b',
  457. attributes: {
  458. class: bind.to( 'foo' )
  459. }
  460. } ).render();
  461. const childB = new Template( {
  462. text: bind.to( 'bar' )
  463. } ).render();
  464. const rendered = new Template( {
  465. tag: 'p',
  466. children: [
  467. childA,
  468. childB
  469. ]
  470. } ).render();
  471. expect( normalizeHtml( rendered.outerHTML ) ).to.equal( '<p><b class="bar"></b>baz</p>' );
  472. } );
  473. // #117
  474. it( 'renders template children', () => {
  475. const childTplA = new Template( {
  476. tag: 'a'
  477. } );
  478. const childTplB = new Template( {
  479. tag: 'b'
  480. } );
  481. const view = new View();
  482. view.set( {
  483. foo: 'bar',
  484. bar: 'foo'
  485. } );
  486. const bind = Template.bind( view, view );
  487. // Yes, this TC is crazy in some sort of way. It's all about template
  488. // normalization and deep cloning and the like.
  489. //
  490. // To **really** prove the code is safe there must be a View instance,
  491. // which has some child bound to its attribute and... there must be a
  492. // Template instance (below), which also has a child bound to the same attribute.
  493. //
  494. // I know, the view instance and its template aren't even rendered.
  495. //
  496. // The truth is that madness behind this test case is so deep there are no
  497. // words to explain it. But what actually matters is that it proves the Template
  498. // class is free of "Maximum call stack size exceeded" error in certain
  499. // situations.
  500. view.setTemplate( {
  501. tag: 'span',
  502. children: [
  503. {
  504. text: bind.to( 'bar' )
  505. }
  506. ]
  507. } );
  508. const childTplC = new Template( {
  509. tag: 'i',
  510. children: [
  511. {
  512. text: bind.to( 'bar' )
  513. }
  514. ]
  515. } );
  516. const tpl = new Template( {
  517. tag: 'p',
  518. children: [
  519. childTplA,
  520. childTplB,
  521. childTplC
  522. ]
  523. } );
  524. // Make sure child instances weren't cloned.
  525. expect( tpl.children[ 0 ] ).to.equal( childTplA );
  526. expect( tpl.children[ 1 ] ).to.equal( childTplB );
  527. expect( tpl.children[ 2 ] ).to.equal( childTplC );
  528. expect( normalizeHtml( tpl.render().outerHTML ) ).to.equal(
  529. '<p><a></a><b></b><i>foo</i></p>'
  530. );
  531. } );
  532. // https://github.com/ckeditor/ckeditor5-ui/issues/289
  533. it( 'does not throw when child does not have an "id" property', () => {
  534. const strongView = getView( {
  535. tag: 'strong'
  536. } );
  537. strongView.set( 'id' );
  538. expectToThrowCKEditorError( () => {
  539. getView( {
  540. tag: 'div',
  541. children: [
  542. strongView
  543. ]
  544. } );
  545. } ).to.not.throw();
  546. } );
  547. } );
  548. describe( 'bindings', () => {
  549. it( 'activates model bindings – root', () => {
  550. const observable = new Model( {
  551. foo: 'bar'
  552. } );
  553. const emitter = Object.create( EmitterMixin );
  554. const bind = Template.bind( observable, emitter );
  555. const el = new Template( {
  556. tag: 'div',
  557. attributes: {
  558. class: bind.to( 'foo' )
  559. }
  560. } ).render();
  561. expect( el.getAttribute( 'class' ) ).to.equal( 'bar' );
  562. observable.foo = 'baz';
  563. expect( el.getAttribute( 'class' ) ).to.equal( 'baz' );
  564. } );
  565. it( 'activates model bindings – children', () => {
  566. const observable = new Model( {
  567. foo: 'bar'
  568. } );
  569. const emitter = Object.create( EmitterMixin );
  570. const bind = Template.bind( observable, emitter );
  571. const el = new Template( {
  572. tag: 'div',
  573. children: [
  574. {
  575. tag: 'span',
  576. children: [
  577. {
  578. text: [
  579. bind.to( 'foo' ),
  580. 'static'
  581. ]
  582. }
  583. ]
  584. }
  585. ]
  586. } ).render();
  587. expect( el.firstChild.textContent ).to.equal( 'bar static' );
  588. observable.foo = 'baz';
  589. expect( el.firstChild.textContent ).to.equal( 'baz static' );
  590. } );
  591. } );
  592. } );
  593. describe( 'apply()', () => {
  594. let observable, domEmitter, bind;
  595. beforeEach( () => {
  596. setElement( { tag: 'div' } );
  597. text = document.createTextNode( '' );
  598. observable = new Model( {
  599. foo: 'bar',
  600. baz: 'qux'
  601. } );
  602. domEmitter = Object.create( DomEmitterMixin );
  603. bind = Template.bind( observable, domEmitter );
  604. } );
  605. it( 'throws when wrong template definition', () => {
  606. expectToThrowCKEditorError( () => {
  607. new Template( {
  608. tag: 'p',
  609. text: 'foo'
  610. } ).apply( el );
  611. }, /ui-template-wrong-syntax/ );
  612. } );
  613. it( 'accepts empty template definition', () => {
  614. new Template( {} ).apply( el );
  615. new Template( {} ).apply( text );
  616. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div></div>' );
  617. expect( text.textContent ).to.equal( '' );
  618. } );
  619. describe( 'text', () => {
  620. it( 'applies textContent to a Text Node', () => {
  621. new Template( {
  622. text: 'abc'
  623. } ).apply( text );
  624. expect( text.textContent ).to.equal( 'abc' );
  625. } );
  626. it( 'overrides existing textContent of a Text Node', () => {
  627. text.textContent = 'foo';
  628. new Template( {
  629. text: bind.to( 'foo' )
  630. } ).apply( text );
  631. expect( text.textContent ).to.equal( 'bar' );
  632. observable.foo = 'qux';
  633. expect( text.textContent ).to.equal( 'qux' );
  634. } );
  635. it( 'overrides textContent of an existing Text Node in a HTMLElement', () => {
  636. el.textContent = 'bar';
  637. new Template( {
  638. tag: 'div',
  639. children: [ 'foo' ]
  640. } ).apply( el );
  641. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div>foo</div>' );
  642. } );
  643. } );
  644. describe( 'attributes', () => {
  645. it( 'applies attributes to an HTMLElement', () => {
  646. new Template( {
  647. tag: 'div',
  648. attributes: {
  649. 'class': [ 'a', 'b' ],
  650. x: 'bar'
  651. }
  652. } ).apply( el );
  653. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div class="a b" x="bar"></div>' );
  654. } );
  655. it( 'manages existing attribute values ("class" vs. "non–class")', () => {
  656. el.setAttribute( 'class', 'default' );
  657. el.setAttribute( 'x', 'foo' );
  658. new Template( {
  659. tag: 'div',
  660. attributes: {
  661. 'class': [ 'a', 'b' ],
  662. x: 'bar'
  663. }
  664. } ).apply( el );
  665. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  666. '<div class="a b default" x="bar"></div>'
  667. );
  668. } );
  669. it( 'applies attributes and TextContent to a DOM tree', () => {
  670. el.textContent = 'abc';
  671. el.appendChild( getElement( { tag: 'span' } ) );
  672. new Template( {
  673. tag: 'div',
  674. attributes: {
  675. 'class': 'parent'
  676. },
  677. children: [
  678. 'Children:',
  679. {
  680. tag: 'span',
  681. attributes: {
  682. class: 'child'
  683. }
  684. }
  685. ]
  686. } ).apply( el );
  687. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div class="parent">Children:<span class="child"></span></div>' );
  688. } );
  689. describe( 'style', () => {
  690. beforeEach( () => {
  691. observable = new Model( {
  692. width: '10px',
  693. backgroundColor: 'yellow'
  694. } );
  695. bind = Template.bind( observable, domEmitter );
  696. } );
  697. it( 'applies as a static value', () => {
  698. setElement( {
  699. tag: 'p',
  700. attributes: {
  701. style: 'color: red;'
  702. }
  703. } );
  704. new Template( {
  705. attributes: {
  706. style: 'display: block'
  707. }
  708. } ).apply( el );
  709. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="color:red;display:block"></p>' );
  710. } );
  711. it( 'applies as a static value (Array of values)', () => {
  712. setElement( {
  713. tag: 'p',
  714. attributes: {
  715. style: [ 'color: red;', 'display: block;' ]
  716. }
  717. } );
  718. new Template( {
  719. attributes: {
  720. style: [ 'float: left;', 'overflow: hidden;' ]
  721. }
  722. } ).apply( el );
  723. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  724. '<p style="color:red;display:block;float:left;overflow:hidden"></p>'
  725. );
  726. } );
  727. it( 'applies when in an object syntax', () => {
  728. setElement( {
  729. tag: 'p',
  730. attributes: {
  731. style: {
  732. width: '20px',
  733. }
  734. }
  735. } );
  736. new Template( {
  737. attributes: {
  738. style: {
  739. height: '10px',
  740. float: 'left',
  741. backgroundColor: 'green'
  742. }
  743. }
  744. } ).apply( el );
  745. expect( normalizeHtml( el.outerHTML ) )
  746. .to.equal( '<p style="background-color:green;float:left;height:10px;width:20px"></p>' );
  747. } );
  748. it( 'applies when bound to observable', () => {
  749. setElement( {
  750. tag: 'p',
  751. attributes: {
  752. style: {
  753. left: '20px',
  754. }
  755. }
  756. } );
  757. new Template( {
  758. attributes: {
  759. style: {
  760. width: bind.to( 'width' ),
  761. float: 'left',
  762. backgroundColor: 'green'
  763. }
  764. }
  765. } ).apply( el );
  766. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  767. '<p style="background-color:green;float:left;left:20px;width:10px"></p>'
  768. );
  769. observable.width = '100px';
  770. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  771. '<p style="background-color:green;float:left;left:20px;width:100px"></p>'
  772. );
  773. } );
  774. } );
  775. } );
  776. describe( 'children', () => {
  777. it( 'doesn\'t apply new child to an HTMLElement – Text Node', () => {
  778. new Template( {
  779. tag: 'div',
  780. children: [ 'foo' ]
  781. } ).apply( el );
  782. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div></div>' );
  783. } );
  784. it( 'doesn\'t apply new child to an HTMLElement – HTMLElement', () => {
  785. new Template( {
  786. tag: 'div',
  787. children: [
  788. {
  789. tag: 'span'
  790. }
  791. ]
  792. } ).apply( el );
  793. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div></div>' );
  794. } );
  795. it( 'doesn\'t apply new child to an HTMLElement – view', () => {
  796. const view = getView( {
  797. tag: 'span',
  798. attributes: {
  799. class: [
  800. 'v1'
  801. ]
  802. }
  803. } );
  804. new Template( {
  805. tag: 'p',
  806. children: [ view ]
  807. } ).apply( el );
  808. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div></div>' );
  809. } );
  810. it( 'doesn\'t apply new child to an HTMLElement – view collection', () => {
  811. const collection = new ViewCollection();
  812. collection.add( getView( {
  813. tag: 'span',
  814. attributes: {
  815. class: [
  816. 'v1'
  817. ]
  818. }
  819. } ) );
  820. new Template( {
  821. tag: 'p',
  822. children: collection
  823. } ).apply( el );
  824. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div></div>' );
  825. expect( collection._parentElement ).to.be.null;
  826. } );
  827. it( 'should work for deep DOM structure with bindings and event listeners', () => {
  828. const childA = getElement( {
  829. tag: 'a',
  830. attributes: {
  831. class: 'a1 a2'
  832. },
  833. children: [
  834. 'a'
  835. ]
  836. } );
  837. const childB = getElement( {
  838. tag: 'b',
  839. attributes: {
  840. class: 'b1 b2'
  841. },
  842. children: [
  843. 'b'
  844. ]
  845. } );
  846. el.appendChild( childA );
  847. el.appendChild( childB );
  848. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  849. '<div><a class="a1 a2">a</a><b class="b1 b2">b</b></div>'
  850. );
  851. const spy1 = testUtils.sinon.spy();
  852. const spy2 = testUtils.sinon.spy();
  853. const spy3 = testUtils.sinon.spy();
  854. observable.on( 'ku', spy1 );
  855. observable.on( 'kd', spy2 );
  856. observable.on( 'mo', spy3 );
  857. new Template( {
  858. tag: 'div',
  859. children: [
  860. {
  861. tag: 'a',
  862. on: {
  863. keyup: bind.to( 'ku' )
  864. },
  865. attributes: {
  866. class: bind.to( 'foo', val => 'applied-A-' + val ),
  867. id: 'applied-A'
  868. },
  869. children: [ 'applied-a' ]
  870. },
  871. {
  872. tag: 'b',
  873. on: {
  874. keydown: bind.to( 'kd' )
  875. },
  876. attributes: {
  877. class: bind.to( 'baz', val => 'applied-B-' + val ),
  878. id: 'applied-B'
  879. },
  880. children: [ 'applied-b' ]
  881. },
  882. 'Text which is not to be applied because it does NOT exist in original element.'
  883. ],
  884. on: {
  885. 'mouseover@a': bind.to( 'mo' )
  886. },
  887. attributes: {
  888. id: bind.to( 'foo', val => val.toUpperCase() ),
  889. class: bind.to( 'baz', val => 'applied-parent-' + val )
  890. }
  891. } ).apply( el );
  892. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div class="applied-parent-qux" id="BAR">' +
  893. '<a class="a1 a2 applied-A-bar" id="applied-A">applied-a</a>' +
  894. '<b class="applied-B-qux b1 b2" id="applied-B">applied-b</b>' +
  895. '</div>' );
  896. observable.foo = 'updated';
  897. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div class="applied-parent-qux" id="UPDATED">' +
  898. '<a class="a1 a2 applied-A-updated" id="applied-A">applied-a</a>' +
  899. '<b class="applied-B-qux b1 b2" id="applied-B">applied-b</b>' +
  900. '</div>' );
  901. document.body.appendChild( el );
  902. // Test "mouseover@a".
  903. dispatchEvent( el, 'mouseover' );
  904. dispatchEvent( childA, 'mouseover' );
  905. // Test "keyup".
  906. dispatchEvent( childA, 'keyup' );
  907. // Test "keydown".
  908. dispatchEvent( childB, 'keydown' );
  909. sinon.assert.calledOnce( spy1 );
  910. sinon.assert.calledOnce( spy2 );
  911. sinon.assert.calledOnce( spy3 );
  912. } );
  913. } );
  914. } );
  915. describe( 'revert()', () => {
  916. let observable, domEmitter, bind;
  917. beforeEach( () => {
  918. el = getElement( { tag: 'div' } );
  919. observable = new Model( {
  920. foo: 'bar',
  921. baz: 'qux'
  922. } );
  923. domEmitter = Object.create( DomEmitterMixin );
  924. bind = Template.bind( observable, domEmitter );
  925. } );
  926. it( 'should throw if template is not applied', () => {
  927. const tpl = new Template( {
  928. tag: 'div'
  929. } );
  930. expectToThrowCKEditorError( () => {
  931. tpl.revert( el );
  932. }, /ui-template-revert-not-applied/ );
  933. tpl.render();
  934. expectToThrowCKEditorError( () => {
  935. tpl.revert( el );
  936. }, /ui-template-revert-not-applied/ );
  937. } );
  938. describe( 'text', () => {
  939. it( 'should revert textContent to the initial value', () => {
  940. el = getElement( {
  941. tag: 'a',
  942. children: [
  943. 'a',
  944. {
  945. tag: 'b',
  946. children: [
  947. 'b'
  948. ]
  949. }
  950. ]
  951. } );
  952. const tpl = new Template( {
  953. children: [
  954. 'bar',
  955. {
  956. children: [
  957. 'qux'
  958. ]
  959. }
  960. ]
  961. } );
  962. tpl.apply( el );
  963. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  964. '<a>bar<b>qux</b></a>'
  965. );
  966. tpl.revert( el );
  967. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  968. '<a>a<b>b</b></a>'
  969. );
  970. } );
  971. it( 'should remove bindings', () => {
  972. el = getElement( {
  973. tag: 'a',
  974. children: [
  975. 'a',
  976. {
  977. tag: 'b',
  978. children: [
  979. 'b'
  980. ]
  981. }
  982. ]
  983. } );
  984. const tpl = new Template( {
  985. children: [
  986. 'foo',
  987. {
  988. children: [
  989. {
  990. text: bind.to( 'foo' )
  991. }
  992. ]
  993. }
  994. ]
  995. } );
  996. tpl.apply( el );
  997. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  998. '<a>foo<b>bar</b></a>'
  999. );
  1000. observable.foo = 'abc';
  1001. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1002. '<a>foo<b>abc</b></a>'
  1003. );
  1004. tpl.revert( el );
  1005. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1006. '<a>a<b>b</b></a>'
  1007. );
  1008. observable.foo = 'xyz';
  1009. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1010. '<a>a<b>b</b></a>'
  1011. );
  1012. } );
  1013. } );
  1014. describe( 'attributes', () => {
  1015. it( 'should revert attributes to the initial values', () => {
  1016. el = getElement( {
  1017. tag: 'a',
  1018. attributes: {
  1019. foo: 'af',
  1020. bar: 'ab',
  1021. },
  1022. children: [
  1023. {
  1024. tag: 'b',
  1025. attributes: {
  1026. foo: 'bf',
  1027. bar: 'bb',
  1028. }
  1029. }
  1030. ]
  1031. } );
  1032. const tpl = new Template( {
  1033. attributes: {
  1034. foo: 'af1',
  1035. bar: [ 'ab1', 'ab2' ],
  1036. baz: 'x'
  1037. },
  1038. children: [
  1039. {
  1040. attributes: {
  1041. foo: 'bf1'
  1042. }
  1043. }
  1044. ]
  1045. } );
  1046. tpl.apply( el );
  1047. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1048. '<a bar="ab1 ab2" baz="x" foo="af1">' +
  1049. '<b bar="bb" foo="bf1"></b>' +
  1050. '</a>'
  1051. );
  1052. tpl.revert( el );
  1053. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1054. '<a bar="ab" foo="af">' +
  1055. '<b bar="bb" foo="bf"></b>' +
  1056. '</a>'
  1057. );
  1058. } );
  1059. it( 'should remove bindings', () => {
  1060. el = getElement( {
  1061. tag: 'a',
  1062. attributes: {
  1063. foo: 'af',
  1064. bar: 'ab',
  1065. },
  1066. children: [
  1067. {
  1068. tag: 'b',
  1069. attributes: {
  1070. foo: 'bf',
  1071. bar: 'bb',
  1072. }
  1073. }
  1074. ]
  1075. } );
  1076. const tpl = new Template( {
  1077. attributes: {
  1078. foo: 'af1',
  1079. bar: [
  1080. 'ab1',
  1081. bind.to( 'baz' )
  1082. ]
  1083. },
  1084. children: [
  1085. {
  1086. attributes: {
  1087. foo: bind.to( 'foo' )
  1088. }
  1089. }
  1090. ]
  1091. } );
  1092. tpl.apply( el );
  1093. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1094. '<a bar="ab1 qux" foo="af1">' +
  1095. '<b bar="bb" foo="bar"></b>' +
  1096. '</a>'
  1097. );
  1098. observable.foo = 'x';
  1099. observable.baz = 'y';
  1100. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1101. '<a bar="ab1 y" foo="af1">' +
  1102. '<b bar="bb" foo="x"></b>' +
  1103. '</a>'
  1104. );
  1105. tpl.revert( el );
  1106. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1107. '<a bar="ab" foo="af">' +
  1108. '<b bar="bb" foo="bf"></b>' +
  1109. '</a>'
  1110. );
  1111. observable.foo = 'abc';
  1112. observable.baz = 'cba';
  1113. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1114. '<a bar="ab" foo="af">' +
  1115. '<b bar="bb" foo="bf"></b>' +
  1116. '</a>'
  1117. );
  1118. } );
  1119. describe( 'style', () => {
  1120. beforeEach( () => {
  1121. observable = new Model( {
  1122. overflow: 'visible'
  1123. } );
  1124. bind = Template.bind( observable, domEmitter );
  1125. } );
  1126. it( 'should remove bindings', () => {
  1127. el = getElement( {
  1128. tag: 'a',
  1129. attributes: {
  1130. style: {
  1131. fontWeight: 'bold'
  1132. }
  1133. },
  1134. children: [
  1135. {
  1136. tag: 'b',
  1137. attributes: {
  1138. style: {
  1139. color: 'red'
  1140. }
  1141. }
  1142. }
  1143. ]
  1144. } );
  1145. const tpl = new Template( {
  1146. attributes: {
  1147. style: {
  1148. overflow: bind.to( 'overflow' )
  1149. }
  1150. },
  1151. children: [
  1152. {
  1153. tag: 'b',
  1154. attributes: {
  1155. style: {
  1156. display: 'block'
  1157. }
  1158. }
  1159. }
  1160. ]
  1161. } );
  1162. tpl.apply( el );
  1163. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1164. '<a style="font-weight:bold;overflow:visible">' +
  1165. '<b style="color:red;display:block"></b>' +
  1166. '</a>'
  1167. );
  1168. tpl.revert( el );
  1169. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1170. '<a style="font-weight:bold">' +
  1171. '<b style="color:red"></b>' +
  1172. '</a>'
  1173. );
  1174. observable.overflow = 'hidden';
  1175. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1176. '<a style="font-weight:bold">' +
  1177. '<b style="color:red"></b>' +
  1178. '</a>'
  1179. );
  1180. } );
  1181. } );
  1182. } );
  1183. describe( 'children', () => {
  1184. it( 'should work for deep DOM structure with bindings and event listeners', () => {
  1185. el = getElement( {
  1186. tag: 'div',
  1187. children: [
  1188. {
  1189. tag: 'a',
  1190. attributes: {
  1191. class: [ 'a1', 'a2' ]
  1192. },
  1193. children: [
  1194. 'a'
  1195. ]
  1196. },
  1197. {
  1198. tag: 'b',
  1199. attributes: {
  1200. class: [ 'b1', 'b2' ]
  1201. },
  1202. children: [
  1203. 'b'
  1204. ]
  1205. }
  1206. ]
  1207. } );
  1208. const spy = sinon.spy();
  1209. observable.on( 'ku', spy );
  1210. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1211. '<div><a class="a1 a2">a</a><b class="b1 b2">b</b></div>'
  1212. );
  1213. const tpl = new Template( {
  1214. tag: 'div',
  1215. attributes: {
  1216. class: [ 'div1', 'div2' ],
  1217. style: {
  1218. fontWeight: 'bold'
  1219. }
  1220. },
  1221. children: [
  1222. {
  1223. tag: 'a',
  1224. attributes: {
  1225. class: [ 'x', 'y' ],
  1226. 'data-new-attr': 'foo'
  1227. },
  1228. children: [ 'applied-a' ]
  1229. },
  1230. {
  1231. tag: 'b',
  1232. attributes: {
  1233. class: [
  1234. 'a',
  1235. 'b',
  1236. bind.to( 'foo' )
  1237. ]
  1238. },
  1239. children: [ 'applied-b' ]
  1240. }
  1241. ],
  1242. on: {
  1243. keyup: bind.to( 'ku' )
  1244. }
  1245. } );
  1246. tpl.apply( el );
  1247. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1248. '<div class="div1 div2" style="font-weight:bold">' +
  1249. '<a class="a1 a2 x y" data-new-attr="foo">applied-a</a>' +
  1250. '<b class="a b b1 b2 bar">applied-b</b>' +
  1251. '</div>'
  1252. );
  1253. observable.foo = 'baz';
  1254. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1255. '<div class="div1 div2" style="font-weight:bold">' +
  1256. '<a class="a1 a2 x y" data-new-attr="foo">applied-a</a>' +
  1257. '<b class="a b b1 b2 baz">applied-b</b>' +
  1258. '</div>'
  1259. );
  1260. dispatchEvent( el.firstChild, 'keyup' );
  1261. sinon.assert.calledOnce( spy );
  1262. tpl.revert( el );
  1263. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1264. '<div><a class="a1 a2">a</a><b class="b1 b2">b</b></div>'
  1265. );
  1266. observable.foo = 'qux';
  1267. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1268. '<div><a class="a1 a2">a</a><b class="b1 b2">b</b></div>'
  1269. );
  1270. dispatchEvent( el.firstChild, 'keyup' );
  1271. sinon.assert.calledOnce( spy );
  1272. } );
  1273. } );
  1274. } );
  1275. describe( 'getViews()', () => {
  1276. it( 'returns iterator', () => {
  1277. const template = new Template( {} );
  1278. expect( template.getViews().next ).to.be.a( 'function' );
  1279. expect( Array.from( template.getViews() ) ).to.have.length( 0 );
  1280. } );
  1281. it( 'returns all child views', () => {
  1282. const viewA = new View();
  1283. const viewB = new View();
  1284. const viewC = new View();
  1285. const template = new Template( {
  1286. tag: 'div',
  1287. children: [
  1288. viewA,
  1289. {
  1290. tag: 'div',
  1291. children: [
  1292. viewB
  1293. ]
  1294. },
  1295. viewC
  1296. ]
  1297. } );
  1298. expect( Array.from( template.getViews() ) ).to.have.members( [ viewA, viewB, viewC ] );
  1299. } );
  1300. // https://github.com/ckeditor/ckeditor5-ui/issues/337
  1301. it( 'does not traverse non–Template children', () => {
  1302. const viewA = new View();
  1303. const viewB = new View();
  1304. const viewC = new View();
  1305. const template = new Template( {
  1306. tag: 'div',
  1307. children: [
  1308. viewA,
  1309. ]
  1310. } );
  1311. // Technically, this kind of child is invalid but the aim of this test is to
  1312. // check if the generator will accidentally traverse non–Template objects
  1313. // like native HTML elements, which also have "children" property. It could happen
  1314. // because it is possible to pass HTML elements directly to the templateDefinition.
  1315. template.children.push( {
  1316. children: [ viewB ]
  1317. } );
  1318. template.children.push( viewC );
  1319. expect( Array.from( template.getViews() ) ).to.have.members( [ viewA, viewC ] );
  1320. } );
  1321. } );
  1322. describe( 'bind()', () => {
  1323. it( 'returns object', () => {
  1324. expect( Template.bind() ).to.be.an( 'object' );
  1325. } );
  1326. it( 'provides "to" and "if" interface', () => {
  1327. const bind = Template.bind();
  1328. expect( bind ).to.have.keys( 'to', 'if' );
  1329. expect( bind.to ).to.be.a( 'function' );
  1330. expect( bind.if ).to.be.a( 'function' );
  1331. } );
  1332. describe( 'event', () => {
  1333. let observable, domEmitter, bind;
  1334. beforeEach( () => {
  1335. observable = new Model( {
  1336. foo: 'bar',
  1337. baz: 'qux'
  1338. } );
  1339. domEmitter = Object.create( DomEmitterMixin );
  1340. bind = Template.bind( observable, domEmitter );
  1341. } );
  1342. it( 'accepts plain binding', () => {
  1343. const spy = testUtils.sinon.spy();
  1344. setElement( {
  1345. tag: 'p',
  1346. on: {
  1347. x: bind.to( 'a' ),
  1348. }
  1349. } );
  1350. observable.on( 'a', spy );
  1351. dispatchEvent( el, 'x' );
  1352. sinon.assert.calledWithExactly( spy,
  1353. sinon.match.has( 'name', 'a' ),
  1354. sinon.match.has( 'target', el )
  1355. );
  1356. } );
  1357. it( 'accepts an array of event bindings', () => {
  1358. const spy1 = testUtils.sinon.spy();
  1359. const spy2 = testUtils.sinon.spy();
  1360. setElement( {
  1361. tag: 'p',
  1362. on: {
  1363. x: [
  1364. bind.to( 'a' ),
  1365. bind.to( 'b' )
  1366. ]
  1367. }
  1368. } );
  1369. observable.on( 'a', spy1 );
  1370. observable.on( 'b', spy2 );
  1371. dispatchEvent( el, 'x' );
  1372. sinon.assert.calledWithExactly( spy1,
  1373. sinon.match.has( 'name', 'a' ),
  1374. sinon.match.has( 'target', el )
  1375. );
  1376. sinon.assert.calledWithExactly( spy2,
  1377. sinon.match.has( 'name', 'b' ),
  1378. sinon.match.has( 'target', el )
  1379. );
  1380. } );
  1381. it( 'accepts DOM selectors', () => {
  1382. const spy1 = testUtils.sinon.spy();
  1383. const spy2 = testUtils.sinon.spy();
  1384. const spy3 = testUtils.sinon.spy();
  1385. setElement( {
  1386. tag: 'p',
  1387. children: [
  1388. {
  1389. tag: 'span',
  1390. attributes: {
  1391. 'class': 'y',
  1392. },
  1393. on: {
  1394. 'test@p': bind.to( 'c' )
  1395. }
  1396. },
  1397. {
  1398. tag: 'div',
  1399. children: [
  1400. {
  1401. tag: 'span',
  1402. attributes: {
  1403. 'class': 'y',
  1404. }
  1405. }
  1406. ],
  1407. }
  1408. ],
  1409. on: {
  1410. 'test@.y': bind.to( 'a' ),
  1411. 'test@div': bind.to( 'b' )
  1412. }
  1413. } );
  1414. observable.on( 'a', spy1 );
  1415. observable.on( 'b', spy2 );
  1416. observable.on( 'c', spy3 );
  1417. // Test "test@p".
  1418. dispatchEvent( el, 'test' );
  1419. sinon.assert.callCount( spy1, 0 );
  1420. sinon.assert.callCount( spy2, 0 );
  1421. sinon.assert.callCount( spy3, 0 );
  1422. // Test "test@.y".
  1423. dispatchEvent( el.firstChild, 'test' );
  1424. expect( spy1.firstCall.calledWithExactly(
  1425. sinon.match.has( 'name', 'a' ),
  1426. sinon.match.has( 'target', el.firstChild )
  1427. ) ).to.be.true;
  1428. sinon.assert.callCount( spy2, 0 );
  1429. sinon.assert.callCount( spy3, 0 );
  1430. // Test "test@div".
  1431. dispatchEvent( el.lastChild, 'test' );
  1432. sinon.assert.callCount( spy1, 1 );
  1433. expect( spy2.firstCall.calledWithExactly(
  1434. sinon.match.has( 'name', 'b' ),
  1435. sinon.match.has( 'target', el.lastChild )
  1436. ) ).to.be.true;
  1437. sinon.assert.callCount( spy3, 0 );
  1438. // Test "test@.y".
  1439. dispatchEvent( el.lastChild.firstChild, 'test' );
  1440. expect( spy1.secondCall.calledWithExactly(
  1441. sinon.match.has( 'name', 'a' ),
  1442. sinon.match.has( 'target', el.lastChild.firstChild )
  1443. ) ).to.be.true;
  1444. sinon.assert.callCount( spy2, 1 );
  1445. sinon.assert.callCount( spy3, 0 );
  1446. } );
  1447. it( 'accepts function callbacks', () => {
  1448. const spy1 = testUtils.sinon.spy();
  1449. const spy2 = testUtils.sinon.spy();
  1450. setElement( {
  1451. tag: 'p',
  1452. children: [
  1453. {
  1454. tag: 'span'
  1455. }
  1456. ],
  1457. on: {
  1458. x: bind.to( spy1 ),
  1459. 'y@span': [
  1460. bind.to( spy2 ),
  1461. bind.to( 'c' )
  1462. ]
  1463. }
  1464. } );
  1465. dispatchEvent( el, 'x' );
  1466. dispatchEvent( el.firstChild, 'y' );
  1467. sinon.assert.calledWithExactly( spy1,
  1468. sinon.match.has( 'target', el )
  1469. );
  1470. sinon.assert.calledWithExactly( spy2,
  1471. sinon.match.has( 'target', el.firstChild )
  1472. );
  1473. } );
  1474. it( 'supports event delegation', () => {
  1475. const spy = testUtils.sinon.spy();
  1476. setElement( {
  1477. tag: 'p',
  1478. children: [
  1479. {
  1480. tag: 'span'
  1481. }
  1482. ],
  1483. on: {
  1484. x: bind.to( 'a' ),
  1485. }
  1486. } );
  1487. observable.on( 'a', spy );
  1488. dispatchEvent( el.firstChild, 'x' );
  1489. sinon.assert.calledWithExactly( spy,
  1490. sinon.match.has( 'name', 'a' ),
  1491. sinon.match.has( 'target', el.firstChild )
  1492. );
  1493. } );
  1494. it( 'works for future elements', () => {
  1495. const spy = testUtils.sinon.spy();
  1496. setElement( {
  1497. tag: 'p',
  1498. on: {
  1499. 'test@div': bind.to( 'a' )
  1500. }
  1501. } );
  1502. observable.on( 'a', spy );
  1503. const div = getElement( { tag: 'div' } );
  1504. el.appendChild( div );
  1505. dispatchEvent( div, 'test' );
  1506. sinon.assert.calledWithExactly( spy, sinon.match.has( 'name', 'a' ), sinon.match.has( 'target', div ) );
  1507. } );
  1508. } );
  1509. describe( 'model', () => {
  1510. let observable, emitter, bind;
  1511. beforeEach( () => {
  1512. observable = new Model( {
  1513. foo: 'bar',
  1514. baz: 'qux'
  1515. } );
  1516. emitter = Object.create( EmitterMixin );
  1517. bind = Template.bind( observable, emitter );
  1518. } );
  1519. describe( 'to', () => {
  1520. it( 'returns an instance of TemplateToBinding', () => {
  1521. const spy = testUtils.sinon.spy();
  1522. const binding = bind.to( 'foo', spy );
  1523. expect( binding ).to.be.instanceof( TemplateToBinding );
  1524. expect( spy.called ).to.be.false;
  1525. expect( binding ).to.have.keys( [ 'observable', 'eventNameOrFunction', 'emitter', 'attribute', 'callback' ] );
  1526. expect( binding.observable ).to.equal( observable );
  1527. expect( binding.callback ).to.equal( spy );
  1528. expect( binding.attribute ).to.equal( 'foo' );
  1529. } );
  1530. it( 'allows binding attribute to the observable – simple (HTMLElement attribute)', () => {
  1531. setElement( {
  1532. tag: 'p',
  1533. attributes: {
  1534. 'class': bind.to( 'foo' )
  1535. },
  1536. children: [ 'abc' ]
  1537. } );
  1538. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar">abc</p>' );
  1539. observable.foo = 'baz';
  1540. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="baz">abc</p>' );
  1541. expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.be.null;
  1542. } );
  1543. it( 'allows binding attribute to the observable – simple (Text Node)', () => {
  1544. setElement( {
  1545. tag: 'p',
  1546. children: [
  1547. {
  1548. text: bind.to( 'foo' )
  1549. }
  1550. ]
  1551. } );
  1552. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>bar</p>' );
  1553. observable.foo = 'baz';
  1554. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>baz</p>' );
  1555. } );
  1556. it( 'allows binding attribute to the observable – value processing', () => {
  1557. const callback = value => value > 0 ? 'positive' : 'negative';
  1558. setElement( {
  1559. tag: 'p',
  1560. attributes: {
  1561. 'class': bind.to( 'foo', callback )
  1562. },
  1563. children: [
  1564. {
  1565. text: bind.to( 'foo', callback )
  1566. }
  1567. ]
  1568. } );
  1569. observable.foo = 3;
  1570. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="positive">positive</p>' );
  1571. observable.foo = -7;
  1572. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="negative">negative</p>' );
  1573. } );
  1574. it( 'allows binding attribute to the observable – value processing (use Node)', () => {
  1575. const callback = ( value, node ) => {
  1576. return ( !!node.tagName && value > 0 ) ? 'HTMLElement positive' : '';
  1577. };
  1578. setElement( {
  1579. tag: 'p',
  1580. attributes: {
  1581. 'class': bind.to( 'foo', callback )
  1582. },
  1583. children: [
  1584. {
  1585. text: bind.to( 'foo', callback )
  1586. }
  1587. ]
  1588. } );
  1589. observable.foo = 3;
  1590. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="HTMLElement positive"></p>' );
  1591. observable.foo = -7;
  1592. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  1593. } );
  1594. it( 'allows binding attribute to the observable – custom callback', () => {
  1595. setElement( {
  1596. tag: 'p',
  1597. attributes: {
  1598. 'class': bind.to( 'foo', ( value, el ) => {
  1599. el.innerHTML = value;
  1600. if ( value == 'changed' ) {
  1601. return value;
  1602. }
  1603. } )
  1604. }
  1605. } );
  1606. observable.foo = 'moo';
  1607. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>moo</p>' );
  1608. observable.foo = 'changed';
  1609. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="changed">changed</p>' );
  1610. } );
  1611. it( 'allows binding attribute to the observable – array of bindings (HTMLElement attribute)', () => {
  1612. setElement( {
  1613. tag: 'p',
  1614. attributes: {
  1615. 'class': [
  1616. 'ck-class',
  1617. bind.to( 'foo' ),
  1618. bind.to( 'baz' ),
  1619. bind.to( 'foo', value => `foo-is-${ value }` ),
  1620. 'ck-end'
  1621. ]
  1622. },
  1623. children: [ 'abc' ]
  1624. } );
  1625. observable.foo = 'a';
  1626. observable.baz = 'b';
  1627. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="a b ck-class ck-end foo-is-a">abc</p>' );
  1628. observable.foo = 'c';
  1629. observable.baz = 'd';
  1630. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="c ck-class ck-end d foo-is-c">abc</p>' );
  1631. } );
  1632. it( 'allows binding attribute to the observable – array of bindings (Text Node)', () => {
  1633. setElement( {
  1634. tag: 'p',
  1635. attributes: {
  1636. },
  1637. children: [
  1638. {
  1639. text: [
  1640. 'ck-class',
  1641. bind.to( 'foo' ),
  1642. bind.to( 'baz' ),
  1643. bind.to( 'foo', value => `foo-is-${ value }` ),
  1644. 'ck-end'
  1645. ]
  1646. }
  1647. ]
  1648. } );
  1649. observable.foo = 'a';
  1650. observable.baz = 'b';
  1651. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>ck-class a b foo-is-a ck-end</p>' );
  1652. observable.foo = 'c';
  1653. observable.baz = 'd';
  1654. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>ck-class c d foo-is-c ck-end</p>' );
  1655. } );
  1656. it( 'allows binding attribute to the observable – falsy values', () => {
  1657. setElement( {
  1658. tag: 'p',
  1659. attributes: {
  1660. simple: bind.to( 'foo' ),
  1661. complex: [ null, bind.to( 'foo' ), undefined, false ],
  1662. zero: [ 0, bind.to( 'foo' ) ],
  1663. emptystring: [ '', bind.to( 'foo' ) ]
  1664. },
  1665. children: [ 'abc' ]
  1666. } );
  1667. observable.foo = 'bar';
  1668. expect( normalizeHtml( el.outerHTML ) )
  1669. .to.equal( '<p complex="bar" emptystring="bar" simple="bar" zero="0 bar">abc</p>' );
  1670. observable.foo = 0;
  1671. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p complex="0" emptystring="0" simple="0" zero="0 0">abc</p>' );
  1672. observable.foo = false;
  1673. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p zero="0">abc</p>' );
  1674. observable.foo = null;
  1675. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p zero="0">abc</p>' );
  1676. observable.foo = undefined;
  1677. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p zero="0">abc</p>' );
  1678. observable.foo = '';
  1679. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p zero="0">abc</p>' );
  1680. } );
  1681. it( 'allows binding attribute to the observable – a custom namespace', () => {
  1682. setElement( {
  1683. tag: 'p',
  1684. attributes: {
  1685. class: {
  1686. ns: 'foo',
  1687. value: bind.to( 'foo' )
  1688. },
  1689. custom: {
  1690. ns: 'foo',
  1691. value: [
  1692. bind.to( 'foo' ),
  1693. bind.to( 'baz' )
  1694. ]
  1695. }
  1696. },
  1697. children: [ 'abc' ]
  1698. } );
  1699. observable.foo = 'bar';
  1700. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar" custom="bar qux">abc</p>' );
  1701. expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.equal( 'foo' );
  1702. observable.foo = 'baz';
  1703. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="baz" custom="baz qux">abc</p>' );
  1704. expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.equal( 'foo' );
  1705. } );
  1706. } );
  1707. describe( 'if', () => {
  1708. it( 'returns an object which describes the binding', () => {
  1709. const spy = testUtils.sinon.spy();
  1710. const binding = bind.if( 'foo', 'whenTrue', spy );
  1711. expect( binding ).to.be.instanceof( TemplateIfBinding );
  1712. expect( spy.called ).to.be.false;
  1713. expect( binding ).to.have.keys( [ 'observable', 'emitter', 'attribute', 'callback', 'valueIfTrue' ] );
  1714. expect( binding.observable ).to.equal( observable );
  1715. expect( binding.callback ).to.equal( spy );
  1716. expect( binding.attribute ).to.equal( 'foo' );
  1717. expect( binding.valueIfTrue ).to.equal( 'whenTrue' );
  1718. } );
  1719. it( 'allows binding attribute to the observable – presence of an attribute (HTMLElement attribute)', () => {
  1720. setElement( {
  1721. tag: 'p',
  1722. attributes: {
  1723. 'class': bind.if( 'foo' )
  1724. },
  1725. children: [ 'abc' ]
  1726. } );
  1727. observable.foo = 'bar';
  1728. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="true">abc</p>' );
  1729. observable.foo = true;
  1730. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="true">abc</p>' );
  1731. observable.foo = 0;
  1732. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="true">abc</p>' );
  1733. observable.foo = false;
  1734. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1735. observable.foo = null;
  1736. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1737. observable.foo = undefined;
  1738. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1739. observable.foo = '';
  1740. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1741. } );
  1742. // TODO: Is this alright? It makes sense but it's pretty useless. Text Node cannot be
  1743. // removed just like an attribute of some HTMLElement.
  1744. it( 'allows binding attribute to the observable – presence of an attribute (Text Node)', () => {
  1745. setElement( {
  1746. tag: 'p',
  1747. children: [
  1748. {
  1749. text: bind.if( 'foo' )
  1750. }
  1751. ]
  1752. } );
  1753. observable.foo = 'abc';
  1754. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>true</p>' );
  1755. observable.foo = true;
  1756. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>true</p>' );
  1757. observable.foo = 0;
  1758. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>true</p>' );
  1759. observable.foo = false;
  1760. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  1761. observable.foo = null;
  1762. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  1763. observable.foo = undefined;
  1764. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  1765. observable.foo = '';
  1766. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  1767. } );
  1768. it( 'allows binding attribute to the observable – value of an attribute (HTMLElement attribute)', () => {
  1769. setElement( {
  1770. tag: 'p',
  1771. attributes: {
  1772. 'class': bind.if( 'foo', 'bar' )
  1773. },
  1774. children: [ 'abc' ]
  1775. } );
  1776. observable.foo = 'bar';
  1777. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar">abc</p>' );
  1778. observable.foo = true;
  1779. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar">abc</p>' );
  1780. observable.foo = 0;
  1781. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar">abc</p>' );
  1782. observable.foo = 64;
  1783. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar">abc</p>' );
  1784. observable.foo = false;
  1785. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1786. observable.foo = null;
  1787. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1788. observable.foo = undefined;
  1789. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1790. } );
  1791. it( 'allows binding attribute to the observable – value of an attribute (Text Node)', () => {
  1792. setElement( {
  1793. tag: 'p',
  1794. children: [
  1795. {
  1796. text: bind.if( 'foo', 'bar' )
  1797. }
  1798. ]
  1799. } );
  1800. observable.foo = 'bar';
  1801. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>bar</p>' );
  1802. observable.foo = false;
  1803. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  1804. observable.foo = 64;
  1805. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>bar</p>' );
  1806. } );
  1807. it( 'allows binding attribute to the observable – array of bindings (HTMLElement attribute)', () => {
  1808. setElement( {
  1809. tag: 'p',
  1810. attributes: {
  1811. 'class': [
  1812. 'ck-class',
  1813. bind.if( 'foo', 'foo-set' ),
  1814. bind.if( 'bar', 'bar-not-set', value => !value ),
  1815. 'ck-end'
  1816. ]
  1817. },
  1818. children: [ 'abc' ]
  1819. } );
  1820. observable.foo = observable.bar = true;
  1821. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="ck-class ck-end foo-set">abc</p>' );
  1822. observable.foo = observable.bar = false;
  1823. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar-not-set ck-class ck-end">abc</p>' );
  1824. } );
  1825. it( 'allows binding attribute to the observable – value of an attribute processed by a callback', () => {
  1826. setElement( {
  1827. tag: 'p',
  1828. attributes: {
  1829. 'class': bind.if( 'foo', 'there–is–no–foo', value => !value )
  1830. },
  1831. children: [ 'abc' ]
  1832. } );
  1833. observable.foo = 'bar';
  1834. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1835. observable.foo = false;
  1836. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="there–is–no–foo">abc</p>' );
  1837. observable.foo = 64;
  1838. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1839. } );
  1840. it( 'allows binding attribute to the observable – value of an attribute processed by a callback (use Node)', () => {
  1841. setElement( {
  1842. tag: 'p',
  1843. attributes: {
  1844. 'class': bind.if( 'foo', 'eqls-tag-name', ( value, el ) => el.tagName === value )
  1845. },
  1846. children: [ 'abc' ]
  1847. } );
  1848. observable.foo = 'bar';
  1849. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1850. observable.foo = 'P';
  1851. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="eqls-tag-name">abc</p>' );
  1852. observable.foo = 64;
  1853. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1854. } );
  1855. it( 'allows binding attribute to the observable – falsy values', () => {
  1856. setElement( {
  1857. tag: 'p',
  1858. attributes: {
  1859. 'class': bind.if( 'foo', 'foo-is-set' )
  1860. },
  1861. children: [ 'abc' ]
  1862. } );
  1863. observable.foo = 'bar';
  1864. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="foo-is-set">abc</p>' );
  1865. observable.foo = true;
  1866. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="foo-is-set">abc</p>' );
  1867. observable.foo = 0;
  1868. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="foo-is-set">abc</p>' );
  1869. observable.foo = false;
  1870. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1871. observable.foo = null;
  1872. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1873. observable.foo = undefined;
  1874. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1875. observable.foo = '';
  1876. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1877. } );
  1878. } );
  1879. it( 'works with Template#apply() – root element', () => {
  1880. new Template( {
  1881. tag: 'div',
  1882. attributes: {
  1883. class: bind.to( 'foo' )
  1884. }
  1885. } ).apply( el );
  1886. expect( el.getAttribute( 'class' ) ).to.equal( 'bar' );
  1887. observable.foo = 'baz';
  1888. expect( el.getAttribute( 'class' ) ).to.equal( 'baz' );
  1889. } );
  1890. it( 'works with Template#apply() – children', () => {
  1891. const el = getElement( { tag: 'div' } );
  1892. const child = getElement( {
  1893. tag: 'span',
  1894. children: [ 'foo' ]
  1895. } );
  1896. el.appendChild( child );
  1897. new Template( {
  1898. tag: 'div',
  1899. children: [
  1900. {
  1901. tag: 'span',
  1902. children: [
  1903. {
  1904. text: bind.to( 'foo' )
  1905. }
  1906. ]
  1907. }
  1908. ]
  1909. } ).apply( el );
  1910. expect( child.textContent ).to.equal( 'bar' );
  1911. observable.foo = 'baz';
  1912. expect( child.textContent ).to.equal( 'baz' );
  1913. } );
  1914. } );
  1915. } );
  1916. describe( 'extend()', () => {
  1917. let observable, emitter, bind;
  1918. beforeEach( () => {
  1919. observable = new Model( {
  1920. foo: 'bar',
  1921. baz: 'qux'
  1922. } );
  1923. emitter = Object.create( DomEmitterMixin );
  1924. bind = Template.bind( observable, emitter );
  1925. } );
  1926. it( 'does not modify passed definition', () => {
  1927. const def = {
  1928. tag: 'p',
  1929. attributes: {
  1930. a: 'foo',
  1931. }
  1932. };
  1933. const ext = {
  1934. attributes: {
  1935. b: 'bar'
  1936. }
  1937. };
  1938. const tpl = new Template( def );
  1939. Template.extend( tpl, ext );
  1940. expect( ext.attributes.b ).to.equal( 'bar' );
  1941. expect( tpl.attributes.a[ 0 ] ).to.equal( 'foo' );
  1942. expect( tpl.attributes.b[ 0 ] ).to.equal( 'bar' );
  1943. } );
  1944. it( 'logs a warning if an element has already been rendered', () => {
  1945. const spy = testUtils.sinon.stub( log, 'warn' );
  1946. const tpl = new Template( {
  1947. tag: 'p'
  1948. } );
  1949. Template.extend( tpl, {
  1950. attributes: {
  1951. class: 'foo'
  1952. }
  1953. } );
  1954. tpl.render();
  1955. Template.extend( tpl, {
  1956. attributes: {
  1957. class: 'bar'
  1958. }
  1959. } );
  1960. sinon.assert.calledOnce( spy );
  1961. sinon.assert.calledWithExactly( spy, sinon.match( /^template-extend-render/ ) );
  1962. } );
  1963. describe( 'attributes', () => {
  1964. it( 'extends existing - simple', () => {
  1965. extensionTest(
  1966. {
  1967. tag: 'p',
  1968. attributes: {
  1969. a: 'b'
  1970. }
  1971. },
  1972. {
  1973. attributes: {
  1974. a: 'c'
  1975. }
  1976. },
  1977. '<p a="b c"></p>'
  1978. );
  1979. } );
  1980. it( 'extends existing - complex #1', () => {
  1981. extensionTest(
  1982. {
  1983. tag: 'p',
  1984. attributes: {
  1985. a: [ 'b', 'c' ]
  1986. }
  1987. },
  1988. {
  1989. attributes: {
  1990. a: 'd'
  1991. }
  1992. },
  1993. '<p a="b c d"></p>'
  1994. );
  1995. } );
  1996. it( 'extends existing - complex #2', () => {
  1997. extensionTest(
  1998. {
  1999. tag: 'p',
  2000. attributes: {
  2001. a: {
  2002. value: 'b'
  2003. }
  2004. }
  2005. },
  2006. {
  2007. attributes: {
  2008. a: [ 'c', 'd' ]
  2009. }
  2010. },
  2011. '<p a="b c d"></p>'
  2012. );
  2013. } );
  2014. it( 'extends existing - complex #3', () => {
  2015. extensionTest(
  2016. {
  2017. tag: 'p',
  2018. attributes: {
  2019. a: [ 'b' ]
  2020. }
  2021. },
  2022. {
  2023. attributes: {
  2024. a: [ 'c', 'd' ]
  2025. }
  2026. },
  2027. '<p a="b c d"></p>'
  2028. );
  2029. } );
  2030. it( 'extends existing - bindings #1', () => {
  2031. const el = extensionTest(
  2032. {
  2033. tag: 'p',
  2034. attributes: {
  2035. a: bind.to( 'foo' )
  2036. }
  2037. },
  2038. {
  2039. attributes: {
  2040. a: [ 'c', 'd' ]
  2041. }
  2042. },
  2043. '<p a="bar c d"></p>'
  2044. );
  2045. observable.foo = 'baz';
  2046. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p a="baz c d"></p>' );
  2047. } );
  2048. it( 'extends existing - bindings #2', () => {
  2049. const el = extensionTest(
  2050. {
  2051. tag: 'p',
  2052. attributes: {
  2053. a: [ 'b', bind.to( 'foo' ) ]
  2054. }
  2055. },
  2056. {
  2057. attributes: {
  2058. a: [ 'c', bind.to( 'baz' ) ]
  2059. }
  2060. },
  2061. '<p a="b bar c qux"></p>'
  2062. );
  2063. observable.foo = 'abc';
  2064. observable.baz = 'def';
  2065. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p a="b abc c def"></p>' );
  2066. } );
  2067. it( 'creates new - no attributes', () => {
  2068. extensionTest(
  2069. {
  2070. tag: 'p',
  2071. },
  2072. {
  2073. attributes: {
  2074. c: 'd'
  2075. }
  2076. },
  2077. '<p c="d"></p>'
  2078. );
  2079. } );
  2080. it( 'creates new - simple', () => {
  2081. extensionTest(
  2082. {
  2083. tag: 'p',
  2084. attributes: {
  2085. a: 'b'
  2086. }
  2087. },
  2088. {
  2089. attributes: {
  2090. c: 'd'
  2091. }
  2092. },
  2093. '<p a="b" c="d"></p>'
  2094. );
  2095. } );
  2096. it( 'creates new - array', () => {
  2097. extensionTest(
  2098. {
  2099. tag: 'p',
  2100. attributes: {
  2101. a: 'b'
  2102. }
  2103. },
  2104. {
  2105. attributes: {
  2106. c: [ 'd', 'e' ]
  2107. }
  2108. },
  2109. '<p a="b" c="d e"></p>'
  2110. );
  2111. } );
  2112. it( 'creates new - bindings #1', () => {
  2113. const el = extensionTest(
  2114. {
  2115. tag: 'p',
  2116. attributes: {
  2117. a: 'b'
  2118. }
  2119. },
  2120. {
  2121. attributes: {
  2122. c: bind.to( 'foo' )
  2123. }
  2124. },
  2125. '<p a="b" c="bar"></p>'
  2126. );
  2127. observable.foo = 'abc';
  2128. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p a="b" c="abc"></p>' );
  2129. } );
  2130. it( 'creates new - bindings #2', () => {
  2131. const el = extensionTest(
  2132. {
  2133. tag: 'p',
  2134. attributes: {
  2135. a: 'b'
  2136. }
  2137. },
  2138. {
  2139. attributes: {
  2140. c: [ 'd', bind.to( 'foo' ) ]
  2141. }
  2142. },
  2143. '<p a="b" c="d bar"></p>'
  2144. );
  2145. observable.foo = 'abc';
  2146. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p a="b" c="d abc"></p>' );
  2147. } );
  2148. } );
  2149. describe( 'text', () => {
  2150. it( 'extends existing - simple', () => {
  2151. const el = extensionTest(
  2152. {
  2153. tag: 'p',
  2154. children: [
  2155. 'foo'
  2156. ]
  2157. },
  2158. {
  2159. children: [
  2160. 'bar'
  2161. ]
  2162. },
  2163. '<p>foobar</p>'
  2164. );
  2165. expect( el.childNodes ).to.have.length( 1 );
  2166. } );
  2167. it( 'extends existing - complex #1', () => {
  2168. const el = extensionTest(
  2169. {
  2170. tag: 'p',
  2171. children: [
  2172. { text: 'foo' }
  2173. ]
  2174. },
  2175. {
  2176. children: [
  2177. 'bar'
  2178. ]
  2179. },
  2180. '<p>foobar</p>'
  2181. );
  2182. expect( el.childNodes ).to.have.length( 1 );
  2183. } );
  2184. it( 'extends existing - complex #2', () => {
  2185. const el = extensionTest(
  2186. {
  2187. tag: 'p',
  2188. children: [
  2189. { text: 'foo' }
  2190. ]
  2191. },
  2192. {
  2193. children: [
  2194. { text: 'bar' }
  2195. ]
  2196. },
  2197. '<p>foobar</p>'
  2198. );
  2199. expect( el.childNodes ).to.have.length( 1 );
  2200. } );
  2201. it( 'extends existing - bindings #1', () => {
  2202. const el = extensionTest(
  2203. {
  2204. tag: 'p',
  2205. children: [
  2206. { text: bind.to( 'foo' ) }
  2207. ]
  2208. },
  2209. {
  2210. children: [
  2211. 'abc'
  2212. ]
  2213. },
  2214. '<p>bar abc</p>'
  2215. );
  2216. observable.foo = 'asd';
  2217. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>asd abc</p>' );
  2218. expect( el.childNodes ).to.have.length( 1 );
  2219. } );
  2220. it( 'extends existing - bindings #2', () => {
  2221. const el = extensionTest(
  2222. {
  2223. tag: 'p',
  2224. children: [
  2225. 'abc'
  2226. ]
  2227. },
  2228. {
  2229. children: [
  2230. { text: bind.to( 'foo' ) }
  2231. ]
  2232. },
  2233. '<p>abc bar</p>'
  2234. );
  2235. observable.foo = 'asd';
  2236. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc asd</p>' );
  2237. expect( el.childNodes ).to.have.length( 1 );
  2238. } );
  2239. it( 'extends existing - bindings #3', () => {
  2240. const el = extensionTest(
  2241. {
  2242. tag: 'p',
  2243. children: [
  2244. { text: bind.to( 'foo' ) },
  2245. 'X'
  2246. ]
  2247. },
  2248. {
  2249. children: [
  2250. { text: bind.to( 'baz' ) },
  2251. 'Y'
  2252. ]
  2253. },
  2254. '<p>bar quxXY</p>'
  2255. );
  2256. observable.foo = 'A';
  2257. observable.baz = 'B';
  2258. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>A BXY</p>' );
  2259. expect( el.childNodes ).to.have.length( 2 );
  2260. } );
  2261. } );
  2262. describe( 'children', () => {
  2263. it( 'should throw when the number of children does not correspond', () => {
  2264. expectToThrowCKEditorError( () => {
  2265. extensionTest(
  2266. {
  2267. tag: 'p',
  2268. children: [
  2269. 'foo'
  2270. ]
  2271. },
  2272. {
  2273. children: [
  2274. 'foo',
  2275. 'bar'
  2276. ]
  2277. },
  2278. 'it should fail'
  2279. );
  2280. }, /ui-template-extend-children-mismatch/ );
  2281. } );
  2282. it( 'should throw when no children in target but extending one', () => {
  2283. expectToThrowCKEditorError( () => {
  2284. extensionTest(
  2285. {
  2286. tag: 'p',
  2287. },
  2288. {
  2289. children: [
  2290. {
  2291. tag: 'b'
  2292. }
  2293. ]
  2294. },
  2295. 'it should fail'
  2296. );
  2297. }, /ui-template-extend-children-mismatch/ );
  2298. } );
  2299. it( 'should throw when the number of children does not correspond on some deeper level', () => {
  2300. expectToThrowCKEditorError( () => {
  2301. extensionTest(
  2302. {
  2303. tag: 'p',
  2304. children: [
  2305. {
  2306. tag: 'span',
  2307. attributes: {
  2308. class: 'A'
  2309. },
  2310. children: [
  2311. 'A',
  2312. {
  2313. tag: 'span',
  2314. attributes: {
  2315. class: 'AA'
  2316. },
  2317. children: [
  2318. 'AA'
  2319. ]
  2320. }
  2321. ]
  2322. }
  2323. ]
  2324. },
  2325. {
  2326. children: [
  2327. {
  2328. attributes: {
  2329. class: 'B'
  2330. },
  2331. children: [
  2332. 'B'
  2333. ]
  2334. }
  2335. ]
  2336. },
  2337. 'it should fail'
  2338. );
  2339. }, /ui-template-extend-children-mismatch/ );
  2340. } );
  2341. it( 'extends existing - simple', () => {
  2342. extensionTest(
  2343. {
  2344. tag: 'p',
  2345. children: [
  2346. {
  2347. tag: 'span',
  2348. attributes: {
  2349. class: 'foo'
  2350. }
  2351. }
  2352. ]
  2353. },
  2354. {
  2355. children: [
  2356. {
  2357. tag: 'span',
  2358. attributes: {
  2359. class: 'bar'
  2360. }
  2361. }
  2362. ]
  2363. },
  2364. '<p><span class="bar foo"></span></p>'
  2365. );
  2366. } );
  2367. it( 'extends existing - complex', () => {
  2368. extensionTest(
  2369. {
  2370. tag: 'p',
  2371. children: [
  2372. {
  2373. tag: 'span',
  2374. attributes: {
  2375. class: 'A'
  2376. },
  2377. children: [
  2378. 'A',
  2379. {
  2380. tag: 'span',
  2381. attributes: {
  2382. class: 'AA'
  2383. },
  2384. children: [
  2385. 'AA'
  2386. ]
  2387. }
  2388. ]
  2389. }
  2390. ]
  2391. },
  2392. {
  2393. children: [
  2394. {
  2395. tag: 'span',
  2396. attributes: {
  2397. class: 'B'
  2398. },
  2399. children: [
  2400. 'B',
  2401. {
  2402. tag: 'span',
  2403. attributes: {
  2404. class: 'BB'
  2405. },
  2406. children: [
  2407. 'BB'
  2408. ]
  2409. }
  2410. ]
  2411. }
  2412. ]
  2413. },
  2414. '<p><span class="A B">AB<span class="AA BB">AABB</span></span></p>'
  2415. );
  2416. } );
  2417. it( 'allows extending a particular child', () => {
  2418. const template = new Template( {
  2419. tag: 'p',
  2420. children: [
  2421. {
  2422. tag: 'span',
  2423. attributes: {
  2424. class: 'foo'
  2425. }
  2426. }
  2427. ]
  2428. } );
  2429. Template.extend( template.children[ 0 ], {
  2430. attributes: {
  2431. class: 'bar'
  2432. }
  2433. } );
  2434. expect( template.render().outerHTML ).to.equal( '<p><span class="foo bar"></span></p>' );
  2435. } );
  2436. it( 'allows extending a particular child – recursively', () => {
  2437. const template = new Template( {
  2438. tag: 'p',
  2439. children: [
  2440. {
  2441. tag: 'span',
  2442. attributes: {
  2443. class: 'A'
  2444. },
  2445. children: [
  2446. 'A',
  2447. {
  2448. tag: 'span',
  2449. attributes: {
  2450. class: 'AA'
  2451. },
  2452. children: [
  2453. 'AA'
  2454. ]
  2455. }
  2456. ]
  2457. }
  2458. ]
  2459. } );
  2460. Template.extend( template.children[ 0 ], {
  2461. attributes: {
  2462. class: 'B',
  2463. },
  2464. children: [
  2465. 'B',
  2466. {
  2467. attributes: {
  2468. class: 'BB'
  2469. }
  2470. }
  2471. ]
  2472. } );
  2473. expect( template.render().outerHTML ).to.equal( '<p><span class="A B">AB<span class="AA BB">AA</span></span></p>' );
  2474. } );
  2475. } );
  2476. describe( 'listeners', () => {
  2477. it( 'extends existing', () => {
  2478. const spy1 = testUtils.sinon.spy();
  2479. const spy2 = testUtils.sinon.spy();
  2480. const spy3 = testUtils.sinon.spy();
  2481. const spy4 = testUtils.sinon.spy();
  2482. const spy5 = testUtils.sinon.spy();
  2483. observable.on( 'A', spy1 );
  2484. observable.on( 'C', spy2 );
  2485. observable.on( 'B', spy3 );
  2486. observable.on( 'D', spy4 );
  2487. const el = extensionTest(
  2488. {
  2489. tag: 'p',
  2490. children: [
  2491. {
  2492. tag: 'span'
  2493. }
  2494. ],
  2495. on: {
  2496. click: bind.to( 'A' ),
  2497. 'click@span': [
  2498. bind.to( 'B' ),
  2499. bind.to( spy5 )
  2500. ]
  2501. }
  2502. },
  2503. {
  2504. on: {
  2505. click: bind.to( 'C' ),
  2506. 'click@span': bind.to( 'D' )
  2507. }
  2508. },
  2509. '<p><span></span></p>'
  2510. );
  2511. dispatchEvent( el, 'click' );
  2512. expect( spy1.calledOnce ).to.be.true;
  2513. expect( spy2.calledOnce ).to.be.true;
  2514. expect( spy3.called ).to.be.false;
  2515. expect( spy4.called ).to.be.false;
  2516. expect( spy5.called ).to.be.false;
  2517. dispatchEvent( el.firstChild, 'click' );
  2518. expect( spy1.calledTwice ).to.be.true;
  2519. expect( spy2.calledTwice ).to.be.true;
  2520. expect( spy3.calledOnce ).to.be.true;
  2521. expect( spy4.calledOnce ).to.be.true;
  2522. expect( spy5.calledOnce ).to.be.true;
  2523. } );
  2524. it( 'creates new', () => {
  2525. const spy1 = testUtils.sinon.spy();
  2526. const spy2 = testUtils.sinon.spy();
  2527. const spy3 = testUtils.sinon.spy();
  2528. observable.on( 'A', spy1 );
  2529. observable.on( 'B', spy2 );
  2530. const el = extensionTest(
  2531. {
  2532. tag: 'p',
  2533. children: [
  2534. {
  2535. tag: 'span'
  2536. }
  2537. ],
  2538. },
  2539. {
  2540. on: {
  2541. click: bind.to( 'A' ),
  2542. 'click@span': [
  2543. bind.to( 'B' ),
  2544. bind.to( spy3 )
  2545. ]
  2546. }
  2547. },
  2548. '<p><span></span></p>'
  2549. );
  2550. dispatchEvent( el, 'click' );
  2551. expect( spy1.calledOnce ).to.be.true;
  2552. expect( spy2.called ).to.be.false;
  2553. expect( spy3.called ).to.be.false;
  2554. dispatchEvent( el.firstChild, 'click' );
  2555. expect( spy1.calledTwice ).to.be.true;
  2556. expect( spy2.calledOnce ).to.be.true;
  2557. expect( spy3.calledOnce ).to.be.true;
  2558. } );
  2559. } );
  2560. } );
  2561. } );
  2562. function getElement( template ) {
  2563. return new Template( template ).render();
  2564. }
  2565. function setElement( template ) {
  2566. el = new Template( template ).render();
  2567. document.body.appendChild( el );
  2568. injectedElements.push( el );
  2569. }
  2570. function extensionTest( baseDefinition, extendedDefinition, expectedHtml ) {
  2571. const template = new Template( baseDefinition );
  2572. Template.extend( template, extendedDefinition );
  2573. const el = template.render();
  2574. document.body.appendChild( el );
  2575. expect( normalizeHtml( el.outerHTML ) ).to.equal( expectedHtml );
  2576. injectedElements.push( el );
  2577. return el;
  2578. }
  2579. function dispatchEvent( el, domEvtName ) {
  2580. if ( !el.parentNode ) {
  2581. throw new Error( 'To dispatch an event, element must be in DOM. Otherwise #target is null.' );
  2582. }
  2583. el.dispatchEvent( new Event( domEvtName, {
  2584. bubbles: true
  2585. } ) );
  2586. }
  2587. function getView( def ) {
  2588. const view = new View();
  2589. view.setTemplate( def );
  2590. return view;
  2591. }