template.js 45 KB

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