8
0

differ.js 66 KB

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