| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import transform from '../../../src/model/operation/transform';
- import Model from '../../../src/model/model';
- import RootElement from '../../../src/model/rootelement';
- import Node from '../../../src/model/node';
- import Position from '../../../src/model/position';
- import Range from '../../../src/model/range';
- import InsertOperation from '../../../src/model/operation/insertoperation';
- import AttributeOperation from '../../../src/model/operation/attributeoperation';
- import RootAttributeOperation from '../../../src/model/operation/rootattributeoperation';
- import MarkerOperation from '../../../src/model/operation/markeroperation';
- import MoveOperation from '../../../src/model/operation/moveoperation';
- import RenameOperation from '../../../src/model/operation/renameoperation';
- import NoOperation from '../../../src/model/operation/nooperation';
- import log from '@ckeditor/ckeditor5-utils/src/log';
- describe( 'transform', () => {
- let model, doc, root, op, nodeA, nodeB, expected;
- beforeEach( () => {
- model = new Model();
- doc = model.document;
- root = doc.createRoot();
- nodeA = new Node();
- nodeB = new Node();
- } );
- function expectOperation( op, params ) {
- for ( const i in params ) {
- if ( params.hasOwnProperty( i ) ) {
- if ( i == 'type' ) {
- expect( op, 'type' ).to.be.instanceof( params[ i ] );
- }
- else if ( params[ i ] instanceof Array ) {
- expect( op[ i ].length, i ).to.equal( params[ i ].length );
- for ( let j = 0; j < params[ i ].length; j++ ) {
- expect( op[ i ][ j ] ).to.equal( params[ i ][ j ] );
- }
- } else if ( params[ i ] instanceof Position || params[ i ] instanceof Range ) {
- expect( op[ i ].isEqual( params[ i ] ), i ).to.be.true;
- } else {
- expect( op[ i ], i ).to.equal( params[ i ] );
- }
- }
- }
- }
- const contextIsStrong = {
- aIsStrong: true
- };
- it( 'error logging', () => {
- const spy = sinon.spy( log, 'error' );
- const nodeA = new Node();
- const nodeB = new Node();
- const position = new Position( root, [ 0 ] );
- const a = new InsertOperation( position, [ nodeA ], 0 );
- const b = new InsertOperation( position, [ nodeB ], 0 );
- // Modify an operation so it will throw an error.
- a.position = null;
- expect( () => {
- transform.transform( a, b );
- } ).to.throw();
- sinon.assert.called( spy );
- } );
- describe( 'InsertOperation', () => {
- let nodeC, nodeD, position;
- beforeEach( () => {
- nodeC = new Node();
- nodeD = new Node();
- position = new Position( root, [ 0, 2, 2 ] );
- op = new InsertOperation( position, [ nodeA, nodeB ], 0 );
- expected = {
- type: InsertOperation,
- position: Position.createFromPosition( position )
- };
- } );
- describe( 'by InsertOperation', () => {
- it( 'target at different position: no position update', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 1, 3, 2 ] ),
- [ nodeC, nodeD ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target at offset before: increment offset', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 0, 2, 0 ] ),
- [ nodeC, nodeD ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.position.offset += 2;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target at same offset and is important: increment offset', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 0, 2, 2 ] ),
- [ nodeC, nodeD ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.position.offset += 2;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target at same offset and is less important: no position update', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 0, 2, 2 ] ),
- [ nodeC, nodeD ],
- 0
- );
- const transOp = transform.transform( op, transformBy, contextIsStrong );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target at same offset and context.insertBefore = false: increment offset', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 0, 2, 2 ] ),
- [ nodeC, nodeD ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.position.offset += 2;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target at offset after: no position update', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 0, 2, 3 ] ),
- [ nodeC, nodeD ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target before node from path: increment index on path', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 0, 1 ] ),
- [ nodeC, nodeD ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.position.path[ 1 ] += 2;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target after node from path: no position update', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 0, 6 ] ),
- [ nodeC, nodeD ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by AttributeOperation', () => {
- it( 'no position update', () => {
- const transformBy = new AttributeOperation(
- Range.createFromPositionAndShift( position, 2 ),
- 'foo',
- null,
- 'bar',
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by RootAttributeOperation', () => {
- it( 'no position update', () => {
- const transformBy = new RootAttributeOperation(
- root,
- 'foo',
- null,
- 'bar',
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by MoveOperation', () => {
- it( 'range and target are different than insert position: no position update', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 1, 3, 2 ] ),
- 2,
- new Position( root, [ 2, 1 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range is before insert position offset: decrement offset', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 0, 2, 0 ] ),
- 1,
- new Position( root, [ 1, 1 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.position.offset--;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range offset is after insert position offset: no position update', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 0, 2, 5 ] ),
- 1,
- new Position( root, [ 1, 1 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target offset before insert position offset: increment offset', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 1, 1 ] ),
- 2,
- new Position( root, [ 0, 2, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.position.offset += 2;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target offset after insert position offset: no position update', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 1, 1 ] ),
- 2,
- new Position( root, [ 0, 2, 5 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target offset same as insert position offset and is important: increment offset', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 1, 1 ] ),
- 2,
- new Position( root, [ 0, 2, 2 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.position.offset += 2;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target offset same as insert position offset and is less important: increment offset', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 1, 1 ] ),
- 2,
- new Position( root, [ 0, 2, 2 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy, contextIsStrong );
- expected.position.offset += 2;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range is before node from insert position path: decrement index on path', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 0, 0 ] ),
- 1,
- new Position( root, [ 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.position.path[ 1 ] -= 1;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range is after node from insert position path: no position update', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 0, 4 ] ),
- 2,
- new Position( root, [ 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target before node from insert position path: increment index on path', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 1, 0 ] ),
- 2,
- new Position( root, [ 0, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.position.path[ 1 ] += 2;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target after node from insert position path: no position update', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 1, 0 ] ),
- 2,
- new Position( root, [ 0, 4 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range has node that contains insert position: update position', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 0, 1 ] ),
- 2,
- new Position( root, [ 1, 1 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.position.path = [ 1, 2, 2 ];
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range contains insert position (on same level): update position', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 0, 2, 0 ] ),
- 4,
- new Position( root, [ 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.position.path = [ 1, 2 ];
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by NoOperation', () => {
- it( 'no operation update', () => {
- const transformBy = new NoOperation( 0 );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by RenameOperation', () => {
- it( 'no position update', () => {
- const transformBy = new RenameOperation( new Position( root, [ 0, 2, 0 ] ), 'oldName', 'newName', 0 );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by MarkerOperation', () => {
- it( 'no position update', () => {
- const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 4 ] ) );
- const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, 0 );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- } );
- describe( 'AttributeOperation', () => {
- let start, end, range;
- beforeEach( () => {
- start = new Position( root, [ 0, 2, 1 ] );
- end = new Position( root, [ 0, 2, 4 ] );
- range = new Range( start, end );
- op = new AttributeOperation( range, 'foo', 'abc', 'bar', 0 );
- expected = {
- type: AttributeOperation,
- range,
- key: 'foo',
- oldValue: 'abc',
- newValue: 'bar'
- };
- } );
- describe( 'by InsertOperation', () => {
- it( 'target at offset before: increment offset', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 0, 2, 0 ] ),
- [ nodeA, nodeB ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.range.start.offset += 2;
- expected.range.end.offset += 2;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target at same offset: increment offset', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 0, 2, 1 ] ),
- [ nodeA, nodeB ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.range.start.offset += 2;
- expected.range.end.offset += 2;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by MoveOperation', () => {
- it( 'range offset is before change range start offset: decrement offset', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 0, 2, 0 ] ),
- 1,
- new Position( root, [ 2, 4, 1 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.range.start.offset--;
- expected.range.end.offset--;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target offset is before change range start offset: increment offset', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 4, 1 ] ),
- 2,
- new Position( root, [ 0, 2, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.range.start.offset += 2;
- expected.range.end.offset += 2;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range intersects on left with change range: split into two operations', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 0, 2, 2 ] ),
- 4,
- new Position( root, [ 2, 4, 1 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 2 );
- expected.range.end.offset -= 2;
- expectOperation( transOp[ 0 ], expected );
- expected.range.start.path = [ 2, 4, 1 ];
- expected.range.end.path = [ 2, 4, 3 ];
- expectOperation( transOp[ 1 ], expected );
- } );
- it( 'range intersects on right with change range: split into two operation', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 0, 2, 0 ] ),
- 2,
- new Position( root, [ 2, 4, 1 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 2 );
- expected.range.start.offset -= 1;
- expected.range.end.offset -= 2;
- expectOperation( transOp[ 0 ], expected );
- expected.range.start.path = [ 2, 4, 2 ];
- expected.range.end.path = [ 2, 4, 3 ];
- expectOperation( transOp[ 1 ], expected );
- } );
- it( 'range contains change range: update change range', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 0, 1 ] ),
- 3,
- new Position( root, [ 2, 4, 1 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.range.start.path = [ 2, 4, 2, 1 ];
- expected.range.end.path = [ 2, 4, 2, 4 ];
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range is inside change range: split into two operations', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 0, 2, 2 ] ),
- 1,
- new Position( root, [ 2, 4, 1 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 3 );
- expected.range.start.path = [ 0, 2, 1 ];
- expected.range.end.path = [ 0, 2, 2 ];
- expectOperation( transOp[ 0 ], expected );
- expected.range.start.path = [ 0, 2, 2 ];
- expected.range.end.path = [ 0, 2, 3 ];
- expectOperation( transOp[ 1 ], expected );
- expected.range.start.path = [ 2, 4, 1 ];
- expected.range.end.path = [ 2, 4, 2 ];
- expectOperation( transOp[ 2 ], expected );
- } );
- it( 'range is same as change range: update change range', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 0, 2, 1 ] ),
- 3,
- new Position( root, [ 2, 4, 1 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.range.start.path = [ 2, 4, 1 ];
- expected.range.end.path = [ 2, 4, 4 ];
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target inside change range: split into two operations', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 4, 1 ] ),
- 2,
- new Position( root, [ 0, 2, 2 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 2 );
- expected.range.end.offset = 2;
- expectOperation( transOp[ 0 ], expected );
- expected.range.start.offset = 4;
- expected.range.end.offset = 6;
- expectOperation( transOp[ 1 ], expected );
- } );
- it( 'range intersects change range and target inside change range: split into three operations', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 0, 2, 0 ] ),
- 2,
- new Position( root, [ 0, 2, 3 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 3 );
- expected.range.start.path = [ 0, 2, 0 ];
- expected.range.end.path = [ 0, 2, 1 ];
- expectOperation( transOp[ 0 ], expected );
- expected.range.start.path = [ 0, 2, 3 ];
- expected.range.end.path = [ 0, 2, 4 ];
- expectOperation( transOp[ 1 ], expected );
- expected.range.start.path = [ 0, 2, 2 ];
- expected.range.end.path = [ 0, 2, 3 ];
- expectOperation( transOp[ 2 ], expected );
- } );
- } );
- } );
- describe( 'RootAttributeOperation', () => {
- const diffRoot = new RootElement( null );
- beforeEach( () => {
- expected = {
- type: RootAttributeOperation,
- key: 'foo',
- oldValue: 'abc',
- newValue: 'bar'
- };
- op = new RootAttributeOperation( root, 'foo', 'abc', 'bar', 0 );
- } );
- describe( 'by InsertOperation', () => {
- it( 'no operation update', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 0 ] ),
- 'a',
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by AttributeOperation', () => {
- it( 'no operation update', () => {
- const transformBy = new AttributeOperation(
- new Range(
- new Position( root, [ 0 ] ),
- new Position( root, [ 1 ] )
- ),
- 'foo',
- 'bar',
- 'xyz',
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by RootAttributeOperation', () => {
- it( 'changes different root: no operation update', () => {
- const transformBy = new RootAttributeOperation(
- diffRoot,
- 'foo',
- 'abc',
- 'xyz',
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'changes different key: no operation update', () => {
- const transformBy = new RootAttributeOperation(
- root,
- 'abc',
- 'abc',
- 'xyz',
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'sets same value for same key: convert to NoOperation', () => {
- const transformBy = new RootAttributeOperation(
- root,
- 'foo',
- 'abc',
- 'bar',
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], {
- type: NoOperation
- } );
- } );
- it( 'sets different value for same key on same root and is important: convert to NoOperation', () => {
- const transformBy = new RootAttributeOperation(
- root,
- 'foo',
- 'abc',
- 'xyz',
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], {
- type: NoOperation
- } );
- } );
- it( 'sets different value for same key on same root and is less important: change oldValue', () => {
- const transformBy = new RootAttributeOperation(
- root,
- 'foo',
- 'abc',
- 'xyz',
- 0
- );
- const transOp = transform.transform( op, transformBy, contextIsStrong );
- expected.oldValue = 'xyz';
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by MoveOperation', () => {
- it( 'no operation update', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 0 ] ),
- 2,
- new Position( root, [ 1 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by NoOperation', () => {
- it( 'no operation update', () => {
- const transformBy = new NoOperation( 0 );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by RenameOperation', () => {
- it( 'no position update', () => {
- const transformBy = new RenameOperation( new Position( root, [ 0 ] ), 'oldName', 'newName', 0 );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by MarkerOperation', () => {
- it( 'no position update', () => {
- const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 8 ] ) );
- const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, 0 );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- } );
- describe( 'MoveOperation', () => {
- let sourcePosition, targetPosition, rangeEnd, howMany;
- beforeEach( () => {
- sourcePosition = new Position( root, [ 2, 2, 4 ] );
- targetPosition = new Position( root, [ 3, 3, 3 ] );
- howMany = 2;
- rangeEnd = Position.createFromPosition( sourcePosition );
- rangeEnd.offset += howMany;
- op = new MoveOperation( sourcePosition, howMany, targetPosition, 0 );
- expected = {
- type: MoveOperation,
- sourcePosition: Position.createFromPosition( sourcePosition ),
- targetPosition: Position.createFromPosition( targetPosition ),
- howMany
- };
- } );
- describe( 'by InsertOperation', () => {
- it( 'target at different position than move range and target: no operation update', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 1, 3, 2 ] ),
- [ nodeA, nodeB ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target inside node from move range: no operation update', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 2, 2, 4, 1 ] ),
- [ nodeA, nodeB ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target at offset before range offset: increment range offset', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 2, 2, 0 ] ),
- [ nodeA, nodeB ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.sourcePosition.offset += 2;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target at offset after range offset: no operation update', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 2, 2, 7 ] ),
- [ nodeA, nodeB ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target before node from range start path: increment index on range start path', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 2, 0 ] ),
- [ nodeA, nodeB ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.sourcePosition.path[ 1 ] += 2;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target after node from range start path: no operation update', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 2, 3 ] ),
- [ nodeA, nodeB ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target offset before move target offset: increment target offset', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 3, 3, 2 ] ),
- [ nodeA, nodeB ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.targetPosition.offset += 2;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target offset after move target offset: no operation update', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 3, 3, 4 ] ),
- [ nodeA, nodeB ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target before node from move target position path: increment index on move target position path', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 3, 3 ] ),
- [ nodeA, nodeB ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.targetPosition.path[ 1 ] += 2;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target after node from move target position path: no operation update', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 3, 6 ] ),
- [ nodeA, nodeB ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target offset same as move target offset and is important: no operation update', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 3, 3, 3 ] ),
- [ nodeA, nodeB ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target offset same as move target offset and is less important: no operation update', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 3, 3, 3 ] ),
- [ nodeA, nodeB ],
- 0
- );
- const transOp = transform.transform( op, transformBy, contextIsStrong );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target inside move range: expand range', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 2, 2, 5 ] ),
- [ nodeA, nodeB ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expected.howMany = 4;
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target at offset same as range end boundary: no operation update', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 2, 2, 6 ] ),
- [ nodeA, nodeB ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by AttributeOperation', () => {
- it( 'no operation update', () => {
- const transformBy = new AttributeOperation(
- new Range( sourcePosition, rangeEnd ),
- 'abc',
- true,
- false,
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by RootAttributeOperation', () => {
- it( 'no operation update', () => {
- const transformBy = new RootAttributeOperation(
- root,
- 'foo',
- null,
- 'bar',
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by MoveOperation', () => {
- it( 'range and target different than transforming range and target: no operation update', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 1, 2 ] ),
- 3,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target offset before transforming range start offset: increment range offset', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 4, 1, 0 ] ),
- 2,
- new Position( root, [ 2, 2, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.sourcePosition.offset += 2;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target offset after transforming range start offset: no operation update', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 4, 1, 0 ] ),
- 2,
- new Position( root, [ 2, 2, 7 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range start offset before transforming range start offset: decrement range offset', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 2, 0 ] ),
- 2,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.sourcePosition.offset -= 2;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range start offset after transforming range start offset: no operation update', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 2, 9 ] ),
- 2,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy, contextIsStrong );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target before node from transforming range start path: increment index on range start path', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 4, 1, 0 ] ),
- 2,
- new Position( root, [ 2, 1 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.sourcePosition.path[ 1 ] += 2;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target after node from transforming range start path: no operation update', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 4, 1, 0 ] ),
- 2,
- new Position( root, [ 2, 5 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range before node from transforming range start path: decrement index on range start path', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 0 ] ),
- 1,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.sourcePosition.path[ 1 ] -= 1;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range after node from transforming range start path: no operation update', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 3 ] ),
- 2,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target offset before transforming target offset: increment target offset', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 4, 1, 0 ] ),
- 2,
- new Position( root, [ 3, 3, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.targetPosition.offset += 2;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target offset after transforming target offset: no operation update', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 4, 1, 0 ] ),
- 2,
- new Position( root, [ 3, 3, 5 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range offset before transforming target offset: decrement target offset', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 3, 3, 0 ] ),
- 2,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.targetPosition.offset -= 2;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range offset after transforming target offset: no operation update', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 3, 3, 5 ] ),
- 2,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target before node from transforming target path: increment index on target path', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 4, 1, 0 ] ),
- 2,
- new Position( root, [ 3, 1 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.targetPosition.path[ 1 ] += 2;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target after node from transforming target path: no operation update', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 4, 1, 0 ] ),
- 2,
- new Position( root, [ 3, 5 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range before node from transforming target path: decrement index on target path', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 3, 0 ] ),
- 2,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.targetPosition.path[ 1 ] -= 2;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range after node from transforming target path: no operation update', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 3, 5 ] ),
- 2,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target inside transforming move range: split into two operations', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 4, 1, 0 ] ),
- 2,
- new Position( root, [ 2, 2, 5 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 2 );
- expected.howMany = 1;
- expectOperation( transOp[ 0 ], expected );
- expected.sourcePosition.path = [ 2, 2, 6 ];
- expected.targetPosition = targetPosition.getShiftedBy( 1 );
- expectOperation( transOp[ 1 ], expected );
- } );
- it( 'target at start boundary of transforming move range: increment source offset', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 4, 1, 0 ] ),
- 2,
- new Position( root, [ 2, 2, 4 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expected.sourcePosition.offset = 6;
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target at end boundary of transforming move range: no operation update', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 4, 1, 0 ] ),
- 2,
- new Position( root, [ 2, 2, 6 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target inside a node from transforming range: no operation update', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 4, 1, 0 ] ),
- 2,
- new Position( root, [ 2, 2, 5, 1 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range has node that contains transforming range: update range path', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 1 ] ),
- 3,
- new Position( root, [ 4, 2 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.sourcePosition.path = [ 4, 3, 4 ];
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range has node that contains transforming target: update target path', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 3, 2 ] ),
- 3,
- new Position( root, [ 0, 1 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.targetPosition.path = [ 0, 2, 3 ];
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target inside a node from transforming range and vice versa: reverse transform-by operation', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 3, 2 ] ),
- 3,
- new Position( root, [ 2, 2, 5, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- const reversed = transformBy.getReversed();
- expected.sourcePosition = reversed.sourcePosition;
- expected.targetPosition = reversed.targetPosition;
- expected.howMany = reversed.howMany;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range is same as transforming range and is important: convert to NoOperation', () => {
- const transformBy = new MoveOperation(
- op.sourcePosition,
- op.howMany,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], {
- type: NoOperation
- } );
- } );
- it( 'range is same as transforming range and is less important: update range path', () => {
- const transformBy = new MoveOperation(
- op.sourcePosition,
- op.howMany,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy, contextIsStrong );
- expected.sourcePosition.path = [ 4, 1, 0 ];
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range contains transforming range and is important: convert to NoOperation', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 2, 3 ] ),
- 4,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], {
- type: NoOperation
- } );
- } );
- it( 'range contains transforming range and is less important: update range path', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 2, 3 ] ),
- 4,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy, contextIsStrong );
- expected.sourcePosition.path = [ 4, 1, 1 ];
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range contains transforming range and target and is important: update range path and target', () => {
- op.targetPosition.path = [ 2, 2, 7 ];
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 2, 3 ] ),
- 5,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expected.sourcePosition.path = [ 4, 1, 1 ];
- expected.targetPosition.path = [ 4, 1, 4 ];
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range contains transforming range and target and is less important: update range path and target', () => {
- op.targetPosition.path = [ 2, 2, 7 ];
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 2, 3 ] ),
- 5,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy, contextIsStrong );
- expect( transOp.length ).to.equal( 1 );
- expected.sourcePosition.path = [ 4, 1, 1 ];
- expected.targetPosition.path = [ 4, 1, 4 ];
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range intersects on left side of transforming range and is important: shrink range', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 2, 3 ] ),
- 2,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.sourcePosition.path = [ 2, 2, 3 ];
- expected.howMany = 1;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range intersects on left side of transforming range and is less important: split into two operations', () => {
- // Get more test cases and better code coverage
- const otherRoot = new RootElement( null );
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 2, 3 ] ),
- 2,
- new Position( otherRoot, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy, contextIsStrong );
- expect( transOp.length ).to.equal( 2 );
- expected.sourcePosition = new Position( otherRoot, [ 4, 1, 1 ] );
- expected.howMany = 1;
- expectOperation( transOp[ 0 ], expected );
- expected.sourcePosition = new Position( root, [ 2, 2, 3 ] );
- expected.targetPosition = targetPosition.getShiftedBy( 1 );
- expectOperation( transOp[ 1 ], expected );
- } );
- it( 'range intersects on right side of transforming range and is important: shrink range', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 2, 5 ] ),
- 2,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.howMany = 1;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range intersects on right side of transforming range and is less important: split into two operations', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 2, 5 ] ),
- 2,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy, contextIsStrong );
- expect( transOp.length ).to.equal( 2 );
- expected.sourcePosition = sourcePosition;
- expected.howMany = 1;
- expectOperation( transOp[ 0 ], expected );
- expected.sourcePosition = new Position( root, [ 4, 1, 0 ] );
- expected.targetPosition = targetPosition.getShiftedBy( 1 );
- expectOperation( transOp[ 1 ], expected );
- } );
- it( 'range intersects on left side, target inside transforming range and is important: split into two operations', () => {
- op.howMany = 4;
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 2, 3 ] ),
- 2,
- new Position( root, [ 2, 2, 6 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 2 );
- expected.sourcePosition.path = [ 2, 2, 3 ];
- expected.howMany = 1;
- expectOperation( transOp[ 0 ], expected );
- expected.sourcePosition.path = [ 2, 2, 5 ];
- expected.howMany = 2;
- expected.targetPosition = targetPosition.getShiftedBy( 1 );
- expectOperation( transOp[ 1 ], expected );
- } );
- it( 'range intersects on left side, target inside transforming range and is less important: split into two operations', () => {
- op.howMany = 4;
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 2, 3 ] ),
- 2,
- new Position( root, [ 2, 2, 6 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy, contextIsStrong );
- expect( transOp.length ).to.equal( 2 );
- expected.sourcePosition.path = [ 2, 2, 3 ];
- expected.howMany = 1;
- expectOperation( transOp[ 0 ], expected );
- expected.sourcePosition.path = [ 2, 2, 5 ];
- expected.howMany = 2;
- expected.targetPosition = targetPosition.getShiftedBy( 1 );
- expectOperation( transOp[ 1 ], expected );
- } );
- it( 'range intersects on right side of transforming range and is important: shrink range', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 2, 5 ] ),
- 2,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expected.howMany = 1;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range intersects on right side of transforming range and is less important: split into two operations', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 2, 5 ] ),
- 2,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy, contextIsStrong );
- expect( transOp.length ).to.equal( 2 );
- expected.sourcePosition = sourcePosition;
- expected.howMany = 1;
- expectOperation( transOp[ 0 ], expected );
- expected.sourcePosition = new Position( root, [ 4, 1, 0 ] );
- expected.targetPosition = targetPosition.getShiftedBy( 1 );
- expectOperation( transOp[ 1 ], expected );
- } );
- it( 'range intersects, target inside transforming range and is important: split into two operations', () => {
- op.targetPosition.path = [ 2, 2, 7 ];
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 2, 5 ] ),
- 4,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy, contextIsStrong );
- expect( transOp.length ).to.equal( 2 );
- expected.sourcePosition.path = [ 2, 2, 4 ];
- expected.targetPosition.path = [ 4, 1, 2 ];
- expected.howMany = 1;
- expectOperation( transOp[ 0 ], expected );
- expected.sourcePosition.path = [ 4, 1, 0 ];
- expected.targetPosition.path = [ 4, 1, 3 ];
- expectOperation( transOp[ 1 ], expected );
- } );
- it( 'range intersects, target inside transforming range and is less important: shrink range', () => {
- op.targetPosition.path = [ 2, 2, 7 ];
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 2, 5 ] ),
- 4,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expected.sourcePosition.path = [ 2, 2, 4 ];
- expected.targetPosition.path = [ 4, 1, 2 ];
- expected.howMany = 1;
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range inside transforming range and is important: split into two operations', () => {
- op.howMany = 4;
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 2, 5 ] ),
- 2,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 2 );
- expected.sourcePosition.path = [ 2, 2, 4 ];
- expected.howMany = 1;
- expectOperation( transOp[ 0 ], expected );
- expected.targetPosition = targetPosition.getShiftedBy( 1 );
- expectOperation( transOp[ 1 ], expected );
- } );
- it( 'range inside transforming range and is less important: split into three operations', () => {
- op.howMany = 4;
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 2, 5 ] ),
- 2,
- new Position( root, [ 4, 1, 0 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy, contextIsStrong );
- expect( transOp.length ).to.equal( 3 );
- expected.sourcePosition.path = [ 2, 2, 4 ];
- expected.howMany = 1;
- expectOperation( transOp[ 0 ], expected );
- expected.sourcePosition.path = [ 4, 1, 0 ];
- expected.targetPosition = targetPosition.getShiftedBy( 1 );
- expected.howMany = 2;
- expectOperation( transOp[ 1 ], expected );
- expected.sourcePosition.path = [ 2, 2, 4 ];
- expected.targetPosition = targetPosition.getShiftedBy( 3 );
- expected.howMany = 1;
- expectOperation( transOp[ 2 ], expected );
- } );
- it( 'range and target inside transforming range and is important: no operation update', () => {
- op.howMany = 6;
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 2, 5 ] ),
- 2,
- new Position( root, [ 2, 2, 9 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expected.howMany = 6;
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'range and target inside transforming range and is less important: no operation update', () => {
- op.howMany = 6;
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 2, 5 ] ),
- 2,
- new Position( root, [ 2, 2, 9 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy, contextIsStrong );
- expect( transOp.length ).to.equal( 1 );
- expected.howMany = 6;
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by MoveOperation to graveyard', () => {
- let transformBy;
- beforeEach( () => {
- transformBy = new MoveOperation(
- Position.createFromPosition( op.sourcePosition ),
- op.howMany,
- new Position( doc.graveyard, [ 0 ] ),
- 0
- );
- } );
- it( 'should skip context.aIsStrong and be less important than MoveOperation', () => {
- const transOp = transform.transform( op, transformBy, contextIsStrong );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], {
- type: NoOperation
- } );
- } );
- } );
- describe( 'by NoOperation', () => {
- it( 'no operation update', () => {
- const transformBy = new NoOperation( 0 );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by RenameOperation', () => {
- it( 'no position update', () => {
- const transformBy = new RenameOperation( new Position( root, [ 2, 2, 4 ] ), 'oldName', 'newName', 0 );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by MarkerOperation', () => {
- it( 'no position update', () => {
- const newRange = new Range( new Position( root, [ 2, 2, 3 ] ), new Position( root, [ 2, 2, 8 ] ) );
- const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, 0 );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- } );
- describe( 'MoveOperation to graveyard', () => {
- describe( 'by MoveOperation', () => {
- it( 'should force removing content even if was less important', () => {
- const op = new MoveOperation( new Position( root, [ 8 ] ), 2, new Position( doc.graveyard, [ 0 ] ), 0 );
- const targetPosition = Position.createFromPosition( op.targetPosition );
- const transformBy = new MoveOperation( new Position( root, [ 8 ] ), 2, new Position( root, [ 1 ] ), 0 );
- const sourcePosition = Position.createFromPosition( transformBy.targetPosition );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], {
- type: MoveOperation,
- howMany: 2,
- sourcePosition,
- targetPosition
- } );
- } );
- } );
- } );
- describe( 'NoOperation', () => {
- beforeEach( () => {
- op = new NoOperation( 0 );
- expected = {
- type: NoOperation
- };
- } );
- describe( 'by InsertOperation', () => {
- it( 'no operation update', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 0 ] ),
- 'a',
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by AttributeOperation', () => {
- it( 'no operation update', () => {
- const transformBy = new AttributeOperation(
- new Range(
- new Position( root, [ 0 ] ),
- new Position( root, [ 1 ] )
- ),
- 'foo',
- 'bar',
- 'xyz',
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by RootAttributeOperation', () => {
- it( 'no operation update', () => {
- const transformBy = new RootAttributeOperation(
- root,
- 'foo',
- null,
- 'bar',
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by MoveOperation', () => {
- it( 'no operation update', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 0 ] ),
- 2,
- new Position( root, [ 1 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by NoOperation', () => {
- it( 'no operation update', () => {
- const transformBy = new NoOperation( 0 );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by RenameOperation', () => {
- it( 'no position update', () => {
- const transformBy = new RenameOperation( new Position( root, [ 0, 2, 0 ] ), 'oldName', 'newName', 0 );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by MarkerOperation', () => {
- it( 'no position update', () => {
- const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 8 ] ) );
- const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, 0 );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- } );
- describe( 'RenameOperation', () => {
- beforeEach( () => {
- const position = new Position( root, [ 0, 2, 2 ] );
- op = new RenameOperation( position, 'oldName', 'newName', 0 );
- expected = {
- position,
- oldName: 'oldName',
- newName: 'newName'
- };
- } );
- describe( 'by InsertOperation', () => {
- it( 'target before renamed element: offset update', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 0, 2, 1 ] ),
- [ nodeA, nodeB ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expected.position.offset = 4;
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target after renamed element: no change', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 0, 2, 3 ] ),
- [ nodeA, nodeB ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target before a node on path to renamed element: path update', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 0, 1 ] ),
- [ nodeA, nodeB ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expected.position.path = [ 0, 4, 2 ];
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target after a node on path to renamed element: no change', () => {
- const transformBy = new InsertOperation(
- new Position( root, [ 0, 3 ] ),
- [ nodeA, nodeB ],
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by AttributeOperation', () => {
- it( 'no operation update', () => {
- const transformBy = new AttributeOperation(
- new Range(
- new Position( root, [ 0, 2, 1 ] ),
- new Position( root, [ 1, 3 ] )
- ),
- 'foo',
- 'bar',
- 'xyz',
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by RootAttributeOperation', () => {
- it( 'no operation update', () => {
- const transformBy = new RootAttributeOperation(
- root,
- 'foo',
- null,
- 'bar',
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by MarkerOperation', () => {
- it( 'no operation update', () => {
- const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 8 ] ) );
- const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, 0 );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by RenameOperation', () => {
- it( 'different element: no change', () => {
- const transformBy = new RenameOperation(
- new Position( root, [ 0, 2, 1 ] ),
- 'foo',
- 'bar',
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'same element and is important: convert to NoOperation', () => {
- const transformBy = new RenameOperation(
- new Position( root, [ 0, 2, 2 ] ),
- 'oldName',
- 'otherName',
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], {
- type: NoOperation
- } );
- } );
- it( 'same element and is not important: change old name to new name', () => {
- const transformBy = new RenameOperation(
- new Position( root, [ 0, 2, 2 ] ),
- 'oldName',
- 'otherName',
- 0
- );
- const transOp = transform.transform( op, transformBy, contextIsStrong );
- expect( transOp.length ).to.equal( 1 );
- expected.oldName = 'otherName';
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by MoveOperation', () => {
- it( 'moved range before renamed element: update offset', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 0, 2, 0 ] ),
- 2,
- new Position( root, [ 2, 5 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expected.position.offset = 0;
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'moved range before an element on path to renamed element: update path', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 0, 0 ] ),
- 2,
- new Position( root, [ 2, 5 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expected.position.path = [ 0, 0, 2 ];
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'moved range contains renamed element: update path', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 0, 2, 1 ] ),
- 3,
- new Position( root, [ 2, 5 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expected.position.path = [ 2, 6 ];
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'moved range contains renamed element parent: updated path', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 0, 1 ] ),
- 3,
- new Position( root, [ 2, 5 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expected.position.path = [ 2, 6, 2 ];
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'move target before renamed element: update offset', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 2, 5 ] ),
- 2,
- new Position( root, [ 0, 2, 1 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expected.position.offset = 4;
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'move target before an element on path to renamed element: update path', () => {
- const transformBy = new MoveOperation(
- new Position( root, [ 0, 2, 0 ] ),
- 2,
- new Position( root, [ 2, 5 ] ),
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expected.position.offset = 0;
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- } );
- describe( 'MarkerOperation', () => {
- let oldRange, newRange;
- beforeEach( () => {
- oldRange = Range.createFromParentsAndOffsets( root, 1, root, 4 );
- newRange = Range.createFromParentsAndOffsets( root, 10, root, 12 );
- op = new MarkerOperation( 'name', oldRange, newRange, model.markers, 0 );
- expected = {
- name: 'name',
- oldRange,
- newRange
- };
- } );
- describe( 'by InsertOperation', () => {
- it( 'insert position affecting oldRange: update oldRange', () => {
- // Just CC things.
- op.newRange = null;
- const transformBy = new InsertOperation( Position.createAt( root, 0 ), [ nodeA, nodeB ], 0 );
- const transOp = transform.transform( op, transformBy );
- expected.newRange = null;
- expected.oldRange.start.offset = 3;
- expected.oldRange.end.offset = 6;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'insert position affecting newRange: update newRange', () => {
- // Just CC things.
- op.oldRange = null;
- const transformBy = new InsertOperation( Position.createAt( root, 8 ), [ nodeA, nodeB ], 0 );
- const transOp = transform.transform( op, transformBy );
- expected.oldRange = null;
- expected.newRange.start.offset = 12;
- expected.newRange.end.offset = 14;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by AttributeOperation', () => {
- it( 'no operation update', () => {
- const transformBy = new AttributeOperation(
- new Range(
- new Position( root, [ 2 ] ),
- new Position( root, [ 11 ] )
- ),
- 'foo',
- 'bar',
- 'xyz',
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by MoveOperation', () => {
- it( 'moved range is before oldRange: update oldRange', () => {
- // Just CC things.
- op.newRange = null;
- const transformBy = new MoveOperation( Position.createAt( root, 0 ), 1, Position.createAt( root, 20 ), 0 );
- const transOp = transform.transform( op, transformBy );
- expected.newRange = null;
- expected.oldRange.start.offset = 0;
- expected.oldRange.end.offset = 3;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'moved range contains oldRange and is before newRange: update oldRange and newRange', () => {
- const transformBy = new MoveOperation( Position.createAt( root, 2 ), 2, Position.createAt( root, 20 ), 0 );
- const transOp = transform.transform( op, transformBy );
- expected.oldRange.start.offset = 1;
- expected.oldRange.end.offset = 2;
- expected.newRange.start.offset = 8;
- expected.newRange.end.offset = 10;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target position is inside newRange: update newRange', () => {
- // Just CC things.
- op.oldRange = null;
- const transformBy = new MoveOperation( Position.createAt( root, 20 ), 2, Position.createAt( root, 11 ), 0 );
- const transOp = transform.transform( op, transformBy );
- expected.oldRange = null;
- expected.newRange.start.offset = 10;
- expected.newRange.end.offset = 14;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'target position is inside oldRange and before newRange: update oldRange and newRange', () => {
- const transformBy = new MoveOperation( Position.createAt( root, 20 ), 4, Position.createAt( root, 2 ), 0 );
- const transOp = transform.transform( op, transformBy );
- expected.oldRange.start.offset = 1;
- expected.oldRange.end.offset = 8;
- expected.newRange.start.offset = 14;
- expected.newRange.end.offset = 16;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by RootAttributeOperation', () => {
- it( 'no operation update', () => {
- const transformBy = new RootAttributeOperation(
- root,
- 'foo',
- null,
- 'bar',
- 0
- );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by RenameOperation', () => {
- it( 'no operation update', () => {
- const transformBy = new RenameOperation( new Position( root, [ 1 ] ), 'oldName', 'newName', 0 );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- describe( 'by MarkerOperation', () => {
- it( 'different marker name: no operation update', () => {
- const transformBy = new MarkerOperation( 'otherName', oldRange, newRange, model.markers, 0 );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- it( 'same marker name and is important: convert to NoOperation', () => {
- const anotherRange = Range.createFromParentsAndOffsets( root, 2, root, 2 );
- const transformBy = new MarkerOperation( 'name', oldRange, anotherRange, model.markers, 0 );
- const transOp = transform.transform( op, transformBy );
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], {
- type: NoOperation
- } );
- } );
- it( 'same marker name and is less important: update oldRange parameter', () => {
- const anotherRange = Range.createFromParentsAndOffsets( root, 2, root, 2 );
- const transformBy = new MarkerOperation( 'name', oldRange, anotherRange, model.markers, 0 );
- const transOp = transform.transform( op, transformBy, contextIsStrong );
- expected.oldRange = anotherRange;
- expect( transOp.length ).to.equal( 1 );
- expectOperation( transOp[ 0 ], expected );
- } );
- } );
- } );
- } );
|