template.js 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  12. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  13. import DomEmitterMixin from '@ckeditor/ckeditor5-utils/src/dom/emittermixin';
  14. import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
  15. import log from '@ckeditor/ckeditor5-utils/src/log';
  16. testUtils.createSinonSandbox();
  17. let el, text;
  18. const injectedElements = [];
  19. describe( 'Template', () => {
  20. // Clean-up document.body from the rendered elements.
  21. afterEach( () => {
  22. for ( const el of injectedElements ) {
  23. el.remove();
  24. }
  25. } );
  26. describe( 'constructor()', () => {
  27. it( 'sets #_isRendered property', () => {
  28. expect( new Template( { tag: 'p' } )._isRendered ).to.be.false;
  29. } );
  30. it( 'accepts and normalizes the definition', () => {
  31. const bind = Template.bind( new Model( {} ), Object.create( DomEmitterMixin ) );
  32. const childNode = document.createElement( 'div' );
  33. const childTemplate = new Template( {
  34. tag: 'b'
  35. } );
  36. const tpl = new Template( {
  37. tag: 'p',
  38. attributes: {
  39. a: 'foo',
  40. b: [ 'bar', 'baz' ],
  41. c: {
  42. ns: 'abc',
  43. value: bind.to( 'qux' )
  44. }
  45. },
  46. children: [
  47. {
  48. text: 'content'
  49. },
  50. {
  51. text: bind.to( 'x' )
  52. },
  53. 'abc',
  54. {
  55. text: [ 'a', 'b' ]
  56. },
  57. childNode,
  58. childTemplate
  59. ],
  60. on: {
  61. 'a@span': bind.to( 'b' ),
  62. 'b@span': bind.to( () => {} ),
  63. 'c@span': [
  64. bind.to( 'c' ),
  65. bind.to( () => {} )
  66. ]
  67. }
  68. } );
  69. expect( tpl.attributes.a[ 0 ] ).to.equal( 'foo' );
  70. expect( tpl.attributes.b[ 0 ] ).to.equal( 'bar' );
  71. expect( tpl.attributes.b[ 1 ] ).to.equal( 'baz' );
  72. expect( tpl.attributes.c[ 0 ].value[ 0 ] ).to.be.instanceof( TemplateToBinding );
  73. expect( tpl.children ).to.have.length( 6 );
  74. expect( tpl.children[ 0 ].text[ 0 ] ).to.equal( 'content' );
  75. expect( tpl.children[ 1 ].text[ 0 ] ).to.be.instanceof( TemplateToBinding );
  76. expect( tpl.children[ 2 ].text[ 0 ] ).to.equal( 'abc' );
  77. expect( tpl.children[ 3 ].text[ 0 ] ).to.equal( 'a' );
  78. expect( tpl.children[ 3 ].text[ 1 ] ).to.equal( 'b' );
  79. expect( tpl.children[ 4 ] ).to.equal( childNode );
  80. expect( tpl.children[ 5 ] ).to.equal( childTemplate );
  81. expect( tpl.eventListeners[ 'a@span' ][ 0 ] ).to.be.instanceof( TemplateToBinding );
  82. expect( tpl.eventListeners[ 'b@span' ][ 0 ] ).to.be.instanceof( TemplateToBinding );
  83. expect( tpl.eventListeners[ 'c@span' ][ 0 ] ).to.be.instanceof( TemplateToBinding );
  84. expect( tpl.eventListeners[ 'c@span' ][ 1 ] ).to.be.instanceof( TemplateToBinding );
  85. // Note that Template mixes EmitterMixin.
  86. expect( tpl.on ).to.be.a( 'function' );
  87. expect( tpl.on[ 'a@span' ] ).to.be.undefined;
  88. } );
  89. it( 'defines #children collection', () => {
  90. const elementTpl = new Template( {
  91. tag: 'p',
  92. } );
  93. const textTpl = new Template( {
  94. text: 'foo'
  95. } );
  96. expect( elementTpl.children ).to.be.an( 'array' );
  97. expect( elementTpl.children ).to.have.length( 0 );
  98. // Text will never have children.
  99. expect( textTpl.children ).to.be.undefined;
  100. } );
  101. it( 'does not modify passed definition', () => {
  102. const def = {
  103. tag: 'p',
  104. attributes: {
  105. a: 'foo',
  106. },
  107. children: [
  108. {
  109. tag: 'span'
  110. }
  111. ]
  112. };
  113. const tpl = new Template( def );
  114. expect( def.attributes ).to.not.equal( tpl.attributes );
  115. expect( def.children ).to.not.equal( tpl.children );
  116. expect( def.children[ 0 ] ).to.not.equal( tpl.children[ 0 ] );
  117. expect( def.attributes.a ).to.equal( 'foo' );
  118. expect( def.children[ 0 ].tag ).to.equal( 'span' );
  119. expect( tpl.attributes.a[ 0 ] ).to.equal( 'foo' );
  120. expect( tpl.children[ 0 ].tag ).to.equal( 'span' );
  121. } );
  122. } );
  123. describe( 'render()', () => {
  124. it( 'throws when the template definition is wrong', () => {
  125. expect( () => {
  126. new Template( {} ).render();
  127. } ).to.throw( CKEditorError, /ui-template-wrong-syntax/ );
  128. expect( () => {
  129. new Template( {
  130. tag: 'p',
  131. text: 'foo'
  132. } ).render();
  133. } ).to.throw( CKEditorError, /ui-template-wrong-syntax/ );
  134. } );
  135. it( 'sets #_isRendered true', () => {
  136. const tpl = new Template( { tag: 'p' } );
  137. expect( tpl._isRendered ).to.be.false;
  138. tpl.render();
  139. expect( tpl._isRendered ).to.be.true;
  140. } );
  141. describe( 'DOM Node', () => {
  142. it( 'creates HTMLElement', () => {
  143. const el = new Template( {
  144. tag: 'p',
  145. } ).render();
  146. expect( el ).to.be.instanceof( HTMLElement );
  147. expect( el.parentNode ).to.be.null;
  148. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  149. expect( el.namespaceURI ).to.equal( 'http://www.w3.org/1999/xhtml' );
  150. } );
  151. it( 'creates an element in a custom namespace', () => {
  152. const el = new Template( {
  153. tag: 'p',
  154. ns: 'foo'
  155. } ).render();
  156. expect( el.namespaceURI ).to.equal( 'foo' );
  157. } );
  158. it( 'creates a Text node', () => {
  159. const node = new Template( { text: 'foo' } ).render();
  160. expect( node.nodeType ).to.equal( 3 );
  161. expect( node.textContent ).to.equal( 'foo' );
  162. } );
  163. } );
  164. describe( 'attributes', () => {
  165. it( 'renders HTMLElement attributes', () => {
  166. const el = new Template( {
  167. tag: 'p',
  168. attributes: {
  169. 'class': [ 'a', 'b' ],
  170. x: 'bar'
  171. },
  172. children: [ 'foo' ]
  173. } ).render();
  174. expect( el ).to.be.instanceof( HTMLElement );
  175. expect( el.parentNode ).to.be.null;
  176. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="a b" x="bar">foo</p>' );
  177. expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.be.null;
  178. } );
  179. it( 'renders HTMLElement attributes – empty', () => {
  180. const el = new Template( {
  181. tag: 'p',
  182. attributes: {
  183. class: '',
  184. x: [ '', '' ]
  185. },
  186. children: [ 'foo' ]
  187. } ).render();
  188. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>foo</p>' );
  189. } );
  190. it( 'renders HTMLElement attributes – falsy values', () => {
  191. const el = new Template( {
  192. tag: 'p',
  193. attributes: {
  194. class: false,
  195. x: [ '', null, undefined, false ],
  196. y: [ 'foo', null, undefined, false ]
  197. },
  198. children: [ 'foo' ]
  199. } ).render();
  200. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p y="foo">foo</p>' );
  201. } );
  202. it( 'renders HTMLElement attributes in a custom namespace', () => {
  203. const el = new Template( {
  204. tag: 'p',
  205. attributes: {
  206. class: {
  207. ns: 'foo',
  208. value: [ 'a', 'b' ]
  209. },
  210. x: {
  211. ns: 'abc',
  212. value: 'bar'
  213. }
  214. },
  215. children: [ 'foo' ]
  216. } ).render();
  217. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="a b" x="bar">foo</p>' );
  218. expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.equal( 'foo' );
  219. expect( el.attributes.getNamedItem( 'x' ).namespaceURI ).to.equal( 'abc' );
  220. } );
  221. describe( 'style', () => {
  222. let observable, emitter, bind;
  223. beforeEach( () => {
  224. observable = new Model( {
  225. width: '10px',
  226. backgroundColor: 'yellow'
  227. } );
  228. emitter = Object.create( EmitterMixin );
  229. bind = Template.bind( observable, emitter );
  230. } );
  231. it( 'renders as a static value', () => {
  232. setElement( {
  233. tag: 'p',
  234. attributes: {
  235. style: 'color: red'
  236. }
  237. } );
  238. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="color:red;"></p>' );
  239. } );
  240. it( 'renders as a static value (Array of values)', () => {
  241. setElement( {
  242. tag: 'p',
  243. attributes: {
  244. style: [ 'color: red;', 'display: block;' ]
  245. }
  246. } );
  247. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="color:red;display:block;"></p>' );
  248. } );
  249. it( 'renders as a value bound to the model', () => {
  250. setElement( {
  251. tag: 'p',
  252. attributes: {
  253. style: bind.to( 'width', w => `width: ${ w }` )
  254. }
  255. } );
  256. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="width:10px;"></p>' );
  257. observable.width = '1em';
  258. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="width:1em;"></p>' );
  259. } );
  260. it( 'renders as a value bound to the model (Array of bindings)', () => {
  261. setElement( {
  262. tag: 'p',
  263. attributes: {
  264. style: [
  265. bind.to( 'width', w => `width: ${ w };` ),
  266. bind.to( 'backgroundColor', c => `background-color: ${ c };` )
  267. ]
  268. }
  269. } );
  270. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="width:10px;background-color:yellow;"></p>' );
  271. observable.width = '1em';
  272. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="width:1em;background-color:yellow;"></p>' );
  273. } );
  274. describe( 'object', () => {
  275. it( 'renders with static and bound attributes', () => {
  276. setElement( {
  277. tag: 'p',
  278. attributes: {
  279. style: {
  280. width: bind.to( 'width' ),
  281. height: '10px',
  282. backgroundColor: bind.to( 'backgroundColor' )
  283. }
  284. }
  285. } );
  286. expect( normalizeHtml( el.outerHTML ) )
  287. .to.equal( '<p style="width:10px;height:10px;background-color:yellow;"></p>' );
  288. observable.width = '20px';
  289. observable.backgroundColor = 'green';
  290. expect( normalizeHtml( el.outerHTML ) )
  291. .to.equal( '<p style="width:20px;height:10px;background-color:green;"></p>' );
  292. } );
  293. it( 'renders with empty string attributes', () => {
  294. setElement( {
  295. tag: 'p',
  296. attributes: {
  297. style: {
  298. width: '',
  299. backgroundColor: bind.to( 'backgroundColor' )
  300. }
  301. }
  302. } );
  303. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="background-color:yellow;"></p>' );
  304. observable.backgroundColor = '';
  305. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  306. } );
  307. it( 'renders with falsy values', () => {
  308. setElement( {
  309. tag: 'p',
  310. attributes: {
  311. style: {
  312. width: null,
  313. height: false,
  314. color: undefined
  315. }
  316. }
  317. } );
  318. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  319. } );
  320. } );
  321. } );
  322. } );
  323. describe( 'children', () => {
  324. it( 'creates HTMLElement children', () => {
  325. const el = new Template( {
  326. tag: 'p',
  327. attributes: {
  328. a: 'A'
  329. },
  330. children: [
  331. {
  332. tag: 'b',
  333. children: [ 'B' ]
  334. },
  335. {
  336. tag: 'i',
  337. children: [
  338. 'C',
  339. {
  340. tag: 'b',
  341. children: [ 'D' ]
  342. }
  343. ]
  344. }
  345. ]
  346. } ).render();
  347. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p a="A"><b>B</b><i>C<b>D</b></i></p>' );
  348. } );
  349. it( 'creates a child Text Node (different syntaxes)', () => {
  350. const el = new Template( {
  351. tag: 'p',
  352. children: [
  353. 'foo',
  354. { text: 'bar' }
  355. ]
  356. } ).render();
  357. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>foobar</p>' );
  358. } );
  359. it( 'creates multiple child Text Nodes', () => {
  360. const el = new Template( {
  361. tag: 'p',
  362. children: [
  363. 'a',
  364. 'b',
  365. { text: 'c' },
  366. 'd',
  367. { text: [ 'e', 'f' ] }
  368. ]
  369. } ).render();
  370. expect( el.childNodes ).to.have.length( 5 );
  371. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abcdef</p>' );
  372. } );
  373. it( 'renders view children', () => {
  374. const v1 = getView( {
  375. tag: 'b',
  376. attributes: {
  377. class: [
  378. 'v1'
  379. ]
  380. }
  381. } );
  382. const v2 = getView( {
  383. tag: 'b',
  384. attributes: {
  385. class: [
  386. 'v2'
  387. ]
  388. }
  389. } );
  390. const v3 = getView( {
  391. tag: 'b',
  392. attributes: {
  393. class: [
  394. 'v3'
  395. ]
  396. }
  397. } );
  398. v3.render();
  399. const tpl = new Template( {
  400. tag: 'p',
  401. children: [ v1, v2, v3 ]
  402. } );
  403. expect( tpl.children[ 0 ] ).to.equal( v1 );
  404. expect( tpl.children[ 1 ] ).to.equal( v2 );
  405. expect( tpl.children[ 2 ] ).to.equal( v3 );
  406. const rendered = tpl.render();
  407. expect( normalizeHtml( rendered.outerHTML ) )
  408. .to.equal( '<p><b class="v1"></b><b class="v2"></b><b class="v3"></b></p>' );
  409. // Make sure the child views will not re–render their elements but
  410. // use ones rendered by the template instance above.
  411. expect( v1.element ).to.equal( rendered.firstChild );
  412. expect( v2.element ).to.equal( rendered.children[ 1 ] );
  413. expect( v3.element ).to.equal( rendered.lastChild );
  414. } );
  415. it( 'renders view collection', () => {
  416. const collection = new ViewCollection();
  417. const v1 = getView( {
  418. tag: 'span',
  419. attributes: {
  420. class: [
  421. 'v1'
  422. ]
  423. }
  424. } );
  425. const v2 = getView( {
  426. tag: 'span',
  427. attributes: {
  428. class: [
  429. 'v2'
  430. ]
  431. }
  432. } );
  433. collection.add( v1 );
  434. collection.add( v2 );
  435. const rendered = new Template( {
  436. tag: 'p',
  437. children: collection
  438. } ).render();
  439. expect( normalizeHtml( rendered.outerHTML ) ).to.equal( '<p><span class="v1"></span><span class="v2"></span></p>' );
  440. // Make sure the child views will not re–render their elements but
  441. // use ones rendered by the template instance above.
  442. expect( v1.element ).to.equal( rendered.firstChild );
  443. expect( v2.element ).to.equal( rendered.lastChild );
  444. expect( collection._parentElement ).to.equal( rendered );
  445. } );
  446. it( 'renders DOM nodes', () => {
  447. const view = new View();
  448. view.set( {
  449. foo: 'bar',
  450. bar: 'baz'
  451. } );
  452. const bind = Template.bind( view, view );
  453. const childA = new Template( {
  454. tag: 'b',
  455. attributes: {
  456. class: bind.to( 'foo' )
  457. }
  458. } ).render();
  459. const childB = new Template( {
  460. text: bind.to( 'bar' )
  461. } ).render();
  462. const rendered = new Template( {
  463. tag: 'p',
  464. children: [
  465. childA,
  466. childB
  467. ]
  468. } ).render();
  469. expect( normalizeHtml( rendered.outerHTML ) ).to.equal( '<p><b class="bar"></b>baz</p>' );
  470. } );
  471. // #117
  472. it( 'renders template children', () => {
  473. const childTplA = new Template( {
  474. tag: 'a'
  475. } );
  476. const childTplB = new Template( {
  477. tag: 'b'
  478. } );
  479. const view = new View();
  480. view.set( {
  481. foo: 'bar',
  482. bar: 'foo'
  483. } );
  484. const bind = Template.bind( view, view );
  485. // Yes, this TC is crazy in some sort of way. It's all about template
  486. // normalization and deep cloning and the like.
  487. //
  488. // To **really** prove the code is safe there must be a View instance,
  489. // which has some child bound to its attribute and... there must be a
  490. // Template instance (below), which also has a child bound to the same attribute.
  491. //
  492. // I know, the view instance and its template aren't even rendered.
  493. //
  494. // The truth is that madness behind this test case is so deep there are no
  495. // words to explain it. But what actually matters is that it proves the Template
  496. // class is free of "Maximum call stack size exceeded" error in certain
  497. // situations.
  498. view.setTemplate( {
  499. tag: 'span',
  500. children: [
  501. {
  502. text: bind.to( 'bar' )
  503. }
  504. ]
  505. } );
  506. const childTplC = new Template( {
  507. tag: 'i',
  508. children: [
  509. {
  510. text: bind.to( 'bar' )
  511. }
  512. ]
  513. } );
  514. const tpl = new Template( {
  515. tag: 'p',
  516. children: [
  517. childTplA,
  518. childTplB,
  519. childTplC
  520. ]
  521. } );
  522. // Make sure child instances weren't cloned.
  523. expect( tpl.children[ 0 ] ).to.equal( childTplA );
  524. expect( tpl.children[ 1 ] ).to.equal( childTplB );
  525. expect( tpl.children[ 2 ] ).to.equal( childTplC );
  526. expect( normalizeHtml( tpl.render().outerHTML ) ).to.equal(
  527. '<p><a></a><b></b><i>foo</i></p>'
  528. );
  529. } );
  530. // https://github.com/ckeditor/ckeditor5-ui/issues/289
  531. it( 'does not throw when child does not have an "id" property', () => {
  532. const strongView = getView( {
  533. tag: 'strong'
  534. } );
  535. strongView.set( 'id' );
  536. expect( () => {
  537. getView( {
  538. tag: 'div',
  539. children: [
  540. strongView
  541. ]
  542. } );
  543. } ).to.not.throw();
  544. } );
  545. } );
  546. describe( 'bindings', () => {
  547. it( 'activates model bindings – root', () => {
  548. const observable = new Model( {
  549. foo: 'bar'
  550. } );
  551. const emitter = Object.create( EmitterMixin );
  552. const bind = Template.bind( observable, emitter );
  553. const el = new Template( {
  554. tag: 'div',
  555. attributes: {
  556. class: bind.to( 'foo' )
  557. }
  558. } ).render();
  559. expect( el.getAttribute( 'class' ) ).to.equal( 'bar' );
  560. observable.foo = 'baz';
  561. expect( el.getAttribute( 'class' ) ).to.equal( 'baz' );
  562. } );
  563. it( 'activates model bindings – children', () => {
  564. const observable = new Model( {
  565. foo: 'bar'
  566. } );
  567. const emitter = Object.create( EmitterMixin );
  568. const bind = Template.bind( observable, emitter );
  569. const el = new Template( {
  570. tag: 'div',
  571. children: [
  572. {
  573. tag: 'span',
  574. children: [
  575. {
  576. text: [
  577. bind.to( 'foo' ),
  578. 'static'
  579. ]
  580. }
  581. ]
  582. }
  583. ]
  584. } ).render();
  585. expect( el.firstChild.textContent ).to.equal( 'bar static' );
  586. observable.foo = 'baz';
  587. expect( el.firstChild.textContent ).to.equal( 'baz static' );
  588. } );
  589. } );
  590. } );
  591. describe( 'apply()', () => {
  592. let observable, domEmitter, bind;
  593. beforeEach( () => {
  594. setElement( { tag: 'div' } );
  595. text = document.createTextNode( '' );
  596. observable = new Model( {
  597. foo: 'bar',
  598. baz: 'qux'
  599. } );
  600. domEmitter = Object.create( DomEmitterMixin );
  601. bind = Template.bind( observable, domEmitter );
  602. } );
  603. it( 'throws when wrong template definition', () => {
  604. expect( () => {
  605. new Template( {
  606. tag: 'p',
  607. text: 'foo'
  608. } ).apply( el );
  609. } ).to.throw( CKEditorError, /ui-template-wrong-syntax/ );
  610. } );
  611. it( 'accepts empty template definition', () => {
  612. new Template( {} ).apply( el );
  613. new Template( {} ).apply( text );
  614. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div></div>' );
  615. expect( text.textContent ).to.equal( '' );
  616. } );
  617. describe( 'text', () => {
  618. it( 'applies textContent to a Text Node', () => {
  619. new Template( {
  620. text: 'abc'
  621. } ).apply( text );
  622. expect( text.textContent ).to.equal( 'abc' );
  623. } );
  624. it( 'overrides existing textContent of a Text Node', () => {
  625. text.textContent = 'foo';
  626. new Template( {
  627. text: bind.to( 'foo' )
  628. } ).apply( text );
  629. expect( text.textContent ).to.equal( 'bar' );
  630. observable.foo = 'qux';
  631. expect( text.textContent ).to.equal( 'qux' );
  632. } );
  633. it( 'overrides textContent of an existing Text Node in a HTMLElement', () => {
  634. el.textContent = 'bar';
  635. new Template( {
  636. tag: 'div',
  637. children: [ 'foo' ]
  638. } ).apply( el );
  639. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div>foo</div>' );
  640. } );
  641. } );
  642. describe( 'attributes', () => {
  643. it( 'applies attributes to an HTMLElement', () => {
  644. new Template( {
  645. tag: 'div',
  646. attributes: {
  647. 'class': [ 'a', 'b' ],
  648. x: 'bar'
  649. }
  650. } ).apply( el );
  651. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div class="a b" x="bar"></div>' );
  652. } );
  653. it( 'manages existing attribute values ("class" vs. "non–class")', () => {
  654. el.setAttribute( 'class', 'default' );
  655. el.setAttribute( 'x', 'foo' );
  656. new Template( {
  657. tag: 'div',
  658. attributes: {
  659. 'class': [ 'a', 'b' ],
  660. x: 'bar'
  661. }
  662. } ).apply( el );
  663. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  664. '<div class="default a b" x="bar"></div>'
  665. );
  666. } );
  667. it( 'applies attributes and TextContent to a DOM tree', () => {
  668. el.textContent = 'abc';
  669. el.appendChild( getElement( { tag: 'span' } ) );
  670. new Template( {
  671. tag: 'div',
  672. attributes: {
  673. 'class': 'parent'
  674. },
  675. children: [
  676. 'Children:',
  677. {
  678. tag: 'span',
  679. attributes: {
  680. class: 'child'
  681. }
  682. }
  683. ]
  684. } ).apply( el );
  685. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div class="parent">Children:<span class="child"></span></div>' );
  686. } );
  687. describe( 'style', () => {
  688. beforeEach( () => {
  689. observable = new Model( {
  690. width: '10px',
  691. backgroundColor: 'yellow'
  692. } );
  693. bind = Template.bind( observable, domEmitter );
  694. } );
  695. it( 'applies as a static value', () => {
  696. setElement( {
  697. tag: 'p',
  698. attributes: {
  699. style: 'color: red;'
  700. }
  701. } );
  702. new Template( {
  703. attributes: {
  704. style: 'display: block'
  705. }
  706. } ).apply( el );
  707. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="color:red;display:block;"></p>' );
  708. } );
  709. it( 'applies as a static value (Array of values)', () => {
  710. setElement( {
  711. tag: 'p',
  712. attributes: {
  713. style: [ 'color: red;', 'display: block;' ]
  714. }
  715. } );
  716. new Template( {
  717. attributes: {
  718. style: [ 'float: left;', 'overflow: hidden;' ]
  719. }
  720. } ).apply( el );
  721. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  722. '<p style="color:red;display:block;float:left;overflow:hidden;"></p>'
  723. );
  724. } );
  725. it( 'applies when in an object syntax', () => {
  726. setElement( {
  727. tag: 'p',
  728. attributes: {
  729. style: {
  730. width: '20px',
  731. }
  732. }
  733. } );
  734. new Template( {
  735. attributes: {
  736. style: {
  737. height: '10px',
  738. float: 'left',
  739. backgroundColor: 'green'
  740. }
  741. }
  742. } ).apply( el );
  743. expect( normalizeHtml( el.outerHTML ) )
  744. .to.equal( '<p style="width:20px;height:10px;float:left;background-color:green;"></p>' );
  745. } );
  746. it( 'applies when bound to observable', () => {
  747. setElement( {
  748. tag: 'p',
  749. attributes: {
  750. style: {
  751. left: '20px',
  752. }
  753. }
  754. } );
  755. new Template( {
  756. attributes: {
  757. style: {
  758. width: bind.to( 'width' ),
  759. float: 'left',
  760. backgroundColor: 'green'
  761. }
  762. }
  763. } ).apply( el );
  764. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  765. '<p style="left:20px;width:10px;float:left;background-color:green;"></p>'
  766. );
  767. observable.width = '100px';
  768. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  769. '<p style="left:20px;width:100px;float:left;background-color:green;"></p>'
  770. );
  771. } );
  772. } );
  773. } );
  774. describe( 'children', () => {
  775. it( 'doesn\'t apply new child to an HTMLElement – Text Node', () => {
  776. new Template( {
  777. tag: 'div',
  778. children: [ 'foo' ]
  779. } ).apply( el );
  780. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div></div>' );
  781. } );
  782. it( 'doesn\'t apply new child to an HTMLElement – HTMLElement', () => {
  783. new Template( {
  784. tag: 'div',
  785. children: [
  786. {
  787. tag: 'span'
  788. }
  789. ]
  790. } ).apply( el );
  791. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div></div>' );
  792. } );
  793. it( 'doesn\'t apply new child to an HTMLElement – view', () => {
  794. const view = getView( {
  795. tag: 'span',
  796. attributes: {
  797. class: [
  798. 'v1'
  799. ]
  800. }
  801. } );
  802. new Template( {
  803. tag: 'p',
  804. children: [ view ]
  805. } ).apply( el );
  806. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div></div>' );
  807. } );
  808. it( 'doesn\'t apply new child to an HTMLElement – view collection', () => {
  809. const collection = new ViewCollection();
  810. collection.add( getView( {
  811. tag: 'span',
  812. attributes: {
  813. class: [
  814. 'v1'
  815. ]
  816. }
  817. } ) );
  818. new Template( {
  819. tag: 'p',
  820. children: collection
  821. } ).apply( el );
  822. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div></div>' );
  823. expect( collection._parentElement ).to.be.null;
  824. } );
  825. it( 'should work for deep DOM structure with bindings and event listeners', () => {
  826. const childA = getElement( {
  827. tag: 'a',
  828. attributes: {
  829. class: 'a1 a2'
  830. },
  831. children: [
  832. 'a'
  833. ]
  834. } );
  835. const childB = getElement( {
  836. tag: 'b',
  837. attributes: {
  838. class: 'b1 b2'
  839. },
  840. children: [
  841. 'b'
  842. ]
  843. } );
  844. el.appendChild( childA );
  845. el.appendChild( childB );
  846. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  847. '<div><a class="a1 a2">a</a><b class="b1 b2">b</b></div>'
  848. );
  849. const spy1 = testUtils.sinon.spy();
  850. const spy2 = testUtils.sinon.spy();
  851. const spy3 = testUtils.sinon.spy();
  852. observable.on( 'ku', spy1 );
  853. observable.on( 'kd', spy2 );
  854. observable.on( 'mo', spy3 );
  855. new Template( {
  856. tag: 'div',
  857. children: [
  858. {
  859. tag: 'a',
  860. on: {
  861. keyup: bind.to( 'ku' )
  862. },
  863. attributes: {
  864. class: bind.to( 'foo', val => 'applied-A-' + val ),
  865. id: 'applied-A'
  866. },
  867. children: [ 'applied-a' ]
  868. },
  869. {
  870. tag: 'b',
  871. on: {
  872. keydown: bind.to( 'kd' )
  873. },
  874. attributes: {
  875. class: bind.to( 'baz', val => 'applied-B-' + val ),
  876. id: 'applied-B'
  877. },
  878. children: [ 'applied-b' ]
  879. },
  880. 'Text which is not to be applied because it does NOT exist in original element.'
  881. ],
  882. on: {
  883. 'mouseover@a': bind.to( 'mo' )
  884. },
  885. attributes: {
  886. id: bind.to( 'foo', val => val.toUpperCase() ),
  887. class: bind.to( 'baz', val => 'applied-parent-' + val )
  888. }
  889. } ).apply( el );
  890. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div class="applied-parent-qux" id="BAR">' +
  891. '<a class="a1 a2 applied-A-bar" id="applied-A">applied-a</a>' +
  892. '<b class="b1 b2 applied-B-qux" id="applied-B">applied-b</b>' +
  893. '</div>' );
  894. observable.foo = 'updated';
  895. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div class="applied-parent-qux" id="UPDATED">' +
  896. '<a class="a1 a2 applied-A-updated" id="applied-A">applied-a</a>' +
  897. '<b class="b1 b2 applied-B-qux" id="applied-B">applied-b</b>' +
  898. '</div>' );
  899. document.body.appendChild( el );
  900. // Test "mouseover@a".
  901. dispatchEvent( el, 'mouseover' );
  902. dispatchEvent( childA, 'mouseover' );
  903. // Test "keyup".
  904. dispatchEvent( childA, 'keyup' );
  905. // Test "keydown".
  906. dispatchEvent( childB, 'keydown' );
  907. sinon.assert.calledOnce( spy1 );
  908. sinon.assert.calledOnce( spy2 );
  909. sinon.assert.calledOnce( spy3 );
  910. } );
  911. } );
  912. } );
  913. describe( 'revert()', () => {
  914. let observable, domEmitter, bind;
  915. beforeEach( () => {
  916. el = getElement( { tag: 'div' } );
  917. observable = new Model( {
  918. foo: 'bar',
  919. baz: 'qux'
  920. } );
  921. domEmitter = Object.create( DomEmitterMixin );
  922. bind = Template.bind( observable, domEmitter );
  923. } );
  924. it( 'should throw if template is not applied', () => {
  925. const tpl = new Template( {
  926. tag: 'div'
  927. } );
  928. expect( () => {
  929. tpl.revert( el );
  930. } ).to.throw( CKEditorError, /ui-template-revert-not-applied/ );
  931. tpl.render();
  932. expect( () => {
  933. tpl.revert( el );
  934. } ).to.throw( CKEditorError, /ui-template-revert-not-applied/ );
  935. } );
  936. describe( 'text', () => {
  937. it( 'should revert textContent to the initial value', () => {
  938. el = getElement( {
  939. tag: 'a',
  940. children: [
  941. 'a',
  942. {
  943. tag: 'b',
  944. children: [
  945. 'b'
  946. ]
  947. }
  948. ]
  949. } );
  950. const tpl = new Template( {
  951. children: [
  952. 'bar',
  953. {
  954. children: [
  955. 'qux'
  956. ]
  957. }
  958. ]
  959. } );
  960. tpl.apply( el );
  961. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  962. '<a>bar<b>qux</b></a>'
  963. );
  964. tpl.revert( el );
  965. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  966. '<a>a<b>b</b></a>'
  967. );
  968. } );
  969. it( 'should remove bindings', () => {
  970. el = getElement( {
  971. tag: 'a',
  972. children: [
  973. 'a',
  974. {
  975. tag: 'b',
  976. children: [
  977. 'b'
  978. ]
  979. }
  980. ]
  981. } );
  982. const tpl = new Template( {
  983. children: [
  984. 'foo',
  985. {
  986. children: [
  987. {
  988. text: bind.to( 'foo' )
  989. }
  990. ]
  991. }
  992. ]
  993. } );
  994. tpl.apply( el );
  995. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  996. '<a>foo<b>bar</b></a>'
  997. );
  998. observable.foo = 'abc';
  999. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1000. '<a>foo<b>abc</b></a>'
  1001. );
  1002. tpl.revert( el );
  1003. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1004. '<a>a<b>b</b></a>'
  1005. );
  1006. observable.foo = 'xyz';
  1007. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1008. '<a>a<b>b</b></a>'
  1009. );
  1010. } );
  1011. } );
  1012. describe( 'attributes', () => {
  1013. it( 'should revert attributes to the initial values', () => {
  1014. el = getElement( {
  1015. tag: 'a',
  1016. attributes: {
  1017. foo: 'af',
  1018. bar: 'ab',
  1019. },
  1020. children: [
  1021. {
  1022. tag: 'b',
  1023. attributes: {
  1024. foo: 'bf',
  1025. bar: 'bb',
  1026. }
  1027. }
  1028. ]
  1029. } );
  1030. const tpl = new Template( {
  1031. attributes: {
  1032. foo: 'af1',
  1033. bar: [ 'ab1', 'ab2' ],
  1034. baz: 'x'
  1035. },
  1036. children: [
  1037. {
  1038. attributes: {
  1039. foo: 'bf1'
  1040. }
  1041. }
  1042. ]
  1043. } );
  1044. tpl.apply( el );
  1045. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1046. '<a bar="ab1 ab2" baz="x" foo="af1">' +
  1047. '<b bar="bb" foo="bf1"></b>' +
  1048. '</a>'
  1049. );
  1050. tpl.revert( el );
  1051. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1052. '<a bar="ab" foo="af">' +
  1053. '<b bar="bb" foo="bf"></b>' +
  1054. '</a>'
  1055. );
  1056. } );
  1057. it( 'should remove bindings', () => {
  1058. el = getElement( {
  1059. tag: 'a',
  1060. attributes: {
  1061. foo: 'af',
  1062. bar: 'ab',
  1063. },
  1064. children: [
  1065. {
  1066. tag: 'b',
  1067. attributes: {
  1068. foo: 'bf',
  1069. bar: 'bb',
  1070. }
  1071. }
  1072. ]
  1073. } );
  1074. const tpl = new Template( {
  1075. attributes: {
  1076. foo: 'af1',
  1077. bar: [
  1078. 'ab1',
  1079. bind.to( 'baz' )
  1080. ]
  1081. },
  1082. children: [
  1083. {
  1084. attributes: {
  1085. foo: bind.to( 'foo' )
  1086. }
  1087. }
  1088. ]
  1089. } );
  1090. tpl.apply( el );
  1091. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1092. '<a bar="ab1 qux" foo="af1">' +
  1093. '<b bar="bb" foo="bar"></b>' +
  1094. '</a>'
  1095. );
  1096. observable.foo = 'x';
  1097. observable.baz = 'y';
  1098. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1099. '<a bar="ab1 y" foo="af1">' +
  1100. '<b bar="bb" foo="x"></b>' +
  1101. '</a>'
  1102. );
  1103. tpl.revert( el );
  1104. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1105. '<a bar="ab" foo="af">' +
  1106. '<b bar="bb" foo="bf"></b>' +
  1107. '</a>'
  1108. );
  1109. observable.foo = 'abc';
  1110. observable.baz = 'cba';
  1111. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1112. '<a bar="ab" foo="af">' +
  1113. '<b bar="bb" foo="bf"></b>' +
  1114. '</a>'
  1115. );
  1116. } );
  1117. describe( 'style', () => {
  1118. beforeEach( () => {
  1119. observable = new Model( {
  1120. overflow: 'visible'
  1121. } );
  1122. bind = Template.bind( observable, domEmitter );
  1123. } );
  1124. it( 'should remove bindings', () => {
  1125. el = getElement( {
  1126. tag: 'a',
  1127. attributes: {
  1128. style: {
  1129. fontWeight: 'bold'
  1130. }
  1131. },
  1132. children: [
  1133. {
  1134. tag: 'b',
  1135. attributes: {
  1136. style: {
  1137. color: 'red'
  1138. }
  1139. }
  1140. }
  1141. ]
  1142. } );
  1143. const tpl = new Template( {
  1144. attributes: {
  1145. style: {
  1146. overflow: bind.to( 'overflow' )
  1147. }
  1148. },
  1149. children: [
  1150. {
  1151. tag: 'b',
  1152. attributes: {
  1153. style: {
  1154. display: 'block'
  1155. }
  1156. }
  1157. }
  1158. ]
  1159. } );
  1160. tpl.apply( el );
  1161. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1162. '<a style="font-weight:bold;overflow:visible;">' +
  1163. '<b style="color:red;display:block;"></b>' +
  1164. '</a>'
  1165. );
  1166. tpl.revert( el );
  1167. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1168. '<a style="font-weight:bold;">' +
  1169. '<b style="color:red;"></b>' +
  1170. '</a>'
  1171. );
  1172. observable.overflow = 'hidden';
  1173. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1174. '<a style="font-weight:bold;">' +
  1175. '<b style="color:red;"></b>' +
  1176. '</a>'
  1177. );
  1178. } );
  1179. } );
  1180. } );
  1181. describe( 'children', () => {
  1182. it( 'should work for deep DOM structure with bindings and event listeners', () => {
  1183. el = getElement( {
  1184. tag: 'div',
  1185. children: [
  1186. {
  1187. tag: 'a',
  1188. attributes: {
  1189. class: [ 'a1', 'a2' ]
  1190. },
  1191. children: [
  1192. 'a'
  1193. ]
  1194. },
  1195. {
  1196. tag: 'b',
  1197. attributes: {
  1198. class: [ 'b1', 'b2' ]
  1199. },
  1200. children: [
  1201. 'b'
  1202. ]
  1203. }
  1204. ]
  1205. } );
  1206. const spy = sinon.spy();
  1207. observable.on( 'ku', spy );
  1208. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1209. '<div><a class="a1 a2">a</a><b class="b1 b2">b</b></div>'
  1210. );
  1211. const tpl = new Template( {
  1212. tag: 'div',
  1213. attributes: {
  1214. class: [ 'div1', 'div2' ],
  1215. style: {
  1216. fontWeight: 'bold'
  1217. }
  1218. },
  1219. children: [
  1220. {
  1221. tag: 'a',
  1222. attributes: {
  1223. class: [ 'x', 'y' ],
  1224. 'data-new-attr': 'foo'
  1225. },
  1226. children: [ 'applied-a' ]
  1227. },
  1228. {
  1229. tag: 'b',
  1230. attributes: {
  1231. class: [
  1232. 'a',
  1233. 'b',
  1234. bind.to( 'foo' )
  1235. ]
  1236. },
  1237. children: [ 'applied-b' ]
  1238. }
  1239. ],
  1240. on: {
  1241. keyup: bind.to( 'ku' )
  1242. }
  1243. } );
  1244. tpl.apply( el );
  1245. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1246. '<div class="div1 div2" style="font-weight:bold;">' +
  1247. '<a class="a1 a2 x y" data-new-attr="foo">applied-a</a>' +
  1248. '<b class="b1 b2 a b bar">applied-b</b>' +
  1249. '</div>'
  1250. );
  1251. observable.foo = 'baz';
  1252. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1253. '<div class="div1 div2" style="font-weight:bold;">' +
  1254. '<a class="a1 a2 x y" data-new-attr="foo">applied-a</a>' +
  1255. '<b class="b1 b2 a b baz">applied-b</b>' +
  1256. '</div>'
  1257. );
  1258. dispatchEvent( el.firstChild, 'keyup' );
  1259. sinon.assert.calledOnce( spy );
  1260. tpl.revert( el );
  1261. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1262. '<div><a class="a1 a2">a</a><b class="b1 b2">b</b></div>'
  1263. );
  1264. observable.foo = 'qux';
  1265. expect( normalizeHtml( el.outerHTML ) ).to.equal(
  1266. '<div><a class="a1 a2">a</a><b class="b1 b2">b</b></div>'
  1267. );
  1268. dispatchEvent( el.firstChild, 'keyup' );
  1269. sinon.assert.calledOnce( spy );
  1270. } );
  1271. } );
  1272. } );
  1273. describe( 'getViews()', () => {
  1274. it( 'returns iterator', () => {
  1275. const template = new Template( {} );
  1276. expect( template.getViews().next ).to.be.a( 'function' );
  1277. expect( Array.from( template.getViews() ) ).to.have.length( 0 );
  1278. } );
  1279. it( 'returns all child views', () => {
  1280. const viewA = new View();
  1281. const viewB = new View();
  1282. const viewC = new View();
  1283. const template = new Template( {
  1284. tag: 'div',
  1285. children: [
  1286. viewA,
  1287. {
  1288. tag: 'div',
  1289. children: [
  1290. viewB
  1291. ]
  1292. },
  1293. viewC
  1294. ]
  1295. } );
  1296. expect( Array.from( template.getViews() ) ).to.have.members( [ viewA, viewB, viewC ] );
  1297. } );
  1298. // https://github.com/ckeditor/ckeditor5-ui/issues/337
  1299. it( 'does not traverse non–Template children', () => {
  1300. const viewA = new View();
  1301. const viewB = new View();
  1302. const viewC = new View();
  1303. const template = new Template( {
  1304. tag: 'div',
  1305. children: [
  1306. viewA,
  1307. ]
  1308. } );
  1309. // Technically, this kind of child is invalid but the aim of this test is to
  1310. // check if the generator will accidentally traverse non–Template objects
  1311. // like native HTML elements, which also have "children" property. It could happen
  1312. // because it is possible to pass HTML elements directly to the templateDefinition.
  1313. template.children.push( {
  1314. children: [ viewB ]
  1315. } );
  1316. template.children.push( viewC );
  1317. expect( Array.from( template.getViews() ) ).to.have.members( [ viewA, viewC ] );
  1318. } );
  1319. } );
  1320. describe( 'bind()', () => {
  1321. it( 'returns object', () => {
  1322. expect( Template.bind() ).to.be.an( 'object' );
  1323. } );
  1324. it( 'provides "to" and "if" interface', () => {
  1325. const bind = Template.bind();
  1326. expect( bind ).to.have.keys( 'to', 'if' );
  1327. expect( bind.to ).to.be.a( 'function' );
  1328. expect( bind.if ).to.be.a( 'function' );
  1329. } );
  1330. describe( 'event', () => {
  1331. let observable, domEmitter, bind;
  1332. beforeEach( () => {
  1333. observable = new Model( {
  1334. foo: 'bar',
  1335. baz: 'qux'
  1336. } );
  1337. domEmitter = Object.create( DomEmitterMixin );
  1338. bind = Template.bind( observable, domEmitter );
  1339. } );
  1340. it( 'accepts plain binding', () => {
  1341. const spy = testUtils.sinon.spy();
  1342. setElement( {
  1343. tag: 'p',
  1344. on: {
  1345. x: bind.to( 'a' ),
  1346. }
  1347. } );
  1348. observable.on( 'a', spy );
  1349. dispatchEvent( el, 'x' );
  1350. sinon.assert.calledWithExactly( spy,
  1351. sinon.match.has( 'name', 'a' ),
  1352. sinon.match.has( 'target', el )
  1353. );
  1354. } );
  1355. it( 'accepts an array of event bindings', () => {
  1356. const spy1 = testUtils.sinon.spy();
  1357. const spy2 = testUtils.sinon.spy();
  1358. setElement( {
  1359. tag: 'p',
  1360. on: {
  1361. x: [
  1362. bind.to( 'a' ),
  1363. bind.to( 'b' )
  1364. ]
  1365. }
  1366. } );
  1367. observable.on( 'a', spy1 );
  1368. observable.on( 'b', spy2 );
  1369. dispatchEvent( el, 'x' );
  1370. sinon.assert.calledWithExactly( spy1,
  1371. sinon.match.has( 'name', 'a' ),
  1372. sinon.match.has( 'target', el )
  1373. );
  1374. sinon.assert.calledWithExactly( spy2,
  1375. sinon.match.has( 'name', 'b' ),
  1376. sinon.match.has( 'target', el )
  1377. );
  1378. } );
  1379. it( 'accepts DOM selectors', () => {
  1380. const spy1 = testUtils.sinon.spy();
  1381. const spy2 = testUtils.sinon.spy();
  1382. const spy3 = testUtils.sinon.spy();
  1383. setElement( {
  1384. tag: 'p',
  1385. children: [
  1386. {
  1387. tag: 'span',
  1388. attributes: {
  1389. 'class': 'y',
  1390. },
  1391. on: {
  1392. 'test@p': bind.to( 'c' )
  1393. }
  1394. },
  1395. {
  1396. tag: 'div',
  1397. children: [
  1398. {
  1399. tag: 'span',
  1400. attributes: {
  1401. 'class': 'y',
  1402. }
  1403. }
  1404. ],
  1405. }
  1406. ],
  1407. on: {
  1408. 'test@.y': bind.to( 'a' ),
  1409. 'test@div': bind.to( 'b' )
  1410. }
  1411. } );
  1412. observable.on( 'a', spy1 );
  1413. observable.on( 'b', spy2 );
  1414. observable.on( 'c', spy3 );
  1415. // Test "test@p".
  1416. dispatchEvent( el, 'test' );
  1417. sinon.assert.callCount( spy1, 0 );
  1418. sinon.assert.callCount( spy2, 0 );
  1419. sinon.assert.callCount( spy3, 0 );
  1420. // Test "test@.y".
  1421. dispatchEvent( el.firstChild, 'test' );
  1422. expect( spy1.firstCall.calledWithExactly(
  1423. sinon.match.has( 'name', 'a' ),
  1424. sinon.match.has( 'target', el.firstChild )
  1425. ) ).to.be.true;
  1426. sinon.assert.callCount( spy2, 0 );
  1427. sinon.assert.callCount( spy3, 0 );
  1428. // Test "test@div".
  1429. dispatchEvent( el.lastChild, 'test' );
  1430. sinon.assert.callCount( spy1, 1 );
  1431. expect( spy2.firstCall.calledWithExactly(
  1432. sinon.match.has( 'name', 'b' ),
  1433. sinon.match.has( 'target', el.lastChild )
  1434. ) ).to.be.true;
  1435. sinon.assert.callCount( spy3, 0 );
  1436. // Test "test@.y".
  1437. dispatchEvent( el.lastChild.firstChild, 'test' );
  1438. expect( spy1.secondCall.calledWithExactly(
  1439. sinon.match.has( 'name', 'a' ),
  1440. sinon.match.has( 'target', el.lastChild.firstChild )
  1441. ) ).to.be.true;
  1442. sinon.assert.callCount( spy2, 1 );
  1443. sinon.assert.callCount( spy3, 0 );
  1444. } );
  1445. it( 'accepts function callbacks', () => {
  1446. const spy1 = testUtils.sinon.spy();
  1447. const spy2 = testUtils.sinon.spy();
  1448. setElement( {
  1449. tag: 'p',
  1450. children: [
  1451. {
  1452. tag: 'span'
  1453. }
  1454. ],
  1455. on: {
  1456. x: bind.to( spy1 ),
  1457. 'y@span': [
  1458. bind.to( spy2 ),
  1459. bind.to( 'c' )
  1460. ]
  1461. }
  1462. } );
  1463. dispatchEvent( el, 'x' );
  1464. dispatchEvent( el.firstChild, 'y' );
  1465. sinon.assert.calledWithExactly( spy1,
  1466. sinon.match.has( 'target', el )
  1467. );
  1468. sinon.assert.calledWithExactly( spy2,
  1469. sinon.match.has( 'target', el.firstChild )
  1470. );
  1471. } );
  1472. it( 'supports event delegation', () => {
  1473. const spy = testUtils.sinon.spy();
  1474. setElement( {
  1475. tag: 'p',
  1476. children: [
  1477. {
  1478. tag: 'span'
  1479. }
  1480. ],
  1481. on: {
  1482. x: bind.to( 'a' ),
  1483. }
  1484. } );
  1485. observable.on( 'a', spy );
  1486. dispatchEvent( el.firstChild, 'x' );
  1487. sinon.assert.calledWithExactly( spy,
  1488. sinon.match.has( 'name', 'a' ),
  1489. sinon.match.has( 'target', el.firstChild )
  1490. );
  1491. } );
  1492. it( 'works for future elements', () => {
  1493. const spy = testUtils.sinon.spy();
  1494. setElement( {
  1495. tag: 'p',
  1496. on: {
  1497. 'test@div': bind.to( 'a' )
  1498. }
  1499. } );
  1500. observable.on( 'a', spy );
  1501. const div = getElement( { tag: 'div' } );
  1502. el.appendChild( div );
  1503. dispatchEvent( div, 'test' );
  1504. sinon.assert.calledWithExactly( spy, sinon.match.has( 'name', 'a' ), sinon.match.has( 'target', div ) );
  1505. } );
  1506. } );
  1507. describe( 'model', () => {
  1508. let observable, emitter, bind;
  1509. beforeEach( () => {
  1510. observable = new Model( {
  1511. foo: 'bar',
  1512. baz: 'qux'
  1513. } );
  1514. emitter = Object.create( EmitterMixin );
  1515. bind = Template.bind( observable, emitter );
  1516. } );
  1517. describe( 'to', () => {
  1518. it( 'returns an instance of TemplateToBinding', () => {
  1519. const spy = testUtils.sinon.spy();
  1520. const binding = bind.to( 'foo', spy );
  1521. expect( binding ).to.be.instanceof( TemplateToBinding );
  1522. expect( spy.called ).to.be.false;
  1523. expect( binding ).to.have.keys( [ 'observable', 'eventNameOrFunction', 'emitter', 'attribute', 'callback' ] );
  1524. expect( binding.observable ).to.equal( observable );
  1525. expect( binding.callback ).to.equal( spy );
  1526. expect( binding.attribute ).to.equal( 'foo' );
  1527. } );
  1528. it( 'allows binding attribute to the observable – simple (HTMLElement attribute)', () => {
  1529. setElement( {
  1530. tag: 'p',
  1531. attributes: {
  1532. 'class': bind.to( 'foo' )
  1533. },
  1534. children: [ 'abc' ]
  1535. } );
  1536. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar">abc</p>' );
  1537. observable.foo = 'baz';
  1538. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="baz">abc</p>' );
  1539. expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.be.null;
  1540. } );
  1541. it( 'allows binding attribute to the observable – simple (Text Node)', () => {
  1542. setElement( {
  1543. tag: 'p',
  1544. children: [
  1545. {
  1546. text: bind.to( 'foo' )
  1547. }
  1548. ]
  1549. } );
  1550. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>bar</p>' );
  1551. observable.foo = 'baz';
  1552. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>baz</p>' );
  1553. } );
  1554. it( 'allows binding attribute to the observable – value processing', () => {
  1555. const callback = value => value > 0 ? 'positive' : 'negative';
  1556. setElement( {
  1557. tag: 'p',
  1558. attributes: {
  1559. 'class': bind.to( 'foo', callback )
  1560. },
  1561. children: [
  1562. {
  1563. text: bind.to( 'foo', callback )
  1564. }
  1565. ]
  1566. } );
  1567. observable.foo = 3;
  1568. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="positive">positive</p>' );
  1569. observable.foo = -7;
  1570. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="negative">negative</p>' );
  1571. } );
  1572. it( 'allows binding attribute to the observable – value processing (use Node)', () => {
  1573. const callback = ( value, node ) => {
  1574. return ( !!node.tagName && value > 0 ) ? 'HTMLElement positive' : '';
  1575. };
  1576. setElement( {
  1577. tag: 'p',
  1578. attributes: {
  1579. 'class': bind.to( 'foo', callback )
  1580. },
  1581. children: [
  1582. {
  1583. text: bind.to( 'foo', callback )
  1584. }
  1585. ]
  1586. } );
  1587. observable.foo = 3;
  1588. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="HTMLElement positive"></p>' );
  1589. observable.foo = -7;
  1590. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  1591. } );
  1592. it( 'allows binding attribute to the observable – custom callback', () => {
  1593. setElement( {
  1594. tag: 'p',
  1595. attributes: {
  1596. 'class': bind.to( 'foo', ( value, el ) => {
  1597. el.innerHTML = value;
  1598. if ( value == 'changed' ) {
  1599. return value;
  1600. }
  1601. } )
  1602. }
  1603. } );
  1604. observable.foo = 'moo';
  1605. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>moo</p>' );
  1606. observable.foo = 'changed';
  1607. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="changed">changed</p>' );
  1608. } );
  1609. it( 'allows binding attribute to the observable – array of bindings (HTMLElement attribute)', () => {
  1610. setElement( {
  1611. tag: 'p',
  1612. attributes: {
  1613. 'class': [
  1614. 'ck-class',
  1615. bind.to( 'foo' ),
  1616. bind.to( 'baz' ),
  1617. bind.to( 'foo', value => `foo-is-${ value }` ),
  1618. 'ck-end'
  1619. ]
  1620. },
  1621. children: [ 'abc' ]
  1622. } );
  1623. observable.foo = 'a';
  1624. observable.baz = 'b';
  1625. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="ck-class a b foo-is-a ck-end">abc</p>' );
  1626. observable.foo = 'c';
  1627. observable.baz = 'd';
  1628. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="ck-class c d foo-is-c ck-end">abc</p>' );
  1629. } );
  1630. it( 'allows binding attribute to the observable – array of bindings (Text Node)', () => {
  1631. setElement( {
  1632. tag: 'p',
  1633. attributes: {
  1634. },
  1635. children: [
  1636. {
  1637. text: [
  1638. 'ck-class',
  1639. bind.to( 'foo' ),
  1640. bind.to( 'baz' ),
  1641. bind.to( 'foo', value => `foo-is-${ value }` ),
  1642. 'ck-end'
  1643. ]
  1644. }
  1645. ]
  1646. } );
  1647. observable.foo = 'a';
  1648. observable.baz = 'b';
  1649. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>ck-class a b foo-is-a ck-end</p>' );
  1650. observable.foo = 'c';
  1651. observable.baz = 'd';
  1652. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>ck-class c d foo-is-c ck-end</p>' );
  1653. } );
  1654. it( 'allows binding attribute to the observable – falsy values', () => {
  1655. setElement( {
  1656. tag: 'p',
  1657. attributes: {
  1658. simple: bind.to( 'foo' ),
  1659. complex: [ null, bind.to( 'foo' ), undefined, false ],
  1660. zero: [ 0, bind.to( 'foo' ) ],
  1661. emptystring: [ '', bind.to( 'foo' ) ]
  1662. },
  1663. children: [ 'abc' ]
  1664. } );
  1665. observable.foo = 'bar';
  1666. expect( normalizeHtml( el.outerHTML ) )
  1667. .to.equal( '<p complex="bar" emptystring="bar" simple="bar" zero="0 bar">abc</p>' );
  1668. observable.foo = 0;
  1669. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p complex="0" emptystring="0" simple="0" zero="0 0">abc</p>' );
  1670. observable.foo = false;
  1671. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p zero="0">abc</p>' );
  1672. observable.foo = null;
  1673. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p zero="0">abc</p>' );
  1674. observable.foo = undefined;
  1675. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p zero="0">abc</p>' );
  1676. observable.foo = '';
  1677. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p zero="0">abc</p>' );
  1678. } );
  1679. it( 'allows binding attribute to the observable – a custom namespace', () => {
  1680. setElement( {
  1681. tag: 'p',
  1682. attributes: {
  1683. class: {
  1684. ns: 'foo',
  1685. value: bind.to( 'foo' )
  1686. },
  1687. custom: {
  1688. ns: 'foo',
  1689. value: [
  1690. bind.to( 'foo' ),
  1691. bind.to( 'baz' )
  1692. ]
  1693. }
  1694. },
  1695. children: [ 'abc' ]
  1696. } );
  1697. observable.foo = 'bar';
  1698. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar" custom="bar qux">abc</p>' );
  1699. expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.equal( 'foo' );
  1700. observable.foo = 'baz';
  1701. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="baz" custom="baz qux">abc</p>' );
  1702. expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.equal( 'foo' );
  1703. } );
  1704. } );
  1705. describe( 'if', () => {
  1706. it( 'returns an object which describes the binding', () => {
  1707. const spy = testUtils.sinon.spy();
  1708. const binding = bind.if( 'foo', 'whenTrue', spy );
  1709. expect( binding ).to.be.instanceof( TemplateIfBinding );
  1710. expect( spy.called ).to.be.false;
  1711. expect( binding ).to.have.keys( [ 'observable', 'emitter', 'attribute', 'callback', 'valueIfTrue' ] );
  1712. expect( binding.observable ).to.equal( observable );
  1713. expect( binding.callback ).to.equal( spy );
  1714. expect( binding.attribute ).to.equal( 'foo' );
  1715. expect( binding.valueIfTrue ).to.equal( 'whenTrue' );
  1716. } );
  1717. it( 'allows binding attribute to the observable – presence of an attribute (HTMLElement attribute)', () => {
  1718. setElement( {
  1719. tag: 'p',
  1720. attributes: {
  1721. 'class': bind.if( 'foo' )
  1722. },
  1723. children: [ 'abc' ]
  1724. } );
  1725. observable.foo = 'bar';
  1726. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="true">abc</p>' );
  1727. observable.foo = true;
  1728. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="true">abc</p>' );
  1729. observable.foo = 0;
  1730. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="true">abc</p>' );
  1731. observable.foo = false;
  1732. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1733. observable.foo = null;
  1734. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1735. observable.foo = undefined;
  1736. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1737. observable.foo = '';
  1738. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1739. } );
  1740. // TODO: Is this alright? It makes sense but it's pretty useless. Text Node cannot be
  1741. // removed just like an attribute of some HTMLElement.
  1742. it( 'allows binding attribute to the observable – presence of an attribute (Text Node)', () => {
  1743. setElement( {
  1744. tag: 'p',
  1745. children: [
  1746. {
  1747. text: bind.if( 'foo' )
  1748. }
  1749. ]
  1750. } );
  1751. observable.foo = 'abc';
  1752. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>true</p>' );
  1753. observable.foo = true;
  1754. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>true</p>' );
  1755. observable.foo = 0;
  1756. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>true</p>' );
  1757. observable.foo = false;
  1758. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  1759. observable.foo = null;
  1760. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  1761. observable.foo = undefined;
  1762. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  1763. observable.foo = '';
  1764. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  1765. } );
  1766. it( 'allows binding attribute to the observable – value of an attribute (HTMLElement attribute)', () => {
  1767. setElement( {
  1768. tag: 'p',
  1769. attributes: {
  1770. 'class': bind.if( 'foo', 'bar' )
  1771. },
  1772. children: [ 'abc' ]
  1773. } );
  1774. observable.foo = 'bar';
  1775. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar">abc</p>' );
  1776. observable.foo = true;
  1777. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar">abc</p>' );
  1778. observable.foo = 0;
  1779. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar">abc</p>' );
  1780. observable.foo = 64;
  1781. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar">abc</p>' );
  1782. observable.foo = false;
  1783. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1784. observable.foo = null;
  1785. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1786. observable.foo = undefined;
  1787. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1788. } );
  1789. it( 'allows binding attribute to the observable – value of an attribute (Text Node)', () => {
  1790. setElement( {
  1791. tag: 'p',
  1792. children: [
  1793. {
  1794. text: bind.if( 'foo', 'bar' )
  1795. }
  1796. ]
  1797. } );
  1798. observable.foo = 'bar';
  1799. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>bar</p>' );
  1800. observable.foo = false;
  1801. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  1802. observable.foo = 64;
  1803. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>bar</p>' );
  1804. } );
  1805. it( 'allows binding attribute to the observable – array of bindings (HTMLElement attribute)', () => {
  1806. setElement( {
  1807. tag: 'p',
  1808. attributes: {
  1809. 'class': [
  1810. 'ck-class',
  1811. bind.if( 'foo', 'foo-set' ),
  1812. bind.if( 'bar', 'bar-not-set', value => !value ),
  1813. 'ck-end'
  1814. ]
  1815. },
  1816. children: [ 'abc' ]
  1817. } );
  1818. observable.foo = observable.bar = true;
  1819. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="ck-class foo-set ck-end">abc</p>' );
  1820. observable.foo = observable.bar = false;
  1821. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="ck-class bar-not-set ck-end">abc</p>' );
  1822. } );
  1823. it( 'allows binding attribute to the observable – value of an attribute processed by a callback', () => {
  1824. setElement( {
  1825. tag: 'p',
  1826. attributes: {
  1827. 'class': bind.if( 'foo', 'there–is–no–foo', value => !value )
  1828. },
  1829. children: [ 'abc' ]
  1830. } );
  1831. observable.foo = 'bar';
  1832. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1833. observable.foo = false;
  1834. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="there–is–no–foo">abc</p>' );
  1835. observable.foo = 64;
  1836. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1837. } );
  1838. it( 'allows binding attribute to the observable – value of an attribute processed by a callback (use Node)', () => {
  1839. setElement( {
  1840. tag: 'p',
  1841. attributes: {
  1842. 'class': bind.if( 'foo', 'eqls-tag-name', ( value, el ) => el.tagName === value )
  1843. },
  1844. children: [ 'abc' ]
  1845. } );
  1846. observable.foo = 'bar';
  1847. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1848. observable.foo = 'P';
  1849. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="eqls-tag-name">abc</p>' );
  1850. observable.foo = 64;
  1851. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1852. } );
  1853. it( 'allows binding attribute to the observable – falsy values', () => {
  1854. setElement( {
  1855. tag: 'p',
  1856. attributes: {
  1857. 'class': bind.if( 'foo', 'foo-is-set' )
  1858. },
  1859. children: [ 'abc' ]
  1860. } );
  1861. observable.foo = 'bar';
  1862. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="foo-is-set">abc</p>' );
  1863. observable.foo = true;
  1864. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="foo-is-set">abc</p>' );
  1865. observable.foo = 0;
  1866. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="foo-is-set">abc</p>' );
  1867. observable.foo = false;
  1868. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1869. observable.foo = null;
  1870. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1871. observable.foo = undefined;
  1872. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1873. observable.foo = '';
  1874. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1875. } );
  1876. } );
  1877. it( 'works with Template#apply() – root element', () => {
  1878. new Template( {
  1879. tag: 'div',
  1880. attributes: {
  1881. class: bind.to( 'foo' )
  1882. }
  1883. } ).apply( el );
  1884. expect( el.getAttribute( 'class' ) ).to.equal( 'bar' );
  1885. observable.foo = 'baz';
  1886. expect( el.getAttribute( 'class' ) ).to.equal( 'baz' );
  1887. } );
  1888. it( 'works with Template#apply() – children', () => {
  1889. const el = getElement( { tag: 'div' } );
  1890. const child = getElement( {
  1891. tag: 'span',
  1892. children: [ 'foo' ]
  1893. } );
  1894. el.appendChild( child );
  1895. new Template( {
  1896. tag: 'div',
  1897. children: [
  1898. {
  1899. tag: 'span',
  1900. children: [
  1901. {
  1902. text: bind.to( 'foo' )
  1903. }
  1904. ]
  1905. }
  1906. ]
  1907. } ).apply( el );
  1908. expect( child.textContent ).to.equal( 'bar' );
  1909. observable.foo = 'baz';
  1910. expect( child.textContent ).to.equal( 'baz' );
  1911. } );
  1912. } );
  1913. } );
  1914. describe( 'extend()', () => {
  1915. let observable, emitter, bind;
  1916. beforeEach( () => {
  1917. observable = new Model( {
  1918. foo: 'bar',
  1919. baz: 'qux'
  1920. } );
  1921. emitter = Object.create( DomEmitterMixin );
  1922. bind = Template.bind( observable, emitter );
  1923. } );
  1924. it( 'does not modify passed definition', () => {
  1925. const def = {
  1926. tag: 'p',
  1927. attributes: {
  1928. a: 'foo',
  1929. }
  1930. };
  1931. const ext = {
  1932. attributes: {
  1933. b: 'bar'
  1934. }
  1935. };
  1936. const tpl = new Template( def );
  1937. Template.extend( tpl, ext );
  1938. expect( ext.attributes.b ).to.equal( 'bar' );
  1939. expect( tpl.attributes.a[ 0 ] ).to.equal( 'foo' );
  1940. expect( tpl.attributes.b[ 0 ] ).to.equal( 'bar' );
  1941. } );
  1942. it( 'logs a warning if an element has already been rendered', () => {
  1943. const spy = testUtils.sinon.stub( log, 'warn' );
  1944. const tpl = new Template( {
  1945. tag: 'p'
  1946. } );
  1947. Template.extend( tpl, {
  1948. attributes: {
  1949. class: 'foo'
  1950. }
  1951. } );
  1952. tpl.render();
  1953. Template.extend( tpl, {
  1954. attributes: {
  1955. class: 'bar'
  1956. }
  1957. } );
  1958. sinon.assert.calledOnce( spy );
  1959. sinon.assert.calledWithExactly( spy, sinon.match( /^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. expect( () => {
  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. } ).to.throw( CKEditorError, /ui-template-extend-children-mismatch/ );
  2279. } );
  2280. it( 'should throw when no children in target but extending one', () => {
  2281. expect( () => {
  2282. extensionTest(
  2283. {
  2284. tag: 'p',
  2285. },
  2286. {
  2287. children: [
  2288. {
  2289. tag: 'b'
  2290. }
  2291. ]
  2292. },
  2293. 'it should fail'
  2294. );
  2295. } ).to.throw( CKEditorError, /ui-template-extend-children-mismatch/ );
  2296. } );
  2297. it( 'should throw when the number of children does not correspond on some deeper level', () => {
  2298. expect( () => {
  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. } ).to.throw( CKEditorError, /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="foo bar"></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 = testUtils.sinon.spy();
  2477. const spy2 = testUtils.sinon.spy();
  2478. const spy3 = testUtils.sinon.spy();
  2479. const spy4 = testUtils.sinon.spy();
  2480. const spy5 = testUtils.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 = testUtils.sinon.spy();
  2524. const spy2 = testUtils.sinon.spy();
  2525. const spy3 = testUtils.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. }