differ.js 58 KB

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