| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059 |
- /**
- * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
- /* globals HTMLElement, Event, document */
- import { default as Template, TemplateToBinding, TemplateIfBinding } from '../src/template';
- import View from '../src/view';
- import ViewCollection from '../src/viewcollection';
- import Model from '../src/model';
- import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
- import DomEmitterMixin from '@ckeditor/ckeditor5-utils/src/dom/emittermixin';
- import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
- import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
- let el, text;
- const injectedElements = [];
- describe( 'Template', () => {
- // Clean-up document.body from the rendered elements.
- afterEach( () => {
- sinon.restore();
- for ( const el of injectedElements ) {
- el.remove();
- }
- } );
- describe( 'constructor()', () => {
- it( 'sets #_isRendered property', () => {
- expect( new Template( { tag: 'p' } )._isRendered ).to.be.false;
- } );
- it( 'accepts and normalizes the definition', () => {
- const bind = Template.bind( new Model( {} ), Object.create( DomEmitterMixin ) );
- const childNode = document.createElement( 'div' );
- const childTemplate = new Template( {
- tag: 'b'
- } );
- const tpl = new Template( {
- tag: 'p',
- attributes: {
- a: 'foo',
- b: [ 'bar', 'baz' ],
- c: {
- ns: 'abc',
- value: bind.to( 'qux' )
- }
- },
- children: [
- {
- text: 'content'
- },
- {
- text: bind.to( 'x' )
- },
- 'abc',
- {
- text: [ 'a', 'b' ]
- },
- childNode,
- childTemplate
- ],
- on: {
- 'a@span': bind.to( 'b' ),
- 'b@span': bind.to( () => {} ),
- 'c@span': [
- bind.to( 'c' ),
- bind.to( () => {} )
- ]
- }
- } );
- expect( tpl.attributes.a[ 0 ] ).to.equal( 'foo' );
- expect( tpl.attributes.b[ 0 ] ).to.equal( 'bar' );
- expect( tpl.attributes.b[ 1 ] ).to.equal( 'baz' );
- expect( tpl.attributes.c[ 0 ].value[ 0 ] ).to.be.instanceof( TemplateToBinding );
- expect( tpl.children ).to.have.length( 6 );
- expect( tpl.children[ 0 ].text[ 0 ] ).to.equal( 'content' );
- expect( tpl.children[ 1 ].text[ 0 ] ).to.be.instanceof( TemplateToBinding );
- expect( tpl.children[ 2 ].text[ 0 ] ).to.equal( 'abc' );
- expect( tpl.children[ 3 ].text[ 0 ] ).to.equal( 'a' );
- expect( tpl.children[ 3 ].text[ 1 ] ).to.equal( 'b' );
- expect( tpl.children[ 4 ] ).to.equal( childNode );
- expect( tpl.children[ 5 ] ).to.equal( childTemplate );
- expect( tpl.eventListeners[ 'a@span' ][ 0 ] ).to.be.instanceof( TemplateToBinding );
- expect( tpl.eventListeners[ 'b@span' ][ 0 ] ).to.be.instanceof( TemplateToBinding );
- expect( tpl.eventListeners[ 'c@span' ][ 0 ] ).to.be.instanceof( TemplateToBinding );
- expect( tpl.eventListeners[ 'c@span' ][ 1 ] ).to.be.instanceof( TemplateToBinding );
- // Note that Template mixes EmitterMixin.
- expect( tpl.on ).to.be.a( 'function' );
- expect( tpl.on[ 'a@span' ] ).to.be.undefined;
- } );
- it( 'defines #children collection', () => {
- const elementTpl = new Template( {
- tag: 'p'
- } );
- const textTpl = new Template( {
- text: 'foo'
- } );
- expect( elementTpl.children ).to.be.an( 'array' );
- expect( elementTpl.children ).to.have.length( 0 );
- // Text will never have children.
- expect( textTpl.children ).to.be.undefined;
- } );
- it( 'does not modify passed definition', () => {
- const def = {
- tag: 'p',
- attributes: {
- a: 'foo'
- },
- children: [
- {
- tag: 'span'
- }
- ]
- };
- const tpl = new Template( def );
- expect( def.attributes ).to.not.equal( tpl.attributes );
- expect( def.children ).to.not.equal( tpl.children );
- expect( def.children[ 0 ] ).to.not.equal( tpl.children[ 0 ] );
- expect( def.attributes.a ).to.equal( 'foo' );
- expect( def.children[ 0 ].tag ).to.equal( 'span' );
- expect( tpl.attributes.a[ 0 ] ).to.equal( 'foo' );
- expect( tpl.children[ 0 ].tag ).to.equal( 'span' );
- } );
- } );
- describe( 'render()', () => {
- it( 'throws when the template definition is wrong', () => {
- expectToThrowCKEditorError( () => {
- new Template( {} ).render();
- }, /ui-template-wrong-syntax/ );
- expectToThrowCKEditorError( () => {
- new Template( {
- tag: 'p',
- text: 'foo'
- } ).render();
- }, /ui-template-wrong-syntax/ );
- } );
- it( 'sets #_isRendered true', () => {
- const tpl = new Template( { tag: 'p' } );
- expect( tpl._isRendered ).to.be.false;
- tpl.render();
- expect( tpl._isRendered ).to.be.true;
- } );
- describe( 'DOM Node', () => {
- it( 'creates HTMLElement', () => {
- const el = new Template( {
- tag: 'p'
- } ).render();
- expect( el ).to.be.instanceof( HTMLElement );
- expect( el.parentNode ).to.be.null;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
- expect( el.namespaceURI ).to.equal( 'http://www.w3.org/1999/xhtml' );
- } );
- it( 'creates an element in a custom namespace', () => {
- const el = new Template( {
- tag: 'p',
- ns: 'foo'
- } ).render();
- expect( el.namespaceURI ).to.equal( 'foo' );
- } );
- it( 'creates a Text node', () => {
- const node = new Template( { text: 'foo' } ).render();
- expect( node.nodeType ).to.equal( 3 );
- expect( node.textContent ).to.equal( 'foo' );
- } );
- } );
- describe( 'attributes', () => {
- it( 'renders HTMLElement attributes', () => {
- const el = new Template( {
- tag: 'p',
- attributes: {
- 'class': [ 'a', 'b' ],
- x: 'bar'
- },
- children: [ 'foo' ]
- } ).render();
- expect( el ).to.be.instanceof( HTMLElement );
- expect( el.parentNode ).to.be.null;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="a b" x="bar">foo</p>' );
- expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.be.null;
- } );
- it( 'renders HTMLElement attributes – empty', () => {
- const el = new Template( {
- tag: 'p',
- attributes: {
- class: '',
- x: [ '', '' ]
- },
- children: [ 'foo' ]
- } ).render();
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>foo</p>' );
- } );
- it( 'renders HTMLElement attributes – falsy values', () => {
- const el = new Template( {
- tag: 'p',
- attributes: {
- class: false,
- x: [ '', null, undefined, false ],
- y: [ 'foo', null, undefined, false ]
- },
- children: [ 'foo' ]
- } ).render();
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p y="foo">foo</p>' );
- } );
- it( 'renders HTMLElement attributes in a custom namespace', () => {
- const el = new Template( {
- tag: 'p',
- attributes: {
- class: {
- ns: 'foo',
- value: [ 'a', 'b' ]
- },
- x: {
- ns: 'abc',
- value: 'bar'
- }
- },
- children: [ 'foo' ]
- } ).render();
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="a b" x="bar">foo</p>' );
- expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.equal( 'foo' );
- expect( el.attributes.getNamedItem( 'x' ).namespaceURI ).to.equal( 'abc' );
- } );
- describe( 'style', () => {
- let observable, emitter, bind;
- beforeEach( () => {
- observable = new Model( {
- width: '10px',
- backgroundColor: 'yellow'
- } );
- emitter = Object.create( EmitterMixin );
- bind = Template.bind( observable, emitter );
- } );
- it( 'renders as a static value', () => {
- setElement( {
- tag: 'p',
- attributes: {
- style: 'color: red'
- }
- } );
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="color:red"></p>' );
- } );
- it( 'renders as a static value (Array of values)', () => {
- setElement( {
- tag: 'p',
- attributes: {
- style: {
- color: 'red',
- display: 'block'
- }
- }
- } );
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="color:red;display:block"></p>' );
- } );
- it( 'renders as a value bound to the model', () => {
- setElement( {
- tag: 'p',
- attributes: {
- style: bind.to( 'width', w => `width: ${ w }` )
- }
- } );
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="width:10px"></p>' );
- observable.width = '1em';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="width:1em"></p>' );
- } );
- it( 'renders as a value bound to the model (Array of bindings)', () => {
- setElement( {
- tag: 'p',
- attributes: {
- style: [
- bind.to( 'width', w => `width: ${ w };` ),
- bind.to( 'backgroundColor', c => `background-color: ${ c };` )
- ]
- }
- } );
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="background-color:yellow;width:10px"></p>' );
- observable.width = '1em';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="background-color:yellow;width:1em"></p>' );
- } );
- describe( 'object', () => {
- it( 'renders with static and bound attributes', () => {
- setElement( {
- tag: 'p',
- attributes: {
- style: {
- width: bind.to( 'width' ),
- height: '10px',
- backgroundColor: bind.to( 'backgroundColor' )
- }
- }
- } );
- expect( normalizeHtml( el.outerHTML ) )
- .to.equal( '<p style="background-color:yellow;height:10px;width:10px"></p>' );
- observable.width = '20px';
- observable.backgroundColor = 'green';
- expect( normalizeHtml( el.outerHTML ) )
- .to.equal( '<p style="background-color:green;height:10px;width:20px"></p>' );
- } );
- it( 'renders with empty string attributes', () => {
- setElement( {
- tag: 'p',
- attributes: {
- style: {
- width: '',
- backgroundColor: bind.to( 'backgroundColor' )
- }
- }
- } );
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="background-color:yellow"></p>' );
- observable.backgroundColor = '';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
- } );
- it( 'renders with falsy values', () => {
- setElement( {
- tag: 'p',
- attributes: {
- style: {
- width: null,
- height: false,
- color: undefined
- }
- }
- } );
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
- } );
- } );
- } );
- } );
- describe( 'children', () => {
- it( 'creates HTMLElement children', () => {
- const el = new Template( {
- tag: 'p',
- attributes: {
- a: 'A'
- },
- children: [
- {
- tag: 'b',
- children: [ 'B' ]
- },
- {
- tag: 'i',
- children: [
- 'C',
- {
- tag: 'b',
- children: [ 'D' ]
- }
- ]
- }
- ]
- } ).render();
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p a="A"><b>B</b><i>C<b>D</b></i></p>' );
- } );
- it( 'creates a child Text Node (different syntaxes)', () => {
- const el = new Template( {
- tag: 'p',
- children: [
- 'foo',
- { text: 'bar' }
- ]
- } ).render();
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>foobar</p>' );
- } );
- it( 'creates multiple child Text Nodes', () => {
- const el = new Template( {
- tag: 'p',
- children: [
- 'a',
- 'b',
- { text: 'c' },
- 'd',
- { text: [ 'e', 'f' ] }
- ]
- } ).render();
- expect( el.childNodes ).to.have.length( 5 );
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abcdef</p>' );
- } );
- it( 'renders view children', () => {
- const v1 = getView( {
- tag: 'b',
- attributes: {
- class: [
- 'v1'
- ]
- }
- } );
- const v2 = getView( {
- tag: 'b',
- attributes: {
- class: [
- 'v2'
- ]
- }
- } );
- const v3 = getView( {
- tag: 'b',
- attributes: {
- class: [
- 'v3'
- ]
- }
- } );
- v3.render();
- const tpl = new Template( {
- tag: 'p',
- children: [ v1, v2, v3 ]
- } );
- expect( tpl.children[ 0 ] ).to.equal( v1 );
- expect( tpl.children[ 1 ] ).to.equal( v2 );
- expect( tpl.children[ 2 ] ).to.equal( v3 );
- const rendered = tpl.render();
- expect( normalizeHtml( rendered.outerHTML ) )
- .to.equal( '<p><b class="v1"></b><b class="v2"></b><b class="v3"></b></p>' );
- // Make sure the child views will not re–render their elements but
- // use ones rendered by the template instance above.
- expect( v1.element ).to.equal( rendered.firstChild );
- expect( v2.element ).to.equal( rendered.children[ 1 ] );
- expect( v3.element ).to.equal( rendered.lastChild );
- } );
- it( 'renders view collection', () => {
- const collection = new ViewCollection();
- const v1 = getView( {
- tag: 'span',
- attributes: {
- class: [
- 'v1'
- ]
- }
- } );
- const v2 = getView( {
- tag: 'span',
- attributes: {
- class: [
- 'v2'
- ]
- }
- } );
- collection.add( v1 );
- collection.add( v2 );
- const rendered = new Template( {
- tag: 'p',
- children: collection
- } ).render();
- expect( normalizeHtml( rendered.outerHTML ) ).to.equal( '<p><span class="v1"></span><span class="v2"></span></p>' );
- // Make sure the child views will not re–render their elements but
- // use ones rendered by the template instance above.
- expect( v1.element ).to.equal( rendered.firstChild );
- expect( v2.element ).to.equal( rendered.lastChild );
- expect( collection._parentElement ).to.equal( rendered );
- } );
- it( 'renders DOM nodes', () => {
- const view = new View();
- view.set( {
- foo: 'bar',
- bar: 'baz'
- } );
- const bind = Template.bind( view, view );
- const childA = new Template( {
- tag: 'b',
- attributes: {
- class: bind.to( 'foo' )
- }
- } ).render();
- const childB = new Template( {
- text: bind.to( 'bar' )
- } ).render();
- const rendered = new Template( {
- tag: 'p',
- children: [
- childA,
- childB
- ]
- } ).render();
- expect( normalizeHtml( rendered.outerHTML ) ).to.equal( '<p><b class="bar"></b>baz</p>' );
- } );
- // #117
- it( 'renders template children', () => {
- const childTplA = new Template( {
- tag: 'a'
- } );
- const childTplB = new Template( {
- tag: 'b'
- } );
- const view = new View();
- view.set( {
- foo: 'bar',
- bar: 'foo'
- } );
- const bind = Template.bind( view, view );
- // Yes, this TC is crazy in some sort of way. It's all about template
- // normalization and deep cloning and the like.
- //
- // To **really** prove the code is safe there must be a View instance,
- // which has some child bound to its attribute and... there must be a
- // Template instance (below), which also has a child bound to the same attribute.
- //
- // I know, the view instance and its template aren't even rendered.
- //
- // The truth is that madness behind this test case is so deep there are no
- // words to explain it. But what actually matters is that it proves the Template
- // class is free of "Maximum call stack size exceeded" error in certain
- // situations.
- view.setTemplate( {
- tag: 'span',
- children: [
- {
- text: bind.to( 'bar' )
- }
- ]
- } );
- const childTplC = new Template( {
- tag: 'i',
- children: [
- {
- text: bind.to( 'bar' )
- }
- ]
- } );
- const tpl = new Template( {
- tag: 'p',
- children: [
- childTplA,
- childTplB,
- childTplC
- ]
- } );
- // Make sure child instances weren't cloned.
- expect( tpl.children[ 0 ] ).to.equal( childTplA );
- expect( tpl.children[ 1 ] ).to.equal( childTplB );
- expect( tpl.children[ 2 ] ).to.equal( childTplC );
- expect( normalizeHtml( tpl.render().outerHTML ) ).to.equal(
- '<p><a></a><b></b><i>foo</i></p>'
- );
- } );
- // https://github.com/ckeditor/ckeditor5-ui/issues/289
- it( 'does not throw when child does not have an "id" property', () => {
- const strongView = getView( {
- tag: 'strong'
- } );
- strongView.set( 'id' );
- expect( () => {
- getView( {
- tag: 'div',
- children: [
- strongView
- ]
- } );
- } ).to.not.throw();
- } );
- } );
- describe( 'bindings', () => {
- it( 'activates model bindings – root', () => {
- const observable = new Model( {
- foo: 'bar'
- } );
- const emitter = Object.create( EmitterMixin );
- const bind = Template.bind( observable, emitter );
- const el = new Template( {
- tag: 'div',
- attributes: {
- class: bind.to( 'foo' )
- }
- } ).render();
- expect( el.getAttribute( 'class' ) ).to.equal( 'bar' );
- observable.foo = 'baz';
- expect( el.getAttribute( 'class' ) ).to.equal( 'baz' );
- } );
- it( 'activates model bindings – children', () => {
- const observable = new Model( {
- foo: 'bar'
- } );
- const emitter = Object.create( EmitterMixin );
- const bind = Template.bind( observable, emitter );
- const el = new Template( {
- tag: 'div',
- children: [
- {
- tag: 'span',
- children: [
- {
- text: [
- bind.to( 'foo' ),
- 'static'
- ]
- }
- ]
- }
- ]
- } ).render();
- expect( el.firstChild.textContent ).to.equal( 'bar static' );
- observable.foo = 'baz';
- expect( el.firstChild.textContent ).to.equal( 'baz static' );
- } );
- } );
- } );
- describe( 'apply()', () => {
- let observable, domEmitter, bind;
- beforeEach( () => {
- setElement( { tag: 'div' } );
- text = document.createTextNode( '' );
- observable = new Model( {
- foo: 'bar',
- baz: 'qux'
- } );
- domEmitter = Object.create( DomEmitterMixin );
- bind = Template.bind( observable, domEmitter );
- } );
- it( 'throws when wrong template definition', () => {
- expectToThrowCKEditorError( () => {
- new Template( {
- tag: 'p',
- text: 'foo'
- } ).apply( el );
- }, /ui-template-wrong-syntax/ );
- } );
- it( 'accepts empty template definition', () => {
- new Template( {} ).apply( el );
- new Template( {} ).apply( text );
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div></div>' );
- expect( text.textContent ).to.equal( '' );
- } );
- describe( 'text', () => {
- it( 'applies textContent to a Text Node', () => {
- new Template( {
- text: 'abc'
- } ).apply( text );
- expect( text.textContent ).to.equal( 'abc' );
- } );
- it( 'overrides existing textContent of a Text Node', () => {
- text.textContent = 'foo';
- new Template( {
- text: bind.to( 'foo' )
- } ).apply( text );
- expect( text.textContent ).to.equal( 'bar' );
- observable.foo = 'qux';
- expect( text.textContent ).to.equal( 'qux' );
- } );
- it( 'overrides textContent of an existing Text Node in a HTMLElement', () => {
- el.textContent = 'bar';
- new Template( {
- tag: 'div',
- children: [ 'foo' ]
- } ).apply( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div>foo</div>' );
- } );
- } );
- describe( 'attributes', () => {
- it( 'applies attributes to an HTMLElement', () => {
- new Template( {
- tag: 'div',
- attributes: {
- 'class': [ 'a', 'b' ],
- x: 'bar'
- }
- } ).apply( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div class="a b" x="bar"></div>' );
- } );
- it( 'manages existing attribute values ("class" vs. "non–class")', () => {
- el.setAttribute( 'class', 'default' );
- el.setAttribute( 'x', 'foo' );
- new Template( {
- tag: 'div',
- attributes: {
- 'class': [ 'a', 'b' ],
- x: 'bar'
- }
- } ).apply( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<div class="a b default" x="bar"></div>'
- );
- } );
- it( 'applies attributes and TextContent to a DOM tree', () => {
- el.textContent = 'abc';
- el.appendChild( getElement( { tag: 'span' } ) );
- new Template( {
- tag: 'div',
- attributes: {
- 'class': 'parent'
- },
- children: [
- 'Children:',
- {
- tag: 'span',
- attributes: {
- class: 'child'
- }
- }
- ]
- } ).apply( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div class="parent">Children:<span class="child"></span></div>' );
- } );
- describe( 'style', () => {
- beforeEach( () => {
- observable = new Model( {
- width: '10px',
- backgroundColor: 'yellow'
- } );
- bind = Template.bind( observable, domEmitter );
- } );
- it( 'applies as a static value', () => {
- setElement( {
- tag: 'p',
- attributes: {
- style: 'color: red;'
- }
- } );
- new Template( {
- attributes: {
- style: 'display: block'
- }
- } ).apply( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p style="color:red;display:block"></p>' );
- } );
- it( 'applies as a static value (Array of values)', () => {
- setElement( {
- tag: 'p',
- attributes: {
- style: [ 'color: red;', 'display: block;' ]
- }
- } );
- new Template( {
- attributes: {
- style: [ 'float: left;', 'overflow: hidden;' ]
- }
- } ).apply( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<p style="color:red;display:block;float:left;overflow:hidden"></p>'
- );
- } );
- it( 'applies when in an object syntax', () => {
- setElement( {
- tag: 'p',
- attributes: {
- style: {
- width: '20px'
- }
- }
- } );
- new Template( {
- attributes: {
- style: {
- height: '10px',
- float: 'left',
- backgroundColor: 'green'
- }
- }
- } ).apply( el );
- expect( normalizeHtml( el.outerHTML ) )
- .to.equal( '<p style="background-color:green;float:left;height:10px;width:20px"></p>' );
- } );
- it( 'applies when bound to observable', () => {
- setElement( {
- tag: 'p',
- attributes: {
- style: {
- left: '20px'
- }
- }
- } );
- new Template( {
- attributes: {
- style: {
- width: bind.to( 'width' ),
- float: 'left',
- backgroundColor: 'green'
- }
- }
- } ).apply( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<p style="background-color:green;float:left;left:20px;width:10px"></p>'
- );
- observable.width = '100px';
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<p style="background-color:green;float:left;left:20px;width:100px"></p>'
- );
- } );
- } );
- } );
- describe( 'children', () => {
- it( 'doesn\'t apply new child to an HTMLElement – Text Node', () => {
- new Template( {
- tag: 'div',
- children: [ 'foo' ]
- } ).apply( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div></div>' );
- } );
- it( 'doesn\'t apply new child to an HTMLElement – HTMLElement', () => {
- new Template( {
- tag: 'div',
- children: [
- {
- tag: 'span'
- }
- ]
- } ).apply( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div></div>' );
- } );
- it( 'doesn\'t apply new child to an HTMLElement – view', () => {
- const view = getView( {
- tag: 'span',
- attributes: {
- class: [
- 'v1'
- ]
- }
- } );
- new Template( {
- tag: 'p',
- children: [ view ]
- } ).apply( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div></div>' );
- } );
- it( 'doesn\'t apply new child to an HTMLElement – view collection', () => {
- const collection = new ViewCollection();
- collection.add( getView( {
- tag: 'span',
- attributes: {
- class: [
- 'v1'
- ]
- }
- } ) );
- new Template( {
- tag: 'p',
- children: collection
- } ).apply( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div></div>' );
- expect( collection._parentElement ).to.be.null;
- } );
- it( 'should work for deep DOM structure with bindings and event listeners', () => {
- const childA = getElement( {
- tag: 'a',
- attributes: {
- class: 'a1 a2'
- },
- children: [
- 'a'
- ]
- } );
- const childB = getElement( {
- tag: 'b',
- attributes: {
- class: 'b1 b2'
- },
- children: [
- 'b'
- ]
- } );
- el.appendChild( childA );
- el.appendChild( childB );
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<div><a class="a1 a2">a</a><b class="b1 b2">b</b></div>'
- );
- const spy1 = sinon.spy();
- const spy2 = sinon.spy();
- const spy3 = sinon.spy();
- observable.on( 'ku', spy1 );
- observable.on( 'kd', spy2 );
- observable.on( 'mo', spy3 );
- new Template( {
- tag: 'div',
- children: [
- {
- tag: 'a',
- on: {
- keyup: bind.to( 'ku' )
- },
- attributes: {
- class: bind.to( 'foo', val => 'applied-A-' + val ),
- id: 'applied-A'
- },
- children: [ 'applied-a' ]
- },
- {
- tag: 'b',
- on: {
- keydown: bind.to( 'kd' )
- },
- attributes: {
- class: bind.to( 'baz', val => 'applied-B-' + val ),
- id: 'applied-B'
- },
- children: [ 'applied-b' ]
- },
- 'Text which is not to be applied because it does NOT exist in original element.'
- ],
- on: {
- 'mouseover@a': bind.to( 'mo' )
- },
- attributes: {
- id: bind.to( 'foo', val => val.toUpperCase() ),
- class: bind.to( 'baz', val => 'applied-parent-' + val )
- }
- } ).apply( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div class="applied-parent-qux" id="BAR">' +
- '<a class="a1 a2 applied-A-bar" id="applied-A">applied-a</a>' +
- '<b class="applied-B-qux b1 b2" id="applied-B">applied-b</b>' +
- '</div>' );
- observable.foo = 'updated';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<div class="applied-parent-qux" id="UPDATED">' +
- '<a class="a1 a2 applied-A-updated" id="applied-A">applied-a</a>' +
- '<b class="applied-B-qux b1 b2" id="applied-B">applied-b</b>' +
- '</div>' );
- document.body.appendChild( el );
- // Test "mouseover@a".
- dispatchEvent( el, 'mouseover' );
- dispatchEvent( childA, 'mouseover' );
- // Test "keyup".
- dispatchEvent( childA, 'keyup' );
- // Test "keydown".
- dispatchEvent( childB, 'keydown' );
- sinon.assert.calledOnce( spy1 );
- sinon.assert.calledOnce( spy2 );
- sinon.assert.calledOnce( spy3 );
- } );
- } );
- } );
- describe( 'revert()', () => {
- let observable, domEmitter, bind;
- beforeEach( () => {
- el = getElement( { tag: 'div' } );
- observable = new Model( {
- foo: 'bar',
- baz: 'qux'
- } );
- domEmitter = Object.create( DomEmitterMixin );
- bind = Template.bind( observable, domEmitter );
- } );
- it( 'should throw if template is not applied', () => {
- const tpl = new Template( {
- tag: 'div'
- } );
- expectToThrowCKEditorError( () => {
- tpl.revert( el );
- }, /ui-template-revert-not-applied/ );
- tpl.render();
- expectToThrowCKEditorError( () => {
- tpl.revert( el );
- }, /ui-template-revert-not-applied/ );
- } );
- describe( 'text', () => {
- it( 'should revert textContent to the initial value', () => {
- el = getElement( {
- tag: 'a',
- children: [
- 'a',
- {
- tag: 'b',
- children: [
- 'b'
- ]
- }
- ]
- } );
- const tpl = new Template( {
- children: [
- 'bar',
- {
- children: [
- 'qux'
- ]
- }
- ]
- } );
- tpl.apply( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<a>bar<b>qux</b></a>'
- );
- tpl.revert( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<a>a<b>b</b></a>'
- );
- } );
- it( 'should remove bindings', () => {
- el = getElement( {
- tag: 'a',
- children: [
- 'a',
- {
- tag: 'b',
- children: [
- 'b'
- ]
- }
- ]
- } );
- const tpl = new Template( {
- children: [
- 'foo',
- {
- children: [
- {
- text: bind.to( 'foo' )
- }
- ]
- }
- ]
- } );
- tpl.apply( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<a>foo<b>bar</b></a>'
- );
- observable.foo = 'abc';
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<a>foo<b>abc</b></a>'
- );
- tpl.revert( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<a>a<b>b</b></a>'
- );
- observable.foo = 'xyz';
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<a>a<b>b</b></a>'
- );
- } );
- } );
- describe( 'attributes', () => {
- it( 'should revert attributes to the initial values', () => {
- el = getElement( {
- tag: 'a',
- attributes: {
- foo: 'af',
- bar: 'ab'
- },
- children: [
- {
- tag: 'b',
- attributes: {
- foo: 'bf',
- bar: 'bb'
- }
- }
- ]
- } );
- const tpl = new Template( {
- attributes: {
- foo: 'af1',
- bar: [ 'ab1', 'ab2' ],
- baz: 'x'
- },
- children: [
- {
- attributes: {
- foo: 'bf1'
- }
- }
- ]
- } );
- tpl.apply( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<a bar="ab1 ab2" baz="x" foo="af1">' +
- '<b bar="bb" foo="bf1"></b>' +
- '</a>'
- );
- tpl.revert( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<a bar="ab" foo="af">' +
- '<b bar="bb" foo="bf"></b>' +
- '</a>'
- );
- } );
- it( 'should remove bindings', () => {
- el = getElement( {
- tag: 'a',
- attributes: {
- foo: 'af',
- bar: 'ab'
- },
- children: [
- {
- tag: 'b',
- attributes: {
- foo: 'bf',
- bar: 'bb'
- }
- }
- ]
- } );
- const tpl = new Template( {
- attributes: {
- foo: 'af1',
- bar: [
- 'ab1',
- bind.to( 'baz' )
- ]
- },
- children: [
- {
- attributes: {
- foo: bind.to( 'foo' )
- }
- }
- ]
- } );
- tpl.apply( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<a bar="ab1 qux" foo="af1">' +
- '<b bar="bb" foo="bar"></b>' +
- '</a>'
- );
- observable.foo = 'x';
- observable.baz = 'y';
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<a bar="ab1 y" foo="af1">' +
- '<b bar="bb" foo="x"></b>' +
- '</a>'
- );
- tpl.revert( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<a bar="ab" foo="af">' +
- '<b bar="bb" foo="bf"></b>' +
- '</a>'
- );
- observable.foo = 'abc';
- observable.baz = 'cba';
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<a bar="ab" foo="af">' +
- '<b bar="bb" foo="bf"></b>' +
- '</a>'
- );
- } );
- describe( 'style', () => {
- beforeEach( () => {
- observable = new Model( {
- overflow: 'visible'
- } );
- bind = Template.bind( observable, domEmitter );
- } );
- it( 'should remove bindings', () => {
- el = getElement( {
- tag: 'a',
- attributes: {
- style: {
- fontWeight: 'bold'
- }
- },
- children: [
- {
- tag: 'b',
- attributes: {
- style: {
- color: 'red'
- }
- }
- }
- ]
- } );
- const tpl = new Template( {
- attributes: {
- style: {
- overflow: bind.to( 'overflow' )
- }
- },
- children: [
- {
- tag: 'b',
- attributes: {
- style: {
- display: 'block'
- }
- }
- }
- ]
- } );
- tpl.apply( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<a style="font-weight:bold;overflow:visible">' +
- '<b style="color:red;display:block"></b>' +
- '</a>'
- );
- tpl.revert( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<a style="font-weight:bold">' +
- '<b style="color:red"></b>' +
- '</a>'
- );
- observable.overflow = 'hidden';
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<a style="font-weight:bold">' +
- '<b style="color:red"></b>' +
- '</a>'
- );
- } );
- } );
- } );
- describe( 'children', () => {
- it( 'should work for deep DOM structure with bindings and event listeners', () => {
- el = getElement( {
- tag: 'div',
- children: [
- {
- tag: 'a',
- attributes: {
- class: [ 'a1', 'a2' ]
- },
- children: [
- 'a'
- ]
- },
- {
- tag: 'b',
- attributes: {
- class: [ 'b1', 'b2' ]
- },
- children: [
- 'b'
- ]
- }
- ]
- } );
- const spy = sinon.spy();
- observable.on( 'ku', spy );
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<div><a class="a1 a2">a</a><b class="b1 b2">b</b></div>'
- );
- const tpl = new Template( {
- tag: 'div',
- attributes: {
- class: [ 'div1', 'div2' ],
- style: {
- fontWeight: 'bold'
- }
- },
- children: [
- {
- tag: 'a',
- attributes: {
- class: [ 'x', 'y' ],
- 'data-new-attr': 'foo'
- },
- children: [ 'applied-a' ]
- },
- {
- tag: 'b',
- attributes: {
- class: [
- 'a',
- 'b',
- bind.to( 'foo' )
- ]
- },
- children: [ 'applied-b' ]
- }
- ],
- on: {
- keyup: bind.to( 'ku' )
- }
- } );
- tpl.apply( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<div class="div1 div2" style="font-weight:bold">' +
- '<a class="a1 a2 x y" data-new-attr="foo">applied-a</a>' +
- '<b class="a b b1 b2 bar">applied-b</b>' +
- '</div>'
- );
- observable.foo = 'baz';
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<div class="div1 div2" style="font-weight:bold">' +
- '<a class="a1 a2 x y" data-new-attr="foo">applied-a</a>' +
- '<b class="a b b1 b2 baz">applied-b</b>' +
- '</div>'
- );
- dispatchEvent( el.firstChild, 'keyup' );
- sinon.assert.calledOnce( spy );
- tpl.revert( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<div><a class="a1 a2">a</a><b class="b1 b2">b</b></div>'
- );
- observable.foo = 'qux';
- expect( normalizeHtml( el.outerHTML ) ).to.equal(
- '<div><a class="a1 a2">a</a><b class="b1 b2">b</b></div>'
- );
- dispatchEvent( el.firstChild, 'keyup' );
- sinon.assert.calledOnce( spy );
- } );
- } );
- } );
- describe( 'getViews()', () => {
- it( 'returns iterator', () => {
- const template = new Template( {} );
- expect( template.getViews().next ).to.be.a( 'function' );
- expect( Array.from( template.getViews() ) ).to.have.length( 0 );
- } );
- it( 'returns all child views', () => {
- const viewA = new View();
- const viewB = new View();
- const viewC = new View();
- const template = new Template( {
- tag: 'div',
- children: [
- viewA,
- {
- tag: 'div',
- children: [
- viewB
- ]
- },
- viewC
- ]
- } );
- expect( Array.from( template.getViews() ) ).to.have.members( [ viewA, viewB, viewC ] );
- } );
- // https://github.com/ckeditor/ckeditor5-ui/issues/337
- it( 'does not traverse non–Template children', () => {
- const viewA = new View();
- const viewB = new View();
- const viewC = new View();
- const template = new Template( {
- tag: 'div',
- children: [
- viewA
- ]
- } );
- // Technically, this kind of child is invalid but the aim of this test is to
- // check if the generator will accidentally traverse non–Template objects
- // like native HTML elements, which also have "children" property. It could happen
- // because it is possible to pass HTML elements directly to the templateDefinition.
- template.children.push( {
- children: [ viewB ]
- } );
- template.children.push( viewC );
- expect( Array.from( template.getViews() ) ).to.have.members( [ viewA, viewC ] );
- } );
- } );
- describe( 'bind()', () => {
- it( 'returns object', () => {
- expect( Template.bind() ).to.be.an( 'object' );
- } );
- it( 'provides "to" and "if" interface', () => {
- const bind = Template.bind();
- expect( bind ).to.have.keys( 'to', 'if' );
- expect( bind.to ).to.be.a( 'function' );
- expect( bind.if ).to.be.a( 'function' );
- } );
- describe( 'event', () => {
- let observable, domEmitter, bind;
- beforeEach( () => {
- observable = new Model( {
- foo: 'bar',
- baz: 'qux'
- } );
- domEmitter = Object.create( DomEmitterMixin );
- bind = Template.bind( observable, domEmitter );
- } );
- it( 'accepts plain binding', () => {
- const spy = sinon.spy();
- setElement( {
- tag: 'p',
- on: {
- x: bind.to( 'a' )
- }
- } );
- observable.on( 'a', spy );
- dispatchEvent( el, 'x' );
- sinon.assert.calledWithExactly( spy,
- sinon.match.has( 'name', 'a' ),
- sinon.match.has( 'target', el )
- );
- } );
- it( 'accepts an array of event bindings', () => {
- const spy1 = sinon.spy();
- const spy2 = sinon.spy();
- setElement( {
- tag: 'p',
- on: {
- x: [
- bind.to( 'a' ),
- bind.to( 'b' )
- ]
- }
- } );
- observable.on( 'a', spy1 );
- observable.on( 'b', spy2 );
- dispatchEvent( el, 'x' );
- sinon.assert.calledWithExactly( spy1,
- sinon.match.has( 'name', 'a' ),
- sinon.match.has( 'target', el )
- );
- sinon.assert.calledWithExactly( spy2,
- sinon.match.has( 'name', 'b' ),
- sinon.match.has( 'target', el )
- );
- } );
- it( 'accepts DOM selectors', () => {
- const spy1 = sinon.spy();
- const spy2 = sinon.spy();
- const spy3 = sinon.spy();
- setElement( {
- tag: 'p',
- children: [
- {
- tag: 'span',
- attributes: {
- 'class': 'y'
- },
- on: {
- 'test@p': bind.to( 'c' )
- }
- },
- {
- tag: 'div',
- children: [
- {
- tag: 'span',
- attributes: {
- 'class': 'y'
- }
- }
- ]
- }
- ],
- on: {
- 'test@.y': bind.to( 'a' ),
- 'test@div': bind.to( 'b' )
- }
- } );
- observable.on( 'a', spy1 );
- observable.on( 'b', spy2 );
- observable.on( 'c', spy3 );
- // Test "test@p".
- dispatchEvent( el, 'test' );
- sinon.assert.callCount( spy1, 0 );
- sinon.assert.callCount( spy2, 0 );
- sinon.assert.callCount( spy3, 0 );
- // Test "test@.y".
- dispatchEvent( el.firstChild, 'test' );
- expect( spy1.firstCall.calledWithExactly(
- sinon.match.has( 'name', 'a' ),
- sinon.match.has( 'target', el.firstChild )
- ) ).to.be.true;
- sinon.assert.callCount( spy2, 0 );
- sinon.assert.callCount( spy3, 0 );
- // Test "test@div".
- dispatchEvent( el.lastChild, 'test' );
- sinon.assert.callCount( spy1, 1 );
- expect( spy2.firstCall.calledWithExactly(
- sinon.match.has( 'name', 'b' ),
- sinon.match.has( 'target', el.lastChild )
- ) ).to.be.true;
- sinon.assert.callCount( spy3, 0 );
- // Test "test@.y".
- dispatchEvent( el.lastChild.firstChild, 'test' );
- expect( spy1.secondCall.calledWithExactly(
- sinon.match.has( 'name', 'a' ),
- sinon.match.has( 'target', el.lastChild.firstChild )
- ) ).to.be.true;
- sinon.assert.callCount( spy2, 1 );
- sinon.assert.callCount( spy3, 0 );
- } );
- it( 'accepts function callbacks', () => {
- const spy1 = sinon.spy();
- const spy2 = sinon.spy();
- setElement( {
- tag: 'p',
- children: [
- {
- tag: 'span'
- }
- ],
- on: {
- x: bind.to( spy1 ),
- 'y@span': [
- bind.to( spy2 ),
- bind.to( 'c' )
- ]
- }
- } );
- dispatchEvent( el, 'x' );
- dispatchEvent( el.firstChild, 'y' );
- sinon.assert.calledWithExactly( spy1,
- sinon.match.has( 'target', el )
- );
- sinon.assert.calledWithExactly( spy2,
- sinon.match.has( 'target', el.firstChild )
- );
- } );
- it( 'supports event delegation', () => {
- const spy = sinon.spy();
- setElement( {
- tag: 'p',
- children: [
- {
- tag: 'span'
- }
- ],
- on: {
- x: bind.to( 'a' )
- }
- } );
- observable.on( 'a', spy );
- dispatchEvent( el.firstChild, 'x' );
- sinon.assert.calledWithExactly( spy,
- sinon.match.has( 'name', 'a' ),
- sinon.match.has( 'target', el.firstChild )
- );
- } );
- it( 'works for future elements', () => {
- const spy = sinon.spy();
- setElement( {
- tag: 'p',
- on: {
- 'test@div': bind.to( 'a' )
- }
- } );
- observable.on( 'a', spy );
- const div = getElement( { tag: 'div' } );
- el.appendChild( div );
- dispatchEvent( div, 'test' );
- sinon.assert.calledWithExactly( spy, sinon.match.has( 'name', 'a' ), sinon.match.has( 'target', div ) );
- } );
- } );
- describe( 'model', () => {
- let observable, emitter, bind;
- beforeEach( () => {
- observable = new Model( {
- foo: 'bar',
- baz: 'qux'
- } );
- emitter = Object.create( EmitterMixin );
- bind = Template.bind( observable, emitter );
- } );
- describe( 'to', () => {
- it( 'returns an instance of TemplateToBinding', () => {
- const spy = sinon.spy();
- const binding = bind.to( 'foo', spy );
- expect( binding ).to.be.instanceof( TemplateToBinding );
- expect( spy.called ).to.be.false;
- expect( binding ).to.have.keys( [ 'observable', 'eventNameOrFunction', 'emitter', 'attribute', 'callback' ] );
- expect( binding.observable ).to.equal( observable );
- expect( binding.callback ).to.equal( spy );
- expect( binding.attribute ).to.equal( 'foo' );
- } );
- it( 'allows binding attribute to the observable – simple (HTMLElement attribute)', () => {
- setElement( {
- tag: 'p',
- attributes: {
- 'class': bind.to( 'foo' )
- },
- children: [ 'abc' ]
- } );
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar">abc</p>' );
- observable.foo = 'baz';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="baz">abc</p>' );
- expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.be.null;
- } );
- it( 'allows binding attribute to the observable – simple (Text Node)', () => {
- setElement( {
- tag: 'p',
- children: [
- {
- text: bind.to( 'foo' )
- }
- ]
- } );
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>bar</p>' );
- observable.foo = 'baz';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>baz</p>' );
- } );
- it( 'allows binding attribute to the observable – value processing', () => {
- const callback = value => value > 0 ? 'positive' : 'negative';
- setElement( {
- tag: 'p',
- attributes: {
- 'class': bind.to( 'foo', callback )
- },
- children: [
- {
- text: bind.to( 'foo', callback )
- }
- ]
- } );
- observable.foo = 3;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="positive">positive</p>' );
- observable.foo = -7;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="negative">negative</p>' );
- } );
- it( 'allows binding attribute to the observable – value processing (use Node)', () => {
- const callback = ( value, node ) => {
- return ( !!node.tagName && value > 0 ) ? 'HTMLElement positive' : '';
- };
- setElement( {
- tag: 'p',
- attributes: {
- 'class': bind.to( 'foo', callback )
- },
- children: [
- {
- text: bind.to( 'foo', callback )
- }
- ]
- } );
- observable.foo = 3;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="HTMLElement positive"></p>' );
- observable.foo = -7;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
- } );
- it( 'allows binding attribute to the observable – custom callback', () => {
- setElement( {
- tag: 'p',
- attributes: {
- 'class': bind.to( 'foo', ( value, el ) => {
- el.innerHTML = value;
- if ( value == 'changed' ) {
- return value;
- }
- } )
- }
- } );
- observable.foo = 'moo';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>moo</p>' );
- observable.foo = 'changed';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="changed">changed</p>' );
- } );
- it( 'allows binding attribute to the observable – array of bindings (HTMLElement attribute)', () => {
- setElement( {
- tag: 'p',
- attributes: {
- 'class': [
- 'ck-class',
- bind.to( 'foo' ),
- bind.to( 'baz' ),
- bind.to( 'foo', value => `foo-is-${ value }` ),
- 'ck-end'
- ]
- },
- children: [ 'abc' ]
- } );
- observable.foo = 'a';
- observable.baz = 'b';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="a b ck-class ck-end foo-is-a">abc</p>' );
- observable.foo = 'c';
- observable.baz = 'd';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="c ck-class ck-end d foo-is-c">abc</p>' );
- } );
- it( 'allows binding attribute to the observable – array of bindings (Text Node)', () => {
- setElement( {
- tag: 'p',
- attributes: {
- },
- children: [
- {
- text: [
- 'ck-class',
- bind.to( 'foo' ),
- bind.to( 'baz' ),
- bind.to( 'foo', value => `foo-is-${ value }` ),
- 'ck-end'
- ]
- }
- ]
- } );
- observable.foo = 'a';
- observable.baz = 'b';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>ck-class a b foo-is-a ck-end</p>' );
- observable.foo = 'c';
- observable.baz = 'd';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>ck-class c d foo-is-c ck-end</p>' );
- } );
- it( 'allows binding attribute to the observable – falsy values', () => {
- setElement( {
- tag: 'p',
- attributes: {
- simple: bind.to( 'foo' ),
- complex: [ null, bind.to( 'foo' ), undefined, false ],
- zero: [ 0, bind.to( 'foo' ) ],
- emptystring: [ '', bind.to( 'foo' ) ]
- },
- children: [ 'abc' ]
- } );
- observable.foo = 'bar';
- expect( normalizeHtml( el.outerHTML ) )
- .to.equal( '<p complex="bar" emptystring="bar" simple="bar" zero="0 bar">abc</p>' );
- observable.foo = 0;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p complex="0" emptystring="0" simple="0" zero="0 0">abc</p>' );
- observable.foo = false;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p zero="0">abc</p>' );
- observable.foo = null;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p zero="0">abc</p>' );
- observable.foo = undefined;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p zero="0">abc</p>' );
- observable.foo = '';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p zero="0">abc</p>' );
- } );
- it( 'allows binding attribute to the observable – a custom namespace', () => {
- setElement( {
- tag: 'p',
- attributes: {
- class: {
- ns: 'foo',
- value: bind.to( 'foo' )
- },
- custom: {
- ns: 'foo',
- value: [
- bind.to( 'foo' ),
- bind.to( 'baz' )
- ]
- }
- },
- children: [ 'abc' ]
- } );
- observable.foo = 'bar';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar" custom="bar qux">abc</p>' );
- expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.equal( 'foo' );
- observable.foo = 'baz';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="baz" custom="baz qux">abc</p>' );
- expect( el.attributes.getNamedItem( 'class' ).namespaceURI ).to.equal( 'foo' );
- } );
- } );
- describe( 'if', () => {
- it( 'returns an object which describes the binding', () => {
- const spy = sinon.spy();
- const binding = bind.if( 'foo', 'whenTrue', spy );
- expect( binding ).to.be.instanceof( TemplateIfBinding );
- expect( spy.called ).to.be.false;
- expect( binding ).to.have.keys( [ 'observable', 'emitter', 'attribute', 'callback', 'valueIfTrue' ] );
- expect( binding.observable ).to.equal( observable );
- expect( binding.callback ).to.equal( spy );
- expect( binding.attribute ).to.equal( 'foo' );
- expect( binding.valueIfTrue ).to.equal( 'whenTrue' );
- } );
- it( 'allows binding attribute to the observable – presence of an attribute (HTMLElement attribute)', () => {
- setElement( {
- tag: 'p',
- attributes: {
- 'class': bind.if( 'foo' )
- },
- children: [ 'abc' ]
- } );
- observable.foo = 'bar';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="true">abc</p>' );
- observable.foo = true;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="true">abc</p>' );
- observable.foo = 0;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="true">abc</p>' );
- observable.foo = false;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
- observable.foo = null;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
- observable.foo = undefined;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
- observable.foo = '';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
- } );
- // TODO: Is this alright? It makes sense but it's pretty useless. Text Node cannot be
- // removed just like an attribute of some HTMLElement.
- it( 'allows binding attribute to the observable – presence of an attribute (Text Node)', () => {
- setElement( {
- tag: 'p',
- children: [
- {
- text: bind.if( 'foo' )
- }
- ]
- } );
- observable.foo = 'abc';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>true</p>' );
- observable.foo = true;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>true</p>' );
- observable.foo = 0;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>true</p>' );
- observable.foo = false;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
- observable.foo = null;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
- observable.foo = undefined;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
- observable.foo = '';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
- } );
- it( 'allows binding attribute to the observable – value of an attribute (HTMLElement attribute)', () => {
- setElement( {
- tag: 'p',
- attributes: {
- 'class': bind.if( 'foo', 'bar' )
- },
- children: [ 'abc' ]
- } );
- observable.foo = 'bar';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar">abc</p>' );
- observable.foo = true;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar">abc</p>' );
- observable.foo = 0;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar">abc</p>' );
- observable.foo = 64;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar">abc</p>' );
- observable.foo = false;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
- observable.foo = null;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
- observable.foo = undefined;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
- } );
- it( 'allows binding attribute to the observable – value of an attribute (Text Node)', () => {
- setElement( {
- tag: 'p',
- children: [
- {
- text: bind.if( 'foo', 'bar' )
- }
- ]
- } );
- observable.foo = 'bar';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>bar</p>' );
- observable.foo = false;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p></p>' );
- observable.foo = 64;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>bar</p>' );
- } );
- it( 'allows binding attribute to the observable – array of bindings (HTMLElement attribute)', () => {
- setElement( {
- tag: 'p',
- attributes: {
- 'class': [
- 'ck-class',
- bind.if( 'foo', 'foo-set' ),
- bind.if( 'bar', 'bar-not-set', value => !value ),
- 'ck-end'
- ]
- },
- children: [ 'abc' ]
- } );
- observable.foo = observable.bar = true;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="ck-class ck-end foo-set">abc</p>' );
- observable.foo = observable.bar = false;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="bar-not-set ck-class ck-end">abc</p>' );
- } );
- it( 'allows binding attribute to the observable – value of an attribute processed by a callback', () => {
- setElement( {
- tag: 'p',
- attributes: {
- 'class': bind.if( 'foo', 'there–is–no–foo', value => !value )
- },
- children: [ 'abc' ]
- } );
- observable.foo = 'bar';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
- observable.foo = false;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="there–is–no–foo">abc</p>' );
- observable.foo = 64;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
- } );
- it( 'allows binding attribute to the observable – value of an attribute processed by a callback (use Node)', () => {
- setElement( {
- tag: 'p',
- attributes: {
- 'class': bind.if( 'foo', 'eqls-tag-name', ( value, el ) => el.tagName === value )
- },
- children: [ 'abc' ]
- } );
- observable.foo = 'bar';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
- observable.foo = 'P';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="eqls-tag-name">abc</p>' );
- observable.foo = 64;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
- } );
- it( 'allows binding attribute to the observable – falsy values', () => {
- setElement( {
- tag: 'p',
- attributes: {
- 'class': bind.if( 'foo', 'foo-is-set' )
- },
- children: [ 'abc' ]
- } );
- observable.foo = 'bar';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="foo-is-set">abc</p>' );
- observable.foo = true;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="foo-is-set">abc</p>' );
- observable.foo = 0;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p class="foo-is-set">abc</p>' );
- observable.foo = false;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
- observable.foo = null;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
- observable.foo = undefined;
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
- observable.foo = '';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc</p>' );
- } );
- } );
- it( 'works with Template#apply() – root element', () => {
- new Template( {
- tag: 'div',
- attributes: {
- class: bind.to( 'foo' )
- }
- } ).apply( el );
- expect( el.getAttribute( 'class' ) ).to.equal( 'bar' );
- observable.foo = 'baz';
- expect( el.getAttribute( 'class' ) ).to.equal( 'baz' );
- } );
- it( 'works with Template#apply() – children', () => {
- const el = getElement( { tag: 'div' } );
- const child = getElement( {
- tag: 'span',
- children: [ 'foo' ]
- } );
- el.appendChild( child );
- new Template( {
- tag: 'div',
- children: [
- {
- tag: 'span',
- children: [
- {
- text: bind.to( 'foo' )
- }
- ]
- }
- ]
- } ).apply( el );
- expect( child.textContent ).to.equal( 'bar' );
- observable.foo = 'baz';
- expect( child.textContent ).to.equal( 'baz' );
- } );
- } );
- } );
- describe( 'extend()', () => {
- let observable, emitter, bind;
- beforeEach( () => {
- observable = new Model( {
- foo: 'bar',
- baz: 'qux'
- } );
- emitter = Object.create( DomEmitterMixin );
- bind = Template.bind( observable, emitter );
- } );
- it( 'does not modify passed definition', () => {
- const def = {
- tag: 'p',
- attributes: {
- a: 'foo'
- }
- };
- const ext = {
- attributes: {
- b: 'bar'
- }
- };
- const tpl = new Template( def );
- Template.extend( tpl, ext );
- expect( ext.attributes.b ).to.equal( 'bar' );
- expect( tpl.attributes.a[ 0 ] ).to.equal( 'foo' );
- expect( tpl.attributes.b[ 0 ] ).to.equal( 'bar' );
- } );
- it( 'throws an error if an element has already been rendered', () => {
- const tpl = new Template( {
- tag: 'p'
- } );
- Template.extend( tpl, {
- attributes: {
- class: 'foo'
- }
- } );
- tpl.render();
- expectToThrowCKEditorError( () => {
- Template.extend( tpl, {
- attributes: {
- class: 'bar'
- }
- } );
- }, /^template-extend-render/ );
- } );
- describe( 'attributes', () => {
- it( 'extends existing - simple', () => {
- extensionTest(
- {
- tag: 'p',
- attributes: {
- a: 'b'
- }
- },
- {
- attributes: {
- a: 'c'
- }
- },
- '<p a="b c"></p>'
- );
- } );
- it( 'extends existing - complex #1', () => {
- extensionTest(
- {
- tag: 'p',
- attributes: {
- a: [ 'b', 'c' ]
- }
- },
- {
- attributes: {
- a: 'd'
- }
- },
- '<p a="b c d"></p>'
- );
- } );
- it( 'extends existing - complex #2', () => {
- extensionTest(
- {
- tag: 'p',
- attributes: {
- a: {
- value: 'b'
- }
- }
- },
- {
- attributes: {
- a: [ 'c', 'd' ]
- }
- },
- '<p a="b c d"></p>'
- );
- } );
- it( 'extends existing - complex #3', () => {
- extensionTest(
- {
- tag: 'p',
- attributes: {
- a: [ 'b' ]
- }
- },
- {
- attributes: {
- a: [ 'c', 'd' ]
- }
- },
- '<p a="b c d"></p>'
- );
- } );
- it( 'extends existing - bindings #1', () => {
- const el = extensionTest(
- {
- tag: 'p',
- attributes: {
- a: bind.to( 'foo' )
- }
- },
- {
- attributes: {
- a: [ 'c', 'd' ]
- }
- },
- '<p a="bar c d"></p>'
- );
- observable.foo = 'baz';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p a="baz c d"></p>' );
- } );
- it( 'extends existing - bindings #2', () => {
- const el = extensionTest(
- {
- tag: 'p',
- attributes: {
- a: [ 'b', bind.to( 'foo' ) ]
- }
- },
- {
- attributes: {
- a: [ 'c', bind.to( 'baz' ) ]
- }
- },
- '<p a="b bar c qux"></p>'
- );
- observable.foo = 'abc';
- observable.baz = 'def';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p a="b abc c def"></p>' );
- } );
- it( 'creates new - no attributes', () => {
- extensionTest(
- {
- tag: 'p'
- },
- {
- attributes: {
- c: 'd'
- }
- },
- '<p c="d"></p>'
- );
- } );
- it( 'creates new - simple', () => {
- extensionTest(
- {
- tag: 'p',
- attributes: {
- a: 'b'
- }
- },
- {
- attributes: {
- c: 'd'
- }
- },
- '<p a="b" c="d"></p>'
- );
- } );
- it( 'creates new - array', () => {
- extensionTest(
- {
- tag: 'p',
- attributes: {
- a: 'b'
- }
- },
- {
- attributes: {
- c: [ 'd', 'e' ]
- }
- },
- '<p a="b" c="d e"></p>'
- );
- } );
- it( 'creates new - bindings #1', () => {
- const el = extensionTest(
- {
- tag: 'p',
- attributes: {
- a: 'b'
- }
- },
- {
- attributes: {
- c: bind.to( 'foo' )
- }
- },
- '<p a="b" c="bar"></p>'
- );
- observable.foo = 'abc';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p a="b" c="abc"></p>' );
- } );
- it( 'creates new - bindings #2', () => {
- const el = extensionTest(
- {
- tag: 'p',
- attributes: {
- a: 'b'
- }
- },
- {
- attributes: {
- c: [ 'd', bind.to( 'foo' ) ]
- }
- },
- '<p a="b" c="d bar"></p>'
- );
- observable.foo = 'abc';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p a="b" c="d abc"></p>' );
- } );
- } );
- describe( 'text', () => {
- it( 'extends existing - simple', () => {
- const el = extensionTest(
- {
- tag: 'p',
- children: [
- 'foo'
- ]
- },
- {
- children: [
- 'bar'
- ]
- },
- '<p>foobar</p>'
- );
- expect( el.childNodes ).to.have.length( 1 );
- } );
- it( 'extends existing - complex #1', () => {
- const el = extensionTest(
- {
- tag: 'p',
- children: [
- { text: 'foo' }
- ]
- },
- {
- children: [
- 'bar'
- ]
- },
- '<p>foobar</p>'
- );
- expect( el.childNodes ).to.have.length( 1 );
- } );
- it( 'extends existing - complex #2', () => {
- const el = extensionTest(
- {
- tag: 'p',
- children: [
- { text: 'foo' }
- ]
- },
- {
- children: [
- { text: 'bar' }
- ]
- },
- '<p>foobar</p>'
- );
- expect( el.childNodes ).to.have.length( 1 );
- } );
- it( 'extends existing - bindings #1', () => {
- const el = extensionTest(
- {
- tag: 'p',
- children: [
- { text: bind.to( 'foo' ) }
- ]
- },
- {
- children: [
- 'abc'
- ]
- },
- '<p>bar abc</p>'
- );
- observable.foo = 'asd';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>asd abc</p>' );
- expect( el.childNodes ).to.have.length( 1 );
- } );
- it( 'extends existing - bindings #2', () => {
- const el = extensionTest(
- {
- tag: 'p',
- children: [
- 'abc'
- ]
- },
- {
- children: [
- { text: bind.to( 'foo' ) }
- ]
- },
- '<p>abc bar</p>'
- );
- observable.foo = 'asd';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>abc asd</p>' );
- expect( el.childNodes ).to.have.length( 1 );
- } );
- it( 'extends existing - bindings #3', () => {
- const el = extensionTest(
- {
- tag: 'p',
- children: [
- { text: bind.to( 'foo' ) },
- 'X'
- ]
- },
- {
- children: [
- { text: bind.to( 'baz' ) },
- 'Y'
- ]
- },
- '<p>bar quxXY</p>'
- );
- observable.foo = 'A';
- observable.baz = 'B';
- expect( normalizeHtml( el.outerHTML ) ).to.equal( '<p>A BXY</p>' );
- expect( el.childNodes ).to.have.length( 2 );
- } );
- } );
- describe( 'children', () => {
- it( 'should throw when the number of children does not correspond', () => {
- expectToThrowCKEditorError( () => {
- extensionTest(
- {
- tag: 'p',
- children: [
- 'foo'
- ]
- },
- {
- children: [
- 'foo',
- 'bar'
- ]
- },
- 'it should fail'
- );
- }, /ui-template-extend-children-mismatch/ );
- } );
- it( 'should throw when no children in target but extending one', () => {
- expectToThrowCKEditorError( () => {
- extensionTest(
- {
- tag: 'p'
- },
- {
- children: [
- {
- tag: 'b'
- }
- ]
- },
- 'it should fail'
- );
- }, /ui-template-extend-children-mismatch/ );
- } );
- it( 'should throw when the number of children does not correspond on some deeper level', () => {
- expectToThrowCKEditorError( () => {
- extensionTest(
- {
- tag: 'p',
- children: [
- {
- tag: 'span',
- attributes: {
- class: 'A'
- },
- children: [
- 'A',
- {
- tag: 'span',
- attributes: {
- class: 'AA'
- },
- children: [
- 'AA'
- ]
- }
- ]
- }
- ]
- },
- {
- children: [
- {
- attributes: {
- class: 'B'
- },
- children: [
- 'B'
- ]
- }
- ]
- },
- 'it should fail'
- );
- }, /ui-template-extend-children-mismatch/ );
- } );
- it( 'extends existing - simple', () => {
- extensionTest(
- {
- tag: 'p',
- children: [
- {
- tag: 'span',
- attributes: {
- class: 'foo'
- }
- }
- ]
- },
- {
- children: [
- {
- tag: 'span',
- attributes: {
- class: 'bar'
- }
- }
- ]
- },
- '<p><span class="bar foo"></span></p>'
- );
- } );
- it( 'extends existing - complex', () => {
- extensionTest(
- {
- tag: 'p',
- children: [
- {
- tag: 'span',
- attributes: {
- class: 'A'
- },
- children: [
- 'A',
- {
- tag: 'span',
- attributes: {
- class: 'AA'
- },
- children: [
- 'AA'
- ]
- }
- ]
- }
- ]
- },
- {
- children: [
- {
- tag: 'span',
- attributes: {
- class: 'B'
- },
- children: [
- 'B',
- {
- tag: 'span',
- attributes: {
- class: 'BB'
- },
- children: [
- 'BB'
- ]
- }
- ]
- }
- ]
- },
- '<p><span class="A B">AB<span class="AA BB">AABB</span></span></p>'
- );
- } );
- it( 'allows extending a particular child', () => {
- const template = new Template( {
- tag: 'p',
- children: [
- {
- tag: 'span',
- attributes: {
- class: 'foo'
- }
- }
- ]
- } );
- Template.extend( template.children[ 0 ], {
- attributes: {
- class: 'bar'
- }
- } );
- expect( template.render().outerHTML ).to.equal( '<p><span class="foo bar"></span></p>' );
- } );
- it( 'allows extending a particular child – recursively', () => {
- const template = new Template( {
- tag: 'p',
- children: [
- {
- tag: 'span',
- attributes: {
- class: 'A'
- },
- children: [
- 'A',
- {
- tag: 'span',
- attributes: {
- class: 'AA'
- },
- children: [
- 'AA'
- ]
- }
- ]
- }
- ]
- } );
- Template.extend( template.children[ 0 ], {
- attributes: {
- class: 'B'
- },
- children: [
- 'B',
- {
- attributes: {
- class: 'BB'
- }
- }
- ]
- } );
- expect( template.render().outerHTML ).to.equal( '<p><span class="A B">AB<span class="AA BB">AA</span></span></p>' );
- } );
- } );
- describe( 'listeners', () => {
- it( 'extends existing', () => {
- const spy1 = sinon.spy();
- const spy2 = sinon.spy();
- const spy3 = sinon.spy();
- const spy4 = sinon.spy();
- const spy5 = sinon.spy();
- observable.on( 'A', spy1 );
- observable.on( 'C', spy2 );
- observable.on( 'B', spy3 );
- observable.on( 'D', spy4 );
- const el = extensionTest(
- {
- tag: 'p',
- children: [
- {
- tag: 'span'
- }
- ],
- on: {
- click: bind.to( 'A' ),
- 'click@span': [
- bind.to( 'B' ),
- bind.to( spy5 )
- ]
- }
- },
- {
- on: {
- click: bind.to( 'C' ),
- 'click@span': bind.to( 'D' )
- }
- },
- '<p><span></span></p>'
- );
- dispatchEvent( el, 'click' );
- expect( spy1.calledOnce ).to.be.true;
- expect( spy2.calledOnce ).to.be.true;
- expect( spy3.called ).to.be.false;
- expect( spy4.called ).to.be.false;
- expect( spy5.called ).to.be.false;
- dispatchEvent( el.firstChild, 'click' );
- expect( spy1.calledTwice ).to.be.true;
- expect( spy2.calledTwice ).to.be.true;
- expect( spy3.calledOnce ).to.be.true;
- expect( spy4.calledOnce ).to.be.true;
- expect( spy5.calledOnce ).to.be.true;
- } );
- it( 'creates new', () => {
- const spy1 = sinon.spy();
- const spy2 = sinon.spy();
- const spy3 = sinon.spy();
- observable.on( 'A', spy1 );
- observable.on( 'B', spy2 );
- const el = extensionTest(
- {
- tag: 'p',
- children: [
- {
- tag: 'span'
- }
- ]
- },
- {
- on: {
- click: bind.to( 'A' ),
- 'click@span': [
- bind.to( 'B' ),
- bind.to( spy3 )
- ]
- }
- },
- '<p><span></span></p>'
- );
- dispatchEvent( el, 'click' );
- expect( spy1.calledOnce ).to.be.true;
- expect( spy2.called ).to.be.false;
- expect( spy3.called ).to.be.false;
- dispatchEvent( el.firstChild, 'click' );
- expect( spy1.calledTwice ).to.be.true;
- expect( spy2.calledOnce ).to.be.true;
- expect( spy3.calledOnce ).to.be.true;
- } );
- } );
- } );
- } );
- function getElement( template ) {
- return new Template( template ).render();
- }
- function setElement( template ) {
- el = new Template( template ).render();
- document.body.appendChild( el );
- injectedElements.push( el );
- }
- function extensionTest( baseDefinition, extendedDefinition, expectedHtml ) {
- const template = new Template( baseDefinition );
- Template.extend( template, extendedDefinition );
- const el = template.render();
- document.body.appendChild( el );
- expect( normalizeHtml( el.outerHTML ) ).to.equal( expectedHtml );
- injectedElements.push( el );
- return el;
- }
- function dispatchEvent( el, domEvtName ) {
- if ( !el.parentNode ) {
- throw new Error( 'To dispatch an event, element must be in DOM. Otherwise #target is null.' );
- }
- el.dispatchEvent( new Event( domEvtName, {
- bubbles: true
- } ) );
- }
- function getView( def ) {
- const view = new View();
- view.setTemplate( def );
- return view;
- }
|