differ.js 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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 Document from '../../src/model/document';
  7. import Differ from '../../src/model/differ';
  8. import Element from '../../src/model/element';
  9. import Text from '../../src/model/text';
  10. import Position from '../../src/model/position';
  11. import Range from '../../src/model/range';
  12. import InsertOperation from '../../src/model/operation/insertoperation';
  13. import RemoveOperation from '../../src/model/operation/removeoperation';
  14. import MoveOperation from '../../src/model/operation/moveoperation';
  15. import RenameOperation from '../../src/model/operation/renameoperation';
  16. import AttributeOperation from '../../src/model/operation/attributeoperation';
  17. import { wrapInDelta } from '../../tests/model/_utils/utils';
  18. describe( 'Differ', () => {
  19. let doc, differ, root, model;
  20. beforeEach( () => {
  21. model = new Model();
  22. doc = new Document( model );
  23. differ = new Differ();
  24. root = doc.createRoot();
  25. root.appendChildren( [
  26. new Element( 'paragraph', null, [
  27. new Text( 'foo' )
  28. ] ),
  29. new Element( 'paragraph', null, [
  30. new Text( 'bar' )
  31. ] )
  32. ] );
  33. } );
  34. describe( 'insert', () => {
  35. // Simple.
  36. it( 'an element', () => {
  37. const position = new Position( root, [ 1 ] );
  38. insert( new Element( 'image' ), position );
  39. expectChanges( [
  40. { type: 'insert', name: 'image', length: 1, position }
  41. ] );
  42. } );
  43. it( 'a non-empty element with attributes', () => {
  44. const position = new Position( root, [ 1 ] );
  45. insert(
  46. new Element( 'image', { src: 'foo.jpg' }, new Element( 'caption', null, new Text( 'bar' ) ) ),
  47. position
  48. );
  49. expectChanges( [
  50. { type: 'insert', name: 'image', length: 1, position }
  51. ] );
  52. } );
  53. it( 'multiple elements', () => {
  54. const position = new Position( root, [ 1 ] );
  55. insert( [ new Element( 'image' ), new Element( 'paragraph' ) ], position );
  56. expectChanges( [
  57. { type: 'insert', name: 'image', length: 1, position },
  58. { type: 'insert', name: 'paragraph', length: 1, position: position.getShiftedBy( 1 ) }
  59. ] );
  60. } );
  61. it( 'a character', () => {
  62. const position = new Position( root, [ 0, 2 ] );
  63. insert( new Text( 'x' ), position );
  64. expectChanges( [
  65. { type: 'insert', name: '$text', length: 1, position }
  66. ] );
  67. } );
  68. it( 'multiple characters', () => {
  69. const position = new Position( root, [ 0, 2 ] );
  70. insert( new Text( 'xyz' ), position );
  71. expectChanges( [
  72. { type: 'insert', name: '$text', length: 3, position }
  73. ] );
  74. } );
  75. it( 'multiple consecutive characters in multiple operations', () => {
  76. const position = new Position( root, [ 0, 2 ] );
  77. insert( new Text( 'xy' ), position );
  78. insert( new Text( 'z' ), position.getShiftedBy( 2 ) );
  79. insert( new Text( 'ab' ), position );
  80. expectChanges( [
  81. { type: 'insert', name: '$text', length: 5, position }
  82. ] );
  83. } );
  84. it( 'multiple non-consecutive characters in multiple operations', () => {
  85. const position = new Position( root, [ 0, 0 ] );
  86. insert( new Text( 'xy' ), position );
  87. insert( new Text( 'z' ), position.getShiftedBy( 3 ) );
  88. expectChanges( [
  89. { type: 'insert', name: '$text', length: 2, position },
  90. { type: 'insert', name: '$text', length: 1, position: position.getShiftedBy( 3 ) }
  91. ] );
  92. } );
  93. // Combined.
  94. it( 'node in a new element', () => {
  95. const image = new Element( 'image' );
  96. const position = new Position( root, [ 1 ] );
  97. insert( image, position );
  98. const caption = new Element( 'caption' );
  99. insert( caption, Position.createAt( image, 0 ) );
  100. insert( new Text( 'foo' ), Position.createAt( caption, 0 ) );
  101. expectChanges( [
  102. { type: 'insert', name: 'image', length: 1, position }
  103. ] );
  104. } );
  105. it( 'node in a renamed element', () => {
  106. const text = new Text( 'xyz', { bold: true } );
  107. const position = new Position( root, [ 0, 3 ] );
  108. insert( text, position );
  109. rename( root.getChild( 0 ), 'listItem' );
  110. // Note that since renamed element is removed and then re-inserted, there is no diff for text inserted inside it.
  111. expectChanges( [
  112. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  113. { type: 'insert', name: 'listItem', length: 1, position: new Position( root, [ 0 ] ) }
  114. ] );
  115. } );
  116. it( 'node in a element with changed attribute', () => {
  117. const text = new Text( 'xyz', { bold: true } );
  118. const position = new Position( root, [ 0, 3 ] );
  119. const range = Range.createFromParentsAndOffsets( root, 0, root.getChild( 0 ), 0 );
  120. insert( text, position );
  121. attribute( range, 'align', null, 'center' );
  122. // Compare to scenario above, this time there is only an attribute change on parent element, so there is also a diff for text.
  123. expectChanges( [
  124. { type: 'attribute', range, attributeKey: 'align', attributeOldValue: null, attributeNewValue: 'center' },
  125. { type: 'insert', name: '$text', length: 3, position },
  126. ] );
  127. } );
  128. it( 'nodes between other inserted nodes', () => {
  129. insert( new Text( 'xx' ), new Position( root, [ 0, 1 ] ) );
  130. insert( new Text( 'yy' ), new Position( root, [ 0, 2 ] ) );
  131. expectChanges( [
  132. { type: 'insert', position: new Position( root, [ 0, 1 ] ), length: 4, name: '$text' }
  133. ] );
  134. } );
  135. it( 'nodes before nodes with changed attributes', () => {
  136. const p1 = root.getChild( 0 );
  137. const range = Range.createFromParentsAndOffsets( p1, 1, p1, 3 );
  138. const position = new Position( root, [ 0, 0 ] );
  139. attribute( range, 'bold', null, true );
  140. insert( new Text( 'xx' ), position );
  141. const rangeAfter = Range.createFromParentsAndOffsets( p1, 3, p1, 5 );
  142. expectChanges( [
  143. { type: 'insert', name: '$text', length: 2, position },
  144. { type: 'attribute', range: rangeAfter, attributeKey: 'bold', attributeOldValue: null, attributeNewValue: true }
  145. ] );
  146. } );
  147. it( 'nodes between nodes with changed attributes', () => {
  148. const p1 = root.getChild( 0 );
  149. const range = Range.createFromParentsAndOffsets( p1, 1, p1, 3 );
  150. const position = new Position( root, [ 0, 2 ] );
  151. attribute( range, 'bold', null, true );
  152. insert( new Text( 'xx' ), position );
  153. const rangeBefore = Range.createFromParentsAndOffsets( p1, 1, p1, 2 );
  154. const rangeAfter = Range.createFromParentsAndOffsets( p1, 4, p1, 5 );
  155. expectChanges( [
  156. { type: 'attribute', range: rangeBefore, attributeKey: 'bold', attributeOldValue: null, attributeNewValue: true },
  157. { type: 'insert', name: '$text', length: 2, position },
  158. { type: 'attribute', range: rangeAfter, attributeKey: 'bold', attributeOldValue: null, attributeNewValue: true }
  159. ] );
  160. } );
  161. it( 'nodes after nodes with changed attributes', () => {
  162. const p1 = root.getChild( 0 );
  163. const range = Range.createFromParentsAndOffsets( p1, 1, p1, 3 );
  164. const position = new Position( root, [ 0, 3 ] );
  165. attribute( range, 'bold', null, true );
  166. insert( new Text( 'xx' ), position );
  167. expectChanges( [
  168. { type: 'attribute', range, attributeKey: 'bold', attributeOldValue: null, attributeNewValue: true },
  169. { type: 'insert', name: '$text', length: 2, position }
  170. ] );
  171. } );
  172. } );
  173. describe( 'remove', () => {
  174. it( 'an element', () => {
  175. const position = new Position( root, [ 0 ] );
  176. remove( position, 1 );
  177. expectChanges( [
  178. { type: 'remove', name: 'paragraph', length: 1, position }
  179. ] );
  180. } );
  181. it( 'multiple elements', () => {
  182. const position = new Position( root, [ 0 ] );
  183. remove( position, 2 );
  184. expectChanges( [
  185. { type: 'remove', name: 'paragraph', length: 1, position },
  186. { type: 'remove', name: 'paragraph', length: 1, position }
  187. ] );
  188. } );
  189. it( 'a character', () => {
  190. const position = new Position( root, [ 0, 1 ] );
  191. remove( position, 1 );
  192. expectChanges( [
  193. { type: 'remove', name: '$text', length: 1, position: new Position( root, [ 0, 1 ] ) }
  194. ] );
  195. } );
  196. it( 'multiple characters', () => {
  197. const position = new Position( root, [ 0, 1 ] );
  198. remove( position, 2 );
  199. expectChanges( [
  200. { type: 'remove', name: '$text', length: 2, position }
  201. ] );
  202. } );
  203. it( 'multiple consecutive characters in multiple operations', () => {
  204. const position = new Position( root, [ 0, 0 ] );
  205. remove( position, 1 );
  206. remove( position, 1 );
  207. remove( position, 1 );
  208. expectChanges( [
  209. { type: 'remove', name: '$text', length: 3, position }
  210. ] );
  211. } );
  212. it( 'multiple non-consecutive characters in multiple operations', () => {
  213. const position = new Position( root, [ 0, 0 ] );
  214. remove( position, 1 );
  215. remove( position.getShiftedBy( 1 ), 1 );
  216. expectChanges( [
  217. { type: 'remove', name: '$text', length: 1, position },
  218. { type: 'remove', name: '$text', length: 1, position: position.getShiftedBy( 1 ) }
  219. ] );
  220. } );
  221. it( 'item just before inserted item', () => {
  222. // This tests proper changes sorting.
  223. const insertPosition = new Position( root, [ 0, 2 ] );
  224. const removePosition = new Position( root, [ 0, 1 ] );
  225. insert( new Text( 'x' ), insertPosition );
  226. remove( removePosition, 1 );
  227. expectChanges( [
  228. { type: 'remove', name: '$text', length: 1, position: removePosition },
  229. { type: 'insert', name: '$text', length: 1, position: removePosition }
  230. ] );
  231. } );
  232. it( 'nodes before inserted nodes (together with some inserted nodes)', () => {
  233. const insertPosition = new Position( root, [ 0, 2 ] );
  234. const removePosition = new Position( root, [ 0, 1 ] );
  235. insert( new Text( 'xyz' ), insertPosition );
  236. remove( removePosition, 2 );
  237. expectChanges( [
  238. { type: 'remove', name: '$text', length: 1, position: removePosition },
  239. { type: 'insert', name: '$text', length: 2, position: removePosition }
  240. ] );
  241. } );
  242. it( 'inserted nodes and some nodes after inserted nodes', () => {
  243. const insertPosition = new Position( root, [ 0, 2 ] );
  244. const removePosition = new Position( root, [ 0, 3 ] );
  245. insert( new Text( 'xyz' ), insertPosition );
  246. remove( removePosition, 3 );
  247. expectChanges( [
  248. { type: 'insert', name: '$text', length: 1, position: insertPosition },
  249. { type: 'remove', name: '$text', length: 1, position: removePosition }
  250. ] );
  251. } );
  252. it( 'all inserted nodes', () => {
  253. const insertPosition = new Position( root, [ 0, 2 ] );
  254. const removePosition = new Position( root, [ 0, 1 ] );
  255. insert( new Text( 'xy' ), insertPosition );
  256. remove( removePosition, 4 );
  257. expectChanges( [
  258. { type: 'remove', name: '$text', length: 2, position: removePosition }
  259. ] );
  260. } );
  261. it( 'before removed nodes', () => {
  262. const removePositionA = new Position( root, [ 0, 2 ] );
  263. const removePositionB = new Position( root, [ 0, 0 ] );
  264. remove( removePositionA, 1 );
  265. remove( removePositionB, 1 );
  266. expectChanges( [
  267. { type: 'remove', name: '$text', length: 1, position: removePositionB },
  268. { type: 'remove', name: '$text', length: 1, position: new Position( root, [ 0, 1 ] ) }
  269. ] );
  270. } );
  271. it( 'before and after removed nodes in one operation', () => {
  272. const removePositionA = new Position( root, [ 0, 1 ] );
  273. const removePositionB = new Position( root, [ 0, 0 ] );
  274. remove( removePositionA, 1 );
  275. remove( removePositionB, 2 );
  276. expectChanges( [
  277. { type: 'remove', name: '$text', length: 3, position: removePositionB },
  278. ] );
  279. } );
  280. it( 'before nodes that changed attributes', () => {
  281. const position = new Position( root, [ 0, 0 ] );
  282. const p1 = root.getChild( 0 );
  283. const range = Range.createFromParentsAndOffsets( p1, 2, p1, 3 );
  284. attribute( range, 'bold', null, true );
  285. remove( position, 1 );
  286. const newRange = Range.createFromParentsAndOffsets( p1, 1, p1, 2 );
  287. expectChanges( [
  288. { type: 'remove', name: '$text', length: 1, position },
  289. { type: 'attribute', range: newRange, attributeKey: 'bold', attributeOldValue: null, attributeNewValue: true }
  290. ] );
  291. } );
  292. it( 'before nodes that changed attributes together with some changed nodes', () => {
  293. const position = new Position( root, [ 0, 0 ] );
  294. const p1 = root.getChild( 0 );
  295. const range = Range.createFromParentsAndOffsets( p1, 1, p1, 3 );
  296. attribute( range, 'bold', null, true );
  297. remove( position, 2 );
  298. const newRange = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  299. expectChanges( [
  300. { type: 'remove', name: '$text', length: 2, position },
  301. { type: 'attribute', range: newRange, attributeKey: 'bold', attributeOldValue: null, attributeNewValue: true }
  302. ] );
  303. } );
  304. it( 'some changed nodes', () => {
  305. const position = new Position( root, [ 0, 1 ] );
  306. const p1 = root.getChild( 0 );
  307. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 3 );
  308. attribute( range, 'bold', null, true );
  309. remove( position, 1 );
  310. const rangeBefore = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  311. const rangeAfter = Range.createFromParentsAndOffsets( p1, 1, p1, 2 );
  312. expectChanges( [
  313. { type: 'attribute', range: rangeBefore, attributeKey: 'bold', attributeOldValue: null, attributeNewValue: true },
  314. { type: 'remove', name: '$text', length: 1, position },
  315. { type: 'attribute', range: rangeAfter, attributeKey: 'bold', attributeOldValue: null, attributeNewValue: true }
  316. ] );
  317. } );
  318. it( 'some changed nodes and some nodes after', () => {
  319. const position = new Position( root, [ 0, 1 ] );
  320. const p1 = root.getChild( 0 );
  321. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 2 );
  322. attribute( range, 'bold', null, true );
  323. remove( position, 2 );
  324. const newRange = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  325. expectChanges( [
  326. { type: 'attribute', range: newRange, attributeKey: 'bold', attributeOldValue: null, attributeNewValue: true },
  327. { type: 'remove', name: '$text', length: 2, position }
  328. ] );
  329. } );
  330. it( 'after changed nodes', () => {
  331. const position = new Position( root, [ 0, 2 ] );
  332. const p1 = root.getChild( 0 );
  333. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  334. attribute( range, 'bold', null, true );
  335. remove( position, 1 );
  336. expectChanges( [
  337. { type: 'attribute', range, attributeKey: 'bold', attributeOldValue: null, attributeNewValue: true },
  338. { type: 'remove', name: '$text', length: 1, position }
  339. ] );
  340. } );
  341. } );
  342. // The only main difference between remove operation and move operation is target position.
  343. // In differ, graveyard is treated as other roots. In remove suite, simple cases for move are covered.
  344. // This suite will have only a few cases, focused on things specific to move operation.
  345. describe( 'move', () => {
  346. it( 'an element to the same parent - target position is after source position', () => {
  347. const sourcePosition = new Position( root, [ 0 ] );
  348. const targetPosition = new Position( root, [ 2 ] );
  349. move( sourcePosition, 1, targetPosition );
  350. expectChanges( [
  351. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  352. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  353. ] );
  354. } );
  355. it( 'an element to the same parent - target position is before source position', () => {
  356. const sourcePosition = new Position( root, [ 1 ] );
  357. const targetPosition = new Position( root, [ 0 ] );
  358. move( sourcePosition, 1, targetPosition );
  359. expectChanges( [
  360. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  361. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 2 ] ) }
  362. ] );
  363. } );
  364. it( 'multiple consecutive characters between different roots in multiple operations', () => {
  365. const sourcePosition = new Position( root, [ 0, 1 ] );
  366. const targetPosition = new Position( root, [ 1, 0 ] );
  367. move( sourcePosition, 1, targetPosition );
  368. move( sourcePosition, 1, targetPosition.getShiftedBy( 1 ) );
  369. expectChanges( [
  370. { type: 'remove', name: '$text', length: 2, position: sourcePosition },
  371. { type: 'insert', name: '$text', length: 2, position: targetPosition }
  372. ] );
  373. } );
  374. it( 'reinsert removed element', () => {
  375. doc.graveyard.appendChildren( new Element( 'listItem' ) );
  376. const sourcePosition = new Position( doc.graveyard, [ 0 ] );
  377. const targetPosition = new Position( root, [ 2 ] );
  378. move( sourcePosition, 1, targetPosition );
  379. expectChanges( [
  380. { type: 'insert', name: 'listItem', length: 1, position: targetPosition }
  381. ] );
  382. } );
  383. } );
  384. describe( 'rename', () => {
  385. it( 'an element', () => {
  386. rename( root.getChild( 1 ), 'listItem' );
  387. expectChanges( [
  388. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) },
  389. { type: 'insert', name: 'listItem', length: 1, position: new Position( root, [ 1 ] ) }
  390. ] );
  391. } );
  392. } );
  393. describe( 'attribute', () => {
  394. const attributeKey = 'key';
  395. const attributeOldValue = null;
  396. const attributeNewValue = 'foo';
  397. it( 'on an element', () => {
  398. const range = Range.createFromParentsAndOffsets( root, 0, root.getChild( 0 ), 0 );
  399. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  400. expectChanges( [
  401. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  402. ] );
  403. } );
  404. it( 'on a character', () => {
  405. const parent = root.getChild( 1 );
  406. const range = Range.createFromParentsAndOffsets( parent, 1, parent, 2 );
  407. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  408. expectChanges( [
  409. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  410. ] );
  411. } );
  412. it( 'on a character - case with same characters next to each other', () => {
  413. const parent = root.getChild( 0 );
  414. const range = Range.createFromParentsAndOffsets( parent, 1, parent, 2 );
  415. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  416. expectChanges( [
  417. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  418. ] );
  419. } );
  420. it( 'on multiple characters', () => {
  421. const parent = root.getChild( 0 );
  422. const range = Range.createFromParentsAndOffsets( parent, 0, parent, 3 );
  423. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  424. expectChanges( [
  425. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  426. ] );
  427. } );
  428. it( 'on multiple consecutive characters in multiple operations', () => {
  429. const parent = root.getChild( 0 );
  430. const range1 = Range.createFromParentsAndOffsets( parent, 1, parent, 2 );
  431. const range2 = Range.createFromParentsAndOffsets( parent, 2, parent, 3 );
  432. attribute( range1, attributeKey, attributeOldValue, attributeNewValue );
  433. attribute( range2, attributeKey, attributeOldValue, attributeNewValue );
  434. const range = Range.createFromParentsAndOffsets( parent, 1, parent, 3 );
  435. expectChanges( [
  436. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  437. ] );
  438. } );
  439. it( 'on multiple non-consecutive characters in multiple operations', () => {
  440. const parent = root.getChild( 0 );
  441. const range1 = Range.createFromParentsAndOffsets( parent, 0, parent, 1 );
  442. const range2 = Range.createFromParentsAndOffsets( parent, 2, parent, 3 );
  443. // Note "reversed" order of ranges. Further range is changed first.
  444. attribute( range2, attributeKey, attributeOldValue, attributeNewValue );
  445. attribute( range1, attributeKey, attributeOldValue, attributeNewValue );
  446. // Note that changes has been sorted.
  447. expectChanges( [
  448. { type: 'attribute', range: range1, attributeKey, attributeOldValue, attributeNewValue },
  449. { type: 'attribute', range: range2, attributeKey, attributeOldValue, attributeNewValue }
  450. ] );
  451. } );
  452. it( 'on range containing various nodes', () => {
  453. const range = Range.createFromParentsAndOffsets( root, 0, root, 2 );
  454. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  455. const p1 = root.getChild( 0 );
  456. const p2 = root.getChild( 1 );
  457. const type = 'attribute';
  458. expectChanges( [
  459. { type, range: Range.createFromParentsAndOffsets( root, 0, p1, 0 ), attributeKey, attributeOldValue, attributeNewValue },
  460. { type, range: Range.createFromParentsAndOffsets( p1, 0, p1, 3 ), attributeKey, attributeOldValue, attributeNewValue },
  461. { type, range: Range.createFromParentsAndOffsets( root, 1, p2, 0 ), attributeKey, attributeOldValue, attributeNewValue },
  462. { type, range: Range.createFromParentsAndOffsets( p2, 0, p2, 3 ), attributeKey, attributeOldValue, attributeNewValue }
  463. ] );
  464. } );
  465. it( 'remove and add attribute on text', () => {
  466. const p = root.getChild( 1 );
  467. p.getChild( 0 ).setAttribute( 'bold', true );
  468. const range = Range.createFromParentsAndOffsets( p, 1, p, 3 );
  469. attribute( range, 'bold', true, null );
  470. attribute( range, 'italic', null, true );
  471. const range1 = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  472. const range2 = Range.createFromParentsAndOffsets( p, 2, p, 3 );
  473. // Attribute change glueing does not work 100% correct.
  474. expectChanges( [
  475. { type: 'attribute', range: range1, attributeKey: 'bold', attributeOldValue: true, attributeNewValue: null },
  476. { type: 'attribute', range: range1, attributeKey: 'italic', attributeOldValue: null, attributeNewValue: true },
  477. { type: 'attribute', range: range2, attributeKey: 'bold', attributeOldValue: true, attributeNewValue: null },
  478. { type: 'attribute', range: range2, attributeKey: 'italic', attributeOldValue: null, attributeNewValue: true }
  479. ] );
  480. } );
  481. it( 'on some old nodes and inserted nodes', () => {
  482. const position = new Position( root, [ 0, 1 ] );
  483. const p1 = root.getChild( 0 );
  484. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 2 );
  485. insert( new Text( 'xx' ), position );
  486. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  487. const rangeBefore = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  488. expectChanges( [
  489. { type: 'attribute', range: rangeBefore, attributeKey, attributeOldValue, attributeNewValue },
  490. { type: 'insert', name: '$text', length: 2, position }
  491. ] );
  492. } );
  493. it( 'only on inserted nodes', () => {
  494. const position = new Position( root, [ 0, 1 ] );
  495. const p1 = root.getChild( 0 );
  496. const range = Range.createFromParentsAndOffsets( p1, 2, p1, 3 );
  497. insert( new Text( 'xxx' ), position );
  498. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  499. expectChanges( [
  500. { type: 'insert', name: '$text', length: 3, position }
  501. ] );
  502. } );
  503. it( 'on some inserted nodes and old nodes', () => {
  504. const position = new Position( root, [ 0, 1 ] );
  505. const p1 = root.getChild( 0 );
  506. const range = Range.createFromParentsAndOffsets( p1, 2, p1, 4 );
  507. insert( new Text( 'xx' ), position );
  508. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  509. const rangeAfter = Range.createFromParentsAndOffsets( p1, 3, p1, 4 );
  510. expectChanges( [
  511. { type: 'insert', name: '$text', length: 2, position },
  512. { type: 'attribute', range: rangeAfter, attributeKey, attributeOldValue, attributeNewValue }
  513. ] );
  514. } );
  515. it( 'over all inserted nodes and some old nodes', () => {
  516. const position = new Position( root, [ 0, 1 ] );
  517. const p1 = root.getChild( 0 );
  518. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 4 );
  519. insert( new Text( 'xx' ), position );
  520. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  521. const rangeBefore = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  522. const rangeAfter = Range.createFromParentsAndOffsets( p1, 3, p1, 4 );
  523. expectChanges( [
  524. { type: 'attribute', range: rangeBefore, attributeKey, attributeOldValue, attributeNewValue },
  525. { type: 'insert', name: '$text', length: 2, position },
  526. { type: 'attribute', range: rangeAfter, attributeKey, attributeOldValue, attributeNewValue }
  527. ] );
  528. } );
  529. it( 'on some not changed and some changed nodes', () => {
  530. const p = root.getChild( 0 );
  531. const rangeA = Range.createFromParentsAndOffsets( p, 1, p, 3 );
  532. const rangeB = Range.createFromParentsAndOffsets( p, 0, p, 2 );
  533. attribute( rangeA, 'a', null, true );
  534. attribute( rangeB, 'b', null, true );
  535. const type = 'attribute';
  536. const attributeOldValue = null;
  537. const attributeNewValue = true;
  538. // Attribute change glueing does not work 100% correct.
  539. expectChanges( [
  540. { type, range: Range.createFromParentsAndOffsets( p, 0, p, 1 ), attributeKey: 'b', attributeOldValue, attributeNewValue },
  541. { type, range: Range.createFromParentsAndOffsets( p, 1, p, 2 ), attributeKey: 'a', attributeOldValue, attributeNewValue },
  542. { type, range: Range.createFromParentsAndOffsets( p, 1, p, 2 ), attributeKey: 'b', attributeOldValue, attributeNewValue },
  543. { type, range: Range.createFromParentsAndOffsets( p, 2, p, 3 ), attributeKey: 'a', attributeOldValue, attributeNewValue }
  544. ] );
  545. } );
  546. it( 'on already changed nodes', () => {
  547. const p = root.getChild( 1 );
  548. const rangeA = Range.createFromParentsAndOffsets( p, 0, p, 3 );
  549. const rangeB = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  550. attribute( rangeA, 'a', null, true );
  551. attribute( rangeB, 'b', null, true );
  552. const type = 'attribute';
  553. const attributeOldValue = null;
  554. const attributeNewValue = true;
  555. // Attribute change glueing does not work 100% correct.
  556. expectChanges( [
  557. { type, range: Range.createFromParentsAndOffsets( p, 0, p, 2 ), attributeKey: 'a', attributeOldValue, attributeNewValue },
  558. { type, range: Range.createFromParentsAndOffsets( p, 1, p, 2 ), attributeKey: 'b', attributeOldValue, attributeNewValue },
  559. { type, range: Range.createFromParentsAndOffsets( p, 2, p, 3 ), attributeKey: 'a', attributeOldValue, attributeNewValue }
  560. ] );
  561. } );
  562. it( 'on some changed and some not changed nodes', () => {
  563. const p = root.getChild( 1 );
  564. const rangeA = Range.createFromParentsAndOffsets( p, 0, p, 2 );
  565. const rangeB = Range.createFromParentsAndOffsets( p, 1, p, 3 );
  566. attribute( rangeA, 'a', null, true );
  567. attribute( rangeB, 'b', null, true );
  568. const type = 'attribute';
  569. const attributeOldValue = null;
  570. const attributeNewValue = true;
  571. expectChanges( [
  572. { type, range: Range.createFromParentsAndOffsets( p, 0, p, 2 ), attributeKey: 'a', attributeOldValue, attributeNewValue },
  573. { type, range: Range.createFromParentsAndOffsets( p, 1, p, 3 ), attributeKey: 'b', attributeOldValue, attributeNewValue },
  574. ] );
  575. } );
  576. it( 'over all changed nodes and some not changed nodes', () => {
  577. const p = root.getChild( 0 );
  578. const rangeA = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  579. const rangeB = Range.createFromParentsAndOffsets( p, 0, p, 3 );
  580. attribute( rangeA, 'a', null, true );
  581. attribute( rangeB, 'b', null, true );
  582. const type = 'attribute';
  583. const attributeOldValue = null;
  584. const attributeNewValue = true;
  585. // Attribute change glueing does not work 100% correct.
  586. expectChanges( [
  587. { type, range: Range.createFromParentsAndOffsets( p, 0, p, 1 ), attributeKey: 'b', attributeOldValue, attributeNewValue },
  588. { type, range: Range.createFromParentsAndOffsets( p, 1, p, 2 ), attributeKey: 'a', attributeOldValue, attributeNewValue },
  589. { type, range: Range.createFromParentsAndOffsets( p, 1, p, 3 ), attributeKey: 'b', attributeOldValue, attributeNewValue },
  590. ] );
  591. } );
  592. } );
  593. describe( 'markers', () => {
  594. let range, rangeB;
  595. beforeEach( () => {
  596. range = Range.createFromParentsAndOffsets( root, 0, root, 1 );
  597. rangeB = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  598. } );
  599. it( 'add marker', () => {
  600. differ.bufferMarkerChange( 'name', null, range );
  601. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  602. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  603. { name: 'name', range }
  604. ] );
  605. } );
  606. it( 'remove marker', () => {
  607. differ.bufferMarkerChange( 'name', range, null );
  608. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  609. { name: 'name', range }
  610. ] );
  611. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  612. } );
  613. it( 'change marker', () => {
  614. differ.bufferMarkerChange( 'name', range, rangeB );
  615. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  616. { name: 'name', range }
  617. ] );
  618. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  619. { name: 'name', range: rangeB }
  620. ] );
  621. } );
  622. it( 'add marker and remove it', () => {
  623. differ.bufferMarkerChange( 'name', null, range );
  624. differ.bufferMarkerChange( 'name', range, null );
  625. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  626. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  627. } );
  628. it( 'add marker and change it', () => {
  629. differ.bufferMarkerChange( 'name', null, range );
  630. differ.bufferMarkerChange( 'name', range, rangeB );
  631. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  632. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  633. { name: 'name', range: rangeB }
  634. ] );
  635. } );
  636. it( 'change marker and remove it', () => {
  637. differ.bufferMarkerChange( 'name', range, rangeB );
  638. differ.bufferMarkerChange( 'name', rangeB, null );
  639. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  640. { name: 'name', range }
  641. ] );
  642. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  643. } );
  644. it( 'remove marker and add it at same range', () => {
  645. differ.bufferMarkerChange( 'name', range, null );
  646. differ.bufferMarkerChange( 'name', null, range );
  647. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  648. { name: 'name', range }
  649. ] );
  650. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  651. { name: 'name', range }
  652. ] );
  653. } );
  654. it( 'change marker to the same range', () => {
  655. differ.bufferMarkerChange( 'name', range, range );
  656. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  657. { name: 'name', range }
  658. ] );
  659. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  660. { name: 'name', range }
  661. ] );
  662. } );
  663. } );
  664. describe( 'getChanges()', () => {
  665. let position, p1, rangeAttrChange;
  666. beforeEach( () => {
  667. position = new Position( root, [ 0, 1 ] );
  668. p1 = root.getChild( 0 );
  669. const range = Range.createFromParentsAndOffsets( p1, 2, p1, 4 );
  670. rangeAttrChange = Range.createFromParentsAndOffsets( p1, 3, p1, 4 );
  671. insert( new Text( 'xx' ), position );
  672. attribute( range, 'key', null, 'foo' );
  673. } );
  674. it( 'should return changes in graveyard if a flag was set up', () => {
  675. const removePosition = new Position( root, [ 1 ] );
  676. remove( removePosition, 1 );
  677. expectChanges( [
  678. { type: 'insert', name: 'paragraph', length: 1, position: new Position( doc.graveyard, [ 0 ] ) },
  679. { type: 'insert', name: '$text', length: 2, position },
  680. { type: 'attribute', range: rangeAttrChange, attributeKey: 'key', attributeOldValue: null, attributeNewValue: 'foo' },
  681. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  682. ], true );
  683. } );
  684. // Below tests test caching.
  685. it( 'should return same change set if was called twice in a row', () => {
  686. const changesA = differ.getChanges();
  687. const changesB = differ.getChanges();
  688. expect( changesA ).to.deep.equal( changesB );
  689. } );
  690. it( 'should return same change set if was called twice in a row - graveyard changes', () => {
  691. const removePosition = new Position( root, [ 1 ] );
  692. remove( removePosition, 1 );
  693. const changesA = differ.getChanges( { includeChangesInGraveyard: true } );
  694. const changesB = differ.getChanges( { includeChangesInGraveyard: true } );
  695. expect( changesA ).to.deep.equal( changesB );
  696. } );
  697. it( 'should return correct changes if change happened between getChanges() calls', () => {
  698. expectChanges( [
  699. { type: 'insert', name: '$text', length: 2, position },
  700. { type: 'attribute', range: rangeAttrChange, attributeKey: 'key', attributeOldValue: null, attributeNewValue: 'foo' }
  701. ] );
  702. const removePosition = new Position( root, [ 1 ] );
  703. remove( removePosition, 1 );
  704. expectChanges( [
  705. { type: 'insert', name: '$text', length: 2, position },
  706. { type: 'attribute', range: rangeAttrChange, attributeKey: 'key', attributeOldValue: null, attributeNewValue: 'foo' },
  707. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  708. ] );
  709. } );
  710. it( 'should return correct changes if reset happened between getChanges() calls', () => {
  711. expectChanges( [
  712. { type: 'insert', name: '$text', length: 2, position },
  713. { type: 'attribute', range: rangeAttrChange, attributeKey: 'key', attributeOldValue: null, attributeNewValue: 'foo' }
  714. ] );
  715. differ.reset();
  716. const removePosition = new Position( root, [ 1 ] );
  717. remove( removePosition, 1 );
  718. expectChanges( [
  719. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  720. ] );
  721. } );
  722. } );
  723. function insert( item, position ) {
  724. const operation = new InsertOperation( position, item, doc.version );
  725. differ.bufferOperation( operation );
  726. model.applyOperation( wrapInDelta( operation ) );
  727. }
  728. function remove( sourcePosition, howMany ) {
  729. const targetPosition = Position.createAt( doc.graveyard, doc.graveyard.maxOffset );
  730. const operation = new RemoveOperation( sourcePosition, howMany, targetPosition, doc.version );
  731. differ.bufferOperation( operation );
  732. model.applyOperation( wrapInDelta( operation ) );
  733. }
  734. function move( sourcePosition, howMany, targetPosition ) {
  735. const operation = new MoveOperation( sourcePosition, howMany, targetPosition, doc.version );
  736. differ.bufferOperation( operation );
  737. model.applyOperation( wrapInDelta( operation ) );
  738. }
  739. function rename( element, newName ) {
  740. const operation = new RenameOperation( Position.createBefore( element ), element.name, newName, doc.version );
  741. differ.bufferOperation( operation );
  742. model.applyOperation( wrapInDelta( operation ) );
  743. }
  744. function attribute( range, key, oldValue, newValue ) {
  745. const operation = new AttributeOperation( range, key, oldValue, newValue, doc.version );
  746. differ.bufferOperation( operation );
  747. model.applyOperation( wrapInDelta( operation ) );
  748. }
  749. function expectChanges( expected, includeChangesInGraveyard = false ) {
  750. const changes = differ.getChanges( { includeChangesInGraveyard } );
  751. for ( let i = 0; i < expected.length; i++ ) {
  752. for ( const key in expected[ i ] ) {
  753. if ( expected[ i ].hasOwnProperty( key ) ) {
  754. if ( key == 'position' || key == 'range' ) {
  755. expect( changes[ i ][ key ].isEqual( expected[ i ][ key ] ), `item ${ i }, key "${ key }"` ).to.be.true;
  756. } else {
  757. expect( changes[ i ][ key ], `item ${ i }, key "${ key }"` ).to.equal( expected[ i ][ key ] );
  758. }
  759. }
  760. }
  761. }
  762. }
  763. } );