template.js 50 KB

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