template.js 54 KB

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