8
0

differ.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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 an element - only one of many attributes changes', () => {
  405. const range = Range.createFromParentsAndOffsets( root, 0, root.getChild( 0 ), 0 );
  406. // Set an attribute on an element. It won't change afterwards.
  407. attribute( range, 'otherAttr', null, true );
  408. // "Flush" differ.
  409. differ.getChanges();
  410. differ.reset();
  411. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  412. expectChanges( [
  413. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  414. ] );
  415. } );
  416. it( 'on a character', () => {
  417. const parent = root.getChild( 1 );
  418. const range = Range.createFromParentsAndOffsets( parent, 1, parent, 2 );
  419. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  420. expectChanges( [
  421. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  422. ] );
  423. } );
  424. it( 'on a character - case with same characters next to each other', () => {
  425. const parent = root.getChild( 0 );
  426. const range = Range.createFromParentsAndOffsets( parent, 1, parent, 2 );
  427. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  428. expectChanges( [
  429. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  430. ] );
  431. } );
  432. it( 'on multiple characters', () => {
  433. const parent = root.getChild( 0 );
  434. const range = Range.createFromParentsAndOffsets( parent, 0, parent, 3 );
  435. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  436. expectChanges( [
  437. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  438. ] );
  439. } );
  440. it( 'on multiple consecutive characters in multiple operations', () => {
  441. const parent = root.getChild( 0 );
  442. const range1 = Range.createFromParentsAndOffsets( parent, 1, parent, 2 );
  443. const range2 = Range.createFromParentsAndOffsets( parent, 2, parent, 3 );
  444. attribute( range1, attributeKey, attributeOldValue, attributeNewValue );
  445. attribute( range2, attributeKey, attributeOldValue, attributeNewValue );
  446. const range = Range.createFromParentsAndOffsets( parent, 1, parent, 3 );
  447. expectChanges( [
  448. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  449. ] );
  450. } );
  451. it( 'on multiple non-consecutive characters in multiple operations', () => {
  452. const parent = root.getChild( 0 );
  453. const range1 = Range.createFromParentsAndOffsets( parent, 0, parent, 1 );
  454. const range2 = Range.createFromParentsAndOffsets( parent, 2, parent, 3 );
  455. // Note "reversed" order of ranges. Further range is changed first.
  456. attribute( range2, attributeKey, attributeOldValue, attributeNewValue );
  457. attribute( range1, attributeKey, attributeOldValue, attributeNewValue );
  458. // Note that changes has been sorted.
  459. expectChanges( [
  460. { type: 'attribute', range: range1, attributeKey, attributeOldValue, attributeNewValue },
  461. { type: 'attribute', range: range2, attributeKey, attributeOldValue, attributeNewValue }
  462. ] );
  463. } );
  464. it( 'on range containing various nodes', () => {
  465. const range = Range.createFromParentsAndOffsets( root, 0, root, 2 );
  466. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  467. const p1 = root.getChild( 0 );
  468. const p2 = root.getChild( 1 );
  469. const type = 'attribute';
  470. expectChanges( [
  471. { type, range: Range.createFromParentsAndOffsets( root, 0, p1, 0 ), attributeKey, attributeOldValue, attributeNewValue },
  472. { type, range: Range.createFromParentsAndOffsets( p1, 0, p1, 3 ), attributeKey, attributeOldValue, attributeNewValue },
  473. { type, range: Range.createFromParentsAndOffsets( root, 1, p2, 0 ), attributeKey, attributeOldValue, attributeNewValue },
  474. { type, range: Range.createFromParentsAndOffsets( p2, 0, p2, 3 ), attributeKey, attributeOldValue, attributeNewValue }
  475. ] );
  476. } );
  477. it( 'remove and add attribute on text', () => {
  478. const p = root.getChild( 1 );
  479. p.getChild( 0 ).setAttribute( 'bold', true );
  480. const range = Range.createFromParentsAndOffsets( p, 1, p, 3 );
  481. attribute( range, 'bold', true, null );
  482. attribute( range, 'italic', null, true );
  483. const range1 = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  484. const range2 = Range.createFromParentsAndOffsets( p, 2, p, 3 );
  485. // Attribute change glueing does not work 100% correct.
  486. expectChanges( [
  487. { type: 'attribute', range: range1, attributeKey: 'bold', attributeOldValue: true, attributeNewValue: null },
  488. { type: 'attribute', range: range1, attributeKey: 'italic', attributeOldValue: null, attributeNewValue: true },
  489. { type: 'attribute', range: range2, attributeKey: 'bold', attributeOldValue: true, attributeNewValue: null },
  490. { type: 'attribute', range: range2, attributeKey: 'italic', attributeOldValue: null, attributeNewValue: true }
  491. ] );
  492. } );
  493. it( 'on some old nodes and inserted nodes', () => {
  494. const position = new Position( root, [ 0, 1 ] );
  495. const p1 = root.getChild( 0 );
  496. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 2 );
  497. insert( new Text( 'xx' ), position );
  498. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  499. const rangeBefore = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  500. expectChanges( [
  501. { type: 'attribute', range: rangeBefore, attributeKey, attributeOldValue, attributeNewValue },
  502. { type: 'insert', name: '$text', length: 2, position }
  503. ] );
  504. } );
  505. it( 'only on inserted nodes', () => {
  506. const position = new Position( root, [ 0, 1 ] );
  507. const p1 = root.getChild( 0 );
  508. const range = Range.createFromParentsAndOffsets( p1, 2, p1, 3 );
  509. insert( new Text( 'xxx' ), position );
  510. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  511. expectChanges( [
  512. { type: 'insert', name: '$text', length: 3, position }
  513. ] );
  514. } );
  515. it( 'on some inserted nodes and old nodes', () => {
  516. const position = new Position( root, [ 0, 1 ] );
  517. const p1 = root.getChild( 0 );
  518. const range = Range.createFromParentsAndOffsets( p1, 2, p1, 4 );
  519. insert( new Text( 'xx' ), position );
  520. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  521. const rangeAfter = Range.createFromParentsAndOffsets( p1, 3, p1, 4 );
  522. expectChanges( [
  523. { type: 'insert', name: '$text', length: 2, position },
  524. { type: 'attribute', range: rangeAfter, attributeKey, attributeOldValue, attributeNewValue }
  525. ] );
  526. } );
  527. it( 'over all inserted nodes and some old nodes', () => {
  528. const position = new Position( root, [ 0, 1 ] );
  529. const p1 = root.getChild( 0 );
  530. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 4 );
  531. insert( new Text( 'xx' ), position );
  532. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  533. const rangeBefore = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  534. const rangeAfter = Range.createFromParentsAndOffsets( p1, 3, p1, 4 );
  535. expectChanges( [
  536. { type: 'attribute', range: rangeBefore, attributeKey, attributeOldValue, attributeNewValue },
  537. { type: 'insert', name: '$text', length: 2, position },
  538. { type: 'attribute', range: rangeAfter, attributeKey, attributeOldValue, attributeNewValue }
  539. ] );
  540. } );
  541. it( 'on some not changed and some changed nodes', () => {
  542. const p = root.getChild( 0 );
  543. const rangeA = Range.createFromParentsAndOffsets( p, 1, p, 3 );
  544. const rangeB = Range.createFromParentsAndOffsets( p, 0, p, 2 );
  545. attribute( rangeA, 'a', null, true );
  546. attribute( rangeB, 'b', null, true );
  547. const type = 'attribute';
  548. const attributeOldValue = null;
  549. const attributeNewValue = true;
  550. // Attribute change glueing does not work 100% correct.
  551. expectChanges( [
  552. { type, range: Range.createFromParentsAndOffsets( p, 0, p, 1 ), attributeKey: 'b', attributeOldValue, attributeNewValue },
  553. { type, range: Range.createFromParentsAndOffsets( p, 1, p, 2 ), attributeKey: 'a', attributeOldValue, attributeNewValue },
  554. { type, range: Range.createFromParentsAndOffsets( p, 1, p, 2 ), attributeKey: 'b', attributeOldValue, attributeNewValue },
  555. { type, range: Range.createFromParentsAndOffsets( p, 2, p, 3 ), attributeKey: 'a', attributeOldValue, attributeNewValue }
  556. ] );
  557. } );
  558. it( 'on already changed nodes', () => {
  559. const p = root.getChild( 1 );
  560. const rangeA = Range.createFromParentsAndOffsets( p, 0, p, 3 );
  561. const rangeB = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  562. attribute( rangeA, 'a', null, true );
  563. attribute( rangeB, 'b', null, true );
  564. const type = 'attribute';
  565. const attributeOldValue = null;
  566. const attributeNewValue = true;
  567. // Attribute change glueing does not work 100% correct.
  568. expectChanges( [
  569. { type, range: Range.createFromParentsAndOffsets( p, 0, p, 2 ), attributeKey: 'a', attributeOldValue, attributeNewValue },
  570. { type, range: Range.createFromParentsAndOffsets( p, 1, p, 2 ), attributeKey: 'b', attributeOldValue, attributeNewValue },
  571. { type, range: Range.createFromParentsAndOffsets( p, 2, p, 3 ), attributeKey: 'a', attributeOldValue, attributeNewValue }
  572. ] );
  573. } );
  574. it( 'on some changed and some not changed nodes', () => {
  575. const p = root.getChild( 1 );
  576. const rangeA = Range.createFromParentsAndOffsets( p, 0, p, 2 );
  577. const rangeB = Range.createFromParentsAndOffsets( p, 1, p, 3 );
  578. attribute( rangeA, 'a', null, true );
  579. attribute( rangeB, 'b', null, true );
  580. const type = 'attribute';
  581. const attributeOldValue = null;
  582. const attributeNewValue = true;
  583. expectChanges( [
  584. { type, range: Range.createFromParentsAndOffsets( p, 0, p, 2 ), attributeKey: 'a', attributeOldValue, attributeNewValue },
  585. { type, range: Range.createFromParentsAndOffsets( p, 1, p, 3 ), attributeKey: 'b', attributeOldValue, attributeNewValue },
  586. ] );
  587. } );
  588. it( 'over all changed nodes and some not changed nodes', () => {
  589. const p = root.getChild( 0 );
  590. const rangeA = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  591. const rangeB = Range.createFromParentsAndOffsets( p, 0, p, 3 );
  592. attribute( rangeA, 'a', null, true );
  593. attribute( rangeB, 'b', null, true );
  594. const type = 'attribute';
  595. const attributeOldValue = null;
  596. const attributeNewValue = true;
  597. // Attribute change glueing does not work 100% correct.
  598. expectChanges( [
  599. { type, range: Range.createFromParentsAndOffsets( p, 0, p, 1 ), attributeKey: 'b', attributeOldValue, attributeNewValue },
  600. { type, range: Range.createFromParentsAndOffsets( p, 1, p, 2 ), attributeKey: 'a', attributeOldValue, attributeNewValue },
  601. { type, range: Range.createFromParentsAndOffsets( p, 1, p, 3 ), attributeKey: 'b', attributeOldValue, attributeNewValue },
  602. ] );
  603. } );
  604. } );
  605. describe( 'markers', () => {
  606. let range, rangeB;
  607. beforeEach( () => {
  608. range = Range.createFromParentsAndOffsets( root, 0, root, 1 );
  609. rangeB = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  610. } );
  611. it( 'add marker', () => {
  612. differ.bufferMarkerChange( 'name', null, range );
  613. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  614. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  615. { name: 'name', range }
  616. ] );
  617. } );
  618. it( 'remove marker', () => {
  619. differ.bufferMarkerChange( 'name', range, null );
  620. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  621. { name: 'name', range }
  622. ] );
  623. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  624. } );
  625. it( 'change marker', () => {
  626. differ.bufferMarkerChange( 'name', range, rangeB );
  627. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  628. { name: 'name', range }
  629. ] );
  630. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  631. { name: 'name', range: rangeB }
  632. ] );
  633. } );
  634. it( 'add marker and remove it', () => {
  635. differ.bufferMarkerChange( 'name', null, range );
  636. differ.bufferMarkerChange( 'name', range, null );
  637. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  638. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  639. } );
  640. it( 'add marker and change it', () => {
  641. differ.bufferMarkerChange( 'name', null, range );
  642. differ.bufferMarkerChange( 'name', range, rangeB );
  643. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  644. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  645. { name: 'name', range: rangeB }
  646. ] );
  647. } );
  648. it( 'change marker and remove it', () => {
  649. differ.bufferMarkerChange( 'name', range, rangeB );
  650. differ.bufferMarkerChange( 'name', rangeB, null );
  651. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  652. { name: 'name', range }
  653. ] );
  654. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  655. } );
  656. it( 'remove marker and add it at same range', () => {
  657. differ.bufferMarkerChange( 'name', range, null );
  658. differ.bufferMarkerChange( 'name', null, range );
  659. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  660. { name: 'name', range }
  661. ] );
  662. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  663. { name: 'name', range }
  664. ] );
  665. } );
  666. it( 'change marker to the same range', () => {
  667. differ.bufferMarkerChange( 'name', range, range );
  668. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  669. { name: 'name', range }
  670. ] );
  671. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  672. { name: 'name', range }
  673. ] );
  674. } );
  675. } );
  676. describe( 'getChanges()', () => {
  677. let position, p1, rangeAttrChange;
  678. beforeEach( () => {
  679. position = new Position( root, [ 0, 1 ] );
  680. p1 = root.getChild( 0 );
  681. const range = Range.createFromParentsAndOffsets( p1, 2, p1, 4 );
  682. rangeAttrChange = Range.createFromParentsAndOffsets( p1, 3, p1, 4 );
  683. insert( new Text( 'xx' ), position );
  684. attribute( range, 'key', null, 'foo' );
  685. } );
  686. it( 'should return changes in graveyard if a flag was set up', () => {
  687. const removePosition = new Position( root, [ 1 ] );
  688. remove( removePosition, 1 );
  689. expectChanges( [
  690. { type: 'insert', name: 'paragraph', length: 1, position: new Position( doc.graveyard, [ 0 ] ) },
  691. { type: 'insert', name: '$text', length: 2, position },
  692. { type: 'attribute', range: rangeAttrChange, attributeKey: 'key', attributeOldValue: null, attributeNewValue: 'foo' },
  693. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  694. ], true );
  695. } );
  696. // Below tests test caching.
  697. it( 'should return same change set if was called twice in a row', () => {
  698. const changesA = differ.getChanges();
  699. const changesB = differ.getChanges();
  700. expect( changesA ).to.deep.equal( changesB );
  701. } );
  702. it( 'should return same change set if was called twice in a row - graveyard changes', () => {
  703. const removePosition = new Position( root, [ 1 ] );
  704. remove( removePosition, 1 );
  705. const changesA = differ.getChanges( { includeChangesInGraveyard: true } );
  706. const changesB = differ.getChanges( { includeChangesInGraveyard: true } );
  707. expect( changesA ).to.deep.equal( changesB );
  708. } );
  709. it( 'should return correct changes if change happened between getChanges() calls', () => {
  710. expectChanges( [
  711. { type: 'insert', name: '$text', length: 2, position },
  712. { type: 'attribute', range: rangeAttrChange, attributeKey: 'key', attributeOldValue: null, attributeNewValue: 'foo' }
  713. ] );
  714. const removePosition = new Position( root, [ 1 ] );
  715. remove( removePosition, 1 );
  716. expectChanges( [
  717. { type: 'insert', name: '$text', length: 2, position },
  718. { type: 'attribute', range: rangeAttrChange, attributeKey: 'key', attributeOldValue: null, attributeNewValue: 'foo' },
  719. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  720. ] );
  721. } );
  722. it( 'should return correct changes if reset happened between getChanges() calls', () => {
  723. expectChanges( [
  724. { type: 'insert', name: '$text', length: 2, position },
  725. { type: 'attribute', range: rangeAttrChange, attributeKey: 'key', attributeOldValue: null, attributeNewValue: 'foo' }
  726. ] );
  727. differ.reset();
  728. const removePosition = new Position( root, [ 1 ] );
  729. remove( removePosition, 1 );
  730. expectChanges( [
  731. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  732. ] );
  733. } );
  734. } );
  735. function insert( item, position ) {
  736. const operation = new InsertOperation( position, item, doc.version );
  737. differ.bufferOperation( operation );
  738. model.applyOperation( wrapInDelta( operation ) );
  739. }
  740. function remove( sourcePosition, howMany ) {
  741. const targetPosition = Position.createAt( doc.graveyard, doc.graveyard.maxOffset );
  742. const operation = new RemoveOperation( sourcePosition, howMany, targetPosition, doc.version );
  743. differ.bufferOperation( operation );
  744. model.applyOperation( wrapInDelta( operation ) );
  745. }
  746. function move( sourcePosition, howMany, targetPosition ) {
  747. const operation = new MoveOperation( sourcePosition, howMany, targetPosition, doc.version );
  748. differ.bufferOperation( operation );
  749. model.applyOperation( wrapInDelta( operation ) );
  750. }
  751. function rename( element, newName ) {
  752. const operation = new RenameOperation( Position.createBefore( element ), element.name, newName, doc.version );
  753. differ.bufferOperation( operation );
  754. model.applyOperation( wrapInDelta( operation ) );
  755. }
  756. function attribute( range, key, oldValue, newValue ) {
  757. const operation = new AttributeOperation( range, key, oldValue, newValue, doc.version );
  758. differ.bufferOperation( operation );
  759. model.applyOperation( wrapInDelta( operation ) );
  760. }
  761. function expectChanges( expected, includeChangesInGraveyard = false ) {
  762. const changes = differ.getChanges( { includeChangesInGraveyard } );
  763. for ( let i = 0; i < expected.length; i++ ) {
  764. for ( const key in expected[ i ] ) {
  765. if ( expected[ i ].hasOwnProperty( key ) ) {
  766. if ( key == 'position' || key == 'range' ) {
  767. expect( changes[ i ][ key ].isEqual( expected[ i ][ key ] ), `item ${ i }, key "${ key }"` ).to.be.true;
  768. } else {
  769. expect( changes[ i ][ key ], `item ${ i }, key "${ key }"` ).to.equal( expected[ i ][ key ] );
  770. }
  771. }
  772. }
  773. }
  774. }
  775. } );