template.js 50 KB

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