8
0

differ.js 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../src/model/model';
  6. import Element from '../../src/model/element';
  7. import Text from '../../src/model/text';
  8. import Position from '../../src/model/position';
  9. import Range from '../../src/model/range';
  10. import InsertOperation from '../../src/model/operation/insertoperation';
  11. import MoveOperation from '../../src/model/operation/moveoperation';
  12. import RenameOperation from '../../src/model/operation/renameoperation';
  13. import AttributeOperation from '../../src/model/operation/attributeoperation';
  14. import SplitOperation from '../../src/model/operation/splitoperation';
  15. import MergeOperation from '../../src/model/operation/mergeoperation';
  16. 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. } );
  508. describe( 'rename', () => {
  509. it( 'an element', () => {
  510. model.change( () => {
  511. rename( root.getChild( 1 ), 'listItem' );
  512. expectChanges( [
  513. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) },
  514. { type: 'insert', name: 'listItem', length: 1, position: new Position( root, [ 1 ] ) }
  515. ] );
  516. } );
  517. } );
  518. it( 'inside a new element', () => {
  519. // Since the rename is inside a new element, it should not be listed on changes list.
  520. model.change( () => {
  521. insert( new Element( 'blockQuote', null, new Element( 'paragraph' ) ), new Position( root, [ 2 ] ) );
  522. rename( root.getChild( 2 ).getChild( 0 ), 'listItem' );
  523. expectChanges( [
  524. { type: 'insert', name: 'blockQuote', length: 1, position: new Position( root, [ 2 ] ) }
  525. ] );
  526. } );
  527. } );
  528. it( 'markers refreshing', () => {
  529. // Since rename is "translated" to remove+insert pair, we need to refresh all the markers that
  530. // intersect with the renamed element.
  531. model.change( () => {
  532. // Renamed element contains marker.
  533. model.markers._set( 'markerA', new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) );
  534. // Marker contains renamed element.
  535. model.markers._set( 'markerB', new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ) );
  536. // Intersecting.
  537. model.markers._set( 'markerC', new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 2 ] ) ) );
  538. // Not intersecting.
  539. model.markers._set( 'markerD', new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 1 ] ) ) );
  540. } );
  541. const markersToRefresh = [ 'markerA', 'markerB', 'markerC' ];
  542. model.change( () => {
  543. rename( root.getChild( 1 ), 'listItem' );
  544. expectChanges( [
  545. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) },
  546. { type: 'insert', name: 'listItem', length: 1, position: new Position( root, [ 1 ] ) }
  547. ] );
  548. const markersToRemove = differ.getMarkersToRemove().map( entry => entry.name );
  549. const markersToAdd = differ.getMarkersToAdd().map( entry => entry.name );
  550. expect( markersToRefresh ).to.deep.equal( markersToRemove );
  551. expect( markersToRefresh ).to.deep.equal( markersToAdd );
  552. } );
  553. } );
  554. } );
  555. describe( 'attribute', () => {
  556. const attributeKey = 'key';
  557. const attributeOldValue = null;
  558. const attributeNewValue = 'foo';
  559. it( 'on an element', () => {
  560. const range = new Range( Position._createAt( root, 0 ), Position._createAt( root, 1 ) );
  561. model.change( () => {
  562. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  563. const diffRange = new Range( Position._createAt( root, 0 ), Position._createAt( root.getChild( 0 ), 0 ) );
  564. expectChanges( [
  565. { type: 'attribute', range: diffRange, attributeKey, attributeOldValue, attributeNewValue }
  566. ] );
  567. } );
  568. } );
  569. it( 'on an element - only one of many attributes changes', () => {
  570. const range = new Range( Position._createAt( root, 0 ), Position._createAt( root, 1 ) );
  571. model.change( () => {
  572. // Set an attribute on an element. It won't change afterwards.
  573. attribute( range, 'otherAttr', null, true );
  574. } );
  575. model.change( () => {
  576. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  577. const diffRange = new Range( Position._createAt( root, 0 ), Position._createAt( root.getChild( 0 ), 0 ) );
  578. expectChanges( [
  579. { type: 'attribute', range: diffRange, attributeKey, attributeOldValue, attributeNewValue }
  580. ] );
  581. } );
  582. } );
  583. it( 'on a character', () => {
  584. const parent = root.getChild( 1 );
  585. const range = new Range( Position._createAt( parent, 1 ), Position._createAt( parent, 2 ) );
  586. model.change( () => {
  587. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  588. expectChanges( [
  589. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  590. ] );
  591. } );
  592. } );
  593. it( 'on a character - case with same characters next to each other', () => {
  594. const parent = root.getChild( 0 );
  595. const range = new Range( Position._createAt( parent, 1 ), Position._createAt( parent, 2 ) );
  596. model.change( () => {
  597. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  598. expectChanges( [
  599. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  600. ] );
  601. } );
  602. } );
  603. it( 'on multiple characters', () => {
  604. const parent = root.getChild( 0 );
  605. const range = new Range( Position._createAt( parent, 0 ), Position._createAt( parent, 3 ) );
  606. model.change( () => {
  607. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  608. expectChanges( [
  609. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  610. ] );
  611. } );
  612. } );
  613. it( 'on multiple consecutive characters in multiple operations', () => {
  614. const parent = root.getChild( 0 );
  615. const range1 = new Range( Position._createAt( parent, 1 ), Position._createAt( parent, 2 ) );
  616. const range2 = new Range( Position._createAt( parent, 2 ), Position._createAt( parent, 3 ) );
  617. model.change( () => {
  618. attribute( range1, attributeKey, attributeOldValue, attributeNewValue );
  619. attribute( range2, attributeKey, attributeOldValue, attributeNewValue );
  620. const range = new Range( Position._createAt( parent, 1 ), Position._createAt( parent, 3 ) );
  621. expectChanges( [
  622. { type: 'attribute', range, attributeKey, attributeOldValue, attributeNewValue }
  623. ] );
  624. } );
  625. } );
  626. it( 'attribute changes intersecting #1', () => {
  627. const parent = root.getChild( 1 );
  628. // Be aware that you cannot make an intersecting changes with the same attribute key,
  629. // cause the value would be incorrect for the common part of the ranges.
  630. const ranges = [
  631. [ 0, 2, null, true, 'foo' ],
  632. [ 1, 3, null, true, 'bar' ]
  633. ];
  634. model.change( () => {
  635. for ( const item of ranges ) {
  636. const range = new Range( Position._createAt( parent, item[ 0 ] ), Position._createAt( parent, item[ 1 ] ) );
  637. attribute( range, item[ 4 ], item[ 2 ], item[ 3 ] );
  638. }
  639. expectChanges( [
  640. {
  641. type: 'attribute',
  642. range: new Range( Position._createAt( parent, 0 ), Position._createAt( parent, 2 ) ),
  643. attributeKey: 'foo',
  644. attributeOldValue: null,
  645. attributeNewValue: true
  646. },
  647. {
  648. type: 'attribute',
  649. range: new Range( Position._createAt( parent, 1 ), Position._createAt( parent, 3 ) ),
  650. attributeKey: 'bar',
  651. attributeOldValue: null,
  652. attributeNewValue: true
  653. }
  654. ] );
  655. } );
  656. } );
  657. it( 'attribute changes intersecting #2', () => {
  658. const parent = root.getChild( 1 );
  659. // Be aware that you cannot make an intersecting changes with the same attribute key,
  660. // cause the value would be incorrect for the common part of the ranges.
  661. const ranges = [
  662. [ 1, 3, null, true, 'foo' ],
  663. [ 0, 2, null, true, 'bar' ]
  664. ];
  665. model.change( () => {
  666. for ( const item of ranges ) {
  667. const range = new Range( Position._createAt( parent, item[ 0 ] ), Position._createAt( parent, item[ 1 ] ) );
  668. attribute( range, item[ 4 ], item[ 2 ], item[ 3 ] );
  669. }
  670. expectChanges( [
  671. {
  672. type: 'attribute',
  673. range: new Range( Position._createAt( parent, 0 ), Position._createAt( parent, 1 ) ),
  674. attributeKey: 'bar',
  675. attributeOldValue: null,
  676. attributeNewValue: true
  677. },
  678. {
  679. type: 'attribute',
  680. range: new Range( Position._createAt( parent, 1 ), Position._createAt( parent, 2 ) ),
  681. attributeKey: 'foo',
  682. attributeOldValue: null,
  683. attributeNewValue: true
  684. },
  685. {
  686. type: 'attribute',
  687. range: new Range( Position._createAt( parent, 1 ), Position._createAt( parent, 2 ) ),
  688. attributeKey: 'bar',
  689. attributeOldValue: null,
  690. attributeNewValue: true
  691. },
  692. {
  693. type: 'attribute',
  694. range: new Range( Position._createAt( parent, 2 ), Position._createAt( parent, 3 ) ),
  695. attributeKey: 'foo',
  696. attributeOldValue: null,
  697. attributeNewValue: true
  698. }
  699. ] );
  700. } );
  701. } );
  702. it( 'attribute changes included in an attribute change #1 - changes are reversed at the end', () => {
  703. const parent = root.getChild( 1 );
  704. const ranges = [
  705. [ 0, 1, null, true ],
  706. [ 1, 2, null, true ],
  707. [ 0, 2, true, null ]
  708. ];
  709. model.change( () => {
  710. for ( const item of ranges ) {
  711. const range = new Range( Position._createAt( parent, item[ 0 ] ), Position._createAt( parent, item[ 1 ] ) );
  712. attribute( range, attributeKey, item[ 2 ], item[ 3 ] );
  713. }
  714. expectChanges( [] );
  715. } );
  716. } );
  717. it( 'attribute changes included in an attribute change #2 - changes are re-applied at the end', () => {
  718. const parent = root.getChild( 1 );
  719. const ranges = [
  720. [ 0, 1, null, true ],
  721. [ 1, 2, null, true ],
  722. [ 0, 2, true, null ],
  723. [ 0, 1, null, true ],
  724. [ 1, 2, null, true ]
  725. ];
  726. model.change( () => {
  727. for ( const item of ranges ) {
  728. const range = new Range( Position._createAt( parent, item[ 0 ] ), Position._createAt( parent, item[ 1 ] ) );
  729. attribute( range, attributeKey, item[ 2 ], item[ 3 ] );
  730. }
  731. expectChanges( [ {
  732. type: 'attribute',
  733. range: new Range( Position._createAt( parent, 0 ), Position._createAt( parent, 2 ) ),
  734. attributeKey,
  735. attributeOldValue: null,
  736. attributeNewValue: true
  737. } ] );
  738. } );
  739. } );
  740. it( 'on multiple non-consecutive characters in multiple operations', () => {
  741. const parent = root.getChild( 0 );
  742. const range1 = new Range( Position._createAt( parent, 0 ), Position._createAt( parent, 1 ) );
  743. const range2 = new Range( Position._createAt( parent, 2 ), Position._createAt( parent, 3 ) );
  744. model.change( () => {
  745. // Note "reversed" order of ranges. Further range is changed first.
  746. attribute( range2, attributeKey, attributeOldValue, attributeNewValue );
  747. attribute( range1, attributeKey, attributeOldValue, attributeNewValue );
  748. // Note that changes has been sorted.
  749. expectChanges( [
  750. { type: 'attribute', range: range1, attributeKey, attributeOldValue, attributeNewValue },
  751. { type: 'attribute', range: range2, attributeKey, attributeOldValue, attributeNewValue }
  752. ] );
  753. } );
  754. } );
  755. it( 'on range containing various nodes', () => {
  756. const range = new Range( Position._createAt( root, 0 ), Position._createAt( root, 2 ) );
  757. model.change( () => {
  758. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  759. const p1 = root.getChild( 0 );
  760. const p2 = root.getChild( 1 );
  761. const type = 'attribute';
  762. expectChanges( [
  763. {
  764. type,
  765. range: new Range( Position._createAt( root, 0 ), Position._createAt( p1, 0 ) ),
  766. attributeKey,
  767. attributeOldValue,
  768. attributeNewValue
  769. },
  770. {
  771. type,
  772. range: new Range( Position._createAt( root, 1 ), Position._createAt( p2, 0 ) ),
  773. attributeKey,
  774. attributeOldValue,
  775. attributeNewValue
  776. }
  777. ] );
  778. } );
  779. } );
  780. it( 'remove and add attribute on text', () => {
  781. const p = root.getChild( 1 );
  782. p.getChild( 0 )._setAttribute( 'bold', true );
  783. const range = new Range( Position._createAt( p, 1 ), Position._createAt( p, 3 ) );
  784. model.change( () => {
  785. attribute( range, 'bold', true, null );
  786. attribute( range, 'italic', null, true );
  787. const range1 = new Range( Position._createAt( p, 1 ), Position._createAt( p, 2 ) );
  788. const range2 = new Range( Position._createAt( p, 2 ), Position._createAt( p, 3 ) );
  789. // Attribute change glueing does not work 100% correct.
  790. expectChanges( [
  791. {
  792. type: 'attribute',
  793. range: range1,
  794. attributeKey: 'bold',
  795. attributeOldValue: true,
  796. attributeNewValue: null
  797. },
  798. {
  799. type: 'attribute',
  800. range: range1,
  801. attributeKey: 'italic',
  802. attributeOldValue: null,
  803. attributeNewValue: true
  804. },
  805. {
  806. type: 'attribute',
  807. range: range2,
  808. attributeKey: 'bold',
  809. attributeOldValue: true,
  810. attributeNewValue: null
  811. },
  812. {
  813. type: 'attribute',
  814. range: range2,
  815. attributeKey: 'italic',
  816. attributeOldValue: null,
  817. attributeNewValue: true
  818. }
  819. ] );
  820. } );
  821. } );
  822. it( 'on some old nodes and inserted nodes', () => {
  823. const position = new Position( root, [ 0, 1 ] );
  824. const p1 = root.getChild( 0 );
  825. const range = new Range( Position._createAt( p1, 0 ), Position._createAt( p1, 2 ) );
  826. model.change( () => {
  827. insert( new Text( 'xx' ), position );
  828. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  829. const rangeBefore = new Range( Position._createAt( p1, 0 ), Position._createAt( p1, 1 ) );
  830. expectChanges( [
  831. { type: 'attribute', range: rangeBefore, attributeKey, attributeOldValue, attributeNewValue },
  832. { type: 'insert', name: '$text', length: 2, position }
  833. ] );
  834. } );
  835. } );
  836. it( 'only on inserted nodes', () => {
  837. const position = new Position( root, [ 0, 1 ] );
  838. const p1 = root.getChild( 0 );
  839. const range = new Range( Position._createAt( p1, 2 ), Position._createAt( p1, 3 ) );
  840. model.change( () => {
  841. insert( new Text( 'xxx' ), position );
  842. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  843. expectChanges( [
  844. { type: 'insert', name: '$text', length: 3, position }
  845. ] );
  846. } );
  847. } );
  848. it( 'on some inserted nodes and old nodes', () => {
  849. const position = new Position( root, [ 0, 1 ] );
  850. const p1 = root.getChild( 0 );
  851. const range = new Range( Position._createAt( p1, 2 ), Position._createAt( p1, 4 ) );
  852. model.change( () => {
  853. insert( new Text( 'xx' ), position );
  854. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  855. const rangeAfter = new Range( Position._createAt( p1, 3 ), Position._createAt( p1, 4 ) );
  856. expectChanges( [
  857. { type: 'insert', name: '$text', length: 2, position },
  858. { type: 'attribute', range: rangeAfter, attributeKey, attributeOldValue, attributeNewValue }
  859. ] );
  860. } );
  861. } );
  862. it( 'over all inserted nodes and some old nodes', () => {
  863. const position = new Position( root, [ 0, 1 ] );
  864. const p1 = root.getChild( 0 );
  865. const range = new Range( Position._createAt( p1, 0 ), Position._createAt( p1, 4 ) );
  866. model.change( () => {
  867. insert( new Text( 'xx' ), position );
  868. attribute( range, attributeKey, attributeOldValue, attributeNewValue );
  869. const rangeBefore = new Range( Position._createAt( p1, 0 ), Position._createAt( p1, 1 ) );
  870. const rangeAfter = new Range( Position._createAt( p1, 3 ), Position._createAt( p1, 4 ) );
  871. expectChanges( [
  872. { type: 'attribute', range: rangeBefore, attributeKey, attributeOldValue, attributeNewValue },
  873. { type: 'insert', name: '$text', length: 2, position },
  874. { type: 'attribute', range: rangeAfter, attributeKey, attributeOldValue, attributeNewValue }
  875. ] );
  876. } );
  877. } );
  878. it( 'on some not changed and some changed nodes', () => {
  879. const p = root.getChild( 0 );
  880. const rangeA = new Range( Position._createAt( p, 1 ), Position._createAt( p, 3 ) );
  881. const rangeB = new Range( Position._createAt( p, 0 ), Position._createAt( p, 2 ) );
  882. model.change( () => {
  883. attribute( rangeA, 'a', null, true );
  884. attribute( rangeB, 'b', null, true );
  885. const type = 'attribute';
  886. const attributeOldValue = null;
  887. const attributeNewValue = true;
  888. // Attribute change glueing does not work 100% correct.
  889. expectChanges( [
  890. {
  891. type,
  892. range: new Range( Position._createAt( p, 0 ), Position._createAt( p, 1 ) ),
  893. attributeKey: 'b',
  894. attributeOldValue,
  895. attributeNewValue
  896. },
  897. {
  898. type,
  899. range: new Range( Position._createAt( p, 1 ), Position._createAt( p, 2 ) ),
  900. attributeKey: 'a',
  901. attributeOldValue,
  902. attributeNewValue
  903. },
  904. {
  905. type,
  906. range: new Range( Position._createAt( p, 1 ), Position._createAt( p, 2 ) ),
  907. attributeKey: 'b',
  908. attributeOldValue,
  909. attributeNewValue
  910. },
  911. {
  912. type,
  913. range: new Range( Position._createAt( p, 2 ), Position._createAt( p, 3 ) ),
  914. attributeKey: 'a',
  915. attributeOldValue,
  916. attributeNewValue
  917. }
  918. ] );
  919. } );
  920. } );
  921. it( 'on already changed nodes', () => {
  922. const p = root.getChild( 1 );
  923. const rangeA = new Range( Position._createAt( p, 0 ), Position._createAt( p, 3 ) );
  924. const rangeB = new Range( Position._createAt( p, 1 ), Position._createAt( p, 2 ) );
  925. model.change( () => {
  926. attribute( rangeA, 'a', null, true );
  927. attribute( rangeB, 'b', null, true );
  928. const type = 'attribute';
  929. const attributeOldValue = null;
  930. const attributeNewValue = true;
  931. // Attribute change glueing does not work 100% correct.
  932. expectChanges( [
  933. {
  934. type,
  935. range: new Range( Position._createAt( p, 0 ), Position._createAt( p, 2 ) ),
  936. attributeKey: 'a',
  937. attributeOldValue,
  938. attributeNewValue
  939. },
  940. {
  941. type,
  942. range: new Range( Position._createAt( p, 1 ), Position._createAt( p, 2 ) ),
  943. attributeKey: 'b',
  944. attributeOldValue,
  945. attributeNewValue
  946. },
  947. {
  948. type,
  949. range: new Range( Position._createAt( p, 2 ), Position._createAt( p, 3 ) ),
  950. attributeKey: 'a',
  951. attributeOldValue,
  952. attributeNewValue
  953. }
  954. ] );
  955. } );
  956. } );
  957. it( 'on some changed and some not changed nodes', () => {
  958. const p = root.getChild( 1 );
  959. const rangeA = new Range( Position._createAt( p, 0 ), Position._createAt( p, 2 ) );
  960. const rangeB = new Range( Position._createAt( p, 1 ), Position._createAt( p, 3 ) );
  961. model.change( () => {
  962. attribute( rangeA, 'a', null, true );
  963. attribute( rangeB, 'b', null, true );
  964. const type = 'attribute';
  965. const attributeOldValue = null;
  966. const attributeNewValue = true;
  967. expectChanges( [
  968. {
  969. type,
  970. range: new Range( Position._createAt( p, 0 ), Position._createAt( p, 2 ) ),
  971. attributeKey: 'a',
  972. attributeOldValue,
  973. attributeNewValue
  974. },
  975. {
  976. type,
  977. range: new Range( Position._createAt( p, 1 ), Position._createAt( p, 3 ) ),
  978. attributeKey: 'b',
  979. attributeOldValue,
  980. attributeNewValue
  981. }
  982. ] );
  983. } );
  984. } );
  985. it( 'over all changed nodes and some not changed nodes', () => {
  986. const p = root.getChild( 0 );
  987. const rangeA = new Range( Position._createAt( p, 1 ), Position._createAt( p, 2 ) );
  988. const rangeB = new Range( Position._createAt( p, 0 ), Position._createAt( p, 3 ) );
  989. model.change( () => {
  990. attribute( rangeA, 'a', null, true );
  991. attribute( rangeB, 'b', null, true );
  992. const type = 'attribute';
  993. const attributeOldValue = null;
  994. const attributeNewValue = true;
  995. // Attribute change glueing does not work 100% correct.
  996. expectChanges( [
  997. {
  998. type,
  999. range: new Range( Position._createAt( p, 0 ), Position._createAt( p, 1 ) ),
  1000. attributeKey: 'b',
  1001. attributeOldValue,
  1002. attributeNewValue
  1003. },
  1004. {
  1005. type,
  1006. range: new Range( Position._createAt( p, 1 ), Position._createAt( p, 2 ) ),
  1007. attributeKey: 'a',
  1008. attributeOldValue,
  1009. attributeNewValue
  1010. },
  1011. {
  1012. type,
  1013. range: new Range( Position._createAt( p, 1 ), Position._createAt( p, 3 ) ),
  1014. attributeKey: 'b',
  1015. attributeOldValue,
  1016. attributeNewValue
  1017. }
  1018. ] );
  1019. } );
  1020. } );
  1021. } );
  1022. describe( 'split', () => {
  1023. it( 'split an element', () => {
  1024. const position = new Position( root, [ 0, 2 ] );
  1025. model.change( () => {
  1026. split( position );
  1027. expectChanges( [
  1028. { type: 'remove', name: '$text', length: 1, position },
  1029. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1030. ] );
  1031. } );
  1032. } );
  1033. it( 'split a new element', () => {
  1034. model.change( () => {
  1035. insert(
  1036. new Element( 'paragraph', null, new Text( 'Ab' ) ),
  1037. new Position( root, [ 0 ] )
  1038. );
  1039. split( new Position( root, [ 0, 1 ] ) );
  1040. expectChanges( [
  1041. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  1042. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1043. ] );
  1044. } );
  1045. } );
  1046. it( 'split an element inside a new element', () => {
  1047. model.change( () => {
  1048. insert(
  1049. new Element( 'blockQuote', null, new Element( 'paragraph', null, new Text( 'Ab' ) ) ),
  1050. new Position( root, [ 0 ] )
  1051. );
  1052. split( new Position( root, [ 0, 0, 1 ] ) );
  1053. expectChanges( [
  1054. { type: 'insert', name: 'blockQuote', length: 1, position: new Position( root, [ 0 ] ) }
  1055. ] );
  1056. } );
  1057. } );
  1058. it( 'should correctly mark a change in graveyard', () => {
  1059. model.change( () => {
  1060. merge( new Position( root, [ 1, 0 ] ), new Position( root, [ 0, 3 ] ) );
  1061. } );
  1062. model.change( () => {
  1063. const position = new Position( root, [ 0, 3 ] );
  1064. const operation = new SplitOperation( position, 3, new Position( doc.graveyard, [ 0 ] ), doc.version );
  1065. model.applyOperation( operation );
  1066. expectChanges( [
  1067. { type: 'remove', name: 'paragraph', length: 1, position: new Position( doc.graveyard, [ 0 ] ) },
  1068. { type: 'remove', name: '$text', length: 3, position: new Position( root, [ 0, 3 ] ) },
  1069. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1070. ], true );
  1071. } );
  1072. } );
  1073. } );
  1074. describe( 'merge', () => {
  1075. it( 'merge two elements', () => {
  1076. model.change( () => {
  1077. merge( new Position( root, [ 1, 0 ] ), new Position( root, [ 0, 3 ] ) );
  1078. expectChanges( [
  1079. { type: 'insert', name: '$text', length: 3, position: new Position( root, [ 0, 3 ] ) },
  1080. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1081. ] );
  1082. } );
  1083. } );
  1084. it( 'merge a new element', () => {
  1085. model.change( () => {
  1086. insert( new Element( 'paragraph', null, new Text( 'Ab' ) ), new Position( root, [ 1 ] ) );
  1087. merge( new Position( root, [ 1, 0 ] ), new Position( root, [ 0, 3 ] ) );
  1088. expectChanges( [
  1089. { type: 'insert', name: '$text', length: 2, position: new Position( root, [ 0, 3 ] ) }
  1090. ] );
  1091. } );
  1092. } );
  1093. it( 'merge into a new element', () => {
  1094. model.change( () => {
  1095. insert( new Element( 'paragraph', null, new Text( 'Ab' ) ), new Position( root, [ 0 ] ) );
  1096. merge( new Position( root, [ 1, 0 ] ), new Position( root, [ 0, 2 ] ) );
  1097. expectChanges( [
  1098. { type: 'insert', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  1099. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1100. ] );
  1101. } );
  1102. } );
  1103. it( 'merge elements inside a new element', () => {
  1104. model.change( () => {
  1105. insert(
  1106. new Element( 'blockQuote', null, [
  1107. new Element( 'paragraph', null, new Text( 'Ab' ) ),
  1108. new Element( 'paragraph', null, new Text( 'Xyz' ) )
  1109. ] ),
  1110. new Position( root, [ 0 ] )
  1111. );
  1112. merge( new Position( root, [ 0, 1, 0 ] ), new Position( root, [ 0, 0, 2 ] ) );
  1113. expectChanges( [
  1114. { type: 'insert', name: 'blockQuote', length: 1, position: new Position( root, [ 0 ] ) }
  1115. ] );
  1116. } );
  1117. } );
  1118. it( 'should correctly mark a change in graveyard', () => {
  1119. model.change( () => {
  1120. merge( new Position( root, [ 1, 0 ] ), new Position( root, [ 0, 3 ] ) );
  1121. expectChanges( [
  1122. { type: 'insert', name: 'paragraph', length: 1, position: new Position( doc.graveyard, [ 0 ] ) },
  1123. { type: 'insert', name: '$text', length: 3, position: new Position( root, [ 0, 3 ] ) },
  1124. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1 ] ) }
  1125. ], true );
  1126. } );
  1127. } );
  1128. } );
  1129. describe( 'markers', () => {
  1130. let range, rangeB;
  1131. beforeEach( () => {
  1132. range = new Range( Position._createAt( root, 0 ), Position._createAt( root, 1 ) );
  1133. rangeB = new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) );
  1134. } );
  1135. it( 'add marker', () => {
  1136. differ.bufferMarkerChange( 'name', null, range, true );
  1137. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  1138. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  1139. { name: 'name', range }
  1140. ] );
  1141. } );
  1142. it( 'remove marker', () => {
  1143. differ.bufferMarkerChange( 'name', range, null, true );
  1144. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  1145. { name: 'name', range }
  1146. ] );
  1147. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  1148. } );
  1149. it( 'change marker\'s range', () => {
  1150. differ.bufferMarkerChange( 'name', range, rangeB, true );
  1151. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  1152. { name: 'name', range }
  1153. ] );
  1154. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  1155. { name: 'name', range: rangeB }
  1156. ] );
  1157. } );
  1158. it( 'add marker not affecting data', () => {
  1159. differ.bufferMarkerChange( 'name', range, rangeB, false );
  1160. expect( differ.hasDataChanges() ).to.be.false;
  1161. } );
  1162. it( 'add marker affecting data', () => {
  1163. differ.bufferMarkerChange( 'name', range, rangeB, true );
  1164. expect( differ.hasDataChanges() ).to.be.true;
  1165. } );
  1166. it( 'add marker and remove it', () => {
  1167. differ.bufferMarkerChange( 'name', null, range, true );
  1168. differ.bufferMarkerChange( 'name', range, null, true );
  1169. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  1170. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  1171. expect( differ.hasDataChanges() ).to.be.false;
  1172. } );
  1173. it( 'add marker and change it', () => {
  1174. differ.bufferMarkerChange( 'name', null, range, true );
  1175. differ.bufferMarkerChange( 'name', range, rangeB, true );
  1176. expect( differ.getMarkersToRemove() ).to.deep.equal( [] );
  1177. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  1178. { name: 'name', range: rangeB }
  1179. ] );
  1180. } );
  1181. it( 'change marker to not affecting data', () => {
  1182. differ.bufferMarkerChange( 'name', range, rangeB, true );
  1183. differ.bufferMarkerChange( 'name', range, rangeB, false );
  1184. expect( differ.hasDataChanges() ).to.be.false;
  1185. } );
  1186. it( 'change marker and remove it', () => {
  1187. differ.bufferMarkerChange( 'name', range, rangeB, true );
  1188. differ.bufferMarkerChange( 'name', rangeB, null, true );
  1189. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  1190. { name: 'name', range }
  1191. ] );
  1192. expect( differ.getMarkersToAdd() ).to.deep.equal( [] );
  1193. expect( differ.hasDataChanges() ).to.be.true;
  1194. } );
  1195. it( 'remove marker and add it at same range', () => {
  1196. differ.bufferMarkerChange( 'name', range, null, true );
  1197. differ.bufferMarkerChange( 'name', null, range, true );
  1198. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  1199. { name: 'name', range }
  1200. ] );
  1201. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  1202. { name: 'name', range }
  1203. ] );
  1204. } );
  1205. it( 'change marker to the same range', () => {
  1206. differ.bufferMarkerChange( 'name', range, range, true );
  1207. expect( differ.getMarkersToRemove() ).to.deep.equal( [
  1208. { name: 'name', range }
  1209. ] );
  1210. expect( differ.getMarkersToAdd() ).to.deep.equal( [
  1211. { name: 'name', range }
  1212. ] );
  1213. } );
  1214. } );
  1215. describe( 'other cases', () => {
  1216. // #1309.
  1217. it( 'multiple inserts and removes in one element', () => {
  1218. model.change( () => {
  1219. insert( new Text( 'x' ), new Position( root, [ 0, 2 ] ) );
  1220. insert( new Text( 'x' ), new Position( root, [ 0, 3 ] ) );
  1221. move( new Position( root, [ 0, 2 ] ), 1, new Position( root, [ 1, 0 ] ) );
  1222. expectChanges( [
  1223. { type: 'insert', name: '$text', length: 1, position: new Position( root, [ 0, 2 ] ) },
  1224. { type: 'insert', name: '$text', length: 1, position: new Position( root, [ 1, 0 ] ) }
  1225. ] );
  1226. } );
  1227. } );
  1228. // ckeditor5#733.
  1229. it( 'proper filtering of changes in removed elements', () => {
  1230. // Before fix there was a buggy scenario described in ckeditor5#733.
  1231. // There was this structure: `<paragraph>foo[</paragraph><image /><blockQuote><p>te]xt</p></blockQuote>`
  1232. // On delete of above selection `image` and `paragraph` inside `blockQuote` are removed (it gets merged).
  1233. // However, since `image` was removed first, when checking if `paragraph` is in a removed element,
  1234. // it appeared that `blockQuote` looks like it is removed because it had the same path as the already removed `<image>`.
  1235. // In a result, removing `paragraph` was discarded.
  1236. // The mistake was that the checking for removing was done at incorrect moment.
  1237. root._removeChildren( 0, root.childCount );
  1238. root._appendChild( [
  1239. new Element( 'paragraph', null, new Text( 'foo' ) ),
  1240. new Element( 'image' ),
  1241. new Element( 'blockQuote', null, [
  1242. new Element( 'paragraph', null, new Text( 'text' ) )
  1243. ] )
  1244. ] );
  1245. model.change( () => {
  1246. // Remove `"te"`.
  1247. remove( new Position( root, [ 2, 0, 0 ] ), 2 );
  1248. // Remove `image` before `blockQuote`.
  1249. remove( new Position( root, [ 1 ] ), 1 );
  1250. // Move `"xt"` to first `paragraph` (merging).
  1251. move( new Position( root, [ 1, 0, 0 ] ), 2, new Position( root, [ 0, 3 ] ) );
  1252. // Remove `paragraph` inside `blockQuote`.
  1253. remove( new Position( root, [ 1, 0 ] ), 1 );
  1254. expectChanges( [
  1255. { type: 'insert', name: '$text', length: 2, position: new Position( root, [ 0, 3 ] ) },
  1256. { type: 'remove', name: 'image', length: 1, position: new Position( root, [ 1 ] ) },
  1257. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 1, 0 ] ) }
  1258. ] );
  1259. } );
  1260. } );
  1261. // In this scenario we create a new element, then remove something from before it to mess up with offsets,
  1262. // finally we insert some content into a new element. Since we are inserting into a new element, the
  1263. // inserted children should not be shown on changes list.
  1264. it( 'proper filtering of changes in inserted elements', () => {
  1265. root._removeChildren( 0, root.childCount );
  1266. root._appendChild( new Element( 'image' ) );
  1267. const blockQuote = new Element( 'blockQuote', null, new Element( 'paragraph' ) );
  1268. model.change( () => {
  1269. // Insert `blockQuote` with `paragraph` after `image`.
  1270. insert( blockQuote, new Position( root, [ 1 ] ) );
  1271. // Remove `image` from before `blockQuote`.
  1272. remove( new Position( root, [ 0 ] ), 1 );
  1273. // Insert text into `paragraph` in `blockQuote`.
  1274. insert( new Text( 'foo' ), new Position( root, [ 0, 0, 0 ] ) );
  1275. expectChanges( [
  1276. { type: 'remove', name: 'image', length: 1, position: new Position( root, [ 0 ] ) },
  1277. { type: 'insert', name: 'blockQuote', length: 1, position: new Position( root, [ 0 ] ) }
  1278. ] );
  1279. } );
  1280. } );
  1281. // In this scenario we create a new element, then move another element that was before the new element into
  1282. // the new element. This way we mess up with offsets and insert content into a new element in one operation.
  1283. // Since we are inserting into a new element, the insertion of moved element should not be shown on changes list.
  1284. it( 'proper filtering of changes in inserted elements #2', () => {
  1285. root._removeChildren( 0, root.childCount );
  1286. root._appendChild( new Element( 'image' ) );
  1287. model.change( () => {
  1288. // Insert `div` after `image`.
  1289. insert( new Element( 'div' ), new Position( root, [ 1 ] ) );
  1290. // Move `image` to the new `div`.
  1291. move( new Position( root, [ 0 ] ), 1, new Position( root, [ 1, 0 ] ) );
  1292. expectChanges( [
  1293. { type: 'remove', name: 'image', length: 1, position: new Position( root, [ 0 ] ) },
  1294. { type: 'insert', name: 'div', length: 1, position: new Position( root, [ 0 ] ) }
  1295. ] );
  1296. } );
  1297. } );
  1298. // #1392.
  1299. it( 'remove is correctly transformed by multiple affecting changes', () => {
  1300. root._appendChild( new Element( 'paragraph', null, new Text( 'xyz' ) ) );
  1301. model.change( writer => {
  1302. rename( root.getChild( 1 ), 'heading' );
  1303. rename( root.getChild( 2 ), 'heading' );
  1304. remove( writer.createPositionAt( root, 0 ), 3 );
  1305. expectChanges( [
  1306. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  1307. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) },
  1308. { type: 'remove', name: 'paragraph', length: 1, position: new Position( root, [ 0 ] ) }
  1309. ] );
  1310. } );
  1311. } );
  1312. } );
  1313. describe( 'getChanges()', () => {
  1314. let position, p1, rangeAttrChange, range;
  1315. beforeEach( () => {
  1316. position = new Position( root, [ 0, 1 ] );
  1317. p1 = root.getChild( 0 );
  1318. range = new Range( Position._createAt( p1, 2 ), Position._createAt( p1, 4 ) );
  1319. rangeAttrChange = new Range( Position._createAt( p1, 3 ), Position._createAt( p1, 4 ) );
  1320. } );
  1321. it( 'should return changes in graveyard if a flag was set up', () => {
  1322. const removePosition = new Position( root, [ 1 ] );
  1323. model.change( () => {
  1324. insert( new Text( 'xx' ), position );
  1325. attribute( range, 'key', null, 'foo' );
  1326. remove( removePosition, 1 );
  1327. expectChanges( [
  1328. { type: 'insert', name: 'paragraph', length: 1, position: new Position( doc.graveyard, [ 0 ] ) },
  1329. { type: 'insert', name: '$text', length: 2, position },
  1330. {
  1331. type: 'attribute',
  1332. range: rangeAttrChange,
  1333. attributeKey: 'key',
  1334. attributeOldValue: null,
  1335. attributeNewValue: 'foo'
  1336. },
  1337. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  1338. ], true );
  1339. } );
  1340. } );
  1341. // Below tests test caching.
  1342. it( 'should return same change set if was called twice in a row', () => {
  1343. model.change( () => {
  1344. insert( new Text( 'xx' ), position );
  1345. attribute( range, 'key', null, 'foo' );
  1346. const changesA = differ.getChanges();
  1347. const changesB = differ.getChanges();
  1348. expect( changesA ).to.deep.equal( changesB );
  1349. } );
  1350. } );
  1351. it( 'should return same change set if was called twice in a row - graveyard changes', () => {
  1352. model.change( () => {
  1353. insert( new Text( 'xx' ), position );
  1354. attribute( range, 'key', null, 'foo' );
  1355. const removePosition = new Position( root, [ 1 ] );
  1356. remove( removePosition, 1 );
  1357. const changesA = differ.getChanges( { includeChangesInGraveyard: true } );
  1358. const changesB = differ.getChanges( { includeChangesInGraveyard: true } );
  1359. expect( changesA ).to.deep.equal( changesB );
  1360. } );
  1361. } );
  1362. it( 'should return correct changes if change happened between getChanges() calls', () => {
  1363. model.change( () => {
  1364. insert( new Text( 'xx' ), position );
  1365. attribute( range, 'key', null, 'foo' );
  1366. expectChanges( [
  1367. { type: 'insert', name: '$text', length: 2, position },
  1368. {
  1369. type: 'attribute',
  1370. range: rangeAttrChange,
  1371. attributeKey: 'key',
  1372. attributeOldValue: null,
  1373. attributeNewValue: 'foo'
  1374. }
  1375. ] );
  1376. const removePosition = new Position( root, [ 1 ] );
  1377. remove( removePosition, 1 );
  1378. expectChanges( [
  1379. { type: 'insert', name: '$text', length: 2, position },
  1380. {
  1381. type: 'attribute',
  1382. range: rangeAttrChange,
  1383. attributeKey: 'key',
  1384. attributeOldValue: null,
  1385. attributeNewValue: 'foo'
  1386. },
  1387. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  1388. ] );
  1389. } );
  1390. } );
  1391. it( 'should return correct changes if reset happened between getChanges() calls', () => {
  1392. model.change( () => {
  1393. insert( new Text( 'xx' ), position );
  1394. attribute( range, 'key', null, 'foo' );
  1395. expectChanges( [
  1396. { type: 'insert', name: '$text', length: 2, position },
  1397. {
  1398. type: 'attribute',
  1399. range: rangeAttrChange,
  1400. attributeKey: 'key',
  1401. attributeOldValue: null,
  1402. attributeNewValue: 'foo'
  1403. }
  1404. ] );
  1405. differ.reset();
  1406. const removePosition = new Position( root, [ 1 ] );
  1407. remove( removePosition, 1 );
  1408. expectChanges( [
  1409. { type: 'remove', name: 'paragraph', position: removePosition, length: 1 }
  1410. ] );
  1411. } );
  1412. } );
  1413. } );
  1414. function insert( item, position ) {
  1415. const operation = new InsertOperation( position, item, doc.version );
  1416. model.applyOperation( operation );
  1417. }
  1418. function split( position ) {
  1419. const howMany = position.parent.maxOffset - position.offset;
  1420. const operation = new SplitOperation( position, howMany, null, doc.version );
  1421. model.applyOperation( operation );
  1422. }
  1423. function merge( source, target ) {
  1424. const howMany = source.parent.maxOffset;
  1425. const operation = new MergeOperation( source, howMany, target, new Position( doc.graveyard, [ 0 ] ), doc.version );
  1426. model.applyOperation( operation );
  1427. }
  1428. function remove( sourcePosition, howMany ) {
  1429. const targetPosition = Position._createAt( doc.graveyard, doc.graveyard.maxOffset );
  1430. const operation = new MoveOperation( sourcePosition, howMany, targetPosition, doc.version );
  1431. model.applyOperation( operation );
  1432. }
  1433. function move( sourcePosition, howMany, targetPosition ) {
  1434. const operation = new MoveOperation( sourcePosition, howMany, targetPosition, doc.version );
  1435. model.applyOperation( operation );
  1436. }
  1437. function rename( element, newName ) {
  1438. const operation = new RenameOperation( Position._createBefore( element ), element.name, newName, doc.version );
  1439. model.applyOperation( operation );
  1440. }
  1441. function attribute( range, key, oldValue, newValue ) {
  1442. const operation = new AttributeOperation( range, key, oldValue, newValue, doc.version );
  1443. model.applyOperation( operation );
  1444. }
  1445. function expectChanges( expected, includeChangesInGraveyard = false ) {
  1446. const changes = differ.getChanges( { includeChangesInGraveyard } );
  1447. expect( changes.length ).to.equal( expected.length );
  1448. for ( let i = 0; i < expected.length; i++ ) {
  1449. for ( const key in expected[ i ] ) {
  1450. if ( expected[ i ].hasOwnProperty( key ) ) {
  1451. if ( key == 'position' || key == 'range' ) {
  1452. expect( changes[ i ][ key ].isEqual( expected[ i ][ key ] ), `item ${ i }, key "${ key }"` ).to.be.true;
  1453. } else {
  1454. expect( changes[ i ][ key ], `item ${ i }, key "${ key }"` ).to.equal( expected[ i ][ key ] );
  1455. }
  1456. }
  1457. }
  1458. }
  1459. }
  1460. } );