template.js 66 KB

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