template.js 48 KB

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