template.js 42 KB

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