transform.js 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import transform from '../../../src/model/operation/transform';
  6. import Model from '../../../src/model/model';
  7. import RootElement from '../../../src/model/rootelement';
  8. import Node from '../../../src/model/node';
  9. import Position from '../../../src/model/position';
  10. import Range from '../../../src/model/range';
  11. import InsertOperation from '../../../src/model/operation/insertoperation';
  12. import AttributeOperation from '../../../src/model/operation/attributeoperation';
  13. import RootAttributeOperation from '../../../src/model/operation/rootattributeoperation';
  14. import MarkerOperation from '../../../src/model/operation/markeroperation';
  15. import MoveOperation from '../../../src/model/operation/moveoperation';
  16. import RemoveOperation from '../../../src/model/operation/removeoperation';
  17. import RenameOperation from '../../../src/model/operation/renameoperation';
  18. import NoOperation from '../../../src/model/operation/nooperation';
  19. describe( 'transform', () => {
  20. let model, doc, root, op, nodeA, nodeB, expected;
  21. beforeEach( () => {
  22. model = new Model();
  23. doc = model.document;
  24. root = doc.createRoot();
  25. nodeA = new Node();
  26. nodeB = new Node();
  27. } );
  28. function expectOperation( op, params ) {
  29. for ( const i in params ) {
  30. if ( params.hasOwnProperty( i ) ) {
  31. if ( i == 'type' ) {
  32. expect( op, 'type' ).to.be.instanceof( params[ i ] );
  33. }
  34. else if ( params[ i ] instanceof Array ) {
  35. expect( op[ i ].length, i ).to.equal( params[ i ].length );
  36. for ( let j = 0; j < params[ i ].length; j++ ) {
  37. expect( op[ i ][ j ] ).to.equal( params[ i ][ j ] );
  38. }
  39. } else if ( params[ i ] instanceof Position || params[ i ] instanceof Range ) {
  40. expect( op[ i ].isEqual( params[ i ] ), i ).to.be.true;
  41. } else {
  42. expect( op[ i ], i ).to.equal( params[ i ] );
  43. }
  44. }
  45. }
  46. }
  47. const contextIsStrong = {
  48. aIsStrong: true,
  49. wasUndone: () => false,
  50. getRelation: () => false
  51. };
  52. describe( 'InsertOperation', () => {
  53. let nodeC, nodeD, position;
  54. beforeEach( () => {
  55. nodeC = new Node();
  56. nodeD = new Node();
  57. position = new Position( root, [ 0, 2, 2 ] );
  58. op = new InsertOperation( position, [ nodeA, nodeB ], 0 );
  59. expected = {
  60. type: InsertOperation,
  61. position: Position.createFromPosition( position )
  62. };
  63. } );
  64. describe( 'by InsertOperation', () => {
  65. it( 'target at different position: no position update', () => {
  66. const transformBy = new InsertOperation(
  67. new Position( root, [ 1, 3, 2 ] ),
  68. [ nodeC, nodeD ],
  69. 0
  70. );
  71. const transOp = transform.transform( op, transformBy );
  72. expect( transOp.length ).to.equal( 1 );
  73. expectOperation( transOp[ 0 ], expected );
  74. } );
  75. it( 'target at offset before: increment offset', () => {
  76. const transformBy = new InsertOperation(
  77. new Position( root, [ 0, 2, 0 ] ),
  78. [ nodeC, nodeD ],
  79. 0
  80. );
  81. const transOp = transform.transform( op, transformBy );
  82. expected.position.offset += 2;
  83. expect( transOp.length ).to.equal( 1 );
  84. expectOperation( transOp[ 0 ], expected );
  85. } );
  86. it( 'target at same offset and is important: increment offset', () => {
  87. const transformBy = new InsertOperation(
  88. new Position( root, [ 0, 2, 2 ] ),
  89. [ nodeC, nodeD ],
  90. 0
  91. );
  92. const transOp = transform.transform( op, transformBy );
  93. expected.position.offset += 2;
  94. expect( transOp.length ).to.equal( 1 );
  95. expectOperation( transOp[ 0 ], expected );
  96. } );
  97. it( 'target at same offset and is less important: no position update', () => {
  98. const transformBy = new InsertOperation(
  99. new Position( root, [ 0, 2, 2 ] ),
  100. [ nodeC, nodeD ],
  101. 0
  102. );
  103. const transOp = transform.transform( op, transformBy, contextIsStrong );
  104. expect( transOp.length ).to.equal( 1 );
  105. expectOperation( transOp[ 0 ], expected );
  106. } );
  107. it( 'target at same offset and context.insertBefore = false: increment offset', () => {
  108. const transformBy = new InsertOperation(
  109. new Position( root, [ 0, 2, 2 ] ),
  110. [ nodeC, nodeD ],
  111. 0
  112. );
  113. const transOp = transform.transform( op, transformBy );
  114. expected.position.offset += 2;
  115. expect( transOp.length ).to.equal( 1 );
  116. expectOperation( transOp[ 0 ], expected );
  117. } );
  118. it( 'target at offset after: no position update', () => {
  119. const transformBy = new InsertOperation(
  120. new Position( root, [ 0, 2, 3 ] ),
  121. [ nodeC, nodeD ],
  122. 0
  123. );
  124. const transOp = transform.transform( op, transformBy );
  125. expect( transOp.length ).to.equal( 1 );
  126. expectOperation( transOp[ 0 ], expected );
  127. } );
  128. it( 'target before node from path: increment index on path', () => {
  129. const transformBy = new InsertOperation(
  130. new Position( root, [ 0, 1 ] ),
  131. [ nodeC, nodeD ],
  132. 0
  133. );
  134. const transOp = transform.transform( op, transformBy );
  135. expected.position.path[ 1 ] += 2;
  136. expect( transOp.length ).to.equal( 1 );
  137. expectOperation( transOp[ 0 ], expected );
  138. } );
  139. it( 'target after node from path: no position update', () => {
  140. const transformBy = new InsertOperation(
  141. new Position( root, [ 0, 6 ] ),
  142. [ nodeC, nodeD ],
  143. 0
  144. );
  145. const transOp = transform.transform( op, transformBy );
  146. expect( transOp.length ).to.equal( 1 );
  147. expectOperation( transOp[ 0 ], expected );
  148. } );
  149. } );
  150. describe( 'by AttributeOperation', () => {
  151. it( 'no position update', () => {
  152. const transformBy = new AttributeOperation(
  153. Range.createFromPositionAndShift( position, 2 ),
  154. 'foo',
  155. null,
  156. 'bar',
  157. 0
  158. );
  159. const transOp = transform.transform( op, transformBy );
  160. expect( transOp.length ).to.equal( 1 );
  161. expectOperation( transOp[ 0 ], expected );
  162. } );
  163. } );
  164. describe( 'by RootAttributeOperation', () => {
  165. it( 'no position update', () => {
  166. const transformBy = new RootAttributeOperation(
  167. root,
  168. 'foo',
  169. null,
  170. 'bar',
  171. 0
  172. );
  173. const transOp = transform.transform( op, transformBy );
  174. expect( transOp.length ).to.equal( 1 );
  175. expectOperation( transOp[ 0 ], expected );
  176. } );
  177. } );
  178. describe( 'by MoveOperation', () => {
  179. it( 'range and target are different than insert position: no position update', () => {
  180. const transformBy = new MoveOperation(
  181. new Position( root, [ 1, 3, 2 ] ),
  182. 2,
  183. new Position( root, [ 2, 1 ] ),
  184. 0
  185. );
  186. const transOp = transform.transform( op, transformBy );
  187. expect( transOp.length ).to.equal( 1 );
  188. expectOperation( transOp[ 0 ], expected );
  189. } );
  190. it( 'range is before insert position offset: decrement offset', () => {
  191. const transformBy = new MoveOperation(
  192. new Position( root, [ 0, 2, 0 ] ),
  193. 1,
  194. new Position( root, [ 1, 1 ] ),
  195. 0
  196. );
  197. const transOp = transform.transform( op, transformBy );
  198. expected.position.offset--;
  199. expect( transOp.length ).to.equal( 1 );
  200. expectOperation( transOp[ 0 ], expected );
  201. } );
  202. it( 'range offset is after insert position offset: no position update', () => {
  203. const transformBy = new MoveOperation(
  204. new Position( root, [ 0, 2, 5 ] ),
  205. 1,
  206. new Position( root, [ 1, 1 ] ),
  207. 0
  208. );
  209. const transOp = transform.transform( op, transformBy );
  210. expect( transOp.length ).to.equal( 1 );
  211. expectOperation( transOp[ 0 ], expected );
  212. } );
  213. it( 'target offset before insert position offset: increment offset', () => {
  214. const transformBy = new MoveOperation(
  215. new Position( root, [ 1, 1 ] ),
  216. 2,
  217. new Position( root, [ 0, 2, 0 ] ),
  218. 0
  219. );
  220. const transOp = transform.transform( op, transformBy );
  221. expected.position.offset += 2;
  222. expect( transOp.length ).to.equal( 1 );
  223. expectOperation( transOp[ 0 ], expected );
  224. } );
  225. it( 'target offset after insert position offset: no position update', () => {
  226. const transformBy = new MoveOperation(
  227. new Position( root, [ 1, 1 ] ),
  228. 2,
  229. new Position( root, [ 0, 2, 5 ] ),
  230. 0
  231. );
  232. const transOp = transform.transform( op, transformBy );
  233. expect( transOp.length ).to.equal( 1 );
  234. expectOperation( transOp[ 0 ], expected );
  235. } );
  236. it( 'target offset same as insert position offset and is important: increment offset', () => {
  237. const transformBy = new MoveOperation(
  238. new Position( root, [ 1, 1 ] ),
  239. 2,
  240. new Position( root, [ 0, 2, 2 ] ),
  241. 0
  242. );
  243. const transOp = transform.transform( op, transformBy );
  244. expected.position.offset += 2;
  245. expect( transOp.length ).to.equal( 1 );
  246. expectOperation( transOp[ 0 ], expected );
  247. } );
  248. it( 'target offset same as insert position offset and is less important: increment offset', () => {
  249. const transformBy = new MoveOperation(
  250. new Position( root, [ 1, 1 ] ),
  251. 2,
  252. new Position( root, [ 0, 2, 2 ] ),
  253. 0
  254. );
  255. const transOp = transform.transform( op, transformBy, contextIsStrong );
  256. expected.position.offset += 2;
  257. expect( transOp.length ).to.equal( 1 );
  258. expectOperation( transOp[ 0 ], expected );
  259. } );
  260. it( 'range is before node from insert position path: decrement index on path', () => {
  261. const transformBy = new MoveOperation(
  262. new Position( root, [ 0, 0 ] ),
  263. 1,
  264. new Position( root, [ 1, 0 ] ),
  265. 0
  266. );
  267. const transOp = transform.transform( op, transformBy );
  268. expected.position.path[ 1 ] -= 1;
  269. expect( transOp.length ).to.equal( 1 );
  270. expectOperation( transOp[ 0 ], expected );
  271. } );
  272. it( 'range is after node from insert position path: no position update', () => {
  273. const transformBy = new MoveOperation(
  274. new Position( root, [ 0, 4 ] ),
  275. 2,
  276. new Position( root, [ 1, 0 ] ),
  277. 0
  278. );
  279. const transOp = transform.transform( op, transformBy );
  280. expect( transOp.length ).to.equal( 1 );
  281. expectOperation( transOp[ 0 ], expected );
  282. } );
  283. it( 'target before node from insert position path: increment index on path', () => {
  284. const transformBy = new MoveOperation(
  285. new Position( root, [ 1, 0 ] ),
  286. 2,
  287. new Position( root, [ 0, 0 ] ),
  288. 0
  289. );
  290. const transOp = transform.transform( op, transformBy );
  291. expected.position.path[ 1 ] += 2;
  292. expect( transOp.length ).to.equal( 1 );
  293. expectOperation( transOp[ 0 ], expected );
  294. } );
  295. it( 'target after node from insert position path: no position update', () => {
  296. const transformBy = new MoveOperation(
  297. new Position( root, [ 1, 0 ] ),
  298. 2,
  299. new Position( root, [ 0, 4 ] ),
  300. 0
  301. );
  302. const transOp = transform.transform( op, transformBy );
  303. expect( transOp.length ).to.equal( 1 );
  304. expectOperation( transOp[ 0 ], expected );
  305. } );
  306. it( 'range has node that contains insert position: update position', () => {
  307. const transformBy = new MoveOperation(
  308. new Position( root, [ 0, 1 ] ),
  309. 2,
  310. new Position( root, [ 1, 1 ] ),
  311. 0
  312. );
  313. const transOp = transform.transform( op, transformBy );
  314. expected.position.path = [ 1, 2, 2 ];
  315. expect( transOp.length ).to.equal( 1 );
  316. expectOperation( transOp[ 0 ], expected );
  317. } );
  318. it( 'range contains insert position (on same level): update position', () => {
  319. const transformBy = new MoveOperation(
  320. new Position( root, [ 0, 2, 0 ] ),
  321. 4,
  322. new Position( root, [ 1, 0 ] ),
  323. 0
  324. );
  325. const transOp = transform.transform( op, transformBy );
  326. expected.position.path = [ 1, 2 ];
  327. expect( transOp.length ).to.equal( 1 );
  328. expectOperation( transOp[ 0 ], expected );
  329. } );
  330. } );
  331. describe( 'by NoOperation', () => {
  332. it( 'no operation update', () => {
  333. const transformBy = new NoOperation( 0 );
  334. const transOp = transform.transform( op, transformBy );
  335. expect( transOp.length ).to.equal( 1 );
  336. expectOperation( transOp[ 0 ], expected );
  337. } );
  338. } );
  339. describe( 'by RenameOperation', () => {
  340. it( 'no position update', () => {
  341. const transformBy = new RenameOperation( new Position( root, [ 0, 2, 0 ] ), 'oldName', 'newName', 0 );
  342. const transOp = transform.transform( op, transformBy );
  343. expect( transOp.length ).to.equal( 1 );
  344. expectOperation( transOp[ 0 ], expected );
  345. } );
  346. } );
  347. describe( 'by MarkerOperation', () => {
  348. it( 'no position update', () => {
  349. const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 4 ] ) );
  350. const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, 0 );
  351. const transOp = transform.transform( op, transformBy );
  352. expect( transOp.length ).to.equal( 1 );
  353. expectOperation( transOp[ 0 ], expected );
  354. } );
  355. } );
  356. } );
  357. describe( 'AttributeOperation', () => {
  358. let start, end, range;
  359. beforeEach( () => {
  360. start = new Position( root, [ 0, 2, 1 ] );
  361. end = new Position( root, [ 0, 2, 4 ] );
  362. range = new Range( start, end );
  363. op = new AttributeOperation( range, 'foo', 'abc', 'bar', 0 );
  364. expected = {
  365. type: AttributeOperation,
  366. range,
  367. key: 'foo',
  368. oldValue: 'abc',
  369. newValue: 'bar'
  370. };
  371. } );
  372. describe( 'by InsertOperation', () => {
  373. it( 'target at offset before: increment offset', () => {
  374. const transformBy = new InsertOperation(
  375. new Position( root, [ 0, 2, 0 ] ),
  376. [ nodeA, nodeB ],
  377. 0
  378. );
  379. const transOp = transform.transform( op, transformBy );
  380. expected.range.start.offset += 2;
  381. expected.range.end.offset += 2;
  382. expect( transOp.length ).to.equal( 1 );
  383. expectOperation( transOp[ 0 ], expected );
  384. } );
  385. it( 'target at same offset: increment offset', () => {
  386. const transformBy = new InsertOperation(
  387. new Position( root, [ 0, 2, 1 ] ),
  388. [ nodeA, nodeB ],
  389. 0
  390. );
  391. const transOp = transform.transform( op, transformBy );
  392. expected.range.start.offset += 2;
  393. expected.range.end.offset += 2;
  394. expect( transOp.length ).to.equal( 1 );
  395. expectOperation( transOp[ 0 ], expected );
  396. } );
  397. } );
  398. describe( 'by MoveOperation', () => {
  399. it( 'range offset is before change range start offset: decrement offset', () => {
  400. const transformBy = new MoveOperation(
  401. new Position( root, [ 0, 2, 0 ] ),
  402. 1,
  403. new Position( root, [ 2, 4, 1 ] ),
  404. 0
  405. );
  406. const transOp = transform.transform( op, transformBy );
  407. expected.range.start.offset--;
  408. expected.range.end.offset--;
  409. expect( transOp.length ).to.equal( 1 );
  410. expectOperation( transOp[ 0 ], expected );
  411. } );
  412. it( 'target offset is before change range start offset: increment offset', () => {
  413. const transformBy = new MoveOperation(
  414. new Position( root, [ 2, 4, 1 ] ),
  415. 2,
  416. new Position( root, [ 0, 2, 0 ] ),
  417. 0
  418. );
  419. const transOp = transform.transform( op, transformBy );
  420. expected.range.start.offset += 2;
  421. expected.range.end.offset += 2;
  422. expect( transOp.length ).to.equal( 1 );
  423. expectOperation( transOp[ 0 ], expected );
  424. } );
  425. it( 'range intersects on left with change range: split into two operations', () => {
  426. const transformBy = new MoveOperation(
  427. new Position( root, [ 0, 2, 2 ] ),
  428. 4,
  429. new Position( root, [ 2, 4, 1 ] ),
  430. 0
  431. );
  432. const transOp = transform.transform( op, transformBy );
  433. expect( transOp.length ).to.equal( 2 );
  434. expected.range.end.offset -= 2;
  435. expectOperation( transOp[ 0 ], expected );
  436. expected.range.start.path = [ 2, 4, 1 ];
  437. expected.range.end.path = [ 2, 4, 3 ];
  438. expectOperation( transOp[ 1 ], expected );
  439. } );
  440. it( 'range intersects on right with change range: split into two operation', () => {
  441. const transformBy = new MoveOperation(
  442. new Position( root, [ 0, 2, 0 ] ),
  443. 2,
  444. new Position( root, [ 2, 4, 1 ] ),
  445. 0
  446. );
  447. const transOp = transform.transform( op, transformBy );
  448. expect( transOp.length ).to.equal( 2 );
  449. expected.range.start.offset -= 1;
  450. expected.range.end.offset -= 2;
  451. expectOperation( transOp[ 0 ], expected );
  452. expected.range.start.path = [ 2, 4, 2 ];
  453. expected.range.end.path = [ 2, 4, 3 ];
  454. expectOperation( transOp[ 1 ], expected );
  455. } );
  456. it( 'range contains change range: update change range', () => {
  457. const transformBy = new MoveOperation(
  458. new Position( root, [ 0, 1 ] ),
  459. 3,
  460. new Position( root, [ 2, 4, 1 ] ),
  461. 0
  462. );
  463. const transOp = transform.transform( op, transformBy );
  464. expected.range.start.path = [ 2, 4, 2, 1 ];
  465. expected.range.end.path = [ 2, 4, 2, 4 ];
  466. expect( transOp.length ).to.equal( 1 );
  467. expectOperation( transOp[ 0 ], expected );
  468. } );
  469. it( 'range is inside change range: split into two operations', () => {
  470. const transformBy = new MoveOperation(
  471. new Position( root, [ 0, 2, 2 ] ),
  472. 1,
  473. new Position( root, [ 2, 4, 1 ] ),
  474. 0
  475. );
  476. const transOp = transform.transform( op, transformBy );
  477. expect( transOp.length ).to.equal( 3 );
  478. expected.range.start.path = [ 0, 2, 1 ];
  479. expected.range.end.path = [ 0, 2, 2 ];
  480. expectOperation( transOp[ 0 ], expected );
  481. expected.range.start.path = [ 0, 2, 2 ];
  482. expected.range.end.path = [ 0, 2, 3 ];
  483. expectOperation( transOp[ 1 ], expected );
  484. expected.range.start.path = [ 2, 4, 1 ];
  485. expected.range.end.path = [ 2, 4, 2 ];
  486. expectOperation( transOp[ 2 ], expected );
  487. } );
  488. it( 'range is same as change range: update change range', () => {
  489. const transformBy = new MoveOperation(
  490. new Position( root, [ 0, 2, 1 ] ),
  491. 3,
  492. new Position( root, [ 2, 4, 1 ] ),
  493. 0
  494. );
  495. const transOp = transform.transform( op, transformBy );
  496. expected.range.start.path = [ 2, 4, 1 ];
  497. expected.range.end.path = [ 2, 4, 4 ];
  498. expect( transOp.length ).to.equal( 1 );
  499. expectOperation( transOp[ 0 ], expected );
  500. } );
  501. it( 'target inside change range: split into two operations', () => {
  502. const transformBy = new MoveOperation(
  503. new Position( root, [ 2, 4, 1 ] ),
  504. 2,
  505. new Position( root, [ 0, 2, 2 ] ),
  506. 0
  507. );
  508. const transOp = transform.transform( op, transformBy );
  509. expect( transOp.length ).to.equal( 2 );
  510. expected.range.end.offset = 2;
  511. expectOperation( transOp[ 0 ], expected );
  512. expected.range.start.offset = 4;
  513. expected.range.end.offset = 6;
  514. expectOperation( transOp[ 1 ], expected );
  515. } );
  516. it( 'range intersects change range and target inside change range: split into three operations', () => {
  517. const transformBy = new MoveOperation(
  518. new Position( root, [ 0, 2, 0 ] ),
  519. 2,
  520. new Position( root, [ 0, 2, 3 ] ),
  521. 0
  522. );
  523. const transOp = transform.transform( op, transformBy );
  524. expect( transOp.length ).to.equal( 3 );
  525. expected.range.start.path = [ 0, 2, 0 ];
  526. expected.range.end.path = [ 0, 2, 1 ];
  527. expectOperation( transOp[ 0 ], expected );
  528. expected.range.start.path = [ 0, 2, 3 ];
  529. expected.range.end.path = [ 0, 2, 4 ];
  530. expectOperation( transOp[ 1 ], expected );
  531. expected.range.start.path = [ 0, 2, 2 ];
  532. expected.range.end.path = [ 0, 2, 3 ];
  533. expectOperation( transOp[ 2 ], expected );
  534. } );
  535. } );
  536. } );
  537. describe( 'RootAttributeOperation', () => {
  538. const diffRoot = new RootElement( null );
  539. beforeEach( () => {
  540. expected = {
  541. type: RootAttributeOperation,
  542. key: 'foo',
  543. oldValue: 'abc',
  544. newValue: 'bar'
  545. };
  546. op = new RootAttributeOperation( root, 'foo', 'abc', 'bar', 0 );
  547. } );
  548. describe( 'by InsertOperation', () => {
  549. it( 'no operation update', () => {
  550. const transformBy = new InsertOperation(
  551. new Position( root, [ 0 ] ),
  552. 'a',
  553. 0
  554. );
  555. const transOp = transform.transform( op, transformBy );
  556. expect( transOp.length ).to.equal( 1 );
  557. expectOperation( transOp[ 0 ], expected );
  558. } );
  559. } );
  560. describe( 'by AttributeOperation', () => {
  561. it( 'no operation update', () => {
  562. const transformBy = new AttributeOperation(
  563. new Range(
  564. new Position( root, [ 0 ] ),
  565. new Position( root, [ 1 ] )
  566. ),
  567. 'foo',
  568. 'bar',
  569. 'xyz',
  570. 0
  571. );
  572. const transOp = transform.transform( op, transformBy );
  573. expect( transOp.length ).to.equal( 1 );
  574. expectOperation( transOp[ 0 ], expected );
  575. } );
  576. } );
  577. describe( 'by RootAttributeOperation', () => {
  578. it( 'changes different root: no operation update', () => {
  579. const transformBy = new RootAttributeOperation(
  580. diffRoot,
  581. 'foo',
  582. 'abc',
  583. 'xyz',
  584. 0
  585. );
  586. const transOp = transform.transform( op, transformBy );
  587. expect( transOp.length ).to.equal( 1 );
  588. expectOperation( transOp[ 0 ], expected );
  589. } );
  590. it( 'changes different key: no operation update', () => {
  591. const transformBy = new RootAttributeOperation(
  592. root,
  593. 'abc',
  594. 'abc',
  595. 'xyz',
  596. 0
  597. );
  598. const transOp = transform.transform( op, transformBy );
  599. expect( transOp.length ).to.equal( 1 );
  600. expectOperation( transOp[ 0 ], expected );
  601. } );
  602. it( 'sets same value for same key: convert to NoOperation', () => {
  603. const transformBy = new RootAttributeOperation(
  604. root,
  605. 'foo',
  606. 'abc',
  607. 'bar',
  608. 0
  609. );
  610. const transOp = transform.transform( op, transformBy );
  611. expect( transOp.length ).to.equal( 1 );
  612. expectOperation( transOp[ 0 ], {
  613. type: NoOperation
  614. } );
  615. } );
  616. it( 'sets different value for same key on same root and is important: convert to NoOperation', () => {
  617. const transformBy = new RootAttributeOperation(
  618. root,
  619. 'foo',
  620. 'abc',
  621. 'xyz',
  622. 0
  623. );
  624. const transOp = transform.transform( op, transformBy );
  625. expect( transOp.length ).to.equal( 1 );
  626. expectOperation( transOp[ 0 ], {
  627. type: NoOperation
  628. } );
  629. } );
  630. it( 'sets different value for same key on same root and is less important: change oldValue', () => {
  631. const transformBy = new RootAttributeOperation(
  632. root,
  633. 'foo',
  634. 'abc',
  635. 'xyz',
  636. 0
  637. );
  638. const transOp = transform.transform( op, transformBy, contextIsStrong );
  639. expected.oldValue = 'xyz';
  640. expect( transOp.length ).to.equal( 1 );
  641. expectOperation( transOp[ 0 ], expected );
  642. } );
  643. } );
  644. describe( 'by MoveOperation', () => {
  645. it( 'no operation update', () => {
  646. const transformBy = new MoveOperation(
  647. new Position( root, [ 0 ] ),
  648. 2,
  649. new Position( root, [ 1 ] ),
  650. 0
  651. );
  652. const transOp = transform.transform( op, transformBy );
  653. expect( transOp.length ).to.equal( 1 );
  654. expectOperation( transOp[ 0 ], expected );
  655. } );
  656. } );
  657. describe( 'by NoOperation', () => {
  658. it( 'no operation update', () => {
  659. const transformBy = new NoOperation( 0 );
  660. const transOp = transform.transform( op, transformBy );
  661. expect( transOp.length ).to.equal( 1 );
  662. expectOperation( transOp[ 0 ], expected );
  663. } );
  664. } );
  665. describe( 'by RenameOperation', () => {
  666. it( 'no position update', () => {
  667. const transformBy = new RenameOperation( new Position( root, [ 0 ] ), 'oldName', 'newName', 0 );
  668. const transOp = transform.transform( op, transformBy );
  669. expect( transOp.length ).to.equal( 1 );
  670. expectOperation( transOp[ 0 ], expected );
  671. } );
  672. } );
  673. describe( 'by MarkerOperation', () => {
  674. it( 'no position update', () => {
  675. const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 8 ] ) );
  676. const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, 0 );
  677. const transOp = transform.transform( op, transformBy );
  678. expect( transOp.length ).to.equal( 1 );
  679. expectOperation( transOp[ 0 ], expected );
  680. } );
  681. } );
  682. } );
  683. describe( 'MoveOperation', () => {
  684. let sourcePosition, targetPosition, rangeEnd, howMany;
  685. beforeEach( () => {
  686. sourcePosition = new Position( root, [ 2, 2, 4 ] );
  687. targetPosition = new Position( root, [ 3, 3, 3 ] );
  688. howMany = 2;
  689. rangeEnd = Position.createFromPosition( sourcePosition );
  690. rangeEnd.offset += howMany;
  691. op = new MoveOperation( sourcePosition, howMany, targetPosition, 0 );
  692. expected = {
  693. type: MoveOperation,
  694. sourcePosition: Position.createFromPosition( sourcePosition ),
  695. targetPosition: Position.createFromPosition( targetPosition ),
  696. howMany
  697. };
  698. } );
  699. describe( 'by InsertOperation', () => {
  700. it( 'target at different position than move range and target: no operation update', () => {
  701. const transformBy = new InsertOperation(
  702. new Position( root, [ 1, 3, 2 ] ),
  703. [ nodeA, nodeB ],
  704. 0
  705. );
  706. const transOp = transform.transform( op, transformBy );
  707. expect( transOp.length ).to.equal( 1 );
  708. expectOperation( transOp[ 0 ], expected );
  709. } );
  710. it( 'target inside node from move range: no operation update', () => {
  711. const transformBy = new InsertOperation(
  712. new Position( root, [ 2, 2, 4, 1 ] ),
  713. [ nodeA, nodeB ],
  714. 0
  715. );
  716. const transOp = transform.transform( op, transformBy );
  717. expect( transOp.length ).to.equal( 1 );
  718. expectOperation( transOp[ 0 ], expected );
  719. } );
  720. it( 'target at offset before range offset: increment range offset', () => {
  721. const transformBy = new InsertOperation(
  722. new Position( root, [ 2, 2, 0 ] ),
  723. [ nodeA, nodeB ],
  724. 0
  725. );
  726. const transOp = transform.transform( op, transformBy );
  727. expected.sourcePosition.offset += 2;
  728. expect( transOp.length ).to.equal( 1 );
  729. expectOperation( transOp[ 0 ], expected );
  730. } );
  731. it( 'target at offset after range offset: no operation update', () => {
  732. const transformBy = new InsertOperation(
  733. new Position( root, [ 2, 2, 7 ] ),
  734. [ nodeA, nodeB ],
  735. 0
  736. );
  737. const transOp = transform.transform( op, transformBy );
  738. expect( transOp.length ).to.equal( 1 );
  739. expectOperation( transOp[ 0 ], expected );
  740. } );
  741. it( 'target before node from range start path: increment index on range start path', () => {
  742. const transformBy = new InsertOperation(
  743. new Position( root, [ 2, 0 ] ),
  744. [ nodeA, nodeB ],
  745. 0
  746. );
  747. const transOp = transform.transform( op, transformBy );
  748. expected.sourcePosition.path[ 1 ] += 2;
  749. expect( transOp.length ).to.equal( 1 );
  750. expectOperation( transOp[ 0 ], expected );
  751. } );
  752. it( 'target after node from range start path: no operation update', () => {
  753. const transformBy = new InsertOperation(
  754. new Position( root, [ 2, 3 ] ),
  755. [ nodeA, nodeB ],
  756. 0
  757. );
  758. const transOp = transform.transform( op, transformBy );
  759. expect( transOp.length ).to.equal( 1 );
  760. expectOperation( transOp[ 0 ], expected );
  761. } );
  762. it( 'target offset before move target offset: increment target offset', () => {
  763. const transformBy = new InsertOperation(
  764. new Position( root, [ 3, 3, 2 ] ),
  765. [ nodeA, nodeB ],
  766. 0
  767. );
  768. const transOp = transform.transform( op, transformBy );
  769. expected.targetPosition.offset += 2;
  770. expect( transOp.length ).to.equal( 1 );
  771. expectOperation( transOp[ 0 ], expected );
  772. } );
  773. it( 'target offset after move target offset: no operation update', () => {
  774. const transformBy = new InsertOperation(
  775. new Position( root, [ 3, 3, 4 ] ),
  776. [ nodeA, nodeB ],
  777. 0
  778. );
  779. const transOp = transform.transform( op, transformBy );
  780. expect( transOp.length ).to.equal( 1 );
  781. expectOperation( transOp[ 0 ], expected );
  782. } );
  783. it( 'target before node from move target position path: increment index on move target position path', () => {
  784. const transformBy = new InsertOperation(
  785. new Position( root, [ 3, 3 ] ),
  786. [ nodeA, nodeB ],
  787. 0
  788. );
  789. const transOp = transform.transform( op, transformBy );
  790. expected.targetPosition.path[ 1 ] += 2;
  791. expect( transOp.length ).to.equal( 1 );
  792. expectOperation( transOp[ 0 ], expected );
  793. } );
  794. it( 'target after node from move target position path: no operation update', () => {
  795. const transformBy = new InsertOperation(
  796. new Position( root, [ 3, 6 ] ),
  797. [ nodeA, nodeB ],
  798. 0
  799. );
  800. const transOp = transform.transform( op, transformBy );
  801. expect( transOp.length ).to.equal( 1 );
  802. expectOperation( transOp[ 0 ], expected );
  803. } );
  804. it( 'target offset same as move target offset and is important: no operation update', () => {
  805. const transformBy = new InsertOperation(
  806. new Position( root, [ 3, 3, 3 ] ),
  807. [ nodeA, nodeB ],
  808. 0
  809. );
  810. const transOp = transform.transform( op, transformBy );
  811. expect( transOp.length ).to.equal( 1 );
  812. expectOperation( transOp[ 0 ], expected );
  813. } );
  814. it( 'target offset same as move target offset and is less important: no operation update', () => {
  815. const transformBy = new InsertOperation(
  816. new Position( root, [ 3, 3, 3 ] ),
  817. [ nodeA, nodeB ],
  818. 0
  819. );
  820. const transOp = transform.transform( op, transformBy, contextIsStrong );
  821. expect( transOp.length ).to.equal( 1 );
  822. expectOperation( transOp[ 0 ], expected );
  823. } );
  824. it( 'target inside move range: expand range', () => {
  825. const transformBy = new InsertOperation(
  826. new Position( root, [ 2, 2, 5 ] ),
  827. [ nodeA, nodeB ],
  828. 0
  829. );
  830. const transOp = transform.transform( op, transformBy );
  831. expect( transOp.length ).to.equal( 1 );
  832. expected.howMany = 4;
  833. expectOperation( transOp[ 0 ], expected );
  834. } );
  835. it( 'target at offset same as range end boundary: no operation update', () => {
  836. const transformBy = new InsertOperation(
  837. new Position( root, [ 2, 2, 6 ] ),
  838. [ nodeA, nodeB ],
  839. 0
  840. );
  841. const transOp = transform.transform( op, transformBy );
  842. expect( transOp.length ).to.equal( 1 );
  843. expectOperation( transOp[ 0 ], expected );
  844. } );
  845. } );
  846. describe( 'by AttributeOperation', () => {
  847. it( 'no operation update', () => {
  848. const transformBy = new AttributeOperation(
  849. new Range( sourcePosition, rangeEnd ),
  850. 'abc',
  851. true,
  852. false,
  853. 0
  854. );
  855. const transOp = transform.transform( op, transformBy );
  856. expect( transOp.length ).to.equal( 1 );
  857. expectOperation( transOp[ 0 ], expected );
  858. } );
  859. } );
  860. describe( 'by RootAttributeOperation', () => {
  861. it( 'no operation update', () => {
  862. const transformBy = new RootAttributeOperation(
  863. root,
  864. 'foo',
  865. null,
  866. 'bar',
  867. 0
  868. );
  869. const transOp = transform.transform( op, transformBy );
  870. expect( transOp.length ).to.equal( 1 );
  871. expectOperation( transOp[ 0 ], expected );
  872. } );
  873. } );
  874. describe( 'by MoveOperation', () => {
  875. it( 'range and target different than transforming range and target: no operation update', () => {
  876. const transformBy = new MoveOperation(
  877. new Position( root, [ 1, 2 ] ),
  878. 3,
  879. new Position( root, [ 4, 1, 0 ] ),
  880. 0
  881. );
  882. const transOp = transform.transform( op, transformBy );
  883. expect( transOp.length ).to.equal( 1 );
  884. expectOperation( transOp[ 0 ], expected );
  885. } );
  886. it( 'target offset before transforming range start offset: increment range offset', () => {
  887. const transformBy = new MoveOperation(
  888. new Position( root, [ 4, 1, 0 ] ),
  889. 2,
  890. new Position( root, [ 2, 2, 0 ] ),
  891. 0
  892. );
  893. const transOp = transform.transform( op, transformBy );
  894. expected.sourcePosition.offset += 2;
  895. expect( transOp.length ).to.equal( 1 );
  896. expectOperation( transOp[ 0 ], expected );
  897. } );
  898. it( 'target offset after transforming range start offset: no operation update', () => {
  899. const transformBy = new MoveOperation(
  900. new Position( root, [ 4, 1, 0 ] ),
  901. 2,
  902. new Position( root, [ 2, 2, 7 ] ),
  903. 0
  904. );
  905. const transOp = transform.transform( op, transformBy );
  906. expect( transOp.length ).to.equal( 1 );
  907. expectOperation( transOp[ 0 ], expected );
  908. } );
  909. it( 'range start offset before transforming range start offset: decrement range offset', () => {
  910. const transformBy = new MoveOperation(
  911. new Position( root, [ 2, 2, 0 ] ),
  912. 2,
  913. new Position( root, [ 4, 1, 0 ] ),
  914. 0
  915. );
  916. const transOp = transform.transform( op, transformBy );
  917. expected.sourcePosition.offset -= 2;
  918. expect( transOp.length ).to.equal( 1 );
  919. expectOperation( transOp[ 0 ], expected );
  920. } );
  921. it( 'range start offset after transforming range start offset: no operation update', () => {
  922. const transformBy = new MoveOperation(
  923. new Position( root, [ 2, 2, 9 ] ),
  924. 2,
  925. new Position( root, [ 4, 1, 0 ] ),
  926. 0
  927. );
  928. const transOp = transform.transform( op, transformBy, contextIsStrong );
  929. expect( transOp.length ).to.equal( 1 );
  930. expectOperation( transOp[ 0 ], expected );
  931. } );
  932. it( 'target before node from transforming range start path: increment index on range start path', () => {
  933. const transformBy = new MoveOperation(
  934. new Position( root, [ 4, 1, 0 ] ),
  935. 2,
  936. new Position( root, [ 2, 1 ] ),
  937. 0
  938. );
  939. const transOp = transform.transform( op, transformBy );
  940. expected.sourcePosition.path[ 1 ] += 2;
  941. expect( transOp.length ).to.equal( 1 );
  942. expectOperation( transOp[ 0 ], expected );
  943. } );
  944. it( 'target after node from transforming range start path: no operation update', () => {
  945. const transformBy = new MoveOperation(
  946. new Position( root, [ 4, 1, 0 ] ),
  947. 2,
  948. new Position( root, [ 2, 5 ] ),
  949. 0
  950. );
  951. const transOp = transform.transform( op, transformBy );
  952. expect( transOp.length ).to.equal( 1 );
  953. expectOperation( transOp[ 0 ], expected );
  954. } );
  955. it( 'range before node from transforming range start path: decrement index on range start path', () => {
  956. const transformBy = new MoveOperation(
  957. new Position( root, [ 2, 0 ] ),
  958. 1,
  959. new Position( root, [ 4, 1, 0 ] ),
  960. 0
  961. );
  962. const transOp = transform.transform( op, transformBy );
  963. expected.sourcePosition.path[ 1 ] -= 1;
  964. expect( transOp.length ).to.equal( 1 );
  965. expectOperation( transOp[ 0 ], expected );
  966. } );
  967. it( 'range after node from transforming range start path: no operation update', () => {
  968. const transformBy = new MoveOperation(
  969. new Position( root, [ 2, 3 ] ),
  970. 2,
  971. new Position( root, [ 4, 1, 0 ] ),
  972. 0
  973. );
  974. const transOp = transform.transform( op, transformBy );
  975. expect( transOp.length ).to.equal( 1 );
  976. expectOperation( transOp[ 0 ], expected );
  977. } );
  978. it( 'target offset before transforming target offset: increment target offset', () => {
  979. const transformBy = new MoveOperation(
  980. new Position( root, [ 4, 1, 0 ] ),
  981. 2,
  982. new Position( root, [ 3, 3, 0 ] ),
  983. 0
  984. );
  985. const transOp = transform.transform( op, transformBy );
  986. expected.targetPosition.offset += 2;
  987. expect( transOp.length ).to.equal( 1 );
  988. expectOperation( transOp[ 0 ], expected );
  989. } );
  990. it( 'target offset after transforming target offset: no operation update', () => {
  991. const transformBy = new MoveOperation(
  992. new Position( root, [ 4, 1, 0 ] ),
  993. 2,
  994. new Position( root, [ 3, 3, 5 ] ),
  995. 0
  996. );
  997. const transOp = transform.transform( op, transformBy );
  998. expect( transOp.length ).to.equal( 1 );
  999. expectOperation( transOp[ 0 ], expected );
  1000. } );
  1001. it( 'range offset before transforming target offset: decrement target offset', () => {
  1002. const transformBy = new MoveOperation(
  1003. new Position( root, [ 3, 3, 0 ] ),
  1004. 2,
  1005. new Position( root, [ 4, 1, 0 ] ),
  1006. 0
  1007. );
  1008. const transOp = transform.transform( op, transformBy );
  1009. expected.targetPosition.offset -= 2;
  1010. expect( transOp.length ).to.equal( 1 );
  1011. expectOperation( transOp[ 0 ], expected );
  1012. } );
  1013. it( 'range offset after transforming target offset: no operation update', () => {
  1014. const transformBy = new MoveOperation(
  1015. new Position( root, [ 3, 3, 5 ] ),
  1016. 2,
  1017. new Position( root, [ 4, 1, 0 ] ),
  1018. 0
  1019. );
  1020. const transOp = transform.transform( op, transformBy );
  1021. expect( transOp.length ).to.equal( 1 );
  1022. expectOperation( transOp[ 0 ], expected );
  1023. } );
  1024. it( 'target before node from transforming target path: increment index on target path', () => {
  1025. const transformBy = new MoveOperation(
  1026. new Position( root, [ 4, 1, 0 ] ),
  1027. 2,
  1028. new Position( root, [ 3, 1 ] ),
  1029. 0
  1030. );
  1031. const transOp = transform.transform( op, transformBy );
  1032. expected.targetPosition.path[ 1 ] += 2;
  1033. expect( transOp.length ).to.equal( 1 );
  1034. expectOperation( transOp[ 0 ], expected );
  1035. } );
  1036. it( 'target after node from transforming target path: no operation update', () => {
  1037. const transformBy = new MoveOperation(
  1038. new Position( root, [ 4, 1, 0 ] ),
  1039. 2,
  1040. new Position( root, [ 3, 5 ] ),
  1041. 0
  1042. );
  1043. const transOp = transform.transform( op, transformBy );
  1044. expect( transOp.length ).to.equal( 1 );
  1045. expectOperation( transOp[ 0 ], expected );
  1046. } );
  1047. it( 'range before node from transforming target path: decrement index on target path', () => {
  1048. const transformBy = new MoveOperation(
  1049. new Position( root, [ 3, 0 ] ),
  1050. 2,
  1051. new Position( root, [ 4, 1, 0 ] ),
  1052. 0
  1053. );
  1054. const transOp = transform.transform( op, transformBy );
  1055. expected.targetPosition.path[ 1 ] -= 2;
  1056. expect( transOp.length ).to.equal( 1 );
  1057. expectOperation( transOp[ 0 ], expected );
  1058. } );
  1059. it( 'range after node from transforming target path: no operation update', () => {
  1060. const transformBy = new MoveOperation(
  1061. new Position( root, [ 3, 5 ] ),
  1062. 2,
  1063. new Position( root, [ 4, 1, 0 ] ),
  1064. 0
  1065. );
  1066. const transOp = transform.transform( op, transformBy );
  1067. expect( transOp.length ).to.equal( 1 );
  1068. expectOperation( transOp[ 0 ], expected );
  1069. } );
  1070. it( 'target inside transforming move range: split into two operations', () => {
  1071. const transformBy = new MoveOperation(
  1072. new Position( root, [ 4, 1, 0 ] ),
  1073. 2,
  1074. new Position( root, [ 2, 2, 5 ] ),
  1075. 0
  1076. );
  1077. const transOp = transform.transform( op, transformBy );
  1078. expect( transOp.length ).to.equal( 2 );
  1079. expected.howMany = 1;
  1080. expectOperation( transOp[ 0 ], expected );
  1081. expected.sourcePosition.path = [ 2, 2, 6 ];
  1082. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1083. expectOperation( transOp[ 1 ], expected );
  1084. } );
  1085. it( 'target at start boundary of transforming move range: increment source offset', () => {
  1086. const transformBy = new MoveOperation(
  1087. new Position( root, [ 4, 1, 0 ] ),
  1088. 2,
  1089. new Position( root, [ 2, 2, 4 ] ),
  1090. 0
  1091. );
  1092. const transOp = transform.transform( op, transformBy );
  1093. expect( transOp.length ).to.equal( 1 );
  1094. expected.sourcePosition.offset = 6;
  1095. expectOperation( transOp[ 0 ], expected );
  1096. } );
  1097. it( 'target at end boundary of transforming move range: no operation update', () => {
  1098. const transformBy = new MoveOperation(
  1099. new Position( root, [ 4, 1, 0 ] ),
  1100. 2,
  1101. new Position( root, [ 2, 2, 6 ] ),
  1102. 0
  1103. );
  1104. const transOp = transform.transform( op, transformBy );
  1105. expect( transOp.length ).to.equal( 1 );
  1106. expectOperation( transOp[ 0 ], expected );
  1107. } );
  1108. it( 'target inside a node from transforming range: no operation update', () => {
  1109. const transformBy = new MoveOperation(
  1110. new Position( root, [ 4, 1, 0 ] ),
  1111. 2,
  1112. new Position( root, [ 2, 2, 5, 1 ] ),
  1113. 0
  1114. );
  1115. const transOp = transform.transform( op, transformBy );
  1116. expect( transOp.length ).to.equal( 1 );
  1117. expectOperation( transOp[ 0 ], expected );
  1118. } );
  1119. it( 'range has node that contains transforming range: update range path', () => {
  1120. const transformBy = new MoveOperation(
  1121. new Position( root, [ 2, 1 ] ),
  1122. 3,
  1123. new Position( root, [ 4, 2 ] ),
  1124. 0
  1125. );
  1126. const transOp = transform.transform( op, transformBy );
  1127. expected.sourcePosition.path = [ 4, 3, 4 ];
  1128. expect( transOp.length ).to.equal( 1 );
  1129. expectOperation( transOp[ 0 ], expected );
  1130. } );
  1131. it( 'range has node that contains transforming target: update target path', () => {
  1132. const transformBy = new MoveOperation(
  1133. new Position( root, [ 3, 2 ] ),
  1134. 3,
  1135. new Position( root, [ 0, 1 ] ),
  1136. 0
  1137. );
  1138. const transOp = transform.transform( op, transformBy );
  1139. expected.targetPosition.path = [ 0, 2, 3 ];
  1140. expect( transOp.length ).to.equal( 1 );
  1141. expectOperation( transOp[ 0 ], expected );
  1142. } );
  1143. it( 'target inside a node from transforming range and vice versa: reverse transform-by operation', () => {
  1144. const transformBy = new MoveOperation(
  1145. new Position( root, [ 3, 2 ] ),
  1146. 3,
  1147. new Position( root, [ 2, 2, 5, 0 ] ),
  1148. 0
  1149. );
  1150. const transOp = transform.transform( op, transformBy );
  1151. const reversed = transformBy.getReversed();
  1152. expected.sourcePosition = reversed.sourcePosition;
  1153. expected.targetPosition = reversed.targetPosition;
  1154. expected.howMany = reversed.howMany;
  1155. expect( transOp.length ).to.equal( 1 );
  1156. expectOperation( transOp[ 0 ], expected );
  1157. } );
  1158. it( 'range is same as transforming range and is important: convert to NoOperation', () => {
  1159. const transformBy = new MoveOperation(
  1160. op.sourcePosition,
  1161. op.howMany,
  1162. new Position( root, [ 4, 1, 0 ] ),
  1163. 0
  1164. );
  1165. const transOp = transform.transform( op, transformBy );
  1166. expect( transOp.length ).to.equal( 1 );
  1167. expectOperation( transOp[ 0 ], {
  1168. type: NoOperation
  1169. } );
  1170. } );
  1171. it( 'range is same as transforming range and is less important: update range path', () => {
  1172. const transformBy = new MoveOperation(
  1173. op.sourcePosition,
  1174. op.howMany,
  1175. new Position( root, [ 4, 1, 0 ] ),
  1176. 0
  1177. );
  1178. const transOp = transform.transform( op, transformBy, contextIsStrong );
  1179. expected.sourcePosition.path = [ 4, 1, 0 ];
  1180. expect( transOp.length ).to.equal( 1 );
  1181. expectOperation( transOp[ 0 ], expected );
  1182. } );
  1183. it( 'range contains transforming range and is important: convert to NoOperation', () => {
  1184. const transformBy = new MoveOperation(
  1185. new Position( root, [ 2, 2, 3 ] ),
  1186. 4,
  1187. new Position( root, [ 4, 1, 0 ] ),
  1188. 0
  1189. );
  1190. const transOp = transform.transform( op, transformBy );
  1191. expect( transOp.length ).to.equal( 1 );
  1192. expectOperation( transOp[ 0 ], {
  1193. type: NoOperation
  1194. } );
  1195. } );
  1196. it( 'range contains transforming range and is less important: update range path', () => {
  1197. const transformBy = new MoveOperation(
  1198. new Position( root, [ 2, 2, 3 ] ),
  1199. 4,
  1200. new Position( root, [ 4, 1, 0 ] ),
  1201. 0
  1202. );
  1203. const transOp = transform.transform( op, transformBy, contextIsStrong );
  1204. expected.sourcePosition.path = [ 4, 1, 1 ];
  1205. expect( transOp.length ).to.equal( 1 );
  1206. expectOperation( transOp[ 0 ], expected );
  1207. } );
  1208. it( 'range contains transforming range and target and is important: update range path and target', () => {
  1209. op.targetPosition.path = [ 2, 2, 7 ];
  1210. const transformBy = new MoveOperation(
  1211. new Position( root, [ 2, 2, 3 ] ),
  1212. 5,
  1213. new Position( root, [ 4, 1, 0 ] ),
  1214. 0
  1215. );
  1216. const transOp = transform.transform( op, transformBy );
  1217. expect( transOp.length ).to.equal( 1 );
  1218. expected.sourcePosition.path = [ 4, 1, 1 ];
  1219. expected.targetPosition.path = [ 4, 1, 4 ];
  1220. expectOperation( transOp[ 0 ], expected );
  1221. } );
  1222. it( 'range contains transforming range and target and is less important: update range path and target', () => {
  1223. op.targetPosition.path = [ 2, 2, 7 ];
  1224. const transformBy = new MoveOperation(
  1225. new Position( root, [ 2, 2, 3 ] ),
  1226. 5,
  1227. new Position( root, [ 4, 1, 0 ] ),
  1228. 0
  1229. );
  1230. const transOp = transform.transform( op, transformBy, contextIsStrong );
  1231. expect( transOp.length ).to.equal( 1 );
  1232. expected.sourcePosition.path = [ 4, 1, 1 ];
  1233. expected.targetPosition.path = [ 4, 1, 4 ];
  1234. expectOperation( transOp[ 0 ], expected );
  1235. } );
  1236. it( 'range intersects on left side of transforming range and is important: shrink range', () => {
  1237. const transformBy = new MoveOperation(
  1238. new Position( root, [ 2, 2, 3 ] ),
  1239. 2,
  1240. new Position( root, [ 4, 1, 0 ] ),
  1241. 0
  1242. );
  1243. const transOp = transform.transform( op, transformBy );
  1244. expected.sourcePosition.path = [ 2, 2, 3 ];
  1245. expected.howMany = 1;
  1246. expect( transOp.length ).to.equal( 1 );
  1247. expectOperation( transOp[ 0 ], expected );
  1248. } );
  1249. it( 'range intersects on left side of transforming range and is less important: split into two operations', () => {
  1250. // Get more test cases and better code coverage
  1251. const otherRoot = new RootElement( null );
  1252. const transformBy = new MoveOperation(
  1253. new Position( root, [ 2, 2, 3 ] ),
  1254. 2,
  1255. new Position( otherRoot, [ 4, 1, 0 ] ),
  1256. 0
  1257. );
  1258. const transOp = transform.transform( op, transformBy, contextIsStrong );
  1259. expect( transOp.length ).to.equal( 2 );
  1260. expected.sourcePosition = new Position( otherRoot, [ 4, 1, 1 ] );
  1261. expected.howMany = 1;
  1262. expectOperation( transOp[ 0 ], expected );
  1263. expected.sourcePosition = new Position( root, [ 2, 2, 3 ] );
  1264. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1265. expectOperation( transOp[ 1 ], expected );
  1266. } );
  1267. it( 'range intersects on right side of transforming range and is important: shrink range', () => {
  1268. const transformBy = new MoveOperation(
  1269. new Position( root, [ 2, 2, 5 ] ),
  1270. 2,
  1271. new Position( root, [ 4, 1, 0 ] ),
  1272. 0
  1273. );
  1274. const transOp = transform.transform( op, transformBy );
  1275. expected.howMany = 1;
  1276. expect( transOp.length ).to.equal( 1 );
  1277. expectOperation( transOp[ 0 ], expected );
  1278. } );
  1279. it( 'range intersects on right side of transforming range and is less important: split into two operations', () => {
  1280. const transformBy = new MoveOperation(
  1281. new Position( root, [ 2, 2, 5 ] ),
  1282. 2,
  1283. new Position( root, [ 4, 1, 0 ] ),
  1284. 0
  1285. );
  1286. const transOp = transform.transform( op, transformBy, contextIsStrong );
  1287. expect( transOp.length ).to.equal( 2 );
  1288. expected.sourcePosition = sourcePosition;
  1289. expected.howMany = 1;
  1290. expectOperation( transOp[ 0 ], expected );
  1291. expected.sourcePosition = new Position( root, [ 4, 1, 0 ] );
  1292. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1293. expectOperation( transOp[ 1 ], expected );
  1294. } );
  1295. it( 'range intersects on left side, target inside transforming range and is important: split into two operations', () => {
  1296. op.howMany = 4;
  1297. const transformBy = new MoveOperation(
  1298. new Position( root, [ 2, 2, 3 ] ),
  1299. 2,
  1300. new Position( root, [ 2, 2, 6 ] ),
  1301. 0
  1302. );
  1303. const transOp = transform.transform( op, transformBy );
  1304. expect( transOp.length ).to.equal( 2 );
  1305. expected.sourcePosition.path = [ 2, 2, 3 ];
  1306. expected.howMany = 1;
  1307. expectOperation( transOp[ 0 ], expected );
  1308. expected.sourcePosition.path = [ 2, 2, 5 ];
  1309. expected.howMany = 2;
  1310. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1311. expectOperation( transOp[ 1 ], expected );
  1312. } );
  1313. it( 'range intersects on left side, target inside transforming range and is less important: split into two operations', () => {
  1314. op.howMany = 4;
  1315. const transformBy = new MoveOperation(
  1316. new Position( root, [ 2, 2, 3 ] ),
  1317. 2,
  1318. new Position( root, [ 2, 2, 6 ] ),
  1319. 0
  1320. );
  1321. const transOp = transform.transform( op, transformBy, contextIsStrong );
  1322. expect( transOp.length ).to.equal( 2 );
  1323. expected.sourcePosition.path = [ 2, 2, 3 ];
  1324. expected.howMany = 1;
  1325. expectOperation( transOp[ 0 ], expected );
  1326. expected.sourcePosition.path = [ 2, 2, 5 ];
  1327. expected.howMany = 2;
  1328. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1329. expectOperation( transOp[ 1 ], expected );
  1330. } );
  1331. it( 'range intersects on right side of transforming range and is important: shrink range', () => {
  1332. const transformBy = new MoveOperation(
  1333. new Position( root, [ 2, 2, 5 ] ),
  1334. 2,
  1335. new Position( root, [ 4, 1, 0 ] ),
  1336. 0
  1337. );
  1338. const transOp = transform.transform( op, transformBy );
  1339. expected.howMany = 1;
  1340. expect( transOp.length ).to.equal( 1 );
  1341. expectOperation( transOp[ 0 ], expected );
  1342. } );
  1343. it( 'range intersects on right side of transforming range and is less important: split into two operations', () => {
  1344. const transformBy = new MoveOperation(
  1345. new Position( root, [ 2, 2, 5 ] ),
  1346. 2,
  1347. new Position( root, [ 4, 1, 0 ] ),
  1348. 0
  1349. );
  1350. const transOp = transform.transform( op, transformBy, contextIsStrong );
  1351. expect( transOp.length ).to.equal( 2 );
  1352. expected.sourcePosition = sourcePosition;
  1353. expected.howMany = 1;
  1354. expectOperation( transOp[ 0 ], expected );
  1355. expected.sourcePosition = new Position( root, [ 4, 1, 0 ] );
  1356. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1357. expectOperation( transOp[ 1 ], expected );
  1358. } );
  1359. it( 'range intersects, target inside transforming range and is important: split into two operations', () => {
  1360. op.targetPosition.path = [ 2, 2, 7 ];
  1361. const transformBy = new MoveOperation(
  1362. new Position( root, [ 2, 2, 5 ] ),
  1363. 4,
  1364. new Position( root, [ 4, 1, 0 ] ),
  1365. 0
  1366. );
  1367. const transOp = transform.transform( op, transformBy, contextIsStrong );
  1368. expect( transOp.length ).to.equal( 2 );
  1369. expected.sourcePosition.path = [ 2, 2, 4 ];
  1370. expected.targetPosition.path = [ 4, 1, 2 ];
  1371. expected.howMany = 1;
  1372. expectOperation( transOp[ 0 ], expected );
  1373. expected.sourcePosition.path = [ 4, 1, 0 ];
  1374. expected.targetPosition.path = [ 4, 1, 3 ];
  1375. expectOperation( transOp[ 1 ], expected );
  1376. } );
  1377. it( 'range intersects, target inside transforming range and is less important: shrink range', () => {
  1378. op.targetPosition.path = [ 2, 2, 7 ];
  1379. const transformBy = new MoveOperation(
  1380. new Position( root, [ 2, 2, 5 ] ),
  1381. 4,
  1382. new Position( root, [ 4, 1, 0 ] ),
  1383. 0
  1384. );
  1385. const transOp = transform.transform( op, transformBy );
  1386. expect( transOp.length ).to.equal( 1 );
  1387. expected.sourcePosition.path = [ 2, 2, 4 ];
  1388. expected.targetPosition.path = [ 4, 1, 2 ];
  1389. expected.howMany = 1;
  1390. expectOperation( transOp[ 0 ], expected );
  1391. } );
  1392. it( 'range inside transforming range and is important: split into two operations', () => {
  1393. op.howMany = 4;
  1394. const transformBy = new MoveOperation(
  1395. new Position( root, [ 2, 2, 5 ] ),
  1396. 2,
  1397. new Position( root, [ 4, 1, 0 ] ),
  1398. 0
  1399. );
  1400. const transOp = transform.transform( op, transformBy );
  1401. expect( transOp.length ).to.equal( 2 );
  1402. expected.sourcePosition.path = [ 2, 2, 4 ];
  1403. expected.howMany = 1;
  1404. expectOperation( transOp[ 0 ], expected );
  1405. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1406. expectOperation( transOp[ 1 ], expected );
  1407. } );
  1408. it( 'range inside transforming range and is less important: split into three operations', () => {
  1409. op.howMany = 4;
  1410. const transformBy = new MoveOperation(
  1411. new Position( root, [ 2, 2, 5 ] ),
  1412. 2,
  1413. new Position( root, [ 4, 1, 0 ] ),
  1414. 0
  1415. );
  1416. const transOp = transform.transform( op, transformBy, contextIsStrong );
  1417. expect( transOp.length ).to.equal( 3 );
  1418. expected.sourcePosition.path = [ 2, 2, 4 ];
  1419. expected.howMany = 1;
  1420. expectOperation( transOp[ 0 ], expected );
  1421. expected.sourcePosition.path = [ 4, 1, 0 ];
  1422. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1423. expected.howMany = 2;
  1424. expectOperation( transOp[ 1 ], expected );
  1425. expected.sourcePosition.path = [ 2, 2, 4 ];
  1426. expected.targetPosition = targetPosition.getShiftedBy( 3 );
  1427. expected.howMany = 1;
  1428. expectOperation( transOp[ 2 ], expected );
  1429. } );
  1430. it( 'range and target inside transforming range and is important: no operation update', () => {
  1431. op.howMany = 6;
  1432. const transformBy = new MoveOperation(
  1433. new Position( root, [ 2, 2, 5 ] ),
  1434. 2,
  1435. new Position( root, [ 2, 2, 9 ] ),
  1436. 0
  1437. );
  1438. const transOp = transform.transform( op, transformBy );
  1439. expect( transOp.length ).to.equal( 1 );
  1440. expected.howMany = 6;
  1441. expectOperation( transOp[ 0 ], expected );
  1442. } );
  1443. it( 'range and target inside transforming range and is less important: no operation update', () => {
  1444. op.howMany = 6;
  1445. const transformBy = new MoveOperation(
  1446. new Position( root, [ 2, 2, 5 ] ),
  1447. 2,
  1448. new Position( root, [ 2, 2, 9 ] ),
  1449. 0
  1450. );
  1451. const transOp = transform.transform( op, transformBy, contextIsStrong );
  1452. expect( transOp.length ).to.equal( 1 );
  1453. expected.howMany = 6;
  1454. expectOperation( transOp[ 0 ], expected );
  1455. } );
  1456. } );
  1457. describe( 'by RemoveOperation', () => {
  1458. let transformBy;
  1459. beforeEach( () => {
  1460. transformBy = new RemoveOperation(
  1461. Position.createFromPosition( op.sourcePosition ),
  1462. op.howMany,
  1463. new Position( doc.graveyard, [ 0 ] ),
  1464. 0
  1465. );
  1466. } );
  1467. it( 'should skip context.aIsStrong and be less important than RemoveOperation', () => {
  1468. const transOp = transform.transform( op, transformBy, contextIsStrong );
  1469. expect( transOp.length ).to.equal( 1 );
  1470. expectOperation( transOp[ 0 ], {
  1471. type: NoOperation
  1472. } );
  1473. } );
  1474. } );
  1475. describe( 'by NoOperation', () => {
  1476. it( 'no operation update', () => {
  1477. const transformBy = new NoOperation( 0 );
  1478. const transOp = transform.transform( op, transformBy );
  1479. expect( transOp.length ).to.equal( 1 );
  1480. expectOperation( transOp[ 0 ], expected );
  1481. } );
  1482. } );
  1483. describe( 'by RenameOperation', () => {
  1484. it( 'no position update', () => {
  1485. const transformBy = new RenameOperation( new Position( root, [ 2, 2, 4 ] ), 'oldName', 'newName', 0 );
  1486. const transOp = transform.transform( op, transformBy );
  1487. expect( transOp.length ).to.equal( 1 );
  1488. expectOperation( transOp[ 0 ], expected );
  1489. } );
  1490. } );
  1491. describe( 'by MarkerOperation', () => {
  1492. it( 'no position update', () => {
  1493. const newRange = new Range( new Position( root, [ 2, 2, 3 ] ), new Position( root, [ 2, 2, 8 ] ) );
  1494. const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, 0 );
  1495. const transOp = transform.transform( op, transformBy );
  1496. expect( transOp.length ).to.equal( 1 );
  1497. expectOperation( transOp[ 0 ], expected );
  1498. } );
  1499. } );
  1500. } );
  1501. describe( 'RemoveOperation', () => {
  1502. describe( 'by MoveOperation', () => {
  1503. it( 'should force removing content even if was less important', () => {
  1504. const op = new RemoveOperation( new Position( root, [ 8 ] ), 2, new Position( doc.graveyard, [ 0 ] ), 0 );
  1505. const targetPosition = Position.createFromPosition( op.targetPosition );
  1506. const transformBy = new MoveOperation( new Position( root, [ 8 ] ), 2, new Position( root, [ 1 ] ), 0 );
  1507. const sourcePosition = Position.createFromPosition( transformBy.targetPosition );
  1508. const transOp = transform.transform( op, transformBy );
  1509. expect( transOp.length ).to.equal( 1 );
  1510. expectOperation( transOp[ 0 ], {
  1511. type: RemoveOperation,
  1512. howMany: 2,
  1513. sourcePosition,
  1514. targetPosition
  1515. } );
  1516. } );
  1517. } );
  1518. } );
  1519. describe( 'NoOperation', () => {
  1520. beforeEach( () => {
  1521. op = new NoOperation( 0 );
  1522. expected = {
  1523. type: NoOperation
  1524. };
  1525. } );
  1526. describe( 'by InsertOperation', () => {
  1527. it( 'no operation update', () => {
  1528. const transformBy = new InsertOperation(
  1529. new Position( root, [ 0 ] ),
  1530. 'a',
  1531. 0
  1532. );
  1533. const transOp = transform.transform( op, transformBy );
  1534. expect( transOp.length ).to.equal( 1 );
  1535. expectOperation( transOp[ 0 ], expected );
  1536. } );
  1537. } );
  1538. describe( 'by AttributeOperation', () => {
  1539. it( 'no operation update', () => {
  1540. const transformBy = new AttributeOperation(
  1541. new Range(
  1542. new Position( root, [ 0 ] ),
  1543. new Position( root, [ 1 ] )
  1544. ),
  1545. 'foo',
  1546. 'bar',
  1547. 'xyz',
  1548. 0
  1549. );
  1550. const transOp = transform.transform( op, transformBy );
  1551. expect( transOp.length ).to.equal( 1 );
  1552. expectOperation( transOp[ 0 ], expected );
  1553. } );
  1554. } );
  1555. describe( 'by RootAttributeOperation', () => {
  1556. it( 'no operation update', () => {
  1557. const transformBy = new RootAttributeOperation(
  1558. root,
  1559. 'foo',
  1560. null,
  1561. 'bar',
  1562. 0
  1563. );
  1564. const transOp = transform.transform( op, transformBy );
  1565. expect( transOp.length ).to.equal( 1 );
  1566. expectOperation( transOp[ 0 ], expected );
  1567. } );
  1568. } );
  1569. describe( 'by MoveOperation', () => {
  1570. it( 'no operation update', () => {
  1571. const transformBy = new MoveOperation(
  1572. new Position( root, [ 0 ] ),
  1573. 2,
  1574. new Position( root, [ 1 ] ),
  1575. 0
  1576. );
  1577. const transOp = transform.transform( op, transformBy );
  1578. expect( transOp.length ).to.equal( 1 );
  1579. expectOperation( transOp[ 0 ], expected );
  1580. } );
  1581. } );
  1582. describe( 'by NoOperation', () => {
  1583. it( 'no operation update', () => {
  1584. const transformBy = new NoOperation( 0 );
  1585. const transOp = transform.transform( op, transformBy );
  1586. expect( transOp.length ).to.equal( 1 );
  1587. expectOperation( transOp[ 0 ], expected );
  1588. } );
  1589. } );
  1590. describe( 'by RenameOperation', () => {
  1591. it( 'no position update', () => {
  1592. const transformBy = new RenameOperation( new Position( root, [ 0, 2, 0 ] ), 'oldName', 'newName', 0 );
  1593. const transOp = transform.transform( op, transformBy );
  1594. expect( transOp.length ).to.equal( 1 );
  1595. expectOperation( transOp[ 0 ], expected );
  1596. } );
  1597. } );
  1598. describe( 'by MarkerOperation', () => {
  1599. it( 'no position update', () => {
  1600. const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 8 ] ) );
  1601. const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, 0 );
  1602. const transOp = transform.transform( op, transformBy );
  1603. expect( transOp.length ).to.equal( 1 );
  1604. expectOperation( transOp[ 0 ], expected );
  1605. } );
  1606. } );
  1607. } );
  1608. describe( 'RenameOperation', () => {
  1609. beforeEach( () => {
  1610. const position = new Position( root, [ 0, 2, 2 ] );
  1611. op = new RenameOperation( position, 'oldName', 'newName', 0 );
  1612. expected = {
  1613. position,
  1614. oldName: 'oldName',
  1615. newName: 'newName'
  1616. };
  1617. } );
  1618. describe( 'by InsertOperation', () => {
  1619. it( 'target before renamed element: offset update', () => {
  1620. const transformBy = new InsertOperation(
  1621. new Position( root, [ 0, 2, 1 ] ),
  1622. [ nodeA, nodeB ],
  1623. 0
  1624. );
  1625. const transOp = transform.transform( op, transformBy );
  1626. expect( transOp.length ).to.equal( 1 );
  1627. expected.position.offset = 4;
  1628. expectOperation( transOp[ 0 ], expected );
  1629. } );
  1630. it( 'target after renamed element: no change', () => {
  1631. const transformBy = new InsertOperation(
  1632. new Position( root, [ 0, 2, 3 ] ),
  1633. [ nodeA, nodeB ],
  1634. 0
  1635. );
  1636. const transOp = transform.transform( op, transformBy );
  1637. expect( transOp.length ).to.equal( 1 );
  1638. expectOperation( transOp[ 0 ], expected );
  1639. } );
  1640. it( 'target before a node on path to renamed element: path update', () => {
  1641. const transformBy = new InsertOperation(
  1642. new Position( root, [ 0, 1 ] ),
  1643. [ nodeA, nodeB ],
  1644. 0
  1645. );
  1646. const transOp = transform.transform( op, transformBy );
  1647. expect( transOp.length ).to.equal( 1 );
  1648. expected.position.path = [ 0, 4, 2 ];
  1649. expectOperation( transOp[ 0 ], expected );
  1650. } );
  1651. it( 'target after a node on path to renamed element: no change', () => {
  1652. const transformBy = new InsertOperation(
  1653. new Position( root, [ 0, 3 ] ),
  1654. [ nodeA, nodeB ],
  1655. 0
  1656. );
  1657. const transOp = transform.transform( op, transformBy );
  1658. expect( transOp.length ).to.equal( 1 );
  1659. expectOperation( transOp[ 0 ], expected );
  1660. } );
  1661. } );
  1662. describe( 'by AttributeOperation', () => {
  1663. it( 'no operation update', () => {
  1664. const transformBy = new AttributeOperation(
  1665. new Range(
  1666. new Position( root, [ 0, 2, 1 ] ),
  1667. new Position( root, [ 1, 3 ] )
  1668. ),
  1669. 'foo',
  1670. 'bar',
  1671. 'xyz',
  1672. 0
  1673. );
  1674. const transOp = transform.transform( op, transformBy );
  1675. expect( transOp.length ).to.equal( 1 );
  1676. expectOperation( transOp[ 0 ], expected );
  1677. } );
  1678. } );
  1679. describe( 'by RootAttributeOperation', () => {
  1680. it( 'no operation update', () => {
  1681. const transformBy = new RootAttributeOperation(
  1682. root,
  1683. 'foo',
  1684. null,
  1685. 'bar',
  1686. 0
  1687. );
  1688. const transOp = transform.transform( op, transformBy );
  1689. expect( transOp.length ).to.equal( 1 );
  1690. expectOperation( transOp[ 0 ], expected );
  1691. } );
  1692. } );
  1693. describe( 'by MarkerOperation', () => {
  1694. it( 'no operation update', () => {
  1695. const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 8 ] ) );
  1696. const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, 0 );
  1697. const transOp = transform.transform( op, transformBy );
  1698. expect( transOp.length ).to.equal( 1 );
  1699. expectOperation( transOp[ 0 ], expected );
  1700. } );
  1701. } );
  1702. describe( 'by RenameOperation', () => {
  1703. it( 'different element: no change', () => {
  1704. const transformBy = new RenameOperation(
  1705. new Position( root, [ 0, 2, 1 ] ),
  1706. 'foo',
  1707. 'bar',
  1708. 0
  1709. );
  1710. const transOp = transform.transform( op, transformBy );
  1711. expect( transOp.length ).to.equal( 1 );
  1712. expectOperation( transOp[ 0 ], expected );
  1713. } );
  1714. it( 'same element and is important: convert to NoOperation', () => {
  1715. const transformBy = new RenameOperation(
  1716. new Position( root, [ 0, 2, 2 ] ),
  1717. 'oldName',
  1718. 'otherName',
  1719. 0
  1720. );
  1721. const transOp = transform.transform( op, transformBy );
  1722. expect( transOp.length ).to.equal( 1 );
  1723. expectOperation( transOp[ 0 ], {
  1724. type: NoOperation
  1725. } );
  1726. } );
  1727. it( 'same element and is not important: change old name to new name', () => {
  1728. const transformBy = new RenameOperation(
  1729. new Position( root, [ 0, 2, 2 ] ),
  1730. 'oldName',
  1731. 'otherName',
  1732. 0
  1733. );
  1734. const transOp = transform.transform( op, transformBy, contextIsStrong );
  1735. expect( transOp.length ).to.equal( 1 );
  1736. expected.oldName = 'otherName';
  1737. expectOperation( transOp[ 0 ], expected );
  1738. } );
  1739. } );
  1740. describe( 'by MoveOperation', () => {
  1741. it( 'moved range before renamed element: update offset', () => {
  1742. const transformBy = new MoveOperation(
  1743. new Position( root, [ 0, 2, 0 ] ),
  1744. 2,
  1745. new Position( root, [ 2, 5 ] ),
  1746. 0
  1747. );
  1748. const transOp = transform.transform( op, transformBy );
  1749. expect( transOp.length ).to.equal( 1 );
  1750. expected.position.offset = 0;
  1751. expectOperation( transOp[ 0 ], expected );
  1752. } );
  1753. it( 'moved range before an element on path to renamed element: update path', () => {
  1754. const transformBy = new MoveOperation(
  1755. new Position( root, [ 0, 0 ] ),
  1756. 2,
  1757. new Position( root, [ 2, 5 ] ),
  1758. 0
  1759. );
  1760. const transOp = transform.transform( op, transformBy );
  1761. expect( transOp.length ).to.equal( 1 );
  1762. expected.position.path = [ 0, 0, 2 ];
  1763. expectOperation( transOp[ 0 ], expected );
  1764. } );
  1765. it( 'moved range contains renamed element: update path', () => {
  1766. const transformBy = new MoveOperation(
  1767. new Position( root, [ 0, 2, 1 ] ),
  1768. 3,
  1769. new Position( root, [ 2, 5 ] ),
  1770. 0
  1771. );
  1772. const transOp = transform.transform( op, transformBy );
  1773. expect( transOp.length ).to.equal( 1 );
  1774. expected.position.path = [ 2, 6 ];
  1775. expectOperation( transOp[ 0 ], expected );
  1776. } );
  1777. it( 'moved range contains renamed element parent: updated path', () => {
  1778. const transformBy = new MoveOperation(
  1779. new Position( root, [ 0, 1 ] ),
  1780. 3,
  1781. new Position( root, [ 2, 5 ] ),
  1782. 0
  1783. );
  1784. const transOp = transform.transform( op, transformBy );
  1785. expect( transOp.length ).to.equal( 1 );
  1786. expected.position.path = [ 2, 6, 2 ];
  1787. expectOperation( transOp[ 0 ], expected );
  1788. } );
  1789. it( 'move target before renamed element: update offset', () => {
  1790. const transformBy = new MoveOperation(
  1791. new Position( root, [ 2, 5 ] ),
  1792. 2,
  1793. new Position( root, [ 0, 2, 1 ] ),
  1794. 0
  1795. );
  1796. const transOp = transform.transform( op, transformBy );
  1797. expect( transOp.length ).to.equal( 1 );
  1798. expected.position.offset = 4;
  1799. expectOperation( transOp[ 0 ], expected );
  1800. } );
  1801. it( 'move target before an element on path to renamed element: update path', () => {
  1802. const transformBy = new MoveOperation(
  1803. new Position( root, [ 0, 2, 0 ] ),
  1804. 2,
  1805. new Position( root, [ 2, 5 ] ),
  1806. 0
  1807. );
  1808. const transOp = transform.transform( op, transformBy );
  1809. expect( transOp.length ).to.equal( 1 );
  1810. expected.position.offset = 0;
  1811. expectOperation( transOp[ 0 ], expected );
  1812. } );
  1813. } );
  1814. } );
  1815. describe( 'MarkerOperation', () => {
  1816. let oldRange, newRange;
  1817. beforeEach( () => {
  1818. oldRange = Range.createFromParentsAndOffsets( root, 1, root, 4 );
  1819. newRange = Range.createFromParentsAndOffsets( root, 10, root, 12 );
  1820. op = new MarkerOperation( 'name', oldRange, newRange, model.markers, 0 );
  1821. expected = {
  1822. name: 'name',
  1823. oldRange,
  1824. newRange
  1825. };
  1826. } );
  1827. describe( 'by InsertOperation', () => {
  1828. it( 'insert position affecting oldRange: update oldRange', () => {
  1829. // Just CC things.
  1830. op.newRange = null;
  1831. const transformBy = new InsertOperation( Position.createAt( root, 0 ), [ nodeA, nodeB ], 0 );
  1832. const transOp = transform.transform( op, transformBy );
  1833. expected.newRange = null;
  1834. expected.oldRange.start.offset = 3;
  1835. expected.oldRange.end.offset = 6;
  1836. expect( transOp.length ).to.equal( 1 );
  1837. expectOperation( transOp[ 0 ], expected );
  1838. } );
  1839. it( 'insert position affecting newRange: update newRange', () => {
  1840. // Just CC things.
  1841. op.oldRange = null;
  1842. const transformBy = new InsertOperation( Position.createAt( root, 8 ), [ nodeA, nodeB ], 0 );
  1843. const transOp = transform.transform( op, transformBy );
  1844. expected.oldRange = null;
  1845. expected.newRange.start.offset = 12;
  1846. expected.newRange.end.offset = 14;
  1847. expect( transOp.length ).to.equal( 1 );
  1848. expectOperation( transOp[ 0 ], expected );
  1849. } );
  1850. } );
  1851. describe( 'by AttributeOperation', () => {
  1852. it( 'no operation update', () => {
  1853. const transformBy = new AttributeOperation(
  1854. new Range(
  1855. new Position( root, [ 2 ] ),
  1856. new Position( root, [ 11 ] )
  1857. ),
  1858. 'foo',
  1859. 'bar',
  1860. 'xyz',
  1861. 0
  1862. );
  1863. const transOp = transform.transform( op, transformBy );
  1864. expect( transOp.length ).to.equal( 1 );
  1865. expectOperation( transOp[ 0 ], expected );
  1866. } );
  1867. } );
  1868. describe( 'by MoveOperation', () => {
  1869. it( 'moved range is before oldRange: update oldRange', () => {
  1870. // Just CC things.
  1871. op.newRange = null;
  1872. const transformBy = new MoveOperation( Position.createAt( root, 0 ), 1, Position.createAt( root, 20 ), 0 );
  1873. const transOp = transform.transform( op, transformBy );
  1874. expected.newRange = null;
  1875. expected.oldRange.start.offset = 0;
  1876. expected.oldRange.end.offset = 3;
  1877. expect( transOp.length ).to.equal( 1 );
  1878. expectOperation( transOp[ 0 ], expected );
  1879. } );
  1880. it( 'moved range contains oldRange and is before newRange: update oldRange and newRange', () => {
  1881. const transformBy = new MoveOperation( Position.createAt( root, 2 ), 2, Position.createAt( root, 20 ), 0 );
  1882. const transOp = transform.transform( op, transformBy );
  1883. expected.oldRange.start.offset = 1;
  1884. expected.oldRange.end.offset = 2;
  1885. expected.newRange.start.offset = 8;
  1886. expected.newRange.end.offset = 10;
  1887. expect( transOp.length ).to.equal( 1 );
  1888. expectOperation( transOp[ 0 ], expected );
  1889. } );
  1890. it( 'target position is inside newRange: update newRange', () => {
  1891. // Just CC things.
  1892. op.oldRange = null;
  1893. const transformBy = new MoveOperation( Position.createAt( root, 20 ), 2, Position.createAt( root, 11 ), 0 );
  1894. const transOp = transform.transform( op, transformBy );
  1895. expected.oldRange = null;
  1896. expected.newRange.start.offset = 10;
  1897. expected.newRange.end.offset = 14;
  1898. expect( transOp.length ).to.equal( 1 );
  1899. expectOperation( transOp[ 0 ], expected );
  1900. } );
  1901. it( 'target position is inside oldRange and before newRange: update oldRange and newRange', () => {
  1902. const transformBy = new MoveOperation( Position.createAt( root, 20 ), 4, Position.createAt( root, 2 ), 0 );
  1903. const transOp = transform.transform( op, transformBy );
  1904. expected.oldRange.start.offset = 1;
  1905. expected.oldRange.end.offset = 8;
  1906. expected.newRange.start.offset = 14;
  1907. expected.newRange.end.offset = 16;
  1908. expect( transOp.length ).to.equal( 1 );
  1909. expectOperation( transOp[ 0 ], expected );
  1910. } );
  1911. } );
  1912. describe( 'by RootAttributeOperation', () => {
  1913. it( 'no operation update', () => {
  1914. const transformBy = new RootAttributeOperation(
  1915. root,
  1916. 'foo',
  1917. null,
  1918. 'bar',
  1919. 0
  1920. );
  1921. const transOp = transform.transform( op, transformBy );
  1922. expect( transOp.length ).to.equal( 1 );
  1923. expectOperation( transOp[ 0 ], expected );
  1924. } );
  1925. } );
  1926. describe( 'by RenameOperation', () => {
  1927. it( 'no operation update', () => {
  1928. const transformBy = new RenameOperation( new Position( root, [ 1 ] ), 'oldName', 'newName', 0 );
  1929. const transOp = transform.transform( op, transformBy );
  1930. expect( transOp.length ).to.equal( 1 );
  1931. expectOperation( transOp[ 0 ], expected );
  1932. } );
  1933. } );
  1934. describe( 'by MarkerOperation', () => {
  1935. it( 'different marker name: no operation update', () => {
  1936. const transformBy = new MarkerOperation( 'otherName', oldRange, newRange, model.markers, 0 );
  1937. const transOp = transform.transform( op, transformBy );
  1938. expect( transOp.length ).to.equal( 1 );
  1939. expectOperation( transOp[ 0 ], expected );
  1940. } );
  1941. it( 'same marker name and is important: convert to NoOperation', () => {
  1942. const anotherRange = Range.createFromParentsAndOffsets( root, 2, root, 2 );
  1943. const transformBy = new MarkerOperation( 'name', oldRange, anotherRange, model.markers, 0 );
  1944. const transOp = transform.transform( op, transformBy );
  1945. expect( transOp.length ).to.equal( 1 );
  1946. expectOperation( transOp[ 0 ], {
  1947. type: NoOperation
  1948. } );
  1949. } );
  1950. it( 'same marker name and is less important: update oldRange parameter', () => {
  1951. const anotherRange = Range.createFromParentsAndOffsets( root, 2, root, 2 );
  1952. const transformBy = new MarkerOperation( 'name', oldRange, anotherRange, model.markers, 0 );
  1953. const transOp = transform.transform( op, transformBy, contextIsStrong );
  1954. expected.oldRange = anotherRange;
  1955. expect( transOp.length ).to.equal( 1 );
  1956. expectOperation( transOp[ 0 ], expected );
  1957. } );
  1958. } );
  1959. } );
  1960. } );