template.js 53 KB

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