8
0

undoediting-integration.js 33 KB

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