template.js 68 KB

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