differ.js 48 KB

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