differ.js 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  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. import { wrapInDelta } from '../../tests/model/_utils/utils';
  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._appendChildren( [
  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 = Range.createFromParentsAndOffsets( root, 0, root.getChild( 0 ), 0 );
  136. model.change( () => {
  137. insert( text, position );
  138. attribute( range, 'align', null, 'center' );
  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, 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._appendChildren( 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.getChild( 0 ), 0 );
  560. model.change( () => {
  561. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  562. expectChanges( [
  563. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  564. ] );
  565. } );
  566. } );
  567. it( 'on an element - only one of many attributes changes', () => {
  568. const range = Range.createFromParentsAndOffsets( root, 0, root.getChild( 0 ), 0 );
  569. model.change( () => {
  570. // Set an attribute on an element. It won't change afterwards.
  571. attribute( range, 'otherAttr', null, true );
  572. } );
  573. model.change( () => {
  574. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  575. expectChanges( [
  576. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  577. ] );
  578. } );
  579. } );
  580. it( 'on a character', () => {
  581. const parent = root.getChild( 1 );
  582. const range = Range.createFromParentsAndOffsets( parent, 1, parent, 2 );
  583. model.change( () => {
  584. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  585. expectChanges( [
  586. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  587. ] );
  588. } );
  589. } );
  590. it( 'on a character - case with same characters next to each other', () => {
  591. const parent = root.getChild( 0 );
  592. const range = Range.createFromParentsAndOffsets( parent, 1, parent, 2 );
  593. model.change( () => {
  594. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  595. expectChanges( [
  596. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  597. ] );
  598. } );
  599. } );
  600. it( 'on multiple characters', () => {
  601. const parent = root.getChild( 0 );
  602. const range = Range.createFromParentsAndOffsets( parent, 0, parent, 3 );
  603. model.change( () => {
  604. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  605. expectChanges( [
  606. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  607. ] );
  608. } );
  609. } );
  610. it( 'on multiple consecutive characters in multiple operations', () => {
  611. const parent = root.getChild( 0 );
  612. const range1 = Range.createFromParentsAndOffsets( parent, 1, parent, 2 );
  613. const range2 = Range.createFromParentsAndOffsets( parent, 2, parent, 3 );
  614. model.change( () => {
  615. attribute( range1, attributeKey, attributeOldValue, attributeNewValue );
  616. attribute( range2, attributeKey, attributeOldValue, attributeNewValue );
  617. const range = Range.createFromParentsAndOffsets( parent, 1, parent, 3 );
  618. expectChanges( [
  619. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  620. ] );
  621. } );
  622. } );
  623. it( 'on multiple non-consecutive characters in multiple operations', () => {
  624. const parent = root.getChild( 0 );
  625. const range1 = Range.createFromParentsAndOffsets( parent, 0, parent, 1 );
  626. const range2 = Range.createFromParentsAndOffsets( parent, 2, parent, 3 );
  627. model.change( () => {
  628. // Note "reversed" order of ranges. Further range is changed first.
  629. attribute( range2, attributeKey, attributeOldValue, attributeNewValue );
  630. attribute( range1, attributeKey, attributeOldValue, attributeNewValue );
  631. // Note that changes has been sorted.
  632. expectChanges( [
  633. { type: 'attribute', range: range1, attributeKey, attributeOldValue, attributeNewValue },
  634. { type: 'attribute', range: range2, attributeKey, attributeOldValue, attributeNewValue }
  635. ] );
  636. } );
  637. } );
  638. it( 'on range containing various nodes', () => {
  639. const range = Range.createFromParentsAndOffsets( root, 0, root, 2 );
  640. model.change( () => {
  641. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  642. const p1 = root.getChild( 0 );
  643. const p2 = root.getChild( 1 );
  644. const type = 'attribute';
  645. expectChanges( [
  646. {
  647. type,
  648. range: Range.createFromParentsAndOffsets( root, 0, p1, 0 ),
  649. attributeKey,
  650. attributeOldValue,
  651. attributeNewValue
  652. },
  653. {
  654. type,
  655. range: Range.createFromParentsAndOffsets( p1, 0, p1, 3 ),
  656. attributeKey,
  657. attributeOldValue,
  658. attributeNewValue
  659. },
  660. {
  661. type,
  662. range: Range.createFromParentsAndOffsets( root, 1, p2, 0 ),
  663. attributeKey,
  664. attributeOldValue,
  665. attributeNewValue
  666. },
  667. {
  668. type,
  669. range: Range.createFromParentsAndOffsets( p2, 0, p2, 3 ),
  670. attributeKey,
  671. attributeOldValue,
  672. attributeNewValue
  673. }
  674. ] );
  675. } );
  676. } );
  677. it( 'remove and add attribute on text', () => {
  678. const p = root.getChild( 1 );
  679. p.getChild( 0 )._setAttribute( 'bold', true );
  680. const range = Range.createFromParentsAndOffsets( p, 1, p, 3 );
  681. model.change( () => {
  682. attribute( range, 'bold', true, null );
  683. attribute( range, 'italic', null, true );
  684. const range1 = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  685. const range2 = Range.createFromParentsAndOffsets( p, 2, p, 3 );
  686. // Attribute change glueing does not work 100% correct.
  687. expectChanges( [
  688. {
  689. type: 'attribute',
  690. range: range1,
  691. attributeKey: 'bold',
  692. attributeOldValue: true,
  693. attributeNewValue: null
  694. },
  695. {
  696. type: 'attribute',
  697. range: range1,
  698. attributeKey: 'italic',
  699. attributeOldValue: null,
  700. attributeNewValue: true
  701. },
  702. {
  703. type: 'attribute',
  704. range: range2,
  705. attributeKey: 'bold',
  706. attributeOldValue: true,
  707. attributeNewValue: null
  708. },
  709. {
  710. type: 'attribute',
  711. range: range2,
  712. attributeKey: 'italic',
  713. attributeOldValue: null,
  714. attributeNewValue: true
  715. }
  716. ] );
  717. } );
  718. } );
  719. it( 'on some old nodes and inserted nodes', () => {
  720. const position = new Position( root, [ 0, 1 ] );
  721. const p1 = root.getChild( 0 );
  722. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 2 );
  723. model.change( () => {
  724. insert( new Text( 'xx' ), position );
  725. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  726. const rangeBefore = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  727. expectChanges( [
  728. { type: 'attribute', range: rangeBefore, attributeKey, attributeOldValue, attributeNewValue },
  729. { type: 'insert', name: '$text', length: 2, position }
  730. ] );
  731. } );
  732. } );
  733. it( 'only on inserted nodes', () => {
  734. const position = new Position( root, [ 0, 1 ] );
  735. const p1 = root.getChild( 0 );
  736. const range = Range.createFromParentsAndOffsets( p1, 2, p1, 3 );
  737. model.change( () => {
  738. insert( new Text( 'xxx' ), position );
  739. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  740. expectChanges( [
  741. { type: 'insert', name: '$text', length: 3, position }
  742. ] );
  743. } );
  744. } );
  745. it( 'on some inserted nodes and old nodes', () => {
  746. const position = new Position( root, [ 0, 1 ] );
  747. const p1 = root.getChild( 0 );
  748. const range = Range.createFromParentsAndOffsets( p1, 2, p1, 4 );
  749. model.change( () => {
  750. insert( new Text( 'xx' ), position );
  751. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  752. const rangeAfter = Range.createFromParentsAndOffsets( p1, 3, p1, 4 );
  753. expectChanges( [
  754. { type: 'insert', name: '$text', length: 2, position },
  755. { type: 'attribute', range: rangeAfter, attributeKey, attributeOldValue, attributeNewValue }
  756. ] );
  757. } );
  758. } );
  759. it( 'over all inserted nodes and some old nodes', () => {
  760. const position = new Position( root, [ 0, 1 ] );
  761. const p1 = root.getChild( 0 );
  762. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 4 );
  763. model.change( () => {
  764. insert( new Text( 'xx' ), position );
  765. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  766. const rangeBefore = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  767. const rangeAfter = Range.createFromParentsAndOffsets( p1, 3, p1, 4 );
  768. expectChanges( [
  769. { type: 'attribute', range: rangeBefore, attributeKey, attributeOldValue, attributeNewValue },
  770. { type: 'insert', name: '$text', length: 2, position },
  771. { type: 'attribute', range: rangeAfter, attributeKey, attributeOldValue, attributeNewValue }
  772. ] );
  773. } );
  774. } );
  775. it( 'on some not changed and some changed nodes', () => {
  776. const p = root.getChild( 0 );
  777. const rangeA = Range.createFromParentsAndOffsets( p, 1, p, 3 );
  778. const rangeB = Range.createFromParentsAndOffsets( p, 0, p, 2 );
  779. model.change( () => {
  780. attribute( rangeA, 'a', null, true );
  781. attribute( rangeB, 'b', null, true );
  782. const type = 'attribute';
  783. const attributeOldValue = null;
  784. const attributeNewValue = true;
  785. // Attribute change glueing does not work 100% correct.
  786. expectChanges( [
  787. {
  788. type,
  789. range: Range.createFromParentsAndOffsets( p, 0, p, 1 ),
  790. attributeKey: 'b',
  791. attributeOldValue,
  792. attributeNewValue
  793. },
  794. {
  795. type,
  796. range: Range.createFromParentsAndOffsets( p, 1, p, 2 ),
  797. attributeKey: 'a',
  798. attributeOldValue,
  799. attributeNewValue
  800. },
  801. {
  802. type,
  803. range: Range.createFromParentsAndOffsets( p, 1, p, 2 ),
  804. attributeKey: 'b',
  805. attributeOldValue,
  806. attributeNewValue
  807. },
  808. {
  809. type,
  810. range: Range.createFromParentsAndOffsets( p, 2, p, 3 ),
  811. attributeKey: 'a',
  812. attributeOldValue,
  813. attributeNewValue
  814. }
  815. ] );
  816. } );
  817. } );
  818. it( 'on already changed nodes', () => {
  819. const p = root.getChild( 1 );
  820. const rangeA = Range.createFromParentsAndOffsets( p, 0, p, 3 );
  821. const rangeB = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  822. model.change( () => {
  823. attribute( rangeA, 'a', null, true );
  824. attribute( rangeB, 'b', null, true );
  825. const type = 'attribute';
  826. const attributeOldValue = null;
  827. const attributeNewValue = true;
  828. // Attribute change glueing does not work 100% correct.
  829. expectChanges( [
  830. {
  831. type,
  832. range: Range.createFromParentsAndOffsets( p, 0, p, 2 ),
  833. attributeKey: 'a',
  834. attributeOldValue,
  835. attributeNewValue
  836. },
  837. {
  838. type,
  839. range: Range.createFromParentsAndOffsets( p, 1, p, 2 ),
  840. attributeKey: 'b',
  841. attributeOldValue,
  842. attributeNewValue
  843. },
  844. {
  845. type,
  846. range: Range.createFromParentsAndOffsets( p, 2, p, 3 ),
  847. attributeKey: 'a',
  848. attributeOldValue,
  849. attributeNewValue
  850. }
  851. ] );
  852. } );
  853. } );
  854. it( 'on some changed and some not changed nodes', () => {
  855. const p = root.getChild( 1 );
  856. const rangeA = Range.createFromParentsAndOffsets( p, 0, p, 2 );
  857. const rangeB = Range.createFromParentsAndOffsets( p, 1, p, 3 );
  858. model.change( () => {
  859. attribute( rangeA, 'a', null, true );
  860. attribute( rangeB, 'b', null, true );
  861. const type = 'attribute';
  862. const attributeOldValue = null;
  863. const attributeNewValue = true;
  864. expectChanges( [
  865. {
  866. type,
  867. range: Range.createFromParentsAndOffsets( p, 0, p, 2 ),
  868. attributeKey: 'a',
  869. attributeOldValue,
  870. attributeNewValue
  871. },
  872. {
  873. type,
  874. range: Range.createFromParentsAndOffsets( p, 1, p, 3 ),
  875. attributeKey: 'b',
  876. attributeOldValue,
  877. attributeNewValue
  878. }
  879. ] );
  880. } );
  881. } );
  882. it( 'over all changed nodes and some not changed nodes', () => {
  883. const p = root.getChild( 0 );
  884. const rangeA = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  885. const rangeB = Range.createFromParentsAndOffsets( p, 0, p, 3 );
  886. model.change( () => {
  887. attribute( rangeA, 'a', null, true );
  888. attribute( rangeB, 'b', null, true );
  889. const type = 'attribute';
  890. const attributeOldValue = null;
  891. const attributeNewValue = true;
  892. // Attribute change glueing does not work 100% correct.
  893. expectChanges( [
  894. {
  895. type,
  896. range: Range.createFromParentsAndOffsets( p, 0, p, 1 ),
  897. attributeKey: 'b',
  898. attributeOldValue,
  899. attributeNewValue
  900. },
  901. {
  902. type,
  903. range: Range.createFromParentsAndOffsets( p, 1, p, 2 ),
  904. attributeKey: 'a',
  905. attributeOldValue,
  906. attributeNewValue
  907. },
  908. {
  909. type,
  910. range: Range.createFromParentsAndOffsets( p, 1, p, 3 ),
  911. attributeKey: 'b',
  912. attributeOldValue,
  913. attributeNewValue
  914. }
  915. ] );
  916. } );
  917. } );
  918. } );
  919. describe( 'markers', () => {
  920. let range, rangeB;
  921. beforeEach( () => {
  922. range = Range.createFromParentsAndOffsets( root, 0, root, 1 );
  923. rangeB = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  924. } );
  925. it( 'add marker', () => {
  926. differ.bufferMarkerChange( 'name', null, range );
  927. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  928. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  929. { name: 'name', range }
  930. ] );
  931. } );
  932. it( 'remove marker', () => {
  933. differ.bufferMarkerChange( 'name', range, null );
  934. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  935. { name: 'name', range }
  936. ] );
  937. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  938. } );
  939. it( 'change marker', () => {
  940. differ.bufferMarkerChange( 'name', range, rangeB );
  941. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  942. { name: 'name', range }
  943. ] );
  944. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  945. { name: 'name', range: rangeB }
  946. ] );
  947. } );
  948. it( 'add marker and remove it', () => {
  949. differ.bufferMarkerChange( 'name', null, range );
  950. differ.bufferMarkerChange( 'name', range, null );
  951. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  952. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  953. } );
  954. it( 'add marker and change it', () => {
  955. differ.bufferMarkerChange( 'name', null, range );
  956. differ.bufferMarkerChange( 'name', range, rangeB );
  957. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  958. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  959. { name: 'name', range: rangeB }
  960. ] );
  961. } );
  962. it( 'change marker and remove it', () => {
  963. differ.bufferMarkerChange( 'name', range, rangeB );
  964. differ.bufferMarkerChange( 'name', rangeB, null );
  965. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  966. { name: 'name', range }
  967. ] );
  968. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  969. } );
  970. it( 'remove marker and add it at same range', () => {
  971. differ.bufferMarkerChange( 'name', range, null );
  972. differ.bufferMarkerChange( 'name', null, range );
  973. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  974. { name: 'name', range }
  975. ] );
  976. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  977. { name: 'name', range }
  978. ] );
  979. } );
  980. it( 'change marker to the same range', () => {
  981. differ.bufferMarkerChange( 'name', range, range );
  982. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  983. { name: 'name', range }
  984. ] );
  985. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  986. { name: 'name', range }
  987. ] );
  988. } );
  989. } );
  990. describe( 'other cases', () => {
  991. // #1309.
  992. it( 'multiple inserts and removes in one element', () => {
  993. model.change( () => {
  994. insert( new Text( 'x' ), new Position( root, [ 0, 2 ] ) );
  995. insert( new Text( 'x' ), new Position( root, [ 0, 3 ] ) );
  996. move( new Position( root, [ 0, 2 ] ), 1, new Position( root, [ 1, 0 ] ) );
  997. expectChanges( [
  998. { type: 'insert', name: '$text', length: 1, position: new Position( root, [ 0, 2 ] ) },
  999. { type: 'insert', name: '$text', length: 1, position: new Position( root, [ 1, 0 ] ) }
  1000. ] );
  1001. } );
  1002. } );
  1003. // ckeditor5#733.
  1004. it( 'proper filtering of changes in removed elements', () => {
  1005. // Before fix there was a buggy scenario described in ckeditor5#733.
  1006. // There was this structure: `<paragraph>foo[</paragraph><image /><blockQuote><p>te]xt</p></blockQuote>`
  1007. // On delete of above selection `image` and `paragraph` inside `blockQuote` are removed (it gets merged).
  1008. // However, since `image` was removed first, when checking if `paragraph` is in a removed element,
  1009. // it appeared that `blockQuote` looks like it is removed because it had the same path as the already removed `<image>`.
  1010. // In a result, removing `paragraph` was discarded.
  1011. // The mistake was that the checking for removing was done at incorrect moment.
  1012. root._removeChildren( 0, root.childCount );
  1013. root._appendChildren( [
  1014. new Element( 'paragraph', null, new Text( 'foo' ) ),
  1015. new Element( 'image' ),
  1016. new Element( 'blockQuote', null, [
  1017. new Element( 'paragraph', null, new Text( 'text' ) )
  1018. ] )
  1019. ] );
  1020. model.change( () => {
  1021. // Remove `"te"`.
  1022. remove( new Position( root, [ 2, 0, 0 ] ), 2 );
  1023. // Remove `image` before `blockQuote`.
  1024. remove( new Position( root, [ 1 ] ), 1 );
  1025. // Move `"xt"` to first `paragraph` (merging).
  1026. move( new Position( root, [ 1, 0, 0 ] ), 2, new Position( root, [ 0, 3 ] ) );
  1027. // Remove `paragraph` inside `blockQuote`.
  1028. remove( new Position( root, [ 1, 0 ] ), 1 );
  1029. expectChanges( [
  1030. { type: 'insert', name: '$text', length: 2, position: new Position( root, [ 0, 3 ] ) },
  1031. { type: 'remove', name: 'image', length: 1, position: new Position( root, [ 1 ] ) },
  1032. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1, 0 ] ) }
  1033. ] );
  1034. } );
  1035. } );
  1036. // In this scenario we create a new element, then remove something from before it to mess up with offsets,
  1037. // finally we insert some content into a new element. Since we are inserting into a new element, the
  1038. // inserted children should not be shown on changes list.
  1039. it( 'proper filtering of changes in inserted elements', () => {
  1040. root._removeChildren( 0, root.childCount );
  1041. root._appendChildren( new Element( 'image' ) );
  1042. const blockQuote = new Element( 'blockQuote', null, new Element( 'paragraph' ) );
  1043. model.change( () => {
  1044. // Insert `blockQuote` with `paragraph` after `image`.
  1045. insert( blockQuote, new Position( root, [ 1 ] ) );
  1046. // Remove `image` from before `blockQuote`.
  1047. remove( new Position( root, [ 0 ] ), 1 );
  1048. // Insert text into `paragraph` in `blockQuote`.
  1049. insert( new Text( 'foo' ), new Position( root, [ 0, 0, 0 ] ) );
  1050. expectChanges( [
  1051. { type: 'remove', name: 'image', length: 1, position: new Position( root, [ 0 ] ) },
  1052. { type: 'insert', name: 'blockQuote', length: 1, position: new Position( root, [ 0 ] ) }
  1053. ] );
  1054. } );
  1055. } );
  1056. // In this scenario we create a new element, then move another element that was before the new element into
  1057. // the new element. This way we mess up with offsets and insert content into a new element in one operation.
  1058. // Since we are inserting into a new element, the insertion of moved element should not be shown on changes list.
  1059. it( 'proper filtering of changes in inserted elements #2', () => {
  1060. root._removeChildren( 0, root.childCount );
  1061. root._appendChildren( new Element( 'image' ) );
  1062. model.change( () => {
  1063. // Insert `div` after `image`.
  1064. insert( new Element( 'div' ), new Position( root, [ 1 ] ) );
  1065. // Move `image` to the new `div`.
  1066. move( new Position( root, [ 0 ] ), 1, new Position( root, [ 1, 0 ] ) );
  1067. expectChanges( [
  1068. { type: 'remove', name: 'image', length: 1, position: new Position( root, [ 0 ] ) },
  1069. { type: 'insert', name: 'div', length: 1, position: new Position( root, [ 0 ] ) }
  1070. ] );
  1071. } );
  1072. } );
  1073. } );
  1074. describe( 'getChanges()', () => {
  1075. let position, p1, rangeAttrChange, range;
  1076. beforeEach( () => {
  1077. position = new Position( root, [ 0, 1 ] );
  1078. p1 = root.getChild( 0 );
  1079. range = Range.createFromParentsAndOffsets( p1, 2, p1, 4 );
  1080. rangeAttrChange = Range.createFromParentsAndOffsets( p1, 3, p1, 4 );
  1081. } );
  1082. it( 'should return changes in graveyard if a flag was set up', () => {
  1083. const removePosition = new Position( root, [ 1 ] );
  1084. model.change( () => {
  1085. insert( new Text( 'xx' ), position );
  1086. attribute( range, 'key', null, 'foo' );
  1087. remove( removePosition, 1 );
  1088. expectChanges( [
  1089. { type: 'insert', name: 'paragraph', length: 1, position: new Position( doc.graveyard, [ 0 ] ) },
  1090. { type: 'insert', name: '$text', length: 2, position },
  1091. {
  1092. type: 'attribute',
  1093. range: rangeAttrChange,
  1094. attributeKey: 'key',
  1095. attributeOldValue: null,
  1096. attributeNewValue: 'foo'
  1097. },
  1098. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  1099. ], true );
  1100. } );
  1101. } );
  1102. // Below tests test caching.
  1103. it( 'should return same change set if was called twice in a row', () => {
  1104. model.change( () => {
  1105. insert( new Text( 'xx' ), position );
  1106. attribute( range, 'key', null, 'foo' );
  1107. const changesA = differ.getChanges();
  1108. const changesB = differ.getChanges();
  1109. expect( changesA ).to.deep.equal( changesB );
  1110. } );
  1111. } );
  1112. it( 'should return same change set if was called twice in a row - graveyard changes', () => {
  1113. model.change( () => {
  1114. insert( new Text( 'xx' ), position );
  1115. attribute( range, 'key', null, 'foo' );
  1116. const removePosition = new Position( root, [ 1 ] );
  1117. remove( removePosition, 1 );
  1118. const changesA = differ.getChanges( { includeChangesInGraveyard: true } );
  1119. const changesB = differ.getChanges( { includeChangesInGraveyard: true } );
  1120. expect( changesA ).to.deep.equal( changesB );
  1121. } );
  1122. } );
  1123. it( 'should return correct changes if change happened between getChanges() calls', () => {
  1124. model.change( () => {
  1125. insert( new Text( 'xx' ), position );
  1126. attribute( range, 'key', null, 'foo' );
  1127. expectChanges( [
  1128. { type: 'insert', name: '$text', length: 2, position },
  1129. {
  1130. type: 'attribute',
  1131. range: rangeAttrChange,
  1132. attributeKey: 'key',
  1133. attributeOldValue: null,
  1134. attributeNewValue: 'foo'
  1135. }
  1136. ] );
  1137. const removePosition = new Position( root, [ 1 ] );
  1138. remove( removePosition, 1 );
  1139. expectChanges( [
  1140. { type: 'insert', name: '$text', length: 2, position },
  1141. {
  1142. type: 'attribute',
  1143. range: rangeAttrChange,
  1144. attributeKey: 'key',
  1145. attributeOldValue: null,
  1146. attributeNewValue: 'foo'
  1147. },
  1148. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  1149. ] );
  1150. } );
  1151. } );
  1152. it( 'should return correct changes if reset happened between getChanges() calls', () => {
  1153. model.change( () => {
  1154. insert( new Text( 'xx' ), position );
  1155. attribute( range, 'key', null, 'foo' );
  1156. expectChanges( [
  1157. { type: 'insert', name: '$text', length: 2, position },
  1158. {
  1159. type: 'attribute',
  1160. range: rangeAttrChange,
  1161. attributeKey: 'key',
  1162. attributeOldValue: null,
  1163. attributeNewValue: 'foo'
  1164. }
  1165. ] );
  1166. differ.reset();
  1167. const removePosition = new Position( root, [ 1 ] );
  1168. remove( removePosition, 1 );
  1169. expectChanges( [
  1170. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  1171. ] );
  1172. } );
  1173. } );
  1174. } );
  1175. function insert( item, position ) {
  1176. const operation = new InsertOperation( position, item, doc.version );
  1177. model.applyOperation( wrapInDelta( operation ) );
  1178. }
  1179. function remove( sourcePosition, howMany ) {
  1180. const targetPosition = Position.createAt( doc.graveyard, doc.graveyard.maxOffset );
  1181. const operation = new RemoveOperation( sourcePosition, howMany, targetPosition, doc.version );
  1182. model.applyOperation( wrapInDelta( operation ) );
  1183. }
  1184. function move( sourcePosition, howMany, targetPosition ) {
  1185. const operation = new MoveOperation( sourcePosition, howMany, targetPosition, doc.version );
  1186. model.applyOperation( wrapInDelta( operation ) );
  1187. }
  1188. function rename( element, newName ) {
  1189. const operation = new RenameOperation( Position.createBefore( element ), element.name, newName, doc.version );
  1190. model.applyOperation( wrapInDelta( operation ) );
  1191. }
  1192. function attribute( range, key, oldValue, newValue ) {
  1193. const operation = new AttributeOperation( range, key, oldValue, newValue, doc.version );
  1194. model.applyOperation( wrapInDelta( operation ) );
  1195. }
  1196. function expectChanges( expected, includeChangesInGraveyard = false ) {
  1197. const changes = differ.getChanges( { includeChangesInGraveyard } );
  1198. expect( changes.length ).to.equal( expected.length );
  1199. for ( let i = 0; i < expected.length; i++ ) {
  1200. for ( const key in expected[ i ] ) {
  1201. if ( expected[ i ].hasOwnProperty( key ) ) {
  1202. if ( key == 'position' || key == 'range' ) {
  1203. expect( changes[ i ][ key ].isEqual( expected[ i ][ key ] ), `item ${ i }, key "${ key }"` ).to.be.true;
  1204. } else {
  1205. expect( changes[ i ][ key ], `item ${ i }, key "${ key }"` ).to.equal( expected[ i ][ key ] );
  1206. }
  1207. }
  1208. }
  1209. }
  1210. }
  1211. } );