undoediting-integration.js 33 KB

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