8
0

template.js 48 KB

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