undoengine-integration.js 32 KB

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