| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import Model from '../../src/model/model';
- import Writer from '../../src/model/writer';
- import Batch from '../../src/model/batch';
- import InsertDelta from '../../src/model/delta/insertdelta';
- import WeakInsertDelta from '../../src/model/delta/weakinsertdelta';
- import DocumentFragment from '../../src/model/documentfragment';
- import Element from '../../src/model/element';
- import Text from '../../src/model/text';
- import Position from '../../src/model/position';
- import Range from '../../src/model/range';
- import count from '@ckeditor/ckeditor5-utils/src/count';
- import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
- import { getNodesAndText } from '../../tests/model/_utils/utils';
- import DocumentSelection from '../../src/model/documentselection';
- describe( 'Writer', () => {
- let model, doc, batch;
- beforeEach( () => {
- model = new Model();
- batch = new Batch();
- doc = model.document;
- } );
- describe( 'constructor()', () => {
- let writer;
- beforeEach( () => {
- writer = new Writer( model, batch );
- } );
- it( 'should have model instance', () => {
- expect( writer.model ).to.instanceof( Model );
- } );
- it( 'should have batch instance', () => {
- expect( writer.batch ).to.instanceof( Batch );
- } );
- } );
- describe( 'createText()', () => {
- it( 'should create text node', () => {
- const text = createText( 'foo' );
- expect( text ).to.instanceof( Text );
- expect( text.data ).to.equal( 'foo' );
- expect( Array.from( text.getAttributes() ) ).to.length( 0 );
- } );
- it( 'should create text with attributes', () => {
- const text = createText( 'foo', { foo: 'bar', biz: 'baz' } );
- expect( Array.from( text.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'biz', 'baz' ] ] );
- } );
- } );
- describe( 'createElement()', () => {
- it( 'should create element', () => {
- const element = createElement( 'foo' );
- expect( element ).to.instanceof( Element );
- expect( element.name ).to.equal( 'foo' );
- expect( Array.from( element.getAttributes() ) ).to.length( 0 );
- } );
- it( 'should create element with attributes', () => {
- const element = createText( 'foo', { foo: 'bar', biz: 'baz' } );
- expect( Array.from( element.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'biz', 'baz' ] ] );
- } );
- } );
- describe( 'createDocumentFragment()', () => {
- it( 'should create element', () => {
- const element = createDocumentFragment();
- expect( element ).to.instanceof( DocumentFragment );
- } );
- } );
- describe( 'insert()', () => {
- it( 'should insert node at given position', () => {
- const parent = createDocumentFragment();
- const child = createElement( 'child' );
- const textChild = createText( 'textChild' );
- insert( child, new Position( parent, [ 0 ] ) );
- insert( textChild, new Position( parent, [ 1 ] ) );
- expect( Array.from( parent ) ).to.deep.equal( [ child, textChild ] );
- } );
- it( 'should insert node at the beginning of given element', () => {
- const parent = createDocumentFragment();
- const child1 = createElement( 'child' );
- const child2 = createElement( 'child' );
- insert( child1, parent );
- insert( child2, parent );
- expect( Array.from( parent.getChildren() ) ).to.deep.equal( [ child2, child1 ] );
- } );
- it( 'should insert node at the end of given element', () => {
- const parent = createDocumentFragment();
- const child1 = createElement( 'child' );
- const child2 = createElement( 'child' );
- insert( child1, parent );
- insert( child2, parent, 'end' );
- expect( Array.from( parent.getChildren() ) ).to.deep.equal( [ child1, child2 ] );
- } );
- it( 'should insert node at the given offset of given element', () => {
- const parent = createDocumentFragment();
- const child1 = createElement( 'child' );
- const child2 = createElement( 'child' );
- const child3 = createElement( 'child' );
- insert( child3, parent );
- insert( child1, parent );
- insert( child2, parent, 1 );
- expect( Array.from( parent.getChildren() ) ).to.deep.equal( [ child1, child2, child3 ] );
- } );
- it( 'should insert node before the given node', () => {
- const parent = createDocumentFragment();
- const child1 = createElement( 'child' );
- const child2 = createElement( 'child' );
- const child3 = createElement( 'child' );
- insert( child3, parent );
- insert( child1, parent );
- insert( child2, child3, 'before' );
- expect( Array.from( parent.getChildren() ) ).to.deep.equal( [ child1, child2, child3 ] );
- } );
- it( 'should insert node after the given node', () => {
- const parent = createDocumentFragment();
- const child1 = createElement( 'child' );
- const child2 = createElement( 'child' );
- const child3 = createElement( 'child' );
- insert( child3, parent );
- insert( child1, parent );
- insert( child2, child1, 'after' );
- expect( Array.from( parent.getChildren() ) ).to.deep.equal( [ child1, child2, child3 ] );
- } );
- it( 'should create proper delta for inserting element', () => {
- const parent = createDocumentFragment();
- const element = createElement( 'child' );
- const spy = sinon.spy( model, 'applyOperation' );
- insert( element, parent );
- sinon.assert.calledOnce( spy );
- expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
- expect( spy.lastCall.args[ 0 ].delta ).to.instanceof( InsertDelta );
- expect( spy.lastCall.args[ 0 ].delta ).to.not.instanceof( WeakInsertDelta );
- expect( spy.lastCall.args[ 0 ].delta.batch ).to.equal( batch );
- } );
- it( 'should create proper delta for inserting text', () => {
- const parent = createDocumentFragment();
- const text = createText( 'child' );
- const spy = sinon.spy( model, 'applyOperation' );
- insert( text, parent );
- sinon.assert.calledOnce( spy );
- expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
- expect( spy.lastCall.args[ 0 ].delta ).to.instanceof( WeakInsertDelta );
- expect( spy.lastCall.args[ 0 ].delta.batch ).to.equal( batch );
- } );
- it( 'should move element from one parent to the other within the same document (rootA -> rootA)', () => {
- const root = doc.createRoot();
- const parent1 = createElement( 'parent' );
- const parent2 = createElement( 'parent' );
- const node = createText( 'foo' );
- insert( node, parent1 );
- insert( parent1, root );
- insert( parent2, root );
- const spy = sinon.spy( model, 'applyOperation' );
- insert( node, parent2 );
- // Verify result.
- expect( Array.from( parent1.getChildren() ) ).to.deep.equal( [] );
- expect( Array.from( parent2.getChildren() ) ).to.deep.equal( [ node ] );
- // Verify deltas and operations.
- sinon.assert.calledOnce( spy );
- expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
- expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
- } );
- it( 'should move element from one parent to the other within the same document (rootA -> rootB)', () => {
- const rootA = doc.createRoot( '$root', 'A' );
- const rootB = doc.createRoot( '$root', 'B' );
- const node = createText( 'foo' );
- insert( node, rootA );
- const spy = sinon.spy( model, 'applyOperation' );
- insert( node, rootB );
- // Verify result.
- expect( Array.from( rootA.getChildren() ) ).to.deep.equal( [] );
- expect( Array.from( rootB.getChildren() ) ).to.deep.equal( [ node ] );
- // Verify deltas and operations.
- sinon.assert.calledOnce( spy );
- expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
- expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
- } );
- it( 'should move element from one parent to the other within the same document (docFragA -> docFragA)', () => {
- const docFragA = createDocumentFragment();
- const parent1 = createElement( 'parent' );
- const parent2 = createElement( 'parent' );
- const node = createText( 'foo' );
- insert( node, parent1 );
- insert( parent1, docFragA );
- insert( parent2, docFragA );
- const spy = sinon.spy( model, 'applyOperation' );
- insert( node, parent2 );
- // Verify result.
- expect( Array.from( parent1.getChildren() ) ).to.deep.equal( [] );
- expect( Array.from( parent2.getChildren() ) ).to.deep.equal( [ node ] );
- // Verify deltas and operations.
- sinon.assert.calledOnce( spy );
- expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
- expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
- } );
- it( 'should move element from one parent to the other within different document (docFragA -> docFragB)', () => {
- const docFragA = createDocumentFragment();
- const docFragB = createDocumentFragment();
- const node = createText( 'foo' );
- insert( node, docFragA );
- const spy = sinon.spy( model, 'applyOperation' );
- insert( node, docFragB );
- // Verify result.
- expect( Array.from( docFragA ) ).to.deep.equal( [] );
- expect( Array.from( docFragB ) ).to.deep.equal( [ node ] );
- // Verify deltas and operations.
- sinon.assert.calledTwice( spy );
- expect( spy.firstCall.args[ 0 ].type ).to.equal( 'detach' );
- expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
- expect( spy.secondCall.args[ 0 ].type ).to.equal( 'insert' );
- expect( spy.secondCall.args[ 0 ].delta.batch ).to.equal( batch );
- } );
- it( 'should throw when moving element from document to document fragment', () => {
- const root = doc.createRoot();
- const docFrag = createDocumentFragment();
- const node = createText( 'foo' );
- insert( node, root );
- expect( () => {
- insert( node, docFrag );
- } ).to.throw();
- } );
- it( 'should transfer markers from given DocumentFragment', () => {
- const root = doc.createRoot();
- const docFrag = createDocumentFragment();
- appendText( 'abcd', root );
- appendElement( 'p', docFrag );
- insertText( 'foo bar', new Position( docFrag, [ 0, 0 ] ) );
- const marker = new Range( new Position( docFrag, [ 0, 1 ] ), new Position( docFrag, [ 0, 5 ] ) );
- docFrag.markers.set( 'marker', marker );
- insert( docFrag, new Position( root, [ 2 ] ) );
- expect( Array.from( model.markers ).length ).to.equal( 1 );
- const range = model.markers.get( 'marker' ).getRange();
- expect( range.root ).to.equal( root );
- expect( range.start.path ).to.deep.equal( [ 2, 1 ] );
- expect( range.end.path ).to.deep.equal( [ 2, 5 ] );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const writer = new Writer( model, batch );
- const root = doc.createRoot();
- const node = createText( 'foo' );
- expect( () => {
- writer.insert( node, root );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- describe( 'insertText()', () => {
- it( 'should create and insert text node with attributes at given position', () => {
- const parent = createDocumentFragment();
- insertText( 'foo', { bar: 'biz' }, new Position( parent, [ 0 ] ) );
- expect( parent.childCount ).to.equal( 1 );
- expect( parent.getChild( 0 ) ).to.instanceof( Text );
- expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
- expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'bar', 'biz' ] ] );
- } );
- it( 'should create and insert text node with no attributes at given position', () => {
- const parent = createDocumentFragment();
- insertText( 'foo', null, new Position( parent, [ 0 ] ) );
- expect( parent.childCount ).to.equal( 1 );
- expect( parent.getChild( 0 ) ).to.instanceof( Text );
- expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
- expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
- } );
- it( 'should create and insert text node omitting attributes param', () => {
- const parent = createDocumentFragment();
- insertText( 'foo', new Position( parent, [ 0 ] ) );
- expect( parent.childCount ).to.equal( 1 );
- expect( parent.getChild( 0 ) ).to.instanceof( Text );
- expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
- expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
- } );
- it( 'should create and insert text node at the beginning of given element', () => {
- const parent = createDocumentFragment();
- insert( createElement( 'child' ), parent );
- insertText( 'foo', parent );
- expect( parent.childCount ).to.equal( 2 );
- expect( parent.getChild( 0 ) ).to.instanceof( Text );
- expect( parent.getChild( 1 ) ).to.instanceof( Element );
- } );
- it( 'should create and insert text node at the end of given element', () => {
- const parent = createDocumentFragment();
- insert( createElement( 'child' ), parent );
- insertText( 'foo', parent, 'end' );
- expect( parent.childCount ).to.equal( 2 );
- expect( parent.getChild( 0 ) ).to.instanceof( Element );
- expect( parent.getChild( 1 ) ).to.instanceof( Text );
- } );
- it( 'should create and insert text node at the given offset of given element', () => {
- const parent = createDocumentFragment();
- insert( createElement( 'child' ), parent );
- insert( createElement( 'child' ), parent );
- insertText( 'foo', parent, 1 );
- expect( parent.childCount ).to.equal( 3 );
- expect( parent.getChild( 0 ) ).to.instanceof( Element );
- expect( parent.getChild( 1 ) ).to.instanceof( Text );
- expect( parent.getChild( 2 ) ).to.instanceof( Element );
- } );
- it( 'should create and insert text node before the given node', () => {
- const parent = createDocumentFragment();
- const child1 = createElement( 'child' );
- const child2 = createElement( 'child' );
- insert( child1, parent );
- insert( child2, parent, 'end' );
- insertText( 'foo', child2, 'before' );
- expect( parent.childCount ).to.equal( 3 );
- expect( parent.getChild( 0 ) ).to.instanceof( Element );
- expect( parent.getChild( 1 ) ).to.instanceof( Text );
- expect( parent.getChild( 2 ) ).to.instanceof( Element );
- } );
- it( 'should create and insert text node after the given node', () => {
- const parent = createDocumentFragment();
- const child1 = createElement( 'child' );
- const child2 = createElement( 'child' );
- insert( child1, parent );
- insert( child2, parent, 'end' );
- insertText( 'foo', child1, 'after' );
- expect( parent.childCount ).to.equal( 3 );
- expect( parent.getChild( 0 ) ).to.instanceof( Element );
- expect( parent.getChild( 1 ) ).to.instanceof( Text );
- expect( parent.getChild( 2 ) ).to.instanceof( Element );
- } );
- it( 'should create proper delta', () => {
- const parent = createDocumentFragment();
- const spy = sinon.spy( model, 'applyOperation' );
- insertText( 'foo', parent );
- sinon.assert.calledOnce( spy );
- expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
- expect( spy.lastCall.args[ 0 ].delta ).to.instanceof( WeakInsertDelta );
- expect( spy.lastCall.args[ 0 ].delta.batch ).to.equal( batch );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const writer = new Writer( model, batch );
- const parent = createDocumentFragment();
- expect( () => {
- writer.insertText( 'foo', parent );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- describe( 'insertElement()', () => {
- it( 'should create and insert element with attributes at given position', () => {
- const parent = createDocumentFragment();
- insertElement( 'foo', { bar: 'biz' }, new Position( parent, [ 0 ] ) );
- expect( parent.childCount ).to.equal( 1 );
- expect( parent.getChild( 0 ) ).to.instanceof( Element );
- expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
- expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'bar', 'biz' ] ] );
- } );
- it( 'should create and insert element with no attributes at given position', () => {
- const parent = createDocumentFragment();
- insertElement( 'foo', null, new Position( parent, [ 0 ] ) );
- expect( parent.childCount ).to.equal( 1 );
- expect( parent.getChild( 0 ) ).to.instanceof( Element );
- expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
- expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
- } );
- it( 'should create and insert element with no attributes omitting attributes param', () => {
- const parent = createDocumentFragment();
- insertElement( 'foo', new Position( parent, [ 0 ] ) );
- expect( parent.childCount ).to.equal( 1 );
- expect( parent.getChild( 0 ) ).to.instanceof( Element );
- expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
- expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
- } );
- it( 'should create and insert element at the beginning of given element', () => {
- const parent = createDocumentFragment();
- insert( createElement( 'child' ), parent );
- insertElement( 'foo', parent );
- expect( parent.childCount ).to.equal( 2 );
- expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
- expect( parent.getChild( 1 ).name ).to.equal( 'child' );
- } );
- it( 'should create and insert element at the end of given element', () => {
- const parent = createDocumentFragment();
- insert( createElement( 'child' ), parent );
- insertElement( 'foo', parent, 'end' );
- expect( parent.childCount ).to.equal( 2 );
- expect( parent.getChild( 0 ).name ).to.equal( 'child' );
- expect( parent.getChild( 1 ).name ).to.equal( 'foo' );
- } );
- it( 'should create and insert element at the given offset of given element', () => {
- const parent = createDocumentFragment();
- insert( createElement( 'child1' ), parent );
- insert( createElement( 'child2' ), parent, 'end' );
- insertElement( 'foo', parent, 1 );
- expect( parent.childCount ).to.equal( 3 );
- expect( parent.getChild( 0 ).name ).to.equal( 'child1' );
- expect( parent.getChild( 1 ).name ).to.equal( 'foo' );
- expect( parent.getChild( 2 ).name ).to.equal( 'child2' );
- } );
- it( 'should create and insert element before the given node', () => {
- const parent = createDocumentFragment();
- const child1 = createElement( 'child1' );
- const child2 = createElement( 'child2' );
- insert( child1, parent );
- insert( child2, parent, 'end' );
- insertElement( 'foo', child2, 'before' );
- expect( parent.childCount ).to.equal( 3 );
- expect( parent.getChild( 0 ).name ).to.equal( 'child1' );
- expect( parent.getChild( 1 ).name ).to.equal( 'foo' );
- expect( parent.getChild( 2 ).name ).to.equal( 'child2' );
- } );
- it( 'should create and insert element after the given node', () => {
- const parent = createDocumentFragment();
- const child1 = createElement( 'child1' );
- const child2 = createElement( 'child2' );
- insert( child1, parent );
- insert( child2, parent, 'end' );
- insertElement( 'foo', child1, 'after' );
- expect( parent.childCount ).to.equal( 3 );
- expect( parent.getChild( 0 ).name ).to.equal( 'child1' );
- expect( parent.getChild( 1 ).name ).to.equal( 'foo' );
- expect( parent.getChild( 2 ).name ).to.equal( 'child2' );
- } );
- it( 'should create proper delta', () => {
- const parent = createDocumentFragment();
- const spy = sinon.spy( model, 'applyOperation' );
- insertText( 'foo', parent );
- sinon.assert.calledOnce( spy );
- expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
- expect( spy.lastCall.args[ 0 ].delta ).to.instanceof( InsertDelta );
- expect( spy.lastCall.args[ 0 ].delta.batch ).to.equal( batch );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const writer = new Writer( model, batch );
- const child = createElement( 'child' );
- expect( () => {
- writer.insertElement( 'foo', child, 'after' );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- describe( 'append()', () => {
- it( 'should insert element at the end of the parent', () => {
- const parent = createDocumentFragment();
- const childText = createText( 'foo' );
- const childElement = createElement( 'foo' );
- append( childText, parent );
- append( childElement, parent );
- expect( Array.from( parent ) ).to.deep.equal( [ childText, childElement ] );
- } );
- it( 'should create proper delta', () => {
- const parent = createDocumentFragment();
- const text = createText( 'foo' );
- const spy = sinon.spy( model, 'applyOperation' );
- append( text, parent );
- sinon.assert.calledOnce( spy );
- expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
- expect( spy.lastCall.args[ 0 ].delta.batch ).to.equal( batch );
- } );
- it( 'should move element from one parent to the other within the same root (rootA -> rootA)', () => {
- const rootA = doc.createRoot();
- const parent1 = createElement( 'parent' );
- const parent2 = createElement( 'parent' );
- const node = createText( 'foo' );
- insert( node, parent1 );
- insert( parent1, rootA );
- insert( parent2, rootA );
- const spy = sinon.spy( model, 'applyOperation' );
- append( node, parent2 );
- // Verify result.
- expect( Array.from( parent1.getChildren() ) ).to.deep.equal( [] );
- expect( Array.from( parent2.getChildren() ) ).to.deep.equal( [ node ] );
- // Verify deltas and operations.
- sinon.assert.calledOnce( spy );
- expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
- expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
- } );
- it( 'should move element from one parent to the other within the same document (rootA -> rootB)', () => {
- const rootA = doc.createRoot( '$root', 'A' );
- const rootB = doc.createRoot( '$root', 'B' );
- const node = createText( 'foo' );
- insert( node, rootA );
- const spy = sinon.spy( model, 'applyOperation' );
- append( node, rootB );
- // Verify result.
- expect( Array.from( rootA.getChildren() ) ).to.deep.equal( [] );
- expect( Array.from( rootB.getChildren() ) ).to.deep.equal( [ node ] );
- // Verify deltas and operations.
- sinon.assert.calledOnce( spy );
- expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
- expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
- } );
- it( 'should move element from one parent to the other within the same document fragment (docFragA -> docFragA)', () => {
- const docFragA = createDocumentFragment();
- const parent1 = createElement( 'parent' );
- const parent2 = createElement( 'parent' );
- const node = createText( 'foo' );
- insert( node, parent1 );
- insert( parent1, docFragA );
- insert( parent2, docFragA );
- const spy = sinon.spy( model, 'applyOperation' );
- append( node, parent2 );
- // Verify result.
- expect( Array.from( parent1.getChildren() ) ).to.deep.equal( [] );
- expect( Array.from( parent2.getChildren() ) ).to.deep.equal( [ node ] );
- // Verify deltas and operations.
- sinon.assert.calledOnce( spy );
- expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
- expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
- } );
- it( 'should move element from one parent to the other within different document fragments (docFragA -> docFragB)', () => {
- const docFragA = createDocumentFragment();
- const docFragB = createDocumentFragment();
- const node = createText( 'foo' );
- insert( node, docFragA );
- const spy = sinon.spy( model, 'applyOperation' );
- append( node, docFragB );
- // Verify result.
- expect( Array.from( docFragA ) ).to.deep.equal( [] );
- expect( Array.from( docFragB ) ).to.deep.equal( [ node ] );
- // Verify deltas and operations.
- sinon.assert.calledTwice( spy );
- expect( spy.firstCall.args[ 0 ].type ).to.equal( 'detach' );
- expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
- expect( spy.secondCall.args[ 0 ].type ).to.equal( 'insert' );
- expect( spy.secondCall.args[ 0 ].delta.batch ).to.equal( batch );
- } );
- it( 'should throw when moving element from document to document fragment', () => {
- const root = doc.createRoot();
- const docFrag = createDocumentFragment();
- const node = createText( 'foo' );
- insert( node, root );
- expect( () => {
- append( node, docFrag );
- } ).to.throw();
- } );
- } );
- describe( 'appendText()', () => {
- it( 'should create and insert text node with attributes at the end of the parent', () => {
- const parent = createDocumentFragment();
- appendText( 'foo', { bar: 'biz' }, parent );
- appendText( 'bar', { biz: 'bar' }, parent );
- expect( parent.childCount ).to.equal( 2 );
- expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
- expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'bar', 'biz' ] ] );
- expect( parent.getChild( 1 ).data ).to.equal( 'bar' );
- expect( Array.from( parent.getChild( 1 ).getAttributes() ) ).to.deep.equal( [ [ 'biz', 'bar' ] ] );
- } );
- it( 'should create and insert text node with no attributes at the end of the parent', () => {
- const parent = createDocumentFragment();
- appendText( 'foo', null, parent );
- expect( parent.childCount ).to.equal( 1 );
- expect( parent.getChild( 0 ) ).to.instanceof( Text );
- expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
- expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
- } );
- it( 'should create and insert text node with no attributes omitting attributes param', () => {
- const parent = createDocumentFragment();
- appendText( 'foo', parent );
- expect( parent.childCount ).to.equal( 1 );
- expect( parent.getChild( 0 ) ).to.instanceof( Text );
- expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
- expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
- } );
- it( 'should create proper delta and operations', () => {
- const parent = createDocumentFragment();
- const spy = sinon.spy( model, 'applyOperation' );
- appendText( 'foo', parent );
- sinon.assert.calledOnce( spy );
- expect( spy.firstCall.args[ 0 ].type ).to.equal( 'insert' );
- expect( spy.firstCall.args[ 0 ].delta ).to.instanceof( WeakInsertDelta );
- expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const writer = new Writer( model, batch );
- const parent = createDocumentFragment();
- expect( () => {
- writer.appendText( 'foo', parent );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- describe( 'appendElement()', () => {
- it( 'should create and insert element with attributes at the end of the parent', () => {
- const parent = createDocumentFragment();
- appendElement( 'foo', { bar: 'biz' }, parent );
- appendElement( 'bar', { biz: 'bar' }, parent );
- expect( parent.childCount ).to.equal( 2 );
- expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
- expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'bar', 'biz' ] ] );
- expect( parent.getChild( 1 ).name ).to.equal( 'bar' );
- expect( Array.from( parent.getChild( 1 ).getAttributes() ) ).to.deep.equal( [ [ 'biz', 'bar' ] ] );
- } );
- it( 'should create and insert element with no attributes at the end of the parent', () => {
- const parent = createDocumentFragment();
- appendElement( 'foo', null, parent );
- expect( parent.childCount ).to.equal( 1 );
- expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
- expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
- } );
- it( 'should create and insert element with no attributes omitting attributes param', () => {
- const parent = createDocumentFragment();
- appendElement( 'foo', parent );
- expect( parent.childCount ).to.equal( 1 );
- expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
- expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
- } );
- it( 'should create proper delta and operation', () => {
- const parent = createDocumentFragment();
- const spy = sinon.spy( model, 'applyOperation' );
- appendElement( 'foo', parent );
- sinon.assert.calledOnce( spy );
- expect( spy.firstCall.args[ 0 ].type ).to.equal( 'insert' );
- expect( spy.firstCall.args[ 0 ].delta ).to.instanceof( InsertDelta ).to.not.instanceof( WeakInsertDelta );
- expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const writer = new Writer( model, batch );
- const parent = createDocumentFragment();
- expect( () => {
- writer.appendElement( 'foo', parent );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- describe( 'setAttribute() / removeAttribute()', () => {
- let root, spy;
- beforeEach( () => {
- root = doc.createRoot();
- } );
- describe( 'change attribute on node', () => {
- let node, text;
- beforeEach( () => {
- node = createElement( 'p', { a: 1 } );
- text = createText( 'c', { a: 1 } );
- append( node, root );
- append( text, root );
- spy = sinon.spy( model, 'applyOperation' );
- } );
- describe( 'setAttribute', () => {
- it( 'should create the attribute on element', () => {
- setAttribute( 'b', 2, node );
- expect( spy.callCount ).to.equal( 1 );
- expect( node.getAttribute( 'b' ) ).to.equal( 2 );
- } );
- it( 'should change the attribute of element', () => {
- setAttribute( 'a', 2, node );
- expect( spy.callCount ).to.equal( 1 );
- expect( node.getAttribute( 'a' ) ).to.equal( 2 );
- } );
- it( 'should create the attribute on text node', () => {
- setAttribute( 'b', 2, text );
- expect( spy.callCount ).to.equal( 1 );
- expect( root.getChild( 1 ).getAttribute( 'b' ) ).to.equal( 2 );
- } );
- it( 'should change the attribute of text node', () => {
- setAttribute( 'a', 2, text );
- expect( spy.callCount ).to.equal( 1 );
- expect( root.getChild( 1 ).getAttribute( 'a' ) ).to.equal( 2 );
- } );
- it( 'should do nothing if the attribute value is the same', () => {
- setAttribute( 'a', 1, node );
- expect( spy.callCount ).to.equal( 0 );
- expect( node.getAttribute( 'a' ) ).to.equal( 1 );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const writer = new Writer( model, batch );
- expect( () => {
- writer.setAttribute( 'a', 1, node );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- describe( 'removeAttribute', () => {
- it( 'should remove the attribute from element', () => {
- removeAttribute( 'a', node );
- expect( spy.callCount ).to.equal( 1 );
- expect( node.getAttribute( 'a' ) ).to.be.undefined;
- } );
- it( 'should remove the attribute from character', () => {
- removeAttribute( 'a', text );
- expect( spy.callCount ).to.equal( 1 );
- expect( root.getChild( 1 ).getAttribute( 'a' ) ).to.be.undefined;
- } );
- it( 'should do nothing if the attribute is not set', () => {
- removeAttribute( 'b', node );
- expect( spy.callCount ).to.equal( 0 );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const writer = new Writer( model, batch );
- expect( () => {
- writer.removeAttribute( 'b', node );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- } );
- describe( 'change attribute on range', () => {
- beforeEach( () => {
- const element = createElement( 'e', { a: 2 } );
- appendText( 'xxx', { a: 1 }, root );
- appendText( 'xxx', root );
- appendText( 'xxx', { a: 1 }, root );
- appendText( 'xxx', { a: 2 }, root );
- appendText( 'xxx', root );
- appendText( 'xxx', { a: 1 }, root );
- appendText( 'xxx', element );
- append( element, root );
- appendText( 'xxx', root );
- spy = sinon.spy( model, 'applyOperation' );
- } );
- function getRange( startIndex, endIndex ) {
- return new Range(
- Position.createFromParentAndOffset( root, startIndex ),
- Position.createFromParentAndOffset( root, endIndex )
- );
- }
- function getChangesAttrsCount() {
- let totalNumber = 0;
- for ( const delta of batch.deltas ) {
- for ( const operation of delta.operations ) {
- if ( operation.range ) {
- totalNumber += count( operation.range.getItems( { singleCharacters: true } ) );
- }
- }
- }
- return totalNumber;
- }
- function getCompressedAttrs() {
- // default: 111---111222---1112------
- const range = Range.createIn( root );
- return Array.from( range.getItems( { singleCharacters: true } ) )
- .map( item => item.getAttribute( 'a' ) || '-' )
- .join( '' );
- }
- describe( 'setAttribute', () => {
- it( 'should set the attribute on the range', () => {
- setAttribute( 'a', 3, getRange( 3, 6 ) );
- expect( spy.callCount ).to.equal( 1 );
- expect( getChangesAttrsCount() ).to.equal( 3 );
- expect( getCompressedAttrs() ).to.equal( '111333111222---1112------' );
- } );
- it( 'should split the operations if parts of the range have different attributes', () => {
- setAttribute( 'a', 3, getRange( 4, 14 ) );
- expect( spy.callCount ).to.equal( 4 );
- expect( getChangesAttrsCount() ).to.equal( 10 );
- expect( getCompressedAttrs() ).to.equal( '111-3333333333-1112------' );
- } );
- it( 'should split the operations if parts of the part of the range have the attribute', () => {
- setAttribute( 'a', 2, getRange( 4, 14 ) );
- expect( spy.callCount ).to.equal( 3 );
- expect( getChangesAttrsCount() ).to.equal( 7 );
- expect( getCompressedAttrs() ).to.equal( '111-2222222222-1112------' );
- } );
- it( 'should strip the range if the beginning have the attribute', () => {
- setAttribute( 'a', 1, getRange( 1, 5 ) );
- expect( spy.callCount ).to.equal( 1 );
- expect( getChangesAttrsCount() ).to.equal( 2 );
- expect( getCompressedAttrs() ).to.equal( '11111-111222---1112------' );
- } );
- it( 'should strip the range if the ending have the attribute', () => {
- setAttribute( 'a', 1, getRange( 13, 17 ) );
- expect( spy.callCount ).to.equal( 1 );
- expect( getChangesAttrsCount() ).to.equal( 2 );
- expect( getCompressedAttrs() ).to.equal( '111---111222-111112------' );
- } );
- it( 'should do nothing if the range has attribute', () => {
- setAttribute( 'a', 1, getRange( 0, 3 ) );
- expect( spy.callCount ).to.equal( 0 );
- expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
- } );
- it( 'should not check range\'s start position node when creating operations', () => {
- const range = new Range(
- new Position( root, [ 18, 1 ] ),
- new Position( root, [ 19 ] )
- );
- setAttribute( 'a', 1, range );
- expect( spy.callCount ).to.equal( 1 );
- expect( getChangesAttrsCount() ).to.equal( 2 );
- expect( getCompressedAttrs() ).to.equal( '111---111222---1112-11---' );
- } );
- it( 'should not change elements attribute if range contains closing tag', () => {
- const range = new Range(
- new Position( root, [ 18, 1 ] ),
- new Position( root, [ 21 ] )
- );
- setAttribute( 'a', 1, range );
- expect( spy.callCount ).to.equal( 1 );
- expect( getChangesAttrsCount() ).to.equal( 4 );
- expect( getCompressedAttrs() ).to.equal( '111---111222---1112-1111-' );
- } );
- it( 'should not create an operation if the range contains only closing tag', () => {
- const range = new Range(
- new Position( root, [ 18, 3 ] ),
- new Position( root, [ 19 ] )
- );
- setAttribute( 'a', 3, range );
- expect( spy.callCount ).to.equal( 0 );
- expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
- } );
- it( 'should not create an operation if is collapsed', () => {
- setAttribute( 'a', 1, getRange( 3, 3 ) );
- expect( spy.callCount ).to.equal( 0 );
- expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
- } );
- it( 'should create a proper operations for the mixed range', () => {
- setAttribute( 'a', 1, getRange( 0, 20 ) );
- expect( spy.callCount ).to.equal( 5 );
- expect( getChangesAttrsCount() ).to.equal( 14 );
- expect( getCompressedAttrs() ).to.equal( '11111111111111111111111--' );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const writer = new Writer( model, batch );
- expect( () => {
- writer.setAttribute( 'a', 1, getRange( 0, 20 ) );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- describe( 'removeAttribute', () => {
- it( 'should remove the attribute on the range', () => {
- removeAttribute( 'a', getRange( 0, 2 ) );
- expect( spy.callCount ).to.equal( 1 );
- expect( getChangesAttrsCount() ).to.equal( 2 );
- expect( getCompressedAttrs() ).to.equal( '--1---111222---1112------' );
- } );
- it( 'should split the operations if parts of the range have different attributes', () => {
- removeAttribute( 'a', getRange( 7, 11 ) );
- expect( spy.callCount ).to.equal( 2 );
- expect( getChangesAttrsCount() ).to.equal( 4 );
- expect( getCompressedAttrs() ).to.equal( '111---1----2---1112------' );
- } );
- it( 'should split the operations if parts of the part of the range have no attribute', () => {
- removeAttribute( 'a', getRange( 1, 7 ) );
- expect( spy.callCount ).to.equal( 2 );
- expect( getChangesAttrsCount() ).to.equal( 3 );
- expect( getCompressedAttrs() ).to.equal( '1------11222---1112------' );
- } );
- it( 'should strip the range if the beginning have no attribute', () => {
- removeAttribute( 'a', getRange( 4, 12 ) );
- expect( spy.callCount ).to.equal( 2 );
- expect( getChangesAttrsCount() ).to.equal( 6 );
- expect( getCompressedAttrs() ).to.equal( '111------------1112------' );
- } );
- it( 'should strip the range if the ending have no attribute', () => {
- removeAttribute( 'a', getRange( 7, 15 ) );
- expect( spy.callCount ).to.equal( 2 );
- expect( getChangesAttrsCount() ).to.equal( 5 );
- expect( getCompressedAttrs() ).to.equal( '111---1--------1112------' );
- } );
- it( 'should do nothing if the range has no attribute', () => {
- removeAttribute( 'a', getRange( 4, 5 ) );
- expect( spy.callCount ).to.equal( 0 );
- expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
- } );
- it( 'should not check range\'s start position node when creating operations', () => {
- const range = new Range(
- new Position( root, [ 18, 3 ] ),
- new Position( root, [ 19 ] )
- );
- removeAttribute( 'a', range );
- expect( spy.callCount ).to.equal( 0 );
- expect( getChangesAttrsCount() ).to.equal( 0 );
- expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
- } );
- it( 'should not apply operation twice in the range contains opening and closing tags', () => {
- removeAttribute( 'a', getRange( 18, 22 ) );
- expect( spy.callCount ).to.equal( 1 );
- expect( getChangesAttrsCount() ).to.equal( 1 );
- expect( getCompressedAttrs() ).to.equal( '111---111222---111-------' );
- } );
- it( 'should not create an operation if range is collapsed', () => {
- removeAttribute( 'a', getRange( 3, 3 ) );
- expect( spy.callCount ).to.equal( 0 );
- expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
- } );
- it( 'should create a proper operations for the mixed range', () => {
- removeAttribute( 'a', getRange( 3, 15 ) );
- expect( spy.callCount ).to.equal( 2 );
- expect( getChangesAttrsCount() ).to.equal( 6 );
- expect( getCompressedAttrs() ).to.equal( '111------------1112------' );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const writer = new Writer( model, batch );
- expect( () => {
- writer.removeAttribute( 'a', getRange( 3, 15 ) );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- } );
- describe( 'change attribute on root element', () => {
- let p;
- beforeEach( () => {
- p = createElement( 'p', { a: 3 } );
- spy = sinon.spy( model, 'applyOperation' );
- } );
- describe( 'setAttribute', () => {
- it( 'should create the attribute on root', () => {
- setAttribute( 'b', 2, root );
- expect( spy.callCount ).to.equal( 1 );
- expect( root.getAttribute( 'b' ) ).to.equal( 2 );
- } );
- it( 'should create the attribute on detached root', () => {
- setAttribute( 'b', 2, p );
- expect( spy.callCount ).to.equal( 1 );
- expect( p.getAttribute( 'b' ) ).to.equal( 2 );
- } );
- it( 'should change the attribute of root', () => {
- setAttribute( 'a', 2, root );
- expect( spy.callCount ).to.equal( 1 );
- expect( root.getAttribute( 'a' ) ).to.equal( 2 );
- } );
- it( 'should change the attribute of detached root', () => {
- setAttribute( 'a', 2, p );
- expect( spy.callCount ).to.equal( 1 );
- expect( p.getAttribute( 'a' ) ).to.equal( 2 );
- } );
- it( 'should do nothing if the attribute value is the same', () => {
- setAttribute( 'a', 1, root );
- expect( spy.callCount ).to.equal( 1 );
- setAttribute( 'a', 1, root );
- expect( spy.callCount ).to.equal( 1 );
- expect( root.getAttribute( 'a' ) ).to.equal( 1 );
- } );
- it( 'should do nothing if the attribute value is the same on detached root', () => {
- setAttribute( 'a', 1, p );
- expect( spy.callCount ).to.equal( 1 );
- setAttribute( 'a', 1, p );
- expect( spy.callCount ).to.equal( 1 );
- expect( p.getAttribute( 'a' ) ).to.equal( 1 );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const writer = new Writer( model, batch );
- expect( () => {
- writer.setAttribute( 'a', 1, p );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- describe( 'removeAttribute', () => {
- it( 'should remove the attribute from root', () => {
- setAttribute( 'a', 1, root );
- removeAttribute( 'a', root );
- expect( spy.callCount ).to.equal( 2 );
- expect( root.getAttribute( 'a' ) ).to.be.undefined;
- } );
- it( 'should do nothing if the attribute is not set', () => {
- removeAttribute( 'b', root );
- expect( spy.callCount ).to.equal( 0 );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const writer = new Writer( model, batch );
- expect( () => {
- writer.removeAttribute( 'b', root );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- describe( 'clearAttributes', () => {
- it( 'should clear attributes from range', () => {
- appendText( 'xxx', { a: 1, b: 2, c: 3 }, root );
- appendText( 'xxx', root );
- appendText( 'xxx', { a: 1 }, root );
- appendText( 'xxx', { b: 2 }, root );
- appendText( 'xxx', root );
- appendElement( 'e', { a: 1 }, root );
- appendText( 'xxx', root );
- const range = Range.createIn( root );
- clearAttributes( range );
- let itemsCount = 0;
- for ( const item of range.getItems() ) {
- itemsCount++;
- expect( Array.from( item.getAttributeKeys() ).length ).to.equal( 0 );
- }
- expect( itemsCount ).to.equal( 3 );
- } );
- it( 'should clear attributes on element', () => {
- const element = createElement( 'x', { a: 1, b: 2, c: 3 }, root );
- expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 3 );
- clearAttributes( element );
- expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 0 );
- } );
- it( 'should clear attributes on root element', () => {
- setAttributes( { a: 1, b: 2, c: 3 }, root );
- expect( Array.from( root.getAttributeKeys() ).length ).to.equal( 3 );
- clearAttributes( root );
- expect( Array.from( root.getAttributeKeys() ).length ).to.equal( 0 );
- } );
- it( 'should do nothing if there are no attributes', () => {
- const element = createElement( 'x' );
- expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 0 );
- clearAttributes( element );
- expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 0 );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const writer = new Writer( model, batch );
- const element = createElement( 'x' );
- expect( () => {
- writer.clearAttributes( element );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- } );
- it( 'should not add empty delta to the batch', () => {
- const nodeA = new Element( 'p', { a: 1 } );
- const nodeB = new Element( 'p', { b: 2 } );
- root._insertChild( 0, [ nodeA, nodeB ] );
- setAttribute( 'a', 1, nodeA );
- expect( batch.deltas.length ).to.equal( 0 );
- removeAttribute( 'x', Range.createIn( root ) );
- expect( batch.deltas.length ).to.equal( 0 );
- } );
- } );
- describe( 'setAttributes()', () => {
- let frag, item;
- beforeEach( () => {
- frag = createDocumentFragment();
- item = createText( 'xxx', { b: 2, c: 3 } );
- appendText( 'xxx', { a: 1 }, frag );
- append( item, frag );
- } );
- it( 'should set attributes one by one on range', () => {
- const range = Range.createIn( frag );
- let spy;
- model.change( writer => {
- // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
- // such a big amount of the same tests, so let's use a spy here.
- spy = sinon.spy( writer, 'setAttribute' );
- writer.setAttributes( { a: 3, c: null }, range );
- } );
- // Verify result.
- expect( Array.from( frag.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'a', 3 ] ] );
- expect( Array.from( frag.getChild( 1 ).getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
- // Verify operations
- sinon.assert.calledTwice( spy );
- sinon.assert.calledWith( spy.firstCall, 'a', 3, range );
- sinon.assert.calledWith( spy.secondCall, 'c', null, range );
- } );
- it( 'should set attributes one by one on range for map as attributes list', () => {
- const range = Range.createIn( frag );
- let spy;
- model.change( writer => {
- // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
- // such a big amount of the same tests, so let's use a spy here.
- spy = sinon.spy( writer, 'setAttribute' );
- writer.setAttributes( new Map( [ [ 'a', 3 ], [ 'c', null ] ] ), range );
- } );
- // Verify result.
- expect( Array.from( frag.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'a', 3 ] ] );
- expect( Array.from( frag.getChild( 1 ).getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
- // Verify operations
- sinon.assert.calledTwice( spy );
- sinon.assert.calledWith( spy.firstCall, 'a', 3, range );
- sinon.assert.calledWith( spy.secondCall, 'c', null, range );
- } );
- it( 'should set attributes one by one on item', () => {
- let spy;
- model.change( writer => {
- // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
- // such a big amount of the same tests, so let's use a spy here.
- spy = sinon.spy( writer, 'setAttribute' );
- writer.setAttributes( { a: 3, c: null }, item );
- } );
- // Verify result.
- expect( Array.from( item.getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
- // Verify operations
- sinon.assert.calledTwice( spy );
- sinon.assert.calledWith( spy.firstCall, 'a', 3, item );
- sinon.assert.calledWith( spy.secondCall, 'c', null, item );
- } );
- it( 'should set attributes one by one on item for maps as attributes list', () => {
- let spy;
- model.change( writer => {
- // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
- // such a big amount of the same tests, so let's use a spy here.
- spy = sinon.spy( writer, 'setAttribute' );
- writer.setAttributes( new Map( [ [ 'a', 3 ], [ 'c', null ] ] ), item );
- } );
- // Verify result.
- expect( Array.from( item.getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
- // Verify operations
- sinon.assert.calledTwice( spy );
- sinon.assert.calledWith( spy.firstCall, 'a', 3, item );
- sinon.assert.calledWith( spy.secondCall, 'c', null, item );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const writer = new Writer( model, batch );
- expect( () => {
- writer.setAttributes( new Map( [ [ 'a', 3 ], [ 'c', null ] ] ), item );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- describe( 'merge()', () => {
- let root, p1, p2;
- beforeEach( () => {
- root = doc.createRoot();
- p1 = new Element( 'p', { key1: 'value1' }, new Text( 'foo' ) );
- p2 = new Element( 'p', { key2: 'value2' }, new Text( 'bar' ) );
- root._insertChild( 0, [ p1, p2 ] );
- } );
- it( 'should merge foo and bar into foobar', () => {
- merge( new Position( root, [ 1 ] ) );
- expect( root.maxOffset ).to.equal( 1 );
- expect( root.getChild( 0 ).name ).to.equal( 'p' );
- expect( root.getChild( 0 ).maxOffset ).to.equal( 6 );
- expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
- expect( root.getChild( 0 ).getAttribute( 'key1' ) ).to.equal( 'value1' );
- expect( root.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foobar' );
- } );
- it( 'should correctly merge in document fragment', () => {
- const docFrag = new DocumentFragment( [
- new Element( 'p', null, 'foo' ),
- new Element( 'p', null, 'bar' )
- ] );
- merge( new Position( docFrag, [ 1 ] ) );
- expect( docFrag.getChild( 0 ).name ).to.equal( 'p' );
- expect( docFrag.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foobar' );
- } );
- it( 'should throw if there is no element after', () => {
- expect( () => {
- merge( new Position( root, [ 2 ] ) );
- } ).to.throw( CKEditorError, /^writer-merge-no-element-after/ );
- } );
- it( 'should throw if there is no element before', () => {
- expect( () => {
- merge( new Position( root, [ 0, 2 ] ) );
- } ).to.throw( CKEditorError, /^writer-merge-no-element-before/ );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const writer = new Writer( model, batch );
- expect( () => {
- writer.merge( new Position( root, [ 1 ] ) );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- describe( 'move()', () => {
- let root, range, div, p;
- beforeEach( () => {
- root = doc.createRoot();
- div = new Element( 'div', [], new Text( 'foobar' ) );
- p = new Element( 'p', [], new Text( 'abcxyz' ) );
- div._insertChild( 0, [ new Element( 'p', [], new Text( 'gggg' ) ) ] );
- div._insertChild( 2, [ new Element( 'p', [], new Text( 'hhhh' ) ) ] );
- root._insertChild( 0, [ div, p ] );
- range = new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 0, 7 ] ) );
- } );
- it( 'should move flat range of nodes', () => {
- move( range, new Position( root, [ 1, 3 ] ) );
- expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'PggggPfoPhhhhP' );
- expect( getNodesAndText( Range.createIn( root.getChild( 1 ) ) ) ).to.equal( 'abcobarxyz' );
- } );
- it( 'should throw if object to move is not a range', () => {
- expect( () => {
- move( root.getChild( 0 ), new Position( root, [ 1, 3 ] ) );
- } ).to.throw( CKEditorError, /^writer-move-invalid-range/ );
- } );
- it( 'should throw if given range is not flat', () => {
- const notFlatRange = new Range( new Position( root, [ 0, 2, 2 ] ), new Position( root, [ 0, 6 ] ) );
- expect( () => {
- move( notFlatRange, new Position( root, [ 1, 3 ] ) );
- } ).to.throw( CKEditorError, /^writer-move-range-not-flat/ );
- } );
- it( 'should throw if range is going to be moved to the other document', () => {
- const docFrag = createDocumentFragment();
- expect( () => {
- move( range, docFrag );
- } ).to.throw( CKEditorError, /^writer-move-different-document/ );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const writer = new Writer( model, batch );
- expect( () => {
- writer.move( range, new Position( root, [ 1, 3 ] ) );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- describe( 'remove()', () => {
- let div, p, range;
- beforeEach( () => {
- div = createElement( 'div' );
- appendText( 'foobar', div );
- p = createElement( 'p' );
- appendText( 'abcxyz', p );
- insertElement( 'p', div );
- appendElement( 'p', div );
- insertText( 'gggg', new Position( div, [ 0, 0 ] ) );
- insertText( 'hhhh', new Position( div, [ 7, 0 ] ) );
- } );
- describe( 'remove from document', () => {
- let root;
- beforeEach( () => {
- root = doc.createRoot();
- append( div, root );
- append( p, root );
- // Range starts in ROOT > DIV > P > gg|gg.
- // Range ends in ROOT > DIV > ...|ar.
- range = new Range( new Position( root, [ 0, 0, 2 ] ), new Position( root, [ 0, 5 ] ) );
- } );
- it( 'should remove specified node', () => {
- remove( div );
- expect( root.maxOffset ).to.equal( 1 );
- expect( root.childCount ).to.equal( 1 );
- expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'abcxyz' );
- } );
- it( 'should remove specified text node', () => {
- remove( p.getChild( 0 ) );
- expect( getNodesAndText( Range.createOn( p ) ) ).to.equal( 'PP' );
- } );
- it( 'should remove any range of nodes', () => {
- remove( range );
- expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'PggParPhhhhP' );
- expect( getNodesAndText( Range.createIn( root.getChild( 1 ) ) ) ).to.equal( 'abcxyz' );
- } );
- it( 'should create minimal number of remove deltas, each with only one operation', () => {
- batch = new Batch();
- remove( range );
- expect( batch.deltas.length ).to.equal( 2 );
- expect( batch.deltas[ 0 ].operations.length ).to.equal( 1 );
- expect( batch.deltas[ 1 ].operations.length ).to.equal( 1 );
- } );
- it( 'should use RemoveOperation', () => {
- batch = new Batch();
- remove( div );
- expect( batch.deltas[ 0 ].operations[ 0 ].type ).to.equal( 'remove' );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const writer = new Writer( model, batch );
- expect( () => {
- writer.remove( range );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- describe( 'remove from document fragment', () => {
- let frag;
- beforeEach( () => {
- frag = createDocumentFragment();
- append( div, frag );
- append( p, frag );
- // Range starts in FRAG > DIV > P > gg|gg.
- // Range ends in FRAG > DIV > ...|ar.
- range = new Range( new Position( frag, [ 0, 0, 2 ] ), new Position( frag, [ 0, 5 ] ) );
- } );
- it( 'should remove specified node', () => {
- remove( div );
- expect( frag.maxOffset ).to.equal( 1 );
- expect( frag.childCount ).to.equal( 1 );
- expect( getNodesAndText( Range.createIn( frag.getChild( 0 ) ) ) ).to.equal( 'abcxyz' );
- } );
- it( 'should remove specified text node', () => {
- remove( p.getChild( 0 ) );
- expect( getNodesAndText( Range.createOn( p ) ) ).to.equal( 'PP' );
- } );
- it( 'should remove any range of nodes', () => {
- remove( range );
- expect( getNodesAndText( Range.createIn( frag.getChild( 0 ) ) ) ).to.equal( 'PggParPhhhhP' );
- expect( getNodesAndText( Range.createIn( frag.getChild( 1 ) ) ) ).to.equal( 'abcxyz' );
- } );
- it( 'should create minimal number of remove deltas, each with only one operation', () => {
- batch = new Batch();
- remove( range );
- expect( batch.deltas.length ).to.equal( 2 );
- expect( batch.deltas[ 0 ].operations.length ).to.equal( 1 );
- expect( batch.deltas[ 1 ].operations.length ).to.equal( 1 );
- } );
- it( 'should use DetachOperation', () => {
- batch = new Batch();
- remove( div );
- expect( batch.deltas[ 0 ].operations[ 0 ].type ).to.equal( 'detach' );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const writer = new Writer( model, batch );
- expect( () => {
- writer.remove( range );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- } );
- describe( 'rename()', () => {
- it( 'should rename given element', () => {
- const root = doc.createRoot();
- const p = new Element( 'p', null, new Text( 'abc' ) );
- root._appendChild( p );
- rename( p, 'h' );
- expect( root.maxOffset ).to.equal( 1 );
- expect( root.getChild( 0 ) ).to.have.property( 'name', 'h' );
- } );
- it( 'should rename in document fragment', () => {
- const docFrag = new DocumentFragment();
- const p = new Element( 'p' );
- docFrag._appendChild( p );
- rename( p, 'h' );
- expect( docFrag.maxOffset ).to.equal( 1 );
- expect( docFrag.getChild( 0 ) ).to.have.property( 'name', 'h' );
- } );
- it( 'should throw if not an Element instance is passed', () => {
- expect( () => {
- rename( new Text( 'abc' ), 'h' );
- } ).to.throw( CKEditorError, /^writer-rename-not-element-instance/ );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const writer = new Writer( model, batch );
- const p = new Element( 'p', null, new Text( 'abc' ) );
- expect( () => {
- writer.rename( p, 'h' );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- describe( 'split()', () => {
- let root, p;
- beforeEach( () => {
- root = doc.createRoot();
- p = new Element( 'p', { key: 'value' }, new Text( 'foobar' ) );
- root._insertChild( 0, p );
- } );
- it( 'should split foobar to foo and bar', () => {
- split( new Position( root, [ 0, 3 ] ) );
- expect( root.maxOffset ).to.equal( 2 );
- expect( root.getChild( 0 ).name ).to.equal( 'p' );
- expect( root.getChild( 0 ).maxOffset ).to.equal( 3 );
- expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
- expect( root.getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
- expect( root.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
- expect( root.getChild( 1 ).name ).to.equal( 'p' );
- expect( root.getChild( 1 ).maxOffset ).to.equal( 3 );
- expect( count( root.getChild( 1 ).getAttributes() ) ).to.equal( 1 );
- expect( root.getChild( 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
- expect( root.getChild( 1 ).getChild( 0 ).data ).to.equal( 'bar' );
- } );
- it( 'should split inside document fragment', () => {
- const docFrag = new DocumentFragment();
- docFrag._appendChild( new Element( 'p', null, new Text( 'foobar' ) ) );
- split( new Position( docFrag, [ 0, 3 ] ) );
- expect( docFrag.maxOffset ).to.equal( 2 );
- expect( docFrag.getChild( 0 ).name ).to.equal( 'p' );
- expect( docFrag.getChild( 0 ).maxOffset ).to.equal( 3 );
- expect( docFrag.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
- expect( docFrag.getChild( 1 ).name ).to.equal( 'p' );
- expect( docFrag.getChild( 1 ).maxOffset ).to.equal( 3 );
- expect( docFrag.getChild( 1 ).getChild( 0 ).data ).to.equal( 'bar' );
- } );
- it( 'should create an empty paragraph if we split at the end', () => {
- split( new Position( root, [ 0, 6 ] ) );
- expect( root.maxOffset ).to.equal( 2 );
- expect( root.getChild( 0 ).name ).to.equal( 'p' );
- expect( root.getChild( 0 ).maxOffset ).to.equal( 6 );
- expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
- expect( root.getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
- expect( root.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foobar' );
- expect( root.getChild( 1 ).name ).to.equal( 'p' );
- expect( root.getChild( 1 ).maxOffset ).to.equal( 0 );
- expect( count( root.getChild( 1 ).getAttributes() ) ).to.equal( 1 );
- expect( root.getChild( 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
- } );
- it( 'should throw if we try to split a root', () => {
- expect( () => {
- split( new Position( root, [ 0 ] ) );
- } ).to.throw( CKEditorError, /^writer-split-element-no-parent/ );
- } );
- it( 'should throw if we try to split an element with no parent', () => {
- expect( () => {
- const element = createElement( 'p' );
- split( new Position( element, [ 0 ] ) );
- } ).to.throw( CKEditorError, /^writer-split-element-no-parent/ );
- } );
- it( 'should throw if we try to split a document fragment', () => {
- expect( () => {
- const documentFragment = createDocumentFragment();
- split( new Position( documentFragment, [ 0 ] ) );
- } ).to.throw( CKEditorError, /^writer-split-element-no-parent/ );
- } );
- it( 'should split elements to limitElement', () => {
- const div = new Element( 'div', null, p );
- const section = new Element( 'section', null, div );
- root._insertChild( 0, section );
- split( new Position( p, [ 3 ] ), section );
- expect( root.maxOffset ).to.equal( 1 );
- expect( section.maxOffset ).to.equal( 2 );
- expect( section.getChild( 0 ).name ).to.equal( 'div' );
- expect( section.getChild( 0 ).getChild( 0 ).name ).to.equal( 'p' );
- expect( section.getChild( 0 ).getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
- expect( count( section.getChild( 0 ).getChild( 0 ).getAttributes() ) ).to.equal( 1 );
- expect( section.getChild( 0 ).getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
- expect( section.getChild( 1 ).name ).to.equal( 'div' );
- expect( section.getChild( 1 ).getChild( 0 ).name ).to.equal( 'p' );
- expect( section.getChild( 1 ).getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
- expect( count( section.getChild( 1 ).getChild( 0 ).getAttributes() ) ).to.equal( 1 );
- expect( section.getChild( 1 ).getChild( 0 ).getChild( 0 ).data ).to.equal( 'bar' );
- } );
- it( 'should throw when limitElement is not a position ancestor', () => {
- const div = new Element( 'div', null, p );
- const section = new Element( 'section', null, div );
- root._insertChild( 0, div );
- root._insertChild( 1, section );
- expect( () => {
- split( new Position( p, [ 3 ] ), section );
- } ).to.throw( CKEditorError, /^writer-split-invalid-limit-element/ );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const writer = new Writer( model, batch );
- expect( () => {
- writer.split( new Position( root, [ 0, 3 ] ) );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- describe( 'wrap()', () => {
- let root, range;
- beforeEach( () => {
- root = doc.createRoot();
- root._insertChild( 0, new Text( 'foobar' ) );
- range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
- } );
- it( 'should wrap flat range with given element', () => {
- const p = new Element( 'p' );
- wrap( range, p );
- expect( root.maxOffset ).to.equal( 5 );
- expect( root.getChild( 0 ).data ).to.equal( 'fo' );
- expect( root.getChild( 1 ) ).to.equal( p );
- expect( p.getChild( 0 ).data ).to.equal( 'ob' );
- expect( root.getChild( 2 ).data ).to.equal( 'ar' );
- } );
- it( 'should wrap flat range with an element of given name', () => {
- wrap( range, 'p' );
- expect( root.maxOffset ).to.equal( 5 );
- expect( root.getChild( 0 ).data ).to.equal( 'fo' );
- expect( root.getChild( 1 ).name ).to.equal( 'p' );
- expect( root.getChild( 1 ).getChild( 0 ).data ).to.equal( 'ob' );
- expect( root.getChild( 2 ).data ).to.equal( 'ar' );
- } );
- it( 'should wrap inside document fragment', () => {
- const docFrag = new DocumentFragment( new Text( 'foo' ) );
- wrap( Range.createIn( docFrag ), 'p' );
- expect( docFrag.maxOffset ).to.equal( 1 );
- expect( docFrag.getChild( 0 ).name ).to.equal( 'p' );
- expect( docFrag.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
- } );
- it( 'should throw if range to wrap is not flat', () => {
- root._insertChild( 1, [ new Element( 'p', [], new Text( 'xyz' ) ) ] );
- const notFlatRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 6, 2 ] ) );
- expect( () => {
- wrap( notFlatRange, 'p' );
- } ).to.throw( CKEditorError, /^writer-wrap-range-not-flat/ );
- } );
- it( 'should throw if element to wrap with has children #1', () => {
- const p = new Element( 'p', [], new Text( 'a' ) );
- expect( () => {
- wrap( range, p );
- } ).to.throw( CKEditorError, /^writer-wrap-element-not-empty/ );
- } );
- it( 'should throw if element to wrap with has children #2', () => {
- const p = new Element( 'p' );
- root._insertChild( 0, p );
- expect( () => {
- wrap( range, p );
- } ).to.throw( CKEditorError, /^writer-wrap-element-attached/ );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const writer = new Writer( model, batch );
- expect( () => {
- writer.wrap( range, 'p' );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- describe( 'unwrap()', () => {
- let root, p;
- beforeEach( () => {
- root = doc.createRoot();
- p = new Element( 'p', [], new Text( 'xyz' ) );
- root._insertChild( 0, [ new Text( 'a' ), p, new Text( 'b' ) ] );
- } );
- it( 'should unwrap given element', () => {
- unwrap( p );
- expect( root.maxOffset ).to.equal( 5 );
- expect( root.getChild( 0 ).data ).to.equal( 'axyzb' );
- } );
- it( 'should unwrap inside document fragment', () => {
- const docFrag = new DocumentFragment( new Element( 'p', null, new Text( 'foo' ) ) );
- unwrap( docFrag.getChild( 0 ) );
- expect( docFrag.maxOffset ).to.equal( 3 );
- expect( docFrag.getChild( 0 ).data ).to.equal( 'foo' );
- } );
- it( 'should throw if element to unwrap has no parent', () => {
- const element = new Element( 'p' );
- expect( () => {
- unwrap( element );
- } ).to.throw( CKEditorError, /^writer-unwrap-element-no-parent/ );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const writer = new Writer( model, batch );
- expect( () => {
- writer.unwrap( p );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- describe( 'addMarker()', () => {
- let root, range;
- beforeEach( () => {
- root = doc.createRoot();
- root._appendChild( new Text( 'foo' ) );
- range = Range.createIn( root );
- } );
- it( 'should throw if options.usingOperations is not defined', () => {
- expect( () => {
- addMarker( 'name' );
- } ).to.throw( CKEditorError, /^writer-addMarker-no-usingOperations/ );
- } );
- it( 'should throw if name and range is defined but options.usingOperations is not defined', () => {
- expect( () => {
- addMarker( 'name', { range } );
- } ).to.throw( CKEditorError, /^writer-addMarker-no-usingOperations/ );
- } );
- it( 'should add marker to the document marker collection', () => {
- addMarker( 'name', { range, usingOperation: false } );
- expect( model.markers.get( 'name' ).getRange().isEqual( range ) ).to.be.true;
- } );
- it( 'should return marker if that was set directly', () => {
- const marker = addMarker( 'name', { range, usingOperation: false } );
- expect( model.markers.get( 'name' ) ).to.equal( marker );
- } );
- it( 'should return marker if that was set using operation API', () => {
- const marker = addMarker( 'name', { range, usingOperation: true } );
- expect( model.markers.get( 'name' ) ).to.equal( marker );
- } );
- it( 'should return marker with properly set managedUsingOperations (to true)', () => {
- const marker = addMarker( 'name', { range, usingOperation: true } );
- expect( marker.managedUsingOperations ).to.equal( true );
- } );
- it( 'should return marker with properly set managedUsingOperations (to false)', () => {
- const marker = addMarker( 'name', { range, usingOperation: false } );
- expect( marker.managedUsingOperations ).to.equal( false );
- } );
- it( 'should throw when trying to update existing marker in the document marker collection', () => {
- addMarker( 'name', { range, usingOperation: false } );
- const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
- expect( () => {
- addMarker( 'name', { range: range2, usingOperation: false } );
- } ).to.throw( CKEditorError, /^writer-addMarker-marker-exists/ );
- } );
- it( 'should use operations when having set usingOperation to true', () => {
- const spy = sinon.spy();
- model.on( 'applyOperation', spy );
- addMarker( 'name', { range, usingOperation: true } );
- const op = batch.deltas[ 0 ].operations[ 0 ];
- sinon.assert.calledOnce( spy );
- expect( spy.firstCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
- expect( op.oldRange ).to.be.null;
- expect( op.newRange.isEqual( range ) ).to.be.true;
- } );
- it( 'should throw if marker with given name does not exist and range is not passed', () => {
- expect( () => {
- addMarker( 'name', { usingOperation: true } );
- } ).to.throw( CKEditorError, /^writer-addMarker-no-range/ );
- } );
- it( 'should throw if marker is set directly and range is not passed', () => {
- expect( () => {
- addMarker( 'name', { usingOperation: false } );
- } ).to.throw( CKEditorError, /^writer-addMarker-no-range/ );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const marker = addMarker( 'name', { range, usingOperation: false } );
- const writer = new Writer( model, batch );
- expect( () => {
- writer.addMarker( marker, null, { usingOperation: true } );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- describe( 'updateMarker()', () => {
- let root, range;
- beforeEach( () => {
- root = doc.createRoot();
- root._appendChild( new Text( 'foo' ) );
- range = Range.createIn( root );
- } );
- it( 'should update managed marker\'s range by marker instance using operations', () => {
- const marker = addMarker( 'name', { range, usingOperation: true } );
- const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
- updateMarker( marker, { range: range2 } );
- expect( batch.deltas.length ).to.equal( 2 );
- const op = batch.deltas[ 1 ].operations[ 0 ];
- expect( marker.getRange().isEqual( range2 ) ).to.be.true;
- expect( op.oldRange.isEqual( range ) ).to.be.true;
- expect( op.newRange.isEqual( range2 ) ).to.be.true;
- } );
- it( 'should update managed marker\'s range by marker name using operations', () => {
- const marker = addMarker( 'name', { range, usingOperation: true } );
- const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
- updateMarker( 'name', { range: range2 } );
- expect( batch.deltas.length ).to.equal( 2 );
- const op = batch.deltas[ 1 ].operations[ 0 ];
- expect( marker.getRange().isEqual( range2 ) ).to.be.true;
- expect( op.oldRange.isEqual( range ) ).to.be.true;
- expect( op.newRange.isEqual( range2 ) ).to.be.true;
- } );
- it( 'should update managed marker\'s range by marker instance using operations and usingOperation explicitly passed', () => {
- const marker = addMarker( 'name', { range, usingOperation: true } );
- const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
- updateMarker( marker, { range: range2, usingOperation: true } );
- expect( batch.deltas.length ).to.equal( 2 );
- const op = batch.deltas[ 1 ].operations[ 0 ];
- expect( marker.getRange().isEqual( range2 ) ).to.be.true;
- expect( op.oldRange.isEqual( range ) ).to.be.true;
- expect( op.newRange.isEqual( range2 ) ).to.be.true;
- } );
- it( 'should update managed marker\'s range by marker name using operations and usingOperation explicitly passed', () => {
- const marker = addMarker( 'name', { range, usingOperation: true } );
- const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
- updateMarker( 'name', { range: range2, usingOperation: true } );
- expect( batch.deltas.length ).to.equal( 2 );
- const op = batch.deltas[ 1 ].operations[ 0 ];
- expect( marker.getRange().isEqual( range2 ) ).to.be.true;
- expect( op.oldRange.isEqual( range ) ).to.be.true;
- expect( op.newRange.isEqual( range2 ) ).to.be.true;
- } );
- it( 'should not use operations when updating marker which does not use operations', () => {
- const spy = sinon.spy();
- model.on( 'applyOperation', spy );
- const marker = addMarker( 'name', { range, usingOperation: false } );
- const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
- updateMarker( marker, { range: range2 } );
- sinon.assert.notCalled( spy );
- } );
- it( 'should create additional operation when marker type changes to not managed using operation', () => {
- const spy = sinon.spy();
- model.on( 'applyOperation', spy );
- addMarker( 'name', { range, usingOperation: true } );
- updateMarker( 'name', { usingOperation: false } );
- const marker = model.markers.get( 'name' );
- const op1 = batch.deltas[ 0 ].operations[ 0 ];
- const op2 = batch.deltas[ 1 ].operations[ 0 ];
- sinon.assert.calledTwice( spy );
- expect( spy.firstCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
- expect( spy.secondCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
- expect( op1.oldRange ).to.be.null;
- expect( op1.newRange.isEqual( range ) ).to.be.true;
- expect( op2.oldRange.isEqual( range ) ).to.be.true;
- expect( op2.newRange ).to.be.null;
- expect( marker.managedUsingOperations ).to.be.false;
- } );
- it( 'should create additional operation when marker type changes to not managed using operation and changing its range', () => {
- const spy = sinon.spy();
- model.on( 'applyOperation', spy );
- const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
- addMarker( 'name', { range, usingOperation: true } );
- updateMarker( 'name', { range: range2, usingOperation: false } );
- const marker = model.markers.get( 'name' );
- const op1 = batch.deltas[ 0 ].operations[ 0 ];
- const op2 = batch.deltas[ 1 ].operations[ 0 ];
- sinon.assert.calledTwice( spy );
- expect( spy.firstCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
- expect( spy.secondCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
- expect( op1.oldRange ).to.be.null;
- expect( op1.newRange.isEqual( range ) ).to.be.true;
- expect( op2.oldRange.isEqual( range ) ).to.be.true;
- expect( op2.newRange ).to.be.null;
- expect( marker.getRange().isEqual( range2 ) );
- expect( marker.managedUsingOperations ).to.be.false;
- } );
- it( 'should enable changing marker to be managed using operation', () => {
- const spy = sinon.spy();
- model.on( 'applyOperation', spy );
- addMarker( 'name', { range, usingOperation: false } );
- updateMarker( 'name', { usingOperation: true } );
- const marker = model.markers.get( 'name' );
- sinon.assert.calledOnce( spy );
- expect( spy.firstCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
- expect( marker.managedUsingOperations ).to.be.true;
- } );
- it( 'should enable changing marker to be managed using operation while changing range', () => {
- const spy = sinon.spy();
- model.on( 'applyOperation', spy );
- const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
- addMarker( 'name', { range, usingOperation: false } );
- updateMarker( 'name', { range: range2, usingOperation: true } );
- const marker = model.markers.get( 'name' );
- sinon.assert.calledOnce( spy );
- expect( spy.firstCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
- expect( marker.getRange().isEqual( range2 ) ).to.be.true;
- expect( marker.managedUsingOperations ).to.be.true;
- } );
- it( 'should throw when range and usingOperations were not provided', () => {
- expect( () => {
- addMarker( 'name', { range, usingOperation: false } );
- updateMarker( 'name' );
- } ).to.throw( CKEditorError, /^writer-updateMarker-wrong-options/ );
- } );
- it( 'should throw when marker provided by name does not exists', () => {
- expect( () => {
- updateMarker( 'name', { usingOperation: false } );
- } ).to.throw( CKEditorError, /^writer-updateMarker-marker-not-exists/ );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const marker = addMarker( 'name', { range, usingOperation: false } );
- const writer = new Writer( model, batch );
- expect( () => {
- writer.updateMarker( marker, { usingOperation: true } );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- } );
- describe( 'removeMarker()', () => {
- let root, range;
- beforeEach( () => {
- root = doc.createRoot();
- root._appendChild( new Text( 'foo' ) );
- range = Range.createIn( root );
- } );
- it( 'should remove marker from the document marker collection', () => {
- addMarker( 'name', { range, usingOperation: false } );
- removeMarker( 'name' );
- expect( model.markers.get( 'name' ) ).to.be.null;
- } );
- it( 'should throw when trying to remove non existing marker', () => {
- expect( () => {
- removeMarker( 'name' );
- } ).to.throw( CKEditorError, /^writer-removeMarker-no-marker/ );
- } );
- it( 'should throw when trying to use detached writer', () => {
- const writer = new Writer( model, batch );
- expect( () => {
- writer.removeMarker( 'name' );
- } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
- } );
- it( 'should accept marker instance', () => {
- addMarker( 'name', { range, usingOperation: false } );
- const marker = model.markers.get( 'name' );
- removeMarker( marker );
- expect( model.markers.get( 'name' ) ).to.be.null;
- } );
- it( 'should use MarkerOperation when marker was created using operation', () => {
- addMarker( 'name', { range, usingOperation: true } );
- const marker = model.markers.get( 'name' );
- const spy = sinon.spy();
- model.on( 'applyOperation', spy );
- removeMarker( marker );
- sinon.assert.calledOnce( spy );
- expect( spy.firstCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
- expect( model.markers.get( 'name' ) ).to.be.null;
- } );
- } );
- describe( 'setSelection()', () => {
- let root;
- beforeEach( () => {
- model.schema.register( 'p', { inheritAllFrom: '$block' } );
- model.schema.extend( 'p', { allowIn: '$root' } );
- root = doc.createRoot();
- root._appendChild( [
- new Element( 'p' ),
- new Element( 'p' ),
- new Element( 'p', [], new Text( 'foo' ) )
- ] );
- } );
- it( 'should use DocumentSelection#_setTo method', () => {
- const firstParagraph = root.getNodeByPath( [ 1 ] );
- const setToSpy = sinon.spy( DocumentSelection.prototype, '_setTo' );
- setSelection( firstParagraph, 0 );
- expect( setToSpy.calledOnce ).to.be.true;
- setToSpy.restore();
- } );
- it( 'should change document selection ranges', () => {
- const range = new Range( new Position( root, [ 1 ] ), new Position( root, [ 2, 2 ] ) );
- setSelection( range, { backward: true } );
- expect( model.document.selection._ranges.length ).to.equal( 1 );
- expect( model.document.selection._ranges[ 0 ].start.path ).to.deep.equal( [ 1 ] );
- expect( model.document.selection._ranges[ 0 ].end.path ).to.deep.equal( [ 2, 2 ] );
- expect( model.document.selection.isBackward ).to.be.true;
- } );
- } );
- describe( 'setSelectionFocus()', () => {
- let root;
- beforeEach( () => {
- model.schema.register( 'p', { inheritAllFrom: '$block' } );
- model.schema.extend( 'p', { allowIn: '$root' } );
- root = doc.createRoot();
- root._appendChild( [
- new Element( 'p' ),
- new Element( 'p' ),
- new Element( 'p', [], new Text( 'foo' ) )
- ] );
- } );
- it( 'should use DocumentSelection#_setFocus method', () => {
- const firstParagraph = root.getNodeByPath( [ 1 ] );
- const setFocusSpy = sinon.spy( DocumentSelection.prototype, '_setFocus' );
- setSelectionFocus( firstParagraph );
- expect( setFocusSpy.calledOnce ).to.be.true;
- setFocusSpy.restore();
- } );
- it( 'should change document selection ranges', () => {
- setSelection( new Position( root, [ 1 ] ) );
- setSelectionFocus( new Position( root, [ 2, 2 ] ) );
- expect( model.document.selection._ranges.length ).to.equal( 1 );
- expect( model.document.selection._ranges[ 0 ].start.path ).to.deep.equal( [ 1 ] );
- expect( model.document.selection._ranges[ 0 ].end.path ).to.deep.equal( [ 2, 2 ] );
- } );
- } );
- describe( 'setSelectionAttribute()', () => {
- const fooStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'foo' );
- let root, rangeInEmptyP, emptyP;
- beforeEach( () => {
- model.schema.register( 'p', { inheritAllFrom: '$block' } );
- model.schema.extend( 'p', { allowIn: '$root' } );
- root = doc.createRoot();
- root._appendChild( [
- new Element( 'p', [], [] ),
- new Element( 'p' ),
- new Element( 'p', [], new Text( 'foo' ) )
- ] );
- rangeInEmptyP = new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) );
- emptyP = root.getChild( 0 );
- } );
- it( 'should store attribute if the selection is in empty node', () => {
- setSelection( rangeInEmptyP );
- setSelectionAttribute( 'foo', 'bar' );
- expect( model.document.selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
- expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
- } );
- it( 'should be able to store attributes from the given object', () => {
- setSelection( rangeInEmptyP );
- setSelectionAttribute( { key1: 'foo', key2: 'bar' } );
- expect( model.document.selection.getAttribute( 'key1' ) ).to.equal( 'foo' );
- expect( model.document.selection.getAttribute( 'key2' ) ).to.equal( 'bar' );
- } );
- it( 'should be able to store attributes from the given iterable', () => {
- setSelection( rangeInEmptyP );
- setSelectionAttribute( new Map( [ [ 'key1', 'foo' ], [ 'key2', 'bar' ] ] ) );
- expect( model.document.selection.getAttribute( 'key1' ) ).to.equal( 'foo' );
- expect( model.document.selection.getAttribute( 'key2' ) ).to.equal( 'bar' );
- } );
- } );
- describe( 'removeSelectionAttribute()', () => {
- const fooStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'foo' );
- let root, rangeInEmptyP, emptyP;
- beforeEach( () => {
- model.schema.register( 'p', { inheritAllFrom: '$block' } );
- model.schema.extend( 'p', { allowIn: '$root' } );
- root = doc.createRoot();
- root._appendChild( [
- new Element( 'p', [], [] ),
- new Element( 'p' ),
- new Element( 'p', [], new Text( 'foo' ) )
- ] );
- rangeInEmptyP = new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) );
- emptyP = root.getChild( 0 );
- } );
- it( 'should remove stored attribute if the selection is in empty node', () => {
- setSelection( rangeInEmptyP );
- setSelectionAttribute( 'foo', 'bar' );
- removeSelectionAttribute( 'foo' );
- expect( model.document.selection.getAttribute( 'foo' ) ).to.be.undefined;
- expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
- } );
- it( 'should remove all attributes from the given iterable', () => {
- setSelection( rangeInEmptyP );
- setSelectionAttribute( 'foo', 'bar' );
- setSelectionAttribute( 'foo2', 'bar2' );
- removeSelectionAttribute( [ 'foo', 'foo2' ] );
- expect( model.document.selection.getAttribute( 'foo' ) ).to.be.undefined;
- expect( model.document.selection.getAttribute( 'foo2' ) ).to.be.undefined;
- expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
- } );
- it( 'should do nothing if attribute does not exist in the selection', () => {
- setSelection( rangeInEmptyP );
- setSelectionAttribute( 'foo', 'bar' );
- setSelectionAttribute( 'foo2', 'bar2' );
- removeSelectionAttribute( [ 'foo', 'baz' ] );
- expect( model.document.selection.getAttribute( 'foo' ) ).to.be.undefined;
- expect( model.document.selection.getAttribute( 'foo2' ) ).to.equal( 'bar2' );
- expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
- } );
- } );
- describe( 'overrideSelectionGravity()', () => {
- it( 'should use DocumentSelection#_overrideGravity', () => {
- const overrideGravitySpy = sinon.spy( DocumentSelection.prototype, '_overrideGravity' );
- overrideSelectionGravity();
- sinon.assert.calledOnce( overrideGravitySpy );
- overrideGravitySpy.restore();
- } );
- it( 'should not get attributes from the node before the caret when gravity is overridden', () => {
- const root = doc.createRoot();
- root._appendChild( [
- new Text( 'foo', { foo: true } ),
- new Text( 'bar', { foo: true, bar: true } ),
- new Text( 'biz', { foo: true } )
- ] );
- setSelection( new Position( root, [ 6 ] ) );
- expect( Array.from( model.document.selection.getAttributeKeys() ) ).to.deep.equal( [ 'foo', 'bar' ] );
- overrideSelectionGravity();
- expect( Array.from( model.document.selection.getAttributeKeys() ) ).to.deep.equal( [ 'foo' ] );
- expect( model.document.selection.isGravityOverridden ).to.true;
- // Disable override by moving selection.
- setSelection( new Position( root, [ 5 ] ) );
- expect( Array.from( model.document.selection.getAttributeKeys() ) ).to.deep.equal( [ 'foo', 'bar' ] );
- expect( model.document.selection.isGravityOverridden ).to.false;
- } );
- it( 'should allow to restorer gravity in a custom way', () => {
- const root = doc.createRoot();
- root._appendChild( [ new Text( 'foobar', { foo: true } ) ] );
- setSelection( new Position( root, [ 1 ] ) );
- overrideSelectionGravity( true );
- // Moving selection does not restore gravity.
- setSelection( new Position( root, [ 2 ] ) );
- expect( model.document.selection.isGravityOverridden ).to.true;
- // We need to do it manually.
- restoreSelectionGravity();
- expect( model.document.selection.isGravityOverridden ).to.false;
- } );
- } );
- describe( 'restoreSelectionGravity()', () => {
- it( 'should use DocumentSelection#_restoreGravity', () => {
- const restoreGravitySpy = sinon.spy( DocumentSelection.prototype, '_restoreGravity' );
- restoreSelectionGravity();
- sinon.assert.calledOnce( restoreGravitySpy );
- restoreGravitySpy.restore();
- } );
- it( 'should restore overridden gravity to default', () => {
- const root = doc.createRoot();
- root._appendChild( [
- new Text( 'foo', { foo: true } ),
- new Text( 'bar', { foo: true, bar: true } ),
- new Text( 'biz', { foo: true } )
- ] );
- setSelection( new Position( root, [ 6 ] ) );
- overrideSelectionGravity();
- expect( Array.from( model.document.selection.getAttributeKeys() ) ).to.deep.equal( [ 'foo' ] );
- restoreSelectionGravity();
- expect( Array.from( model.document.selection.getAttributeKeys() ) ).to.deep.equal( [ 'foo', 'bar' ] );
- } );
- } );
- function createText( data, attributes ) {
- return model.change( writer => {
- return writer.createText( data, attributes );
- } );
- }
- function createElement( name, attributes ) {
- return model.change( writer => {
- return writer.createElement( name, attributes );
- } );
- }
- function createDocumentFragment() {
- return model.change( writer => {
- return writer.createDocumentFragment();
- } );
- }
- function insert( item, itemOrPosition, offset ) {
- model.enqueueChange( batch, writer => {
- writer.insert( item, itemOrPosition, offset );
- } );
- }
- function insertText( text, attributes, itemOrPosition, offset ) {
- model.enqueueChange( batch, writer => {
- writer.insertText( text, attributes, itemOrPosition, offset );
- } );
- }
- function insertElement( name, attributes, itemOrPosition, offset ) {
- model.enqueueChange( batch, writer => {
- writer.insertElement( name, attributes, itemOrPosition, offset );
- } );
- }
- function append( item, parent ) {
- model.enqueueChange( batch, writer => {
- writer.append( item, parent );
- } );
- }
- function appendText( text, attributes, parent ) {
- model.enqueueChange( batch, writer => {
- writer.appendText( text, attributes, parent );
- } );
- }
- function appendElement( name, attributes, parent ) {
- model.enqueueChange( batch, writer => {
- writer.appendElement( name, attributes, parent );
- } );
- }
- function setAttribute( key, value, itemOrRange ) {
- model.enqueueChange( batch, writer => {
- writer.setAttribute( key, value, itemOrRange );
- } );
- }
- function setAttributes( attributes, itemOrRange ) {
- model.enqueueChange( batch, writer => {
- writer.setAttributes( attributes, itemOrRange );
- } );
- }
- function removeAttribute( key, itemOrRange ) {
- model.enqueueChange( batch, writer => {
- writer.removeAttribute( key, itemOrRange );
- } );
- }
- function clearAttributes( itemOrRange ) {
- model.enqueueChange( batch, writer => {
- writer.clearAttributes( itemOrRange );
- } );
- }
- function move( range, itemOrPosition, offset ) {
- model.enqueueChange( batch, writer => {
- writer.move( range, itemOrPosition, offset );
- } );
- }
- function remove( itemOrRange ) {
- model.enqueueChange( batch, writer => {
- writer.remove( itemOrRange );
- } );
- }
- function merge( position ) {
- model.enqueueChange( batch, writer => {
- writer.merge( position );
- } );
- }
- function rename( element, newName ) {
- model.enqueueChange( batch, writer => {
- writer.rename( element, newName );
- } );
- }
- function split( position, limitElement ) {
- model.enqueueChange( batch, writer => {
- writer.split( position, limitElement );
- } );
- }
- function wrap( range, elementOrString ) {
- model.enqueueChange( batch, writer => {
- writer.wrap( range, elementOrString );
- } );
- }
- function unwrap( element ) {
- model.enqueueChange( batch, writer => {
- writer.unwrap( element );
- } );
- }
- function addMarker( name, options ) {
- let marker;
- model.enqueueChange( batch, writer => {
- marker = writer.addMarker( name, options );
- } );
- return marker;
- }
- function updateMarker( markerOrName, options ) {
- model.enqueueChange( batch, writer => {
- writer.updateMarker( markerOrName, options );
- } );
- }
- function removeMarker( markerOrName, options ) {
- model.enqueueChange( batch, writer => {
- writer.removeMarker( markerOrName, options );
- } );
- }
- function setSelection( selectable, optionsOrPlaceOrOffset, options ) {
- model.enqueueChange( batch, writer => {
- writer.setSelection( selectable, optionsOrPlaceOrOffset, options );
- } );
- }
- function setSelectionFocus( itemOrPosition, offset ) {
- model.enqueueChange( batch, writer => {
- writer.setSelectionFocus( itemOrPosition, offset );
- } );
- }
- function setSelectionAttribute( key, value ) {
- model.enqueueChange( batch, writer => {
- writer.setSelectionAttribute( key, value );
- } );
- }
- function removeSelectionAttribute( key ) {
- model.enqueueChange( batch, writer => {
- writer.removeSelectionAttribute( key );
- } );
- }
- function overrideSelectionGravity( customRestore ) {
- model.change( writer => {
- writer.overrideSelectionGravity( customRestore );
- } );
- }
- function restoreSelectionGravity() {
- model.change( writer => {
- writer.restoreSelectionGravity();
- } );
- }
- } );
|