undoediting-integration.js 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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 UndoEditing from '../src/undoediting';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import HeadingEditing from '@ckeditor/ckeditor5-heading/src/headingediting';
  10. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  11. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  12. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  13. import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
  14. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  15. describe( 'UndoEditing integration', () => {
  16. let editor, model, doc, root, div;
  17. beforeEach( () => {
  18. div = document.createElement( 'div' );
  19. document.body.appendChild( div );
  20. return ClassicEditor.create( div, { plugins: [ Paragraph, HeadingEditing, Typing, Enter, Clipboard, BoldEditing, UndoEditing ] } )
  21. .then( newEditor => {
  22. editor = newEditor;
  23. model = editor.model;
  24. doc = model.document;
  25. root = doc.getRoot();
  26. } );
  27. } );
  28. afterEach( () => {
  29. div.remove();
  30. return editor.destroy();
  31. } );
  32. function setSelection( pathA, pathB ) {
  33. model.change( writer => {
  34. writer.setSelection( writer.createRange(
  35. writer.createPositionFromPath( root, pathA ), writer.createPositionFromPath( root, pathB )
  36. ) );
  37. } );
  38. }
  39. function input( input ) {
  40. model.enqueueChange( 'transparent', () => {
  41. setData( model, input );
  42. } );
  43. }
  44. function output( output ) {
  45. expect( getData( model ) ).to.equal( output );
  46. }
  47. function undoDisabled() {
  48. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false;
  49. }
  50. function redoDisabled() {
  51. expect( editor.commands.get( 'redo' ).isEnabled ).to.be.false;
  52. }
  53. describe( 'adding and removing content', () => {
  54. it( 'add and undo', () => {
  55. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  56. model.change( writer => {
  57. writer.insertText( 'zzz', doc.selection.getFirstPosition() );
  58. } );
  59. output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
  60. editor.execute( 'undo' );
  61. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  62. undoDisabled();
  63. } );
  64. it( 'multiple adding and undo', () => {
  65. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  66. model.change( writer => {
  67. writer.insertText( 'zzz', doc.selection.getFirstPosition() );
  68. writer.insertText( 'xxx', writer.createPositionFromPath( root, [ 1, 0 ] ) );
  69. } );
  70. output( '<paragraph>fozzz[]o</paragraph><paragraph>xxxbar</paragraph>' );
  71. model.change( writer => {
  72. setSelection( [ 1, 0 ], [ 1, 0 ] );
  73. writer.insertText( 'yyy', doc.selection.getFirstPosition() );
  74. } );
  75. output( '<paragraph>fozzzo</paragraph><paragraph>yyy[]xxxbar</paragraph>' );
  76. editor.execute( 'undo' );
  77. output( '<paragraph>fozzzo</paragraph><paragraph>[]xxxbar</paragraph>' );
  78. editor.execute( 'undo' );
  79. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  80. undoDisabled();
  81. } );
  82. it( 'multiple adding mixed with undo', () => {
  83. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  84. model.change( writer => {
  85. writer.insertText( 'zzz', doc.selection.getFirstPosition() );
  86. } );
  87. output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
  88. model.change( writer => {
  89. setSelection( [ 1, 0 ], [ 1, 0 ] );
  90. writer.insertText( 'yyy', doc.selection.getFirstPosition() );
  91. } );
  92. output( '<paragraph>fozzzo</paragraph><paragraph>yyy[]bar</paragraph>' );
  93. editor.execute( 'undo' );
  94. output( '<paragraph>fozzzo</paragraph><paragraph>[]bar</paragraph>' );
  95. model.change( writer => {
  96. setSelection( [ 0, 0 ], [ 0, 0 ] );
  97. writer.insertText( 'xxx', doc.selection.getFirstPosition() );
  98. } );
  99. output( '<paragraph>xxx[]fozzzo</paragraph><paragraph>bar</paragraph>' );
  100. editor.execute( 'undo' );
  101. output( '<paragraph>[]fozzzo</paragraph><paragraph>bar</paragraph>' );
  102. editor.execute( 'undo' );
  103. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  104. undoDisabled();
  105. } );
  106. it( 'multiple remove and undo', () => {
  107. input( '<paragraph>[]foo</paragraph><paragraph>bar</paragraph>' );
  108. model.change( writer => {
  109. const start = doc.selection.getFirstPosition();
  110. writer.remove( writer.createRange( start, start.getShiftedBy( 2 ) ) );
  111. } );
  112. output( '<paragraph>[]o</paragraph><paragraph>bar</paragraph>' );
  113. model.change( writer => {
  114. setSelection( [ 1, 1 ], [ 1, 1 ] );
  115. const start = doc.selection.getFirstPosition();
  116. writer.remove( writer.createRange( start, start.getShiftedBy( 2 ) ) );
  117. } );
  118. output( '<paragraph>o</paragraph><paragraph>b[]</paragraph>' );
  119. editor.execute( 'undo' );
  120. // Here is an edge case that selection could be before or after `ar`.
  121. output( '<paragraph>o</paragraph><paragraph>bar[]</paragraph>' );
  122. editor.execute( 'undo' );
  123. // As above.
  124. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  125. undoDisabled();
  126. } );
  127. it( 'add and remove different parts and undo', () => {
  128. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  129. model.change( writer => {
  130. writer.insertText( 'zzz', doc.selection.getFirstPosition() );
  131. } );
  132. output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
  133. model.change( writer => {
  134. setSelection( [ 1, 2 ], [ 1, 2 ] );
  135. const start = writer.createPositionFromPath( root, [ 1, 1 ] );
  136. writer.remove( writer.createRange( start, start.getShiftedBy( 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. const start = writer.createPositionFromPath( root, [ 0, 2 ] );
  153. writer.remove( writer.createRange( start, start.getShiftedBy( 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( writer.createRangeIn( 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( writer.createRangeIn( 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 = model.createPositionFromPath( 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(), writer.createPositionFromPath( root, [ 1, 0 ] ) );
  205. } );
  206. output( '<paragraph>fz</paragraph><paragraph>[o]bar</paragraph>' );
  207. model.change( writer => {
  208. writer.move( doc.selection.getFirstRange(), writer.createPositionFromPath( 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(), writer.createPositionFromPath( 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(), writer.createPositionFromPath( 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(), writer.createPositionFromPath( 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( writer.createPositionFromPath( root, [ 1 ] ) );
  303. } );
  304. output( '<paragraph>foo[]bar</paragraph>' );
  305. editor.execute( 'undo' );
  306. output( '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
  307. undoDisabled();
  308. } );
  309. it( 'split and undo', () => {
  310. input( '<paragraph>foo[]bar</paragraph>' );
  311. model.change( writer => {
  312. writer.split( doc.selection.getFirstPosition() );
  313. } );
  314. output( '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
  315. editor.execute( 'undo' );
  316. output( '<paragraph>foo[]bar</paragraph>' );
  317. undoDisabled();
  318. } );
  319. } );
  320. // Restoring selection in those examples may be completely off.
  321. describe( 'multiple enters, deletes and typing', () => {
  322. it( 'split, split, split', () => {
  323. input( '<paragraph>12345678</paragraph>' );
  324. split( [ 0, 3 ] );
  325. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  326. split( [ 1, 4 ] );
  327. output( '<paragraph>123</paragraph><paragraph>4567</paragraph><paragraph>[]8</paragraph>' );
  328. split( [ 1, 2 ] );
  329. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]67</paragraph><paragraph>8</paragraph>' );
  330. editor.execute( 'undo' );
  331. output( '<paragraph>123</paragraph><paragraph>45[]67</paragraph><paragraph>8</paragraph>' );
  332. editor.execute( 'undo' );
  333. output( '<paragraph>123</paragraph><paragraph>4567[]8</paragraph>' );
  334. editor.execute( 'undo' );
  335. output( '<paragraph>123[]45678</paragraph>' );
  336. undoDisabled();
  337. editor.execute( 'redo' );
  338. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  339. editor.execute( 'redo' );
  340. output( '<paragraph>123</paragraph><paragraph>4567[]</paragraph><paragraph>8</paragraph>' );
  341. editor.execute( 'redo' );
  342. output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  343. redoDisabled();
  344. } );
  345. it( 'merge, merge, merge', () => {
  346. input( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  347. merge( [ 1 ] );
  348. output( '<paragraph>123[]45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  349. merge( [ 2 ] );
  350. output( '<paragraph>12345</paragraph><paragraph>67[]8</paragraph>' );
  351. merge( [ 1 ] );
  352. output( '<paragraph>12345[]678</paragraph>' );
  353. editor.execute( 'undo' );
  354. output( '<paragraph>12345[]</paragraph><paragraph>678</paragraph>' );
  355. editor.execute( 'undo' );
  356. output( '<paragraph>12345</paragraph><paragraph>67[]</paragraph><paragraph>8</paragraph>' );
  357. editor.execute( 'undo' );
  358. output( '<paragraph>123[]</paragraph><paragraph>45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  359. undoDisabled();
  360. editor.execute( 'redo' );
  361. output( '<paragraph>123[]45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  362. editor.execute( 'redo' );
  363. output( '<paragraph>12345</paragraph><paragraph>67[]8</paragraph>' );
  364. editor.execute( 'redo' );
  365. output( '<paragraph>12345[]678</paragraph>' );
  366. redoDisabled();
  367. } );
  368. it( 'split, merge, split, merge (same position)', () => {
  369. input( '<paragraph>12345678</paragraph>' );
  370. split( [ 0, 3 ] );
  371. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  372. merge( [ 1 ] );
  373. output( '<paragraph>123[]45678</paragraph>' );
  374. split( [ 0, 3 ] );
  375. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  376. merge( [ 1 ] );
  377. output( '<paragraph>123[]45678</paragraph>' );
  378. editor.execute( 'undo' );
  379. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  380. editor.execute( 'undo' );
  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>123[]45678</paragraph>' );
  386. undoDisabled();
  387. editor.execute( 'redo' );
  388. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  389. editor.execute( 'redo' );
  390. output( '<paragraph>123[]45678</paragraph>' );
  391. editor.execute( 'redo' );
  392. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  393. editor.execute( 'redo' );
  394. output( '<paragraph>123[]45678</paragraph>' );
  395. redoDisabled();
  396. } );
  397. it( 'split, split, split, merge, merge, merge', () => {
  398. input( '<paragraph>12345678</paragraph>' );
  399. split( [ 0, 3 ] );
  400. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  401. split( [ 1, 4 ] );
  402. output( '<paragraph>123</paragraph><paragraph>4567</paragraph><paragraph>[]8</paragraph>' );
  403. split( [ 1, 2 ] );
  404. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]67</paragraph><paragraph>8</paragraph>' );
  405. merge( [ 1 ] );
  406. output( '<paragraph>123[]45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  407. merge( [ 2 ] );
  408. output( '<paragraph>12345</paragraph><paragraph>67[]8</paragraph>' );
  409. merge( [ 1 ] );
  410. output( '<paragraph>12345[]678</paragraph>' );
  411. editor.execute( 'undo' );
  412. output( '<paragraph>12345[]</paragraph><paragraph>678</paragraph>' );
  413. editor.execute( 'undo' );
  414. output( '<paragraph>12345</paragraph><paragraph>67[]</paragraph><paragraph>8</paragraph>' );
  415. editor.execute( 'undo' );
  416. output( '<paragraph>123[]</paragraph><paragraph>45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  417. editor.execute( 'undo' );
  418. output( '<paragraph>123</paragraph><paragraph>45[]67</paragraph><paragraph>8</paragraph>' );
  419. editor.execute( 'undo' );
  420. output( '<paragraph>123</paragraph><paragraph>4567[]8</paragraph>' );
  421. editor.execute( 'undo' );
  422. output( '<paragraph>123[]45678</paragraph>' );
  423. undoDisabled();
  424. editor.execute( 'redo' );
  425. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  426. editor.execute( 'redo' );
  427. output( '<paragraph>123</paragraph><paragraph>4567[]</paragraph><paragraph>8</paragraph>' );
  428. editor.execute( 'redo' );
  429. output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  430. editor.execute( 'redo' );
  431. output( '<paragraph>123[]45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  432. editor.execute( 'redo' );
  433. output( '<paragraph>12345</paragraph><paragraph>67[]8</paragraph>' );
  434. editor.execute( 'redo' );
  435. output( '<paragraph>12345[]678</paragraph>' );
  436. redoDisabled();
  437. } );
  438. it( 'split, split, merge, split, merge (different order)', () => {
  439. input( '<paragraph>12345678</paragraph>' );
  440. split( [ 0, 3 ] );
  441. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  442. split( [ 1, 2 ] );
  443. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]678</paragraph>' );
  444. merge( [ 1 ] );
  445. output( '<paragraph>123[]45</paragraph><paragraph>678</paragraph>' );
  446. split( [ 1, 1 ] );
  447. output( '<paragraph>12345</paragraph><paragraph>6</paragraph><paragraph>[]78</paragraph>' );
  448. merge( [ 1 ] );
  449. output( '<paragraph>12345[]6</paragraph><paragraph>78</paragraph>' );
  450. editor.execute( 'undo' );
  451. output( '<paragraph>12345[]</paragraph><paragraph>6</paragraph><paragraph>78</paragraph>' );
  452. editor.execute( 'undo' );
  453. output( '<paragraph>12345</paragraph><paragraph>6[]78</paragraph>' );
  454. editor.execute( 'undo' );
  455. output( '<paragraph>123[]</paragraph><paragraph>45</paragraph><paragraph>678</paragraph>' );
  456. editor.execute( 'undo' );
  457. output( '<paragraph>123</paragraph><paragraph>45[]678</paragraph>' );
  458. editor.execute( 'undo' );
  459. output( '<paragraph>123[]45678</paragraph>' );
  460. undoDisabled();
  461. editor.execute( 'redo' );
  462. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  463. editor.execute( 'redo' );
  464. output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>678</paragraph>' );
  465. editor.execute( 'redo' );
  466. output( '<paragraph>123[]45</paragraph><paragraph>678</paragraph>' );
  467. editor.execute( 'redo' );
  468. output( '<paragraph>12345</paragraph><paragraph>6[]</paragraph><paragraph>78</paragraph>' );
  469. editor.execute( 'redo' );
  470. output( '<paragraph>12345[]6</paragraph><paragraph>78</paragraph>' );
  471. redoDisabled();
  472. } );
  473. it( 'split, remove, split, merge, merge', () => {
  474. input( '<paragraph>12345678</paragraph>' );
  475. split( [ 0, 3 ] );
  476. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  477. remove( [ 1, 4 ] );
  478. remove( [ 1, 3 ] );
  479. output( '<paragraph>123</paragraph><paragraph>45[]8</paragraph>' );
  480. split( [ 1, 1 ] );
  481. output( '<paragraph>123</paragraph><paragraph>4</paragraph><paragraph>[]58</paragraph>' );
  482. merge( [ 1 ] );
  483. output( '<paragraph>123[]4</paragraph><paragraph>58</paragraph>' );
  484. merge( [ 1 ] );
  485. output( '<paragraph>1234[]58</paragraph>' );
  486. editor.execute( 'undo' );
  487. output( '<paragraph>1234[]</paragraph><paragraph>58</paragraph>' );
  488. editor.execute( 'undo' );
  489. output( '<paragraph>123[]</paragraph><paragraph>4</paragraph><paragraph>58</paragraph>' );
  490. editor.execute( 'undo' );
  491. output( '<paragraph>123</paragraph><paragraph>4[]58</paragraph>' );
  492. editor.execute( 'undo' );
  493. output( '<paragraph>123</paragraph><paragraph>4567[]8</paragraph>' );
  494. editor.execute( 'undo' );
  495. output( '<paragraph>123[]45678</paragraph>' );
  496. undoDisabled();
  497. editor.execute( 'redo' );
  498. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  499. editor.execute( 'redo' );
  500. output( '<paragraph>123</paragraph><paragraph>45[]8</paragraph>' );
  501. editor.execute( 'redo' );
  502. output( '<paragraph>123</paragraph><paragraph>4[]</paragraph><paragraph>58</paragraph>' );
  503. editor.execute( 'redo' );
  504. output( '<paragraph>123[]4</paragraph><paragraph>58</paragraph>' );
  505. editor.execute( 'redo' );
  506. output( '<paragraph>1234[]58</paragraph>' );
  507. redoDisabled();
  508. } );
  509. it( 'split, typing, split, merge, merge', () => {
  510. input( '<paragraph>12345678</paragraph>' );
  511. split( [ 0, 3 ] );
  512. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  513. type( [ 1, 4 ], 'x' );
  514. type( [ 1, 5 ], 'y' );
  515. output( '<paragraph>123</paragraph><paragraph>4567xy[]8</paragraph>' );
  516. split( [ 1, 2 ] );
  517. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]67xy8</paragraph>' );
  518. merge( [ 1 ] );
  519. output( '<paragraph>123[]45</paragraph><paragraph>67xy8</paragraph>' );
  520. merge( [ 1 ] );
  521. output( '<paragraph>12345[]67xy8</paragraph>' );
  522. editor.execute( 'undo' );
  523. output( '<paragraph>12345[]</paragraph><paragraph>67xy8</paragraph>' );
  524. editor.execute( 'undo' );
  525. output( '<paragraph>123[]</paragraph><paragraph>45</paragraph><paragraph>67xy8</paragraph>' );
  526. editor.execute( 'undo' );
  527. output( '<paragraph>123</paragraph><paragraph>45[]67xy8</paragraph>' );
  528. editor.execute( 'undo' );
  529. output( '<paragraph>123</paragraph><paragraph>4567[]8</paragraph>' );
  530. editor.execute( 'undo' );
  531. output( '<paragraph>123[]45678</paragraph>' );
  532. undoDisabled();
  533. editor.execute( 'redo' );
  534. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  535. editor.execute( 'redo' );
  536. output( '<paragraph>123</paragraph><paragraph>4567xy[]8</paragraph>' );
  537. editor.execute( 'redo' );
  538. output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>67xy8</paragraph>' );
  539. editor.execute( 'redo' );
  540. output( '<paragraph>123[]45</paragraph><paragraph>67xy8</paragraph>' );
  541. editor.execute( 'redo' );
  542. output( '<paragraph>12345[]67xy8</paragraph>' );
  543. redoDisabled();
  544. } );
  545. } );
  546. describe( 'other reported cases', () => {
  547. // ckeditor5-engine#t/1051
  548. it( 'rename leaks to other elements on undo #1', () => {
  549. input( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  550. model.change( writer => {
  551. writer.rename( root.getChild( 0 ), 'paragraph' );
  552. } );
  553. output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  554. model.change( writer => {
  555. writer.split( writer.createPositionAt( root.getChild( 0 ), 1 ) );
  556. } );
  557. output( '<paragraph>[]F</paragraph><paragraph>oo</paragraph><paragraph>Bar</paragraph>' );
  558. model.change( writer => {
  559. writer.merge( writer.createPositionAt( root, 2 ) );
  560. } );
  561. output( '<paragraph>[]F</paragraph><paragraph>ooBar</paragraph>' );
  562. editor.execute( 'undo' );
  563. output( '<paragraph>[]F</paragraph><paragraph>oo</paragraph><paragraph>Bar</paragraph>' );
  564. editor.execute( 'undo' );
  565. output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  566. editor.execute( 'undo' );
  567. output( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  568. } );
  569. // Similar issue that bases on the same error as above, however here we first merge (above we first split).
  570. it( 'rename leaks to other elements on undo #2', () => {
  571. input( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  572. model.change( writer => {
  573. writer.rename( root.getChild( 0 ), 'heading2' );
  574. } );
  575. output( '<heading2>[]Foo</heading2><paragraph>Bar</paragraph>' );
  576. model.change( writer => {
  577. writer.merge( writer.createPositionAt( root, 1 ) );
  578. } );
  579. output( '<heading2>[]FooBar</heading2>' );
  580. editor.execute( 'undo' );
  581. output( '<heading2>[]Foo</heading2><paragraph>Bar</paragraph>' );
  582. editor.execute( 'undo' );
  583. output( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  584. } );
  585. // Reverse issue, this time first operation is merge and then rename.
  586. it( 'merge, rename, undo, undo is correct', () => {
  587. input( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  588. model.change( writer => {
  589. writer.merge( writer.createPositionAt( root, 1 ) );
  590. } );
  591. output( '<heading1>[]FooBar</heading1>' );
  592. model.change( writer => {
  593. writer.rename( root.getChild( 0 ), 'heading2' );
  594. } );
  595. output( '<heading2>[]FooBar</heading2>' );
  596. editor.execute( 'undo' );
  597. output( '<heading1>[]FooBar</heading1>' );
  598. editor.execute( 'undo' );
  599. output( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  600. } );
  601. // ckeditor5-engine#t/1053
  602. it( 'wrap, split, undo, undo is correct', () => {
  603. // Add a "div feature".
  604. model.schema.register( 'div', {
  605. allowWhere: '$block',
  606. allowContentOf: '$root'
  607. } );
  608. editor.conversion.for( 'downcast' ).elementToElement( { model: 'div', view: 'div' } );
  609. editor.conversion.for( 'upcast' ).elementToElement( { model: 'div', view: 'div' } );
  610. input( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  611. model.change( writer => {
  612. writer.wrap( writer.createRangeIn( root ), 'div' );
  613. } );
  614. output( '<div><paragraph>[]Foo</paragraph><paragraph>Bar</paragraph></div>' );
  615. model.change( writer => {
  616. writer.split( writer.createPositionFromPath( root, [ 0, 0, 1 ] ) );
  617. } );
  618. output( '<div><paragraph>[]F</paragraph><paragraph>oo</paragraph><paragraph>Bar</paragraph></div>' );
  619. editor.execute( 'undo' );
  620. output( '<div><paragraph>[]Foo</paragraph><paragraph>Bar</paragraph></div>' );
  621. editor.execute( 'undo' );
  622. output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  623. } );
  624. // ckeditor5-engine#t/1055
  625. it( 'selection attribute setting: split, bold, merge, undo, undo, undo', () => {
  626. input( '<paragraph>Foo[]</paragraph><paragraph>Bar</paragraph>' );
  627. editor.execute( 'enter' );
  628. output( '<paragraph>Foo</paragraph><paragraph>[]</paragraph><paragraph>Bar</paragraph>' );
  629. editor.execute( 'bold' );
  630. output(
  631. '<paragraph>Foo</paragraph>' +
  632. '<paragraph selection:bold="true"><$text bold="true">[]</$text></paragraph>' +
  633. '<paragraph>Bar</paragraph>'
  634. );
  635. editor.execute( 'forwardDelete' );
  636. output( '<paragraph>Foo</paragraph><paragraph>[]Bar</paragraph>' );
  637. editor.execute( 'undo' );
  638. output(
  639. '<paragraph>Foo</paragraph>' +
  640. '<paragraph selection:bold="true"><$text bold="true">[]</$text></paragraph>' +
  641. '<paragraph>Bar</paragraph>'
  642. );
  643. editor.execute( 'undo' );
  644. output( '<paragraph>Foo</paragraph><paragraph>[]</paragraph><paragraph>Bar</paragraph>' );
  645. editor.execute( 'undo' );
  646. output( '<paragraph>Foo[]</paragraph><paragraph>Bar</paragraph>' );
  647. } );
  648. // https://github.com/ckeditor/ckeditor5-undo/issues/65#issuecomment-323682195
  649. it( 'undoing split after the element created by split has been removed', () => {
  650. input( '<paragraph>Foo[]bar</paragraph>' );
  651. editor.execute( 'enter' );
  652. model.change( writer => {
  653. const range = writer.createRange(
  654. writer.createPositionFromPath( root, [ 0, 3 ] ),
  655. writer.createPositionFromPath( root, [ 1, 3 ] )
  656. );
  657. writer.setSelection( range );
  658. editor.execute( 'delete' );
  659. } );
  660. output( '<paragraph>Foo[]</paragraph>' );
  661. editor.execute( 'undo' );
  662. output( '<paragraph>Foo[</paragraph><paragraph>bar]</paragraph>' );
  663. editor.execute( 'undo' );
  664. output( '<paragraph>Foo[]bar</paragraph>' );
  665. } );
  666. } );
  667. describe( 'clipboard', () => {
  668. function pasteHtml( editor, html ) {
  669. editor.editing.view.document.fire( 'paste', {
  670. dataTransfer: createDataTransfer( { 'text/html': html } ),
  671. preventDefault() {}
  672. } );
  673. }
  674. function createDataTransfer( data ) {
  675. return {
  676. getData( type ) {
  677. return data[ type ];
  678. },
  679. setData() {}
  680. };
  681. }
  682. // ckeditor5-engine#t/1065
  683. it( 'undo paste into non empty element should not throw and be correct', () => {
  684. model.change( () => {
  685. input( '<paragraph>Foo[]</paragraph>' );
  686. } );
  687. model.change( () => {
  688. pasteHtml( editor, '<p>a</p><p>b</p>' );
  689. } );
  690. output( '<paragraph>Fooa</paragraph><paragraph>b[]</paragraph>' );
  691. model.change( () => {
  692. pasteHtml( editor, '<p>c</p><p>d</p>' );
  693. } );
  694. output( '<paragraph>Fooa</paragraph><paragraph>bc</paragraph><paragraph>d[]</paragraph>' );
  695. editor.execute( 'undo' );
  696. output( '<paragraph>Fooa</paragraph><paragraph>b[]</paragraph>' );
  697. editor.execute( 'undo' );
  698. output( '<paragraph>Foo[]</paragraph>' );
  699. } );
  700. // ckeditor5#781
  701. it( 'cutting should not create empty undo step', () => {
  702. input( '<paragraph>Fo[oba]r</paragraph>' );
  703. editor.editing.view.document.fire( 'cut', {
  704. dataTransfer: createDataTransfer(),
  705. preventDefault() {},
  706. method: 'cut'
  707. } );
  708. editor.execute( 'undo' );
  709. output( '<paragraph>Fo[oba]r</paragraph>' );
  710. undoDisabled();
  711. } );
  712. } );
  713. describe( 'other edge cases', () => {
  714. it( 'deleteContent between two nodes', () => {
  715. input( '<paragraph>fo[o</paragraph><paragraph>b]ar</paragraph>' );
  716. editor.model.deleteContent( doc.selection );
  717. output( '<paragraph>fo[]ar</paragraph>' );
  718. editor.execute( 'undo' );
  719. output( '<paragraph>fo[o</paragraph><paragraph>b]ar</paragraph>' );
  720. } );
  721. // Related to ckeditor5-engine#891 and ckeditor5-list#51.
  722. it( 'change attribute of removed node then undo and redo', () => {
  723. input( '<paragraph></paragraph>' );
  724. const gy = doc.graveyard;
  725. const p = doc.getRoot().getChild( 0 );
  726. model.change( writer => {
  727. writer.remove( p );
  728. } );
  729. model.change( writer => {
  730. writer.setAttribute( 'bold', true, p );
  731. } );
  732. editor.execute( 'undo' );
  733. editor.execute( 'redo' );
  734. expect( p.root ).to.equal( gy );
  735. expect( p.getAttribute( 'bold' ) ).to.be.true;
  736. } );
  737. } );
  738. it( 'postfixers should not add another undo step when fixing undo changes', () => {
  739. input( '<paragraph>[]</paragraph>' );
  740. const paragraph = root.getChild( 0 );
  741. // 1. Step - add text and marker to the paragraph.
  742. model.change( writer => {
  743. writer.appendText( 'foo', paragraph );
  744. writer.addMarker( 'marker', { range: writer.createRangeIn( paragraph ), usingOperation: true } );
  745. } );
  746. // 2. Step - remove text from paragraph.
  747. model.change( writer => {
  748. writer.remove( writer.createRangeIn( paragraph ) );
  749. } );
  750. // Register post fixer to remove marker from non-empty paragraph.
  751. model.document.registerPostFixer( writer => {
  752. const hasMarker = model.markers.has( 'marker' );
  753. const isEmpty = paragraph.childCount === 0;
  754. // Remove marker when paragraph is empty.
  755. if ( hasMarker && !isEmpty ) {
  756. writer.removeMarker( 'marker' );
  757. return true;
  758. }
  759. return false;
  760. } );
  761. editor.execute( 'undo' );
  762. // Check if postfixer changes are executed together with undo and there is no additional undo steps.
  763. expect( model.markers.has( 'marker' ) ).to.be.false;
  764. expect( editor.commands.get( 'undo' )._stack.length ).to.equal( 1 );
  765. output( '<paragraph>foo[]</paragraph>' );
  766. } );
  767. function split( path ) {
  768. setSelection( path.slice(), path.slice() );
  769. editor.execute( 'enter' );
  770. }
  771. function merge( path ) {
  772. const selPath = path.slice();
  773. selPath.push( 0 );
  774. setSelection( selPath, selPath.slice() );
  775. editor.execute( 'delete' );
  776. }
  777. function type( path, text ) {
  778. setSelection( path.slice(), path.slice() );
  779. editor.execute( 'input', { text } );
  780. }
  781. function remove( path ) {
  782. setSelection( path.slice(), path.slice() );
  783. editor.execute( 'delete' );
  784. }
  785. } );