differ.js 58 KB

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