differ.js 43 KB

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