template.js 65 KB

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