undoediting-integration.js 33 KB

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