template.js 44 KB

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