undoengine-integration.js 20 KB

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