differ.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../src/model/model';
  6. import Element from '../../src/model/element';
  7. import Text from '../../src/model/text';
  8. import Position from '../../src/model/position';
  9. import Range from '../../src/model/range';
  10. import InsertOperation from '../../src/model/operation/insertoperation';
  11. import RemoveOperation from '../../src/model/operation/removeoperation';
  12. import MoveOperation from '../../src/model/operation/moveoperation';
  13. import RenameOperation from '../../src/model/operation/renameoperation';
  14. import AttributeOperation from '../../src/model/operation/attributeoperation';
  15. import { wrapInDelta } from '../../tests/model/_utils/utils';
  16. describe( 'Differ', () => {
  17. let doc, differ, root, model;
  18. beforeEach( () => {
  19. model = new Model();
  20. doc = model.document;
  21. differ = doc.differ;
  22. root = doc.createRoot();
  23. root.appendChildren( [
  24. new Element( 'paragraph', null, [
  25. new Text( 'foo' )
  26. ] ),
  27. new Element( 'paragraph', null, [
  28. new Text( 'bar' )
  29. ] )
  30. ] );
  31. } );
  32. describe( 'insert', () => {
  33. // Simple.
  34. it( 'an element', () => {
  35. const position = new Position( root, [ 1 ] );
  36. model.change( () => {
  37. insert( new Element( 'image' ), position );
  38. expectChanges( [
  39. { type: 'insert', name: 'image', length: 1, position }
  40. ] );
  41. } );
  42. } );
  43. it( 'a non-empty element with attributes', () => {
  44. const position = new Position( root, [ 1 ] );
  45. model.change( () => {
  46. insert(
  47. new Element( 'image', { src: 'foo.jpg' }, new Element( 'caption', null, new Text( 'bar' ) ) ),
  48. position
  49. );
  50. expectChanges( [
  51. { type: 'insert', name: 'image', length: 1, position }
  52. ] );
  53. } );
  54. } );
  55. it( 'multiple elements', () => {
  56. const position = new Position( root, [ 1 ] );
  57. model.change( () => {
  58. insert( [ new Element( 'image' ), new Element( 'paragraph' ) ], position );
  59. expectChanges( [
  60. { type: 'insert', name: 'image', length: 1, position },
  61. { type: 'insert', name: 'paragraph', length: 1, position: position.getShiftedBy( 1 ) }
  62. ] );
  63. } );
  64. } );
  65. it( 'a character', () => {
  66. const position = new Position( root, [ 0, 2 ] );
  67. model.change( () => {
  68. insert( new Text( 'x' ), position );
  69. expectChanges( [
  70. { type: 'insert', name: '$text', length: 1, position }
  71. ] );
  72. } );
  73. } );
  74. it( 'multiple characters', () => {
  75. const position = new Position( root, [ 0, 2 ] );
  76. model.change( () => {
  77. insert( new Text( 'xyz' ), position );
  78. expectChanges( [
  79. { type: 'insert', name: '$text', length: 3, position }
  80. ] );
  81. } );
  82. } );
  83. it( 'multiple consecutive characters in multiple operations', () => {
  84. const position = new Position( root, [ 0, 2 ] );
  85. model.change( () => {
  86. insert( new Text( 'xy' ), position );
  87. insert( new Text( 'z' ), position.getShiftedBy( 2 ) );
  88. insert( new Text( 'ab' ), position );
  89. expectChanges( [
  90. { type: 'insert', name: '$text', length: 5, position }
  91. ] );
  92. } );
  93. } );
  94. it( 'multiple non-consecutive characters in multiple operations', () => {
  95. const position = new Position( root, [ 0, 0 ] );
  96. model.change( () => {
  97. insert( new Text( 'xy' ), position );
  98. insert( new Text( 'z' ), position.getShiftedBy( 3 ) );
  99. expectChanges( [
  100. { type: 'insert', name: '$text', length: 2, position },
  101. { type: 'insert', name: '$text', length: 1, position: position.getShiftedBy( 3 ) }
  102. ] );
  103. } );
  104. } );
  105. // Combined.
  106. it( 'node in a new element', () => {
  107. const image = new Element( 'image' );
  108. const position = new Position( root, [ 1 ] );
  109. model.change( () => {
  110. insert( image, position );
  111. const caption = new Element( 'caption' );
  112. insert( caption, Position.createAt( image, 0 ) );
  113. insert( new Text( 'foo' ), Position.createAt( caption, 0 ) );
  114. expectChanges( [
  115. { type: 'insert', name: 'image', length: 1, position }
  116. ] );
  117. } );
  118. } );
  119. it( 'node in a renamed element', () => {
  120. const text = new Text( 'xyz', { bold: true } );
  121. const position = new Position( root, [ 0, 3 ] );
  122. model.change( () => {
  123. insert( text, position );
  124. rename( root.getChild( 0 ), 'listItem' );
  125. // Note that since renamed element is removed and then re-inserted, there is no diff for text inserted inside it.
  126. expectChanges( [
  127. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  128. { type: 'insert', name: 'listItem', length: 1, position: new Position( root, [ 0 ] ) }
  129. ] );
  130. } );
  131. } );
  132. it( 'node in a element with changed attribute', () => {
  133. const text = new Text( 'xyz', { bold: true } );
  134. const position = new Position( root, [ 0, 3 ] );
  135. const range = Range.createFromParentsAndOffsets( root, 0, root.getChild( 0 ), 0 );
  136. model.change( () => {
  137. insert( text, position );
  138. attribute( range, 'align', null, 'center' );
  139. // Compare to scenario above, this time there is only an attribute change on parent element,
  140. // so there is also a diff for text.
  141. expectChanges( [
  142. { type: 'attribute', range, attributeKey: 'align', attributeOldValue: null, attributeNewValue: 'center' },
  143. { type: 'insert', name: '$text', length: 3, position },
  144. ] );
  145. } );
  146. } );
  147. it( 'nodes between other inserted nodes', () => {
  148. model.change( () => {
  149. insert( new Text( 'xx' ), new Position( root, [ 0, 1 ] ) );
  150. insert( new Text( 'yy' ), new Position( root, [ 0, 2 ] ) );
  151. expectChanges( [
  152. { type: 'insert', position: new Position( root, [ 0, 1 ] ), length: 4, name: '$text' }
  153. ] );
  154. } );
  155. } );
  156. it( 'nodes before nodes with changed attributes', () => {
  157. const p1 = root.getChild( 0 );
  158. const range = Range.createFromParentsAndOffsets( p1, 1, p1, 3 );
  159. const position = new Position( root, [ 0, 0 ] );
  160. model.change( () => {
  161. attribute( range, 'bold', null, true );
  162. insert( new Text( 'xx' ), position );
  163. const rangeAfter = Range.createFromParentsAndOffsets( p1, 3, p1, 5 );
  164. expectChanges( [
  165. { type: 'insert', name: '$text', length: 2, position },
  166. { type: 'attribute', range: rangeAfter, attributeKey: 'bold', attributeOldValue: null, attributeNewValue: true }
  167. ] );
  168. } );
  169. } );
  170. it( 'nodes between nodes with changed attributes', () => {
  171. const p1 = root.getChild( 0 );
  172. const range = Range.createFromParentsAndOffsets( p1, 1, p1, 3 );
  173. const position = new Position( root, [ 0, 2 ] );
  174. model.change( () => {
  175. attribute( range, 'bold', null, true );
  176. insert( new Text( 'xx' ), position );
  177. const rangeBefore = Range.createFromParentsAndOffsets( p1, 1, p1, 2 );
  178. const rangeAfter = Range.createFromParentsAndOffsets( p1, 4, p1, 5 );
  179. expectChanges( [
  180. {
  181. type: 'attribute',
  182. range: rangeBefore,
  183. attributeKey: 'bold',
  184. attributeOldValue: null,
  185. attributeNewValue: true
  186. },
  187. { type: 'insert', name: '$text', length: 2, position },
  188. {
  189. type: 'attribute',
  190. range: rangeAfter,
  191. attributeKey: 'bold',
  192. attributeOldValue: null,
  193. attributeNewValue: true
  194. }
  195. ] );
  196. } );
  197. } );
  198. it( 'nodes after nodes with changed attributes', () => {
  199. const p1 = root.getChild( 0 );
  200. const range = Range.createFromParentsAndOffsets( p1, 1, p1, 3 );
  201. const position = new Position( root, [ 0, 3 ] );
  202. model.change( () => {
  203. attribute( range, 'bold', null, true );
  204. insert( new Text( 'xx' ), position );
  205. expectChanges( [
  206. {
  207. type: 'attribute',
  208. range,
  209. attributeKey: 'bold',
  210. attributeOldValue: null,
  211. attributeNewValue: true
  212. },
  213. { type: 'insert', name: '$text', length: 2, position }
  214. ] );
  215. } );
  216. } );
  217. } );
  218. describe( 'remove', () => {
  219. it( 'an element', () => {
  220. const position = new Position( root, [ 0 ] );
  221. model.change( () => {
  222. remove( position, 1 );
  223. expectChanges( [
  224. { type: 'remove', name: 'paragraph', length: 1, position }
  225. ] );
  226. } );
  227. } );
  228. it( 'multiple elements', () => {
  229. const position = new Position( root, [ 0 ] );
  230. model.change( () => {
  231. remove( position, 2 );
  232. expectChanges( [
  233. { type: 'remove', name: 'paragraph', length: 1, position },
  234. { type: 'remove', name: 'paragraph', length: 1, position }
  235. ] );
  236. } );
  237. } );
  238. it( 'a character', () => {
  239. const position = new Position( root, [ 0, 1 ] );
  240. model.change( () => {
  241. remove( position, 1 );
  242. expectChanges( [
  243. { type: 'remove', name: '$text', length: 1, position: new Position( root, [ 0, 1 ] ) }
  244. ] );
  245. } );
  246. } );
  247. it( 'multiple characters', () => {
  248. const position = new Position( root, [ 0, 1 ] );
  249. model.change( () => {
  250. remove( position, 2 );
  251. expectChanges( [
  252. { type: 'remove', name: '$text', length: 2, position }
  253. ] );
  254. } );
  255. } );
  256. it( 'multiple consecutive characters in multiple operations', () => {
  257. const position = new Position( root, [ 0, 0 ] );
  258. model.change( () => {
  259. remove( position, 1 );
  260. remove( position, 1 );
  261. remove( position, 1 );
  262. expectChanges( [
  263. { type: 'remove', name: '$text', length: 3, position }
  264. ] );
  265. } );
  266. } );
  267. it( 'multiple non-consecutive characters in multiple operations', () => {
  268. const position = new Position( root, [ 0, 0 ] );
  269. model.change( () => {
  270. remove( position, 1 );
  271. remove( position.getShiftedBy( 1 ), 1 );
  272. expectChanges( [
  273. { type: 'remove', name: '$text', length: 1, position },
  274. { type: 'remove', name: '$text', length: 1, position: position.getShiftedBy( 1 ) }
  275. ] );
  276. } );
  277. } );
  278. it( 'item just before inserted item', () => {
  279. // This tests proper changes sorting.
  280. const insertPosition = new Position( root, [ 0, 2 ] );
  281. const removePosition = new Position( root, [ 0, 1 ] );
  282. model.change( () => {
  283. insert( new Text( 'x' ), insertPosition );
  284. remove( removePosition, 1 );
  285. expectChanges( [
  286. { type: 'remove', name: '$text', length: 1, position: removePosition },
  287. { type: 'insert', name: '$text', length: 1, position: removePosition }
  288. ] );
  289. } );
  290. } );
  291. it( 'nodes before inserted nodes (together with some inserted nodes)', () => {
  292. const insertPosition = new Position( root, [ 0, 2 ] );
  293. const removePosition = new Position( root, [ 0, 1 ] );
  294. model.change( () => {
  295. insert( new Text( 'xyz' ), insertPosition );
  296. remove( removePosition, 2 );
  297. expectChanges( [
  298. { type: 'remove', name: '$text', length: 1, position: removePosition },
  299. { type: 'insert', name: '$text', length: 2, position: removePosition }
  300. ] );
  301. } );
  302. } );
  303. it( 'inserted nodes and some nodes after inserted nodes', () => {
  304. const insertPosition = new Position( root, [ 0, 2 ] );
  305. const removePosition = new Position( root, [ 0, 3 ] );
  306. model.change( () => {
  307. insert( new Text( 'xyz' ), insertPosition );
  308. remove( removePosition, 3 );
  309. expectChanges( [
  310. { type: 'insert', name: '$text', length: 1, position: insertPosition },
  311. { type: 'remove', name: '$text', length: 1, position: removePosition }
  312. ] );
  313. } );
  314. } );
  315. it( 'all inserted nodes', () => {
  316. const insertPosition = new Position( root, [ 0, 2 ] );
  317. const removePosition = new Position( root, [ 0, 1 ] );
  318. model.change( () => {
  319. insert( new Text( 'xy' ), insertPosition );
  320. remove( removePosition, 4 );
  321. expectChanges( [
  322. { type: 'remove', name: '$text', length: 2, position: removePosition }
  323. ] );
  324. } );
  325. } );
  326. it( 'before removed nodes', () => {
  327. const removePositionA = new Position( root, [ 0, 2 ] );
  328. const removePositionB = new Position( root, [ 0, 0 ] );
  329. model.change( () => {
  330. remove( removePositionA, 1 );
  331. remove( removePositionB, 1 );
  332. expectChanges( [
  333. { type: 'remove', name: '$text', length: 1, position: removePositionB },
  334. { type: 'remove', name: '$text', length: 1, position: new Position( root, [ 0, 1 ] ) }
  335. ] );
  336. } );
  337. } );
  338. it( 'before and after removed nodes in one operation', () => {
  339. const removePositionA = new Position( root, [ 0, 1 ] );
  340. const removePositionB = new Position( root, [ 0, 0 ] );
  341. model.change( () => {
  342. remove( removePositionA, 1 );
  343. remove( removePositionB, 2 );
  344. expectChanges( [
  345. { type: 'remove', name: '$text', length: 3, position: removePositionB },
  346. ] );
  347. } );
  348. } );
  349. it( 'before nodes that changed attributes', () => {
  350. const position = new Position( root, [ 0, 0 ] );
  351. const p1 = root.getChild( 0 );
  352. const range = Range.createFromParentsAndOffsets( p1, 2, p1, 3 );
  353. model.change( () => {
  354. attribute( range, 'bold', null, true );
  355. remove( position, 1 );
  356. const newRange = Range.createFromParentsAndOffsets( p1, 1, p1, 2 );
  357. expectChanges( [
  358. { type: 'remove', name: '$text', length: 1, position },
  359. {
  360. type: 'attribute',
  361. range: newRange,
  362. attributeKey: 'bold',
  363. attributeOldValue: null,
  364. attributeNewValue: true
  365. }
  366. ] );
  367. } );
  368. } );
  369. it( 'before nodes that changed attributes together with some changed nodes', () => {
  370. const position = new Position( root, [ 0, 0 ] );
  371. const p1 = root.getChild( 0 );
  372. const range = Range.createFromParentsAndOffsets( p1, 1, p1, 3 );
  373. model.change( () => {
  374. attribute( range, 'bold', null, true );
  375. remove( position, 2 );
  376. const newRange = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  377. expectChanges( [
  378. { type: 'remove', name: '$text', length: 2, position },
  379. {
  380. type: 'attribute',
  381. range: newRange,
  382. attributeKey: 'bold',
  383. attributeOldValue: null,
  384. attributeNewValue: true
  385. }
  386. ] );
  387. } );
  388. } );
  389. it( 'some changed nodes', () => {
  390. const position = new Position( root, [ 0, 1 ] );
  391. const p1 = root.getChild( 0 );
  392. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 3 );
  393. model.change( () => {
  394. attribute( range, 'bold', null, true );
  395. remove( position, 1 );
  396. const rangeBefore = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  397. const rangeAfter = Range.createFromParentsAndOffsets( p1, 1, p1, 2 );
  398. expectChanges( [
  399. {
  400. type: 'attribute',
  401. range: rangeBefore,
  402. attributeKey: 'bold',
  403. attributeOldValue: null,
  404. attributeNewValue: true
  405. },
  406. { type: 'remove', name: '$text', length: 1, position },
  407. {
  408. type: 'attribute',
  409. range: rangeAfter,
  410. attributeKey: 'bold',
  411. attributeOldValue: null,
  412. attributeNewValue: true
  413. }
  414. ] );
  415. } );
  416. } );
  417. it( 'some changed nodes and some nodes after', () => {
  418. const position = new Position( root, [ 0, 1 ] );
  419. const p1 = root.getChild( 0 );
  420. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 2 );
  421. model.change( () => {
  422. attribute( range, 'bold', null, true );
  423. remove( position, 2 );
  424. const newRange = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  425. expectChanges( [
  426. {
  427. type: 'attribute',
  428. range: newRange,
  429. attributeKey: 'bold',
  430. attributeOldValue: null,
  431. attributeNewValue: true
  432. },
  433. { type: 'remove', name: '$text', length: 2, position }
  434. ] );
  435. } );
  436. } );
  437. it( 'after changed nodes', () => {
  438. const position = new Position( root, [ 0, 2 ] );
  439. const p1 = root.getChild( 0 );
  440. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  441. model.change( () => {
  442. attribute( range, 'bold', null, true );
  443. remove( position, 1 );
  444. expectChanges( [
  445. {
  446. type: 'attribute',
  447. range,
  448. attributeKey: 'bold',
  449. attributeOldValue: null,
  450. attributeNewValue: true
  451. },
  452. { type: 'remove', name: '$text', length: 1, position }
  453. ] );
  454. } );
  455. } );
  456. } );
  457. // The only main difference between remove operation and move operation is target position.
  458. // In differ, graveyard is treated as other roots. In remove suite, simple cases for move are covered.
  459. // This suite will have only a few cases, focused on things specific to move operation.
  460. describe( 'move', () => {
  461. it( 'an element to the same parent - target position is after source position', () => {
  462. const sourcePosition = new Position( root, [ 0 ] );
  463. const targetPosition = new Position( root, [ 2 ] );
  464. model.change( () => {
  465. move( sourcePosition, 1, targetPosition );
  466. expectChanges( [
  467. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  468. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  469. ] );
  470. } );
  471. } );
  472. it( 'an element to the same parent - target position is before source position', () => {
  473. const sourcePosition = new Position( root, [ 1 ] );
  474. const targetPosition = new Position( root, [ 0 ] );
  475. model.change( () => {
  476. move( sourcePosition, 1, targetPosition );
  477. expectChanges( [
  478. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  479. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 2 ] ) }
  480. ] );
  481. } );
  482. } );
  483. it( 'multiple consecutive characters between different roots in multiple operations', () => {
  484. const sourcePosition = new Position( root, [ 0, 1 ] );
  485. const targetPosition = new Position( root, [ 1, 0 ] );
  486. model.change( () => {
  487. move( sourcePosition, 1, targetPosition );
  488. move( sourcePosition, 1, targetPosition.getShiftedBy( 1 ) );
  489. expectChanges( [
  490. { type: 'remove', name: '$text', length: 2, position: sourcePosition },
  491. { type: 'insert', name: '$text', length: 2, position: targetPosition }
  492. ] );
  493. } );
  494. } );
  495. it( 'reinsert removed element', () => {
  496. doc.graveyard.appendChildren( new Element( 'listItem' ) );
  497. const sourcePosition = new Position( doc.graveyard, [ 0 ] );
  498. const targetPosition = new Position( root, [ 2 ] );
  499. model.change( () => {
  500. move( sourcePosition, 1, targetPosition );
  501. expectChanges( [
  502. { type: 'insert', name: 'listItem', length: 1, position: targetPosition }
  503. ] );
  504. } );
  505. } );
  506. } );
  507. describe( 'rename', () => {
  508. it( 'an element', () => {
  509. model.change( () => {
  510. rename( root.getChild( 1 ), 'listItem' );
  511. expectChanges( [
  512. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) },
  513. { type: 'insert', name: 'listItem', length: 1, position: new Position( root, [ 1 ] ) }
  514. ] );
  515. } );
  516. } );
  517. } );
  518. describe( 'attribute', () => {
  519. const attributeKey = 'key';
  520. const attributeOldValue = null;
  521. const attributeNewValue = 'foo';
  522. it( 'on an element', () => {
  523. const range = Range.createFromParentsAndOffsets( root, 0, root.getChild( 0 ), 0 );
  524. model.change( () => {
  525. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  526. expectChanges( [
  527. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  528. ] );
  529. } );
  530. } );
  531. it( 'on an element - only one of many attributes changes', () => {
  532. const range = Range.createFromParentsAndOffsets( root, 0, root.getChild( 0 ), 0 );
  533. model.change( () => {
  534. // Set an attribute on an element. It won't change afterwards.
  535. attribute( range, 'otherAttr', null, true );
  536. } );
  537. model.change( () => {
  538. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  539. expectChanges( [
  540. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  541. ] );
  542. } );
  543. } );
  544. it( 'on a character', () => {
  545. const parent = root.getChild( 1 );
  546. const range = Range.createFromParentsAndOffsets( parent, 1, parent, 2 );
  547. model.change( () => {
  548. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  549. expectChanges( [
  550. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  551. ] );
  552. } );
  553. } );
  554. it( 'on a character - case with same characters next to each other', () => {
  555. const parent = root.getChild( 0 );
  556. const range = Range.createFromParentsAndOffsets( parent, 1, parent, 2 );
  557. model.change( () => {
  558. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  559. expectChanges( [
  560. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  561. ] );
  562. } );
  563. } );
  564. it( 'on multiple characters', () => {
  565. const parent = root.getChild( 0 );
  566. const range = Range.createFromParentsAndOffsets( parent, 0, parent, 3 );
  567. model.change( () => {
  568. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  569. expectChanges( [
  570. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  571. ] );
  572. } );
  573. } );
  574. it( 'on multiple consecutive characters in multiple operations', () => {
  575. const parent = root.getChild( 0 );
  576. const range1 = Range.createFromParentsAndOffsets( parent, 1, parent, 2 );
  577. const range2 = Range.createFromParentsAndOffsets( parent, 2, parent, 3 );
  578. model.change( () => {
  579. attribute( range1, attributeKey, attributeOldValue, attributeNewValue );
  580. attribute( range2, attributeKey, attributeOldValue, attributeNewValue );
  581. const range = Range.createFromParentsAndOffsets( parent, 1, parent, 3 );
  582. expectChanges( [
  583. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  584. ] );
  585. } );
  586. } );
  587. it( 'on multiple non-consecutive characters in multiple operations', () => {
  588. const parent = root.getChild( 0 );
  589. const range1 = Range.createFromParentsAndOffsets( parent, 0, parent, 1 );
  590. const range2 = Range.createFromParentsAndOffsets( parent, 2, parent, 3 );
  591. model.change( () => {
  592. // Note "reversed" order of ranges. Further range is changed first.
  593. attribute( range2, attributeKey, attributeOldValue, attributeNewValue );
  594. attribute( range1, attributeKey, attributeOldValue, attributeNewValue );
  595. // Note that changes has been sorted.
  596. expectChanges( [
  597. { type: 'attribute', range: range1, attributeKey, attributeOldValue, attributeNewValue },
  598. { type: 'attribute', range: range2, attributeKey, attributeOldValue, attributeNewValue }
  599. ] );
  600. } );
  601. } );
  602. it( 'on range containing various nodes', () => {
  603. const range = Range.createFromParentsAndOffsets( root, 0, root, 2 );
  604. model.change( () => {
  605. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  606. const p1 = root.getChild( 0 );
  607. const p2 = root.getChild( 1 );
  608. const type = 'attribute';
  609. expectChanges( [
  610. {
  611. type,
  612. range: Range.createFromParentsAndOffsets( root, 0, p1, 0 ),
  613. attributeKey,
  614. attributeOldValue,
  615. attributeNewValue
  616. },
  617. {
  618. type,
  619. range: Range.createFromParentsAndOffsets( p1, 0, p1, 3 ),
  620. attributeKey,
  621. attributeOldValue,
  622. attributeNewValue
  623. },
  624. {
  625. type,
  626. range: Range.createFromParentsAndOffsets( root, 1, p2, 0 ),
  627. attributeKey,
  628. attributeOldValue,
  629. attributeNewValue
  630. },
  631. {
  632. type,
  633. range: Range.createFromParentsAndOffsets( p2, 0, p2, 3 ),
  634. attributeKey,
  635. attributeOldValue,
  636. attributeNewValue
  637. }
  638. ] );
  639. } );
  640. } );
  641. it( 'remove and add attribute on text', () => {
  642. const p = root.getChild( 1 );
  643. p.getChild( 0 ).setAttribute( 'bold', true );
  644. const range = Range.createFromParentsAndOffsets( p, 1, p, 3 );
  645. model.change( () => {
  646. attribute( range, 'bold', true, null );
  647. attribute( range, 'italic', null, true );
  648. const range1 = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  649. const range2 = Range.createFromParentsAndOffsets( p, 2, p, 3 );
  650. // Attribute change glueing does not work 100% correct.
  651. expectChanges( [
  652. {
  653. type: 'attribute',
  654. range: range1,
  655. attributeKey: 'bold',
  656. attributeOldValue: true,
  657. attributeNewValue: null
  658. },
  659. {
  660. type: 'attribute',
  661. range: range1,
  662. attributeKey: 'italic',
  663. attributeOldValue: null,
  664. attributeNewValue: true
  665. },
  666. {
  667. type: 'attribute',
  668. range: range2,
  669. attributeKey: 'bold',
  670. attributeOldValue: true,
  671. attributeNewValue: null
  672. },
  673. {
  674. type: 'attribute',
  675. range: range2,
  676. attributeKey: 'italic',
  677. attributeOldValue: null,
  678. attributeNewValue: true
  679. }
  680. ] );
  681. } );
  682. } );
  683. it( 'on some old nodes and inserted nodes', () => {
  684. const position = new Position( root, [ 0, 1 ] );
  685. const p1 = root.getChild( 0 );
  686. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 2 );
  687. model.change( () => {
  688. insert( new Text( 'xx' ), position );
  689. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  690. const rangeBefore = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  691. expectChanges( [
  692. { type: 'attribute', range: rangeBefore, attributeKey, attributeOldValue, attributeNewValue },
  693. { type: 'insert', name: '$text', length: 2, position }
  694. ] );
  695. } );
  696. } );
  697. it( 'only on inserted nodes', () => {
  698. const position = new Position( root, [ 0, 1 ] );
  699. const p1 = root.getChild( 0 );
  700. const range = Range.createFromParentsAndOffsets( p1, 2, p1, 3 );
  701. model.change( () => {
  702. insert( new Text( 'xxx' ), position );
  703. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  704. expectChanges( [
  705. { type: 'insert', name: '$text', length: 3, position }
  706. ] );
  707. } );
  708. } );
  709. it( 'on some inserted nodes and old nodes', () => {
  710. const position = new Position( root, [ 0, 1 ] );
  711. const p1 = root.getChild( 0 );
  712. const range = Range.createFromParentsAndOffsets( p1, 2, p1, 4 );
  713. model.change( () => {
  714. insert( new Text( 'xx' ), position );
  715. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  716. const rangeAfter = Range.createFromParentsAndOffsets( p1, 3, p1, 4 );
  717. expectChanges( [
  718. { type: 'insert', name: '$text', length: 2, position },
  719. { type: 'attribute', range: rangeAfter, attributeKey, attributeOldValue, attributeNewValue }
  720. ] );
  721. } );
  722. } );
  723. it( 'over all inserted nodes and some old nodes', () => {
  724. const position = new Position( root, [ 0, 1 ] );
  725. const p1 = root.getChild( 0 );
  726. const range = Range.createFromParentsAndOffsets( p1, 0, p1, 4 );
  727. model.change( () => {
  728. insert( new Text( 'xx' ), position );
  729. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  730. const rangeBefore = Range.createFromParentsAndOffsets( p1, 0, p1, 1 );
  731. const rangeAfter = Range.createFromParentsAndOffsets( p1, 3, p1, 4 );
  732. expectChanges( [
  733. { type: 'attribute', range: rangeBefore, attributeKey, attributeOldValue, attributeNewValue },
  734. { type: 'insert', name: '$text', length: 2, position },
  735. { type: 'attribute', range: rangeAfter, attributeKey, attributeOldValue, attributeNewValue }
  736. ] );
  737. } );
  738. } );
  739. it( 'on some not changed and some changed nodes', () => {
  740. const p = root.getChild( 0 );
  741. const rangeA = Range.createFromParentsAndOffsets( p, 1, p, 3 );
  742. const rangeB = Range.createFromParentsAndOffsets( p, 0, p, 2 );
  743. model.change( () => {
  744. attribute( rangeA, 'a', null, true );
  745. attribute( rangeB, 'b', null, true );
  746. const type = 'attribute';
  747. const attributeOldValue = null;
  748. const attributeNewValue = true;
  749. // Attribute change glueing does not work 100% correct.
  750. expectChanges( [
  751. {
  752. type,
  753. range: Range.createFromParentsAndOffsets( p, 0, p, 1 ),
  754. attributeKey: 'b',
  755. attributeOldValue,
  756. attributeNewValue
  757. },
  758. {
  759. type,
  760. range: Range.createFromParentsAndOffsets( p, 1, p, 2 ),
  761. attributeKey: 'a',
  762. attributeOldValue,
  763. attributeNewValue
  764. },
  765. {
  766. type,
  767. range: Range.createFromParentsAndOffsets( p, 1, p, 2 ),
  768. attributeKey: 'b',
  769. attributeOldValue,
  770. attributeNewValue
  771. },
  772. {
  773. type,
  774. range: Range.createFromParentsAndOffsets( p, 2, p, 3 ),
  775. attributeKey: 'a',
  776. attributeOldValue,
  777. attributeNewValue
  778. }
  779. ] );
  780. } );
  781. } );
  782. it( 'on already changed nodes', () => {
  783. const p = root.getChild( 1 );
  784. const rangeA = Range.createFromParentsAndOffsets( p, 0, p, 3 );
  785. const rangeB = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  786. model.change( () => {
  787. attribute( rangeA, 'a', null, true );
  788. attribute( rangeB, 'b', null, true );
  789. const type = 'attribute';
  790. const attributeOldValue = null;
  791. const attributeNewValue = true;
  792. // Attribute change glueing does not work 100% correct.
  793. expectChanges( [
  794. {
  795. type,
  796. range: Range.createFromParentsAndOffsets( p, 0, p, 2 ),
  797. attributeKey: 'a',
  798. attributeOldValue,
  799. attributeNewValue
  800. },
  801. {
  802. type,
  803. range: Range.createFromParentsAndOffsets( p, 1, p, 2 ),
  804. attributeKey: 'b',
  805. attributeOldValue,
  806. attributeNewValue
  807. },
  808. {
  809. type,
  810. range: Range.createFromParentsAndOffsets( p, 2, p, 3 ),
  811. attributeKey: 'a',
  812. attributeOldValue,
  813. attributeNewValue
  814. }
  815. ] );
  816. } );
  817. } );
  818. it( 'on some changed and some not changed nodes', () => {
  819. const p = root.getChild( 1 );
  820. const rangeA = Range.createFromParentsAndOffsets( p, 0, p, 2 );
  821. const rangeB = Range.createFromParentsAndOffsets( p, 1, p, 3 );
  822. model.change( () => {
  823. attribute( rangeA, 'a', null, true );
  824. attribute( rangeB, 'b', null, true );
  825. const type = 'attribute';
  826. const attributeOldValue = null;
  827. const attributeNewValue = true;
  828. expectChanges( [
  829. {
  830. type,
  831. range: Range.createFromParentsAndOffsets( p, 0, p, 2 ),
  832. attributeKey: 'a',
  833. attributeOldValue,
  834. attributeNewValue
  835. },
  836. {
  837. type,
  838. range: Range.createFromParentsAndOffsets( p, 1, p, 3 ),
  839. attributeKey: 'b',
  840. attributeOldValue,
  841. attributeNewValue
  842. }
  843. ] );
  844. } );
  845. } );
  846. it( 'over all changed nodes and some not changed nodes', () => {
  847. const p = root.getChild( 0 );
  848. const rangeA = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  849. const rangeB = Range.createFromParentsAndOffsets( p, 0, p, 3 );
  850. model.change( () => {
  851. attribute( rangeA, 'a', null, true );
  852. attribute( rangeB, 'b', null, true );
  853. const type = 'attribute';
  854. const attributeOldValue = null;
  855. const attributeNewValue = true;
  856. // Attribute change glueing does not work 100% correct.
  857. expectChanges( [
  858. {
  859. type,
  860. range: Range.createFromParentsAndOffsets( p, 0, p, 1 ),
  861. attributeKey: 'b',
  862. attributeOldValue,
  863. attributeNewValue
  864. },
  865. {
  866. type,
  867. range: Range.createFromParentsAndOffsets( p, 1, p, 2 ),
  868. attributeKey: 'a',
  869. attributeOldValue,
  870. attributeNewValue
  871. },
  872. {
  873. type,
  874. range: Range.createFromParentsAndOffsets( p, 1, p, 3 ),
  875. attributeKey: 'b',
  876. attributeOldValue,
  877. attributeNewValue
  878. }
  879. ] );
  880. } );
  881. } );
  882. } );
  883. describe( 'markers', () => {
  884. let range, rangeB;
  885. beforeEach( () => {
  886. range = Range.createFromParentsAndOffsets( root, 0, root, 1 );
  887. rangeB = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  888. } );
  889. it( 'add marker', () => {
  890. differ.bufferMarkerChange( 'name', null, range );
  891. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  892. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  893. { name: 'name', range }
  894. ] );
  895. } );
  896. it( 'remove marker', () => {
  897. differ.bufferMarkerChange( 'name', range, null );
  898. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  899. { name: 'name', range }
  900. ] );
  901. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  902. } );
  903. it( 'change marker', () => {
  904. differ.bufferMarkerChange( 'name', range, rangeB );
  905. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  906. { name: 'name', range }
  907. ] );
  908. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  909. { name: 'name', range: rangeB }
  910. ] );
  911. } );
  912. it( 'add marker and remove it', () => {
  913. differ.bufferMarkerChange( 'name', null, range );
  914. differ.bufferMarkerChange( 'name', range, null );
  915. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  916. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  917. } );
  918. it( 'add marker and change it', () => {
  919. differ.bufferMarkerChange( 'name', null, range );
  920. differ.bufferMarkerChange( 'name', range, rangeB );
  921. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  922. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  923. { name: 'name', range: rangeB }
  924. ] );
  925. } );
  926. it( 'change marker and remove it', () => {
  927. differ.bufferMarkerChange( 'name', range, rangeB );
  928. differ.bufferMarkerChange( 'name', rangeB, null );
  929. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  930. { name: 'name', range }
  931. ] );
  932. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  933. } );
  934. it( 'remove marker and add it at same range', () => {
  935. differ.bufferMarkerChange( 'name', range, null );
  936. differ.bufferMarkerChange( 'name', null, range );
  937. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  938. { name: 'name', range }
  939. ] );
  940. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  941. { name: 'name', range }
  942. ] );
  943. } );
  944. it( 'change marker to the same range', () => {
  945. differ.bufferMarkerChange( 'name', range, range );
  946. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  947. { name: 'name', range }
  948. ] );
  949. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  950. { name: 'name', range }
  951. ] );
  952. } );
  953. } );
  954. describe( 'other cases', () => {
  955. // #1309.
  956. it( 'multiple inserts and removes in one element', () => {
  957. model.change( () => {
  958. insert( new Text( 'x' ), new Position( root, [ 0, 2 ] ) );
  959. insert( new Text( 'x' ), new Position( root, [ 0, 3 ] ) );
  960. move( new Position( root, [ 0, 2 ] ), 1, new Position( root, [ 1, 0 ] ) );
  961. expectChanges( [
  962. { type: 'insert', name: '$text', length: 1, position: new Position( root, [ 0, 2 ] ) },
  963. { type: 'insert', name: '$text', length: 1, position: new Position( root, [ 1, 0 ] ) }
  964. ] );
  965. } );
  966. } );
  967. } );
  968. describe( 'getChanges()', () => {
  969. let position, p1, rangeAttrChange, range;
  970. beforeEach( () => {
  971. position = new Position( root, [ 0, 1 ] );
  972. p1 = root.getChild( 0 );
  973. range = Range.createFromParentsAndOffsets( p1, 2, p1, 4 );
  974. rangeAttrChange = Range.createFromParentsAndOffsets( p1, 3, p1, 4 );
  975. } );
  976. it( 'should return changes in graveyard if a flag was set up', () => {
  977. const removePosition = new Position( root, [ 1 ] );
  978. model.change( () => {
  979. insert( new Text( 'xx' ), position );
  980. attribute( range, 'key', null, 'foo' );
  981. remove( removePosition, 1 );
  982. expectChanges( [
  983. { type: 'insert', name: 'paragraph', length: 1, position: new Position( doc.graveyard, [ 0 ] ) },
  984. { type: 'insert', name: '$text', length: 2, position },
  985. {
  986. type: 'attribute',
  987. range: rangeAttrChange,
  988. attributeKey: 'key',
  989. attributeOldValue: null,
  990. attributeNewValue: 'foo'
  991. },
  992. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  993. ], true );
  994. } );
  995. } );
  996. // Below tests test caching.
  997. it( 'should return same change set if was called twice in a row', () => {
  998. model.change( () => {
  999. insert( new Text( 'xx' ), position );
  1000. attribute( range, 'key', null, 'foo' );
  1001. const changesA = differ.getChanges();
  1002. const changesB = differ.getChanges();
  1003. expect( changesA ).to.deep.equal( changesB );
  1004. } );
  1005. } );
  1006. it( 'should return same change set if was called twice in a row - graveyard changes', () => {
  1007. model.change( () => {
  1008. insert( new Text( 'xx' ), position );
  1009. attribute( range, 'key', null, 'foo' );
  1010. const removePosition = new Position( root, [ 1 ] );
  1011. remove( removePosition, 1 );
  1012. const changesA = differ.getChanges( { includeChangesInGraveyard: true } );
  1013. const changesB = differ.getChanges( { includeChangesInGraveyard: true } );
  1014. expect( changesA ).to.deep.equal( changesB );
  1015. } );
  1016. } );
  1017. it( 'should return correct changes if change happened between getChanges() calls', () => {
  1018. model.change( () => {
  1019. insert( new Text( 'xx' ), position );
  1020. attribute( range, 'key', null, 'foo' );
  1021. expectChanges( [
  1022. { type: 'insert', name: '$text', length: 2, position },
  1023. {
  1024. type: 'attribute',
  1025. range: rangeAttrChange,
  1026. attributeKey: 'key',
  1027. attributeOldValue: null,
  1028. attributeNewValue: 'foo'
  1029. }
  1030. ] );
  1031. const removePosition = new Position( root, [ 1 ] );
  1032. remove( removePosition, 1 );
  1033. expectChanges( [
  1034. { type: 'insert', name: '$text', length: 2, position },
  1035. {
  1036. type: 'attribute',
  1037. range: rangeAttrChange,
  1038. attributeKey: 'key',
  1039. attributeOldValue: null,
  1040. attributeNewValue: 'foo'
  1041. },
  1042. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  1043. ] );
  1044. } );
  1045. } );
  1046. it( 'should return correct changes if reset happened between getChanges() calls', () => {
  1047. model.change( () => {
  1048. insert( new Text( 'xx' ), position );
  1049. attribute( range, 'key', null, 'foo' );
  1050. expectChanges( [
  1051. { type: 'insert', name: '$text', length: 2, position },
  1052. {
  1053. type: 'attribute',
  1054. range: rangeAttrChange,
  1055. attributeKey: 'key',
  1056. attributeOldValue: null,
  1057. attributeNewValue: 'foo'
  1058. }
  1059. ] );
  1060. differ.reset();
  1061. const removePosition = new Position( root, [ 1 ] );
  1062. remove( removePosition, 1 );
  1063. expectChanges( [
  1064. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  1065. ] );
  1066. } );
  1067. } );
  1068. } );
  1069. function insert( item, position ) {
  1070. const operation = new InsertOperation( position, item, doc.version );
  1071. model.applyOperation( wrapInDelta( operation ) );
  1072. }
  1073. function remove( sourcePosition, howMany ) {
  1074. const targetPosition = Position.createAt( doc.graveyard, doc.graveyard.maxOffset );
  1075. const operation = new RemoveOperation( sourcePosition, howMany, targetPosition, doc.version );
  1076. model.applyOperation( wrapInDelta( operation ) );
  1077. }
  1078. function move( sourcePosition, howMany, targetPosition ) {
  1079. const operation = new MoveOperation( sourcePosition, howMany, targetPosition, doc.version );
  1080. model.applyOperation( wrapInDelta( operation ) );
  1081. }
  1082. function rename( element, newName ) {
  1083. const operation = new RenameOperation( Position.createBefore( element ), element.name, newName, doc.version );
  1084. model.applyOperation( wrapInDelta( operation ) );
  1085. }
  1086. function attribute( range, key, oldValue, newValue ) {
  1087. const operation = new AttributeOperation( range, key, oldValue, newValue, doc.version );
  1088. model.applyOperation( wrapInDelta( operation ) );
  1089. }
  1090. function expectChanges( expected, includeChangesInGraveyard = false ) {
  1091. const changes = differ.getChanges( { includeChangesInGraveyard } );
  1092. for ( let i = 0; i < expected.length; i++ ) {
  1093. for ( const key in expected[ i ] ) {
  1094. if ( expected[ i ].hasOwnProperty( key ) ) {
  1095. if ( key == 'position' || key == 'range' ) {
  1096. expect( changes[ i ][ key ].isEqual( expected[ i ][ key ] ), `item ${ i }, key "${ key }"` ).to.be.true;
  1097. } else {
  1098. expect( changes[ i ][ key ], `item ${ i }, key "${ key }"` ).to.equal( expected[ i ][ key ] );
  1099. }
  1100. }
  1101. }
  1102. }
  1103. }
  1104. } );