template.js 37 KB

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