template.js 51 KB

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