8
0

differ.js 61 KB

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