8
0

template.js 46 KB

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