template.js 47 KB

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