undoengine-integration.js 22 KB

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