undoediting-integration.js 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. /**
  2. * @license Copyright (c) 2003-2018, 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 Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
  10. import UndoEditing from '../src/undoediting';
  11. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  12. import HeadingEditing from '@ckeditor/ckeditor5-heading/src/headingediting';
  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 BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
  17. import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
  18. import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
  19. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  20. describe( 'UndoEditing integration', () => {
  21. let editor, model, doc, root, div;
  22. beforeEach( () => {
  23. div = document.createElement( 'div' );
  24. document.body.appendChild( div );
  25. return ClassicEditor.create( div, { plugins: [ Paragraph, HeadingEditing, Typing, Enter, Clipboard, BoldEditing, UndoEditing ] } )
  26. .then( newEditor => {
  27. editor = newEditor;
  28. model = editor.model;
  29. doc = model.document;
  30. // Add "div feature".
  31. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  32. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'div', view: 'div' } ) );
  33. editor.conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'div', view: 'div' } ) );
  34. root = doc.getRoot();
  35. } );
  36. } );
  37. afterEach( () => {
  38. return editor.destroy();
  39. } );
  40. function setSelection( pathA, pathB ) {
  41. model.change( writer => {
  42. writer.setSelection( new Range( new Position( root, pathA ), new Position( root, pathB ) ) );
  43. } );
  44. }
  45. function input( input ) {
  46. model.enqueueChange( 'transparent', () => {
  47. setData( model, input );
  48. } );
  49. }
  50. function output( output ) {
  51. expect( getData( model ) ).to.equal( output );
  52. }
  53. function undoDisabled() {
  54. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false;
  55. }
  56. function redoDisabled() {
  57. expect( editor.commands.get( 'redo' ).isEnabled ).to.be.false;
  58. }
  59. describe( 'adding and removing content', () => {
  60. it( 'add and undo', () => {
  61. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  62. model.change( writer => {
  63. writer.insertText( 'zzz', doc.selection.getFirstPosition() );
  64. } );
  65. output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
  66. editor.execute( 'undo' );
  67. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  68. undoDisabled();
  69. } );
  70. it( 'multiple adding and undo', () => {
  71. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  72. model.change( writer => {
  73. writer.insertText( 'zzz', doc.selection.getFirstPosition() );
  74. writer.insertText( 'xxx', new Position( root, [ 1, 0 ] ) );
  75. } );
  76. output( '<paragraph>fozzz[]o</paragraph><paragraph>xxxbar</paragraph>' );
  77. model.change( writer => {
  78. setSelection( [ 1, 0 ], [ 1, 0 ] );
  79. writer.insertText( 'yyy', doc.selection.getFirstPosition() );
  80. } );
  81. output( '<paragraph>fozzzo</paragraph><paragraph>yyy[]xxxbar</paragraph>' );
  82. editor.execute( 'undo' );
  83. output( '<paragraph>fozzzo</paragraph><paragraph>[]xxxbar</paragraph>' );
  84. editor.execute( 'undo' );
  85. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  86. undoDisabled();
  87. } );
  88. it( 'multiple adding mixed with undo', () => {
  89. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  90. model.change( writer => {
  91. writer.insertText( 'zzz', doc.selection.getFirstPosition() );
  92. } );
  93. output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
  94. model.change( writer => {
  95. setSelection( [ 1, 0 ], [ 1, 0 ] );
  96. writer.insertText( 'yyy', doc.selection.getFirstPosition() );
  97. } );
  98. output( '<paragraph>fozzzo</paragraph><paragraph>yyy[]bar</paragraph>' );
  99. editor.execute( 'undo' );
  100. output( '<paragraph>fozzzo</paragraph><paragraph>[]bar</paragraph>' );
  101. model.change( writer => {
  102. setSelection( [ 0, 0 ], [ 0, 0 ] );
  103. writer.insertText( 'xxx', doc.selection.getFirstPosition() );
  104. } );
  105. output( '<paragraph>xxx[]fozzzo</paragraph><paragraph>bar</paragraph>' );
  106. editor.execute( 'undo' );
  107. output( '<paragraph>[]fozzzo</paragraph><paragraph>bar</paragraph>' );
  108. editor.execute( 'undo' );
  109. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  110. undoDisabled();
  111. } );
  112. it( 'multiple remove and undo', () => {
  113. input( '<paragraph>[]foo</paragraph><paragraph>bar</paragraph>' );
  114. model.change( writer => {
  115. writer.remove( Range.createFromPositionAndShift( doc.selection.getFirstPosition(), 2 ) );
  116. } );
  117. output( '<paragraph>[]o</paragraph><paragraph>bar</paragraph>' );
  118. model.change( writer => {
  119. setSelection( [ 1, 1 ], [ 1, 1 ] );
  120. writer.remove( Range.createFromPositionAndShift( doc.selection.getFirstPosition(), 2 ) );
  121. } );
  122. output( '<paragraph>o</paragraph><paragraph>b[]</paragraph>' );
  123. editor.execute( 'undo' );
  124. // Here is an edge case that selection could be before or after `ar`.
  125. output( '<paragraph>o</paragraph><paragraph>bar[]</paragraph>' );
  126. editor.execute( 'undo' );
  127. // As above.
  128. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  129. undoDisabled();
  130. } );
  131. it( 'add and remove different parts and undo', () => {
  132. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  133. model.change( writer => {
  134. writer.insertText( 'zzz', doc.selection.getFirstPosition() );
  135. } );
  136. output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
  137. model.change( writer => {
  138. setSelection( [ 1, 2 ], [ 1, 2 ] );
  139. writer.remove( Range.createFromPositionAndShift( new Position( root, [ 1, 1 ] ), 1 ) );
  140. } );
  141. output( '<paragraph>fozzzo</paragraph><paragraph>b[]r</paragraph>' );
  142. editor.execute( 'undo' );
  143. output( '<paragraph>fozzzo</paragraph><paragraph>ba[]r</paragraph>' );
  144. editor.execute( 'undo' );
  145. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  146. undoDisabled();
  147. } );
  148. it( 'add and remove same part and undo', () => {
  149. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  150. model.change( writer => {
  151. writer.insertText( 'zzz', doc.selection.getFirstPosition() );
  152. } );
  153. output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
  154. model.change( writer => {
  155. writer.remove( Range.createFromPositionAndShift( new Position( root, [ 0, 2 ] ), 3 ) );
  156. } );
  157. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  158. editor.execute( 'undo' );
  159. output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
  160. editor.execute( 'undo' );
  161. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  162. undoDisabled();
  163. } );
  164. it( 'undo remove all content', () => {
  165. input( '<paragraph>foo[]</paragraph>' );
  166. model.change( writer => {
  167. writer.remove( Range.createIn( root ) );
  168. } );
  169. output( '<paragraph>[]</paragraph>' ); // All hail our king and savior, autoparagraphing!
  170. editor.execute( 'undo' );
  171. output( '<paragraph>foo[]</paragraph>' );
  172. undoDisabled();
  173. } );
  174. it( 'undo insert first content', () => {
  175. input( '' );
  176. output( '<paragraph>[]</paragraph>' ); // All hail our king and savior, autoparagraphing!
  177. model.change( writer => {
  178. writer.remove( Range.createIn( root ) );
  179. writer.insertElement( 'heading1', doc.selection.getFirstPosition() );
  180. } );
  181. output( '<heading1>[]</heading1>' );
  182. editor.execute( 'undo' );
  183. output( '<paragraph>[]</paragraph>' );
  184. undoDisabled();
  185. } );
  186. // #72.
  187. it( 'undo paste multiple elements simulation', () => {
  188. input( '<paragraph></paragraph>' );
  189. const p = root.getChild( 0 );
  190. const pos = new Position( root, [ 0 ] );
  191. model.change( writer => {
  192. writer.remove( p );
  193. writer.insertElement( 'heading1', pos );
  194. writer.insertElement( 'heading2', pos.getShiftedBy( 1 ) );
  195. } );
  196. output( '<heading1>[]</heading1><heading2></heading2>' );
  197. editor.execute( 'undo' );
  198. output( '<paragraph>[]</paragraph>' );
  199. undoDisabled();
  200. } );
  201. } );
  202. describe( 'moving', () => {
  203. it( 'move same content twice then undo', () => {
  204. input( '<paragraph>f[o]z</paragraph><paragraph>bar</paragraph>' );
  205. model.change( writer => {
  206. writer.move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) );
  207. } );
  208. output( '<paragraph>fz</paragraph><paragraph>[o]bar</paragraph>' );
  209. model.change( writer => {
  210. writer.move( doc.selection.getFirstRange(), new Position( root, [ 0, 2 ] ) );
  211. } );
  212. output( '<paragraph>fz[o]</paragraph><paragraph>bar</paragraph>' );
  213. editor.execute( 'undo' );
  214. output( '<paragraph>fz</paragraph><paragraph>[o]bar</paragraph>' );
  215. editor.execute( 'undo' );
  216. output( '<paragraph>f[o]z</paragraph><paragraph>bar</paragraph>' );
  217. undoDisabled();
  218. } );
  219. it( 'move content and new parent then undo', () => {
  220. input( '<paragraph>f[o]z</paragraph><paragraph>bar</paragraph>' );
  221. model.change( writer => {
  222. writer.move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) );
  223. } );
  224. output( '<paragraph>fz</paragraph><paragraph>[o]bar</paragraph>' );
  225. model.change( writer => {
  226. setSelection( [ 1 ], [ 2 ] );
  227. writer.move( doc.selection.getFirstRange(), new Position( root, [ 0 ] ) );
  228. } );
  229. output( '[<paragraph>obar</paragraph>]<paragraph>fz</paragraph>' );
  230. editor.execute( 'undo' );
  231. output( '<paragraph>fz</paragraph>[<paragraph>obar</paragraph>]' );
  232. editor.execute( 'undo' );
  233. output( '<paragraph>f[o]z</paragraph><paragraph>bar</paragraph>' );
  234. undoDisabled();
  235. } );
  236. } );
  237. describe( 'attributes with other', () => {
  238. it( 'attributes then insert inside then undo', () => {
  239. input( '<paragraph>fo[ob]ar</paragraph>' );
  240. model.change( writer => {
  241. writer.setAttribute( 'bold', true, doc.selection.getFirstRange() );
  242. } );
  243. output( '<paragraph>fo[<$text bold="true">ob</$text>]ar</paragraph>' );
  244. expect( doc.selection.getAttribute( 'bold' ) ).to.be.true;
  245. model.change( writer => {
  246. setSelection( [ 0, 3 ], [ 0, 3 ] );
  247. writer.insertText( 'zzz', doc.selection.getFirstPosition() );
  248. } );
  249. output( '<paragraph>fo<$text bold="true">o</$text>zzz[]<$text bold="true">b</$text>ar</paragraph>' );
  250. expect( doc.selection.getAttribute( 'bold' ) ).to.be.undefined;
  251. editor.execute( 'undo' );
  252. output( '<paragraph>fo<$text bold="true">o[]b</$text>ar</paragraph>' );
  253. expect( doc.selection.getAttribute( 'bold' ) ).to.be.true;
  254. editor.execute( 'undo' );
  255. output( '<paragraph>fo[ob]ar</paragraph>' );
  256. expect( doc.selection.getAttribute( 'bold' ) ).to.be.undefined;
  257. undoDisabled();
  258. } );
  259. } );
  260. describe( 'wrapping, unwrapping, merging, splitting', () => {
  261. it( 'wrap and undo', () => {
  262. model.schema.extend( '$text', { allowIn: '$root' } );
  263. input( 'fo[zb]ar' );
  264. model.change( writer => {
  265. writer.wrap( doc.selection.getFirstRange(), 'paragraph' );
  266. } );
  267. output( 'fo<paragraph>[zb]</paragraph>ar' );
  268. editor.execute( 'undo' );
  269. output( 'fo[zb]ar' );
  270. undoDisabled();
  271. } );
  272. it( 'wrap, move and undo', () => {
  273. model.schema.extend( '$text', { allowIn: '$root' } );
  274. input( 'fo[zb]ar' );
  275. model.change( writer => {
  276. writer.wrap( doc.selection.getFirstRange(), 'paragraph' );
  277. } );
  278. // Would be better if selection was inside P.
  279. output( 'fo<paragraph>[zb]</paragraph>ar' );
  280. model.change( writer => {
  281. setSelection( [ 2, 0 ], [ 2, 1 ] );
  282. writer.move( doc.selection.getFirstRange(), new Position( root, [ 0 ] ) );
  283. } );
  284. output( '[z]fo<paragraph>b</paragraph>ar' );
  285. editor.execute( 'undo' );
  286. output( 'fo<paragraph>[z]b</paragraph>ar' );
  287. editor.execute( 'undo' );
  288. output( 'fo[zb]ar' );
  289. undoDisabled();
  290. } );
  291. it( 'unwrap and undo', () => {
  292. input( '<paragraph>foo[]bar</paragraph>' );
  293. model.change( writer => {
  294. writer.unwrap( doc.selection.getFirstPosition().parent );
  295. } );
  296. output( 'foo[]bar' );
  297. editor.execute( 'undo' );
  298. output( '<paragraph>foo[]bar</paragraph>' );
  299. undoDisabled();
  300. } );
  301. it( 'merge and undo', () => {
  302. input( '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>' );
  303. model.change( writer => {
  304. writer.merge( new Position( root, [ 1 ] ) );
  305. // Because selection is stuck with <paragraph> it ends up in graveyard. We have to manually move it to correct node.
  306. setSelection( [ 0, 3 ], [ 0, 3 ] );
  307. } );
  308. output( '<paragraph>foo[]bar</paragraph>' );
  309. editor.execute( 'undo' );
  310. output( '<paragraph>foo</paragraph><paragraph>bar[]</paragraph>' );
  311. undoDisabled();
  312. } );
  313. it( 'split and undo', () => {
  314. input( '<paragraph>foo[]bar</paragraph>' );
  315. model.change( writer => {
  316. writer.split( doc.selection.getFirstPosition() );
  317. // Because selection is stuck with <paragraph> it ends up in wrong node. We have to manually move it to correct node.
  318. setSelection( [ 1, 0 ], [ 1, 0 ] );
  319. } );
  320. output( '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>' );
  321. editor.execute( 'undo' );
  322. output( '<paragraph>foobar[]</paragraph>' );
  323. undoDisabled();
  324. } );
  325. } );
  326. // Restoring selection in those examples may be completely off.
  327. describe( 'multiple enters, deletes and typing', () => {
  328. it( 'split, split, split', () => {
  329. input( '<paragraph>12345678</paragraph>' );
  330. split( [ 0, 3 ] );
  331. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  332. split( [ 1, 4 ] );
  333. output( '<paragraph>123</paragraph><paragraph>4567</paragraph><paragraph>[]8</paragraph>' );
  334. split( [ 1, 2 ] );
  335. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]67</paragraph><paragraph>8</paragraph>' );
  336. editor.execute( 'undo' );
  337. output( '<paragraph>123</paragraph><paragraph>4567[]</paragraph><paragraph>8</paragraph>' );
  338. editor.execute( 'undo' );
  339. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  340. editor.execute( 'undo' );
  341. output( '<paragraph>12345678[]</paragraph>' );
  342. undoDisabled();
  343. editor.execute( 'redo' );
  344. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  345. editor.execute( 'redo' );
  346. output( '<paragraph>123</paragraph><paragraph>4567</paragraph><paragraph>8[]</paragraph>' );
  347. editor.execute( 'redo' );
  348. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>67[]</paragraph><paragraph>8</paragraph>' );
  349. redoDisabled();
  350. } );
  351. it( 'merge, merge, merge', () => {
  352. input( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  353. merge( [ 1 ] );
  354. output( '<paragraph>123[]45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  355. merge( [ 2 ] );
  356. output( '<paragraph>12345</paragraph><paragraph>67[]8</paragraph>' );
  357. merge( [ 1 ] );
  358. output( '<paragraph>12345[]678</paragraph>' );
  359. editor.execute( 'undo' );
  360. output( '<paragraph>12345</paragraph><paragraph>678[]</paragraph>' );
  361. editor.execute( 'undo' );
  362. output( '<paragraph>12345</paragraph><paragraph>67</paragraph><paragraph>8[]</paragraph>' );
  363. editor.execute( 'undo' );
  364. output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  365. undoDisabled();
  366. editor.execute( 'redo' );
  367. output( '<paragraph>12345[]</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  368. editor.execute( 'redo' );
  369. output( '<paragraph>12345</paragraph><paragraph>678[]</paragraph>' );
  370. editor.execute( 'redo' );
  371. output( '<paragraph>12345678[]</paragraph>' );
  372. redoDisabled();
  373. } );
  374. it( 'split, merge, split, merge (same position)', () => {
  375. input( '<paragraph>12345678</paragraph>' );
  376. split( [ 0, 3 ] );
  377. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  378. merge( [ 1 ] );
  379. output( '<paragraph>123[]45678</paragraph>' );
  380. split( [ 0, 3 ] );
  381. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  382. merge( [ 1 ] );
  383. output( '<paragraph>123[]45678</paragraph>' );
  384. editor.execute( 'undo' );
  385. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  386. editor.execute( 'undo' );
  387. output( '<paragraph>12345678[]</paragraph>' );
  388. editor.execute( 'undo' );
  389. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  390. editor.execute( 'undo' );
  391. output( '<paragraph>12345678[]</paragraph>' );
  392. undoDisabled();
  393. editor.execute( 'redo' );
  394. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  395. editor.execute( 'redo' );
  396. output( '<paragraph>12345678[]</paragraph>' );
  397. editor.execute( 'redo' );
  398. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  399. editor.execute( 'redo' );
  400. output( '<paragraph>12345678[]</paragraph>' );
  401. redoDisabled();
  402. } );
  403. it( 'split, split, split, merge, merge, merge', () => {
  404. input( '<paragraph>12345678</paragraph>' );
  405. split( [ 0, 3 ] );
  406. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  407. split( [ 1, 4 ] );
  408. output( '<paragraph>123</paragraph><paragraph>4567</paragraph><paragraph>[]8</paragraph>' );
  409. split( [ 1, 2 ] );
  410. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]67</paragraph><paragraph>8</paragraph>' );
  411. merge( [ 1 ] );
  412. output( '<paragraph>123[]45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  413. merge( [ 2 ] );
  414. output( '<paragraph>12345</paragraph><paragraph>67[]8</paragraph>' );
  415. merge( [ 1 ] );
  416. output( '<paragraph>12345[]678</paragraph>' );
  417. editor.execute( 'undo' );
  418. output( '<paragraph>12345</paragraph><paragraph>678[]</paragraph>' );
  419. editor.execute( 'undo' );
  420. output( '<paragraph>12345</paragraph><paragraph>67</paragraph><paragraph>8[]</paragraph>' );
  421. editor.execute( 'undo' );
  422. output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  423. editor.execute( 'undo' );
  424. output( '<paragraph>123</paragraph><paragraph>4567[]</paragraph><paragraph>8</paragraph>' );
  425. editor.execute( 'undo' );
  426. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  427. editor.execute( 'undo' );
  428. output( '<paragraph>12345678[]</paragraph>' );
  429. undoDisabled();
  430. editor.execute( 'redo' );
  431. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  432. editor.execute( 'redo' );
  433. output( '<paragraph>123</paragraph><paragraph>4567</paragraph><paragraph>8[]</paragraph>' );
  434. editor.execute( 'redo' );
  435. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>67[]</paragraph><paragraph>8</paragraph>' );
  436. editor.execute( 'redo' );
  437. output( '<paragraph>12345[]</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  438. editor.execute( 'redo' );
  439. output( '<paragraph>12345</paragraph><paragraph>678[]</paragraph>' );
  440. editor.execute( 'redo' );
  441. output( '<paragraph>12345678[]</paragraph>' );
  442. redoDisabled();
  443. } );
  444. it( 'split, split, merge, split, merge (different order)', () => {
  445. input( '<paragraph>12345678</paragraph>' );
  446. split( [ 0, 3 ] );
  447. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  448. split( [ 1, 2 ] );
  449. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]678</paragraph>' );
  450. merge( [ 1 ] );
  451. output( '<paragraph>123[]45</paragraph><paragraph>678</paragraph>' );
  452. split( [ 1, 1 ] );
  453. output( '<paragraph>12345</paragraph><paragraph>6</paragraph><paragraph>[]78</paragraph>' );
  454. merge( [ 1 ] );
  455. output( '<paragraph>12345[]6</paragraph><paragraph>78</paragraph>' );
  456. editor.execute( 'undo' );
  457. output( '<paragraph>12345</paragraph><paragraph>6[]</paragraph><paragraph>78</paragraph>' );
  458. editor.execute( 'undo' );
  459. output( '<paragraph>12345</paragraph><paragraph>678[]</paragraph>' );
  460. editor.execute( 'undo' );
  461. output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>678</paragraph>' );
  462. editor.execute( 'undo' );
  463. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  464. editor.execute( 'undo' );
  465. output( '<paragraph>12345678[]</paragraph>' );
  466. undoDisabled();
  467. editor.execute( 'redo' );
  468. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  469. editor.execute( 'redo' );
  470. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>678[]</paragraph>' );
  471. editor.execute( 'redo' );
  472. output( '<paragraph>12345[]</paragraph><paragraph>678</paragraph>' );
  473. editor.execute( 'redo' );
  474. output( '<paragraph>12345</paragraph><paragraph>6</paragraph><paragraph>78[]</paragraph>' );
  475. editor.execute( 'redo' );
  476. output( '<paragraph>123456[]</paragraph><paragraph>78</paragraph>' );
  477. redoDisabled();
  478. } );
  479. it( 'split, remove, split, merge, merge', () => {
  480. input( '<paragraph>12345678</paragraph>' );
  481. split( [ 0, 3 ] );
  482. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  483. remove( [ 1, 4 ] );
  484. remove( [ 1, 3 ] );
  485. output( '<paragraph>123</paragraph><paragraph>45[]8</paragraph>' );
  486. split( [ 1, 1 ] );
  487. output( '<paragraph>123</paragraph><paragraph>4</paragraph><paragraph>[]58</paragraph>' );
  488. merge( [ 1 ] );
  489. output( '<paragraph>123[]4</paragraph><paragraph>58</paragraph>' );
  490. merge( [ 1 ] );
  491. output( '<paragraph>1234[]58</paragraph>' );
  492. editor.execute( 'undo' );
  493. output( '<paragraph>1234</paragraph><paragraph>58[]</paragraph>' );
  494. editor.execute( 'undo' );
  495. output( '<paragraph>123</paragraph><paragraph>4[]</paragraph><paragraph>58</paragraph>' );
  496. editor.execute( 'undo' );
  497. output( '<paragraph>123</paragraph><paragraph>458[]</paragraph>' );
  498. editor.execute( 'undo' );
  499. output( '<paragraph>123</paragraph><paragraph>4567[]8</paragraph>' );
  500. editor.execute( 'undo' );
  501. output( '<paragraph>12345678[]</paragraph>' );
  502. undoDisabled();
  503. editor.execute( 'redo' );
  504. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  505. editor.execute( 'redo' );
  506. output( '<paragraph>123</paragraph><paragraph>458[]</paragraph>' );
  507. editor.execute( 'redo' );
  508. output( '<paragraph>123</paragraph><paragraph>4[]</paragraph><paragraph>58</paragraph>' );
  509. editor.execute( 'redo' );
  510. output( '<paragraph>1234[]</paragraph><paragraph>58</paragraph>' );
  511. editor.execute( 'redo' );
  512. output( '<paragraph>123458[]</paragraph>' );
  513. redoDisabled();
  514. } );
  515. it( 'split, typing, split, merge, merge', () => {
  516. input( '<paragraph>12345678</paragraph>' );
  517. split( [ 0, 3 ] );
  518. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  519. type( [ 1, 4 ], 'x' );
  520. type( [ 1, 5 ], 'y' );
  521. output( '<paragraph>123</paragraph><paragraph>4567xy[]8</paragraph>' );
  522. split( [ 1, 2 ] );
  523. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]67xy8</paragraph>' );
  524. merge( [ 1 ] );
  525. output( '<paragraph>123[]45</paragraph><paragraph>67xy8</paragraph>' );
  526. merge( [ 1 ] );
  527. output( '<paragraph>12345[]67xy8</paragraph>' );
  528. editor.execute( 'undo' );
  529. output( '<paragraph>12345</paragraph><paragraph>67xy8[]</paragraph>' );
  530. editor.execute( 'undo' );
  531. output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>67xy8</paragraph>' );
  532. editor.execute( 'undo' );
  533. output( '<paragraph>123</paragraph><paragraph>4567xy8[]</paragraph>' );
  534. editor.execute( 'undo' );
  535. output( '<paragraph>123</paragraph><paragraph>4567[]8</paragraph>' );
  536. editor.execute( 'undo' );
  537. output( '<paragraph>12345678[]</paragraph>' );
  538. undoDisabled();
  539. editor.execute( 'redo' );
  540. output( '<paragraph>123</paragraph><paragraph>45678[]</paragraph>' );
  541. editor.execute( 'redo' );
  542. output( '<paragraph>123</paragraph><paragraph>4567xy8[]</paragraph>' );
  543. editor.execute( 'redo' );
  544. output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>67xy8</paragraph>' );
  545. editor.execute( 'redo' );
  546. output( '<paragraph>12345[]</paragraph><paragraph>67xy8</paragraph>' );
  547. editor.execute( 'redo' );
  548. output( '<paragraph>1234567xy8[]</paragraph>' );
  549. redoDisabled();
  550. } );
  551. } );
  552. describe( 'other reported cases', () => {
  553. // ckeditor5-engine#t/1051
  554. it( 'rename leaks to other elements on undo #1', () => {
  555. input( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  556. model.change( writer => {
  557. writer.rename( root.getChild( 0 ), 'paragraph' );
  558. } );
  559. output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  560. model.change( writer => {
  561. writer.split( Position.createAt( root.getChild( 0 ), 1 ) );
  562. } );
  563. output( '<paragraph>[]F</paragraph><paragraph>oo</paragraph><paragraph>Bar</paragraph>' );
  564. model.change( writer => {
  565. writer.merge( Position.createAt( root, 2 ) );
  566. } );
  567. output( '<paragraph>[]F</paragraph><paragraph>ooBar</paragraph>' );
  568. editor.execute( 'undo' );
  569. output( '<paragraph>[]F</paragraph><paragraph>oo</paragraph><paragraph>Bar</paragraph>' );
  570. editor.execute( 'undo' );
  571. output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  572. editor.execute( 'undo' );
  573. output( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  574. } );
  575. // Similar issue that bases on the same error as above, however here we first merge (above we first split).
  576. it( 'rename leaks to other elements on undo #2', () => {
  577. input( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  578. model.change( writer => {
  579. writer.rename( root.getChild( 0 ), 'heading2' );
  580. } );
  581. output( '<heading2>[]Foo</heading2><paragraph>Bar</paragraph>' );
  582. model.change( writer => {
  583. writer.merge( Position.createAt( root, 1 ) );
  584. } );
  585. output( '<heading2>[]FooBar</heading2>' );
  586. editor.execute( 'undo' );
  587. output( '<heading2>[]Foo</heading2><paragraph>Bar</paragraph>' );
  588. editor.execute( 'undo' );
  589. output( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  590. } );
  591. // Reverse issue, this time first operation is merge and then rename.
  592. it( 'merge, rename, undo, undo is correct', () => {
  593. input( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  594. model.change( writer => {
  595. writer.merge( Position.createAt( root, 1 ) );
  596. } );
  597. output( '<heading1>[]FooBar</heading1>' );
  598. model.change( writer => {
  599. writer.rename( root.getChild( 0 ), 'heading2' );
  600. } );
  601. output( '<heading2>[]FooBar</heading2>' );
  602. editor.execute( 'undo' );
  603. output( '<heading1>[]FooBar</heading1>' );
  604. editor.execute( 'undo' );
  605. output( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  606. } );
  607. // ckeditor5-engine#t/1053
  608. it( 'wrap, split, undo, undo is correct', () => {
  609. input( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  610. model.change( writer => {
  611. writer.wrap( Range.createIn( root ), 'div' );
  612. } );
  613. output( '<div><paragraph>[]Foo</paragraph><paragraph>Bar</paragraph></div>' );
  614. model.change( writer => {
  615. writer.split( new Position( root, [ 0, 0, 1 ] ) );
  616. } );
  617. output( '<div><paragraph>[]F</paragraph><paragraph>oo</paragraph><paragraph>Bar</paragraph></div>' );
  618. editor.execute( 'undo' );
  619. output( '<div><paragraph>[]Foo</paragraph><paragraph>Bar</paragraph></div>' );
  620. editor.execute( 'undo' );
  621. output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  622. } );
  623. // ckeditor5-engine#t/1055
  624. it( 'selection attribute setting: split, bold, merge, undo, undo, undo', () => {
  625. input( '<paragraph>Foo[]</paragraph><paragraph>Bar</paragraph>' );
  626. editor.execute( 'enter' );
  627. output( '<paragraph>Foo</paragraph><paragraph>[]</paragraph><paragraph>Bar</paragraph>' );
  628. editor.execute( 'bold' );
  629. output(
  630. '<paragraph>Foo</paragraph>' +
  631. '<paragraph selection:bold="true"><$text bold="true">[]</$text></paragraph>' +
  632. '<paragraph>Bar</paragraph>'
  633. );
  634. editor.execute( 'forwardDelete' );
  635. output( '<paragraph>Foo</paragraph><paragraph>[]Bar</paragraph>' );
  636. editor.execute( 'undo' );
  637. output(
  638. '<paragraph>Foo</paragraph>' +
  639. '<paragraph selection:bold="true"><$text bold="true">[]</$text></paragraph>' +
  640. '<paragraph>Bar</paragraph>'
  641. );
  642. editor.execute( 'undo' );
  643. output( '<paragraph>Foo</paragraph><paragraph>[]</paragraph><paragraph>Bar</paragraph>' );
  644. editor.execute( 'undo' );
  645. output( '<paragraph>Foo[]</paragraph><paragraph>Bar</paragraph>' );
  646. } );
  647. // https://github.com/ckeditor/ckeditor5-undo/issues/65#issuecomment-323682195
  648. it( 'undoing split after the element created by split has been removed', () => {
  649. input( '<paragraph>Foo[]bar</paragraph>' );
  650. editor.execute( 'enter' );
  651. model.change( writer => {
  652. const range = new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 1, 3 ] ) );
  653. writer.setSelection( range );
  654. editor.execute( 'delete' );
  655. } );
  656. editor.execute( 'undo' );
  657. output( '<paragraph>Foo[</paragraph><paragraph>bar]</paragraph>' );
  658. editor.execute( 'undo' );
  659. output( '<paragraph>Foobar[]</paragraph>' );
  660. } );
  661. } );
  662. describe( 'clipboard', () => {
  663. function pasteHtml( editor, html ) {
  664. editor.editing.view.document.fire( 'paste', {
  665. dataTransfer: createDataTransfer( { 'text/html': html } ),
  666. preventDefault() {}
  667. } );
  668. }
  669. function createDataTransfer( data ) {
  670. return {
  671. getData( type ) {
  672. return data[ type ];
  673. },
  674. setData() {}
  675. };
  676. }
  677. // ckeditor5-engine#t/1065
  678. it( 'undo paste into non empty element should not throw and be correct', () => {
  679. model.change( () => {
  680. input( '<paragraph>Foo[]</paragraph>' );
  681. } );
  682. model.change( () => {
  683. pasteHtml( editor, '<p>a</p><p>b</p>' );
  684. } );
  685. output( '<paragraph>Fooa</paragraph><paragraph>b[]</paragraph>' );
  686. model.change( () => {
  687. pasteHtml( editor, '<p>c</p><p>d</p>' );
  688. } );
  689. output( '<paragraph>Fooa</paragraph><paragraph>bc</paragraph><paragraph>d[]</paragraph>' );
  690. editor.execute( 'undo' );
  691. output( '<paragraph>Fooa</paragraph><paragraph>b[]</paragraph>' );
  692. editor.execute( 'undo' );
  693. output( '<paragraph>Foo[]</paragraph>' );
  694. } );
  695. // ckeditor5#781
  696. it( 'cutting should not create empty undo step', () => {
  697. input( '<paragraph>Fo[oba]r</paragraph>' );
  698. editor.editing.view.document.fire( 'cut', {
  699. dataTransfer: createDataTransfer(),
  700. preventDefault() {},
  701. method: 'cut'
  702. } );
  703. editor.execute( 'undo' );
  704. output( '<paragraph>Fo[oba]r</paragraph>' );
  705. undoDisabled();
  706. } );
  707. } );
  708. describe( 'other edge cases', () => {
  709. it( 'deleteContent between two nodes', () => {
  710. input( '<paragraph>fo[o</paragraph><paragraph>b]ar</paragraph>' );
  711. editor.model.deleteContent( doc.selection );
  712. output( '<paragraph>fo[]ar</paragraph>' );
  713. editor.execute( 'undo' );
  714. output( '<paragraph>fo[o</paragraph><paragraph>b]ar</paragraph>' );
  715. } );
  716. // Related to ckeditor5-engine#891 and ckeditor5-list#51.
  717. it( 'change attribute of removed node then undo and redo', () => {
  718. input( '<paragraph></paragraph>' );
  719. const gy = doc.graveyard;
  720. const p = doc.getRoot().getChild( 0 );
  721. model.change( writer => {
  722. writer.remove( p );
  723. } );
  724. model.change( writer => {
  725. writer.setAttribute( 'bold', true, p );
  726. } );
  727. editor.execute( 'undo' );
  728. editor.execute( 'redo' );
  729. expect( p.root ).to.equal( gy );
  730. expect( p.getAttribute( 'bold' ) ).to.be.true;
  731. } );
  732. it( 'undoing merge after it was already "undone" through transforming by insert delta', () => {
  733. // This is a tricky case that happens during collaborative editing.
  734. // When merge happens at the same place where insert, the merge is automatically undone.
  735. // Then, when the merge is actually undone, the problem occurs.
  736. input( '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>' );
  737. // Remove children from graveyard because they are inserted there after `input` call.
  738. model.change( writer => {
  739. writer.remove( Range.createIn( doc.graveyard ) );
  740. } );
  741. const batchWithMerge = new Batch();
  742. model.enqueueChange( batchWithMerge, writer => {
  743. writer.merge( new Position( root, [ 1 ] ) );
  744. } );
  745. const split = batchWithMerge.deltas[ 0 ].getReversed();
  746. const batch = new Batch();
  747. batch.addDelta( split );
  748. model.applyOperation( split.operations[ 0 ] );
  749. model.applyOperation( split.operations[ 1 ] );
  750. output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  751. editor.execute( 'undo', batchWithMerge );
  752. output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  753. } );
  754. } );
  755. it( 'postfixers should not add another undo step when fixing undo changes', () => {
  756. input( '<paragraph>[]</paragraph>' );
  757. const paragraph = root.getChild( 0 );
  758. // 1. Step - add text and marker to the paragraph.
  759. model.change( writer => {
  760. writer.appendText( 'foo', paragraph );
  761. writer.addMarker( 'marker', { range: Range.createIn( paragraph ), usingOperation: true } );
  762. } );
  763. // 2. Step - remove text from paragraph.
  764. model.change( writer => {
  765. writer.remove( Range.createIn( paragraph ) );
  766. } );
  767. // Register post fixer to remove marker from non-empty paragraph.
  768. model.document.registerPostFixer( writer => {
  769. const hasMarker = model.markers.has( 'marker' );
  770. const isEmpty = paragraph.childCount === 0;
  771. // Remove marker when paragraph is empty.
  772. if ( hasMarker && !isEmpty ) {
  773. writer.removeMarker( 'marker' );
  774. return true;
  775. }
  776. return false;
  777. } );
  778. editor.execute( 'undo' );
  779. // Check if postfixer changes are executed together with undo and there is no additional undo steps.
  780. expect( model.markers.has( 'marker' ) ).to.be.false;
  781. expect( editor.commands.get( 'undo' )._stack.length ).to.equal( 1 );
  782. output( '<paragraph>foo[]</paragraph>' );
  783. } );
  784. function split( path ) {
  785. setSelection( path.slice(), path.slice() );
  786. editor.execute( 'enter' );
  787. }
  788. function merge( path ) {
  789. const selPath = path.slice();
  790. selPath.push( 0 );
  791. setSelection( selPath, selPath.slice() );
  792. editor.execute( 'delete' );
  793. }
  794. function type( path, text ) {
  795. setSelection( path.slice(), path.slice() );
  796. editor.execute( 'input', { text } );
  797. }
  798. function remove( path ) {
  799. setSelection( path.slice(), path.slice() );
  800. editor.execute( 'delete' );
  801. }
  802. } );