undoediting-integration.js 34 KB

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