differ.js 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879
  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. } );
  508. describe( 'rename', () => {
  509. it( 'an element', () => {
  510. model.change( () => {
  511. rename( root.getChild( 1 ), 'listItem' );
  512. expectChanges( [
  513. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) },
  514. { type: 'insert', name: 'listItem', length: 1, position: new Position( root, [ 1 ] ) }
  515. ] );
  516. } );
  517. } );
  518. it( 'inside a new element', () => {
  519. // Since the rename is inside a new element, it should not be listed on changes list.
  520. model.change( () => {
  521. insert( new Element( 'blockQuote', null, new Element( 'paragraph' ) ), new Position( root, [ 2 ] ) );
  522. rename( root.getChild( 2 ).getChild( 0 ), 'listItem' );
  523. expectChanges( [
  524. { type: 'insert', name: 'blockQuote', length: 1, position: new Position( root, [ 2 ] ) }
  525. ] );
  526. } );
  527. } );
  528. it( 'markers refreshing', () => {
  529. // Since rename is "translated" to remove+insert pair, we need to refresh all the markers that
  530. // intersect with the renamed element.
  531. model.change( () => {
  532. // Renamed element contains marker.
  533. model.markers._set( 'markerA', new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) );
  534. // Marker contains renamed element.
  535. model.markers._set( 'markerB', new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ) );
  536. // Intersecting.
  537. model.markers._set( 'markerC', new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 2 ] ) ) );
  538. // Not intersecting.
  539. model.markers._set( 'markerD', new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 1 ] ) ) );
  540. } );
  541. const markersToRefresh = [ 'markerA', 'markerB', 'markerC' ];
  542. model.change( () => {
  543. rename( root.getChild( 1 ), 'listItem' );
  544. expectChanges( [
  545. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) },
  546. { type: 'insert', name: 'listItem', length: 1, position: new Position( root, [ 1 ] ) }
  547. ] );
  548. const markersToRemove = differ.getMarkersToRemove().map( entry => entry.name );
  549. const markersToAdd = differ.getMarkersToAdd().map( entry => entry.name );
  550. expect( markersToRefresh ).to.deep.equal( markersToRemove );
  551. expect( markersToRefresh ).to.deep.equal( markersToAdd );
  552. } );
  553. } );
  554. } );
  555. describe( 'attribute', () => {
  556. const attributeKey = 'key';
  557. const attributeOldValue = null;
  558. const attributeNewValue = 'foo';
  559. it( 'on an element', () => {
  560. const range = new Range( Position._createAt( root, 0 ), Position._createAt( root, 1 ) );
  561. model.change( () => {
  562. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  563. const diffRange = new Range( Position._createAt( root, 0 ), Position._createAt( root.getChild( 0 ), 0 ) );
  564. expectChanges( [
  565. { type: 'attribute', range: diffRange, attributeKey, attributeOldValue, attributeNewValue }
  566. ] );
  567. } );
  568. } );
  569. it( 'on an element - only one of many attributes changes', () => {
  570. const range = new Range( Position._createAt( root, 0 ), Position._createAt( root, 1 ) );
  571. model.change( () => {
  572. // Set an attribute on an element. It won't change afterwards.
  573. attribute( range, 'otherAttr', null, true );
  574. } );
  575. model.change( () => {
  576. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  577. const diffRange = new Range( Position._createAt( root, 0 ), Position._createAt( root.getChild( 0 ), 0 ) );
  578. expectChanges( [
  579. { type: 'attribute', range: diffRange, attributeKey, attributeOldValue, attributeNewValue }
  580. ] );
  581. } );
  582. } );
  583. it( 'on a character', () => {
  584. const parent = root.getChild( 1 );
  585. const range = new Range( Position._createAt( parent, 1 ), Position._createAt( parent, 2 ) );
  586. model.change( () => {
  587. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  588. expectChanges( [
  589. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  590. ] );
  591. } );
  592. } );
  593. it( 'on a character - case with same characters next to each other', () => {
  594. const parent = root.getChild( 0 );
  595. const range = new Range( Position._createAt( parent, 1 ), Position._createAt( parent, 2 ) );
  596. model.change( () => {
  597. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  598. expectChanges( [
  599. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  600. ] );
  601. } );
  602. } );
  603. it( 'on multiple characters', () => {
  604. const parent = root.getChild( 0 );
  605. const range = new Range( Position._createAt( parent, 0 ), Position._createAt( parent, 3 ) );
  606. model.change( () => {
  607. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  608. expectChanges( [
  609. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  610. ] );
  611. } );
  612. } );
  613. it( 'on multiple consecutive characters in multiple operations', () => {
  614. const parent = root.getChild( 0 );
  615. const range1 = new Range( Position._createAt( parent, 1 ), Position._createAt( parent, 2 ) );
  616. const range2 = new Range( Position._createAt( parent, 2 ), Position._createAt( parent, 3 ) );
  617. model.change( () => {
  618. attribute( range1, attributeKey, attributeOldValue, attributeNewValue );
  619. attribute( range2, attributeKey, attributeOldValue, attributeNewValue );
  620. const range = new Range( Position._createAt( parent, 1 ), Position._createAt( parent, 3 ) );
  621. expectChanges( [
  622. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  623. ] );
  624. } );
  625. } );
  626. it( 'attribute changes intersecting #1', () => {
  627. const parent = root.getChild( 1 );
  628. // Be aware that you cannot make an intersecting changes with the same attribute key,
  629. // cause the value would be incorrect for the common part of the ranges.
  630. const ranges = [
  631. [ 0, 2, null, true, 'foo' ],
  632. [ 1, 3, null, true, 'bar' ]
  633. ];
  634. model.change( () => {
  635. for ( const item of ranges ) {
  636. const range = new Range( Position._createAt( parent, item[ 0 ] ), Position._createAt( parent, item[ 1 ] ) );
  637. attribute( range, item[ 4 ], item[ 2 ], item[ 3 ] );
  638. }
  639. expectChanges( [
  640. {
  641. type: 'attribute',
  642. range: new Range( Position._createAt( parent, 0 ), Position._createAt( parent, 2 ) ),
  643. attributeKey: 'foo',
  644. attributeOldValue: null,
  645. attributeNewValue: true
  646. },
  647. {
  648. type: 'attribute',
  649. range: new Range( Position._createAt( parent, 1 ), Position._createAt( parent, 3 ) ),
  650. attributeKey: 'bar',
  651. attributeOldValue: null,
  652. attributeNewValue: true
  653. }
  654. ] );
  655. } );
  656. } );
  657. it( 'attribute changes intersecting #2', () => {
  658. const parent = root.getChild( 1 );
  659. // Be aware that you cannot make an intersecting changes with the same attribute key,
  660. // cause the value would be incorrect for the common part of the ranges.
  661. const ranges = [
  662. [ 1, 3, null, true, 'foo' ],
  663. [ 0, 2, null, true, 'bar' ]
  664. ];
  665. model.change( () => {
  666. for ( const item of ranges ) {
  667. const range = new Range( Position._createAt( parent, item[ 0 ] ), Position._createAt( parent, item[ 1 ] ) );
  668. attribute( range, item[ 4 ], item[ 2 ], item[ 3 ] );
  669. }
  670. expectChanges( [
  671. {
  672. type: 'attribute',
  673. range: new Range( Position._createAt( parent, 0 ), Position._createAt( parent, 1 ) ),
  674. attributeKey: 'bar',
  675. attributeOldValue: null,
  676. attributeNewValue: true
  677. },
  678. {
  679. type: 'attribute',
  680. range: new Range( Position._createAt( parent, 1 ), Position._createAt( parent, 2 ) ),
  681. attributeKey: 'foo',
  682. attributeOldValue: null,
  683. attributeNewValue: true
  684. },
  685. {
  686. type: 'attribute',
  687. range: new Range( Position._createAt( parent, 1 ), Position._createAt( parent, 2 ) ),
  688. attributeKey: 'bar',
  689. attributeOldValue: null,
  690. attributeNewValue: true
  691. },
  692. {
  693. type: 'attribute',
  694. range: new Range( Position._createAt( parent, 2 ), Position._createAt( parent, 3 ) ),
  695. attributeKey: 'foo',
  696. attributeOldValue: null,
  697. attributeNewValue: true
  698. }
  699. ] );
  700. } );
  701. } );
  702. it( 'attribute changes included in an attribute change #1 - changes are reversed at the end', () => {
  703. const parent = root.getChild( 1 );
  704. const ranges = [
  705. [ 0, 1, null, true ],
  706. [ 1, 2, null, true ],
  707. [ 0, 2, true, null ]
  708. ];
  709. model.change( () => {
  710. for ( const item of ranges ) {
  711. const range = new Range( Position._createAt( parent, item[ 0 ] ), Position._createAt( parent, item[ 1 ] ) );
  712. attribute( range, attributeKey, item[ 2 ], item[ 3 ] );
  713. }
  714. expectChanges( [] );
  715. } );
  716. } );
  717. it( 'attribute changes included in an attribute change #2 - changes are re-applied at the end', () => {
  718. const parent = root.getChild( 1 );
  719. const ranges = [
  720. [ 0, 1, null, true ],
  721. [ 1, 2, null, true ],
  722. [ 0, 2, true, null ],
  723. [ 0, 1, null, true ],
  724. [ 1, 2, null, true ]
  725. ];
  726. model.change( () => {
  727. for ( const item of ranges ) {
  728. const range = new Range( Position._createAt( parent, item[ 0 ] ), Position._createAt( parent, item[ 1 ] ) );
  729. attribute( range, attributeKey, item[ 2 ], item[ 3 ] );
  730. }
  731. expectChanges( [ {
  732. type: 'attribute',
  733. range: new Range( Position._createAt( parent, 0 ), Position._createAt( parent, 2 ) ),
  734. attributeKey,
  735. attributeOldValue: null,
  736. attributeNewValue: true
  737. } ] );
  738. } );
  739. } );
  740. it( 'on multiple non-consecutive characters in multiple operations', () => {
  741. const parent = root.getChild( 0 );
  742. const range1 = new Range( Position._createAt( parent, 0 ), Position._createAt( parent, 1 ) );
  743. const range2 = new Range( Position._createAt( parent, 2 ), Position._createAt( parent, 3 ) );
  744. model.change( () => {
  745. // Note "reversed" order of ranges. Further range is changed first.
  746. attribute( range2, attributeKey, attributeOldValue, attributeNewValue );
  747. attribute( range1, attributeKey, attributeOldValue, attributeNewValue );
  748. // Note that changes has been sorted.
  749. expectChanges( [
  750. { type: 'attribute', range: range1, attributeKey, attributeOldValue, attributeNewValue },
  751. { type: 'attribute', range: range2, attributeKey, attributeOldValue, attributeNewValue }
  752. ] );
  753. } );
  754. } );
  755. it( 'on range containing various nodes', () => {
  756. const range = new Range( Position._createAt( root, 0 ), Position._createAt( root, 2 ) );
  757. model.change( () => {
  758. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  759. const p1 = root.getChild( 0 );
  760. const p2 = root.getChild( 1 );
  761. const type = 'attribute';
  762. expectChanges( [
  763. {
  764. type,
  765. range: new Range( Position._createAt( root, 0 ), Position._createAt( p1, 0 ) ),
  766. attributeKey,
  767. attributeOldValue,
  768. attributeNewValue
  769. },
  770. {
  771. type,
  772. range: new Range( Position._createAt( root, 1 ), Position._createAt( p2, 0 ) ),
  773. attributeKey,
  774. attributeOldValue,
  775. attributeNewValue
  776. }
  777. ] );
  778. } );
  779. } );
  780. it( 'remove and add attribute on text', () => {
  781. const p = root.getChild( 1 );
  782. p.getChild( 0 )._setAttribute( 'bold', true );
  783. const range = new Range( Position._createAt( p, 1 ), Position._createAt( p, 3 ) );
  784. model.change( () => {
  785. attribute( range, 'bold', true, null );
  786. attribute( range, 'italic', null, true );
  787. const range1 = new Range( Position._createAt( p, 1 ), Position._createAt( p, 2 ) );
  788. const range2 = new Range( Position._createAt( p, 2 ), Position._createAt( p, 3 ) );
  789. // Attribute change glueing does not work 100% correct.
  790. expectChanges( [
  791. {
  792. type: 'attribute',
  793. range: range1,
  794. attributeKey: 'bold',
  795. attributeOldValue: true,
  796. attributeNewValue: null
  797. },
  798. {
  799. type: 'attribute',
  800. range: range1,
  801. attributeKey: 'italic',
  802. attributeOldValue: null,
  803. attributeNewValue: true
  804. },
  805. {
  806. type: 'attribute',
  807. range: range2,
  808. attributeKey: 'bold',
  809. attributeOldValue: true,
  810. attributeNewValue: null
  811. },
  812. {
  813. type: 'attribute',
  814. range: range2,
  815. attributeKey: 'italic',
  816. attributeOldValue: null,
  817. attributeNewValue: true
  818. }
  819. ] );
  820. } );
  821. } );
  822. it( 'on some old nodes and inserted nodes', () => {
  823. const position = new Position( root, [ 0, 1 ] );
  824. const p1 = root.getChild( 0 );
  825. const range = new Range( Position._createAt( p1, 0 ), Position._createAt( p1, 2 ) );
  826. model.change( () => {
  827. insert( new Text( 'xx' ), position );
  828. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  829. const rangeBefore = new Range( Position._createAt( p1, 0 ), Position._createAt( p1, 1 ) );
  830. expectChanges( [
  831. { type: 'attribute', range: rangeBefore, attributeKey, attributeOldValue, attributeNewValue },
  832. { type: 'insert', name: '$text', length: 2, position }
  833. ] );
  834. } );
  835. } );
  836. it( 'only on inserted nodes', () => {
  837. const position = new Position( root, [ 0, 1 ] );
  838. const p1 = root.getChild( 0 );
  839. const range = new Range( Position._createAt( p1, 2 ), Position._createAt( p1, 3 ) );
  840. model.change( () => {
  841. insert( new Text( 'xxx' ), position );
  842. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  843. expectChanges( [
  844. { type: 'insert', name: '$text', length: 3, position }
  845. ] );
  846. } );
  847. } );
  848. it( 'on some inserted nodes and old nodes', () => {
  849. const position = new Position( root, [ 0, 1 ] );
  850. const p1 = root.getChild( 0 );
  851. const range = new Range( Position._createAt( p1, 2 ), Position._createAt( p1, 4 ) );
  852. model.change( () => {
  853. insert( new Text( 'xx' ), position );
  854. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  855. const rangeAfter = new Range( Position._createAt( p1, 3 ), Position._createAt( p1, 4 ) );
  856. expectChanges( [
  857. { type: 'insert', name: '$text', length: 2, position },
  858. { type: 'attribute', range: rangeAfter, attributeKey, attributeOldValue, attributeNewValue }
  859. ] );
  860. } );
  861. } );
  862. it( 'over all inserted nodes and some old nodes', () => {
  863. const position = new Position( root, [ 0, 1 ] );
  864. const p1 = root.getChild( 0 );
  865. const range = new Range( Position._createAt( p1, 0 ), Position._createAt( p1, 4 ) );
  866. model.change( () => {
  867. insert( new Text( 'xx' ), position );
  868. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  869. const rangeBefore = new Range( Position._createAt( p1, 0 ), Position._createAt( p1, 1 ) );
  870. const rangeAfter = new Range( Position._createAt( p1, 3 ), Position._createAt( p1, 4 ) );
  871. expectChanges( [
  872. { type: 'attribute', range: rangeBefore, attributeKey, attributeOldValue, attributeNewValue },
  873. { type: 'insert', name: '$text', length: 2, position },
  874. { type: 'attribute', range: rangeAfter, attributeKey, attributeOldValue, attributeNewValue }
  875. ] );
  876. } );
  877. } );
  878. it( 'on some not changed and some changed nodes', () => {
  879. const p = root.getChild( 0 );
  880. const rangeA = new Range( Position._createAt( p, 1 ), Position._createAt( p, 3 ) );
  881. const rangeB = new Range( Position._createAt( p, 0 ), Position._createAt( p, 2 ) );
  882. model.change( () => {
  883. attribute( rangeA, 'a', null, true );
  884. attribute( rangeB, 'b', null, true );
  885. const type = 'attribute';
  886. const attributeOldValue = null;
  887. const attributeNewValue = true;
  888. // Attribute change glueing does not work 100% correct.
  889. expectChanges( [
  890. {
  891. type,
  892. range: new Range( Position._createAt( p, 0 ), Position._createAt( p, 1 ) ),
  893. attributeKey: 'b',
  894. attributeOldValue,
  895. attributeNewValue
  896. },
  897. {
  898. type,
  899. range: new Range( Position._createAt( p, 1 ), Position._createAt( p, 2 ) ),
  900. attributeKey: 'a',
  901. attributeOldValue,
  902. attributeNewValue
  903. },
  904. {
  905. type,
  906. range: new Range( Position._createAt( p, 1 ), Position._createAt( p, 2 ) ),
  907. attributeKey: 'b',
  908. attributeOldValue,
  909. attributeNewValue
  910. },
  911. {
  912. type,
  913. range: new Range( Position._createAt( p, 2 ), Position._createAt( p, 3 ) ),
  914. attributeKey: 'a',
  915. attributeOldValue,
  916. attributeNewValue
  917. }
  918. ] );
  919. } );
  920. } );
  921. it( 'on already changed nodes', () => {
  922. const p = root.getChild( 1 );
  923. const rangeA = new Range( Position._createAt( p, 0 ), Position._createAt( p, 3 ) );
  924. const rangeB = new Range( Position._createAt( p, 1 ), Position._createAt( p, 2 ) );
  925. model.change( () => {
  926. attribute( rangeA, 'a', null, true );
  927. attribute( rangeB, 'b', null, true );
  928. const type = 'attribute';
  929. const attributeOldValue = null;
  930. const attributeNewValue = true;
  931. // Attribute change glueing does not work 100% correct.
  932. expectChanges( [
  933. {
  934. type,
  935. range: new Range( Position._createAt( p, 0 ), Position._createAt( p, 2 ) ),
  936. attributeKey: 'a',
  937. attributeOldValue,
  938. attributeNewValue
  939. },
  940. {
  941. type,
  942. range: new Range( Position._createAt( p, 1 ), Position._createAt( p, 2 ) ),
  943. attributeKey: 'b',
  944. attributeOldValue,
  945. attributeNewValue
  946. },
  947. {
  948. type,
  949. range: new Range( Position._createAt( p, 2 ), Position._createAt( p, 3 ) ),
  950. attributeKey: 'a',
  951. attributeOldValue,
  952. attributeNewValue
  953. }
  954. ] );
  955. } );
  956. } );
  957. it( 'on some changed and some not changed nodes', () => {
  958. const p = root.getChild( 1 );
  959. const rangeA = new Range( Position._createAt( p, 0 ), Position._createAt( p, 2 ) );
  960. const rangeB = new Range( Position._createAt( p, 1 ), Position._createAt( p, 3 ) );
  961. model.change( () => {
  962. attribute( rangeA, 'a', null, true );
  963. attribute( rangeB, 'b', null, true );
  964. const type = 'attribute';
  965. const attributeOldValue = null;
  966. const attributeNewValue = true;
  967. expectChanges( [
  968. {
  969. type,
  970. range: new Range( Position._createAt( p, 0 ), Position._createAt( p, 2 ) ),
  971. attributeKey: 'a',
  972. attributeOldValue,
  973. attributeNewValue
  974. },
  975. {
  976. type,
  977. range: new Range( Position._createAt( p, 1 ), Position._createAt( p, 3 ) ),
  978. attributeKey: 'b',
  979. attributeOldValue,
  980. attributeNewValue
  981. }
  982. ] );
  983. } );
  984. } );
  985. it( 'over all changed nodes and some not changed nodes', () => {
  986. const p = root.getChild( 0 );
  987. const rangeA = new Range( Position._createAt( p, 1 ), Position._createAt( p, 2 ) );
  988. const rangeB = new Range( Position._createAt( p, 0 ), Position._createAt( p, 3 ) );
  989. model.change( () => {
  990. attribute( rangeA, 'a', null, true );
  991. attribute( rangeB, 'b', null, true );
  992. const type = 'attribute';
  993. const attributeOldValue = null;
  994. const attributeNewValue = true;
  995. // Attribute change glueing does not work 100% correct.
  996. expectChanges( [
  997. {
  998. type,
  999. range: new Range( Position._createAt( p, 0 ), Position._createAt( p, 1 ) ),
  1000. attributeKey: 'b',
  1001. attributeOldValue,
  1002. attributeNewValue
  1003. },
  1004. {
  1005. type,
  1006. range: new Range( Position._createAt( p, 1 ), Position._createAt( p, 2 ) ),
  1007. attributeKey: 'a',
  1008. attributeOldValue,
  1009. attributeNewValue
  1010. },
  1011. {
  1012. type,
  1013. range: new Range( Position._createAt( p, 1 ), Position._createAt( p, 3 ) ),
  1014. attributeKey: 'b',
  1015. attributeOldValue,
  1016. attributeNewValue
  1017. }
  1018. ] );
  1019. } );
  1020. } );
  1021. } );
  1022. describe( 'split', () => {
  1023. it( 'split an element', () => {
  1024. const position = new Position( root, [ 0, 2 ] );
  1025. model.change( () => {
  1026. split( position );
  1027. expectChanges( [
  1028. { type: 'remove', name: '$text', length: 1, position },
  1029. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1030. ] );
  1031. } );
  1032. } );
  1033. it( 'split a new element', () => {
  1034. model.change( () => {
  1035. insert(
  1036. new Element( 'paragraph', null, new Text( 'Ab' ) ),
  1037. new Position( root, [ 0 ] )
  1038. );
  1039. split( new Position( root, [ 0, 1 ] ) );
  1040. expectChanges( [
  1041. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  1042. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1043. ] );
  1044. } );
  1045. } );
  1046. it( 'split an element inside a new element', () => {
  1047. model.change( () => {
  1048. insert(
  1049. new Element( 'blockQuote', null, new Element( 'paragraph', null, new Text( 'Ab' ) ) ),
  1050. new Position( root, [ 0 ] )
  1051. );
  1052. split( new Position( root, [ 0, 0, 1 ] ) );
  1053. expectChanges( [
  1054. { type: 'insert', name: 'blockQuote', length: 1, position: new Position( root, [ 0 ] ) }
  1055. ] );
  1056. } );
  1057. } );
  1058. it( 'should correctly mark a change in graveyard', () => {
  1059. model.change( () => {
  1060. merge( new Position( root, [ 1, 0 ] ), new Position( root, [ 0, 3 ] ) );
  1061. } );
  1062. model.change( () => {
  1063. const position = new Position( root, [ 0, 3 ] );
  1064. const operation = new SplitOperation( position, 3, new Position( doc.graveyard, [ 0 ] ), doc.version );
  1065. model.applyOperation( operation );
  1066. expectChanges( [
  1067. { type: 'remove', name: 'paragraph', length: 1, position: new Position( doc.graveyard, [ 0 ] ) },
  1068. { type: 'remove', name: '$text', length: 3, position: new Position( root, [ 0, 3 ] ) },
  1069. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1070. ], true );
  1071. } );
  1072. } );
  1073. } );
  1074. describe( 'merge', () => {
  1075. it( 'merge two elements', () => {
  1076. model.change( () => {
  1077. merge( new Position( root, [ 1, 0 ] ), new Position( root, [ 0, 3 ] ) );
  1078. expectChanges( [
  1079. { type: 'insert', name: '$text', length: 3, position: new Position( root, [ 0, 3 ] ) },
  1080. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1081. ] );
  1082. } );
  1083. } );
  1084. it( 'merge a new element', () => {
  1085. model.change( () => {
  1086. insert( new Element( 'paragraph', null, new Text( 'Ab' ) ), new Position( root, [ 1 ] ) );
  1087. merge( new Position( root, [ 1, 0 ] ), new Position( root, [ 0, 3 ] ) );
  1088. expectChanges( [
  1089. { type: 'insert', name: '$text', length: 2, position: new Position( root, [ 0, 3 ] ) }
  1090. ] );
  1091. } );
  1092. } );
  1093. it( 'merge into a new element', () => {
  1094. model.change( () => {
  1095. insert( new Element( 'paragraph', null, new Text( 'Ab' ) ), new Position( root, [ 0 ] ) );
  1096. merge( new Position( root, [ 1, 0 ] ), new Position( root, [ 0, 2 ] ) );
  1097. expectChanges( [
  1098. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  1099. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1100. ] );
  1101. } );
  1102. } );
  1103. it( 'merge elements inside a new element', () => {
  1104. model.change( () => {
  1105. insert(
  1106. new Element( 'blockQuote', null, [
  1107. new Element( 'paragraph', null, new Text( 'Ab' ) ),
  1108. new Element( 'paragraph', null, new Text( 'Xyz' ) )
  1109. ] ),
  1110. new Position( root, [ 0 ] )
  1111. );
  1112. merge( new Position( root, [ 0, 1, 0 ] ), new Position( root, [ 0, 0, 2 ] ) );
  1113. expectChanges( [
  1114. { type: 'insert', name: 'blockQuote', length: 1, position: new Position( root, [ 0 ] ) }
  1115. ] );
  1116. } );
  1117. } );
  1118. it( 'should correctly mark a change in graveyard', () => {
  1119. model.change( () => {
  1120. merge( new Position( root, [ 1, 0 ] ), new Position( root, [ 0, 3 ] ) );
  1121. expectChanges( [
  1122. { type: 'insert', name: 'paragraph', length: 1, position: new Position( doc.graveyard, [ 0 ] ) },
  1123. { type: 'insert', name: '$text', length: 3, position: new Position( root, [ 0, 3 ] ) },
  1124. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1125. ], true );
  1126. } );
  1127. } );
  1128. } );
  1129. describe( 'markers', () => {
  1130. let range, rangeB;
  1131. beforeEach( () => {
  1132. range = new Range( Position._createAt( root, 0 ), Position._createAt( root, 1 ) );
  1133. rangeB = new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) );
  1134. } );
  1135. it( 'add marker', () => {
  1136. differ.bufferMarkerChange( 'name', null, range, true );
  1137. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  1138. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  1139. { name: 'name', range }
  1140. ] );
  1141. expect( differ.getChangedMarkers() ).to.deep.equal( [
  1142. {
  1143. name: 'name',
  1144. data: {
  1145. oldRange: null,
  1146. newRange: range
  1147. }
  1148. }
  1149. ] );
  1150. } );
  1151. it( 'remove marker', () => {
  1152. differ.bufferMarkerChange( 'name', range, null, true );
  1153. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  1154. { name: 'name', range }
  1155. ] );
  1156. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  1157. expect( differ.getChangedMarkers() ).to.deep.equal( [
  1158. {
  1159. name: 'name',
  1160. data: {
  1161. oldRange: range,
  1162. newRange: null
  1163. }
  1164. }
  1165. ] );
  1166. } );
  1167. it( 'change marker\'s range', () => {
  1168. differ.bufferMarkerChange( 'name', range, rangeB, true );
  1169. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  1170. { name: 'name', range }
  1171. ] );
  1172. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  1173. { name: 'name', range: rangeB }
  1174. ] );
  1175. expect( differ.getChangedMarkers() ).to.deep.equal( [
  1176. {
  1177. name: 'name',
  1178. data: {
  1179. oldRange: range,
  1180. newRange: rangeB
  1181. }
  1182. }
  1183. ] );
  1184. } );
  1185. it( 'add marker not affecting data', () => {
  1186. differ.bufferMarkerChange( 'name', range, rangeB, false );
  1187. expect( differ.hasDataChanges() ).to.be.false;
  1188. } );
  1189. it( 'add marker affecting data', () => {
  1190. differ.bufferMarkerChange( 'name', range, rangeB, true );
  1191. expect( differ.hasDataChanges() ).to.be.true;
  1192. } );
  1193. it( 'add marker and remove it', () => {
  1194. differ.bufferMarkerChange( 'name', null, range, true );
  1195. differ.bufferMarkerChange( 'name', range, null, true );
  1196. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  1197. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  1198. expect( differ.getChangedMarkers() ).to.deep.equal( [] );
  1199. expect( differ.hasDataChanges() ).to.be.false;
  1200. } );
  1201. it( 'add marker and change it', () => {
  1202. differ.bufferMarkerChange( 'name', null, range, true );
  1203. differ.bufferMarkerChange( 'name', range, rangeB, true );
  1204. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  1205. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  1206. { name: 'name', range: rangeB }
  1207. ] );
  1208. expect( differ.getChangedMarkers() ).to.deep.equal( [
  1209. {
  1210. name: 'name',
  1211. data: {
  1212. oldRange: null,
  1213. newRange: rangeB
  1214. }
  1215. }
  1216. ] );
  1217. } );
  1218. it( 'change marker to not affecting data', () => {
  1219. differ.bufferMarkerChange( 'name', range, rangeB, true );
  1220. differ.bufferMarkerChange( 'name', range, rangeB, false );
  1221. expect( differ.hasDataChanges() ).to.be.false;
  1222. } );
  1223. it( 'change marker and remove it', () => {
  1224. differ.bufferMarkerChange( 'name', range, rangeB, true );
  1225. differ.bufferMarkerChange( 'name', rangeB, null, true );
  1226. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  1227. { name: 'name', range }
  1228. ] );
  1229. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  1230. expect( differ.getChangedMarkers() ).to.deep.equal( [
  1231. {
  1232. name: 'name',
  1233. data: {
  1234. oldRange: range,
  1235. newRange: null
  1236. }
  1237. }
  1238. ] );
  1239. expect( differ.hasDataChanges() ).to.be.true;
  1240. } );
  1241. it( 'remove marker and add it at same range', () => {
  1242. differ.bufferMarkerChange( 'name', range, null, true );
  1243. differ.bufferMarkerChange( 'name', null, range, true );
  1244. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  1245. { name: 'name', range }
  1246. ] );
  1247. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  1248. { name: 'name', range }
  1249. ] );
  1250. expect( differ.getChangedMarkers() ).to.deep.equal( [
  1251. {
  1252. name: 'name',
  1253. data: {
  1254. oldRange: range,
  1255. newRange: range
  1256. }
  1257. }
  1258. ] );
  1259. } );
  1260. it( 'change marker to the same range', () => {
  1261. differ.bufferMarkerChange( 'name', range, range, true );
  1262. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  1263. { name: 'name', range }
  1264. ] );
  1265. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  1266. { name: 'name', range }
  1267. ] );
  1268. expect( differ.getChangedMarkers() ).to.deep.equal( [
  1269. {
  1270. name: 'name',
  1271. data: {
  1272. oldRange: range,
  1273. newRange: range
  1274. }
  1275. }
  1276. ] );
  1277. } );
  1278. } );
  1279. describe( 'other cases', () => {
  1280. // #1309.
  1281. it( 'multiple inserts and removes in one element', () => {
  1282. model.change( () => {
  1283. insert( new Text( 'x' ), new Position( root, [ 0, 2 ] ) );
  1284. insert( new Text( 'x' ), new Position( root, [ 0, 3 ] ) );
  1285. move( new Position( root, [ 0, 2 ] ), 1, new Position( root, [ 1, 0 ] ) );
  1286. expectChanges( [
  1287. { type: 'insert', name: '$text', length: 1, position: new Position( root, [ 0, 2 ] ) },
  1288. { type: 'insert', name: '$text', length: 1, position: new Position( root, [ 1, 0 ] ) }
  1289. ] );
  1290. } );
  1291. } );
  1292. // ckeditor5#733.
  1293. it( 'proper filtering of changes in removed elements', () => {
  1294. // Before fix there was a buggy scenario described in ckeditor5#733.
  1295. // There was this structure: `<paragraph>foo[</paragraph><image /><blockQuote><p>te]xt</p></blockQuote>`
  1296. // On delete of above selection `image` and `paragraph` inside `blockQuote` are removed (it gets merged).
  1297. // However, since `image` was removed first, when checking if `paragraph` is in a removed element,
  1298. // it appeared that `blockQuote` looks like it is removed because it had the same path as the already removed `<image>`.
  1299. // In a result, removing `paragraph` was discarded.
  1300. // The mistake was that the checking for removing was done at incorrect moment.
  1301. root._removeChildren( 0, root.childCount );
  1302. root._appendChild( [
  1303. new Element( 'paragraph', null, new Text( 'foo' ) ),
  1304. new Element( 'image' ),
  1305. new Element( 'blockQuote', null, [
  1306. new Element( 'paragraph', null, new Text( 'text' ) )
  1307. ] )
  1308. ] );
  1309. model.change( () => {
  1310. // Remove `"te"`.
  1311. remove( new Position( root, [ 2, 0, 0 ] ), 2 );
  1312. // Remove `image` before `blockQuote`.
  1313. remove( new Position( root, [ 1 ] ), 1 );
  1314. // Move `"xt"` to first `paragraph` (merging).
  1315. move( new Position( root, [ 1, 0, 0 ] ), 2, new Position( root, [ 0, 3 ] ) );
  1316. // Remove `paragraph` inside `blockQuote`.
  1317. remove( new Position( root, [ 1, 0 ] ), 1 );
  1318. expectChanges( [
  1319. { type: 'insert', name: '$text', length: 2, position: new Position( root, [ 0, 3 ] ) },
  1320. { type: 'remove', name: 'image', length: 1, position: new Position( root, [ 1 ] ) },
  1321. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1, 0 ] ) }
  1322. ] );
  1323. } );
  1324. } );
  1325. // In this scenario we create a new element, then remove something from before it to mess up with offsets,
  1326. // finally we insert some content into a new element. Since we are inserting into a new element, the
  1327. // inserted children should not be shown on changes list.
  1328. it( 'proper filtering of changes in inserted elements', () => {
  1329. root._removeChildren( 0, root.childCount );
  1330. root._appendChild( new Element( 'image' ) );
  1331. const blockQuote = new Element( 'blockQuote', null, new Element( 'paragraph' ) );
  1332. model.change( () => {
  1333. // Insert `blockQuote` with `paragraph` after `image`.
  1334. insert( blockQuote, new Position( root, [ 1 ] ) );
  1335. // Remove `image` from before `blockQuote`.
  1336. remove( new Position( root, [ 0 ] ), 1 );
  1337. // Insert text into `paragraph` in `blockQuote`.
  1338. insert( new Text( 'foo' ), new Position( root, [ 0, 0, 0 ] ) );
  1339. expectChanges( [
  1340. { type: 'remove', name: 'image', length: 1, position: new Position( root, [ 0 ] ) },
  1341. { type: 'insert', name: 'blockQuote', length: 1, position: new Position( root, [ 0 ] ) }
  1342. ] );
  1343. } );
  1344. } );
  1345. // In this scenario we create a new element, then move another element that was before the new element into
  1346. // the new element. This way we mess up with offsets and insert content into a new element in one operation.
  1347. // Since we are inserting into a new element, the insertion of moved element should not be shown on changes list.
  1348. it( 'proper filtering of changes in inserted elements #2', () => {
  1349. root._removeChildren( 0, root.childCount );
  1350. root._appendChild( new Element( 'image' ) );
  1351. model.change( () => {
  1352. // Insert `div` after `image`.
  1353. insert( new Element( 'div' ), new Position( root, [ 1 ] ) );
  1354. // Move `image` to the new `div`.
  1355. move( new Position( root, [ 0 ] ), 1, new Position( root, [ 1, 0 ] ) );
  1356. expectChanges( [
  1357. { type: 'remove', name: 'image', length: 1, position: new Position( root, [ 0 ] ) },
  1358. { type: 'insert', name: 'div', length: 1, position: new Position( root, [ 0 ] ) }
  1359. ] );
  1360. } );
  1361. } );
  1362. // #1392.
  1363. it( 'remove is correctly transformed by multiple affecting changes', () => {
  1364. root._appendChild( new Element( 'paragraph', null, new Text( 'xyz' ) ) );
  1365. model.change( writer => {
  1366. rename( root.getChild( 1 ), 'heading' );
  1367. rename( root.getChild( 2 ), 'heading' );
  1368. remove( writer.createPositionAt( root, 0 ), 3 );
  1369. expectChanges( [
  1370. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  1371. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  1372. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) }
  1373. ] );
  1374. } );
  1375. } );
  1376. } );
  1377. describe( 'getChanges()', () => {
  1378. let position, p1, rangeAttrChange, range;
  1379. beforeEach( () => {
  1380. position = new Position( root, [ 0, 1 ] );
  1381. p1 = root.getChild( 0 );
  1382. range = new Range( Position._createAt( p1, 2 ), Position._createAt( p1, 4 ) );
  1383. rangeAttrChange = new Range( Position._createAt( p1, 3 ), Position._createAt( p1, 4 ) );
  1384. } );
  1385. it( 'should return changes in graveyard if a flag was set up', () => {
  1386. const removePosition = new Position( root, [ 1 ] );
  1387. model.change( () => {
  1388. insert( new Text( 'xx' ), position );
  1389. attribute( range, 'key', null, 'foo' );
  1390. remove( removePosition, 1 );
  1391. expectChanges( [
  1392. { type: 'insert', name: 'paragraph', length: 1, position: new Position( doc.graveyard, [ 0 ] ) },
  1393. { type: 'insert', name: '$text', length: 2, position },
  1394. {
  1395. type: 'attribute',
  1396. range: rangeAttrChange,
  1397. attributeKey: 'key',
  1398. attributeOldValue: null,
  1399. attributeNewValue: 'foo'
  1400. },
  1401. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  1402. ], true );
  1403. } );
  1404. } );
  1405. // Below tests test caching.
  1406. it( 'should return same change set if was called twice in a row', () => {
  1407. model.change( () => {
  1408. insert( new Text( 'xx' ), position );
  1409. attribute( range, 'key', null, 'foo' );
  1410. const changesA = differ.getChanges();
  1411. const changesB = differ.getChanges();
  1412. expect( changesA ).to.deep.equal( changesB );
  1413. } );
  1414. } );
  1415. it( 'should return same change set if was called twice in a row - graveyard changes', () => {
  1416. model.change( () => {
  1417. insert( new Text( 'xx' ), position );
  1418. attribute( range, 'key', null, 'foo' );
  1419. const removePosition = new Position( root, [ 1 ] );
  1420. remove( removePosition, 1 );
  1421. const changesA = differ.getChanges( { includeChangesInGraveyard: true } );
  1422. const changesB = differ.getChanges( { includeChangesInGraveyard: true } );
  1423. expect( changesA ).to.deep.equal( changesB );
  1424. } );
  1425. } );
  1426. it( 'should return correct changes if change happened between getChanges() calls', () => {
  1427. model.change( () => {
  1428. insert( new Text( 'xx' ), position );
  1429. attribute( range, 'key', null, 'foo' );
  1430. expectChanges( [
  1431. { type: 'insert', name: '$text', length: 2, position },
  1432. {
  1433. type: 'attribute',
  1434. range: rangeAttrChange,
  1435. attributeKey: 'key',
  1436. attributeOldValue: null,
  1437. attributeNewValue: 'foo'
  1438. }
  1439. ] );
  1440. const removePosition = new Position( root, [ 1 ] );
  1441. remove( removePosition, 1 );
  1442. expectChanges( [
  1443. { type: 'insert', name: '$text', length: 2, position },
  1444. {
  1445. type: 'attribute',
  1446. range: rangeAttrChange,
  1447. attributeKey: 'key',
  1448. attributeOldValue: null,
  1449. attributeNewValue: 'foo'
  1450. },
  1451. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  1452. ] );
  1453. } );
  1454. } );
  1455. it( 'should return correct changes if reset happened between getChanges() calls', () => {
  1456. model.change( () => {
  1457. insert( new Text( 'xx' ), position );
  1458. attribute( range, 'key', null, 'foo' );
  1459. expectChanges( [
  1460. { type: 'insert', name: '$text', length: 2, position },
  1461. {
  1462. type: 'attribute',
  1463. range: rangeAttrChange,
  1464. attributeKey: 'key',
  1465. attributeOldValue: null,
  1466. attributeNewValue: 'foo'
  1467. }
  1468. ] );
  1469. differ.reset();
  1470. const removePosition = new Position( root, [ 1 ] );
  1471. remove( removePosition, 1 );
  1472. expectChanges( [
  1473. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  1474. ] );
  1475. } );
  1476. } );
  1477. } );
  1478. function insert( item, position ) {
  1479. const operation = new InsertOperation( position, item, doc.version );
  1480. model.applyOperation( operation );
  1481. }
  1482. function split( position ) {
  1483. const howMany = position.parent.maxOffset - position.offset;
  1484. const operation = new SplitOperation( position, howMany, null, doc.version );
  1485. model.applyOperation( operation );
  1486. }
  1487. function merge( source, target ) {
  1488. const howMany = source.parent.maxOffset;
  1489. const operation = new MergeOperation( source, howMany, target, new Position( doc.graveyard, [ 0 ] ), doc.version );
  1490. model.applyOperation( operation );
  1491. }
  1492. function remove( sourcePosition, howMany ) {
  1493. const targetPosition = Position._createAt( doc.graveyard, doc.graveyard.maxOffset );
  1494. const operation = new MoveOperation( sourcePosition, howMany, targetPosition, doc.version );
  1495. model.applyOperation( operation );
  1496. }
  1497. function move( sourcePosition, howMany, targetPosition ) {
  1498. const operation = new MoveOperation( sourcePosition, howMany, targetPosition, doc.version );
  1499. model.applyOperation( operation );
  1500. }
  1501. function rename( element, newName ) {
  1502. const operation = new RenameOperation( Position._createBefore( element ), element.name, newName, doc.version );
  1503. model.applyOperation( operation );
  1504. }
  1505. function attribute( range, key, oldValue, newValue ) {
  1506. const operation = new AttributeOperation( range, key, oldValue, newValue, doc.version );
  1507. model.applyOperation( operation );
  1508. }
  1509. function expectChanges( expected, includeChangesInGraveyard = false ) {
  1510. const changes = differ.getChanges( { includeChangesInGraveyard } );
  1511. expect( changes.length ).to.equal( expected.length );
  1512. for ( let i = 0; i < expected.length; i++ ) {
  1513. for ( const key in expected[ i ] ) {
  1514. if ( expected[ i ].hasOwnProperty( key ) ) {
  1515. if ( key == 'position' || key == 'range' ) {
  1516. expect( changes[ i ][ key ].isEqual( expected[ i ][ key ] ), `item ${ i }, key "${ key }"` ).to.be.true;
  1517. } else {
  1518. expect( changes[ i ][ key ], `item ${ i }, key "${ key }"` ).to.equal( expected[ i ][ key ] );
  1519. }
  1520. }
  1521. }
  1522. }
  1523. }
  1524. } );