8
0

undoengine-integration.js 32 KB

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