8
0

undoengine-integration.js 30 KB

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