undoengine-integration.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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. } );
  133. describe( 'moving', () => {
  134. it( 'move same content twice then undo', () => {
  135. input( '<p>f[o]z</p><p>bar</p>' );
  136. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) );
  137. output( '<p>fz</p><p>[o]bar</p>' );
  138. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 0, 2 ] ) );
  139. output( '<p>fz[o]</p><p>bar</p>' );
  140. editor.execute( 'undo' );
  141. output( '<p>fz</p><p>[o]bar</p>' );
  142. editor.execute( 'undo' );
  143. output( '<p>f[o]z</p><p>bar</p>' );
  144. undoDisabled();
  145. } );
  146. it( 'move content and new parent then undo', () => {
  147. input( '<p>f[o]z</p><p>bar</p>' );
  148. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) );
  149. output( '<p>fz</p><p>[o]bar</p>' );
  150. setSelection( [ 1 ], [ 2 ] );
  151. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 0 ] ) );
  152. output( '[<p>obar</p>]<p>fz</p>' );
  153. editor.execute( 'undo' );
  154. output( '<p>fz</p>[<p>obar</p>]' );
  155. editor.execute( 'undo' );
  156. output( '<p>f[o]z</p><p>bar</p>' );
  157. undoDisabled();
  158. } );
  159. } );
  160. describe( 'attributes with other', () => {
  161. it( 'attributes then insert inside then undo', () => {
  162. input( '<p>fo[ob]ar</p>' );
  163. doc.batch().setAttribute( doc.selection.getFirstRange(), 'bold', true );
  164. output( '<p>fo[<$text bold="true">ob</$text>]ar</p>' );
  165. setSelection( [ 0, 3 ], [ 0, 3 ] );
  166. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  167. output( '<p>fo<$text bold="true">o</$text>zzz<$text bold="true">[]b</$text>ar</p>' );
  168. expect( doc.selection.getAttribute( 'bold' ) ).to.true;
  169. editor.execute( 'undo' );
  170. output( '<p>fo<$text bold="true">o[]b</$text>ar</p>' );
  171. expect( doc.selection.getAttribute( 'bold' ) ).to.true;
  172. editor.execute( 'undo' );
  173. output( '<p>fo[ob]ar</p>' );
  174. undoDisabled();
  175. } );
  176. } );
  177. describe( 'wrapping, unwrapping, merging, splitting', () => {
  178. it( 'wrap and undo', () => {
  179. doc.schema.allow( { name: '$text', inside: '$root' } );
  180. input( 'fo[zb]ar' );
  181. doc.batch().wrap( doc.selection.getFirstRange(), 'p' );
  182. output( 'fo<p>[zb]</p>ar' );
  183. editor.execute( 'undo' );
  184. output( 'fo[zb]ar' );
  185. undoDisabled();
  186. } );
  187. it( 'wrap, move and undo', () => {
  188. doc.schema.allow( { name: '$text', inside: '$root' } );
  189. input( 'fo[zb]ar' );
  190. doc.batch().wrap( doc.selection.getFirstRange(), 'p' );
  191. // Would be better if selection was inside P.
  192. output( 'fo<p>[zb]</p>ar' );
  193. setSelection( [ 2, 0 ], [ 2, 1 ] );
  194. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 0 ] ) );
  195. output( '[z]fo<p>b</p>ar' );
  196. editor.execute( 'undo' );
  197. output( 'fo<p>[z]b</p>ar' );
  198. editor.execute( 'undo' );
  199. output( 'fo[zb]ar' );
  200. undoDisabled();
  201. } );
  202. it( 'unwrap and undo', () => {
  203. input( '<p>foo[]bar</p>' );
  204. doc.batch().unwrap( doc.selection.getFirstPosition().parent );
  205. output( 'foo[]bar' );
  206. editor.execute( 'undo' );
  207. output( '<p>foo[]bar</p>' );
  208. undoDisabled();
  209. } );
  210. it( 'merge and undo', () => {
  211. input( '<p>foo</p><p>[]bar</p>' );
  212. doc.batch().merge( new Position( root, [ 1 ] ) );
  213. // Because selection is stuck with <p> it ends up in graveyard. We have to manually move it to correct node.
  214. setSelection( [ 0, 3 ], [ 0, 3 ] );
  215. output( '<p>foo[]bar</p>' );
  216. editor.execute( 'undo' );
  217. output( '<p>foo</p><p>bar[]</p>' );
  218. undoDisabled();
  219. } );
  220. it( 'split and undo', () => {
  221. input( '<p>foo[]bar</p>' );
  222. doc.batch().split( doc.selection.getFirstPosition() );
  223. // Because selection is stuck with <p> it ends up in wrong node. We have to manually move it to correct node.
  224. setSelection( [ 1, 0 ], [ 1, 0 ] );
  225. output( '<p>foo</p><p>[]bar</p>' );
  226. editor.execute( 'undo' );
  227. output( '<p>foobar[]</p>' );
  228. undoDisabled();
  229. } );
  230. } );
  231. // Restoring selection in those examples may be completely off.
  232. describe( 'multiple enters, deletes and typing', () => {
  233. function split( path ) {
  234. setSelection( path.slice(), path.slice() );
  235. editor.execute( 'enter' );
  236. }
  237. function merge( path ) {
  238. const selPath = path.slice();
  239. selPath.push( 0 );
  240. setSelection( selPath, selPath.slice() );
  241. editor.execute( 'delete' );
  242. }
  243. function type( path, text ) {
  244. setSelection( path.slice(), path.slice() );
  245. editor.execute( 'input', { text } );
  246. }
  247. function remove( path ) {
  248. setSelection( path.slice(), path.slice() );
  249. editor.execute( 'delete' );
  250. }
  251. it( 'split, split, split', () => {
  252. input( '<p>12345678</p>' );
  253. split( [ 0, 3 ] );
  254. output( '<p>123</p><p>[]45678</p>' );
  255. split( [ 1, 4 ] );
  256. output( '<p>123</p><p>4567</p><p>[]8</p>' );
  257. split( [ 1, 2 ] );
  258. output( '<p>123</p><p>45</p><p>[]67</p><p>8</p>' );
  259. editor.execute( 'undo' );
  260. output( '<p>123</p><p>4567[]</p><p>8</p>' );
  261. editor.execute( 'undo' );
  262. output( '<p>123</p><p>45678[]</p>' );
  263. editor.execute( 'undo' );
  264. output( '<p>12345678[]</p>' );
  265. undoDisabled();
  266. editor.execute( 'redo' );
  267. output( '<p>123[]</p><p>45678</p>' );
  268. editor.execute( 'redo' );
  269. output( '<p>123</p><p>4567[]</p><p>8</p>' );
  270. editor.execute( 'redo' );
  271. output( '<p>123</p><p>45[]</p><p>67</p><p>8</p>' );
  272. redoDisabled();
  273. } );
  274. it( 'merge, merge, merge', () => {
  275. input( '<p>123</p><p>45</p><p>67</p><p>8</p>' );
  276. merge( [ 1 ] );
  277. output( '<p>123[]45</p><p>67</p><p>8</p>' );
  278. merge( [ 2 ] );
  279. output( '<p>12345</p><p>67[]8</p>' );
  280. merge( [ 1 ] );
  281. output( '<p>12345[]678</p>' );
  282. editor.execute( 'undo' );
  283. output( '<p>12345</p><p>678[]</p>' );
  284. editor.execute( 'undo' );
  285. output( '<p>12345</p><p>67</p><p>8[]</p>' );
  286. editor.execute( 'undo' );
  287. output( '<p>123</p><p>45[]</p><p>67</p><p>8</p>' );
  288. undoDisabled();
  289. editor.execute( 'redo' );
  290. output( '<p>12345[]</p><p>67</p><p>8</p>' );
  291. editor.execute( 'redo' );
  292. output( '<p>12345</p><p>678[]</p>' );
  293. editor.execute( 'redo' );
  294. output( '<p>12345678[]</p>' );
  295. redoDisabled();
  296. } );
  297. it( 'split, merge, split, merge (same position)', () => {
  298. input( '<p>12345678</p>' );
  299. split( [ 0, 3 ] );
  300. output( '<p>123</p><p>[]45678</p>' );
  301. merge( [ 1 ] );
  302. output( '<p>123[]45678</p>' );
  303. split( [ 0, 3 ] );
  304. output( '<p>123</p><p>[]45678</p>' );
  305. merge( [ 1 ] );
  306. output( '<p>123[]45678</p>' );
  307. editor.execute( 'undo' );
  308. output( '<p>123</p><p>45678[]</p>' );
  309. editor.execute( 'undo' );
  310. output( '<p>12345678[]</p>' );
  311. editor.execute( 'undo' );
  312. output( '<p>123</p><p>45678[]</p>' );
  313. editor.execute( 'undo' );
  314. output( '<p>12345678[]</p>' );
  315. undoDisabled();
  316. editor.execute( 'redo' );
  317. output( '<p>123[]</p><p>45678</p>' );
  318. editor.execute( 'redo' );
  319. output( '<p>12345678[]</p>' );
  320. editor.execute( 'redo' );
  321. output( '<p>123[]</p><p>45678</p>' );
  322. editor.execute( 'redo' );
  323. output( '<p>12345678[]</p>' );
  324. redoDisabled();
  325. } );
  326. it( 'split, split, split, merge, merge, merge', () => {
  327. input( '<p>12345678</p>' );
  328. split( [ 0, 3 ] );
  329. output( '<p>123</p><p>[]45678</p>' );
  330. split( [ 1, 4 ] );
  331. output( '<p>123</p><p>4567</p><p>[]8</p>' );
  332. split( [ 1, 2 ] );
  333. output( '<p>123</p><p>45</p><p>[]67</p><p>8</p>' );
  334. merge( [ 1 ] );
  335. output( '<p>123[]45</p><p>67</p><p>8</p>' );
  336. merge( [ 2 ] );
  337. output( '<p>12345</p><p>67[]8</p>' );
  338. merge( [ 1 ] );
  339. output( '<p>12345[]678</p>' );
  340. editor.execute( 'undo' );
  341. output( '<p>12345</p><p>678[]</p>' );
  342. editor.execute( 'undo' );
  343. output( '<p>12345</p><p>67</p><p>8[]</p>' );
  344. editor.execute( 'undo' );
  345. output( '<p>123</p><p>45[]</p><p>67</p><p>8</p>' );
  346. editor.execute( 'undo' );
  347. output( '<p>123</p><p>4567[]</p><p>8</p>' );
  348. editor.execute( 'undo' );
  349. output( '<p>123</p><p>45678[]</p>' );
  350. editor.execute( 'undo' );
  351. output( '<p>12345678[]</p>' );
  352. undoDisabled();
  353. editor.execute( 'redo' );
  354. output( '<p>123[]</p><p>45678</p>' );
  355. editor.execute( 'redo' );
  356. output( '<p>123</p><p>4567[]</p><p>8</p>' );
  357. editor.execute( 'redo' );
  358. output( '<p>123</p><p>45[]</p><p>67</p><p>8</p>' );
  359. editor.execute( 'redo' );
  360. output( '<p>12345[]</p><p>67</p><p>8</p>' );
  361. editor.execute( 'redo' );
  362. output( '<p>12345</p><p>678[]</p>' );
  363. editor.execute( 'redo' );
  364. output( '<p>12345678[]</p>' );
  365. redoDisabled();
  366. } );
  367. it( 'split, split, merge, split, merge (different order)', () => {
  368. input( '<p>12345678</p>' );
  369. split( [ 0, 3 ] );
  370. output( '<p>123</p><p>[]45678</p>' );
  371. split( [ 1, 2 ] );
  372. output( '<p>123</p><p>45</p><p>[]678</p>' );
  373. merge( [ 1 ] );
  374. output( '<p>123[]45</p><p>678</p>' );
  375. split( [ 1, 1 ] );
  376. output( '<p>12345</p><p>6</p><p>[]78</p>' );
  377. merge( [ 1 ] );
  378. output( '<p>12345[]6</p><p>78</p>' );
  379. editor.execute( 'undo' );
  380. output( '<p>12345</p><p>6[]</p><p>78</p>' );
  381. editor.execute( 'undo' );
  382. output( '<p>12345</p><p>678[]</p>' );
  383. editor.execute( 'undo' );
  384. output( '<p>123</p><p>45[]</p><p>678</p>' );
  385. editor.execute( 'undo' );
  386. output( '<p>123</p><p>45678[]</p>' );
  387. editor.execute( 'undo' );
  388. output( '<p>12345678[]</p>' );
  389. undoDisabled();
  390. editor.execute( 'redo' );
  391. output( '<p>123[]</p><p>45678</p>' );
  392. editor.execute( 'redo' );
  393. output( '<p>123</p><p>45[]</p><p>678</p>' );
  394. editor.execute( 'redo' );
  395. output( '<p>12345[]</p><p>678</p>' );
  396. editor.execute( 'redo' );
  397. output( '<p>12345</p><p>6[]</p><p>78</p>' );
  398. editor.execute( 'redo' );
  399. output( '<p>123456[]</p><p>78</p>' );
  400. redoDisabled();
  401. } );
  402. it( 'split, remove, split, merge, merge', () => {
  403. input( '<p>12345678</p>' );
  404. split( [ 0, 3 ] );
  405. output( '<p>123</p><p>[]45678</p>' );
  406. remove( [ 1, 4 ] );
  407. remove( [ 1, 3 ] );
  408. output( '<p>123</p><p>45[]8</p>' );
  409. split( [ 1, 1 ] );
  410. output( '<p>123</p><p>4</p><p>[]58</p>' );
  411. merge( [ 1 ] );
  412. output( '<p>123[]4</p><p>58</p>' );
  413. merge( [ 1 ] );
  414. output( '<p>1234[]58</p>' );
  415. editor.execute( 'undo' );
  416. output( '<p>1234</p><p>58[]</p>' );
  417. editor.execute( 'undo' );
  418. output( '<p>123</p><p>4[]</p><p>58</p>' );
  419. editor.execute( 'undo' );
  420. output( '<p>123</p><p>458[]</p>' );
  421. editor.execute( 'undo' );
  422. output( '<p>123</p><p>4567[]8</p>' );
  423. editor.execute( 'undo' );
  424. output( '<p>12345678[]</p>' );
  425. undoDisabled();
  426. editor.execute( 'redo' );
  427. output( '<p>123[]</p><p>45678</p>' );
  428. editor.execute( 'redo' );
  429. output( '<p>123</p><p>458[]</p>' );
  430. editor.execute( 'redo' );
  431. output( '<p>123</p><p>4[]</p><p>58</p>' );
  432. editor.execute( 'redo' );
  433. output( '<p>1234[]</p><p>58</p>' );
  434. editor.execute( 'redo' );
  435. output( '<p>123458[]</p>' );
  436. redoDisabled();
  437. } );
  438. it( 'split, typing, split, merge, merge', () => {
  439. input( '<p>12345678</p>' );
  440. split( [ 0, 3 ] );
  441. output( '<p>123</p><p>[]45678</p>' );
  442. type( [ 1, 4 ], 'x' );
  443. type( [ 1, 5 ], 'y' );
  444. output( '<p>123</p><p>4567xy[]8</p>' );
  445. split( [ 1, 2 ] );
  446. output( '<p>123</p><p>45</p><p>[]67xy8</p>' );
  447. merge( [ 1 ] );
  448. output( '<p>123[]45</p><p>67xy8</p>' );
  449. merge( [ 1 ] );
  450. output( '<p>12345[]67xy8</p>' );
  451. editor.execute( 'undo' );
  452. output( '<p>12345</p><p>67xy8[]</p>' );
  453. editor.execute( 'undo' );
  454. output( '<p>123</p><p>45[]</p><p>67xy8</p>' );
  455. editor.execute( 'undo' );
  456. output( '<p>123</p><p>4567xy8[]</p>' );
  457. editor.execute( 'undo' );
  458. output( '<p>123</p><p>4567[]8</p>' );
  459. editor.execute( 'undo' );
  460. output( '<p>12345678[]</p>' );
  461. undoDisabled();
  462. editor.execute( 'redo' );
  463. output( '<p>123[]</p><p>45678</p>' );
  464. editor.execute( 'redo' );
  465. output( '<p>123</p><p>4567xy8[]</p>' );
  466. editor.execute( 'redo' );
  467. output( '<p>123</p><p>45[]</p><p>67xy8</p>' );
  468. editor.execute( 'redo' );
  469. output( '<p>12345[]</p><p>67xy8</p>' );
  470. editor.execute( 'redo' );
  471. output( '<p>1234567xy8[]</p>' );
  472. redoDisabled();
  473. } );
  474. } );
  475. describe( 'other reported cases', () => {
  476. // ckeditor5-engine#t/1051
  477. it( 'rename leaks to other elements on undo #1', () => {
  478. input( '<h1>[]Foo</h1><p>Bar</p>' );
  479. doc.batch().rename( root.getChild( 0 ), 'p' );
  480. output( '<p>[]Foo</p><p>Bar</p>' );
  481. doc.batch().split( Position.createAt( root.getChild( 0 ), 1 ) );
  482. output( '<p>[]F</p><p>oo</p><p>Bar</p>' );
  483. doc.batch().merge( Position.createAt( root, 2 ) );
  484. output( '<p>[]F</p><p>ooBar</p>' );
  485. editor.execute( 'undo' );
  486. output( '<p>[]F</p><p>oo</p><p>Bar</p>' );
  487. editor.execute( 'undo' );
  488. output( '<p>[]Foo</p><p>Bar</p>' );
  489. editor.execute( 'undo' );
  490. output( '<h1>[]Foo</h1><p>Bar</p>' );
  491. } );
  492. // Similar issue that bases on the same error as above, however here we first merge (above we first split).
  493. it( 'rename leaks to other elements on undo #2', () => {
  494. input( '<h1>[]Foo</h1><p>Bar</p>' );
  495. doc.batch().rename( root.getChild( 0 ), 'h2' );
  496. output( '<h2>[]Foo</h2><p>Bar</p>' );
  497. doc.batch().merge( Position.createAt( root, 1 ) );
  498. output( '<h2>[]FooBar</h2>' );
  499. editor.execute( 'undo' );
  500. output( '<h2>[]Foo</h2><p>Bar</p>' );
  501. editor.execute( 'undo' );
  502. output( '<h1>[]Foo</h1><p>Bar</p>' );
  503. } );
  504. // Reverse issue, this time first operation is merge and then rename.
  505. it( 'merge, rename, undo, undo is correct', () => {
  506. input( '<h1>[]Foo</h1><p>Bar</p>' );
  507. doc.batch().merge( Position.createAt( root, 1 ) );
  508. output( '<h1>[]FooBar</h1>' );
  509. doc.batch().rename( root.getChild( 0 ), 'h2' );
  510. output( '<h2>[]FooBar</h2>' );
  511. editor.execute( 'undo' );
  512. output( '<h1>[]FooBar</h1>' );
  513. editor.execute( 'undo' );
  514. output( '<h1>[]Foo</h1><p>Bar</p>' );
  515. } );
  516. // ckeditor5-engine#t/1053
  517. it( 'wrap, split, undo, undo is correct', () => {
  518. input( '<p>[]Foo</p><p>Bar</p>' );
  519. doc.batch().wrap( Range.createIn( root ), 'div' );
  520. output( '<div><p>[]Foo</p><p>Bar</p></div>' );
  521. doc.batch().split( new Position( root, [ 0, 0, 1 ] ) );
  522. output( '<div><p>[]F</p><p>oo</p><p>Bar</p></div>' );
  523. editor.execute( 'undo' );
  524. output( '<div><p>[]Foo</p><p>Bar</p></div>' );
  525. editor.execute( 'undo' );
  526. output( '<p>[]Foo</p><p>Bar</p>' );
  527. } );
  528. // ckeditor5-engine#t/1055
  529. it( 'selection attribute setting: split, bold, merge, undo, undo, undo', () => {
  530. input( '<p>Foo[]</p><p>Bar</p>' );
  531. editor.execute( 'enter' );
  532. output( '<p>Foo</p><p>[]</p><p>Bar</p>' );
  533. editor.execute( 'bold' );
  534. output( '<p>Foo</p><p selection:bold="true"><$text bold="true">[]</$text></p><p>Bar</p>' );
  535. editor.execute( 'forwardDelete' );
  536. output( '<p>Foo</p><p>[]Bar</p>' );
  537. editor.execute( 'undo' );
  538. output( '<p>Foo</p><p selection:bold="true"><$text bold="true">[]</$text></p><p>Bar</p>' );
  539. editor.execute( 'undo' );
  540. output( '<p>Foo</p><p>[]</p><p>Bar</p>' );
  541. editor.execute( 'undo' );
  542. output( '<p>Foo[]</p><p>Bar</p>' );
  543. } );
  544. } );
  545. describe( 'other edge cases', () => {
  546. it( 'deleteContent between two nodes', () => {
  547. input( '<p>fo[o</p><p>b]ar</p>' );
  548. editor.data.deleteContent( doc.selection, doc.batch() );
  549. output( '<p>fo[]ar</p>' );
  550. editor.execute( 'undo' );
  551. output( '<p>fo[o</p><p>b]ar</p>' );
  552. } );
  553. // Related to ckeditor5-engine#891 and ckeditor5-list#51.
  554. it( 'change attribute of removed node then undo and redo', () => {
  555. const gy = doc.graveyard;
  556. const batch = doc.batch();
  557. const p = new Element( 'p' );
  558. root.appendChildren( p );
  559. batch.remove( p );
  560. batch.setAttribute( p, 'bold', true );
  561. editor.execute( 'undo' );
  562. editor.execute( 'redo' );
  563. expect( p.root ).to.equal( gy );
  564. expect( p.getAttribute( 'bold' ) ).to.be.true;
  565. } );
  566. // Related to ckeditor5-engine#891.
  567. it( 'change attribute of removed node then undo and redo', () => {
  568. const gy = doc.graveyard;
  569. const batch = doc.batch();
  570. const p1 = new Element( 'p' );
  571. const p2 = new Element( 'p' );
  572. const p3 = new Element( 'p' );
  573. root.appendChildren( [ p1, p2 ] );
  574. batch.remove( p1 ).remove( p2 ).insert( new Position( root, [ 0 ] ), p3 );
  575. editor.execute( 'undo' );
  576. editor.execute( 'redo' );
  577. expect( p1.root ).to.equal( gy );
  578. expect( p2.root ).to.equal( gy );
  579. expect( p3.root ).to.equal( root );
  580. } );
  581. } );
  582. } );