8
0

undoengine-integration.js 30 KB

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