differ.js 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../src/model/model';
  6. import Element from '../../src/model/element';
  7. import Text from '../../src/model/text';
  8. import Position from '../../src/model/position';
  9. import Range from '../../src/model/range';
  10. import InsertOperation from '../../src/model/operation/insertoperation';
  11. import MoveOperation from '../../src/model/operation/moveoperation';
  12. import RenameOperation from '../../src/model/operation/renameoperation';
  13. import AttributeOperation from '../../src/model/operation/attributeoperation';
  14. import SplitOperation from '../../src/model/operation/splitoperation';
  15. import MergeOperation from '../../src/model/operation/mergeoperation';
  16. 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 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( 'over all changed nodes and some not changed nodes', () => {
  1008. const p = root.getChild( 0 );
  1009. const rangeA = new Range( Position._createAt( p, 1 ), Position._createAt( p, 2 ) );
  1010. const rangeB = new Range( Position._createAt( p, 0 ), Position._createAt( p, 3 ) );
  1011. model.change( () => {
  1012. attribute( rangeA, 'a', null, true );
  1013. attribute( rangeB, 'b', null, true );
  1014. const type = 'attribute';
  1015. const attributeOldValue = null;
  1016. const attributeNewValue = true;
  1017. // Attribute change glueing does not work 100% correct.
  1018. expectChanges( [
  1019. {
  1020. type,
  1021. range: new Range( Position._createAt( p, 0 ), Position._createAt( p, 1 ) ),
  1022. attributeKey: 'b',
  1023. attributeOldValue,
  1024. attributeNewValue
  1025. },
  1026. {
  1027. type,
  1028. range: new Range( Position._createAt( p, 1 ), Position._createAt( p, 2 ) ),
  1029. attributeKey: 'a',
  1030. attributeOldValue,
  1031. attributeNewValue
  1032. },
  1033. {
  1034. type,
  1035. range: new Range( Position._createAt( p, 1 ), Position._createAt( p, 3 ) ),
  1036. attributeKey: 'b',
  1037. attributeOldValue,
  1038. attributeNewValue
  1039. }
  1040. ] );
  1041. } );
  1042. } );
  1043. } );
  1044. describe( 'split', () => {
  1045. it( 'split an element', () => {
  1046. const position = new Position( root, [ 0, 2 ] );
  1047. model.change( () => {
  1048. split( position );
  1049. expectChanges( [
  1050. { type: 'remove', name: '$text', length: 1, position },
  1051. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1052. ] );
  1053. } );
  1054. } );
  1055. it( 'split a new element', () => {
  1056. model.change( () => {
  1057. insert(
  1058. new Element( 'paragraph', null, new Text( 'Ab' ) ),
  1059. new Position( root, [ 0 ] )
  1060. );
  1061. split( new Position( root, [ 0, 1 ] ) );
  1062. expectChanges( [
  1063. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  1064. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1065. ] );
  1066. } );
  1067. } );
  1068. it( 'split an element inside a new element', () => {
  1069. model.change( () => {
  1070. insert(
  1071. new Element( 'blockQuote', null, new Element( 'paragraph', null, new Text( 'Ab' ) ) ),
  1072. new Position( root, [ 0 ] )
  1073. );
  1074. split( new Position( root, [ 0, 0, 1 ] ) );
  1075. expectChanges( [
  1076. { type: 'insert', name: 'blockQuote', length: 1, position: new Position( root, [ 0 ] ) }
  1077. ] );
  1078. } );
  1079. } );
  1080. it( 'should correctly mark a change in graveyard', () => {
  1081. model.change( () => {
  1082. merge( new Position( root, [ 1, 0 ] ), new Position( root, [ 0, 3 ] ) );
  1083. } );
  1084. model.change( () => {
  1085. const position = new Position( root, [ 0, 3 ] );
  1086. const operation = new SplitOperation( position, 3, new Position( doc.graveyard, [ 0 ] ), doc.version );
  1087. model.applyOperation( operation );
  1088. expectChanges( [
  1089. { type: 'remove', name: 'paragraph', length: 1, position: new Position( doc.graveyard, [ 0 ] ) },
  1090. { type: 'remove', name: '$text', length: 3, position: new Position( root, [ 0, 3 ] ) },
  1091. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1092. ], true );
  1093. } );
  1094. } );
  1095. } );
  1096. describe( 'merge', () => {
  1097. it( 'merge two elements', () => {
  1098. model.change( () => {
  1099. merge( new Position( root, [ 1, 0 ] ), new Position( root, [ 0, 3 ] ) );
  1100. expectChanges( [
  1101. { type: 'insert', name: '$text', length: 3, position: new Position( root, [ 0, 3 ] ) },
  1102. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1103. ] );
  1104. } );
  1105. } );
  1106. it( 'merge a new element', () => {
  1107. model.change( () => {
  1108. insert( new Element( 'paragraph', null, new Text( 'Ab' ) ), new Position( root, [ 1 ] ) );
  1109. merge( new Position( root, [ 1, 0 ] ), new Position( root, [ 0, 3 ] ) );
  1110. expectChanges( [
  1111. { type: 'insert', name: '$text', length: 2, position: new Position( root, [ 0, 3 ] ) }
  1112. ] );
  1113. } );
  1114. } );
  1115. it( 'merge into a new element', () => {
  1116. model.change( () => {
  1117. insert( new Element( 'paragraph', null, new Text( 'Ab' ) ), new Position( root, [ 0 ] ) );
  1118. merge( new Position( root, [ 1, 0 ] ), new Position( root, [ 0, 2 ] ) );
  1119. expectChanges( [
  1120. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  1121. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1122. ] );
  1123. } );
  1124. } );
  1125. it( 'merge elements inside a new element', () => {
  1126. model.change( () => {
  1127. insert(
  1128. new Element( 'blockQuote', null, [
  1129. new Element( 'paragraph', null, new Text( 'Ab' ) ),
  1130. new Element( 'paragraph', null, new Text( 'Xyz' ) )
  1131. ] ),
  1132. new Position( root, [ 0 ] )
  1133. );
  1134. merge( new Position( root, [ 0, 1, 0 ] ), new Position( root, [ 0, 0, 2 ] ) );
  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. expectChanges( [
  1144. { type: 'insert', name: 'paragraph', length: 1, position: new Position( doc.graveyard, [ 0 ] ) },
  1145. { type: 'insert', name: '$text', length: 3, position: new Position( root, [ 0, 3 ] ) },
  1146. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1147. ], true );
  1148. } );
  1149. } );
  1150. } );
  1151. describe( 'markers', () => {
  1152. let range, rangeB;
  1153. beforeEach( () => {
  1154. range = new Range( Position._createAt( root, 0 ), Position._createAt( root, 1 ) );
  1155. rangeB = new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) );
  1156. } );
  1157. it( 'add marker', () => {
  1158. differ.bufferMarkerChange( 'name', null, range, true );
  1159. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  1160. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  1161. { name: 'name', range }
  1162. ] );
  1163. expect( differ.getChangedMarkers() ).to.deep.equal( [
  1164. {
  1165. name: 'name',
  1166. data: {
  1167. oldRange: null,
  1168. newRange: range
  1169. }
  1170. }
  1171. ] );
  1172. } );
  1173. it( 'remove marker', () => {
  1174. differ.bufferMarkerChange( 'name', range, null, true );
  1175. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  1176. { name: 'name', range }
  1177. ] );
  1178. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  1179. expect( differ.getChangedMarkers() ).to.deep.equal( [
  1180. {
  1181. name: 'name',
  1182. data: {
  1183. oldRange: range,
  1184. newRange: null
  1185. }
  1186. }
  1187. ] );
  1188. } );
  1189. it( 'change marker\'s range', () => {
  1190. differ.bufferMarkerChange( 'name', range, rangeB, true );
  1191. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  1192. { name: 'name', range }
  1193. ] );
  1194. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  1195. { name: 'name', range: rangeB }
  1196. ] );
  1197. expect( differ.getChangedMarkers() ).to.deep.equal( [
  1198. {
  1199. name: 'name',
  1200. data: {
  1201. oldRange: range,
  1202. newRange: rangeB
  1203. }
  1204. }
  1205. ] );
  1206. } );
  1207. it( 'add marker not affecting data', () => {
  1208. differ.bufferMarkerChange( 'name', range, rangeB, false );
  1209. expect( differ.hasDataChanges() ).to.be.false;
  1210. } );
  1211. it( 'add marker affecting data', () => {
  1212. differ.bufferMarkerChange( 'name', range, rangeB, true );
  1213. expect( differ.hasDataChanges() ).to.be.true;
  1214. } );
  1215. it( 'add marker and remove it', () => {
  1216. differ.bufferMarkerChange( 'name', null, range, true );
  1217. differ.bufferMarkerChange( 'name', range, null, true );
  1218. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  1219. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  1220. expect( differ.getChangedMarkers() ).to.deep.equal( [] );
  1221. expect( differ.hasDataChanges() ).to.be.false;
  1222. } );
  1223. it( 'add marker and change it', () => {
  1224. differ.bufferMarkerChange( 'name', null, range, true );
  1225. differ.bufferMarkerChange( 'name', range, rangeB, true );
  1226. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  1227. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  1228. { name: 'name', range: rangeB }
  1229. ] );
  1230. expect( differ.getChangedMarkers() ).to.deep.equal( [
  1231. {
  1232. name: 'name',
  1233. data: {
  1234. oldRange: null,
  1235. newRange: rangeB
  1236. }
  1237. }
  1238. ] );
  1239. } );
  1240. it( 'change marker to not affecting data', () => {
  1241. differ.bufferMarkerChange( 'name', range, rangeB, true );
  1242. differ.bufferMarkerChange( 'name', range, rangeB, false );
  1243. expect( differ.hasDataChanges() ).to.be.false;
  1244. } );
  1245. it( 'change marker and remove it', () => {
  1246. differ.bufferMarkerChange( 'name', range, rangeB, true );
  1247. differ.bufferMarkerChange( 'name', rangeB, null, true );
  1248. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  1249. { name: 'name', range }
  1250. ] );
  1251. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  1252. expect( differ.getChangedMarkers() ).to.deep.equal( [
  1253. {
  1254. name: 'name',
  1255. data: {
  1256. oldRange: range,
  1257. newRange: null
  1258. }
  1259. }
  1260. ] );
  1261. expect( differ.hasDataChanges() ).to.be.true;
  1262. } );
  1263. it( 'remove marker and add it at same range', () => {
  1264. differ.bufferMarkerChange( 'name', range, null, true );
  1265. differ.bufferMarkerChange( 'name', null, range, true );
  1266. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  1267. { name: 'name', range }
  1268. ] );
  1269. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  1270. { name: 'name', range }
  1271. ] );
  1272. expect( differ.getChangedMarkers() ).to.deep.equal( [
  1273. {
  1274. name: 'name',
  1275. data: {
  1276. oldRange: range,
  1277. newRange: range
  1278. }
  1279. }
  1280. ] );
  1281. } );
  1282. it( 'change marker to the same range', () => {
  1283. differ.bufferMarkerChange( 'name', range, range, true );
  1284. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  1285. { name: 'name', range }
  1286. ] );
  1287. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  1288. { name: 'name', range }
  1289. ] );
  1290. expect( differ.getChangedMarkers() ).to.deep.equal( [
  1291. {
  1292. name: 'name',
  1293. data: {
  1294. oldRange: range,
  1295. newRange: range
  1296. }
  1297. }
  1298. ] );
  1299. } );
  1300. } );
  1301. describe( 'other cases', () => {
  1302. // #1309.
  1303. it( 'multiple inserts and removes in one element', () => {
  1304. model.change( () => {
  1305. insert( new Text( 'x' ), new Position( root, [ 0, 2 ] ) );
  1306. insert( new Text( 'x' ), new Position( root, [ 0, 3 ] ) );
  1307. move( new Position( root, [ 0, 2 ] ), 1, new Position( root, [ 1, 0 ] ) );
  1308. expectChanges( [
  1309. { type: 'insert', name: '$text', length: 1, position: new Position( root, [ 0, 2 ] ) },
  1310. { type: 'insert', name: '$text', length: 1, position: new Position( root, [ 1, 0 ] ) }
  1311. ] );
  1312. } );
  1313. } );
  1314. // ckeditor5#733.
  1315. it( 'proper filtering of changes in removed elements', () => {
  1316. // Before fix there was a buggy scenario described in ckeditor5#733.
  1317. // There was this structure: `<paragraph>foo[</paragraph><image /><blockQuote><p>te]xt</p></blockQuote>`
  1318. // On delete of above selection `image` and `paragraph` inside `blockQuote` are removed (it gets merged).
  1319. // However, since `image` was removed first, when checking if `paragraph` is in a removed element,
  1320. // it appeared that `blockQuote` looks like it is removed because it had the same path as the already removed `<image>`.
  1321. // In a result, removing `paragraph` was discarded.
  1322. // The mistake was that the checking for removing was done at incorrect moment.
  1323. root._removeChildren( 0, root.childCount );
  1324. root._appendChild( [
  1325. new Element( 'paragraph', null, new Text( 'foo' ) ),
  1326. new Element( 'image' ),
  1327. new Element( 'blockQuote', null, [
  1328. new Element( 'paragraph', null, new Text( 'text' ) )
  1329. ] )
  1330. ] );
  1331. model.change( () => {
  1332. // Remove `"te"`.
  1333. remove( new Position( root, [ 2, 0, 0 ] ), 2 );
  1334. // Remove `image` before `blockQuote`.
  1335. remove( new Position( root, [ 1 ] ), 1 );
  1336. // Move `"xt"` to first `paragraph` (merging).
  1337. move( new Position( root, [ 1, 0, 0 ] ), 2, new Position( root, [ 0, 3 ] ) );
  1338. // Remove `paragraph` inside `blockQuote`.
  1339. remove( new Position( root, [ 1, 0 ] ), 1 );
  1340. expectChanges( [
  1341. { type: 'insert', name: '$text', length: 2, position: new Position( root, [ 0, 3 ] ) },
  1342. { type: 'remove', name: 'image', length: 1, position: new Position( root, [ 1 ] ) },
  1343. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1, 0 ] ) }
  1344. ] );
  1345. } );
  1346. } );
  1347. // In this scenario we create a new element, then remove something from before it to mess up with offsets,
  1348. // finally we insert some content into a new element. Since we are inserting into a new element, the
  1349. // inserted children should not be shown on changes list.
  1350. it( 'proper filtering of changes in inserted elements', () => {
  1351. root._removeChildren( 0, root.childCount );
  1352. root._appendChild( new Element( 'image' ) );
  1353. const blockQuote = new Element( 'blockQuote', null, new Element( 'paragraph' ) );
  1354. model.change( () => {
  1355. // Insert `blockQuote` with `paragraph` after `image`.
  1356. insert( blockQuote, new Position( root, [ 1 ] ) );
  1357. // Remove `image` from before `blockQuote`.
  1358. remove( new Position( root, [ 0 ] ), 1 );
  1359. // Insert text into `paragraph` in `blockQuote`.
  1360. insert( new Text( 'foo' ), new Position( root, [ 0, 0, 0 ] ) );
  1361. expectChanges( [
  1362. { type: 'remove', name: 'image', length: 1, position: new Position( root, [ 0 ] ) },
  1363. { type: 'insert', name: 'blockQuote', length: 1, position: new Position( root, [ 0 ] ) }
  1364. ] );
  1365. } );
  1366. } );
  1367. // In this scenario we create a new element, then move another element that was before the new element into
  1368. // the new element. This way we mess up with offsets and insert content into a new element in one operation.
  1369. // Since we are inserting into a new element, the insertion of moved element should not be shown on changes list.
  1370. it( 'proper filtering of changes in inserted elements #2', () => {
  1371. root._removeChildren( 0, root.childCount );
  1372. root._appendChild( new Element( 'image' ) );
  1373. model.change( () => {
  1374. // Insert `div` after `image`.
  1375. insert( new Element( 'div' ), new Position( root, [ 1 ] ) );
  1376. // Move `image` to the new `div`.
  1377. move( new Position( root, [ 0 ] ), 1, new Position( root, [ 1, 0 ] ) );
  1378. expectChanges( [
  1379. { type: 'remove', name: 'image', length: 1, position: new Position( root, [ 0 ] ) },
  1380. { type: 'insert', name: 'div', length: 1, position: new Position( root, [ 0 ] ) }
  1381. ] );
  1382. } );
  1383. } );
  1384. // #1392.
  1385. it( 'remove is correctly transformed by multiple affecting changes', () => {
  1386. root._appendChild( new Element( 'paragraph', null, new Text( 'xyz' ) ) );
  1387. model.change( writer => {
  1388. rename( root.getChild( 1 ), 'heading' );
  1389. rename( root.getChild( 2 ), 'heading' );
  1390. remove( writer.createPositionAt( root, 0 ), 3 );
  1391. expectChanges( [
  1392. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  1393. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  1394. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) }
  1395. ] );
  1396. } );
  1397. } );
  1398. } );
  1399. describe( 'getChanges()', () => {
  1400. let position, p1, rangeAttrChange, range;
  1401. beforeEach( () => {
  1402. position = new Position( root, [ 0, 1 ] );
  1403. p1 = root.getChild( 0 );
  1404. range = new Range( Position._createAt( p1, 2 ), Position._createAt( p1, 4 ) );
  1405. rangeAttrChange = new Range( Position._createAt( p1, 3 ), Position._createAt( p1, 4 ) );
  1406. } );
  1407. it( 'should return changes in graveyard if a flag was set up', () => {
  1408. const removePosition = new Position( root, [ 1 ] );
  1409. model.change( () => {
  1410. insert( new Text( 'xx' ), position );
  1411. attribute( range, 'key', null, 'foo' );
  1412. remove( removePosition, 1 );
  1413. expectChanges( [
  1414. { type: 'insert', name: 'paragraph', length: 1, position: new Position( doc.graveyard, [ 0 ] ) },
  1415. { type: 'insert', name: '$text', length: 2, position },
  1416. {
  1417. type: 'attribute',
  1418. range: rangeAttrChange,
  1419. attributeKey: 'key',
  1420. attributeOldValue: null,
  1421. attributeNewValue: 'foo'
  1422. },
  1423. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  1424. ], true );
  1425. } );
  1426. } );
  1427. // Below tests test caching.
  1428. it( 'should return same change set if was called twice in a row', () => {
  1429. model.change( () => {
  1430. insert( new Text( 'xx' ), position );
  1431. attribute( range, 'key', null, 'foo' );
  1432. const changesA = differ.getChanges();
  1433. const changesB = differ.getChanges();
  1434. expect( changesA ).to.deep.equal( changesB );
  1435. } );
  1436. } );
  1437. it( 'should return same change set if was called twice in a row - graveyard changes', () => {
  1438. model.change( () => {
  1439. insert( new Text( 'xx' ), position );
  1440. attribute( range, 'key', null, 'foo' );
  1441. const removePosition = new Position( root, [ 1 ] );
  1442. remove( removePosition, 1 );
  1443. const changesA = differ.getChanges( { includeChangesInGraveyard: true } );
  1444. const changesB = differ.getChanges( { includeChangesInGraveyard: true } );
  1445. expect( changesA ).to.deep.equal( changesB );
  1446. } );
  1447. } );
  1448. it( 'should return correct changes if change happened between getChanges() calls', () => {
  1449. model.change( () => {
  1450. insert( new Text( 'xx' ), position );
  1451. attribute( range, 'key', null, 'foo' );
  1452. expectChanges( [
  1453. { type: 'insert', name: '$text', length: 2, position },
  1454. {
  1455. type: 'attribute',
  1456. range: rangeAttrChange,
  1457. attributeKey: 'key',
  1458. attributeOldValue: null,
  1459. attributeNewValue: 'foo'
  1460. }
  1461. ] );
  1462. const removePosition = new Position( root, [ 1 ] );
  1463. remove( removePosition, 1 );
  1464. expectChanges( [
  1465. { type: 'insert', name: '$text', length: 2, position },
  1466. {
  1467. type: 'attribute',
  1468. range: rangeAttrChange,
  1469. attributeKey: 'key',
  1470. attributeOldValue: null,
  1471. attributeNewValue: 'foo'
  1472. },
  1473. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  1474. ] );
  1475. } );
  1476. } );
  1477. it( 'should return correct changes if reset happened between getChanges() calls', () => {
  1478. model.change( () => {
  1479. insert( new Text( 'xx' ), position );
  1480. attribute( range, 'key', null, 'foo' );
  1481. expectChanges( [
  1482. { type: 'insert', name: '$text', length: 2, position },
  1483. {
  1484. type: 'attribute',
  1485. range: rangeAttrChange,
  1486. attributeKey: 'key',
  1487. attributeOldValue: null,
  1488. attributeNewValue: 'foo'
  1489. }
  1490. ] );
  1491. differ.reset();
  1492. const removePosition = new Position( root, [ 1 ] );
  1493. remove( removePosition, 1 );
  1494. expectChanges( [
  1495. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  1496. ] );
  1497. } );
  1498. } );
  1499. } );
  1500. function insert( item, position ) {
  1501. const operation = new InsertOperation( position, item, doc.version );
  1502. model.applyOperation( operation );
  1503. }
  1504. function split( position ) {
  1505. const howMany = position.parent.maxOffset - position.offset;
  1506. const operation = new SplitOperation( position, howMany, null, doc.version );
  1507. model.applyOperation( operation );
  1508. }
  1509. function merge( source, target ) {
  1510. const howMany = source.parent.maxOffset;
  1511. const operation = new MergeOperation( source, howMany, target, new Position( doc.graveyard, [ 0 ] ), doc.version );
  1512. model.applyOperation( operation );
  1513. }
  1514. function remove( sourcePosition, howMany ) {
  1515. const targetPosition = Position._createAt( doc.graveyard, doc.graveyard.maxOffset );
  1516. const operation = new MoveOperation( sourcePosition, howMany, targetPosition, doc.version );
  1517. model.applyOperation( operation );
  1518. }
  1519. function move( sourcePosition, howMany, targetPosition ) {
  1520. const operation = new MoveOperation( sourcePosition, howMany, targetPosition, doc.version );
  1521. model.applyOperation( operation );
  1522. }
  1523. function rename( element, newName ) {
  1524. const operation = new RenameOperation( Position._createBefore( element ), element.name, newName, doc.version );
  1525. model.applyOperation( operation );
  1526. }
  1527. function attribute( range, key, oldValue, newValue ) {
  1528. const operation = new AttributeOperation( range, key, oldValue, newValue, doc.version );
  1529. model.applyOperation( operation );
  1530. }
  1531. function expectChanges( expected, includeChangesInGraveyard = false ) {
  1532. const changes = differ.getChanges( { includeChangesInGraveyard } );
  1533. expect( changes.length ).to.equal( expected.length );
  1534. for ( let i = 0; i < expected.length; i++ ) {
  1535. for ( const key in expected[ i ] ) {
  1536. if ( expected[ i ].hasOwnProperty( key ) ) {
  1537. if ( key == 'position' || key == 'range' ) {
  1538. expect( changes[ i ][ key ].isEqual( expected[ i ][ key ] ), `item ${ i }, key "${ key }"` ).to.be.true;
  1539. } else {
  1540. expect( changes[ i ][ key ], `item ${ i }, key "${ key }"` ).to.equal( expected[ i ][ key ] );
  1541. }
  1542. }
  1543. }
  1544. }
  1545. }
  1546. } );