differ.js 45 KB

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