undoengine-integration.js 20 KB

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