differ.js 56 KB

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