8
0

undoengine-integration.js 32 KB

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