differ.js 58 KB

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