differ.js 49 KB

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