8
0

template.js 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358
  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 = document.createElement( '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( 'applies new textContent to an existing Text Node of an HTMLElement', () => {
  565. el.textContent = 'bar';
  566. new Template( {
  567. tag: 'div',
  568. children: [ 'foo' ]
  569. } ).apply( el );
  570. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div>foo</div>' );
  571. } );
  572. } );
  573. describe( 'attributes', () => {
  574. it( 'applies attributes to an HTMLElement', () => {
  575. new Template( {
  576. tag: 'div',
  577. attributes: {
  578. 'class': [ 'a', 'b' ],
  579. x: 'bar'
  580. }
  581. } ).apply( el );
  582. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div class="a b" x="bar"></div>' );
  583. } );
  584. it( 'applies attributes and TextContent to a DOM tree', () => {
  585. el.textContent = 'abc';
  586. el.appendChild( document.createElement( 'span' ) );
  587. new Template( {
  588. tag: 'div',
  589. attributes: {
  590. 'class': 'parent'
  591. },
  592. children: [
  593. 'Children:',
  594. {
  595. tag: 'span',
  596. attributes: {
  597. class: 'child'
  598. }
  599. }
  600. ]
  601. } ).apply( el );
  602. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div class="parent">Children:<span class="child"></span></div>' );
  603. } );
  604. } );
  605. describe( 'children', () => {
  606. it( 'doesn\'t apply new child to an HTMLElement – Text Node', () => {
  607. new Template( {
  608. tag: 'div',
  609. children: [ 'foo' ]
  610. } ).apply( el );
  611. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div></div>' );
  612. } );
  613. it( 'doesn\'t apply new child to an HTMLElement – HTMLElement', () => {
  614. new Template( {
  615. tag: 'div',
  616. children: [
  617. {
  618. tag: 'span'
  619. }
  620. ]
  621. } ).apply( el );
  622. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div></div>' );
  623. } );
  624. it( 'doesn\'t apply new child to an HTMLElement – view', () => {
  625. const view = getView( {
  626. tag: 'span',
  627. attributes: {
  628. class: [
  629. 'v1'
  630. ]
  631. }
  632. } );
  633. new Template( {
  634. tag: 'p',
  635. children: [ view ]
  636. } ).apply( el );
  637. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div></div>' );
  638. } );
  639. it( 'doesn\'t apply new child to an HTMLElement – view collection', () => {
  640. const collection = new ViewCollection();
  641. collection.add( getView( {
  642. tag: 'span',
  643. attributes: {
  644. class: [
  645. 'v1'
  646. ]
  647. }
  648. } ) );
  649. new Template( {
  650. tag: 'p',
  651. children: collection
  652. } ).apply( el );
  653. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div></div>' );
  654. expect( collection._parentElement ).to.be.null;
  655. } );
  656. it( 'should work for deep DOM structure', () => {
  657. const childA = document.createElement( 'a' );
  658. const childB = document.createElement( 'b' );
  659. childA.textContent = 'anchor';
  660. childB.textContent = 'bold';
  661. el.appendChild( childA );
  662. el.appendChild( childB );
  663. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div><a>anchor</a><b>bold</b></div>' );
  664. const spy1 = testUtils.sinon.spy();
  665. const spy2 = testUtils.sinon.spy();
  666. const spy3 = testUtils.sinon.spy();
  667. observable.on( 'ku', spy1 );
  668. observable.on( 'kd', spy2 );
  669. observable.on( 'mo', spy3 );
  670. new Template( {
  671. tag: 'div',
  672. children: [
  673. {
  674. tag: 'a',
  675. on: {
  676. keyup: bind.to( 'ku' )
  677. },
  678. attributes: {
  679. class: bind.to( 'foo', val => 'applied-A-' + val ),
  680. id: 'applied-A'
  681. },
  682. children: [ 'Text applied to childA.' ]
  683. },
  684. {
  685. tag: 'b',
  686. on: {
  687. keydown: bind.to( 'kd' )
  688. },
  689. attributes: {
  690. class: bind.to( 'baz', val => 'applied-B-' + val ),
  691. id: 'applied-B'
  692. },
  693. children: [ 'Text applied to childB.' ]
  694. },
  695. 'Text which is not to be applied because it does NOT exist in original element.'
  696. ],
  697. on: {
  698. 'mouseover@a': bind.to( 'mo' )
  699. },
  700. attributes: {
  701. id: bind.to( 'foo', val => val.toUpperCase() ),
  702. class: bind.to( 'baz', val => 'applied-parent-' + val )
  703. }
  704. } ).apply( el );
  705. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div class="applied-parent-qux" id="BAR">' +
  706. '<a class="applied-A-bar" id="applied-A">Text applied to childA.</a>' +
  707. '<b class="applied-B-qux" id="applied-B">Text applied to childB.</b>' +
  708. '</div>' );
  709. observable.foo = 'updated';
  710. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div class="applied-parent-qux" id="UPDATED">' +
  711. '<a class="applied-A-updated" id="applied-A">Text applied to childA.</a>' +
  712. '<b class="applied-B-qux" id="applied-B">Text applied to childB.</b>' +
  713. '</div>' );
  714. document.body.appendChild( el );
  715. // Test "mouseover@a".
  716. dispatchEvent( el, 'mouseover' );
  717. dispatchEvent( childA, 'mouseover' );
  718. // Test "keyup".
  719. dispatchEvent( childA, 'keyup' );
  720. // Test "keydown".
  721. dispatchEvent( childB, 'keydown' );
  722. sinon.assert.calledOnce( spy1 );
  723. sinon.assert.calledOnce( spy2 );
  724. sinon.assert.calledOnce( spy3 );
  725. } );
  726. } );
  727. } );
  728. describe( 'bind()', () => {
  729. it( 'returns object', () => {
  730. expect( Template.bind() ).to.be.an( 'object' );
  731. } );
  732. it( 'provides "to" and "if" interface', () => {
  733. const bind = Template.bind();
  734. expect( bind ).to.have.keys( 'to', 'if' );
  735. expect( bind.to ).to.be.a( 'function' );
  736. expect( bind.if ).to.be.a( 'function' );
  737. } );
  738. describe( 'event', () => {
  739. let observable, domEmitter, bind;
  740. beforeEach( () => {
  741. observable = new Model( {
  742. foo: 'bar',
  743. baz: 'qux'
  744. } );
  745. domEmitter = Object.create( DomEmitterMixin );
  746. bind = Template.bind( observable, domEmitter );
  747. } );
  748. it( 'accepts plain binding', () => {
  749. const spy = testUtils.sinon.spy();
  750. setElement( {
  751. tag: 'p',
  752. on: {
  753. x: bind.to( 'a' ),
  754. }
  755. } );
  756. observable.on( 'a', spy );
  757. dispatchEvent( el, 'x' );
  758. sinon.assert.calledWithExactly( spy,
  759. sinon.match.has( 'name', 'a' ),
  760. sinon.match.has( 'target', el )
  761. );
  762. } );
  763. it( 'accepts an array of event bindings', () => {
  764. const spy1 = testUtils.sinon.spy();
  765. const spy2 = testUtils.sinon.spy();
  766. setElement( {
  767. tag: 'p',
  768. on: {
  769. x: [
  770. bind.to( 'a' ),
  771. bind.to( 'b' )
  772. ]
  773. }
  774. } );
  775. observable.on( 'a', spy1 );
  776. observable.on( 'b', spy2 );
  777. dispatchEvent( el, 'x' );
  778. sinon.assert.calledWithExactly( spy1,
  779. sinon.match.has( 'name', 'a' ),
  780. sinon.match.has( 'target', el )
  781. );
  782. sinon.assert.calledWithExactly( spy2,
  783. sinon.match.has( 'name', 'b' ),
  784. sinon.match.has( 'target', el )
  785. );
  786. } );
  787. it( 'accepts DOM selectors', () => {
  788. const spy1 = testUtils.sinon.spy();
  789. const spy2 = testUtils.sinon.spy();
  790. const spy3 = testUtils.sinon.spy();
  791. setElement( {
  792. tag: 'p',
  793. children: [
  794. {
  795. tag: 'span',
  796. attributes: {
  797. 'class': 'y',
  798. },
  799. on: {
  800. 'test@p': bind.to( 'c' )
  801. }
  802. },
  803. {
  804. tag: 'div',
  805. children: [
  806. {
  807. tag: 'span',
  808. attributes: {
  809. 'class': 'y',
  810. }
  811. }
  812. ],
  813. }
  814. ],
  815. on: {
  816. 'test@.y': bind.to( 'a' ),
  817. 'test@div': bind.to( 'b' )
  818. }
  819. } );
  820. observable.on( 'a', spy1 );
  821. observable.on( 'b', spy2 );
  822. observable.on( 'c', spy3 );
  823. // Test "test@p".
  824. dispatchEvent( el, 'test' );
  825. sinon.assert.callCount( spy1, 0 );
  826. sinon.assert.callCount( spy2, 0 );
  827. sinon.assert.callCount( spy3, 0 );
  828. // Test "test@.y".
  829. dispatchEvent( el.firstChild, 'test' );
  830. expect( spy1.firstCall.calledWithExactly(
  831. sinon.match.has( 'name', 'a' ),
  832. sinon.match.has( 'target', el.firstChild )
  833. ) ).to.be.true;
  834. sinon.assert.callCount( spy2, 0 );
  835. sinon.assert.callCount( spy3, 0 );
  836. // Test "test@div".
  837. dispatchEvent( el.lastChild, 'test' );
  838. sinon.assert.callCount( spy1, 1 );
  839. expect( spy2.firstCall.calledWithExactly(
  840. sinon.match.has( 'name', 'b' ),
  841. sinon.match.has( 'target', el.lastChild )
  842. ) ).to.be.true;
  843. sinon.assert.callCount( spy3, 0 );
  844. // Test "test@.y".
  845. dispatchEvent( el.lastChild.firstChild, 'test' );
  846. expect( spy1.secondCall.calledWithExactly(
  847. sinon.match.has( 'name', 'a' ),
  848. sinon.match.has( 'target', el.lastChild.firstChild )
  849. ) ).to.be.true;
  850. sinon.assert.callCount( spy2, 1 );
  851. sinon.assert.callCount( spy3, 0 );
  852. } );
  853. it( 'accepts function callbacks', () => {
  854. const spy1 = testUtils.sinon.spy();
  855. const spy2 = testUtils.sinon.spy();
  856. setElement( {
  857. tag: 'p',
  858. children: [
  859. {
  860. tag: 'span'
  861. }
  862. ],
  863. on: {
  864. x: bind.to( spy1 ),
  865. 'y@span': [
  866. bind.to( spy2 ),
  867. bind.to( 'c' )
  868. ]
  869. }
  870. } );
  871. dispatchEvent( el, 'x' );
  872. dispatchEvent( el.firstChild, 'y' );
  873. sinon.assert.calledWithExactly( spy1,
  874. sinon.match.has( 'target', el )
  875. );
  876. sinon.assert.calledWithExactly( spy2,
  877. sinon.match.has( 'target', el.firstChild )
  878. );
  879. } );
  880. it( 'supports event delegation', () => {
  881. const spy = testUtils.sinon.spy();
  882. setElement( {
  883. tag: 'p',
  884. children: [
  885. {
  886. tag: 'span'
  887. }
  888. ],
  889. on: {
  890. x: bind.to( 'a' ),
  891. }
  892. } );
  893. observable.on( 'a', spy );
  894. dispatchEvent( el.firstChild, 'x' );
  895. sinon.assert.calledWithExactly( spy,
  896. sinon.match.has( 'name', 'a' ),
  897. sinon.match.has( 'target', el.firstChild )
  898. );
  899. } );
  900. it( 'works for future elements', () => {
  901. const spy = testUtils.sinon.spy();
  902. setElement( {
  903. tag: 'p',
  904. on: {
  905. 'test@div': bind.to( 'a' )
  906. }
  907. } );
  908. observable.on( 'a', spy );
  909. const div = document.createElement( 'div' );
  910. el.appendChild( div );
  911. dispatchEvent( div, 'test' );
  912. sinon.assert.calledWithExactly( spy, sinon.match.has( 'name', 'a' ), sinon.match.has( 'target', div ) );
  913. } );
  914. } );
  915. describe( 'model', () => {
  916. let observable, emitter, bind;
  917. beforeEach( () => {
  918. observable = new Model( {
  919. foo: 'bar',
  920. baz: 'qux'
  921. } );
  922. emitter = Object.create( EmitterMixin );
  923. bind = Template.bind( observable, emitter );
  924. } );
  925. describe( 'to', () => {
  926. it( 'returns an instance of TemplateToBinding', () => {
  927. const spy = testUtils.sinon.spy();
  928. const binding = bind.to( 'foo', spy );
  929. expect( binding ).to.be.instanceof( TemplateToBinding );
  930. expect( spy.called ).to.be.false;
  931. expect( binding ).to.have.keys( [ 'observable', 'eventNameOrFunction', 'emitter', 'attribute', 'callback' ] );
  932. expect( binding.observable ).to.equal( observable );
  933. expect( binding.callback ).to.equal( spy );
  934. expect( binding.attribute ).to.equal( 'foo' );
  935. } );
  936. it( 'allows binding attribute to the observable – simple (HTMLElement attribute)', () => {
  937. setElement( {
  938. tag: 'p',
  939. attributes: {
  940. 'class': bind.to( 'foo' )
  941. },
  942. children: [ 'abc' ]
  943. } );
  944. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar">abc</p>' );
  945. observable.foo = 'baz';
  946. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="baz">abc</p>' );
  947. expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.be.null;
  948. } );
  949. it( 'allows binding attribute to the observable – simple (Text Node)', () => {
  950. setElement( {
  951. tag: 'p',
  952. children: [
  953. {
  954. text: bind.to( 'foo' )
  955. }
  956. ]
  957. } );
  958. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>bar</p>' );
  959. observable.foo = 'baz';
  960. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>baz</p>' );
  961. } );
  962. it( 'allows binding attribute to the observable – value processing', () => {
  963. const callback = value => value > 0 ? 'positive' : 'negative';
  964. setElement( {
  965. tag: 'p',
  966. attributes: {
  967. 'class': bind.to( 'foo', callback )
  968. },
  969. children: [
  970. {
  971. text: bind.to( 'foo', callback )
  972. }
  973. ]
  974. } );
  975. observable.foo = 3;
  976. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="positive">positive</p>' );
  977. observable.foo = -7;
  978. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="negative">negative</p>' );
  979. } );
  980. it( 'allows binding attribute to the observable – value processing (use Node)', () => {
  981. const callback = ( value, node ) => {
  982. return ( !!node.tagName && value > 0 ) ? 'HTMLElement positive' : '';
  983. };
  984. setElement( {
  985. tag: 'p',
  986. attributes: {
  987. 'class': bind.to( 'foo', callback )
  988. },
  989. children: [
  990. {
  991. text: bind.to( 'foo', callback )
  992. }
  993. ]
  994. } );
  995. observable.foo = 3;
  996. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="HTMLElement positive"></p>' );
  997. observable.foo = -7;
  998. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  999. } );
  1000. it( 'allows binding attribute to the observable – custom callback', () => {
  1001. setElement( {
  1002. tag: 'p',
  1003. attributes: {
  1004. 'class': bind.to( 'foo', ( value, el ) => {
  1005. el.innerHTML = value;
  1006. if ( value == 'changed' ) {
  1007. return value;
  1008. }
  1009. } )
  1010. }
  1011. } );
  1012. observable.foo = 'moo';
  1013. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>moo</p>' );
  1014. observable.foo = 'changed';
  1015. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="changed">changed</p>' );
  1016. } );
  1017. it( 'allows binding attribute to the observable – array of bindings (HTMLElement attribute)', () => {
  1018. setElement( {
  1019. tag: 'p',
  1020. attributes: {
  1021. 'class': [
  1022. 'ck-class',
  1023. bind.to( 'foo' ),
  1024. bind.to( 'baz' ),
  1025. bind.to( 'foo', value => `foo-is-${value}` ),
  1026. 'ck-end'
  1027. ]
  1028. },
  1029. children: [ 'abc' ]
  1030. } );
  1031. observable.foo = 'a';
  1032. observable.baz = 'b';
  1033. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="ck-class a b foo-is-a ck-end">abc</p>' );
  1034. observable.foo = 'c';
  1035. observable.baz = 'd';
  1036. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="ck-class c d foo-is-c ck-end">abc</p>' );
  1037. } );
  1038. it( 'allows binding attribute to the observable – array of bindings (Text Node)', () => {
  1039. setElement( {
  1040. tag: 'p',
  1041. attributes: {
  1042. },
  1043. children: [
  1044. {
  1045. text: [
  1046. 'ck-class',
  1047. bind.to( 'foo' ),
  1048. bind.to( 'baz' ),
  1049. bind.to( 'foo', value => `foo-is-${value}` ),
  1050. 'ck-end'
  1051. ]
  1052. }
  1053. ]
  1054. } );
  1055. observable.foo = 'a';
  1056. observable.baz = 'b';
  1057. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>ck-class a b foo-is-a ck-end</p>' );
  1058. observable.foo = 'c';
  1059. observable.baz = 'd';
  1060. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>ck-class c d foo-is-c ck-end</p>' );
  1061. } );
  1062. it( 'allows binding attribute to the observable – falsy values', () => {
  1063. setElement( {
  1064. tag: 'p',
  1065. attributes: {
  1066. simple: bind.to( 'foo' ),
  1067. complex: [ null, bind.to( 'foo' ), undefined, false ],
  1068. zero: [ 0, bind.to( 'foo' ) ],
  1069. emptystring: [ '', bind.to( 'foo' ) ]
  1070. },
  1071. children: [ 'abc' ]
  1072. } );
  1073. observable.foo = 'bar';
  1074. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p complex="bar" emptystring="bar" simple="bar" zero="0 bar">abc</p>' );
  1075. observable.foo = 0;
  1076. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p complex="0" emptystring="0" simple="0" zero="0 0">abc</p>' );
  1077. observable.foo = false;
  1078. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p zero="0">abc</p>' );
  1079. observable.foo = null;
  1080. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p zero="0">abc</p>' );
  1081. observable.foo = undefined;
  1082. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p zero="0">abc</p>' );
  1083. observable.foo = '';
  1084. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p zero="0">abc</p>' );
  1085. } );
  1086. it( 'allows binding attribute to the observable – a custom namespace', () => {
  1087. setElement( {
  1088. tag: 'p',
  1089. attributes: {
  1090. class: {
  1091. ns: 'foo',
  1092. value: bind.to( 'foo' )
  1093. },
  1094. custom: {
  1095. ns: 'foo',
  1096. value: [
  1097. bind.to( 'foo' ),
  1098. bind.to( 'baz' )
  1099. ]
  1100. }
  1101. },
  1102. children: [ 'abc' ]
  1103. } );
  1104. observable.foo = 'bar';
  1105. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar" custom="bar qux">abc</p>' );
  1106. expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.equal( 'foo' );
  1107. observable.foo = 'baz';
  1108. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="baz" custom="baz qux">abc</p>' );
  1109. expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.equal( 'foo' );
  1110. } );
  1111. } );
  1112. describe( 'if', () => {
  1113. it( 'returns an object which describes the binding', () => {
  1114. const spy = testUtils.sinon.spy();
  1115. const binding = bind.if( 'foo', 'whenTrue', spy );
  1116. expect( binding ).to.be.instanceof( TemplateIfBinding );
  1117. expect( spy.called ).to.be.false;
  1118. expect( binding ).to.have.keys( [ 'observable', 'emitter', 'attribute', 'callback', 'valueIfTrue' ] );
  1119. expect( binding.observable ).to.equal( observable );
  1120. expect( binding.callback ).to.equal( spy );
  1121. expect( binding.attribute ).to.equal( 'foo' );
  1122. expect( binding.valueIfTrue ).to.equal( 'whenTrue' );
  1123. } );
  1124. it( 'allows binding attribute to the observable – presence of an attribute (HTMLElement attribute)', () => {
  1125. setElement( {
  1126. tag: 'p',
  1127. attributes: {
  1128. 'class': bind.if( 'foo' )
  1129. },
  1130. children: [ 'abc' ]
  1131. } );
  1132. observable.foo = 'bar';
  1133. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="true">abc</p>' );
  1134. observable.foo = true;
  1135. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="true">abc</p>' );
  1136. observable.foo = 0;
  1137. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="true">abc</p>' );
  1138. observable.foo = false;
  1139. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1140. observable.foo = null;
  1141. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1142. observable.foo = undefined;
  1143. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1144. observable.foo = '';
  1145. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1146. } );
  1147. // TODO: Is this alright? It makes sense but it's pretty useless. Text Node cannot be
  1148. // removed just like an attribute of some HTMLElement.
  1149. it( 'allows binding attribute to the observable – presence of an attribute (Text Node)', () => {
  1150. setElement( {
  1151. tag: 'p',
  1152. children: [
  1153. {
  1154. text: bind.if( 'foo' )
  1155. }
  1156. ]
  1157. } );
  1158. observable.foo = 'abc';
  1159. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>true</p>' );
  1160. observable.foo = true;
  1161. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>true</p>' );
  1162. observable.foo = 0;
  1163. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>true</p>' );
  1164. observable.foo = false;
  1165. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  1166. observable.foo = null;
  1167. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  1168. observable.foo = undefined;
  1169. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  1170. observable.foo = '';
  1171. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  1172. } );
  1173. it( 'allows binding attribute to the observable – value of an attribute (HTMLElement attribute)', () => {
  1174. setElement( {
  1175. tag: 'p',
  1176. attributes: {
  1177. 'class': bind.if( 'foo', 'bar' )
  1178. },
  1179. children: [ 'abc' ]
  1180. } );
  1181. observable.foo = 'bar';
  1182. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar">abc</p>' );
  1183. observable.foo = true;
  1184. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar">abc</p>' );
  1185. observable.foo = 0;
  1186. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar">abc</p>' );
  1187. observable.foo = 64;
  1188. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar">abc</p>' );
  1189. observable.foo = false;
  1190. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1191. observable.foo = null;
  1192. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1193. observable.foo = undefined;
  1194. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1195. } );
  1196. it( 'allows binding attribute to the observable – value of an attribute (Text Node)', () => {
  1197. setElement( {
  1198. tag: 'p',
  1199. children: [
  1200. {
  1201. text: bind.if( 'foo', 'bar' )
  1202. }
  1203. ]
  1204. } );
  1205. observable.foo = 'bar';
  1206. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>bar</p>' );
  1207. observable.foo = false;
  1208. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
  1209. observable.foo = 64;
  1210. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>bar</p>' );
  1211. } );
  1212. it( 'allows binding attribute to the observable – array of bindings (HTMLElement attribute)', () => {
  1213. setElement( {
  1214. tag: 'p',
  1215. attributes: {
  1216. 'class': [
  1217. 'ck-class',
  1218. bind.if( 'foo', 'foo-set' ),
  1219. bind.if( 'bar', 'bar-not-set', ( value ) => !value ),
  1220. 'ck-end'
  1221. ]
  1222. },
  1223. children: [ 'abc' ]
  1224. } );
  1225. observable.foo = observable.bar = true;
  1226. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="ck-class foo-set ck-end">abc</p>' );
  1227. observable.foo = observable.bar = false;
  1228. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="ck-class bar-not-set ck-end">abc</p>' );
  1229. } );
  1230. it( 'allows binding attribute to the observable – value of an attribute processed by a callback', () => {
  1231. setElement( {
  1232. tag: 'p',
  1233. attributes: {
  1234. 'class': bind.if( 'foo', 'there–is–no–foo', value => !value )
  1235. },
  1236. children: [ 'abc' ]
  1237. } );
  1238. observable.foo = 'bar';
  1239. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1240. observable.foo = false;
  1241. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="there–is–no–foo">abc</p>' );
  1242. observable.foo = 64;
  1243. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1244. } );
  1245. it( 'allows binding attribute to the observable – value of an attribute processed by a callback (use Node)', () => {
  1246. setElement( {
  1247. tag: 'p',
  1248. attributes: {
  1249. 'class': bind.if( 'foo', 'eqls-tag-name', ( value, el ) => el.tagName === value )
  1250. },
  1251. children: [ 'abc' ]
  1252. } );
  1253. observable.foo = 'bar';
  1254. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1255. observable.foo = 'P';
  1256. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="eqls-tag-name">abc</p>' );
  1257. observable.foo = 64;
  1258. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1259. } );
  1260. it( 'allows binding attribute to the observable – falsy values', () => {
  1261. setElement( {
  1262. tag: 'p',
  1263. attributes: {
  1264. 'class': bind.if( 'foo', 'foo-is-set' )
  1265. },
  1266. children: [ 'abc' ]
  1267. } );
  1268. observable.foo = 'bar';
  1269. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="foo-is-set">abc</p>' );
  1270. observable.foo = true;
  1271. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="foo-is-set">abc</p>' );
  1272. observable.foo = 0;
  1273. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="foo-is-set">abc</p>' );
  1274. observable.foo = false;
  1275. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1276. observable.foo = null;
  1277. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1278. observable.foo = undefined;
  1279. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1280. observable.foo = '';
  1281. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
  1282. } );
  1283. } );
  1284. it( 'works with Template#apply() – root element', () => {
  1285. new Template( {
  1286. tag: 'div',
  1287. attributes: {
  1288. class: bind.to( 'foo' )
  1289. }
  1290. } ).apply( el );
  1291. expect( el.getAttribute( 'class' ) ).to.equal( 'bar' );
  1292. observable.foo = 'baz';
  1293. expect( el.getAttribute( 'class' ) ).to.equal( 'baz' );
  1294. } );
  1295. it( 'works with Template#apply() – children', () => {
  1296. const el = document.createElement( 'div' );
  1297. const child = document.createElement( 'span' );
  1298. child.textContent = 'foo';
  1299. el.appendChild( child );
  1300. new Template( {
  1301. tag: 'div',
  1302. children: [
  1303. {
  1304. tag: 'span',
  1305. children: [
  1306. {
  1307. text: bind.to( 'foo' )
  1308. }
  1309. ]
  1310. }
  1311. ]
  1312. } ).apply( el );
  1313. expect( child.textContent ).to.equal( 'bar' );
  1314. observable.foo = 'baz';
  1315. expect( child.textContent ).to.equal( 'baz' );
  1316. } );
  1317. } );
  1318. } );
  1319. describe( 'extend()', () => {
  1320. let observable, emitter, bind;
  1321. beforeEach( () => {
  1322. observable = new Model( {
  1323. foo: 'bar',
  1324. baz: 'qux'
  1325. } );
  1326. emitter = Object.create( DomEmitterMixin );
  1327. bind = Template.bind( observable, emitter );
  1328. } );
  1329. it( 'does not modify passed definition', () => {
  1330. const def = {
  1331. tag: 'p',
  1332. attributes: {
  1333. a: 'foo',
  1334. }
  1335. };
  1336. const ext = {
  1337. attributes: {
  1338. b: 'bar'
  1339. }
  1340. };
  1341. const tpl = new Template( def );
  1342. Template.extend( tpl, ext );
  1343. expect( ext.attributes.b ).to.equal( 'bar' );
  1344. expect( tpl.attributes.a[ 0 ] ).to.equal( 'foo' );
  1345. expect( tpl.attributes.b[ 0 ] ).to.equal( 'bar' );
  1346. } );
  1347. it( 'logs a warning if an element has already been rendered', () => {
  1348. const spy = testUtils.sinon.stub( log, 'warn' );
  1349. const tpl = new Template( {
  1350. tag: 'p'
  1351. } );
  1352. Template.extend( tpl, {
  1353. attributes: {
  1354. class: 'foo'
  1355. }
  1356. } );
  1357. tpl.render();
  1358. Template.extend( tpl, {
  1359. attributes: {
  1360. class: 'bar'
  1361. }
  1362. } );
  1363. sinon.assert.calledOnce( spy );
  1364. sinon.assert.calledWithExactly( spy, sinon.match( /^template-extend-render/ ) );
  1365. } );
  1366. describe( 'attributes', () => {
  1367. it( 'extends existing - simple', () => {
  1368. extensionTest(
  1369. {
  1370. tag: 'p',
  1371. attributes: {
  1372. a: 'b'
  1373. }
  1374. },
  1375. {
  1376. attributes: {
  1377. a: 'c'
  1378. }
  1379. },
  1380. '<p a="b c"></p>'
  1381. );
  1382. } );
  1383. it( 'extends existing - complex #1', () => {
  1384. extensionTest(
  1385. {
  1386. tag: 'p',
  1387. attributes: {
  1388. a: [ 'b', 'c' ]
  1389. }
  1390. },
  1391. {
  1392. attributes: {
  1393. a: 'd'
  1394. }
  1395. },
  1396. '<p a="b c d"></p>'
  1397. );
  1398. } );
  1399. it( 'extends existing - complex #2', () => {
  1400. extensionTest(
  1401. {
  1402. tag: 'p',
  1403. attributes: {
  1404. a: {
  1405. value: 'b'
  1406. }
  1407. }
  1408. },
  1409. {
  1410. attributes: {
  1411. a: [ 'c', 'd' ]
  1412. }
  1413. },
  1414. '<p a="b c d"></p>'
  1415. );
  1416. } );
  1417. it( 'extends existing - complex #3', () => {
  1418. extensionTest(
  1419. {
  1420. tag: 'p',
  1421. attributes: {
  1422. a: [ 'b' ]
  1423. }
  1424. },
  1425. {
  1426. attributes: {
  1427. a: [ 'c', 'd' ]
  1428. }
  1429. },
  1430. '<p a="b c d"></p>'
  1431. );
  1432. } );
  1433. it( 'extends existing - bindings #1', () => {
  1434. const el = extensionTest(
  1435. {
  1436. tag: 'p',
  1437. attributes: {
  1438. a: bind.to( 'foo' )
  1439. }
  1440. },
  1441. {
  1442. attributes: {
  1443. a: [ 'c', 'd' ]
  1444. }
  1445. },
  1446. '<p a="bar c d"></p>'
  1447. );
  1448. observable.foo = 'baz';
  1449. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p a="baz c d"></p>' );
  1450. } );
  1451. it( 'extends existing - bindings #2', () => {
  1452. const el = extensionTest(
  1453. {
  1454. tag: 'p',
  1455. attributes: {
  1456. a: [ 'b', bind.to( 'foo' ) ]
  1457. }
  1458. },
  1459. {
  1460. attributes: {
  1461. a: [ 'c', bind.to( 'baz' ) ]
  1462. }
  1463. },
  1464. '<p a="b bar c qux"></p>'
  1465. );
  1466. observable.foo = 'abc';
  1467. observable.baz = 'def';
  1468. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p a="b abc c def"></p>' );
  1469. } );
  1470. it( 'creates new - no attributes', () => {
  1471. extensionTest(
  1472. {
  1473. tag: 'p',
  1474. },
  1475. {
  1476. attributes: {
  1477. c: 'd'
  1478. }
  1479. },
  1480. '<p c="d"></p>'
  1481. );
  1482. } );
  1483. it( 'creates new - simple', () => {
  1484. extensionTest(
  1485. {
  1486. tag: 'p',
  1487. attributes: {
  1488. a: 'b'
  1489. }
  1490. },
  1491. {
  1492. attributes: {
  1493. c: 'd'
  1494. }
  1495. },
  1496. '<p a="b" c="d"></p>'
  1497. );
  1498. } );
  1499. it( 'creates new - array', () => {
  1500. extensionTest(
  1501. {
  1502. tag: 'p',
  1503. attributes: {
  1504. a: 'b'
  1505. }
  1506. },
  1507. {
  1508. attributes: {
  1509. c: [ 'd', 'e' ]
  1510. }
  1511. },
  1512. '<p a="b" c="d e"></p>'
  1513. );
  1514. } );
  1515. it( 'creates new - bindings #1', () => {
  1516. const el = extensionTest(
  1517. {
  1518. tag: 'p',
  1519. attributes: {
  1520. a: 'b'
  1521. }
  1522. },
  1523. {
  1524. attributes: {
  1525. c: bind.to( 'foo' )
  1526. }
  1527. },
  1528. '<p a="b" c="bar"></p>'
  1529. );
  1530. observable.foo = 'abc';
  1531. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p a="b" c="abc"></p>' );
  1532. } );
  1533. it( 'creates new - bindings #2', () => {
  1534. const el = extensionTest(
  1535. {
  1536. tag: 'p',
  1537. attributes: {
  1538. a: 'b'
  1539. }
  1540. },
  1541. {
  1542. attributes: {
  1543. c: [ 'd', bind.to( 'foo' ) ]
  1544. }
  1545. },
  1546. '<p a="b" c="d bar"></p>'
  1547. );
  1548. observable.foo = 'abc';
  1549. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p a="b" c="d abc"></p>' );
  1550. } );
  1551. } );
  1552. describe( 'text', () => {
  1553. it( 'extends existing - simple', () => {
  1554. const el = extensionTest(
  1555. {
  1556. tag: 'p',
  1557. children: [
  1558. 'foo'
  1559. ]
  1560. },
  1561. {
  1562. children: [
  1563. 'bar'
  1564. ]
  1565. },
  1566. '<p>foobar</p>'
  1567. );
  1568. expect( el.childNodes ).to.have.length( 1 );
  1569. } );
  1570. it( 'extends existing - complex #1', () => {
  1571. const el = extensionTest(
  1572. {
  1573. tag: 'p',
  1574. children: [
  1575. { text: 'foo' }
  1576. ]
  1577. },
  1578. {
  1579. children: [
  1580. 'bar'
  1581. ]
  1582. },
  1583. '<p>foobar</p>'
  1584. );
  1585. expect( el.childNodes ).to.have.length( 1 );
  1586. } );
  1587. it( 'extends existing - complex #2', () => {
  1588. const el = extensionTest(
  1589. {
  1590. tag: 'p',
  1591. children: [
  1592. { text: 'foo' }
  1593. ]
  1594. },
  1595. {
  1596. children: [
  1597. { text: 'bar' }
  1598. ]
  1599. },
  1600. '<p>foobar</p>'
  1601. );
  1602. expect( el.childNodes ).to.have.length( 1 );
  1603. } );
  1604. it( 'extends existing - bindings #1', () => {
  1605. const el = extensionTest(
  1606. {
  1607. tag: 'p',
  1608. children: [
  1609. { text: bind.to( 'foo' ) }
  1610. ]
  1611. },
  1612. {
  1613. children: [
  1614. 'abc'
  1615. ]
  1616. },
  1617. '<p>bar abc</p>'
  1618. );
  1619. observable.foo = 'asd';
  1620. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>asd abc</p>' );
  1621. expect( el.childNodes ).to.have.length( 1 );
  1622. } );
  1623. it( 'extends existing - bindings #2', () => {
  1624. const el = extensionTest(
  1625. {
  1626. tag: 'p',
  1627. children: [
  1628. 'abc'
  1629. ]
  1630. },
  1631. {
  1632. children: [
  1633. { text: bind.to( 'foo' ) }
  1634. ]
  1635. },
  1636. '<p>abc bar</p>'
  1637. );
  1638. observable.foo = 'asd';
  1639. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc asd</p>' );
  1640. expect( el.childNodes ).to.have.length( 1 );
  1641. } );
  1642. it( 'extends existing - bindings #3', () => {
  1643. const el = extensionTest(
  1644. {
  1645. tag: 'p',
  1646. children: [
  1647. { text: bind.to( 'foo' ) },
  1648. 'X'
  1649. ]
  1650. },
  1651. {
  1652. children: [
  1653. { text: bind.to( 'baz' ) },
  1654. 'Y'
  1655. ]
  1656. },
  1657. '<p>bar quxXY</p>'
  1658. );
  1659. observable.foo = 'A';
  1660. observable.baz = 'B';
  1661. expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>A BXY</p>' );
  1662. expect( el.childNodes ).to.have.length( 2 );
  1663. } );
  1664. } );
  1665. describe( 'children', () => {
  1666. it( 'should throw when the number of children does not correspond', () => {
  1667. expect( () => {
  1668. extensionTest(
  1669. {
  1670. tag: 'p',
  1671. children: [
  1672. 'foo'
  1673. ]
  1674. },
  1675. {
  1676. children: [
  1677. 'foo',
  1678. 'bar'
  1679. ]
  1680. },
  1681. 'it should fail'
  1682. );
  1683. } ).to.throw( CKEditorError, /ui-template-extend-children-mismatch/ );
  1684. } );
  1685. it( 'should throw when no children in target but extending one', () => {
  1686. expect( () => {
  1687. extensionTest(
  1688. {
  1689. tag: 'p',
  1690. },
  1691. {
  1692. children: [
  1693. {
  1694. tag: 'b'
  1695. }
  1696. ]
  1697. },
  1698. 'it should fail'
  1699. );
  1700. } ).to.throw( CKEditorError, /ui-template-extend-children-mismatch/ );
  1701. } );
  1702. it( 'should throw when the number of children does not correspond on some deeper level', () => {
  1703. expect( () => {
  1704. extensionTest(
  1705. {
  1706. tag: 'p',
  1707. children: [
  1708. {
  1709. tag: 'span',
  1710. attributes: {
  1711. class: 'A'
  1712. },
  1713. children: [
  1714. 'A',
  1715. {
  1716. tag: 'span',
  1717. attributes: {
  1718. class: 'AA'
  1719. },
  1720. children: [
  1721. 'AA'
  1722. ]
  1723. }
  1724. ]
  1725. }
  1726. ]
  1727. },
  1728. {
  1729. children: [
  1730. {
  1731. attributes: {
  1732. class: 'B'
  1733. },
  1734. children: [
  1735. 'B'
  1736. ]
  1737. }
  1738. ]
  1739. },
  1740. 'it should fail'
  1741. );
  1742. } ).to.throw( CKEditorError, /ui-template-extend-children-mismatch/ );
  1743. } );
  1744. it( 'extends existing - simple', () => {
  1745. extensionTest(
  1746. {
  1747. tag: 'p',
  1748. children: [
  1749. {
  1750. tag: 'span',
  1751. attributes: {
  1752. class: 'foo'
  1753. }
  1754. }
  1755. ]
  1756. },
  1757. {
  1758. children: [
  1759. {
  1760. tag: 'span',
  1761. attributes: {
  1762. class: 'bar'
  1763. }
  1764. }
  1765. ]
  1766. },
  1767. '<p><span class="foo bar"></span></p>'
  1768. );
  1769. } );
  1770. it( 'extends existing - complex', () => {
  1771. extensionTest(
  1772. {
  1773. tag: 'p',
  1774. children: [
  1775. {
  1776. tag: 'span',
  1777. attributes: {
  1778. class: 'A'
  1779. },
  1780. children: [
  1781. 'A',
  1782. {
  1783. tag: 'span',
  1784. attributes: {
  1785. class: 'AA'
  1786. },
  1787. children: [
  1788. 'AA'
  1789. ]
  1790. }
  1791. ]
  1792. }
  1793. ]
  1794. },
  1795. {
  1796. children: [
  1797. {
  1798. tag: 'span',
  1799. attributes: {
  1800. class: 'B'
  1801. },
  1802. children: [
  1803. 'B',
  1804. {
  1805. tag: 'span',
  1806. attributes: {
  1807. class: 'BB'
  1808. },
  1809. children: [
  1810. 'BB'
  1811. ]
  1812. }
  1813. ]
  1814. }
  1815. ]
  1816. },
  1817. '<p><span class="A B">AB<span class="AA BB">AABB</span></span></p>'
  1818. );
  1819. } );
  1820. it( 'allows extending a particular child', () => {
  1821. const template = new Template( {
  1822. tag: 'p',
  1823. children: [
  1824. {
  1825. tag: 'span',
  1826. attributes: {
  1827. class: 'foo'
  1828. }
  1829. }
  1830. ]
  1831. } );
  1832. Template.extend( template.children.get( 0 ), {
  1833. attributes: {
  1834. class: 'bar'
  1835. }
  1836. } );
  1837. expect( template.render().outerHTML ).to.equal( '<p><span class="foo bar"></span></p>' );
  1838. } );
  1839. it( 'allows extending a particular child – recursively', () => {
  1840. const template = new Template( {
  1841. tag: 'p',
  1842. children: [
  1843. {
  1844. tag: 'span',
  1845. attributes: {
  1846. class: 'A'
  1847. },
  1848. children: [
  1849. 'A',
  1850. {
  1851. tag: 'span',
  1852. attributes: {
  1853. class: 'AA'
  1854. },
  1855. children: [
  1856. 'AA'
  1857. ]
  1858. }
  1859. ]
  1860. }
  1861. ]
  1862. } );
  1863. Template.extend( template.children.get( 0 ), {
  1864. attributes: {
  1865. class: 'B',
  1866. },
  1867. children: [
  1868. 'B',
  1869. {
  1870. attributes: {
  1871. class: 'BB'
  1872. }
  1873. }
  1874. ]
  1875. } );
  1876. expect( template.render().outerHTML ).to.equal( '<p><span class="A B">AB<span class="AA BB">AA</span></span></p>' );
  1877. } );
  1878. } );
  1879. describe( 'listeners', () => {
  1880. it( 'extends existing', () => {
  1881. const spy1 = testUtils.sinon.spy();
  1882. const spy2 = testUtils.sinon.spy();
  1883. const spy3 = testUtils.sinon.spy();
  1884. const spy4 = testUtils.sinon.spy();
  1885. const spy5 = testUtils.sinon.spy();
  1886. observable.on( 'A', spy1 );
  1887. observable.on( 'C', spy2 );
  1888. observable.on( 'B', spy3 );
  1889. observable.on( 'D', spy4 );
  1890. const el = extensionTest(
  1891. {
  1892. tag: 'p',
  1893. children: [
  1894. {
  1895. tag: 'span'
  1896. }
  1897. ],
  1898. on: {
  1899. click: bind.to( 'A' ),
  1900. 'click@span': [
  1901. bind.to( 'B' ),
  1902. bind.to( spy5 )
  1903. ]
  1904. }
  1905. },
  1906. {
  1907. on: {
  1908. click: bind.to( 'C' ),
  1909. 'click@span': bind.to( 'D' )
  1910. }
  1911. },
  1912. '<p><span></span></p>'
  1913. );
  1914. dispatchEvent( el, 'click' );
  1915. expect( spy1.calledOnce ).to.be.true;
  1916. expect( spy2.calledOnce ).to.be.true;
  1917. expect( spy3.called ).to.be.false;
  1918. expect( spy4.called ).to.be.false;
  1919. expect( spy5.called ).to.be.false;
  1920. dispatchEvent( el.firstChild, 'click' );
  1921. expect( spy1.calledTwice ).to.be.true;
  1922. expect( spy2.calledTwice ).to.be.true;
  1923. expect( spy3.calledOnce ).to.be.true;
  1924. expect( spy4.calledOnce ).to.be.true;
  1925. expect( spy5.calledOnce ).to.be.true;
  1926. } );
  1927. it( 'creates new', () => {
  1928. const spy1 = testUtils.sinon.spy();
  1929. const spy2 = testUtils.sinon.spy();
  1930. const spy3 = testUtils.sinon.spy();
  1931. observable.on( 'A', spy1 );
  1932. observable.on( 'B', spy2 );
  1933. const el = extensionTest(
  1934. {
  1935. tag: 'p',
  1936. children: [
  1937. {
  1938. tag: 'span'
  1939. }
  1940. ],
  1941. },
  1942. {
  1943. on: {
  1944. click: bind.to( 'A' ),
  1945. 'click@span': [
  1946. bind.to( 'B' ),
  1947. bind.to( spy3 )
  1948. ]
  1949. }
  1950. },
  1951. '<p><span></span></p>'
  1952. );
  1953. dispatchEvent( el, 'click' );
  1954. expect( spy1.calledOnce ).to.be.true;
  1955. expect( spy2.called ).to.be.false;
  1956. expect( spy3.called ).to.be.false;
  1957. dispatchEvent( el.firstChild, 'click' );
  1958. expect( spy1.calledTwice ).to.be.true;
  1959. expect( spy2.calledOnce ).to.be.true;
  1960. expect( spy3.calledOnce ).to.be.true;
  1961. } );
  1962. } );
  1963. } );
  1964. } );
  1965. function setElement( template ) {
  1966. el = new Template( template ).render();
  1967. document.body.appendChild( el );
  1968. }
  1969. function extensionTest( baseDefinition, extendedDefinition, expectedHtml ) {
  1970. const template = new Template( baseDefinition );
  1971. Template.extend( template, extendedDefinition );
  1972. const el = template.render();
  1973. document.body.appendChild( el );
  1974. expect( normalizeHtml( el.outerHTML ) ).to.equal( expectedHtml );
  1975. return el;
  1976. }
  1977. function dispatchEvent( el, domEvtName ) {
  1978. if ( !el.parentNode ) {
  1979. throw new Error( 'To dispatch an event, element must be in DOM. Otherwise #target is null.' );
  1980. }
  1981. el.dispatchEvent( new Event( domEvtName, {
  1982. bubbles: true
  1983. } ) );
  1984. }
  1985. function getView( def ) {
  1986. const view = new View();
  1987. view.template = new Template( def );
  1988. return view;
  1989. }