8
0

undoengine-integration.js 32 KB

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