8
0

undoediting-integration.js 33 KB

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