differ.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. /**
  2. * @license Copyright (c) 2003-2017, 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: 'insert', name: 'paragraph', length: 1, position: Position.createAt( doc.graveyard, 0 ) },
  179. { type: 'remove', name: 'paragraph', length: 1, position }
  180. ] );
  181. } );
  182. it( 'multiple elements', () => {
  183. const position = new Position( root, [ 0 ] );
  184. remove( position, 2 );
  185. expectChanges( [
  186. { type: 'insert', name: 'paragraph', length: 1, position: Position.createAt( doc.graveyard, 0 ) },
  187. { type: 'insert', name: 'paragraph', length: 1, position: Position.createAt( doc.graveyard, 1 ) },
  188. { type: 'remove', name: 'paragraph', length: 1, position },
  189. { type: 'remove', name: 'paragraph', length: 1, position }
  190. ] );
  191. } );
  192. it( 'a character', () => {
  193. const position = new Position( root, [ 0, 1 ] );
  194. remove( position, 1 );
  195. expectChanges( [
  196. { type: 'insert', name: '$text', length: 1, position: Position.createAt( doc.graveyard, 0 ) },
  197. { type: 'remove', name: '$text', length: 1, position: new Position( root, [ 0, 1 ] ) }
  198. ] );
  199. } );
  200. it( 'multiple characters', () => {
  201. const position = new Position( root, [ 0, 1 ] );
  202. remove( position, 2 );
  203. expectChanges( [
  204. { type: 'insert', name: '$text', length: 2, position: Position.createAt( doc.graveyard, 0 ) },
  205. { type: 'remove', name: '$text', length: 2, position }
  206. ] );
  207. } );
  208. it( 'multiple consecutive characters in multiple operations', () => {
  209. const position = new Position( root, [ 0, 0 ] );
  210. remove( position, 1 );
  211. remove( position, 1 );
  212. remove( position, 1 );
  213. expectChanges( [
  214. { type: 'insert', name: '$text', length: 3, position: Position.createAt( doc.graveyard, 0 ) },
  215. { type: 'remove', name: '$text', length: 3, position }
  216. ] );
  217. } );
  218. it( 'multiple non-consecutive characters in multiple operations', () => {
  219. const position = new Position( root, [ 0, 0 ] );
  220. remove( position, 1 );
  221. remove( position.getShiftedBy( 1 ), 1 );
  222. expectChanges( [
  223. { type: 'insert', name: '$text', length: 2, position: Position.createAt( doc.graveyard, 0 ) },
  224. { type: 'remove', name: '$text', length: 1, position },
  225. { type: 'remove', name: '$text', length: 1, position: position.getShiftedBy( 1 ) }
  226. ] );
  227. } );
  228. it( 'item just before inserted item', () => {
  229. // This tests proper changes sorting.
  230. const insertPosition = new Position( root, [ 0, 2 ] );
  231. const removePosition = new Position( root, [ 0, 1 ] );
  232. insert( new Text( 'x' ), insertPosition );
  233. remove( removePosition, 1 );
  234. expectChanges( [
  235. { type: 'insert', name: '$text', length: 1, position: Position.createAt( doc.graveyard, 0 ) },
  236. { type: 'remove', name: '$text', length: 1, position: removePosition },
  237. { type: 'insert', name: '$text', length: 1, position: removePosition }
  238. ] );
  239. } );
  240. it( 'nodes before inserted nodes (together with some inserted nodes)', () => {
  241. const insertPosition = new Position( root, [ 0, 2 ] );
  242. const removePosition = new Position( root, [ 0, 1 ] );
  243. insert( new Text( 'xyz' ), insertPosition );
  244. remove( removePosition, 2 );
  245. expectChanges( [
  246. { type: 'insert', name: '$text', length: 2, position: Position.createAt( doc.graveyard, 0 ) },
  247. { type: 'remove', name: '$text', length: 1, position: removePosition },
  248. { type: 'insert', name: '$text', length: 2, position: removePosition }
  249. ] );
  250. } );
  251. it( 'inserted nodes and some nodes after inserted nodes', () => {
  252. const insertPosition = new Position( root, [ 0, 2 ] );
  253. const removePosition = new Position( root, [ 0, 3 ] );
  254. insert( new Text( 'xyz' ), insertPosition );
  255. remove( removePosition, 3 );
  256. expectChanges( [
  257. { type: 'insert', name: '$text', length: 3, position: Position.createAt( doc.graveyard, 0 ) },
  258. { type: 'insert', name: '$text', length: 1, position: insertPosition },
  259. { type: 'remove', name: '$text', length: 1, position: removePosition }
  260. ] );
  261. } );
  262. it( 'all inserted nodes', () => {
  263. const insertPosition = new Position( root, [ 0, 2 ] );
  264. const removePosition = new Position( root, [ 0, 1 ] );
  265. insert( new Text( 'xy' ), insertPosition );
  266. remove( removePosition, 4 );
  267. expectChanges( [
  268. { type: 'insert', name: '$text', length: 4, position: Position.createAt( doc.graveyard, 0 ) },
  269. { type: 'remove', name: '$text', length: 2, position: removePosition }
  270. ] );
  271. } );
  272. it( 'before removed nodes', () => {
  273. const removePositionA = new Position( root, [ 0, 2 ] );
  274. const removePositionB = new Position( root, [ 0, 0 ] );
  275. remove( removePositionA, 1 );
  276. remove( removePositionB, 1 );
  277. expectChanges( [
  278. { type: 'insert', name: '$text', length: 2, position: Position.createAt( doc.graveyard, 0 ) },
  279. { type: 'remove', name: '$text', length: 1, position: removePositionB },
  280. { type: 'remove', name: '$text', length: 1, position: new Position( root, [ 0, 1 ] ) }
  281. ] );
  282. } );
  283. it( 'before and after removed nodes in one operation', () => {
  284. const removePositionA = new Position( root, [ 0, 1 ] );
  285. const removePositionB = new Position( root, [ 0, 0 ] );
  286. remove( removePositionA, 1 );
  287. remove( removePositionB, 2 );
  288. expectChanges( [
  289. { type: 'insert', name: '$text', length: 3, position: Position.createAt( doc.graveyard, 0 ) },
  290. { type: 'remove', name: '$text', length: 3, position: removePositionB },
  291. ] );
  292. } );
  293. it( 'before nodes that changed attributes', () => {
  294. const position = new Position( root, [ 0, 0 ] );
  295. const p1 = root.getChild( 0 );
  296. const range = Range.createFromParentsAndOffsets( p1, 2, p1, 3 );
  297. attribute( range, 'bold', null, true );
  298. remove( position, 1 );
  299. const newRange = Range.createFromParentsAndOffsets( p1, 1, p1, 2 );
  300. expectChanges( [
  301. { type: 'insert', name: '$text', length: 1, position: Position.createAt( doc.graveyard, 0 ) },
  302. { type: 'remove', name: '$text', length: 1, position },
  303. { type: 'attribute', range: newRange, attributeKey: 'bold', attributeOldValue: null, attributeNewValue: true }
  304. ] );
  305. } );
  306. it( 'before nodes that changed attributes together with some changed nodes', () => {
  307. const position = new Position( root, [ 0, 0 ] );
  308. const p1 = root.getChild( 0 );
  309. const range = Range.createFromParentsAndOffsets( p1, 1, p1, 3 );
  310. attribute( range, 'bold', null, true );
  311. remove( position, 2 );
  312. const newRange = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  313. expectChanges( [
  314. { type: 'insert', name: '$text', length: 2, position: Position.createAt( doc.graveyard, 0 ) },
  315. { type: 'remove', name: '$text', length: 2, position },
  316. { type: 'attribute', range: newRange, attributeKey: 'bold', attributeOldValue: null, attributeNewValue: true }
  317. ] );
  318. } );
  319. it( 'some changed nodes', () => {
  320. const position = new Position( root, [ 0, 1 ] );
  321. const p1 = root.getChild( 0 );
  322. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 3 );
  323. attribute( range, 'bold', null, true );
  324. remove( position, 1 );
  325. const rangeBefore = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  326. const rangeAfter = Range.createFromParentsAndOffsets( p1, 1, p1, 2 );
  327. expectChanges( [
  328. { type: 'insert', name: '$text', length: 1, position: Position.createAt( doc.graveyard, 0 ) },
  329. { type: 'attribute', range: rangeBefore, attributeKey: 'bold', attributeOldValue: null, attributeNewValue: true },
  330. { type: 'remove', name: '$text', length: 1, position },
  331. { type: 'attribute', range: rangeAfter, attributeKey: 'bold', attributeOldValue: null, attributeNewValue: true }
  332. ] );
  333. } );
  334. it( 'some changed nodes and some nodes after', () => {
  335. const position = new Position( root, [ 0, 1 ] );
  336. const p1 = root.getChild( 0 );
  337. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 2 );
  338. attribute( range, 'bold', null, true );
  339. remove( position, 2 );
  340. const newRange = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  341. expectChanges( [
  342. { type: 'insert', name: '$text', length: 2, position: Position.createAt( doc.graveyard, 0 ) },
  343. { type: 'attribute', range: newRange, attributeKey: 'bold', attributeOldValue: null, attributeNewValue: true },
  344. { type: 'remove', name: '$text', length: 2, position }
  345. ] );
  346. } );
  347. it( 'after changed nodes', () => {
  348. const position = new Position( root, [ 0, 2 ] );
  349. const p1 = root.getChild( 0 );
  350. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  351. attribute( range, 'bold', null, true );
  352. remove( position, 1 );
  353. expectChanges( [
  354. { type: 'insert', name: '$text', length: 1, position: Position.createAt( doc.graveyard, 0 ) },
  355. { type: 'attribute', range, attributeKey: 'bold', attributeOldValue: null, attributeNewValue: true },
  356. { type: 'remove', name: '$text', length: 1, position }
  357. ] );
  358. } );
  359. } );
  360. // The only main difference between remove operation and move operation is target position.
  361. // In differ, graveyard is treated as other roots. In remove suite, simple cases for move are covered.
  362. // This suite will have only a few cases, focused on things specific to move operation.
  363. describe( 'move', () => {
  364. it( 'an element to the same parent - target position is after source position', () => {
  365. const sourcePosition = new Position( root, [ 0 ] );
  366. const targetPosition = new Position( root, [ 2 ] );
  367. move( sourcePosition, 1, targetPosition );
  368. expectChanges( [
  369. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  370. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  371. ] );
  372. } );
  373. it( 'an element to the same parent - target position is before source position', () => {
  374. const sourcePosition = new Position( root, [ 1 ] );
  375. const targetPosition = new Position( root, [ 0 ] );
  376. move( sourcePosition, 1, targetPosition );
  377. expectChanges( [
  378. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  379. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 2 ] ) }
  380. ] );
  381. } );
  382. it( 'multiple consecutive characters between different roots in multiple operations', () => {
  383. const sourcePosition = new Position( root, [ 0, 1 ] );
  384. const targetPosition = new Position( root, [ 1, 0 ] );
  385. move( sourcePosition, 1, targetPosition );
  386. move( sourcePosition, 1, targetPosition.getShiftedBy( 1 ) );
  387. expectChanges( [
  388. { type: 'remove', name: '$text', length: 2, position: sourcePosition },
  389. { type: 'insert', name: '$text', length: 2, position: targetPosition }
  390. ] );
  391. } );
  392. it( 'reinsert removed element', () => {
  393. doc.graveyard.appendChildren( new Element( 'listItem' ) );
  394. const sourcePosition = new Position( doc.graveyard, [ 0 ] );
  395. const targetPosition = new Position( root, [ 2 ] );
  396. move( sourcePosition, 1, targetPosition );
  397. expectChanges( [
  398. { type: 'remove', name: 'listItem', length: 1, position: sourcePosition },
  399. { type: 'insert', name: 'listItem', length: 1, position: targetPosition }
  400. ] );
  401. } );
  402. } );
  403. describe( 'rename', () => {
  404. it( 'an element', () => {
  405. rename( root.getChild( 1 ), 'listItem' );
  406. expectChanges( [
  407. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) },
  408. { type: 'insert', name: 'listItem', length: 1, position: new Position( root, [ 1 ] ) }
  409. ] );
  410. } );
  411. } );
  412. describe( 'attribute', () => {
  413. const attributeKey = 'key';
  414. const attributeOldValue = null;
  415. const attributeNewValue = 'foo';
  416. it( 'on an element', () => {
  417. const range = Range.createFromParentsAndOffsets( root, 0, root.getChild( 0 ), 0 );
  418. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  419. expectChanges( [
  420. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  421. ] );
  422. } );
  423. it( 'on a character', () => {
  424. const parent = root.getChild( 1 );
  425. const range = Range.createFromParentsAndOffsets( parent, 1, parent, 2 );
  426. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  427. expectChanges( [
  428. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  429. ] );
  430. } );
  431. it( 'on a character - case with same characters next to each other', () => {
  432. const parent = root.getChild( 0 );
  433. const range = Range.createFromParentsAndOffsets( parent, 1, parent, 2 );
  434. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  435. expectChanges( [
  436. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  437. ] );
  438. } );
  439. it( 'on multiple characters', () => {
  440. const parent = root.getChild( 0 );
  441. const range = Range.createFromParentsAndOffsets( parent, 0, parent, 3 );
  442. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  443. expectChanges( [
  444. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  445. ] );
  446. } );
  447. it( 'on multiple consecutive characters in multiple operations', () => {
  448. const parent = root.getChild( 0 );
  449. const range1 = Range.createFromParentsAndOffsets( parent, 1, parent, 2 );
  450. const range2 = Range.createFromParentsAndOffsets( parent, 2, parent, 3 );
  451. attribute( range1, attributeKey, attributeOldValue, attributeNewValue );
  452. attribute( range2, attributeKey, attributeOldValue, attributeNewValue );
  453. const range = Range.createFromParentsAndOffsets( parent, 1, parent, 3 );
  454. expectChanges( [
  455. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  456. ] );
  457. } );
  458. it( 'on multiple non-consecutive characters in multiple operations', () => {
  459. const parent = root.getChild( 0 );
  460. const range1 = Range.createFromParentsAndOffsets( parent, 0, parent, 1 );
  461. const range2 = Range.createFromParentsAndOffsets( parent, 2, parent, 3 );
  462. // Note "reversed" order of ranges. Further range is changed first.
  463. attribute( range2, attributeKey, attributeOldValue, attributeNewValue );
  464. attribute( range1, attributeKey, attributeOldValue, attributeNewValue );
  465. // Note that changes has been sorted.
  466. expectChanges( [
  467. { type: 'attribute', range: range1, attributeKey, attributeOldValue, attributeNewValue },
  468. { type: 'attribute', range: range2, attributeKey, attributeOldValue, attributeNewValue }
  469. ] );
  470. } );
  471. it( 'on range containing various nodes', () => {
  472. const range = Range.createFromParentsAndOffsets( root, 0, root, 2 );
  473. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  474. const p1 = root.getChild( 0 );
  475. const p2 = root.getChild( 1 );
  476. const type = 'attribute';
  477. expectChanges( [
  478. { type, range: Range.createFromParentsAndOffsets( root, 0, p1, 0 ), attributeKey, attributeOldValue, attributeNewValue },
  479. { type, range: Range.createFromParentsAndOffsets( p1, 0, p1, 3 ), attributeKey, attributeOldValue, attributeNewValue },
  480. { type, range: Range.createFromParentsAndOffsets( root, 1, p2, 0 ), attributeKey, attributeOldValue, attributeNewValue },
  481. { type, range: Range.createFromParentsAndOffsets( p2, 0, p2, 3 ), attributeKey, attributeOldValue, attributeNewValue }
  482. ] );
  483. } );
  484. it( 'remove and add attribute on text', () => {
  485. const p = root.getChild( 1 );
  486. p.getChild( 0 ).setAttribute( 'bold', true );
  487. const range = Range.createFromParentsAndOffsets( p, 1, p, 3 );
  488. attribute( range, 'bold', true, null );
  489. attribute( range, 'italic', null, true );
  490. const range1 = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  491. const range2 = Range.createFromParentsAndOffsets( p, 2, p, 3 );
  492. // Attribute change glueing does not work 100% correct.
  493. expectChanges( [
  494. { type: 'attribute', range: range1, attributeKey: 'bold', attributeOldValue: true, attributeNewValue: null },
  495. { type: 'attribute', range: range1, attributeKey: 'italic', attributeOldValue: null, attributeNewValue: true },
  496. { type: 'attribute', range: range2, attributeKey: 'bold', attributeOldValue: true, attributeNewValue: null },
  497. { type: 'attribute', range: range2, attributeKey: 'italic', attributeOldValue: null, attributeNewValue: true }
  498. ] );
  499. } );
  500. it( 'on some old nodes and inserted nodes', () => {
  501. const position = new Position( root, [ 0, 1 ] );
  502. const p1 = root.getChild( 0 );
  503. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 2 );
  504. insert( new Text( 'xx' ), position );
  505. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  506. const rangeBefore = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  507. expectChanges( [
  508. { type: 'attribute', range: rangeBefore, attributeKey, attributeOldValue, attributeNewValue },
  509. { type: 'insert', name: '$text', length: 2, position }
  510. ] );
  511. } );
  512. it( 'over all inserted nodes and some old nodes', () => {
  513. const position = new Position( root, [ 0, 1 ] );
  514. const p1 = root.getChild( 0 );
  515. const range = Range.createFromParentsAndOffsets( p1, 2, p1, 3 );
  516. insert( new Text( 'xxx' ), position );
  517. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  518. expectChanges( [
  519. { type: 'insert', name: '$text', length: 3, position }
  520. ] );
  521. } );
  522. it( 'only on inserted nodes', () => {
  523. const position = new Position( root, [ 0, 1 ] );
  524. const p1 = root.getChild( 0 );
  525. const range = Range.createFromParentsAndOffsets( p1, 2, p1, 4 );
  526. insert( new Text( 'xx' ), position );
  527. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  528. const rangeAfter = Range.createFromParentsAndOffsets( p1, 3, p1, 4 );
  529. expectChanges( [
  530. { type: 'insert', name: '$text', length: 2, position },
  531. { type: 'attribute', range: rangeAfter, attributeKey, attributeOldValue, attributeNewValue }
  532. ] );
  533. } );
  534. it( 'on some inserted nodes and old nodes', () => {
  535. const position = new Position( root, [ 0, 1 ] );
  536. const p1 = root.getChild( 0 );
  537. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 4 );
  538. insert( new Text( 'xx' ), position );
  539. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  540. const rangeBefore = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  541. const rangeAfter = Range.createFromParentsAndOffsets( p1, 3, p1, 4 );
  542. expectChanges( [
  543. { type: 'attribute', range: rangeBefore, attributeKey, attributeOldValue, attributeNewValue },
  544. { type: 'insert', name: '$text', length: 2, position },
  545. { type: 'attribute', range: rangeAfter, attributeKey, attributeOldValue, attributeNewValue }
  546. ] );
  547. } );
  548. it( 'on some not changed and some changed nodes', () => {
  549. const p = root.getChild( 0 );
  550. const rangeA = Range.createFromParentsAndOffsets( p, 1, p, 3 );
  551. const rangeB = Range.createFromParentsAndOffsets( p, 0, p, 2 );
  552. attribute( rangeA, 'a', null, true );
  553. attribute( rangeB, 'b', null, true );
  554. const type = 'attribute';
  555. const attributeOldValue = null;
  556. const attributeNewValue = true;
  557. // Attribute change glueing does not work 100% correct.
  558. expectChanges( [
  559. { type, range: Range.createFromParentsAndOffsets( p, 0, p, 1 ), attributeKey: 'b', attributeOldValue, attributeNewValue },
  560. { type, range: Range.createFromParentsAndOffsets( p, 1, p, 2 ), attributeKey: 'a', attributeOldValue, attributeNewValue },
  561. { type, range: Range.createFromParentsAndOffsets( p, 1, p, 2 ), attributeKey: 'b', attributeOldValue, attributeNewValue },
  562. { type, range: Range.createFromParentsAndOffsets( p, 2, p, 3 ), attributeKey: 'a', attributeOldValue, attributeNewValue }
  563. ] );
  564. } );
  565. it( 'on already changed nodes', () => {
  566. const p = root.getChild( 1 );
  567. const rangeA = Range.createFromParentsAndOffsets( p, 0, p, 3 );
  568. const rangeB = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  569. attribute( rangeA, 'a', null, true );
  570. attribute( rangeB, 'b', null, true );
  571. const type = 'attribute';
  572. const attributeOldValue = null;
  573. const attributeNewValue = true;
  574. // Attribute change glueing does not work 100% correct.
  575. expectChanges( [
  576. { type, range: Range.createFromParentsAndOffsets( p, 0, p, 2 ), attributeKey: 'a', attributeOldValue, attributeNewValue },
  577. { type, range: Range.createFromParentsAndOffsets( p, 1, p, 2 ), attributeKey: 'b', attributeOldValue, attributeNewValue },
  578. { type, range: Range.createFromParentsAndOffsets( p, 2, p, 3 ), attributeKey: 'a', attributeOldValue, attributeNewValue }
  579. ] );
  580. } );
  581. it( 'on some changed and some not changed nodes', () => {
  582. const p = root.getChild( 1 );
  583. const rangeA = Range.createFromParentsAndOffsets( p, 0, p, 2 );
  584. const rangeB = Range.createFromParentsAndOffsets( p, 1, p, 3 );
  585. attribute( rangeA, 'a', null, true );
  586. attribute( rangeB, 'b', null, true );
  587. const type = 'attribute';
  588. const attributeOldValue = null;
  589. const attributeNewValue = true;
  590. expectChanges( [
  591. { type, range: Range.createFromParentsAndOffsets( p, 0, p, 2 ), attributeKey: 'a', attributeOldValue, attributeNewValue },
  592. { type, range: Range.createFromParentsAndOffsets( p, 1, p, 3 ), attributeKey: 'b', attributeOldValue, attributeNewValue },
  593. ] );
  594. } );
  595. it( 'over all changed nodes and some not changed nodes', () => {
  596. const p = root.getChild( 0 );
  597. const rangeA = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  598. const rangeB = Range.createFromParentsAndOffsets( p, 0, p, 3 );
  599. attribute( rangeA, 'a', null, true );
  600. attribute( rangeB, 'b', null, true );
  601. const type = 'attribute';
  602. const attributeOldValue = null;
  603. const attributeNewValue = true;
  604. // Attribute change glueing does not work 100% correct.
  605. expectChanges( [
  606. { type, range: Range.createFromParentsAndOffsets( p, 0, p, 1 ), attributeKey: 'b', attributeOldValue, attributeNewValue },
  607. { type, range: Range.createFromParentsAndOffsets( p, 1, p, 2 ), attributeKey: 'a', attributeOldValue, attributeNewValue },
  608. { type, range: Range.createFromParentsAndOffsets( p, 1, p, 3 ), attributeKey: 'b', attributeOldValue, attributeNewValue },
  609. ] );
  610. } );
  611. } );
  612. describe( 'markers', () => {
  613. let range, rangeB;
  614. beforeEach( () => {
  615. range = Range.createFromParentsAndOffsets( root, 0, root, 1 );
  616. rangeB = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  617. } );
  618. it( 'add marker', () => {
  619. differ.bufferMarkerChange( 'name', null, range );
  620. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  621. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  622. { name: 'name', range }
  623. ] );
  624. } );
  625. it( 'remove marker', () => {
  626. differ.bufferMarkerChange( 'name', range, null );
  627. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  628. { name: 'name', range }
  629. ] );
  630. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  631. } );
  632. it( 'change marker', () => {
  633. differ.bufferMarkerChange( 'name', range, rangeB );
  634. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  635. { name: 'name', range }
  636. ] );
  637. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  638. { name: 'name', range: rangeB }
  639. ] );
  640. } );
  641. it( 'add marker and remove it', () => {
  642. differ.bufferMarkerChange( 'name', null, range );
  643. differ.bufferMarkerChange( 'name', range, null );
  644. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  645. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  646. } );
  647. it( 'add marker and change it', () => {
  648. differ.bufferMarkerChange( 'name', null, range );
  649. differ.bufferMarkerChange( 'name', range, rangeB );
  650. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  651. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  652. { name: 'name', range: rangeB }
  653. ] );
  654. } );
  655. it( 'change marker and remove it', () => {
  656. differ.bufferMarkerChange( 'name', range, rangeB );
  657. differ.bufferMarkerChange( 'name', rangeB, null );
  658. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  659. { name: 'name', range }
  660. ] );
  661. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  662. } );
  663. it( 'remove marker and add it at same range', () => {
  664. differ.bufferMarkerChange( 'name', range, null );
  665. differ.bufferMarkerChange( 'name', null, range );
  666. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  667. { name: 'name', range }
  668. ] );
  669. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  670. { name: 'name', range }
  671. ] );
  672. } );
  673. it( 'change marker to the same range', () => {
  674. differ.bufferMarkerChange( 'name', range, range );
  675. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  676. { name: 'name', range }
  677. ] );
  678. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  679. { name: 'name', range }
  680. ] );
  681. } );
  682. } );
  683. function insert( item, position ) {
  684. const operation = new InsertOperation( position, item, doc.version );
  685. differ.bufferOperation( operation );
  686. model.applyOperation( wrapInDelta( operation ) );
  687. }
  688. function remove( sourcePosition, howMany ) {
  689. const targetPosition = Position.createAt( doc.graveyard, doc.graveyard.maxOffset );
  690. const operation = new RemoveOperation( sourcePosition, howMany, targetPosition, doc.version );
  691. differ.bufferOperation( operation );
  692. model.applyOperation( wrapInDelta( operation ) );
  693. }
  694. function move( sourcePosition, howMany, targetPosition ) {
  695. const operation = new MoveOperation( sourcePosition, howMany, targetPosition, doc.version );
  696. differ.bufferOperation( operation );
  697. model.applyOperation( wrapInDelta( operation ) );
  698. }
  699. function rename( element, newName ) {
  700. const operation = new RenameOperation( Position.createBefore( element ), element.name, newName, doc.version );
  701. differ.bufferOperation( operation );
  702. model.applyOperation( wrapInDelta( operation ) );
  703. }
  704. function attribute( range, key, oldValue, newValue ) {
  705. const operation = new AttributeOperation( range, key, oldValue, newValue, doc.version );
  706. differ.bufferOperation( operation );
  707. model.applyOperation( wrapInDelta( operation ) );
  708. }
  709. function expectChanges( expected ) {
  710. const changes = differ.getChanges();
  711. for ( let i = 0; i < expected.length; i++ ) {
  712. for ( const key in expected[ i ] ) {
  713. if ( expected[ i ].hasOwnProperty( key ) ) {
  714. if ( key == 'position' || key == 'range' ) {
  715. expect( changes[ i ][ key ].isEqual( expected[ i ][ key ] ), `item ${ i }, key "${ key }"` ).to.be.true;
  716. } else {
  717. expect( changes[ i ][ key ], `item ${ i }, key "${ key }"` ).to.equal( expected[ i ][ key ] );
  718. }
  719. }
  720. }
  721. }
  722. }
  723. } );