template.js 52 KB

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