8
0

undoediting-integration.js 33 KB

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