undoediting-integration.js 34 KB

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