8
0

undoediting-integration.js 34 KB

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