undoediting-integration.js 32 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 UndoEditing from '../src/undoediting';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import HeadingEditing from '@ckeditor/ckeditor5-heading/src/headingediting';
  12. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  13. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  14. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  15. import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
  16. import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
  17. import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
  18. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  19. describe( 'UndoEditing integration', () => {
  20. let editor, model, doc, root, div;
  21. beforeEach( () => {
  22. div = document.createElement( 'div' );
  23. document.body.appendChild( div );
  24. return ClassicEditor.create( div, { plugins: [ Paragraph, HeadingEditing, Typing, Enter, Clipboard, BoldEditing, UndoEditing ] } )
  25. .then( newEditor => {
  26. editor = newEditor;
  27. model = editor.model;
  28. doc = model.document;
  29. root = doc.getRoot();
  30. } );
  31. } );
  32. afterEach( () => {
  33. div.remove();
  34. return editor.destroy();
  35. } );
  36. function setSelection( pathA, pathB ) {
  37. model.change( writer => {
  38. writer.setSelection( new Range( new Position( root, pathA ), new Position( root, pathB ) ) );
  39. } );
  40. }
  41. function input( input ) {
  42. model.enqueueChange( 'transparent', () => {
  43. setData( model, input );
  44. } );
  45. }
  46. function output( output ) {
  47. expect( getData( model ) ).to.equal( output );
  48. }
  49. function undoDisabled() {
  50. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false;
  51. }
  52. function redoDisabled() {
  53. expect( editor.commands.get( 'redo' ).isEnabled ).to.be.false;
  54. }
  55. describe( 'adding and removing content', () => {
  56. it( 'add and undo', () => {
  57. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  58. model.change( writer => {
  59. writer.insertText( 'zzz', doc.selection.getFirstPosition() );
  60. } );
  61. output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
  62. editor.execute( 'undo' );
  63. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  64. undoDisabled();
  65. } );
  66. it( 'multiple adding and undo', () => {
  67. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  68. model.change( writer => {
  69. writer.insertText( 'zzz', doc.selection.getFirstPosition() );
  70. writer.insertText( 'xxx', new Position( root, [ 1, 0 ] ) );
  71. } );
  72. output( '<paragraph>fozzz[]o</paragraph><paragraph>xxxbar</paragraph>' );
  73. model.change( writer => {
  74. setSelection( [ 1, 0 ], [ 1, 0 ] );
  75. writer.insertText( 'yyy', doc.selection.getFirstPosition() );
  76. } );
  77. output( '<paragraph>fozzzo</paragraph><paragraph>yyy[]xxxbar</paragraph>' );
  78. editor.execute( 'undo' );
  79. output( '<paragraph>fozzzo</paragraph><paragraph>[]xxxbar</paragraph>' );
  80. editor.execute( 'undo' );
  81. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  82. undoDisabled();
  83. } );
  84. it( 'multiple adding mixed with undo', () => {
  85. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  86. model.change( writer => {
  87. writer.insertText( 'zzz', doc.selection.getFirstPosition() );
  88. } );
  89. output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
  90. model.change( writer => {
  91. setSelection( [ 1, 0 ], [ 1, 0 ] );
  92. writer.insertText( 'yyy', doc.selection.getFirstPosition() );
  93. } );
  94. output( '<paragraph>fozzzo</paragraph><paragraph>yyy[]bar</paragraph>' );
  95. editor.execute( 'undo' );
  96. output( '<paragraph>fozzzo</paragraph><paragraph>[]bar</paragraph>' );
  97. model.change( writer => {
  98. setSelection( [ 0, 0 ], [ 0, 0 ] );
  99. writer.insertText( 'xxx', doc.selection.getFirstPosition() );
  100. } );
  101. output( '<paragraph>xxx[]fozzzo</paragraph><paragraph>bar</paragraph>' );
  102. editor.execute( 'undo' );
  103. output( '<paragraph>[]fozzzo</paragraph><paragraph>bar</paragraph>' );
  104. editor.execute( 'undo' );
  105. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  106. undoDisabled();
  107. } );
  108. it( 'multiple remove and undo', () => {
  109. input( '<paragraph>[]foo</paragraph><paragraph>bar</paragraph>' );
  110. model.change( writer => {
  111. writer.remove( Range.createFromPositionAndShift( doc.selection.getFirstPosition(), 2 ) );
  112. } );
  113. output( '<paragraph>[]o</paragraph><paragraph>bar</paragraph>' );
  114. model.change( writer => {
  115. setSelection( [ 1, 1 ], [ 1, 1 ] );
  116. writer.remove( Range.createFromPositionAndShift( doc.selection.getFirstPosition(), 2 ) );
  117. } );
  118. output( '<paragraph>o</paragraph><paragraph>b[]</paragraph>' );
  119. editor.execute( 'undo' );
  120. // Here is an edge case that selection could be before or after `ar`.
  121. output( '<paragraph>o</paragraph><paragraph>bar[]</paragraph>' );
  122. editor.execute( 'undo' );
  123. // As above.
  124. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  125. undoDisabled();
  126. } );
  127. it( 'add and remove different parts and undo', () => {
  128. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  129. model.change( writer => {
  130. writer.insertText( 'zzz', doc.selection.getFirstPosition() );
  131. } );
  132. output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
  133. model.change( writer => {
  134. setSelection( [ 1, 2 ], [ 1, 2 ] );
  135. writer.remove( Range.createFromPositionAndShift( new Position( root, [ 1, 1 ] ), 1 ) );
  136. } );
  137. output( '<paragraph>fozzzo</paragraph><paragraph>b[]r</paragraph>' );
  138. editor.execute( 'undo' );
  139. output( '<paragraph>fozzzo</paragraph><paragraph>ba[]r</paragraph>' );
  140. editor.execute( 'undo' );
  141. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  142. undoDisabled();
  143. } );
  144. it( 'add and remove same part and undo', () => {
  145. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  146. model.change( writer => {
  147. writer.insertText( 'zzz', doc.selection.getFirstPosition() );
  148. } );
  149. output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
  150. model.change( writer => {
  151. writer.remove( Range.createFromPositionAndShift( new Position( root, [ 0, 2 ] ), 3 ) );
  152. } );
  153. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  154. editor.execute( 'undo' );
  155. output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
  156. editor.execute( 'undo' );
  157. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  158. undoDisabled();
  159. } );
  160. it( 'undo remove all content', () => {
  161. input( '<paragraph>foo[]</paragraph>' );
  162. model.change( writer => {
  163. writer.remove( Range.createIn( root ) );
  164. } );
  165. output( '<paragraph>[]</paragraph>' ); // All hail our king and savior, autoparagraphing!
  166. editor.execute( 'undo' );
  167. output( '<paragraph>foo[]</paragraph>' );
  168. undoDisabled();
  169. } );
  170. it( 'undo insert first content', () => {
  171. input( '' );
  172. output( '<paragraph>[]</paragraph>' ); // All hail our king and savior, autoparagraphing!
  173. model.change( writer => {
  174. writer.remove( Range.createIn( root ) );
  175. writer.insertElement( 'heading1', doc.selection.getFirstPosition() );
  176. } );
  177. output( '<heading1>[]</heading1>' );
  178. editor.execute( 'undo' );
  179. output( '<paragraph>[]</paragraph>' );
  180. undoDisabled();
  181. } );
  182. // #72.
  183. it( 'undo paste multiple elements simulation', () => {
  184. input( '<paragraph></paragraph>' );
  185. const p = root.getChild( 0 );
  186. const pos = new Position( root, [ 0 ] );
  187. model.change( writer => {
  188. writer.remove( p );
  189. writer.insertElement( 'heading1', pos );
  190. writer.insertElement( 'heading2', pos.getShiftedBy( 1 ) );
  191. } );
  192. output( '<heading1>[]</heading1><heading2></heading2>' );
  193. editor.execute( 'undo' );
  194. output( '<paragraph>[]</paragraph>' );
  195. undoDisabled();
  196. } );
  197. } );
  198. describe( 'moving', () => {
  199. it( 'move same content twice then undo', () => {
  200. input( '<paragraph>f[o]z</paragraph><paragraph>bar</paragraph>' );
  201. model.change( writer => {
  202. writer.move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) );
  203. } );
  204. output( '<paragraph>fz</paragraph><paragraph>[o]bar</paragraph>' );
  205. model.change( writer => {
  206. writer.move( doc.selection.getFirstRange(), new Position( root, [ 0, 2 ] ) );
  207. } );
  208. output( '<paragraph>fz[o]</paragraph><paragraph>bar</paragraph>' );
  209. editor.execute( 'undo' );
  210. output( '<paragraph>fz</paragraph><paragraph>[o]bar</paragraph>' );
  211. editor.execute( 'undo' );
  212. output( '<paragraph>f[o]z</paragraph><paragraph>bar</paragraph>' );
  213. undoDisabled();
  214. } );
  215. it( 'move content and new parent then undo', () => {
  216. input( '<paragraph>f[o]z</paragraph><paragraph>bar</paragraph>' );
  217. model.change( writer => {
  218. writer.move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) );
  219. } );
  220. output( '<paragraph>fz</paragraph><paragraph>[o]bar</paragraph>' );
  221. model.change( writer => {
  222. setSelection( [ 1 ], [ 2 ] );
  223. writer.move( doc.selection.getFirstRange(), new Position( root, [ 0 ] ) );
  224. } );
  225. output( '<paragraph>[obar]</paragraph><paragraph>fz</paragraph>' );
  226. editor.execute( 'undo' );
  227. output( '<paragraph>fz</paragraph><paragraph>[obar]</paragraph>' );
  228. editor.execute( 'undo' );
  229. output( '<paragraph>f[o]z</paragraph><paragraph>bar</paragraph>' );
  230. undoDisabled();
  231. } );
  232. } );
  233. describe( 'attributes with other', () => {
  234. it( 'attributes then insert inside then undo', () => {
  235. input( '<paragraph>fo[ob]ar</paragraph>' );
  236. model.change( writer => {
  237. writer.setAttribute( 'bold', true, doc.selection.getFirstRange() );
  238. } );
  239. output( '<paragraph>fo[<$text bold="true">ob</$text>]ar</paragraph>' );
  240. expect( doc.selection.getAttribute( 'bold' ) ).to.be.true;
  241. model.change( writer => {
  242. setSelection( [ 0, 3 ], [ 0, 3 ] );
  243. writer.insertText( 'zzz', doc.selection.getFirstPosition() );
  244. } );
  245. output( '<paragraph>fo<$text bold="true">o</$text>zzz[]<$text bold="true">b</$text>ar</paragraph>' );
  246. expect( doc.selection.getAttribute( 'bold' ) ).to.be.undefined;
  247. editor.execute( 'undo' );
  248. output( '<paragraph>fo<$text bold="true">o[]b</$text>ar</paragraph>' );
  249. expect( doc.selection.getAttribute( 'bold' ) ).to.be.true;
  250. editor.execute( 'undo' );
  251. output( '<paragraph>fo[ob]ar</paragraph>' );
  252. expect( doc.selection.getAttribute( 'bold' ) ).to.be.undefined;
  253. undoDisabled();
  254. } );
  255. } );
  256. describe( 'wrapping, unwrapping, merging, splitting', () => {
  257. it( 'wrap and undo', () => {
  258. model.schema.extend( '$text', { allowIn: '$root' } );
  259. input( 'fo[zb]ar' );
  260. model.change( writer => {
  261. writer.wrap( doc.selection.getFirstRange(), 'paragraph' );
  262. } );
  263. output( 'fo<paragraph>[zb]</paragraph>ar' );
  264. editor.execute( 'undo' );
  265. output( 'fo[zb]ar' );
  266. undoDisabled();
  267. } );
  268. it( 'wrap, move and undo', () => {
  269. model.schema.extend( '$text', { allowIn: '$root' } );
  270. input( 'fo[zb]ar' );
  271. model.change( writer => {
  272. writer.wrap( doc.selection.getFirstRange(), 'paragraph' );
  273. } );
  274. // Would be better if selection was inside P.
  275. output( 'fo<paragraph>[zb]</paragraph>ar' );
  276. model.change( writer => {
  277. setSelection( [ 2, 0 ], [ 2, 1 ] );
  278. writer.move( doc.selection.getFirstRange(), new Position( root, [ 0 ] ) );
  279. } );
  280. output( '[z]fo<paragraph>b</paragraph>ar' );
  281. editor.execute( 'undo' );
  282. output( 'fo<paragraph>[z]b</paragraph>ar' );
  283. editor.execute( 'undo' );
  284. output( 'fo[zb]ar' );
  285. undoDisabled();
  286. } );
  287. it( 'unwrap and undo', () => {
  288. input( '<paragraph>foo[]bar</paragraph>' );
  289. model.change( writer => {
  290. writer.unwrap( doc.selection.getFirstPosition().parent );
  291. } );
  292. output( 'foo[]bar' );
  293. editor.execute( 'undo' );
  294. output( '<paragraph>foo[]bar</paragraph>' );
  295. undoDisabled();
  296. } );
  297. it( 'merge and undo', () => {
  298. input( '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>' );
  299. model.change( writer => {
  300. writer.merge( new Position( root, [ 1 ] ) );
  301. } );
  302. output( '<paragraph>foo[]bar</paragraph>' );
  303. editor.execute( 'undo' );
  304. output( '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
  305. undoDisabled();
  306. } );
  307. it( 'split and undo', () => {
  308. input( '<paragraph>foo[]bar</paragraph>' );
  309. model.change( writer => {
  310. writer.split( doc.selection.getFirstPosition() );
  311. } );
  312. output( '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
  313. editor.execute( 'undo' );
  314. output( '<paragraph>foo[]bar</paragraph>' );
  315. undoDisabled();
  316. } );
  317. } );
  318. // Restoring selection in those examples may be completely off.
  319. describe( 'multiple enters, deletes and typing', () => {
  320. it( 'split, split, split', () => {
  321. input( '<paragraph>12345678</paragraph>' );
  322. split( [ 0, 3 ] );
  323. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  324. split( [ 1, 4 ] );
  325. output( '<paragraph>123</paragraph><paragraph>4567</paragraph><paragraph>[]8</paragraph>' );
  326. split( [ 1, 2 ] );
  327. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]67</paragraph><paragraph>8</paragraph>' );
  328. editor.execute( 'undo' );
  329. output( '<paragraph>123</paragraph><paragraph>45[]67</paragraph><paragraph>8</paragraph>' );
  330. editor.execute( 'undo' );
  331. output( '<paragraph>123</paragraph><paragraph>4567[]8</paragraph>' );
  332. editor.execute( 'undo' );
  333. output( '<paragraph>123[]45678</paragraph>' );
  334. undoDisabled();
  335. editor.execute( 'redo' );
  336. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  337. editor.execute( 'redo' );
  338. output( '<paragraph>123</paragraph><paragraph>4567[]</paragraph><paragraph>8</paragraph>' );
  339. editor.execute( 'redo' );
  340. output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  341. redoDisabled();
  342. } );
  343. it( 'merge, merge, merge', () => {
  344. input( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  345. merge( [ 1 ] );
  346. output( '<paragraph>123[]45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  347. merge( [ 2 ] );
  348. output( '<paragraph>12345</paragraph><paragraph>67[]8</paragraph>' );
  349. merge( [ 1 ] );
  350. output( '<paragraph>12345[]678</paragraph>' );
  351. editor.execute( 'undo' );
  352. output( '<paragraph>12345[]</paragraph><paragraph>678</paragraph>' );
  353. editor.execute( 'undo' );
  354. output( '<paragraph>12345</paragraph><paragraph>67[]</paragraph><paragraph>8</paragraph>' );
  355. editor.execute( 'undo' );
  356. output( '<paragraph>123[]</paragraph><paragraph>45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  357. undoDisabled();
  358. editor.execute( 'redo' );
  359. output( '<paragraph>123[]45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  360. editor.execute( 'redo' );
  361. output( '<paragraph>12345</paragraph><paragraph>67[]8</paragraph>' );
  362. editor.execute( 'redo' );
  363. output( '<paragraph>12345[]678</paragraph>' );
  364. redoDisabled();
  365. } );
  366. it( 'split, merge, split, merge (same position)', () => {
  367. input( '<paragraph>12345678</paragraph>' );
  368. split( [ 0, 3 ] );
  369. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  370. merge( [ 1 ] );
  371. output( '<paragraph>123[]45678</paragraph>' );
  372. split( [ 0, 3 ] );
  373. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  374. merge( [ 1 ] );
  375. output( '<paragraph>123[]45678</paragraph>' );
  376. editor.execute( 'undo' );
  377. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  378. editor.execute( 'undo' );
  379. output( '<paragraph>123[]45678</paragraph>' );
  380. editor.execute( 'undo' );
  381. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  382. editor.execute( 'undo' );
  383. output( '<paragraph>123[]45678</paragraph>' );
  384. undoDisabled();
  385. editor.execute( 'redo' );
  386. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  387. editor.execute( 'redo' );
  388. output( '<paragraph>123[]45678</paragraph>' );
  389. editor.execute( 'redo' );
  390. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  391. editor.execute( 'redo' );
  392. output( '<paragraph>123[]45678</paragraph>' );
  393. redoDisabled();
  394. } );
  395. it( 'split, split, split, merge, merge, merge', () => {
  396. input( '<paragraph>12345678</paragraph>' );
  397. split( [ 0, 3 ] );
  398. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  399. split( [ 1, 4 ] );
  400. output( '<paragraph>123</paragraph><paragraph>4567</paragraph><paragraph>[]8</paragraph>' );
  401. split( [ 1, 2 ] );
  402. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]67</paragraph><paragraph>8</paragraph>' );
  403. merge( [ 1 ] );
  404. output( '<paragraph>123[]45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  405. merge( [ 2 ] );
  406. output( '<paragraph>12345</paragraph><paragraph>67[]8</paragraph>' );
  407. merge( [ 1 ] );
  408. output( '<paragraph>12345[]678</paragraph>' );
  409. editor.execute( 'undo' );
  410. output( '<paragraph>12345[]</paragraph><paragraph>678</paragraph>' );
  411. editor.execute( 'undo' );
  412. output( '<paragraph>12345</paragraph><paragraph>67[]</paragraph><paragraph>8</paragraph>' );
  413. editor.execute( 'undo' );
  414. output( '<paragraph>123[]</paragraph><paragraph>45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  415. editor.execute( 'undo' );
  416. output( '<paragraph>123</paragraph><paragraph>45[]67</paragraph><paragraph>8</paragraph>' );
  417. editor.execute( 'undo' );
  418. output( '<paragraph>123</paragraph><paragraph>4567[]8</paragraph>' );
  419. editor.execute( 'undo' );
  420. output( '<paragraph>123[]45678</paragraph>' );
  421. undoDisabled();
  422. editor.execute( 'redo' );
  423. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  424. editor.execute( 'redo' );
  425. output( '<paragraph>123</paragraph><paragraph>4567[]</paragraph><paragraph>8</paragraph>' );
  426. editor.execute( 'redo' );
  427. output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  428. editor.execute( 'redo' );
  429. output( '<paragraph>123[]45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  430. editor.execute( 'redo' );
  431. output( '<paragraph>12345</paragraph><paragraph>67[]8</paragraph>' );
  432. editor.execute( 'redo' );
  433. output( '<paragraph>12345[]678</paragraph>' );
  434. redoDisabled();
  435. } );
  436. it( 'split, split, merge, split, merge (different order)', () => {
  437. input( '<paragraph>12345678</paragraph>' );
  438. split( [ 0, 3 ] );
  439. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  440. split( [ 1, 2 ] );
  441. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]678</paragraph>' );
  442. merge( [ 1 ] );
  443. output( '<paragraph>123[]45</paragraph><paragraph>678</paragraph>' );
  444. split( [ 1, 1 ] );
  445. output( '<paragraph>12345</paragraph><paragraph>6</paragraph><paragraph>[]78</paragraph>' );
  446. merge( [ 1 ] );
  447. output( '<paragraph>12345[]6</paragraph><paragraph>78</paragraph>' );
  448. editor.execute( 'undo' );
  449. output( '<paragraph>12345[]</paragraph><paragraph>6</paragraph><paragraph>78</paragraph>' );
  450. editor.execute( 'undo' );
  451. output( '<paragraph>12345</paragraph><paragraph>6[]78</paragraph>' );
  452. editor.execute( 'undo' );
  453. output( '<paragraph>123[]</paragraph><paragraph>45</paragraph><paragraph>678</paragraph>' );
  454. editor.execute( 'undo' );
  455. output( '<paragraph>123</paragraph><paragraph>45[]678</paragraph>' );
  456. editor.execute( 'undo' );
  457. output( '<paragraph>123[]45678</paragraph>' );
  458. undoDisabled();
  459. editor.execute( 'redo' );
  460. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  461. editor.execute( 'redo' );
  462. output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>678</paragraph>' );
  463. editor.execute( 'redo' );
  464. output( '<paragraph>123[]45</paragraph><paragraph>678</paragraph>' );
  465. editor.execute( 'redo' );
  466. output( '<paragraph>12345</paragraph><paragraph>6[]</paragraph><paragraph>78</paragraph>' );
  467. editor.execute( 'redo' );
  468. output( '<paragraph>12345[]6</paragraph><paragraph>78</paragraph>' );
  469. redoDisabled();
  470. } );
  471. it( 'split, remove, split, merge, merge', () => {
  472. input( '<paragraph>12345678</paragraph>' );
  473. split( [ 0, 3 ] );
  474. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  475. remove( [ 1, 4 ] );
  476. remove( [ 1, 3 ] );
  477. output( '<paragraph>123</paragraph><paragraph>45[]8</paragraph>' );
  478. split( [ 1, 1 ] );
  479. output( '<paragraph>123</paragraph><paragraph>4</paragraph><paragraph>[]58</paragraph>' );
  480. merge( [ 1 ] );
  481. output( '<paragraph>123[]4</paragraph><paragraph>58</paragraph>' );
  482. merge( [ 1 ] );
  483. output( '<paragraph>1234[]58</paragraph>' );
  484. editor.execute( 'undo' );
  485. output( '<paragraph>1234[]</paragraph><paragraph>58</paragraph>' );
  486. editor.execute( 'undo' );
  487. output( '<paragraph>123[]</paragraph><paragraph>4</paragraph><paragraph>58</paragraph>' );
  488. editor.execute( 'undo' );
  489. output( '<paragraph>123</paragraph><paragraph>4[]58</paragraph>' );
  490. editor.execute( 'undo' );
  491. output( '<paragraph>123</paragraph><paragraph>4567[]8</paragraph>' );
  492. editor.execute( 'undo' );
  493. output( '<paragraph>123[]45678</paragraph>' );
  494. undoDisabled();
  495. editor.execute( 'redo' );
  496. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  497. editor.execute( 'redo' );
  498. output( '<paragraph>123</paragraph><paragraph>45[]8</paragraph>' );
  499. editor.execute( 'redo' );
  500. output( '<paragraph>123</paragraph><paragraph>4[]</paragraph><paragraph>58</paragraph>' );
  501. editor.execute( 'redo' );
  502. output( '<paragraph>123[]4</paragraph><paragraph>58</paragraph>' );
  503. editor.execute( 'redo' );
  504. output( '<paragraph>1234[]58</paragraph>' );
  505. redoDisabled();
  506. } );
  507. it( 'split, typing, split, merge, merge', () => {
  508. input( '<paragraph>12345678</paragraph>' );
  509. split( [ 0, 3 ] );
  510. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  511. type( [ 1, 4 ], 'x' );
  512. type( [ 1, 5 ], 'y' );
  513. output( '<paragraph>123</paragraph><paragraph>4567xy[]8</paragraph>' );
  514. split( [ 1, 2 ] );
  515. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]67xy8</paragraph>' );
  516. merge( [ 1 ] );
  517. output( '<paragraph>123[]45</paragraph><paragraph>67xy8</paragraph>' );
  518. merge( [ 1 ] );
  519. output( '<paragraph>12345[]67xy8</paragraph>' );
  520. editor.execute( 'undo' );
  521. output( '<paragraph>12345[]</paragraph><paragraph>67xy8</paragraph>' );
  522. editor.execute( 'undo' );
  523. output( '<paragraph>123[]</paragraph><paragraph>45</paragraph><paragraph>67xy8</paragraph>' );
  524. editor.execute( 'undo' );
  525. output( '<paragraph>123</paragraph><paragraph>45[]67xy8</paragraph>' );
  526. editor.execute( 'undo' );
  527. output( '<paragraph>123</paragraph><paragraph>4567[]8</paragraph>' );
  528. editor.execute( 'undo' );
  529. output( '<paragraph>123[]45678</paragraph>' );
  530. undoDisabled();
  531. editor.execute( 'redo' );
  532. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  533. editor.execute( 'redo' );
  534. output( '<paragraph>123</paragraph><paragraph>4567xy[]8</paragraph>' );
  535. editor.execute( 'redo' );
  536. output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>67xy8</paragraph>' );
  537. editor.execute( 'redo' );
  538. output( '<paragraph>123[]45</paragraph><paragraph>67xy8</paragraph>' );
  539. editor.execute( 'redo' );
  540. output( '<paragraph>12345[]67xy8</paragraph>' );
  541. redoDisabled();
  542. } );
  543. } );
  544. describe( 'other reported cases', () => {
  545. // ckeditor5-engine#t/1051
  546. it( 'rename leaks to other elements on undo #1', () => {
  547. input( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  548. model.change( writer => {
  549. writer.rename( root.getChild( 0 ), 'paragraph' );
  550. } );
  551. output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  552. model.change( writer => {
  553. writer.split( Position.createAt( root.getChild( 0 ), 1 ) );
  554. } );
  555. output( '<paragraph>[]F</paragraph><paragraph>oo</paragraph><paragraph>Bar</paragraph>' );
  556. model.change( writer => {
  557. writer.merge( Position.createAt( root, 2 ) );
  558. } );
  559. output( '<paragraph>[]F</paragraph><paragraph>ooBar</paragraph>' );
  560. editor.execute( 'undo' );
  561. output( '<paragraph>[]F</paragraph><paragraph>oo</paragraph><paragraph>Bar</paragraph>' );
  562. editor.execute( 'undo' );
  563. output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  564. editor.execute( 'undo' );
  565. output( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  566. } );
  567. // Similar issue that bases on the same error as above, however here we first merge (above we first split).
  568. it( 'rename leaks to other elements on undo #2', () => {
  569. input( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  570. model.change( writer => {
  571. writer.rename( root.getChild( 0 ), 'heading2' );
  572. } );
  573. output( '<heading2>[]Foo</heading2><paragraph>Bar</paragraph>' );
  574. model.change( writer => {
  575. writer.merge( Position.createAt( root, 1 ) );
  576. } );
  577. output( '<heading2>[]FooBar</heading2>' );
  578. editor.execute( 'undo' );
  579. output( '<heading2>[]Foo</heading2><paragraph>Bar</paragraph>' );
  580. editor.execute( 'undo' );
  581. output( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  582. } );
  583. // Reverse issue, this time first operation is merge and then rename.
  584. it( 'merge, rename, undo, undo is correct', () => {
  585. input( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  586. model.change( writer => {
  587. writer.merge( Position.createAt( root, 1 ) );
  588. } );
  589. output( '<heading1>[]FooBar</heading1>' );
  590. model.change( writer => {
  591. writer.rename( root.getChild( 0 ), 'heading2' );
  592. } );
  593. output( '<heading2>[]FooBar</heading2>' );
  594. editor.execute( 'undo' );
  595. output( '<heading1>[]FooBar</heading1>' );
  596. editor.execute( 'undo' );
  597. output( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  598. } );
  599. // ckeditor5-engine#t/1053
  600. it( 'wrap, split, undo, undo is correct', () => {
  601. // Add a "div feature".
  602. model.schema.register( 'div', {
  603. allowWhere: '$block',
  604. allowContentOf: '$root'
  605. } );
  606. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'div', view: 'div' } ) );
  607. editor.conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'div', view: 'div' } ) );
  608. input( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  609. model.change( writer => {
  610. writer.wrap( Range.createIn( root ), 'div' );
  611. } );
  612. output( '<div><paragraph>[]Foo</paragraph><paragraph>Bar</paragraph></div>' );
  613. model.change( writer => {
  614. writer.split( new Position( root, [ 0, 0, 1 ] ) );
  615. } );
  616. output( '<div><paragraph>[]F</paragraph><paragraph>oo</paragraph><paragraph>Bar</paragraph></div>' );
  617. editor.execute( 'undo' );
  618. output( '<div><paragraph>[]Foo</paragraph><paragraph>Bar</paragraph></div>' );
  619. editor.execute( 'undo' );
  620. output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  621. } );
  622. // ckeditor5-engine#t/1055
  623. it( 'selection attribute setting: split, bold, merge, undo, undo, undo', () => {
  624. input( '<paragraph>Foo[]</paragraph><paragraph>Bar</paragraph>' );
  625. editor.execute( 'enter' );
  626. output( '<paragraph>Foo</paragraph><paragraph>[]</paragraph><paragraph>Bar</paragraph>' );
  627. editor.execute( 'bold' );
  628. output(
  629. '<paragraph>Foo</paragraph>' +
  630. '<paragraph selection:bold="true"><$text bold="true">[]</$text></paragraph>' +
  631. '<paragraph>Bar</paragraph>'
  632. );
  633. editor.execute( 'forwardDelete' );
  634. output( '<paragraph>Foo</paragraph><paragraph>[]Bar</paragraph>' );
  635. editor.execute( 'undo' );
  636. output(
  637. '<paragraph>Foo</paragraph>' +
  638. '<paragraph selection:bold="true"><$text bold="true">[]</$text></paragraph>' +
  639. '<paragraph>Bar</paragraph>'
  640. );
  641. editor.execute( 'undo' );
  642. output( '<paragraph>Foo</paragraph><paragraph>[]</paragraph><paragraph>Bar</paragraph>' );
  643. editor.execute( 'undo' );
  644. output( '<paragraph>Foo[]</paragraph><paragraph>Bar</paragraph>' );
  645. } );
  646. // https://github.com/ckeditor/ckeditor5-undo/issues/65#issuecomment-323682195
  647. it( 'undoing split after the element created by split has been removed', () => {
  648. input( '<paragraph>Foo[]bar</paragraph>' );
  649. editor.execute( 'enter' );
  650. model.change( writer => {
  651. const range = new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 1, 3 ] ) );
  652. writer.setSelection( range );
  653. editor.execute( 'delete' );
  654. } );
  655. output( '<paragraph>Foo[]</paragraph>' );
  656. editor.execute( 'undo' );
  657. output( '<paragraph>Foo[</paragraph><paragraph>bar]</paragraph>' );
  658. editor.execute( 'undo' );
  659. output( '<paragraph>Foo[]bar</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( '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. } );