8
0

differ.js 49 KB

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