undoengine-integration.js 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  8. import Position from '@ckeditor/ckeditor5-engine/src/model/position';
  9. import Element from '@ckeditor/ckeditor5-engine/src/model/element';
  10. import UndoEngine from '../src/undoengine';
  11. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  12. import HeadingEngine from '@ckeditor/ckeditor5-heading/src/headingengine';
  13. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  14. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  15. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  16. import BoldEngine from '@ckeditor/ckeditor5-basic-styles/src/boldengine';
  17. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  18. import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  19. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  20. describe( 'UndoEngine integration', () => {
  21. let editor, doc, root, div;
  22. beforeEach( () => {
  23. div = document.createElement( 'div' );
  24. document.body.appendChild( div );
  25. return ClassicEditor.create( div, { plugins: [ Paragraph, HeadingEngine, Typing, Enter, Clipboard, BoldEngine, UndoEngine ] } )
  26. .then( newEditor => {
  27. editor = newEditor;
  28. doc = editor.document;
  29. // Add "div feature".
  30. doc.schema.registerItem( 'div', '$block' );
  31. buildModelConverter().for( editor.data.modelToView, editor.editing.modelToView ).fromElement( 'div' ).toElement( 'div' );
  32. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  33. root = doc.getRoot();
  34. } );
  35. } );
  36. function setSelection( pathA, pathB ) {
  37. doc.selection.setRanges( [ new Range( new Position( root, pathA ), new Position( root, pathB ) ) ] );
  38. }
  39. function input( input ) {
  40. setData( doc, input );
  41. }
  42. function output( output ) {
  43. expect( getData( doc ) ).to.equal( output );
  44. }
  45. function undoDisabled() {
  46. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false;
  47. }
  48. function redoDisabled() {
  49. expect( editor.commands.get( 'redo' ).isEnabled ).to.be.false;
  50. }
  51. describe( 'adding and removing content', () => {
  52. it( 'add and undo', () => {
  53. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  54. doc.enqueueChanges( () => {
  55. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  56. } );
  57. output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
  58. editor.execute( 'undo' );
  59. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  60. undoDisabled();
  61. } );
  62. it( 'multiple adding and undo', () => {
  63. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  64. doc.enqueueChanges( () => {
  65. doc.batch()
  66. .insert( doc.selection.getFirstPosition(), 'zzz' )
  67. .insert( new Position( root, [ 1, 0 ] ), 'xxx' );
  68. } );
  69. output( '<paragraph>fozzz[]o</paragraph><paragraph>xxxbar</paragraph>' );
  70. doc.enqueueChanges( () => {
  71. setSelection( [ 1, 0 ], [ 1, 0 ] );
  72. doc.batch().insert( doc.selection.getFirstPosition(), 'yyy' );
  73. } );
  74. output( '<paragraph>fozzzo</paragraph><paragraph>yyy[]xxxbar</paragraph>' );
  75. editor.execute( 'undo' );
  76. output( '<paragraph>fozzzo</paragraph><paragraph>[]xxxbar</paragraph>' );
  77. editor.execute( 'undo' );
  78. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  79. undoDisabled();
  80. } );
  81. it( 'multiple adding mixed with undo', () => {
  82. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  83. doc.enqueueChanges( () => {
  84. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  85. } );
  86. output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
  87. doc.enqueueChanges( () => {
  88. setSelection( [ 1, 0 ], [ 1, 0 ] );
  89. doc.batch().insert( doc.selection.getFirstPosition(), 'yyy' );
  90. } );
  91. output( '<paragraph>fozzzo</paragraph><paragraph>yyy[]bar</paragraph>' );
  92. editor.execute( 'undo' );
  93. output( '<paragraph>fozzzo</paragraph><paragraph>[]bar</paragraph>' );
  94. doc.enqueueChanges( () => {
  95. setSelection( [ 0, 0 ], [ 0, 0 ] );
  96. doc.batch().insert( doc.selection.getFirstPosition(), 'xxx' );
  97. } );
  98. output( '<paragraph>xxx[]fozzzo</paragraph><paragraph>bar</paragraph>' );
  99. editor.execute( 'undo' );
  100. output( '<paragraph>[]fozzzo</paragraph><paragraph>bar</paragraph>' );
  101. editor.execute( 'undo' );
  102. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  103. undoDisabled();
  104. } );
  105. it( 'multiple remove and undo', () => {
  106. input( '<paragraph>[]foo</paragraph><paragraph>bar</paragraph>' );
  107. doc.enqueueChanges( () => {
  108. doc.batch().remove( Range.createFromPositionAndShift( doc.selection.getFirstPosition(), 2 ) );
  109. } );
  110. output( '<paragraph>[]o</paragraph><paragraph>bar</paragraph>' );
  111. doc.enqueueChanges( () => {
  112. setSelection( [ 1, 1 ], [ 1, 1 ] );
  113. doc.batch().remove( Range.createFromPositionAndShift( doc.selection.getFirstPosition(), 2 ) );
  114. } );
  115. output( '<paragraph>o</paragraph><paragraph>b[]</paragraph>' );
  116. editor.execute( 'undo' );
  117. // Here is an edge case that selection could be before or after `ar`.
  118. output( '<paragraph>o</paragraph><paragraph>bar[]</paragraph>' );
  119. editor.execute( 'undo' );
  120. // As above.
  121. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  122. undoDisabled();
  123. } );
  124. it( 'add and remove different parts and undo', () => {
  125. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  126. doc.enqueueChanges( () => {
  127. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  128. } );
  129. output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
  130. doc.enqueueChanges( () => {
  131. setSelection( [ 1, 2 ], [ 1, 2 ] );
  132. doc.batch().remove( Range.createFromPositionAndShift( new Position( root, [ 1, 1 ] ), 1 ) );
  133. } );
  134. output( '<paragraph>fozzzo</paragraph><paragraph>b[]r</paragraph>' );
  135. editor.execute( 'undo' );
  136. output( '<paragraph>fozzzo</paragraph><paragraph>ba[]r</paragraph>' );
  137. editor.execute( 'undo' );
  138. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  139. undoDisabled();
  140. } );
  141. it( 'add and remove same part and undo', () => {
  142. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  143. doc.enqueueChanges( () => {
  144. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  145. } );
  146. output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
  147. doc.enqueueChanges( () => {
  148. doc.batch().remove( Range.createFromPositionAndShift( new Position( root, [ 0, 2 ] ), 3 ) );
  149. } );
  150. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  151. editor.execute( 'undo' );
  152. output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
  153. editor.execute( 'undo' );
  154. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  155. undoDisabled();
  156. } );
  157. it( 'undo remove all content', () => {
  158. input( '<paragraph>foo[]</paragraph>' );
  159. doc.enqueueChanges( () => {
  160. doc.batch().remove( Range.createIn( root ) );
  161. } );
  162. output( '<paragraph>[]</paragraph>' ); // All hail our king and savior, autoparagraphing!
  163. editor.execute( 'undo' );
  164. output( '<paragraph>foo[]</paragraph>' );
  165. undoDisabled();
  166. } );
  167. it( 'undo insert first content', () => {
  168. input( '' );
  169. doc.enqueueChanges( () => {
  170. doc.batch().insert( doc.selection.getFirstPosition(), new Element( 'heading1' ) );
  171. } );
  172. output( '<heading1>[]</heading1>' );
  173. editor.execute( 'undo' );
  174. output( '<paragraph>[]</paragraph>' ); // All hail our king and savior, autoparagraphing!
  175. undoDisabled();
  176. } );
  177. // #72.
  178. it( 'undo paste multiple elements simulation', () => {
  179. input( '<paragraph></paragraph>' );
  180. const p = root.getChild( 0 );
  181. const pos = new Position( root, [ 0 ] );
  182. doc.enqueueChanges( () => {
  183. doc.batch()
  184. .remove( p )
  185. .insert( pos, new Element( 'heading1' ) )
  186. .insert( pos.getShiftedBy( 1 ), new Element( 'heading2' ) );
  187. } );
  188. output( '<heading1>[]</heading1><heading2></heading2>' );
  189. editor.execute( 'undo' );
  190. output( '<paragraph>[]</paragraph>' );
  191. undoDisabled();
  192. } );
  193. } );
  194. describe( 'moving', () => {
  195. it( 'move same content twice then undo', () => {
  196. input( '<paragraph>f[o]z</paragraph><paragraph>bar</paragraph>' );
  197. doc.enqueueChanges( () => {
  198. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) );
  199. } );
  200. output( '<paragraph>fz</paragraph><paragraph>[o]bar</paragraph>' );
  201. doc.enqueueChanges( () => {
  202. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 0, 2 ] ) );
  203. } );
  204. output( '<paragraph>fz[o]</paragraph><paragraph>bar</paragraph>' );
  205. editor.execute( 'undo' );
  206. output( '<paragraph>fz</paragraph><paragraph>[o]bar</paragraph>' );
  207. editor.execute( 'undo' );
  208. output( '<paragraph>f[o]z</paragraph><paragraph>bar</paragraph>' );
  209. undoDisabled();
  210. } );
  211. it( 'move content and new parent then undo', () => {
  212. input( '<paragraph>f[o]z</paragraph><paragraph>bar</paragraph>' );
  213. doc.enqueueChanges( () => {
  214. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) );
  215. } );
  216. output( '<paragraph>fz</paragraph><paragraph>[o]bar</paragraph>' );
  217. doc.enqueueChanges( () => {
  218. setSelection( [ 1 ], [ 2 ] );
  219. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 0 ] ) );
  220. } );
  221. output( '[<paragraph>obar</paragraph>]<paragraph>fz</paragraph>' );
  222. editor.execute( 'undo' );
  223. output( '<paragraph>fz</paragraph>[<paragraph>obar</paragraph>]' );
  224. editor.execute( 'undo' );
  225. output( '<paragraph>f[o]z</paragraph><paragraph>bar</paragraph>' );
  226. undoDisabled();
  227. } );
  228. } );
  229. describe( 'attributes with other', () => {
  230. it( 'attributes then insert inside then undo', () => {
  231. input( '<paragraph>fo[ob]ar</paragraph>' );
  232. doc.enqueueChanges( () => {
  233. doc.batch().setAttribute( doc.selection.getFirstRange(), 'bold', true );
  234. } );
  235. output( '<paragraph>fo[<$text bold="true">ob</$text>]ar</paragraph>' );
  236. doc.enqueueChanges( () => {
  237. setSelection( [ 0, 3 ], [ 0, 3 ] );
  238. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  239. } );
  240. output( '<paragraph>fo<$text bold="true">o</$text>zzz<$text bold="true">[]b</$text>ar</paragraph>' );
  241. expect( doc.selection.getAttribute( 'bold' ) ).to.true;
  242. editor.execute( 'undo' );
  243. output( '<paragraph>fo<$text bold="true">o[]b</$text>ar</paragraph>' );
  244. expect( doc.selection.getAttribute( 'bold' ) ).to.true;
  245. editor.execute( 'undo' );
  246. output( '<paragraph>fo[ob]ar</paragraph>' );
  247. undoDisabled();
  248. } );
  249. } );
  250. describe( 'wrapping, unwrapping, merging, splitting', () => {
  251. it( 'wrap and undo', () => {
  252. doc.schema.allow( { name: '$text', inside: '$root' } );
  253. input( 'fo[zb]ar' );
  254. doc.enqueueChanges( () => {
  255. doc.batch().wrap( doc.selection.getFirstRange(), 'paragraph' );
  256. } );
  257. output( 'fo<paragraph>[zb]</paragraph>ar' );
  258. editor.execute( 'undo' );
  259. output( 'fo[zb]ar' );
  260. undoDisabled();
  261. } );
  262. it( 'wrap, move and undo', () => {
  263. doc.schema.allow( { name: '$text', inside: '$root' } );
  264. input( 'fo[zb]ar' );
  265. doc.enqueueChanges( () => {
  266. doc.batch().wrap( doc.selection.getFirstRange(), 'paragraph' );
  267. } );
  268. // Would be better if selection was inside P.
  269. output( 'fo<paragraph>[zb]</paragraph>ar' );
  270. doc.enqueueChanges( () => {
  271. setSelection( [ 2, 0 ], [ 2, 1 ] );
  272. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 0 ] ) );
  273. } );
  274. output( '[z]fo<paragraph>b</paragraph>ar' );
  275. editor.execute( 'undo' );
  276. output( 'fo<paragraph>[z]b</paragraph>ar' );
  277. editor.execute( 'undo' );
  278. output( 'fo[zb]ar' );
  279. undoDisabled();
  280. } );
  281. it( 'unwrap and undo', () => {
  282. input( '<paragraph>foo[]bar</paragraph>' );
  283. doc.enqueueChanges( () => {
  284. doc.batch().unwrap( doc.selection.getFirstPosition().parent );
  285. } );
  286. output( 'foo[]bar' );
  287. editor.execute( 'undo' );
  288. output( '<paragraph>foo[]bar</paragraph>' );
  289. undoDisabled();
  290. } );
  291. it( 'merge and undo', () => {
  292. input( '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>' );
  293. doc.enqueueChanges( () => {
  294. doc.batch().merge( new Position( root, [ 1 ] ) );
  295. // Because selection is stuck with <paragraph> it ends up in graveyard. We have to manually move it to correct node.
  296. setSelection( [ 0, 3 ], [ 0, 3 ] );
  297. } );
  298. output( '<paragraph>foo[]bar</paragraph>' );
  299. editor.execute( 'undo' );
  300. output( '<paragraph>foo</paragraph><paragraph>bar[]</paragraph>' );
  301. undoDisabled();
  302. } );
  303. it( 'split and undo', () => {
  304. input( '<paragraph>foo[]bar</paragraph>' );
  305. doc.enqueueChanges( () => {
  306. doc.batch().split( doc.selection.getFirstPosition() );
  307. // Because selection is stuck with <paragraph> it ends up in wrong node. We have to manually move it to correct node.
  308. setSelection( [ 1, 0 ], [ 1, 0 ] );
  309. } );
  310. output( '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>' );
  311. editor.execute( 'undo' );
  312. output( '<paragraph>foobar[]</paragraph>' );
  313. undoDisabled();
  314. } );
  315. } );
  316. // Restoring selection in those examples may be completely off.
  317. describe( 'multiple enters, deletes and typing', () => {
  318. it( 'split, split, split', () => {
  319. input( '<paragraph>12345678</paragraph>' );
  320. split( [ 0, 3 ] );
  321. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  322. split( [ 1, 4 ] );
  323. output( '<paragraph>123</paragraph><paragraph>4567</paragraph><paragraph>[]8</paragraph>' );
  324. split( [ 1, 2 ] );
  325. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]67</paragraph><paragraph>8</paragraph>' );
  326. editor.execute( 'undo' );
  327. output( '<paragraph>123</paragraph><paragraph>4567[]</paragraph><paragraph>8</paragraph>' );
  328. editor.execute( 'undo' );
  329. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  330. editor.execute( 'undo' );
  331. output( '<paragraph>12345678[]</paragraph>' );
  332. undoDisabled();
  333. editor.execute( 'redo' );
  334. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  335. editor.execute( 'redo' );
  336. output( '<paragraph>123</paragraph><paragraph>4567</paragraph><paragraph>8[]</paragraph>' );
  337. editor.execute( 'redo' );
  338. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>67[]</paragraph><paragraph>8</paragraph>' );
  339. redoDisabled();
  340. } );
  341. it( 'merge, merge, merge', () => {
  342. input( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  343. merge( [ 1 ] );
  344. output( '<paragraph>123[]45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  345. merge( [ 2 ] );
  346. output( '<paragraph>12345</paragraph><paragraph>67[]8</paragraph>' );
  347. merge( [ 1 ] );
  348. output( '<paragraph>12345[]678</paragraph>' );
  349. editor.execute( 'undo' );
  350. output( '<paragraph>12345</paragraph><paragraph>678[]</paragraph>' );
  351. editor.execute( 'undo' );
  352. output( '<paragraph>12345</paragraph><paragraph>67</paragraph><paragraph>8[]</paragraph>' );
  353. editor.execute( 'undo' );
  354. output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  355. undoDisabled();
  356. editor.execute( 'redo' );
  357. output( '<paragraph>12345[]</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  358. editor.execute( 'redo' );
  359. output( '<paragraph>12345</paragraph><paragraph>678[]</paragraph>' );
  360. editor.execute( 'redo' );
  361. output( '<paragraph>12345678[]</paragraph>' );
  362. redoDisabled();
  363. } );
  364. it( 'split, merge, split, merge (same position)', () => {
  365. input( '<paragraph>12345678</paragraph>' );
  366. split( [ 0, 3 ] );
  367. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  368. merge( [ 1 ] );
  369. output( '<paragraph>123[]45678</paragraph>' );
  370. split( [ 0, 3 ] );
  371. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  372. merge( [ 1 ] );
  373. output( '<paragraph>123[]45678</paragraph>' );
  374. editor.execute( 'undo' );
  375. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  376. editor.execute( 'undo' );
  377. output( '<paragraph>12345678[]</paragraph>' );
  378. editor.execute( 'undo' );
  379. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  380. editor.execute( 'undo' );
  381. output( '<paragraph>12345678[]</paragraph>' );
  382. undoDisabled();
  383. editor.execute( 'redo' );
  384. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  385. editor.execute( 'redo' );
  386. output( '<paragraph>12345678[]</paragraph>' );
  387. editor.execute( 'redo' );
  388. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  389. editor.execute( 'redo' );
  390. output( '<paragraph>12345678[]</paragraph>' );
  391. redoDisabled();
  392. } );
  393. it( 'split, split, split, merge, merge, merge', () => {
  394. input( '<paragraph>12345678</paragraph>' );
  395. split( [ 0, 3 ] );
  396. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  397. split( [ 1, 4 ] );
  398. output( '<paragraph>123</paragraph><paragraph>4567</paragraph><paragraph>[]8</paragraph>' );
  399. split( [ 1, 2 ] );
  400. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]67</paragraph><paragraph>8</paragraph>' );
  401. merge( [ 1 ] );
  402. output( '<paragraph>123[]45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  403. merge( [ 2 ] );
  404. output( '<paragraph>12345</paragraph><paragraph>67[]8</paragraph>' );
  405. merge( [ 1 ] );
  406. output( '<paragraph>12345[]678</paragraph>' );
  407. editor.execute( 'undo' );
  408. output( '<paragraph>12345</paragraph><paragraph>678[]</paragraph>' );
  409. editor.execute( 'undo' );
  410. output( '<paragraph>12345</paragraph><paragraph>67</paragraph><paragraph>8[]</paragraph>' );
  411. editor.execute( 'undo' );
  412. output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  413. editor.execute( 'undo' );
  414. output( '<paragraph>123</paragraph><paragraph>4567[]</paragraph><paragraph>8</paragraph>' );
  415. editor.execute( 'undo' );
  416. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  417. editor.execute( 'undo' );
  418. output( '<paragraph>12345678[]</paragraph>' );
  419. undoDisabled();
  420. editor.execute( 'redo' );
  421. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  422. editor.execute( 'redo' );
  423. output( '<paragraph>123</paragraph><paragraph>4567</paragraph><paragraph>8[]</paragraph>' );
  424. editor.execute( 'redo' );
  425. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>67[]</paragraph><paragraph>8</paragraph>' );
  426. editor.execute( 'redo' );
  427. output( '<paragraph>12345[]</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  428. editor.execute( 'redo' );
  429. output( '<paragraph>12345</paragraph><paragraph>678[]</paragraph>' );
  430. editor.execute( 'redo' );
  431. output( '<paragraph>12345678[]</paragraph>' );
  432. redoDisabled();
  433. } );
  434. it( 'split, split, merge, split, merge (different order)', () => {
  435. input( '<paragraph>12345678</paragraph>' );
  436. split( [ 0, 3 ] );
  437. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  438. split( [ 1, 2 ] );
  439. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]678</paragraph>' );
  440. merge( [ 1 ] );
  441. output( '<paragraph>123[]45</paragraph><paragraph>678</paragraph>' );
  442. split( [ 1, 1 ] );
  443. output( '<paragraph>12345</paragraph><paragraph>6</paragraph><paragraph>[]78</paragraph>' );
  444. merge( [ 1 ] );
  445. output( '<paragraph>12345[]6</paragraph><paragraph>78</paragraph>' );
  446. editor.execute( 'undo' );
  447. output( '<paragraph>12345</paragraph><paragraph>6[]</paragraph><paragraph>78</paragraph>' );
  448. editor.execute( 'undo' );
  449. output( '<paragraph>12345</paragraph><paragraph>678[]</paragraph>' );
  450. editor.execute( 'undo' );
  451. output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>678</paragraph>' );
  452. editor.execute( 'undo' );
  453. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  454. editor.execute( 'undo' );
  455. output( '<paragraph>12345678[]</paragraph>' );
  456. undoDisabled();
  457. editor.execute( 'redo' );
  458. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  459. editor.execute( 'redo' );
  460. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>678[]</paragraph>' );
  461. editor.execute( 'redo' );
  462. output( '<paragraph>12345[]</paragraph><paragraph>678</paragraph>' );
  463. editor.execute( 'redo' );
  464. output( '<paragraph>12345</paragraph><paragraph>6</paragraph><paragraph>78[]</paragraph>' );
  465. editor.execute( 'redo' );
  466. output( '<paragraph>123456[]</paragraph><paragraph>78</paragraph>' );
  467. redoDisabled();
  468. } );
  469. it( 'split, remove, split, merge, merge', () => {
  470. input( '<paragraph>12345678</paragraph>' );
  471. split( [ 0, 3 ] );
  472. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  473. remove( [ 1, 4 ] );
  474. remove( [ 1, 3 ] );
  475. output( '<paragraph>123</paragraph><paragraph>45[]8</paragraph>' );
  476. split( [ 1, 1 ] );
  477. output( '<paragraph>123</paragraph><paragraph>4</paragraph><paragraph>[]58</paragraph>' );
  478. merge( [ 1 ] );
  479. output( '<paragraph>123[]4</paragraph><paragraph>58</paragraph>' );
  480. merge( [ 1 ] );
  481. output( '<paragraph>1234[]58</paragraph>' );
  482. editor.execute( 'undo' );
  483. output( '<paragraph>1234</paragraph><paragraph>58[]</paragraph>' );
  484. editor.execute( 'undo' );
  485. output( '<paragraph>123</paragraph><paragraph>4[]</paragraph><paragraph>58</paragraph>' );
  486. editor.execute( 'undo' );
  487. output( '<paragraph>123</paragraph><paragraph>458[]</paragraph>' );
  488. editor.execute( 'undo' );
  489. output( '<paragraph>123</paragraph><paragraph>4567[]8</paragraph>' );
  490. editor.execute( 'undo' );
  491. output( '<paragraph>12345678[]</paragraph>' );
  492. undoDisabled();
  493. editor.execute( 'redo' );
  494. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  495. editor.execute( 'redo' );
  496. output( '<paragraph>123</paragraph><paragraph>458[]</paragraph>' );
  497. editor.execute( 'redo' );
  498. output( '<paragraph>123</paragraph><paragraph>4[]</paragraph><paragraph>58</paragraph>' );
  499. editor.execute( 'redo' );
  500. output( '<paragraph>1234[]</paragraph><paragraph>58</paragraph>' );
  501. editor.execute( 'redo' );
  502. output( '<paragraph>123458[]</paragraph>' );
  503. redoDisabled();
  504. } );
  505. it( 'split, typing, split, merge, merge', () => {
  506. input( '<paragraph>12345678</paragraph>' );
  507. split( [ 0, 3 ] );
  508. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  509. type( [ 1, 4 ], 'x' );
  510. type( [ 1, 5 ], 'y' );
  511. output( '<paragraph>123</paragraph><paragraph>4567xy[]8</paragraph>' );
  512. split( [ 1, 2 ] );
  513. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]67xy8</paragraph>' );
  514. merge( [ 1 ] );
  515. output( '<paragraph>123[]45</paragraph><paragraph>67xy8</paragraph>' );
  516. merge( [ 1 ] );
  517. output( '<paragraph>12345[]67xy8</paragraph>' );
  518. editor.execute( 'undo' );
  519. output( '<paragraph>12345</paragraph><paragraph>67xy8[]</paragraph>' );
  520. editor.execute( 'undo' );
  521. output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>67xy8</paragraph>' );
  522. editor.execute( 'undo' );
  523. output( '<paragraph>123</paragraph><paragraph>4567xy8[]</paragraph>' );
  524. editor.execute( 'undo' );
  525. output( '<paragraph>123</paragraph><paragraph>4567[]8</paragraph>' );
  526. editor.execute( 'undo' );
  527. output( '<paragraph>12345678[]</paragraph>' );
  528. undoDisabled();
  529. editor.execute( 'redo' );
  530. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  531. editor.execute( 'redo' );
  532. output( '<paragraph>123</paragraph><paragraph>4567xy8[]</paragraph>' );
  533. editor.execute( 'redo' );
  534. output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>67xy8</paragraph>' );
  535. editor.execute( 'redo' );
  536. output( '<paragraph>12345[]</paragraph><paragraph>67xy8</paragraph>' );
  537. editor.execute( 'redo' );
  538. output( '<paragraph>1234567xy8[]</paragraph>' );
  539. redoDisabled();
  540. } );
  541. } );
  542. describe( 'other reported cases', () => {
  543. // ckeditor5-engine#t/1051
  544. it( 'rename leaks to other elements on undo #1', () => {
  545. input( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  546. doc.batch().rename( root.getChild( 0 ), 'paragraph' );
  547. output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  548. doc.batch().split( Position.createAt( root.getChild( 0 ), 1 ) );
  549. output( '<paragraph>[]F</paragraph><paragraph>oo</paragraph><paragraph>Bar</paragraph>' );
  550. doc.batch().merge( Position.createAt( root, 2 ) );
  551. output( '<paragraph>[]F</paragraph><paragraph>ooBar</paragraph>' );
  552. editor.execute( 'undo' );
  553. output( '<paragraph>[]F</paragraph><paragraph>oo</paragraph><paragraph>Bar</paragraph>' );
  554. editor.execute( 'undo' );
  555. output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  556. editor.execute( 'undo' );
  557. output( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  558. } );
  559. // Similar issue that bases on the same error as above, however here we first merge (above we first split).
  560. it( 'rename leaks to other elements on undo #2', () => {
  561. input( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  562. doc.batch().rename( root.getChild( 0 ), 'heading2' );
  563. output( '<heading2>[]Foo</heading2><paragraph>Bar</paragraph>' );
  564. doc.batch().merge( Position.createAt( root, 1 ) );
  565. output( '<heading2>[]FooBar</heading2>' );
  566. editor.execute( 'undo' );
  567. output( '<heading2>[]Foo</heading2><paragraph>Bar</paragraph>' );
  568. editor.execute( 'undo' );
  569. output( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  570. } );
  571. // Reverse issue, this time first operation is merge and then rename.
  572. it( 'merge, rename, undo, undo is correct', () => {
  573. input( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  574. doc.batch().merge( Position.createAt( root, 1 ) );
  575. output( '<heading1>[]FooBar</heading1>' );
  576. doc.batch().rename( root.getChild( 0 ), 'heading2' );
  577. output( '<heading2>[]FooBar</heading2>' );
  578. editor.execute( 'undo' );
  579. output( '<heading1>[]FooBar</heading1>' );
  580. editor.execute( 'undo' );
  581. output( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  582. } );
  583. // ckeditor5-engine#t/1053
  584. it( 'wrap, split, undo, undo is correct', () => {
  585. input( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  586. doc.batch().wrap( Range.createIn( root ), 'div' );
  587. output( '<div><paragraph>[]Foo</paragraph><paragraph>Bar</paragraph></div>' );
  588. doc.batch().split( new Position( root, [ 0, 0, 1 ] ) );
  589. output( '<div><paragraph>[]F</paragraph><paragraph>oo</paragraph><paragraph>Bar</paragraph></div>' );
  590. editor.execute( 'undo' );
  591. output( '<div><paragraph>[]Foo</paragraph><paragraph>Bar</paragraph></div>' );
  592. editor.execute( 'undo' );
  593. output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  594. } );
  595. // ckeditor5-engine#t/1055
  596. it( 'selection attribute setting: split, bold, merge, undo, undo, undo', () => {
  597. input( '<paragraph>Foo[]</paragraph><paragraph>Bar</paragraph>' );
  598. editor.execute( 'enter' );
  599. output( '<paragraph>Foo</paragraph><paragraph>[]</paragraph><paragraph>Bar</paragraph>' );
  600. editor.execute( 'bold' );
  601. output(
  602. '<paragraph>Foo</paragraph>' +
  603. '<paragraph selection:bold="true"><$text bold="true">[]</$text></paragraph>' +
  604. '<paragraph>Bar</paragraph>'
  605. );
  606. editor.execute( 'forwardDelete' );
  607. output( '<paragraph>Foo</paragraph><paragraph>[]Bar</paragraph>' );
  608. editor.execute( 'undo' );
  609. output(
  610. '<paragraph>Foo</paragraph>' +
  611. '<paragraph selection:bold="true"><$text bold="true">[]</$text></paragraph>' +
  612. '<paragraph>Bar</paragraph>'
  613. );
  614. editor.execute( 'undo' );
  615. output( '<paragraph>Foo</paragraph><paragraph>[]</paragraph><paragraph>Bar</paragraph>' );
  616. editor.execute( 'undo' );
  617. output( '<paragraph>Foo[]</paragraph><paragraph>Bar</paragraph>' );
  618. } );
  619. } );
  620. describe( 'pasting', () => {
  621. function pasteHtml( editor, html ) {
  622. editor.editing.view.fire( 'paste', {
  623. dataTransfer: createDataTransfer( { 'text/html': html } ),
  624. preventDefault() {}
  625. } );
  626. }
  627. function createDataTransfer( data ) {
  628. return {
  629. getData( type ) {
  630. return data[ type ];
  631. }
  632. };
  633. }
  634. // ckeditor5-engine#t/1065
  635. it( 'undo paste into non empty element should not throw and be correct', () => {
  636. doc.enqueueChanges( () => {
  637. input( '<paragraph>Foo[]</paragraph>' );
  638. } );
  639. doc.enqueueChanges( () => {
  640. pasteHtml( editor, '<p>a</p><p>b</p>' );
  641. } );
  642. output( '<paragraph>Fooa</paragraph><paragraph>b[]</paragraph>' );
  643. doc.enqueueChanges( () => {
  644. pasteHtml( editor, '<p>c</p><p>d</p>' );
  645. } );
  646. output( '<paragraph>Fooa</paragraph><paragraph>bc</paragraph><paragraph>d[]</paragraph>' );
  647. editor.execute( 'undo' );
  648. output( '<paragraph>Fooa</paragraph><paragraph>b[]</paragraph>' );
  649. editor.execute( 'undo' );
  650. output( '<paragraph>Foo[]</paragraph>' );
  651. } );
  652. } );
  653. describe( 'other edge cases', () => {
  654. it( 'deleteContent between two nodes', () => {
  655. input( '<paragraph>fo[o</paragraph><paragraph>b]ar</paragraph>' );
  656. doc.enqueueChanges( () => {
  657. editor.data.deleteContent( doc.selection, doc.batch() );
  658. } );
  659. output( '<paragraph>fo[]ar</paragraph>' );
  660. editor.execute( 'undo' );
  661. output( '<paragraph>fo[o</paragraph><paragraph>b]ar</paragraph>' );
  662. } );
  663. // Related to ckeditor5-engine#891 and ckeditor5-list#51.
  664. it( 'change attribute of removed node then undo and redo', () => {
  665. input( '<paragraph></paragraph>' );
  666. const gy = doc.graveyard;
  667. const p = doc.getRoot().getChild( 0 );
  668. doc.enqueueChanges( () => {
  669. doc.batch().remove( p );
  670. doc.batch().setAttribute( p, 'bold', true );
  671. } );
  672. editor.execute( 'undo' );
  673. editor.execute( 'redo' );
  674. expect( p.root ).to.equal( gy );
  675. expect( p.getAttribute( 'bold' ) ).to.be.true;
  676. } );
  677. it( 'undoing merge after it was already "undone" through transforming by insert delta', () => {
  678. // This is a tricky case that happens during collaborative editing.
  679. // When merge happens at the same place where insert, the merge is automatically undone.
  680. // Then, when the merge is actually undone, the problem occurs.
  681. input( '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>' );
  682. // Remove children from graveyard because they are inserted there after `input` call.
  683. doc.graveyard.removeChildren( 0, doc.graveyard.childCount );
  684. const batchWithMerge = doc.batch();
  685. doc.enqueueChanges( () => {
  686. batchWithMerge.merge( new Position( root, [ 1 ] ) );
  687. const split = batchWithMerge.deltas[ 0 ].getReversed();
  688. const batch = doc.batch();
  689. batch.addDelta( split );
  690. doc.applyOperation( split.operations[ 0 ] );
  691. doc.applyOperation( split.operations[ 1 ] );
  692. } );
  693. output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  694. editor.execute( 'undo', batchWithMerge );
  695. output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  696. } );
  697. } );
  698. function split( path ) {
  699. setSelection( path.slice(), path.slice() );
  700. editor.execute( 'enter' );
  701. }
  702. function merge( path ) {
  703. const selPath = path.slice();
  704. selPath.push( 0 );
  705. setSelection( selPath, selPath.slice() );
  706. editor.execute( 'delete' );
  707. }
  708. function type( path, text ) {
  709. setSelection( path.slice(), path.slice() );
  710. editor.execute( 'input', { text } );
  711. }
  712. function remove( path ) {
  713. setSelection( path.slice(), path.slice() );
  714. editor.execute( 'delete' );
  715. }
  716. } );