template.js 65 KB

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