differ.js 57 KB

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