differ.js 58 KB

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