undoediting-integration.js 32 KB

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