differ.js 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../src/model/model';
  6. import Element from '../../src/model/element';
  7. import Text from '../../src/model/text';
  8. import Position from '../../src/model/position';
  9. import Range from '../../src/model/range';
  10. import InsertOperation from '../../src/model/operation/insertoperation';
  11. import MoveOperation from '../../src/model/operation/moveoperation';
  12. import RenameOperation from '../../src/model/operation/renameoperation';
  13. import AttributeOperation from '../../src/model/operation/attributeoperation';
  14. import SplitOperation from '../../src/model/operation/splitoperation';
  15. import MergeOperation from '../../src/model/operation/mergeoperation';
  16. import WrapOperation from '../../src/model/operation/wrapoperation';
  17. import UnwrapOperation from '../../src/model/operation/unwrapoperation';
  18. describe( 'Differ', () => {
  19. let doc, differ, root, model;
  20. beforeEach( () => {
  21. model = new Model();
  22. doc = model.document;
  23. differ = doc.differ;
  24. root = doc.createRoot();
  25. root._appendChild( [
  26. new Element( 'paragraph', null, [
  27. new Text( 'foo' )
  28. ] ),
  29. new Element( 'paragraph', null, [
  30. new Text( 'bar' )
  31. ] )
  32. ] );
  33. } );
  34. describe( 'insert', () => {
  35. // Simple.
  36. it( 'an element', () => {
  37. const position = new Position( root, [ 1 ] );
  38. model.change( () => {
  39. insert( new Element( 'image' ), position );
  40. expectChanges( [
  41. { type: 'insert', name: 'image', length: 1, position }
  42. ] );
  43. } );
  44. } );
  45. it( 'a non-empty element with attributes', () => {
  46. const position = new Position( root, [ 1 ] );
  47. model.change( () => {
  48. insert(
  49. new Element( 'image', { src: 'foo.jpg' }, new Element( 'caption', null, new Text( 'bar' ) ) ),
  50. position
  51. );
  52. expectChanges( [
  53. { type: 'insert', name: 'image', length: 1, position }
  54. ] );
  55. } );
  56. } );
  57. it( 'multiple elements', () => {
  58. const position = new Position( root, [ 1 ] );
  59. model.change( () => {
  60. insert( [ new Element( 'image' ), new Element( 'paragraph' ) ], position );
  61. expectChanges( [
  62. { type: 'insert', name: 'image', length: 1, position },
  63. { type: 'insert', name: 'paragraph', length: 1, position: position.getShiftedBy( 1 ) }
  64. ] );
  65. } );
  66. } );
  67. it( 'a character', () => {
  68. const position = new Position( root, [ 0, 2 ] );
  69. model.change( () => {
  70. insert( new Text( 'x' ), position );
  71. expectChanges( [
  72. { type: 'insert', name: '$text', length: 1, position }
  73. ] );
  74. } );
  75. } );
  76. it( 'multiple characters', () => {
  77. const position = new Position( root, [ 0, 2 ] );
  78. model.change( () => {
  79. insert( new Text( 'xyz' ), position );
  80. expectChanges( [
  81. { type: 'insert', name: '$text', length: 3, position }
  82. ] );
  83. } );
  84. } );
  85. it( 'multiple consecutive characters in multiple operations', () => {
  86. const position = new Position( root, [ 0, 2 ] );
  87. model.change( () => {
  88. insert( new Text( 'xy' ), position );
  89. insert( new Text( 'z' ), position.getShiftedBy( 2 ) );
  90. insert( new Text( 'ab' ), position );
  91. expectChanges( [
  92. { type: 'insert', name: '$text', length: 5, position }
  93. ] );
  94. } );
  95. } );
  96. it( 'multiple non-consecutive characters in multiple operations', () => {
  97. const position = new Position( root, [ 0, 0 ] );
  98. model.change( () => {
  99. insert( new Text( 'xy' ), position );
  100. insert( new Text( 'z' ), position.getShiftedBy( 3 ) );
  101. expectChanges( [
  102. { type: 'insert', name: '$text', length: 2, position },
  103. { type: 'insert', name: '$text', length: 1, position: position.getShiftedBy( 3 ) }
  104. ] );
  105. } );
  106. } );
  107. // Combined.
  108. it( 'node in a new element', () => {
  109. const image = new Element( 'image' );
  110. const position = new Position( root, [ 1 ] );
  111. model.change( () => {
  112. insert( image, position );
  113. const caption = new Element( 'caption' );
  114. insert( caption, Position.createAt( image, 0 ) );
  115. insert( new Text( 'foo' ), Position.createAt( caption, 0 ) );
  116. expectChanges( [
  117. { type: 'insert', name: 'image', length: 1, position }
  118. ] );
  119. } );
  120. } );
  121. it( 'node in a renamed element', () => {
  122. const text = new Text( 'xyz', { bold: true } );
  123. const position = new Position( root, [ 0, 3 ] );
  124. model.change( () => {
  125. insert( text, position );
  126. rename( root.getChild( 0 ), 'listItem' );
  127. // Note that since renamed element is removed and then re-inserted, there is no diff for text inserted inside it.
  128. expectChanges( [
  129. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  130. { type: 'insert', name: 'listItem', length: 1, position: new Position( root, [ 0 ] ) }
  131. ] );
  132. } );
  133. } );
  134. it( 'node in a element with changed attribute', () => {
  135. const text = new Text( 'xyz', { bold: true } );
  136. const position = new Position( root, [ 0, 3 ] );
  137. const range = Range.createFromParentsAndOffsets( root, 0, root, 1 );
  138. model.change( () => {
  139. insert( text, position );
  140. attribute( range, 'align', null, 'center' );
  141. const diffRange = Range.createFromParentsAndOffsets( root, 0, root.getChild( 0 ), 0 );
  142. // Compare to scenario above, this time there is only an attribute change on parent element,
  143. // so there is also a diff for text.
  144. expectChanges( [
  145. { type: 'attribute', range: diffRange, attributeKey: 'align', attributeOldValue: null, attributeNewValue: 'center' },
  146. { type: 'insert', name: '$text', length: 3, position },
  147. ] );
  148. } );
  149. } );
  150. it( 'nodes between other inserted nodes', () => {
  151. model.change( () => {
  152. insert( new Text( 'xx' ), new Position( root, [ 0, 1 ] ) );
  153. insert( new Text( 'yy' ), new Position( root, [ 0, 2 ] ) );
  154. expectChanges( [
  155. { type: 'insert', position: new Position( root, [ 0, 1 ] ), length: 4, name: '$text' }
  156. ] );
  157. } );
  158. } );
  159. it( 'nodes before nodes with changed attributes', () => {
  160. const p1 = root.getChild( 0 );
  161. const range = Range.createFromParentsAndOffsets( p1, 1, p1, 3 );
  162. const position = new Position( root, [ 0, 0 ] );
  163. model.change( () => {
  164. attribute( range, 'bold', null, true );
  165. insert( new Text( 'xx' ), position );
  166. const rangeAfter = Range.createFromParentsAndOffsets( p1, 3, p1, 5 );
  167. expectChanges( [
  168. { type: 'insert', name: '$text', length: 2, position },
  169. { type: 'attribute', range: rangeAfter, attributeKey: 'bold', attributeOldValue: null, attributeNewValue: true }
  170. ] );
  171. } );
  172. } );
  173. it( 'nodes between nodes with changed attributes', () => {
  174. const p1 = root.getChild( 0 );
  175. const range = Range.createFromParentsAndOffsets( p1, 1, p1, 3 );
  176. const position = new Position( root, [ 0, 2 ] );
  177. model.change( () => {
  178. attribute( range, 'bold', null, true );
  179. insert( new Text( 'xx' ), position );
  180. const rangeBefore = Range.createFromParentsAndOffsets( p1, 1, p1, 2 );
  181. const rangeAfter = Range.createFromParentsAndOffsets( p1, 4, p1, 5 );
  182. expectChanges( [
  183. {
  184. type: 'attribute',
  185. range: rangeBefore,
  186. attributeKey: 'bold',
  187. attributeOldValue: null,
  188. attributeNewValue: true
  189. },
  190. { type: 'insert', name: '$text', length: 2, position },
  191. {
  192. type: 'attribute',
  193. range: rangeAfter,
  194. attributeKey: 'bold',
  195. attributeOldValue: null,
  196. attributeNewValue: true
  197. }
  198. ] );
  199. } );
  200. } );
  201. it( 'nodes after nodes with changed attributes', () => {
  202. const p1 = root.getChild( 0 );
  203. const range = Range.createFromParentsAndOffsets( p1, 1, p1, 3 );
  204. const position = new Position( root, [ 0, 3 ] );
  205. model.change( () => {
  206. attribute( range, 'bold', null, true );
  207. insert( new Text( 'xx' ), position );
  208. expectChanges( [
  209. {
  210. type: 'attribute',
  211. range,
  212. attributeKey: 'bold',
  213. attributeOldValue: null,
  214. attributeNewValue: true
  215. },
  216. { type: 'insert', name: '$text', length: 2, position }
  217. ] );
  218. } );
  219. } );
  220. } );
  221. describe( 'remove', () => {
  222. it( 'an element', () => {
  223. const position = new Position( root, [ 0 ] );
  224. model.change( () => {
  225. remove( position, 1 );
  226. expectChanges( [
  227. { type: 'remove', name: 'paragraph', length: 1, position }
  228. ] );
  229. } );
  230. } );
  231. it( 'multiple elements', () => {
  232. const position = new Position( root, [ 0 ] );
  233. model.change( () => {
  234. remove( position, 2 );
  235. expectChanges( [
  236. { type: 'remove', name: 'paragraph', length: 1, position },
  237. { type: 'remove', name: 'paragraph', length: 1, position }
  238. ] );
  239. } );
  240. } );
  241. it( 'a character', () => {
  242. const position = new Position( root, [ 0, 1 ] );
  243. model.change( () => {
  244. remove( position, 1 );
  245. expectChanges( [
  246. { type: 'remove', name: '$text', length: 1, position }
  247. ] );
  248. } );
  249. } );
  250. it( 'multiple characters', () => {
  251. const position = new Position( root, [ 0, 1 ] );
  252. model.change( () => {
  253. remove( position, 2 );
  254. expectChanges( [
  255. { type: 'remove', name: '$text', length: 2, position }
  256. ] );
  257. } );
  258. } );
  259. it( 'multiple consecutive characters in multiple operations', () => {
  260. const position = new Position( root, [ 0, 0 ] );
  261. model.change( () => {
  262. remove( position, 1 );
  263. remove( position, 1 );
  264. remove( position, 1 );
  265. expectChanges( [
  266. { type: 'remove', name: '$text', length: 3, position }
  267. ] );
  268. } );
  269. } );
  270. it( 'multiple non-consecutive characters in multiple operations', () => {
  271. const position = new Position( root, [ 0, 0 ] );
  272. model.change( () => {
  273. remove( position, 1 );
  274. remove( position.getShiftedBy( 1 ), 1 );
  275. expectChanges( [
  276. { type: 'remove', name: '$text', length: 1, position },
  277. { type: 'remove', name: '$text', length: 1, position: position.getShiftedBy( 1 ) }
  278. ] );
  279. } );
  280. } );
  281. it( 'item just before inserted item', () => {
  282. // This tests proper changes sorting.
  283. const insertPosition = new Position( root, [ 0, 2 ] );
  284. const removePosition = new Position( root, [ 0, 1 ] );
  285. model.change( () => {
  286. insert( new Text( 'x' ), insertPosition );
  287. remove( removePosition, 1 );
  288. expectChanges( [
  289. { type: 'remove', name: '$text', length: 1, position: removePosition },
  290. { type: 'insert', name: '$text', length: 1, position: removePosition }
  291. ] );
  292. } );
  293. } );
  294. it( 'nodes before inserted nodes (together with some inserted nodes)', () => {
  295. const insertPosition = new Position( root, [ 0, 2 ] );
  296. const removePosition = new Position( root, [ 0, 1 ] );
  297. model.change( () => {
  298. insert( new Text( 'xyz' ), insertPosition );
  299. remove( removePosition, 2 );
  300. expectChanges( [
  301. { type: 'remove', name: '$text', length: 1, position: removePosition },
  302. { type: 'insert', name: '$text', length: 2, position: removePosition }
  303. ] );
  304. } );
  305. } );
  306. it( 'inserted nodes and some nodes after inserted nodes', () => {
  307. const insertPosition = new Position( root, [ 0, 2 ] );
  308. const removePosition = new Position( root, [ 0, 3 ] );
  309. model.change( () => {
  310. insert( new Text( 'xyz' ), insertPosition );
  311. remove( removePosition, 3 );
  312. expectChanges( [
  313. { type: 'insert', name: '$text', length: 1, position: insertPosition },
  314. { type: 'remove', name: '$text', length: 1, position: removePosition }
  315. ] );
  316. } );
  317. } );
  318. it( 'all inserted nodes', () => {
  319. const insertPosition = new Position( root, [ 0, 2 ] );
  320. const removePosition = new Position( root, [ 0, 1 ] );
  321. model.change( () => {
  322. insert( new Text( 'xy' ), insertPosition );
  323. remove( removePosition, 4 );
  324. expectChanges( [
  325. { type: 'remove', name: '$text', length: 2, position: removePosition }
  326. ] );
  327. } );
  328. } );
  329. it( 'before removed nodes', () => {
  330. const removePositionA = new Position( root, [ 0, 2 ] );
  331. const removePositionB = new Position( root, [ 0, 0 ] );
  332. model.change( () => {
  333. remove( removePositionA, 1 );
  334. remove( removePositionB, 1 );
  335. expectChanges( [
  336. { type: 'remove', name: '$text', length: 1, position: removePositionB },
  337. { type: 'remove', name: '$text', length: 1, position: new Position( root, [ 0, 1 ] ) }
  338. ] );
  339. } );
  340. } );
  341. it( 'before and after removed nodes in one operation', () => {
  342. const removePositionA = new Position( root, [ 0, 1 ] );
  343. const removePositionB = new Position( root, [ 0, 0 ] );
  344. model.change( () => {
  345. remove( removePositionA, 1 );
  346. remove( removePositionB, 2 );
  347. expectChanges( [
  348. { type: 'remove', name: '$text', length: 3, position: removePositionB },
  349. ] );
  350. } );
  351. } );
  352. it( 'before nodes that changed attributes', () => {
  353. const position = new Position( root, [ 0, 0 ] );
  354. const p1 = root.getChild( 0 );
  355. const range = Range.createFromParentsAndOffsets( p1, 2, p1, 3 );
  356. model.change( () => {
  357. attribute( range, 'bold', null, true );
  358. remove( position, 1 );
  359. const newRange = Range.createFromParentsAndOffsets( p1, 1, p1, 2 );
  360. expectChanges( [
  361. { type: 'remove', name: '$text', length: 1, position },
  362. {
  363. type: 'attribute',
  364. range: newRange,
  365. attributeKey: 'bold',
  366. attributeOldValue: null,
  367. attributeNewValue: true
  368. }
  369. ] );
  370. } );
  371. } );
  372. it( 'before nodes that changed attributes together with some changed nodes', () => {
  373. const position = new Position( root, [ 0, 0 ] );
  374. const p1 = root.getChild( 0 );
  375. const range = Range.createFromParentsAndOffsets( p1, 1, p1, 3 );
  376. model.change( () => {
  377. attribute( range, 'bold', null, true );
  378. remove( position, 2 );
  379. const newRange = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  380. expectChanges( [
  381. { type: 'remove', name: '$text', length: 2, position },
  382. {
  383. type: 'attribute',
  384. range: newRange,
  385. attributeKey: 'bold',
  386. attributeOldValue: null,
  387. attributeNewValue: true
  388. }
  389. ] );
  390. } );
  391. } );
  392. it( 'some changed nodes', () => {
  393. const position = new Position( root, [ 0, 1 ] );
  394. const p1 = root.getChild( 0 );
  395. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 3 );
  396. model.change( () => {
  397. attribute( range, 'bold', null, true );
  398. remove( position, 1 );
  399. const rangeBefore = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  400. const rangeAfter = Range.createFromParentsAndOffsets( p1, 1, p1, 2 );
  401. expectChanges( [
  402. {
  403. type: 'attribute',
  404. range: rangeBefore,
  405. attributeKey: 'bold',
  406. attributeOldValue: null,
  407. attributeNewValue: true
  408. },
  409. { type: 'remove', name: '$text', length: 1, position },
  410. {
  411. type: 'attribute',
  412. range: rangeAfter,
  413. attributeKey: 'bold',
  414. attributeOldValue: null,
  415. attributeNewValue: true
  416. }
  417. ] );
  418. } );
  419. } );
  420. it( 'some changed nodes and some nodes after', () => {
  421. const position = new Position( root, [ 0, 1 ] );
  422. const p1 = root.getChild( 0 );
  423. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 2 );
  424. model.change( () => {
  425. attribute( range, 'bold', null, true );
  426. remove( position, 2 );
  427. const newRange = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  428. expectChanges( [
  429. {
  430. type: 'attribute',
  431. range: newRange,
  432. attributeKey: 'bold',
  433. attributeOldValue: null,
  434. attributeNewValue: true
  435. },
  436. { type: 'remove', name: '$text', length: 2, position }
  437. ] );
  438. } );
  439. } );
  440. it( 'after changed nodes', () => {
  441. const position = new Position( root, [ 0, 2 ] );
  442. const p1 = root.getChild( 0 );
  443. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  444. model.change( () => {
  445. attribute( range, 'bold', null, true );
  446. remove( position, 1 );
  447. expectChanges( [
  448. {
  449. type: 'attribute',
  450. range,
  451. attributeKey: 'bold',
  452. attributeOldValue: null,
  453. attributeNewValue: true
  454. },
  455. { type: 'remove', name: '$text', length: 1, position }
  456. ] );
  457. } );
  458. } );
  459. } );
  460. // The only main difference between remove operation and move operation is target position.
  461. // In differ, graveyard is treated as other roots. In remove suite, simple cases for move are covered.
  462. // This suite will have only a few cases, focused on things specific to move operation.
  463. describe( 'move', () => {
  464. it( 'an element to the same parent - target position is after source position', () => {
  465. const sourcePosition = new Position( root, [ 0 ] );
  466. const targetPosition = new Position( root, [ 2 ] );
  467. model.change( () => {
  468. move( sourcePosition, 1, targetPosition );
  469. expectChanges( [
  470. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  471. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  472. ] );
  473. } );
  474. } );
  475. it( 'an element to the same parent - target position is before source position', () => {
  476. const sourcePosition = new Position( root, [ 1 ] );
  477. const targetPosition = new Position( root, [ 0 ] );
  478. model.change( () => {
  479. move( sourcePosition, 1, targetPosition );
  480. expectChanges( [
  481. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  482. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 2 ] ) }
  483. ] );
  484. } );
  485. } );
  486. it( 'multiple consecutive characters between different roots in multiple operations', () => {
  487. const sourcePosition = new Position( root, [ 0, 1 ] );
  488. const targetPosition = new Position( root, [ 1, 0 ] );
  489. model.change( () => {
  490. move( sourcePosition, 1, targetPosition );
  491. move( sourcePosition, 1, targetPosition.getShiftedBy( 1 ) );
  492. expectChanges( [
  493. { type: 'remove', name: '$text', length: 2, position: sourcePosition },
  494. { type: 'insert', name: '$text', length: 2, position: targetPosition }
  495. ] );
  496. } );
  497. } );
  498. it( 'reinsert removed element', () => {
  499. doc.graveyard._appendChild( new Element( 'listItem' ) );
  500. const sourcePosition = new Position( doc.graveyard, [ 0 ] );
  501. const targetPosition = new Position( root, [ 2 ] );
  502. model.change( () => {
  503. move( sourcePosition, 1, targetPosition );
  504. expectChanges( [
  505. { type: 'insert', name: 'listItem', length: 1, position: targetPosition }
  506. ] );
  507. } );
  508. } );
  509. } );
  510. describe( 'rename', () => {
  511. it( 'an element', () => {
  512. model.change( () => {
  513. rename( root.getChild( 1 ), 'listItem' );
  514. expectChanges( [
  515. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) },
  516. { type: 'insert', name: 'listItem', length: 1, position: new Position( root, [ 1 ] ) }
  517. ] );
  518. } );
  519. } );
  520. it( 'inside a new element', () => {
  521. // Since the rename is inside a new element, it should not be listed on changes list.
  522. model.change( () => {
  523. insert( new Element( 'blockQuote', null, new Element( 'paragraph' ) ), new Position( root, [ 2 ] ) );
  524. rename( root.getChild( 2 ).getChild( 0 ), 'listItem' );
  525. expectChanges( [
  526. { type: 'insert', name: 'blockQuote', length: 1, position: new Position( root, [ 2 ] ) }
  527. ] );
  528. } );
  529. } );
  530. it( 'markers refreshing', () => {
  531. // Since rename is "translated" to remove+insert pair, we need to refresh all the markers that
  532. // intersect with the renamed element.
  533. model.change( () => {
  534. // Renamed element contains marker.
  535. model.markers._set( 'markerA', new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) );
  536. // Marker contains renamed element.
  537. model.markers._set( 'markerB', new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ) );
  538. // Intersecting.
  539. model.markers._set( 'markerC', new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 2 ] ) ) );
  540. // Not intersecting.
  541. model.markers._set( 'markerD', new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 1 ] ) ) );
  542. } );
  543. const markersToRefresh = [ 'markerA', 'markerB', 'markerC' ];
  544. model.change( () => {
  545. rename( root.getChild( 1 ), 'listItem' );
  546. expectChanges( [
  547. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) },
  548. { type: 'insert', name: 'listItem', length: 1, position: new Position( root, [ 1 ] ) }
  549. ] );
  550. const markersToRemove = differ.getMarkersToRemove().map( entry => entry.name );
  551. const markersToAdd = differ.getMarkersToAdd().map( entry => entry.name );
  552. expect( markersToRefresh ).to.deep.equal( markersToRemove );
  553. expect( markersToRefresh ).to.deep.equal( markersToAdd );
  554. } );
  555. } );
  556. } );
  557. describe( 'attribute', () => {
  558. const attributeKey = 'key';
  559. const attributeOldValue = null;
  560. const attributeNewValue = 'foo';
  561. it( 'on an element', () => {
  562. const range = Range.createFromParentsAndOffsets( root, 0, root, 1 );
  563. model.change( () => {
  564. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  565. const diffRange = Range.createFromParentsAndOffsets( root, 0, root.getChild( 0 ), 0 );
  566. expectChanges( [
  567. { type: 'attribute', range: diffRange, attributeKey, attributeOldValue, attributeNewValue }
  568. ] );
  569. } );
  570. } );
  571. it( 'on an element - only one of many attributes changes', () => {
  572. const range = Range.createFromParentsAndOffsets( root, 0, root, 1 );
  573. model.change( () => {
  574. // Set an attribute on an element. It won't change afterwards.
  575. attribute( range, 'otherAttr', null, true );
  576. } );
  577. model.change( () => {
  578. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  579. const diffRange = Range.createFromParentsAndOffsets( root, 0, root.getChild( 0 ), 0 );
  580. expectChanges( [
  581. { type: 'attribute', range: diffRange, attributeKey, attributeOldValue, attributeNewValue }
  582. ] );
  583. } );
  584. } );
  585. it( 'on a character', () => {
  586. const parent = root.getChild( 1 );
  587. const range = Range.createFromParentsAndOffsets( parent, 1, parent, 2 );
  588. model.change( () => {
  589. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  590. expectChanges( [
  591. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  592. ] );
  593. } );
  594. } );
  595. it( 'on a character - case with same characters next to each other', () => {
  596. const parent = root.getChild( 0 );
  597. const range = Range.createFromParentsAndOffsets( parent, 1, parent, 2 );
  598. model.change( () => {
  599. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  600. expectChanges( [
  601. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  602. ] );
  603. } );
  604. } );
  605. it( 'on multiple characters', () => {
  606. const parent = root.getChild( 0 );
  607. const range = Range.createFromParentsAndOffsets( parent, 0, parent, 3 );
  608. model.change( () => {
  609. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  610. expectChanges( [
  611. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  612. ] );
  613. } );
  614. } );
  615. it( 'on multiple consecutive characters in multiple operations', () => {
  616. const parent = root.getChild( 0 );
  617. const range1 = Range.createFromParentsAndOffsets( parent, 1, parent, 2 );
  618. const range2 = Range.createFromParentsAndOffsets( parent, 2, parent, 3 );
  619. model.change( () => {
  620. attribute( range1, attributeKey, attributeOldValue, attributeNewValue );
  621. attribute( range2, attributeKey, attributeOldValue, attributeNewValue );
  622. const range = Range.createFromParentsAndOffsets( parent, 1, parent, 3 );
  623. expectChanges( [
  624. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  625. ] );
  626. } );
  627. } );
  628. it( 'attribute changes intersecting #1', () => {
  629. const parent = root.getChild( 1 );
  630. // Be aware that you cannot make an intersecting changes with the same attribute key,
  631. // cause the value would be incorrect for the common part of the ranges.
  632. const ranges = [
  633. [ 0, 2, null, true, 'foo' ],
  634. [ 1, 3, null, true, 'bar' ]
  635. ];
  636. model.change( () => {
  637. for ( const item of ranges ) {
  638. const range = Range.createFromParentsAndOffsets( parent, item[ 0 ], parent, item[ 1 ] );
  639. attribute( range, item[ 4 ], item[ 2 ], item[ 3 ] );
  640. }
  641. expectChanges( [
  642. {
  643. type: 'attribute',
  644. range: Range.createFromParentsAndOffsets( parent, 0, parent, 2 ),
  645. attributeKey: 'foo',
  646. attributeOldValue: null,
  647. attributeNewValue: true
  648. },
  649. {
  650. type: 'attribute',
  651. range: Range.createFromParentsAndOffsets( parent, 1, parent, 3 ),
  652. attributeKey: 'bar',
  653. attributeOldValue: null,
  654. attributeNewValue: true
  655. }
  656. ] );
  657. } );
  658. } );
  659. it( 'attribute changes intersecting #2', () => {
  660. const parent = root.getChild( 1 );
  661. // Be aware that you cannot make an intersecting changes with the same attribute key,
  662. // cause the value would be incorrect for the common part of the ranges.
  663. const ranges = [
  664. [ 1, 3, null, true, 'foo' ],
  665. [ 0, 2, null, true, 'bar' ]
  666. ];
  667. model.change( () => {
  668. for ( const item of ranges ) {
  669. const range = Range.createFromParentsAndOffsets( parent, item[ 0 ], parent, item[ 1 ] );
  670. attribute( range, item[ 4 ], item[ 2 ], item[ 3 ] );
  671. }
  672. expectChanges( [
  673. {
  674. type: 'attribute',
  675. range: Range.createFromParentsAndOffsets( parent, 0, parent, 1 ),
  676. attributeKey: 'bar',
  677. attributeOldValue: null,
  678. attributeNewValue: true
  679. },
  680. {
  681. type: 'attribute',
  682. range: Range.createFromParentsAndOffsets( parent, 1, parent, 2 ),
  683. attributeKey: 'foo',
  684. attributeOldValue: null,
  685. attributeNewValue: true
  686. },
  687. {
  688. type: 'attribute',
  689. range: Range.createFromParentsAndOffsets( parent, 1, parent, 2 ),
  690. attributeKey: 'bar',
  691. attributeOldValue: null,
  692. attributeNewValue: true
  693. },
  694. {
  695. type: 'attribute',
  696. range: Range.createFromParentsAndOffsets( parent, 2, parent, 3 ),
  697. attributeKey: 'foo',
  698. attributeOldValue: null,
  699. attributeNewValue: true
  700. }
  701. ] );
  702. } );
  703. } );
  704. it( 'attribute changes included in an attribute change #1 - changes are reversed at the end', () => {
  705. const parent = root.getChild( 1 );
  706. const ranges = [
  707. [ 0, 1, null, true ],
  708. [ 1, 2, null, true ],
  709. [ 0, 2, true, null ]
  710. ];
  711. model.change( () => {
  712. for ( const item of ranges ) {
  713. const range = Range.createFromParentsAndOffsets( parent, item[ 0 ], parent, item[ 1 ] );
  714. attribute( range, attributeKey, item[ 2 ], item[ 3 ] );
  715. }
  716. expectChanges( [] );
  717. } );
  718. } );
  719. it( 'attribute changes included in an attribute change #2 - changes are re-applied at the end', () => {
  720. const parent = root.getChild( 1 );
  721. const ranges = [
  722. [ 0, 1, null, true ],
  723. [ 1, 2, null, true ],
  724. [ 0, 2, true, null ],
  725. [ 0, 1, null, true ],
  726. [ 1, 2, null, true ]
  727. ];
  728. model.change( () => {
  729. for ( const item of ranges ) {
  730. const range = Range.createFromParentsAndOffsets( parent, item[ 0 ], parent, item[ 1 ] );
  731. attribute( range, attributeKey, item[ 2 ], item[ 3 ] );
  732. }
  733. expectChanges( [ {
  734. type: 'attribute',
  735. range: Range.createFromParentsAndOffsets( parent, 0, parent, 2 ),
  736. attributeKey,
  737. attributeOldValue: null,
  738. attributeNewValue: true
  739. } ] );
  740. } );
  741. } );
  742. it( 'on multiple non-consecutive characters in multiple operations', () => {
  743. const parent = root.getChild( 0 );
  744. const range1 = Range.createFromParentsAndOffsets( parent, 0, parent, 1 );
  745. const range2 = Range.createFromParentsAndOffsets( parent, 2, parent, 3 );
  746. model.change( () => {
  747. // Note "reversed" order of ranges. Further range is changed first.
  748. attribute( range2, attributeKey, attributeOldValue, attributeNewValue );
  749. attribute( range1, attributeKey, attributeOldValue, attributeNewValue );
  750. // Note that changes has been sorted.
  751. expectChanges( [
  752. { type: 'attribute', range: range1, attributeKey, attributeOldValue, attributeNewValue },
  753. { type: 'attribute', range: range2, attributeKey, attributeOldValue, attributeNewValue }
  754. ] );
  755. } );
  756. } );
  757. it( 'on range containing various nodes', () => {
  758. const range = Range.createFromParentsAndOffsets( root, 0, root, 2 );
  759. model.change( () => {
  760. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  761. const p1 = root.getChild( 0 );
  762. const p2 = root.getChild( 1 );
  763. const type = 'attribute';
  764. expectChanges( [
  765. {
  766. type,
  767. range: Range.createFromParentsAndOffsets( root, 0, p1, 0 ),
  768. attributeKey,
  769. attributeOldValue,
  770. attributeNewValue
  771. },
  772. {
  773. type,
  774. range: Range.createFromParentsAndOffsets( root, 1, p2, 0 ),
  775. attributeKey,
  776. attributeOldValue,
  777. attributeNewValue
  778. }
  779. ] );
  780. } );
  781. } );
  782. it( 'remove and add attribute on text', () => {
  783. const p = root.getChild( 1 );
  784. p.getChild( 0 )._setAttribute( 'bold', true );
  785. const range = Range.createFromParentsAndOffsets( p, 1, p, 3 );
  786. model.change( () => {
  787. attribute( range, 'bold', true, null );
  788. attribute( range, 'italic', null, true );
  789. const range1 = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  790. const range2 = Range.createFromParentsAndOffsets( p, 2, p, 3 );
  791. // Attribute change glueing does not work 100% correct.
  792. expectChanges( [
  793. {
  794. type: 'attribute',
  795. range: range1,
  796. attributeKey: 'bold',
  797. attributeOldValue: true,
  798. attributeNewValue: null
  799. },
  800. {
  801. type: 'attribute',
  802. range: range1,
  803. attributeKey: 'italic',
  804. attributeOldValue: null,
  805. attributeNewValue: true
  806. },
  807. {
  808. type: 'attribute',
  809. range: range2,
  810. attributeKey: 'bold',
  811. attributeOldValue: true,
  812. attributeNewValue: null
  813. },
  814. {
  815. type: 'attribute',
  816. range: range2,
  817. attributeKey: 'italic',
  818. attributeOldValue: null,
  819. attributeNewValue: true
  820. }
  821. ] );
  822. } );
  823. } );
  824. it( 'on some old nodes and inserted nodes', () => {
  825. const position = new Position( root, [ 0, 1 ] );
  826. const p1 = root.getChild( 0 );
  827. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 2 );
  828. model.change( () => {
  829. insert( new Text( 'xx' ), position );
  830. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  831. const rangeBefore = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  832. expectChanges( [
  833. { type: 'attribute', range: rangeBefore, attributeKey, attributeOldValue, attributeNewValue },
  834. { type: 'insert', name: '$text', length: 2, position }
  835. ] );
  836. } );
  837. } );
  838. it( 'only on inserted nodes', () => {
  839. const position = new Position( root, [ 0, 1 ] );
  840. const p1 = root.getChild( 0 );
  841. const range = Range.createFromParentsAndOffsets( p1, 2, p1, 3 );
  842. model.change( () => {
  843. insert( new Text( 'xxx' ), position );
  844. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  845. expectChanges( [
  846. { type: 'insert', name: '$text', length: 3, position }
  847. ] );
  848. } );
  849. } );
  850. it( 'on some inserted nodes and old nodes', () => {
  851. const position = new Position( root, [ 0, 1 ] );
  852. const p1 = root.getChild( 0 );
  853. const range = Range.createFromParentsAndOffsets( p1, 2, p1, 4 );
  854. model.change( () => {
  855. insert( new Text( 'xx' ), position );
  856. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  857. const rangeAfter = Range.createFromParentsAndOffsets( p1, 3, p1, 4 );
  858. expectChanges( [
  859. { type: 'insert', name: '$text', length: 2, position },
  860. { type: 'attribute', range: rangeAfter, attributeKey, attributeOldValue, attributeNewValue }
  861. ] );
  862. } );
  863. } );
  864. it( 'over all inserted nodes and some old nodes', () => {
  865. const position = new Position( root, [ 0, 1 ] );
  866. const p1 = root.getChild( 0 );
  867. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 4 );
  868. model.change( () => {
  869. insert( new Text( 'xx' ), position );
  870. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  871. const rangeBefore = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  872. const rangeAfter = Range.createFromParentsAndOffsets( p1, 3, p1, 4 );
  873. expectChanges( [
  874. { type: 'attribute', range: rangeBefore, attributeKey, attributeOldValue, attributeNewValue },
  875. { type: 'insert', name: '$text', length: 2, position },
  876. { type: 'attribute', range: rangeAfter, attributeKey, attributeOldValue, attributeNewValue }
  877. ] );
  878. } );
  879. } );
  880. it( 'on some not changed and some changed nodes', () => {
  881. const p = root.getChild( 0 );
  882. const rangeA = Range.createFromParentsAndOffsets( p, 1, p, 3 );
  883. const rangeB = Range.createFromParentsAndOffsets( p, 0, p, 2 );
  884. model.change( () => {
  885. attribute( rangeA, 'a', null, true );
  886. attribute( rangeB, 'b', null, true );
  887. const type = 'attribute';
  888. const attributeOldValue = null;
  889. const attributeNewValue = true;
  890. // Attribute change glueing does not work 100% correct.
  891. expectChanges( [
  892. {
  893. type,
  894. range: Range.createFromParentsAndOffsets( p, 0, p, 1 ),
  895. attributeKey: 'b',
  896. attributeOldValue,
  897. attributeNewValue
  898. },
  899. {
  900. type,
  901. range: Range.createFromParentsAndOffsets( p, 1, p, 2 ),
  902. attributeKey: 'a',
  903. attributeOldValue,
  904. attributeNewValue
  905. },
  906. {
  907. type,
  908. range: Range.createFromParentsAndOffsets( p, 1, p, 2 ),
  909. attributeKey: 'b',
  910. attributeOldValue,
  911. attributeNewValue
  912. },
  913. {
  914. type,
  915. range: Range.createFromParentsAndOffsets( p, 2, p, 3 ),
  916. attributeKey: 'a',
  917. attributeOldValue,
  918. attributeNewValue
  919. }
  920. ] );
  921. } );
  922. } );
  923. it( 'on already changed nodes', () => {
  924. const p = root.getChild( 1 );
  925. const rangeA = Range.createFromParentsAndOffsets( p, 0, p, 3 );
  926. const rangeB = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  927. model.change( () => {
  928. attribute( rangeA, 'a', null, true );
  929. attribute( rangeB, 'b', null, true );
  930. const type = 'attribute';
  931. const attributeOldValue = null;
  932. const attributeNewValue = true;
  933. // Attribute change glueing does not work 100% correct.
  934. expectChanges( [
  935. {
  936. type,
  937. range: Range.createFromParentsAndOffsets( p, 0, p, 2 ),
  938. attributeKey: 'a',
  939. attributeOldValue,
  940. attributeNewValue
  941. },
  942. {
  943. type,
  944. range: Range.createFromParentsAndOffsets( p, 1, p, 2 ),
  945. attributeKey: 'b',
  946. attributeOldValue,
  947. attributeNewValue
  948. },
  949. {
  950. type,
  951. range: Range.createFromParentsAndOffsets( p, 2, p, 3 ),
  952. attributeKey: 'a',
  953. attributeOldValue,
  954. attributeNewValue
  955. }
  956. ] );
  957. } );
  958. } );
  959. it( 'on some changed and some not changed nodes', () => {
  960. const p = root.getChild( 1 );
  961. const rangeA = Range.createFromParentsAndOffsets( p, 0, p, 2 );
  962. const rangeB = Range.createFromParentsAndOffsets( p, 1, p, 3 );
  963. model.change( () => {
  964. attribute( rangeA, 'a', null, true );
  965. attribute( rangeB, 'b', null, true );
  966. const type = 'attribute';
  967. const attributeOldValue = null;
  968. const attributeNewValue = true;
  969. expectChanges( [
  970. {
  971. type,
  972. range: Range.createFromParentsAndOffsets( p, 0, p, 2 ),
  973. attributeKey: 'a',
  974. attributeOldValue,
  975. attributeNewValue
  976. },
  977. {
  978. type,
  979. range: Range.createFromParentsAndOffsets( p, 1, p, 3 ),
  980. attributeKey: 'b',
  981. attributeOldValue,
  982. attributeNewValue
  983. }
  984. ] );
  985. } );
  986. } );
  987. it( 'over all changed nodes and some not changed nodes', () => {
  988. const p = root.getChild( 0 );
  989. const rangeA = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  990. const rangeB = Range.createFromParentsAndOffsets( p, 0, p, 3 );
  991. model.change( () => {
  992. attribute( rangeA, 'a', null, true );
  993. attribute( rangeB, 'b', null, true );
  994. const type = 'attribute';
  995. const attributeOldValue = null;
  996. const attributeNewValue = true;
  997. // Attribute change glueing does not work 100% correct.
  998. expectChanges( [
  999. {
  1000. type,
  1001. range: Range.createFromParentsAndOffsets( p, 0, p, 1 ),
  1002. attributeKey: 'b',
  1003. attributeOldValue,
  1004. attributeNewValue
  1005. },
  1006. {
  1007. type,
  1008. range: Range.createFromParentsAndOffsets( p, 1, p, 2 ),
  1009. attributeKey: 'a',
  1010. attributeOldValue,
  1011. attributeNewValue
  1012. },
  1013. {
  1014. type,
  1015. range: Range.createFromParentsAndOffsets( p, 1, p, 3 ),
  1016. attributeKey: 'b',
  1017. attributeOldValue,
  1018. attributeNewValue
  1019. }
  1020. ] );
  1021. } );
  1022. } );
  1023. } );
  1024. describe( 'split', () => {
  1025. it( 'split an element', () => {
  1026. const position = new Position( root, [ 0, 2 ] );
  1027. model.change( () => {
  1028. split( position );
  1029. expectChanges( [
  1030. { type: 'remove', name: '$text', length: 1, position },
  1031. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1032. ] );
  1033. } );
  1034. } );
  1035. it( 'split a new element', () => {
  1036. model.change( () => {
  1037. insert(
  1038. new Element( 'paragraph', null, new Text( 'Ab' ) ),
  1039. new Position( root, [ 0 ] )
  1040. );
  1041. split( new Position( root, [ 0, 1 ] ) );
  1042. expectChanges( [
  1043. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  1044. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1045. ] );
  1046. } );
  1047. } );
  1048. it( 'split an element inside a new element', () => {
  1049. model.change( () => {
  1050. insert(
  1051. new Element( 'blockQuote', null, new Element( 'paragraph', null, new Text( 'Ab' ) ) ),
  1052. new Position( root, [ 0 ] )
  1053. );
  1054. split( new Position( root, [ 0, 0, 1 ] ) );
  1055. expectChanges( [
  1056. { type: 'insert', name: 'blockQuote', length: 1, position: new Position( root, [ 0 ] ) }
  1057. ] );
  1058. } );
  1059. } );
  1060. it( 'should correctly mark a change in graveyard', () => {
  1061. model.change( () => {
  1062. merge( new Position( root, [ 1, 0 ] ), new Position( root, [ 0, 3 ] ) );
  1063. } );
  1064. model.change( () => {
  1065. const operation = new SplitOperation(
  1066. new Position( root, [ 0, 3 ] ),
  1067. 3,
  1068. new Position( doc.graveyard, [ 0 ] ),
  1069. doc.version
  1070. );
  1071. model.applyOperation( operation );
  1072. expectChanges( [
  1073. { type: 'remove', name: 'paragraph', length: 1, position: new Position( doc.graveyard, [ 0 ] ) },
  1074. { type: 'remove', name: '$text', length: 3, position: new Position( root, [ 0, 3 ] ) },
  1075. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1076. ], true );
  1077. } );
  1078. } );
  1079. } );
  1080. describe( 'merge', () => {
  1081. it( 'merge two elements', () => {
  1082. model.change( () => {
  1083. merge( new Position( root, [ 1, 0 ] ), new Position( root, [ 0, 3 ] ) );
  1084. expectChanges( [
  1085. { type: 'insert', name: '$text', length: 3, position: new Position( root, [ 0, 3 ] ) },
  1086. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1087. ] );
  1088. } );
  1089. } );
  1090. it( 'merge a new element', () => {
  1091. model.change( () => {
  1092. insert( new Element( 'paragraph', null, new Text( 'Ab' ) ), new Position( root, [ 1 ] ) );
  1093. merge( new Position( root, [ 1, 0 ] ), new Position( root, [ 0, 3 ] ) );
  1094. expectChanges( [
  1095. { type: 'insert', name: '$text', length: 2, position: new Position( root, [ 0, 3 ] ) }
  1096. ] );
  1097. } );
  1098. } );
  1099. it( 'merge into a new element', () => {
  1100. model.change( () => {
  1101. insert( new Element( 'paragraph', null, new Text( 'Ab' ) ), new Position( root, [ 0 ] ) );
  1102. merge( new Position( root, [ 1, 0 ] ), new Position( root, [ 0, 2 ] ) );
  1103. expectChanges( [
  1104. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  1105. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1106. ] );
  1107. } );
  1108. } );
  1109. it( 'merge elements inside a new element', () => {
  1110. model.change( () => {
  1111. insert(
  1112. new Element( 'blockQuote', null, [
  1113. new Element( 'paragraph', null, new Text( 'Ab' ) ),
  1114. new Element( 'paragraph', null, new Text( 'Xyz' ) )
  1115. ] ),
  1116. new Position( root, [ 0 ] )
  1117. );
  1118. merge( new Position( root, [ 0, 1, 0 ] ), new Position( root, [ 0, 0, 2 ] ) );
  1119. expectChanges( [
  1120. { type: 'insert', name: 'blockQuote', length: 1, position: new Position( root, [ 0 ] ) }
  1121. ] );
  1122. } );
  1123. } );
  1124. it( 'should correctly mark a change in graveyard', () => {
  1125. model.change( () => {
  1126. merge( new Position( root, [ 1, 0 ] ), new Position( root, [ 0, 3 ] ) );
  1127. expectChanges( [
  1128. { type: 'insert', name: 'paragraph', length: 1, position: new Position( doc.graveyard, [ 0 ] ) },
  1129. { type: 'insert', name: '$text', length: 3, position: new Position( root, [ 0, 3 ] ) },
  1130. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1131. ], true );
  1132. } );
  1133. } );
  1134. } );
  1135. describe( 'wrap', () => {
  1136. it( 'wrap elements', () => {
  1137. model.change( () => {
  1138. wrap( new Position( root, [ 0 ] ), 2, new Element( 'blockQuote' ) );
  1139. expectChanges( [
  1140. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  1141. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  1142. { type: 'insert', name: 'blockQuote', length: 1, position: new Position( root, [ 0 ] ) }
  1143. ] );
  1144. } );
  1145. } );
  1146. it( 'wrap old and new elements', () => {
  1147. model.change( () => {
  1148. insert( new Element( 'paragraph' ), new Position( root, [ 0 ] ) );
  1149. wrap( new Position( root, [ 0 ] ), 2, new Element( 'blockQuote' ) );
  1150. expectChanges( [
  1151. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  1152. { type: 'insert', name: 'blockQuote', length: 1, position: new Position( root, [ 0 ] ) }
  1153. ] );
  1154. } );
  1155. } );
  1156. it( 'wrap inside a new element', () => {
  1157. model.change( () => {
  1158. insert(
  1159. new Element( 'div', null, [
  1160. new Element( 'paragraph' ),
  1161. new Element( 'paragraph' )
  1162. ] ),
  1163. new Position( root, [ 0 ] )
  1164. );
  1165. wrap( new Position( root, [ 0, 0 ] ), 2, new Element( 'blockQuote' ) );
  1166. expectChanges( [
  1167. { type: 'insert', name: 'div', length: 1, position: new Position( root, [ 0 ] ) }
  1168. ] );
  1169. } );
  1170. } );
  1171. it( 'should correctly mark a change in graveyard', () => {
  1172. model.change( () => {
  1173. unwrap( new Position( root, [ 0, 0 ] ) );
  1174. } );
  1175. model.change( () => {
  1176. const operation = new WrapOperation( new Position( root, [ 0 ] ), 3, new Position( doc.graveyard, [ 0 ] ), doc.version );
  1177. model.applyOperation( operation );
  1178. expectChanges( [
  1179. { type: 'remove', name: 'paragraph', length: 1, position: new Position( doc.graveyard, [ 0 ] ) },
  1180. { type: 'remove', name: '$text', length: 3, position: new Position( root, [ 0 ] ) },
  1181. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) }
  1182. ], true );
  1183. } );
  1184. } );
  1185. } );
  1186. describe( 'unwrap', () => {
  1187. it( 'unwrap elements', () => {
  1188. model.change( () => {
  1189. unwrap( new Position( root, [ 0, 0 ] ) );
  1190. expectChanges( [
  1191. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  1192. { type: 'insert', name: '$text', length: 3, position: new Position( root, [ 0 ] ) }
  1193. ] );
  1194. } );
  1195. } );
  1196. it( 'unwrap a new element', () => {
  1197. model.change( () => {
  1198. insert( new Element( 'paragraph', null, new Text( 'Ab' ) ), new Position( root, [ 0 ] ) );
  1199. unwrap( new Position( root, [ 0, 0 ] ) );
  1200. expectChanges( [
  1201. { type: 'insert', name: '$text', length: 2, position: new Position( root, [ 0 ] ) }
  1202. ] );
  1203. } );
  1204. } );
  1205. it( 'unwrap element with new nodes', () => {
  1206. model.change( () => {
  1207. insert( new Text( 'Ab' ), new Position( root, [ 0, 1 ] ) );
  1208. unwrap( new Position( root, [ 0, 0 ] ) );
  1209. expectChanges( [
  1210. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  1211. { type: 'insert', name: '$text', length: 5, position: new Position( root, [ 0 ] ) }
  1212. ] );
  1213. } );
  1214. } );
  1215. it( 'unwrap element inside a new element', () => {
  1216. model.change( () => {
  1217. insert(
  1218. new Element( 'blockQuote', null, [
  1219. new Element( 'paragraph', null, new Text( 'Ab' ) )
  1220. ] ),
  1221. new Position( root, [ 0 ] )
  1222. );
  1223. unwrap( new Position( root, [ 0, 0, 0 ] ) );
  1224. expectChanges( [
  1225. { type: 'insert', name: 'blockQuote', length: 1, position: new Position( root, [ 0 ] ) }
  1226. ] );
  1227. } );
  1228. } );
  1229. it( 'should correctly mark a change in graveyard', () => {
  1230. model.change( () => {
  1231. unwrap( new Position( root, [ 0, 0 ] ) );
  1232. expectChanges( [
  1233. { type: 'insert', name: 'paragraph', length: 1, position: new Position( doc.graveyard, [ 0 ] ) },
  1234. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  1235. { type: 'insert', name: '$text', length: 3, position: new Position( root, [ 0 ] ) }
  1236. ], true );
  1237. } );
  1238. } );
  1239. } );
  1240. describe( 'markers', () => {
  1241. let range, rangeB;
  1242. beforeEach( () => {
  1243. range = Range.createFromParentsAndOffsets( root, 0, root, 1 );
  1244. rangeB = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  1245. } );
  1246. it( 'add marker', () => {
  1247. differ.bufferMarkerChange( 'name', null, range, true );
  1248. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  1249. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  1250. { name: 'name', range }
  1251. ] );
  1252. } );
  1253. it( 'remove marker', () => {
  1254. differ.bufferMarkerChange( 'name', range, null, true );
  1255. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  1256. { name: 'name', range }
  1257. ] );
  1258. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  1259. } );
  1260. it( 'change marker\'s range', () => {
  1261. differ.bufferMarkerChange( 'name', range, rangeB, true );
  1262. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  1263. { name: 'name', range }
  1264. ] );
  1265. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  1266. { name: 'name', range: rangeB }
  1267. ] );
  1268. } );
  1269. it( 'add marker not affecting data', () => {
  1270. differ.bufferMarkerChange( 'name', range, rangeB, false );
  1271. expect( differ.hasDataChanges() ).to.be.false;
  1272. } );
  1273. it( 'add marker affecting data', () => {
  1274. differ.bufferMarkerChange( 'name', range, rangeB, true );
  1275. expect( differ.hasDataChanges() ).to.be.true;
  1276. } );
  1277. it( 'add marker and remove it', () => {
  1278. differ.bufferMarkerChange( 'name', null, range, true );
  1279. differ.bufferMarkerChange( 'name', range, null, true );
  1280. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  1281. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  1282. expect( differ.hasDataChanges() ).to.be.false;
  1283. } );
  1284. it( 'add marker and change it', () => {
  1285. differ.bufferMarkerChange( 'name', null, range, true );
  1286. differ.bufferMarkerChange( 'name', range, rangeB, true );
  1287. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  1288. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  1289. { name: 'name', range: rangeB }
  1290. ] );
  1291. } );
  1292. it( 'change marker to not affecting data', () => {
  1293. differ.bufferMarkerChange( 'name', range, rangeB, true );
  1294. differ.bufferMarkerChange( 'name', range, rangeB, false );
  1295. expect( differ.hasDataChanges() ).to.be.false;
  1296. } );
  1297. it( 'change marker and remove it', () => {
  1298. differ.bufferMarkerChange( 'name', range, rangeB, true );
  1299. differ.bufferMarkerChange( 'name', rangeB, null, true );
  1300. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  1301. { name: 'name', range }
  1302. ] );
  1303. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  1304. expect( differ.hasDataChanges() ).to.be.true;
  1305. } );
  1306. it( 'remove marker and add it at same range', () => {
  1307. differ.bufferMarkerChange( 'name', range, null, true );
  1308. differ.bufferMarkerChange( 'name', null, range, true );
  1309. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  1310. { name: 'name', range }
  1311. ] );
  1312. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  1313. { name: 'name', range }
  1314. ] );
  1315. } );
  1316. it( 'change marker to the same range', () => {
  1317. differ.bufferMarkerChange( 'name', range, range, true );
  1318. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  1319. { name: 'name', range }
  1320. ] );
  1321. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  1322. { name: 'name', range }
  1323. ] );
  1324. } );
  1325. } );
  1326. describe( 'other cases', () => {
  1327. // #1309.
  1328. it( 'multiple inserts and removes in one element', () => {
  1329. model.change( () => {
  1330. insert( new Text( 'x' ), new Position( root, [ 0, 2 ] ) );
  1331. insert( new Text( 'x' ), new Position( root, [ 0, 3 ] ) );
  1332. move( new Position( root, [ 0, 2 ] ), 1, new Position( root, [ 1, 0 ] ) );
  1333. expectChanges( [
  1334. { type: 'insert', name: '$text', length: 1, position: new Position( root, [ 0, 2 ] ) },
  1335. { type: 'insert', name: '$text', length: 1, position: new Position( root, [ 1, 0 ] ) }
  1336. ] );
  1337. } );
  1338. } );
  1339. // ckeditor5#733.
  1340. it( 'proper filtering of changes in removed elements', () => {
  1341. // Before fix there was a buggy scenario described in ckeditor5#733.
  1342. // There was this structure: `<paragraph>foo[</paragraph><image /><blockQuote><p>te]xt</p></blockQuote>`
  1343. // On delete of above selection `image` and `paragraph` inside `blockQuote` are removed (it gets merged).
  1344. // However, since `image` was removed first, when checking if `paragraph` is in a removed element,
  1345. // it appeared that `blockQuote` looks like it is removed because it had the same path as the already removed `<image>`.
  1346. // In a result, removing `paragraph` was discarded.
  1347. // The mistake was that the checking for removing was done at incorrect moment.
  1348. root._removeChildren( 0, root.childCount );
  1349. root._appendChild( [
  1350. new Element( 'paragraph', null, new Text( 'foo' ) ),
  1351. new Element( 'image' ),
  1352. new Element( 'blockQuote', null, [
  1353. new Element( 'paragraph', null, new Text( 'text' ) )
  1354. ] )
  1355. ] );
  1356. model.change( () => {
  1357. // Remove `"te"`.
  1358. remove( new Position( root, [ 2, 0, 0 ] ), 2 );
  1359. // Remove `image` before `blockQuote`.
  1360. remove( new Position( root, [ 1 ] ), 1 );
  1361. // Move `"xt"` to first `paragraph` (merging).
  1362. move( new Position( root, [ 1, 0, 0 ] ), 2, new Position( root, [ 0, 3 ] ) );
  1363. // Remove `paragraph` inside `blockQuote`.
  1364. remove( new Position( root, [ 1, 0 ] ), 1 );
  1365. expectChanges( [
  1366. { type: 'insert', name: '$text', length: 2, position: new Position( root, [ 0, 3 ] ) },
  1367. { type: 'remove', name: 'image', length: 1, position: new Position( root, [ 1 ] ) },
  1368. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1, 0 ] ) }
  1369. ] );
  1370. } );
  1371. } );
  1372. // In this scenario we create a new element, then remove something from before it to mess up with offsets,
  1373. // finally we insert some content into a new element. Since we are inserting into a new element, the
  1374. // inserted children should not be shown on changes list.
  1375. it( 'proper filtering of changes in inserted elements', () => {
  1376. root._removeChildren( 0, root.childCount );
  1377. root._appendChild( new Element( 'image' ) );
  1378. const blockQuote = new Element( 'blockQuote', null, new Element( 'paragraph' ) );
  1379. model.change( () => {
  1380. // Insert `blockQuote` with `paragraph` after `image`.
  1381. insert( blockQuote, new Position( root, [ 1 ] ) );
  1382. // Remove `image` from before `blockQuote`.
  1383. remove( new Position( root, [ 0 ] ), 1 );
  1384. // Insert text into `paragraph` in `blockQuote`.
  1385. insert( new Text( 'foo' ), new Position( root, [ 0, 0, 0 ] ) );
  1386. expectChanges( [
  1387. { type: 'remove', name: 'image', length: 1, position: new Position( root, [ 0 ] ) },
  1388. { type: 'insert', name: 'blockQuote', length: 1, position: new Position( root, [ 0 ] ) }
  1389. ] );
  1390. } );
  1391. } );
  1392. // In this scenario we create a new element, then move another element that was before the new element into
  1393. // the new element. This way we mess up with offsets and insert content into a new element in one operation.
  1394. // Since we are inserting into a new element, the insertion of moved element should not be shown on changes list.
  1395. it( 'proper filtering of changes in inserted elements #2', () => {
  1396. root._removeChildren( 0, root.childCount );
  1397. root._appendChild( new Element( 'image' ) );
  1398. model.change( () => {
  1399. // Insert `div` after `image`.
  1400. insert( new Element( 'div' ), new Position( root, [ 1 ] ) );
  1401. // Move `image` to the new `div`.
  1402. move( new Position( root, [ 0 ] ), 1, new Position( root, [ 1, 0 ] ) );
  1403. expectChanges( [
  1404. { type: 'remove', name: 'image', length: 1, position: new Position( root, [ 0 ] ) },
  1405. { type: 'insert', name: 'div', length: 1, position: new Position( root, [ 0 ] ) }
  1406. ] );
  1407. } );
  1408. } );
  1409. // #1392.
  1410. it( 'remove is correctly transformed by multiple affecting changes', () => {
  1411. root._appendChild( new Element( 'paragraph', null, new Text( 'xyz' ) ) );
  1412. model.change( () => {
  1413. rename( root.getChild( 1 ), 'heading' );
  1414. rename( root.getChild( 2 ), 'heading' );
  1415. remove( Position.createAt( root, 0 ), 3 );
  1416. expectChanges( [
  1417. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  1418. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  1419. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) }
  1420. ] );
  1421. } );
  1422. } );
  1423. } );
  1424. describe( 'getChanges()', () => {
  1425. let position, p1, rangeAttrChange, range;
  1426. beforeEach( () => {
  1427. position = new Position( root, [ 0, 1 ] );
  1428. p1 = root.getChild( 0 );
  1429. range = Range.createFromParentsAndOffsets( p1, 2, p1, 4 );
  1430. rangeAttrChange = Range.createFromParentsAndOffsets( p1, 3, p1, 4 );
  1431. } );
  1432. it( 'should return changes in graveyard if a flag was set up', () => {
  1433. const removePosition = new Position( root, [ 1 ] );
  1434. model.change( () => {
  1435. insert( new Text( 'xx' ), position );
  1436. attribute( range, 'key', null, 'foo' );
  1437. remove( removePosition, 1 );
  1438. expectChanges( [
  1439. { type: 'insert', name: 'paragraph', length: 1, position: new Position( doc.graveyard, [ 0 ] ) },
  1440. { type: 'insert', name: '$text', length: 2, position },
  1441. {
  1442. type: 'attribute',
  1443. range: rangeAttrChange,
  1444. attributeKey: 'key',
  1445. attributeOldValue: null,
  1446. attributeNewValue: 'foo'
  1447. },
  1448. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  1449. ], true );
  1450. } );
  1451. } );
  1452. // Below tests test caching.
  1453. it( 'should return same change set if was called twice in a row', () => {
  1454. model.change( () => {
  1455. insert( new Text( 'xx' ), position );
  1456. attribute( range, 'key', null, 'foo' );
  1457. const changesA = differ.getChanges();
  1458. const changesB = differ.getChanges();
  1459. expect( changesA ).to.deep.equal( changesB );
  1460. } );
  1461. } );
  1462. it( 'should return same change set if was called twice in a row - graveyard changes', () => {
  1463. model.change( () => {
  1464. insert( new Text( 'xx' ), position );
  1465. attribute( range, 'key', null, 'foo' );
  1466. const removePosition = new Position( root, [ 1 ] );
  1467. remove( removePosition, 1 );
  1468. const changesA = differ.getChanges( { includeChangesInGraveyard: true } );
  1469. const changesB = differ.getChanges( { includeChangesInGraveyard: true } );
  1470. expect( changesA ).to.deep.equal( changesB );
  1471. } );
  1472. } );
  1473. it( 'should return correct changes if change happened between getChanges() calls', () => {
  1474. model.change( () => {
  1475. insert( new Text( 'xx' ), position );
  1476. attribute( range, 'key', null, 'foo' );
  1477. expectChanges( [
  1478. { type: 'insert', name: '$text', length: 2, position },
  1479. {
  1480. type: 'attribute',
  1481. range: rangeAttrChange,
  1482. attributeKey: 'key',
  1483. attributeOldValue: null,
  1484. attributeNewValue: 'foo'
  1485. }
  1486. ] );
  1487. const removePosition = new Position( root, [ 1 ] );
  1488. remove( removePosition, 1 );
  1489. expectChanges( [
  1490. { type: 'insert', name: '$text', length: 2, position },
  1491. {
  1492. type: 'attribute',
  1493. range: rangeAttrChange,
  1494. attributeKey: 'key',
  1495. attributeOldValue: null,
  1496. attributeNewValue: 'foo'
  1497. },
  1498. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  1499. ] );
  1500. } );
  1501. } );
  1502. it( 'should return correct changes if reset happened between getChanges() calls', () => {
  1503. model.change( () => {
  1504. insert( new Text( 'xx' ), position );
  1505. attribute( range, 'key', null, 'foo' );
  1506. expectChanges( [
  1507. { type: 'insert', name: '$text', length: 2, position },
  1508. {
  1509. type: 'attribute',
  1510. range: rangeAttrChange,
  1511. attributeKey: 'key',
  1512. attributeOldValue: null,
  1513. attributeNewValue: 'foo'
  1514. }
  1515. ] );
  1516. differ.reset();
  1517. const removePosition = new Position( root, [ 1 ] );
  1518. remove( removePosition, 1 );
  1519. expectChanges( [
  1520. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  1521. ] );
  1522. } );
  1523. } );
  1524. } );
  1525. function insert( item, position ) {
  1526. const operation = new InsertOperation( position, item, doc.version );
  1527. model.applyOperation( operation );
  1528. }
  1529. function split( position ) {
  1530. const howMany = position.parent.maxOffset - position.offset;
  1531. const operation = new SplitOperation( position, howMany, null, doc.version );
  1532. model.applyOperation( operation );
  1533. }
  1534. function merge( source, target ) {
  1535. const howMany = source.parent.maxOffset;
  1536. const operation = new MergeOperation( source, howMany, target, new Position( doc.graveyard, [ 0 ] ), doc.version );
  1537. model.applyOperation( operation );
  1538. }
  1539. function wrap( position, howMany, element ) {
  1540. const operation = new WrapOperation( position, howMany, element, doc.version );
  1541. model.applyOperation( operation );
  1542. }
  1543. function unwrap( position ) {
  1544. const howMany = position.parent.maxOffset;
  1545. const operation = new UnwrapOperation( position, howMany, new Position( doc.graveyard, [ 0 ] ), doc.version );
  1546. model.applyOperation( operation );
  1547. }
  1548. function remove( sourcePosition, howMany ) {
  1549. const targetPosition = Position.createAt( doc.graveyard, doc.graveyard.maxOffset );
  1550. const operation = new MoveOperation( sourcePosition, howMany, targetPosition, doc.version );
  1551. model.applyOperation( operation );
  1552. }
  1553. function move( sourcePosition, howMany, targetPosition ) {
  1554. const operation = new MoveOperation( sourcePosition, howMany, targetPosition, doc.version );
  1555. model.applyOperation( operation );
  1556. }
  1557. function rename( element, newName ) {
  1558. const operation = new RenameOperation( Position.createBefore( element ), element.name, newName, doc.version );
  1559. model.applyOperation( operation );
  1560. }
  1561. function attribute( range, key, oldValue, newValue ) {
  1562. const operation = new AttributeOperation( range, key, oldValue, newValue, doc.version );
  1563. model.applyOperation( operation );
  1564. }
  1565. function expectChanges( expected, includeChangesInGraveyard = false ) {
  1566. const changes = differ.getChanges( { includeChangesInGraveyard } );
  1567. expect( changes.length ).to.equal( expected.length );
  1568. for ( let i = 0; i < expected.length; i++ ) {
  1569. for ( const key in expected[ i ] ) {
  1570. if ( expected[ i ].hasOwnProperty( key ) ) {
  1571. if ( key == 'position' || key == 'range' ) {
  1572. expect( changes[ i ][ key ].isEqual( expected[ i ][ key ] ), `item ${ i }, key "${ key }"` ).to.be.true;
  1573. } else {
  1574. expect( changes[ i ][ key ], `item ${ i }, key "${ key }"` ).to.equal( expected[ i ][ key ] );
  1575. }
  1576. }
  1577. }
  1578. }
  1579. }
  1580. } );