| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942 |
- /**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import Batch from '../../src/model/batch';
- import Delta from '../../src/model/delta/delta';
- import InsertDelta from '../../src/model/delta/insertdelta';
- import WeakInsertDelta from '../../src/model/delta/weakinsertdelta';
- import Operation from '../../src/model/operation/operation';
- import Document from '../../src/model/document';
- 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';
- describe( 'Batch', () => {
- describe( 'type', () => {
- it( 'should default to "default"', () => {
- const batch = new Batch( new Document() );
- expect( batch.type ).to.equal( 'default' );
- } );
- it( 'should be set to the value set in constructor', () => {
- const batch = new Batch( new Document(), 'ignore' );
- expect( batch.type ).to.equal( 'ignore' );
- } );
- } );
- describe( 'baseVersion', () => {
- it( 'should return base version of first delta from the batch', () => {
- const batch = new Batch( new Document() );
- const delta = new Delta();
- const operation = new Operation( 2 );
- delta.addOperation( operation );
- batch.addDelta( delta );
- expect( batch.baseVersion ).to.equal( 2 );
- } );
- it( 'should return null if there are no deltas in batch', () => {
- const batch = new Batch( new Document() );
- expect( batch.baseVersion ).to.be.null;
- } );
- } );
- describe( 'addDelta()', () => {
- it( 'should add delta to the batch', () => {
- const batch = new Batch( new Document() );
- const deltaA = new Delta();
- const deltaB = new Delta();
- batch.addDelta( deltaA );
- batch.addDelta( deltaB );
- expect( batch.deltas.length ).to.equal( 2 );
- expect( batch.deltas[ 0 ] ).to.equal( deltaA );
- expect( batch.deltas[ 1 ] ).to.equal( deltaB );
- } );
- } );
- describe( 'getOperations()', () => {
- it( 'should return collection of operations from all deltas', () => {
- const doc = new Document();
- const batch = new Batch( doc );
- const deltaA = new Delta();
- const deltaB = new Delta();
- const ops = [
- new Operation( doc.version ),
- new Operation( doc.version + 1 ),
- new Operation( doc.version + 2 )
- ];
- batch.addDelta( deltaA );
- deltaA.addOperation( ops[ 0 ] );
- batch.addDelta( deltaB );
- deltaA.addOperation( ops[ 1 ] );
- deltaA.addOperation( ops[ 2 ] );
- expect( Array.from( batch.getOperations() ) ).to.deep.equal( ops );
- expect( batch.getOperations() ).to.have.property( 'next' );
- } );
- } );
- describe( 'createText()', () => {
- let doc, batch;
- beforeEach( () => {
- doc = new Document();
- batch = doc.batch();
- } );
- it( 'should create text node', () => {
- const text = batch.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 = batch.createText( 'foo', { foo: 'bar', biz: 'baz' } );
- expect( Array.from( text.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'biz', 'baz' ] ] );
- } );
- } );
- describe( 'createElement()', () => {
- let doc, batch;
- beforeEach( () => {
- doc = new Document();
- batch = doc.batch();
- } );
- it( 'should create element', () => {
- const element = batch.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 = batch.createText( 'foo', { foo: 'bar', biz: 'baz' } );
- expect( Array.from( element.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'biz', 'baz' ] ] );
- } );
- } );
- describe( 'createDocumentFragment()', () => {
- let doc, batch;
- beforeEach( () => {
- doc = new Document();
- batch = doc.batch();
- } );
- it( 'should create element', () => {
- const element = batch.createDocumentFragment();
- expect( element ).to.instanceof( DocumentFragment );
- } );
- } );
- describe( 'insert()', () => {
- let doc, batch;
- beforeEach( () => {
- doc = new Document();
- batch = doc.batch();
- } );
- it( 'should insert node at given position', () => {
- const parent = batch.createDocumentFragment();
- const child = batch.createElement( 'child' );
- const textChild = batch.createText( 'textChild' );
- batch.insert( child, new Position( parent, [ 0 ] ) );
- batch.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 = batch.createDocumentFragment();
- const child1 = batch.createElement( 'child' );
- const child2 = batch.createElement( 'child' );
- batch.insert( child1, parent );
- batch.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 = batch.createDocumentFragment();
- const child1 = batch.createElement( 'child' );
- const child2 = batch.createElement( 'child' );
- batch.insert( child1, parent );
- batch.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 = batch.createDocumentFragment();
- const child1 = batch.createElement( 'child' );
- const child2 = batch.createElement( 'child' );
- const child3 = batch.createElement( 'child' );
- batch.insert( child3, parent );
- batch.insert( child1, parent );
- batch.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 = batch.createDocumentFragment();
- const child1 = batch.createElement( 'child' );
- const child2 = batch.createElement( 'child' );
- const child3 = batch.createElement( 'child' );
- batch.insert( child3, parent );
- batch.insert( child1, parent );
- batch.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 = batch.createDocumentFragment();
- const child1 = batch.createElement( 'child' );
- const child2 = batch.createElement( 'child' );
- const child3 = batch.createElement( 'child' );
- batch.insert( child3, parent );
- batch.insert( child1, parent );
- batch.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 = batch.createDocumentFragment();
- const element = batch.createElement( 'child' );
- const spy = sinon.spy( doc, 'applyOperation' );
- batch.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 = batch.createDocumentFragment();
- const text = batch.createText( 'child' );
- const spy = sinon.spy( doc, 'applyOperation' );
- batch.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 (documentA -> documentA)', () => {
- const rootA = doc.createRoot();
- const parent1 = batch.createElement( 'parent' );
- const parent2 = batch.createElement( 'parent' );
- const node = batch.createText( 'foo' );
- batch.insert( node, parent1 );
- batch.insert( parent1, rootA );
- batch.insert( parent2, rootA );
- const spy = sinon.spy( doc, 'applyOperation' );
- batch.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 (documentA -> documentB)', () => {
- const rootA = doc.createRoot( '$root', 'A' );
- const rootB = doc.createRoot( '$root', 'B' );
- const node = batch.createText( 'foo' );
- batch.insert( node, rootA );
- const spy = sinon.spy( doc, 'applyOperation' );
- batch.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 = batch.createDocumentFragment();
- const parent1 = batch.createElement( 'parent' );
- const parent2 = batch.createElement( 'parent' );
- const node = batch.createText( 'foo' );
- batch.insert( node, parent1 );
- batch.insert( parent1, docFragA );
- batch.insert( parent2, docFragA );
- const spy = sinon.spy( doc, 'applyOperation' );
- batch.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 (document -> docFrag)', () => {
- const root = doc.createRoot();
- const docFrag = batch.createDocumentFragment();
- const node = batch.createText( 'foo' );
- batch.insert( node, root );
- const spy = sinon.spy( doc, 'applyOperation' );
- batch.insert( node, docFrag );
- // Verify result.
- expect( Array.from( root.getChildren() ) ).to.deep.equal( [] );
- expect( Array.from( docFrag.getChildren() ) ).to.deep.equal( [ node ] );
- // Verify deltas and operations.
- sinon.assert.calledTwice( spy );
- expect( spy.firstCall.args[ 0 ].type ).to.equal( 'remove' );
- 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 move element from one parent to the other within different document (docFragA -> docFragB)', () => {
- const docFragA = batch.createDocumentFragment();
- const docFragB = batch.createDocumentFragment();
- const node = batch.createText( 'foo' );
- batch.insert( node, docFragA );
- const spy = sinon.spy( doc, 'applyOperation' );
- batch.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 transfer markers from given DocumentFragment', () => {
- const root = doc.createRoot();
- const docFrag = batch.createDocumentFragment();
- batch.appendText( 'abcd', root );
- batch.appendElement( 'p', docFrag );
- batch.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 );
- batch.insert( docFrag, new Position( root, [ 2 ] ) );
- expect( Array.from( doc.markers ).length ).to.equal( 1 );
- const range = doc.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 set each marker as a separate operation', () => {
- const spy = sinon.spy();
- const root = doc.createRoot();
- const docFrag = batch.createDocumentFragment();
- batch.appendText( 'abcd', root );
- batch.appendElement( 'p', docFrag );
- batch.insertText( 'foo bar', new Position( docFrag, [ 0, 0 ] ) );
- const marker1 = new Range( new Position( docFrag, [ 0, 1 ] ), new Position( docFrag, [ 0, 2 ] ) );
- const marker2 = new Range( new Position( docFrag, [ 0, 5 ] ), new Position( docFrag, [ 0, 6 ] ) );
- docFrag.markers.set( 'marker1', marker1 );
- docFrag.markers.set( 'marker2', marker2 );
- doc.on( 'change', spy );
- batch.insert( docFrag, new Position( root, [ 2 ] ) );
- sinon.assert.calledThrice( spy );
- expect( spy.firstCall.args[ 1 ] ).to.equal( 'insert' );
- expect( spy.secondCall.args[ 1 ] ).to.equal( 'marker' );
- expect( spy.thirdCall.args[ 1 ] ).to.equal( 'marker' );
- } );
- } );
- describe( 'insertText()', () => {
- let doc, batch;
- beforeEach( () => {
- doc = new Document();
- batch = doc.batch();
- } );
- it( 'should create and insert text node with attributes at given position', () => {
- const parent = batch.createDocumentFragment();
- batch.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 = batch.createDocumentFragment();
- batch.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 = batch.createDocumentFragment();
- batch.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 = batch.createDocumentFragment();
- batch.insert( batch.createElement( 'child' ), parent );
- batch.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 = batch.createDocumentFragment();
- batch.insert( batch.createElement( 'child' ), parent );
- batch.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 = batch.createDocumentFragment();
- batch.insert( batch.createElement( 'child' ), parent );
- batch.insert( batch.createElement( 'child' ), parent );
- batch.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 = batch.createDocumentFragment();
- const child1 = batch.createElement( 'child' );
- const child2 = batch.createElement( 'child' );
- batch.insert( child1, parent );
- batch.insert( child2, parent, 'end' );
- batch.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 = batch.createDocumentFragment();
- const child1 = batch.createElement( 'child' );
- const child2 = batch.createElement( 'child' );
- batch.insert( child1, parent );
- batch.insert( child2, parent, 'end' );
- batch.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 = batch.createDocumentFragment();
- const spy = sinon.spy( doc, 'applyOperation' );
- batch.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 );
- } );
- } );
- describe( 'insertElement()', () => {
- let doc, batch;
- beforeEach( () => {
- doc = new Document();
- batch = doc.batch();
- } );
- it( 'should create and insert element with attributes at given position', () => {
- const parent = batch.createDocumentFragment();
- batch.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 = batch.createDocumentFragment();
- batch.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 = batch.createDocumentFragment();
- batch.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 = batch.createDocumentFragment();
- batch.insert( batch.createElement( 'child' ), parent );
- batch.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 = batch.createDocumentFragment();
- batch.insert( batch.createElement( 'child' ), parent );
- batch.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 = batch.createDocumentFragment();
- batch.insert( batch.createElement( 'child1' ), parent );
- batch.insert( batch.createElement( 'child2' ), parent, 'end' );
- batch.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 = batch.createDocumentFragment();
- const child1 = batch.createElement( 'child1' );
- const child2 = batch.createElement( 'child2' );
- batch.insert( child1, parent );
- batch.insert( child2, parent, 'end' );
- batch.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 = batch.createDocumentFragment();
- const child1 = batch.createElement( 'child1' );
- const child2 = batch.createElement( 'child2' );
- batch.insert( child1, parent );
- batch.insert( child2, parent, 'end' );
- batch.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 = batch.createDocumentFragment();
- const spy = sinon.spy( doc, 'applyOperation' );
- batch.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 );
- } );
- } );
- describe( 'append()', () => {
- let doc, batch;
- beforeEach( () => {
- doc = new Document();
- batch = doc.batch();
- } );
- it( 'should insert element at the end of the parent', () => {
- const parent = doc.batch().createDocumentFragment();
- const childText = doc.batch().createText( 'foo' );
- const childElement = doc.batch().createElement( 'foo' );
- batch.append( childText, parent );
- batch.append( childElement, parent );
- expect( Array.from( parent ) ).to.deep.equal( [ childText, childElement ] );
- } );
- it( 'should create proper delta', () => {
- const parent = batch.createDocumentFragment();
- const text = batch.createText( 'foo' );
- const spy = sinon.spy( doc, 'applyOperation' );
- batch.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 document (documentA -> documentA)', () => {
- const rootA = doc.createRoot();
- const parent1 = batch.createElement( 'parent' );
- const parent2 = batch.createElement( 'parent' );
- const node = batch.createText( 'foo' );
- batch.insert( node, parent1 );
- batch.insert( parent1, rootA );
- batch.insert( parent2, rootA );
- const spy = sinon.spy( doc, 'applyOperation' );
- batch.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 (documentA -> documentB)', () => {
- const rootA = doc.createRoot( '$root', 'A' );
- const rootB = doc.createRoot( '$root', 'B' );
- const node = batch.createText( 'foo' );
- batch.insert( node, rootA );
- const spy = sinon.spy( doc, 'applyOperation' );
- batch.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 (docFragA -> docFragA)', () => {
- const docFragA = batch.createDocumentFragment();
- const parent1 = batch.createElement( 'parent' );
- const parent2 = batch.createElement( 'parent' );
- const node = batch.createText( 'foo' );
- batch.insert( node, parent1 );
- batch.insert( parent1, docFragA );
- batch.insert( parent2, docFragA );
- const spy = sinon.spy( doc, 'applyOperation' );
- batch.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 (document -> docFrag)', () => {
- const root = doc.createRoot();
- const docFrag = batch.createDocumentFragment();
- const node = batch.createText( 'foo' );
- batch.insert( node, root );
- const spy = sinon.spy( doc, 'applyOperation' );
- batch.append( node, docFrag );
- // Verify result.
- expect( Array.from( root.getChildren() ) ).to.deep.equal( [] );
- expect( Array.from( docFrag.getChildren() ) ).to.deep.equal( [ node ] );
- // Verify deltas and operations.
- sinon.assert.calledTwice( spy );
- expect( spy.firstCall.args[ 0 ].type ).to.equal( 'remove' );
- 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 move element from one parent to the other within different document (docFragA -> docFragB)', () => {
- const docFragA = batch.createDocumentFragment();
- const docFragB = batch.createDocumentFragment();
- const node = batch.createText( 'foo' );
- batch.insert( node, docFragA );
- const spy = sinon.spy( doc, 'applyOperation' );
- batch.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 );
- } );
- } );
- describe( 'appendText()', () => {
- let doc, batch;
- beforeEach( () => {
- doc = new Document();
- batch = doc.batch();
- } );
- it( 'should create and insert text node with attributes at the end of the parent', () => {
- const parent = batch.createDocumentFragment();
- batch.appendText( 'foo', { bar: 'biz' }, parent );
- batch.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 = batch.createDocumentFragment();
- batch.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 = batch.createDocumentFragment();
- batch.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 = batch.createDocumentFragment();
- const spy = sinon.spy( doc, 'applyOperation' );
- batch.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 );
- } );
- } );
- describe( 'appendElement()', () => {
- let doc, batch;
- beforeEach( () => {
- doc = new Document();
- batch = doc.batch();
- } );
- it( 'should create and insert element with attributes at the end of the parent', () => {
- const parent = batch.createDocumentFragment();
- batch.appendElement( 'foo', { bar: 'biz' }, parent );
- batch.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 = batch.createDocumentFragment();
- batch.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 = batch.createDocumentFragment();
- batch.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 = batch.createDocumentFragment();
- const spy = sinon.spy( doc, 'applyOperation' );
- batch.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 );
- } );
- } );
- describe( 'setAttribute() / removeAttribute()', () => {
- let batch, doc, root, spy;
- beforeEach( () => {
- doc = new Document();
- root = doc.createRoot();
- batch = doc.batch();
- } );
- describe( 'change attribute on node', () => {
- let node, text;
- beforeEach( () => {
- node = batch.createElement( 'p', { a: 1 } );
- text = batch.createText( 'c', { a: 1 } );
- batch.append( node, root );
- batch.append( text, root );
- spy = sinon.spy( doc, 'applyOperation' );
- } );
- describe( 'setAttribute', () => {
- it( 'should create the attribute on element', () => {
- batch.setAttribute( 'b', 2, node );
- expect( spy.callCount ).to.equal( 1 );
- expect( node.getAttribute( 'b' ) ).to.equal( 2 );
- } );
- it( 'should change the attribute of element', () => {
- batch.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', () => {
- batch.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', () => {
- batch.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', () => {
- batch.setAttribute( 'a', 1, node );
- expect( spy.callCount ).to.equal( 0 );
- expect( node.getAttribute( 'a' ) ).to.equal( 1 );
- } );
- } );
- describe( 'removeAttribute', () => {
- it( 'should remove the attribute from element', () => {
- batch.removeAttribute( 'a', node );
- expect( spy.callCount ).to.equal( 1 );
- expect( node.getAttribute( 'a' ) ).to.be.undefined;
- } );
- it( 'should remove the attribute from character', () => {
- batch.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', () => {
- batch.removeAttribute( 'b', node );
- expect( spy.callCount ).to.equal( 0 );
- } );
- } );
- } );
- describe( 'change attribute on range', () => {
- beforeEach( () => {
- const element = batch.createElement( 'e', { a: 2 } );
- batch.appendText( 'xxx', { a: 1 }, root );
- batch.appendText( 'xxx', root );
- batch.appendText( 'xxx', { a: 1 }, root );
- batch.appendText( 'xxx', { a: 2 }, root );
- batch.appendText( 'xxx', root );
- batch.appendText( 'xxx', { a: 1 }, root );
- batch.appendText( 'xxx', element );
- batch.append( element, root );
- batch.appendText( 'xxx', root );
- spy = sinon.spy( doc, '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', () => {
- batch.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', () => {
- batch.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', () => {
- batch.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', () => {
- batch.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', () => {
- batch.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', () => {
- batch.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 ] )
- );
- batch.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 ] )
- );
- batch.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 ] )
- );
- batch.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', () => {
- batch.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', () => {
- batch.setAttribute( 'a', 1, getRange( 0, 20 ) );
- expect( spy.callCount ).to.equal( 5 );
- expect( getChangesAttrsCount() ).to.equal( 14 );
- expect( getCompressedAttrs() ).to.equal( '11111111111111111111111--' );
- } );
- } );
- describe( 'removeAttribute', () => {
- it( 'should remove the attribute on the range', () => {
- batch.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', () => {
- batch.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', () => {
- batch.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', () => {
- batch.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', () => {
- batch.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', () => {
- batch.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 ] )
- );
- batch.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', () => {
- batch.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', () => {
- batch.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', () => {
- batch.removeAttribute( 'a', getRange( 3, 15 ) );
- expect( spy.callCount ).to.equal( 2 );
- expect( getChangesAttrsCount() ).to.equal( 6 );
- expect( getCompressedAttrs() ).to.equal( '111------------1112------' );
- } );
- } );
- } );
- describe( 'change attribute on root element', () => {
- let p;
- beforeEach( () => {
- p = batch.createElement( 'p', { a: 3 } );
- spy = sinon.spy( doc, 'applyOperation' );
- } );
- describe( 'setAttribute', () => {
- it( 'should create the attribute on root', () => {
- batch.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', () => {
- batch.setAttribute( 'b', 2, p );
- expect( spy.callCount ).to.equal( 1 );
- expect( p.getAttribute( 'b' ) ).to.equal( 2 );
- } );
- it( 'should change the attribute of root', () => {
- batch.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', () => {
- batch.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', () => {
- batch.setAttribute( 'a', 1, root );
- expect( spy.callCount ).to.equal( 1 );
- batch.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', () => {
- batch.setAttribute( 'a', 1, p );
- expect( spy.callCount ).to.equal( 1 );
- batch.setAttribute( 'a', 1, p );
- expect( spy.callCount ).to.equal( 1 );
- expect( p.getAttribute( 'a' ) ).to.equal( 1 );
- } );
- } );
- describe( 'removeAttribute', () => {
- it( 'should remove the attribute from root', () => {
- batch.setAttribute( 'a', 1, root );
- batch.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', () => {
- batch.removeAttribute( 'b', root );
- expect( spy.callCount ).to.equal( 0 );
- } );
- } );
- describe( 'clearAttributes', () => {
- it( 'should clear attributes from range', () => {
- batch.appendText( 'xxx', { a: 1, b: 2, c: 3 }, root );
- batch.appendText( 'xxx', root );
- batch.appendText( 'xxx', { a: 1 }, root );
- batch.appendText( 'xxx', { b: 2 }, root );
- batch.appendText( 'xxx', root );
- batch.appendElement( 'e', { a: 1 }, root );
- batch.appendText( 'xxx', root );
- const range = Range.createIn( root );
- batch.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 = batch.createElement( 'x', { a: 1, b: 2, c: 3 }, root );
- expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 3 );
- batch.clearAttributes( element );
- expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 0 );
- } );
- it( 'should clear attributes on root element', () => {
- batch.setAttributes( { a: 1, b: 2, c: 3 }, root );
- expect( Array.from( root.getAttributeKeys() ).length ).to.equal( 3 );
- batch.clearAttributes( root );
- expect( Array.from( root.getAttributeKeys() ).length ).to.equal( 0 );
- } );
- it( 'should do nothing if there are no attributes', () => {
- const element = batch.createElement( 'x' );
- expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 0 );
- batch.clearAttributes( element );
- expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 0 );
- } );
- } );
- } );
- it( 'should not add empty delta to the batch', () => {
- const nodeA = new Element( 'p', { a: 1 } );
- const nodeB = new Element( 'p', { b: 2 } );
- root.insertChildren( 0, [ nodeA, nodeB ] );
- batch.setAttribute( 'a', 1, nodeA );
- expect( batch.deltas.length ).to.equal( 0 );
- batch.removeAttribute( 'x', Range.createIn( root ) );
- expect( batch.deltas.length ).to.equal( 0 );
- } );
- } );
- describe( 'setAttributes()', () => {
- let doc, batch, frag, item;
- beforeEach( () => {
- doc = new Document();
- batch = doc.batch();
- frag = batch.createDocumentFragment();
- item = batch.createText( 'xxx', { b: 2, c: 3 } );
- batch.appendText( 'xxx', { a: 1 }, frag );
- batch.append( item, frag );
- } );
- it( 'should set attributes one by one on range', () => {
- const range = Range.createIn( frag );
- // `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.
- const spy = sinon.spy( batch, 'setAttribute' );
- batch.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 );
- // `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.
- const spy = sinon.spy( batch, 'setAttribute' );
- batch.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', () => {
- // `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.
- const spy = sinon.spy( batch, 'setAttribute' );
- batch.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', () => {
- // `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.
- const spy = sinon.spy( batch, 'setAttribute' );
- batch.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 );
- } );
- } );
- describe( 'merge()', () => {
- let doc, root, p1, p2;
- beforeEach( () => {
- doc = new Document();
- root = doc.createRoot();
- p1 = new Element( 'p', { key1: 'value1' }, new Text( 'foo' ) );
- p2 = new Element( 'p', { key2: 'value2' }, new Text( 'bar' ) );
- root.insertChildren( 0, [ p1, p2 ] );
- } );
- it( 'should merge foo and bar into foobar', () => {
- doc.batch().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 throw if there is no element after', () => {
- expect( () => {
- doc.batch().merge( new Position( root, [ 2 ] ) );
- } ).to.throw( CKEditorError, /^batch-merge-no-element-after/ );
- } );
- it( 'should throw if there is no element before', () => {
- expect( () => {
- doc.batch().merge( new Position( root, [ 0, 2 ] ) );
- } ).to.throw( CKEditorError, /^batch-merge-no-element-before/ );
- } );
- } );
- describe( 'move()', () => {
- let doc, root, range, div, p, batch;
- beforeEach( () => {
- doc = new Document();
- root = doc.createRoot();
- div = new Element( 'div', [], new Text( 'foobar' ) );
- p = new Element( 'p', [], new Text( 'abcxyz' ) );
- div.insertChildren( 0, [ new Element( 'p', [], new Text( 'gggg' ) ) ] );
- div.insertChildren( 2, [ new Element( 'p', [], new Text( 'hhhh' ) ) ] );
- root.insertChildren( 0, [ div, p ] );
- range = new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 0, 7 ] ) );
- batch = doc.batch();
- } );
- it( 'should move flat range of nodes', () => {
- batch.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( () => {
- doc.batch().move( root.getChild( 0 ), new Position( root, [ 1, 3 ] ) );
- } ).to.throw( CKEditorError, /^batch-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( () => {
- doc.batch().move( notFlatRange, new Position( root, [ 1, 3 ] ) );
- } ).to.throw( CKEditorError, /^batch-move-range-not-flat/ );
- } );
- it( 'should throw if range is going to be moved to the other document', () => {
- const docFrag = batch.createDocumentFragment();
- expect( () => {
- doc.batch().move( range, docFrag );
- } ).to.throw( CKEditorError, /^batch-move-different-document/ );
- } );
- } );
- describe( 'remove()', () => {
- let doc, batch, div, p, range;
- beforeEach( () => {
- doc = new Document();
- batch = doc.batch();
- div = batch.createElement( 'div' );
- batch.appendText( 'foobar', div );
- p = batch.createElement( 'p' );
- batch.appendText( 'abcxyz', p );
- batch.insertElement( 'p', div );
- batch.appendElement( 'p', div );
- batch.insertText( 'gggg', new Position( div, [ 0, 0 ] ) );
- batch.insertText( 'hhhh', new Position( div, [ 7, 0 ] ) );
- } );
- describe( 'remove from document', () => {
- let root;
- beforeEach( () => {
- root = doc.createRoot();
- batch.append( div, root );
- batch.append( p, root );
- // Reset batch.
- batch = doc.batch();
- // 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', () => {
- batch.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', () => {
- batch.remove( p.getChild( 0 ) );
- expect( getNodesAndText( Range.createOn( p ) ) ).to.equal( 'PP' );
- } );
- it( 'should remove any range of nodes', () => {
- batch.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.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.remove( div );
- expect( batch.deltas[ 0 ].operations[ 0 ].type ).to.equal( 'remove' );
- } );
- } );
- describe( 'remove from document fragment', () => {
- let frag;
- beforeEach( () => {
- frag = batch.createDocumentFragment();
- batch.append( div, frag );
- batch.append( p, frag );
- // Reset batch.
- batch = doc.batch();
- // 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', () => {
- batch.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', () => {
- batch.remove( p.getChild( 0 ) );
- expect( getNodesAndText( Range.createOn( p ) ) ).to.equal( 'PP' );
- } );
- it( 'should remove any range of nodes', () => {
- batch.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.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.remove( div );
- expect( batch.deltas[ 0 ].operations[ 0 ].type ).to.equal( 'detach' );
- } );
- } );
- } );
- describe( 'rename()', () => {
- let doc, root, batch;
- beforeEach( () => {
- doc = new Document();
- root = doc.createRoot();
- const p = new Element( 'p', null, new Text( 'abc' ) );
- root.appendChildren( p );
- batch = doc.batch();
- batch.rename( p, 'h' );
- } );
- it( 'should rename given element', () => {
- expect( root.maxOffset ).to.equal( 1 );
- expect( root.getChild( 0 ) ).to.have.property( 'name', 'h' );
- } );
- it( 'should throw if not an Element instance is passed', () => {
- expect( () => {
- batch.rename( new Text( 'abc' ), 'h' );
- } ).to.throw( CKEditorError, /^batch-rename-not-element-instance/ );
- } );
- } );
- describe( 'split()', () => {
- let doc, root, p;
- beforeEach( () => {
- doc = new Document();
- root = doc.createRoot();
- p = new Element( 'p', { key: 'value' }, new Text( 'foobar' ) );
- root.insertChildren( 0, p );
- } );
- it( 'should split foobar to foo and bar', () => {
- doc.batch().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 create an empty paragraph if we split at the end', () => {
- doc.batch().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( () => {
- doc.batch().split( new Position( root, [ 0 ] ) );
- } ).to.throw( CKEditorError, /^batch-split-element-no-parent/ );
- } );
- it( 'should throw if we try to split an element with no parent', () => {
- const batch = doc.batch();
- expect( () => {
- const element = batch.createElement( 'p' );
- batch.split( new Position( element, [ 0 ] ) );
- } ).to.throw( CKEditorError, /^batch-split-element-no-parent/ );
- } );
- it( 'should throw if we try to split a document fragment', () => {
- const batch = doc.batch();
- expect( () => {
- const documentFragment = batch.createDocumentFragment();
- batch.split( new Position( documentFragment, [ 0 ] ) );
- } ).to.throw( CKEditorError, /^batch-split-element-no-parent/ );
- } );
- } );
- describe( 'wrap()', () => {
- let doc, root, range;
- beforeEach( () => {
- doc = new Document();
- root = doc.createRoot();
- root.insertChildren( 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' );
- doc.batch().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', () => {
- doc.batch().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 throw if range to wrap is not flat', () => {
- root.insertChildren( 1, [ new Element( 'p', [], new Text( 'xyz' ) ) ] );
- const notFlatRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 6, 2 ] ) );
- expect( () => {
- doc.batch().wrap( notFlatRange, 'p' );
- } ).to.throw( CKEditorError, /^batch-wrap-range-not-flat/ );
- } );
- it( 'should throw if element to wrap with has children #1', () => {
- const p = new Element( 'p', [], new Text( 'a' ) );
- expect( () => {
- doc.batch().wrap( range, p );
- } ).to.throw( CKEditorError, /^batch-wrap-element-not-empty/ );
- } );
- it( 'should throw if element to wrap with has children #2', () => {
- const p = new Element( 'p' );
- root.insertChildren( 0, p );
- expect( () => {
- doc.batch().wrap( range, p );
- } ).to.throw( CKEditorError, /^batch-wrap-element-attached/ );
- } );
- } );
- describe( 'unwrap()', () => {
- let doc, root, p;
- beforeEach( () => {
- doc = new Document();
- root = doc.createRoot();
- p = new Element( 'p', [], new Text( 'xyz' ) );
- root.insertChildren( 0, [ new Text( 'a' ), p, new Text( 'b' ) ] );
- } );
- it( 'should unwrap given element', () => {
- doc.batch().unwrap( p );
- expect( root.maxOffset ).to.equal( 5 );
- expect( root.getChild( 0 ).data ).to.equal( 'axyzb' );
- } );
- it( 'should throw if element to unwrap has no parent', () => {
- const element = new Element( 'p' );
- expect( () => {
- doc.batch().unwrap( element );
- } ).to.throw( CKEditorError, /^batch-unwrap-element-no-parent/ );
- } );
- } );
- describe( 'setMarker()', () => {
- let doc, root, range;
- beforeEach( () => {
- doc = new Document();
- root = doc.createRoot();
- root.appendChildren( new Text( 'foo' ) );
- range = Range.createIn( root );
- } );
- it( 'should add marker to the document marker collection', () => {
- doc.batch().setMarker( 'name', range );
- expect( doc.markers.get( 'name' ).getRange().isEqual( range ) ).to.be.true;
- } );
- it( 'should update marker in the document marker collection', () => {
- doc.batch().setMarker( 'name', range );
- const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
- doc.batch().setMarker( 'name', range2 );
- expect( doc.markers.get( 'name' ).getRange().isEqual( range2 ) ).to.be.true;
- } );
- it( 'should accept marker instance', () => {
- doc.batch().setMarker( 'name', range );
- const marker = doc.markers.get( 'name' );
- const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
- const batch = doc.batch();
- batch.setMarker( marker, range2 );
- const op = batch.deltas[ 0 ].operations[ 0 ];
- expect( doc.markers.get( 'name' ).getRange().isEqual( range2 ) ).to.be.true;
- expect( op.oldRange.isEqual( range ) ).to.be.true;
- expect( op.newRange.isEqual( range2 ) ).to.be.true;
- } );
- it( 'should accept empty range parameter if marker instance is passed', () => {
- const marker = doc.markers.set( 'name', range );
- sinon.spy( doc, 'fire' );
- doc.on( 'change', ( evt, type, changes ) => {
- if ( type == 'marker' ) {
- expect( changes.type ).to.equal( 'set' );
- expect( changes.name ).to.equal( 'name' );
- }
- } );
- const batch = doc.batch();
- batch.setMarker( marker );
- const op = batch.deltas[ 0 ].operations[ 0 ];
- expect( doc.fire.calledWith( 'change', 'marker' ) ).to.be.true;
- 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( () => {
- doc.batch().setMarker( 'name' );
- } ).to.throw( CKEditorError, /^batch-setMarker-no-range/ );
- } );
- } );
- describe( 'removeMarker()', () => {
- let doc, root, range;
- beforeEach( () => {
- doc = new Document();
- root = doc.createRoot();
- root.appendChildren( new Text( 'foo' ) );
- range = Range.createIn( root );
- } );
- it( 'should remove marker from the document marker collection', () => {
- doc.batch().setMarker( 'name', range );
- doc.batch().removeMarker( 'name' );
- expect( doc.markers.get( 'name' ) ).to.be.null;
- } );
- it( 'should throw when trying to remove non existing marker', () => {
- expect( () => {
- doc.batch().removeMarker( 'name' );
- } ).to.throw( CKEditorError, /^batch-removeMarker-no-marker/ );
- } );
- it( 'should accept marker instance', () => {
- doc.batch().setMarker( 'name', range );
- const marker = doc.markers.get( 'name' );
- doc.batch().removeMarker( marker );
- expect( doc.markers.get( 'name' ) ).to.be.null;
- } );
- } );
- } );
|