undoediting-integration.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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 TableEditing from '@ckeditor/ckeditor5-table/src/tableediting';
  15. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  16. describe( 'UndoEditing integration', () => {
  17. let editor, model, doc, root, div;
  18. beforeEach( () => {
  19. div = document.createElement( 'div' );
  20. document.body.appendChild( div );
  21. return ClassicEditor.create( div, {
  22. plugins: [ Paragraph, HeadingEditing, Typing, Enter, Clipboard, BoldEditing, UndoEditing, TableEditing ]
  23. } ).then( newEditor => {
  24. editor = newEditor;
  25. model = editor.model;
  26. doc = model.document;
  27. root = doc.getRoot();
  28. } );
  29. } );
  30. afterEach( () => {
  31. div.remove();
  32. return editor.destroy();
  33. } );
  34. function setSelection( pathA, pathB ) {
  35. model.change( writer => {
  36. writer.setSelection( writer.createRange(
  37. writer.createPositionFromPath( root, pathA ), writer.createPositionFromPath( root, pathB )
  38. ) );
  39. } );
  40. }
  41. function input( input ) {
  42. model.enqueueChange( 'transparent', () => {
  43. setData( model, input );
  44. } );
  45. }
  46. function output( output ) {
  47. expect( getData( model ) ).to.equal( output );
  48. }
  49. function undoDisabled() {
  50. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false;
  51. }
  52. function redoDisabled() {
  53. expect( editor.commands.get( 'redo' ).isEnabled ).to.be.false;
  54. }
  55. describe( 'adding and removing content', () => {
  56. it( 'add and undo', () => {
  57. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  58. model.change( writer => {
  59. writer.insertText( 'zzz', doc.selection.getFirstPosition() );
  60. } );
  61. output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
  62. editor.execute( 'undo' );
  63. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  64. undoDisabled();
  65. } );
  66. it( 'multiple adding and undo', () => {
  67. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  68. model.change( writer => {
  69. writer.insertText( 'zzz', doc.selection.getFirstPosition() );
  70. writer.insertText( 'xxx', writer.createPositionFromPath( root, [ 1, 0 ] ) );
  71. } );
  72. output( '<paragraph>fozzz[]o</paragraph><paragraph>xxxbar</paragraph>' );
  73. model.change( writer => {
  74. setSelection( [ 1, 0 ], [ 1, 0 ] );
  75. writer.insertText( 'yyy', doc.selection.getFirstPosition() );
  76. } );
  77. output( '<paragraph>fozzzo</paragraph><paragraph>yyy[]xxxbar</paragraph>' );
  78. editor.execute( 'undo' );
  79. output( '<paragraph>fozzzo</paragraph><paragraph>[]xxxbar</paragraph>' );
  80. editor.execute( 'undo' );
  81. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  82. undoDisabled();
  83. } );
  84. it( 'multiple adding mixed with undo', () => {
  85. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  86. model.change( writer => {
  87. writer.insertText( 'zzz', doc.selection.getFirstPosition() );
  88. } );
  89. output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
  90. model.change( writer => {
  91. setSelection( [ 1, 0 ], [ 1, 0 ] );
  92. writer.insertText( 'yyy', doc.selection.getFirstPosition() );
  93. } );
  94. output( '<paragraph>fozzzo</paragraph><paragraph>yyy[]bar</paragraph>' );
  95. editor.execute( 'undo' );
  96. output( '<paragraph>fozzzo</paragraph><paragraph>[]bar</paragraph>' );
  97. model.change( writer => {
  98. setSelection( [ 0, 0 ], [ 0, 0 ] );
  99. writer.insertText( 'xxx', doc.selection.getFirstPosition() );
  100. } );
  101. output( '<paragraph>xxx[]fozzzo</paragraph><paragraph>bar</paragraph>' );
  102. editor.execute( 'undo' );
  103. output( '<paragraph>[]fozzzo</paragraph><paragraph>bar</paragraph>' );
  104. editor.execute( 'undo' );
  105. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  106. undoDisabled();
  107. } );
  108. it( 'multiple remove and undo', () => {
  109. input( '<paragraph>[]foo</paragraph><paragraph>bar</paragraph>' );
  110. model.change( writer => {
  111. const start = doc.selection.getFirstPosition();
  112. writer.remove( writer.createRange( start, start.getShiftedBy( 2 ) ) );
  113. } );
  114. output( '<paragraph>[]o</paragraph><paragraph>bar</paragraph>' );
  115. model.change( writer => {
  116. setSelection( [ 1, 1 ], [ 1, 1 ] );
  117. const start = doc.selection.getFirstPosition();
  118. writer.remove( writer.createRange( start, start.getShiftedBy( 2 ) ) );
  119. } );
  120. output( '<paragraph>o</paragraph><paragraph>b[]</paragraph>' );
  121. editor.execute( 'undo' );
  122. // Here is an edge case that selection could be before or after `ar`.
  123. output( '<paragraph>o</paragraph><paragraph>bar[]</paragraph>' );
  124. editor.execute( 'undo' );
  125. // As above.
  126. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  127. undoDisabled();
  128. } );
  129. it( 'add and remove different parts and undo', () => {
  130. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  131. model.change( writer => {
  132. writer.insertText( 'zzz', doc.selection.getFirstPosition() );
  133. } );
  134. output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
  135. model.change( writer => {
  136. setSelection( [ 1, 2 ], [ 1, 2 ] );
  137. const start = writer.createPositionFromPath( root, [ 1, 1 ] );
  138. writer.remove( writer.createRange( start, start.getShiftedBy( 1 ) ) );
  139. } );
  140. output( '<paragraph>fozzzo</paragraph><paragraph>b[]r</paragraph>' );
  141. editor.execute( 'undo' );
  142. output( '<paragraph>fozzzo</paragraph><paragraph>ba[]r</paragraph>' );
  143. editor.execute( 'undo' );
  144. output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  145. undoDisabled();
  146. } );
  147. it( 'add and remove same part and undo', () => {
  148. input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
  149. model.change( writer => {
  150. writer.insertText( 'zzz', doc.selection.getFirstPosition() );
  151. } );
  152. output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
  153. model.change( writer => {
  154. const start = writer.createPositionFromPath( root, [ 0, 2 ] );
  155. writer.remove( writer.createRange( start, start.getShiftedBy( 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( writer.createRangeIn( 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( writer.createRangeIn( 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 = model.createPositionFromPath( 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(), writer.createPositionFromPath( root, [ 1, 0 ] ) );
  207. } );
  208. output( '<paragraph>fz</paragraph><paragraph>[o]bar</paragraph>' );
  209. model.change( writer => {
  210. writer.move( doc.selection.getFirstRange(), writer.createPositionFromPath( 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(), writer.createPositionFromPath( 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(), writer.createPositionFromPath( 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(), writer.createPositionFromPath( 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( writer.createPositionFromPath( root, [ 1 ] ) );
  305. } );
  306. output( '<paragraph>foo[]bar</paragraph>' );
  307. editor.execute( 'undo' );
  308. output( '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
  309. undoDisabled();
  310. } );
  311. it( 'split and undo', () => {
  312. input( '<paragraph>foo[]bar</paragraph>' );
  313. model.change( writer => {
  314. writer.split( doc.selection.getFirstPosition() );
  315. } );
  316. output( '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
  317. editor.execute( 'undo' );
  318. output( '<paragraph>foo[]bar</paragraph>' );
  319. undoDisabled();
  320. } );
  321. } );
  322. // Restoring selection in those examples may be completely off.
  323. describe( 'multiple enters, deletes and typing', () => {
  324. it( 'split, split, split', () => {
  325. input( '<paragraph>12345678</paragraph>' );
  326. split( [ 0, 3 ] );
  327. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  328. split( [ 1, 4 ] );
  329. output( '<paragraph>123</paragraph><paragraph>4567</paragraph><paragraph>[]8</paragraph>' );
  330. split( [ 1, 2 ] );
  331. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]67</paragraph><paragraph>8</paragraph>' );
  332. editor.execute( 'undo' );
  333. output( '<paragraph>123</paragraph><paragraph>45[]67</paragraph><paragraph>8</paragraph>' );
  334. editor.execute( 'undo' );
  335. output( '<paragraph>123</paragraph><paragraph>4567[]8</paragraph>' );
  336. editor.execute( 'undo' );
  337. output( '<paragraph>123[]45678</paragraph>' );
  338. undoDisabled();
  339. editor.execute( 'redo' );
  340. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  341. editor.execute( 'redo' );
  342. output( '<paragraph>123</paragraph><paragraph>4567[]</paragraph><paragraph>8</paragraph>' );
  343. editor.execute( 'redo' );
  344. output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  345. redoDisabled();
  346. } );
  347. it( 'merge, merge, merge', () => {
  348. input( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  349. merge( [ 1 ] );
  350. output( '<paragraph>123[]45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  351. merge( [ 2 ] );
  352. output( '<paragraph>12345</paragraph><paragraph>67[]8</paragraph>' );
  353. merge( [ 1 ] );
  354. output( '<paragraph>12345[]678</paragraph>' );
  355. editor.execute( 'undo' );
  356. output( '<paragraph>12345[]</paragraph><paragraph>678</paragraph>' );
  357. editor.execute( 'undo' );
  358. output( '<paragraph>12345</paragraph><paragraph>67[]</paragraph><paragraph>8</paragraph>' );
  359. editor.execute( 'undo' );
  360. output( '<paragraph>123[]</paragraph><paragraph>45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  361. undoDisabled();
  362. editor.execute( 'redo' );
  363. output( '<paragraph>123[]45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  364. editor.execute( 'redo' );
  365. output( '<paragraph>12345</paragraph><paragraph>67[]8</paragraph>' );
  366. editor.execute( 'redo' );
  367. output( '<paragraph>12345[]678</paragraph>' );
  368. redoDisabled();
  369. } );
  370. it( 'split, merge, split, merge (same position)', () => {
  371. input( '<paragraph>12345678</paragraph>' );
  372. split( [ 0, 3 ] );
  373. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  374. merge( [ 1 ] );
  375. output( '<paragraph>123[]45678</paragraph>' );
  376. split( [ 0, 3 ] );
  377. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  378. merge( [ 1 ] );
  379. output( '<paragraph>123[]45678</paragraph>' );
  380. editor.execute( 'undo' );
  381. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  382. editor.execute( 'undo' );
  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>123[]45678</paragraph>' );
  388. undoDisabled();
  389. editor.execute( 'redo' );
  390. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  391. editor.execute( 'redo' );
  392. output( '<paragraph>123[]45678</paragraph>' );
  393. editor.execute( 'redo' );
  394. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  395. editor.execute( 'redo' );
  396. output( '<paragraph>123[]45678</paragraph>' );
  397. redoDisabled();
  398. } );
  399. it( 'split, split, split, merge, merge, merge', () => {
  400. input( '<paragraph>12345678</paragraph>' );
  401. split( [ 0, 3 ] );
  402. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  403. split( [ 1, 4 ] );
  404. output( '<paragraph>123</paragraph><paragraph>4567</paragraph><paragraph>[]8</paragraph>' );
  405. split( [ 1, 2 ] );
  406. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]67</paragraph><paragraph>8</paragraph>' );
  407. merge( [ 1 ] );
  408. output( '<paragraph>123[]45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  409. merge( [ 2 ] );
  410. output( '<paragraph>12345</paragraph><paragraph>67[]8</paragraph>' );
  411. merge( [ 1 ] );
  412. output( '<paragraph>12345[]678</paragraph>' );
  413. editor.execute( 'undo' );
  414. output( '<paragraph>12345[]</paragraph><paragraph>678</paragraph>' );
  415. editor.execute( 'undo' );
  416. output( '<paragraph>12345</paragraph><paragraph>67[]</paragraph><paragraph>8</paragraph>' );
  417. editor.execute( 'undo' );
  418. output( '<paragraph>123[]</paragraph><paragraph>45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  419. editor.execute( 'undo' );
  420. output( '<paragraph>123</paragraph><paragraph>45[]67</paragraph><paragraph>8</paragraph>' );
  421. editor.execute( 'undo' );
  422. output( '<paragraph>123</paragraph><paragraph>4567[]8</paragraph>' );
  423. editor.execute( 'undo' );
  424. output( '<paragraph>123[]45678</paragraph>' );
  425. undoDisabled();
  426. editor.execute( 'redo' );
  427. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  428. editor.execute( 'redo' );
  429. output( '<paragraph>123</paragraph><paragraph>4567[]</paragraph><paragraph>8</paragraph>' );
  430. editor.execute( 'redo' );
  431. output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  432. editor.execute( 'redo' );
  433. output( '<paragraph>123[]45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
  434. editor.execute( 'redo' );
  435. output( '<paragraph>12345</paragraph><paragraph>67[]8</paragraph>' );
  436. editor.execute( 'redo' );
  437. output( '<paragraph>12345[]678</paragraph>' );
  438. redoDisabled();
  439. } );
  440. it( 'split, split, merge, split, merge (different order)', () => {
  441. input( '<paragraph>12345678</paragraph>' );
  442. split( [ 0, 3 ] );
  443. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  444. split( [ 1, 2 ] );
  445. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]678</paragraph>' );
  446. merge( [ 1 ] );
  447. output( '<paragraph>123[]45</paragraph><paragraph>678</paragraph>' );
  448. split( [ 1, 1 ] );
  449. output( '<paragraph>12345</paragraph><paragraph>6</paragraph><paragraph>[]78</paragraph>' );
  450. merge( [ 1 ] );
  451. output( '<paragraph>12345[]6</paragraph><paragraph>78</paragraph>' );
  452. editor.execute( 'undo' );
  453. output( '<paragraph>12345[]</paragraph><paragraph>6</paragraph><paragraph>78</paragraph>' );
  454. editor.execute( 'undo' );
  455. output( '<paragraph>12345</paragraph><paragraph>6[]78</paragraph>' );
  456. editor.execute( 'undo' );
  457. output( '<paragraph>123[]</paragraph><paragraph>45</paragraph><paragraph>678</paragraph>' );
  458. editor.execute( 'undo' );
  459. output( '<paragraph>123</paragraph><paragraph>45[]678</paragraph>' );
  460. editor.execute( 'undo' );
  461. output( '<paragraph>123[]45678</paragraph>' );
  462. undoDisabled();
  463. editor.execute( 'redo' );
  464. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  465. editor.execute( 'redo' );
  466. output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>678</paragraph>' );
  467. editor.execute( 'redo' );
  468. output( '<paragraph>123[]45</paragraph><paragraph>678</paragraph>' );
  469. editor.execute( 'redo' );
  470. output( '<paragraph>12345</paragraph><paragraph>6[]</paragraph><paragraph>78</paragraph>' );
  471. editor.execute( 'redo' );
  472. output( '<paragraph>12345[]6</paragraph><paragraph>78</paragraph>' );
  473. redoDisabled();
  474. } );
  475. it( 'split, remove, split, merge, merge', () => {
  476. input( '<paragraph>12345678</paragraph>' );
  477. split( [ 0, 3 ] );
  478. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  479. remove( [ 1, 4 ] );
  480. remove( [ 1, 3 ] );
  481. output( '<paragraph>123</paragraph><paragraph>45[]8</paragraph>' );
  482. split( [ 1, 1 ] );
  483. output( '<paragraph>123</paragraph><paragraph>4</paragraph><paragraph>[]58</paragraph>' );
  484. merge( [ 1 ] );
  485. output( '<paragraph>123[]4</paragraph><paragraph>58</paragraph>' );
  486. merge( [ 1 ] );
  487. output( '<paragraph>1234[]58</paragraph>' );
  488. editor.execute( 'undo' );
  489. output( '<paragraph>1234[]</paragraph><paragraph>58</paragraph>' );
  490. editor.execute( 'undo' );
  491. output( '<paragraph>123[]</paragraph><paragraph>4</paragraph><paragraph>58</paragraph>' );
  492. editor.execute( 'undo' );
  493. output( '<paragraph>123</paragraph><paragraph>4[]58</paragraph>' );
  494. editor.execute( 'undo' );
  495. output( '<paragraph>123</paragraph><paragraph>4567[]8</paragraph>' );
  496. editor.execute( 'undo' );
  497. output( '<paragraph>123[]45678</paragraph>' );
  498. undoDisabled();
  499. editor.execute( 'redo' );
  500. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  501. editor.execute( 'redo' );
  502. output( '<paragraph>123</paragraph><paragraph>45[]8</paragraph>' );
  503. editor.execute( 'redo' );
  504. output( '<paragraph>123</paragraph><paragraph>4[]</paragraph><paragraph>58</paragraph>' );
  505. editor.execute( 'redo' );
  506. output( '<paragraph>123[]4</paragraph><paragraph>58</paragraph>' );
  507. editor.execute( 'redo' );
  508. output( '<paragraph>1234[]58</paragraph>' );
  509. redoDisabled();
  510. } );
  511. it( 'split, typing, split, merge, merge', () => {
  512. input( '<paragraph>12345678</paragraph>' );
  513. split( [ 0, 3 ] );
  514. output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
  515. type( [ 1, 4 ], 'x' );
  516. type( [ 1, 5 ], 'y' );
  517. output( '<paragraph>123</paragraph><paragraph>4567xy[]8</paragraph>' );
  518. split( [ 1, 2 ] );
  519. output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]67xy8</paragraph>' );
  520. merge( [ 1 ] );
  521. output( '<paragraph>123[]45</paragraph><paragraph>67xy8</paragraph>' );
  522. merge( [ 1 ] );
  523. output( '<paragraph>12345[]67xy8</paragraph>' );
  524. editor.execute( 'undo' );
  525. output( '<paragraph>12345[]</paragraph><paragraph>67xy8</paragraph>' );
  526. editor.execute( 'undo' );
  527. output( '<paragraph>123[]</paragraph><paragraph>45</paragraph><paragraph>67xy8</paragraph>' );
  528. editor.execute( 'undo' );
  529. output( '<paragraph>123</paragraph><paragraph>45[]67xy8</paragraph>' );
  530. editor.execute( 'undo' );
  531. output( '<paragraph>123</paragraph><paragraph>4567[]8</paragraph>' );
  532. editor.execute( 'undo' );
  533. output( '<paragraph>123[]45678</paragraph>' );
  534. undoDisabled();
  535. editor.execute( 'redo' );
  536. output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
  537. editor.execute( 'redo' );
  538. output( '<paragraph>123</paragraph><paragraph>4567xy[]8</paragraph>' );
  539. editor.execute( 'redo' );
  540. output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>67xy8</paragraph>' );
  541. editor.execute( 'redo' );
  542. output( '<paragraph>123[]45</paragraph><paragraph>67xy8</paragraph>' );
  543. editor.execute( 'redo' );
  544. output( '<paragraph>12345[]67xy8</paragraph>' );
  545. redoDisabled();
  546. } );
  547. } );
  548. describe( 'other reported cases', () => {
  549. // ckeditor5-engine#t/1051
  550. it( 'rename leaks to other elements on undo #1', () => {
  551. input( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  552. model.change( writer => {
  553. writer.rename( root.getChild( 0 ), 'paragraph' );
  554. } );
  555. output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  556. model.change( writer => {
  557. writer.split( writer.createPositionAt( root.getChild( 0 ), 1 ) );
  558. } );
  559. output( '<paragraph>[]F</paragraph><paragraph>oo</paragraph><paragraph>Bar</paragraph>' );
  560. model.change( writer => {
  561. writer.merge( writer.createPositionAt( root, 2 ) );
  562. } );
  563. output( '<paragraph>[]F</paragraph><paragraph>ooBar</paragraph>' );
  564. editor.execute( 'undo' );
  565. output( '<paragraph>[]F</paragraph><paragraph>oo</paragraph><paragraph>Bar</paragraph>' );
  566. editor.execute( 'undo' );
  567. output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  568. editor.execute( 'undo' );
  569. output( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  570. } );
  571. // Similar issue that bases on the same error as above, however here we first merge (above we first split).
  572. it( 'rename leaks to other elements on undo #2', () => {
  573. input( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  574. model.change( writer => {
  575. writer.rename( root.getChild( 0 ), 'heading2' );
  576. } );
  577. output( '<heading2>[]Foo</heading2><paragraph>Bar</paragraph>' );
  578. model.change( writer => {
  579. writer.merge( writer.createPositionAt( root, 1 ) );
  580. } );
  581. output( '<heading2>[]FooBar</heading2>' );
  582. editor.execute( 'undo' );
  583. output( '<heading2>[]Foo</heading2><paragraph>Bar</paragraph>' );
  584. editor.execute( 'undo' );
  585. output( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  586. } );
  587. // Reverse issue, this time first operation is merge and then rename.
  588. it( 'merge, rename, undo, undo is correct', () => {
  589. input( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  590. model.change( writer => {
  591. writer.merge( writer.createPositionAt( root, 1 ) );
  592. } );
  593. output( '<heading1>[]FooBar</heading1>' );
  594. model.change( writer => {
  595. writer.rename( root.getChild( 0 ), 'heading2' );
  596. } );
  597. output( '<heading2>[]FooBar</heading2>' );
  598. editor.execute( 'undo' );
  599. output( '<heading1>[]FooBar</heading1>' );
  600. editor.execute( 'undo' );
  601. output( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
  602. } );
  603. // ckeditor5-engine#t/1053
  604. it( 'wrap, split, undo, undo is correct', () => {
  605. // Add a "div feature".
  606. model.schema.register( 'div', {
  607. allowWhere: '$block',
  608. allowContentOf: '$root'
  609. } );
  610. editor.conversion.for( 'downcast' ).elementToElement( { model: 'div', view: 'div' } );
  611. editor.conversion.for( 'upcast' ).elementToElement( { model: 'div', view: 'div' } );
  612. input( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  613. model.change( writer => {
  614. writer.wrap( writer.createRangeIn( root ), 'div' );
  615. } );
  616. output( '<div><paragraph>[]Foo</paragraph><paragraph>Bar</paragraph></div>' );
  617. model.change( writer => {
  618. writer.split( writer.createPositionFromPath( root, [ 0, 0, 1 ] ) );
  619. } );
  620. output( '<div><paragraph>[]F</paragraph><paragraph>oo</paragraph><paragraph>Bar</paragraph></div>' );
  621. editor.execute( 'undo' );
  622. output( '<div><paragraph>[]Foo</paragraph><paragraph>Bar</paragraph></div>' );
  623. editor.execute( 'undo' );
  624. output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
  625. } );
  626. // ckeditor5-engine#t/1055
  627. it( 'selection attribute setting: split, bold, merge, undo, undo, undo', () => {
  628. input( '<paragraph>Foo[]</paragraph><paragraph>Bar</paragraph>' );
  629. editor.execute( 'enter' );
  630. output( '<paragraph>Foo</paragraph><paragraph>[]</paragraph><paragraph>Bar</paragraph>' );
  631. editor.execute( 'bold' );
  632. output(
  633. '<paragraph>Foo</paragraph>' +
  634. '<paragraph selection:bold="true"><$text bold="true">[]</$text></paragraph>' +
  635. '<paragraph>Bar</paragraph>'
  636. );
  637. editor.execute( 'forwardDelete' );
  638. output( '<paragraph>Foo</paragraph><paragraph>[]Bar</paragraph>' );
  639. editor.execute( 'undo' );
  640. output(
  641. '<paragraph>Foo</paragraph>' +
  642. '<paragraph selection:bold="true"><$text bold="true">[]</$text></paragraph>' +
  643. '<paragraph>Bar</paragraph>'
  644. );
  645. editor.execute( 'undo' );
  646. output( '<paragraph>Foo</paragraph><paragraph>[]</paragraph><paragraph>Bar</paragraph>' );
  647. editor.execute( 'undo' );
  648. output( '<paragraph>Foo[]</paragraph><paragraph>Bar</paragraph>' );
  649. } );
  650. // https://github.com/ckeditor/ckeditor5-undo/issues/65#issuecomment-323682195
  651. it( 'undoing split after the element created by split has been removed', () => {
  652. input( '<paragraph>Foo[]bar</paragraph>' );
  653. editor.execute( 'enter' );
  654. model.change( writer => {
  655. const range = writer.createRange(
  656. writer.createPositionFromPath( root, [ 0, 3 ] ),
  657. writer.createPositionFromPath( root, [ 1, 3 ] )
  658. );
  659. writer.setSelection( range );
  660. editor.execute( 'delete' );
  661. } );
  662. output( '<paragraph>Foo[]</paragraph>' );
  663. editor.execute( 'undo' );
  664. output( '<paragraph>Foo[</paragraph><paragraph>bar]</paragraph>' );
  665. editor.execute( 'undo' );
  666. output( '<paragraph>Foo[]bar</paragraph>' );
  667. } );
  668. } );
  669. describe( 'clipboard', () => {
  670. function pasteHtml( editor, html ) {
  671. editor.editing.view.document.fire( 'paste', {
  672. dataTransfer: createDataTransfer( { 'text/html': html } ),
  673. stopPropagation() {},
  674. preventDefault() {}
  675. } );
  676. }
  677. function createDataTransfer( data ) {
  678. return {
  679. getData( type ) {
  680. return data[ type ];
  681. },
  682. setData() {}
  683. };
  684. }
  685. // ckeditor5-engine#t/1065
  686. it( 'undo paste into non empty element should not throw and be correct', () => {
  687. model.change( () => {
  688. input( '<paragraph>Foo[]</paragraph>' );
  689. } );
  690. model.change( () => {
  691. pasteHtml( editor, '<p>a</p><p>b</p>' );
  692. } );
  693. output( '<paragraph>Fooa</paragraph><paragraph>b[]</paragraph>' );
  694. model.change( () => {
  695. pasteHtml( editor, '<p>c</p><p>d</p>' );
  696. } );
  697. output( '<paragraph>Fooa</paragraph><paragraph>bc</paragraph><paragraph>d[]</paragraph>' );
  698. editor.execute( 'undo' );
  699. output( '<paragraph>Fooa</paragraph><paragraph>b[]</paragraph>' );
  700. editor.execute( 'undo' );
  701. output( '<paragraph>Foo[]</paragraph>' );
  702. } );
  703. // ckeditor5#781
  704. it( 'cutting should not create empty undo step', () => {
  705. input( '<paragraph>Fo[oba]r</paragraph>' );
  706. editor.editing.view.document.fire( 'cut', {
  707. dataTransfer: createDataTransfer(),
  708. preventDefault() {},
  709. method: 'cut'
  710. } );
  711. editor.execute( 'undo' );
  712. output( '<paragraph>Fo[oba]r</paragraph>' );
  713. undoDisabled();
  714. } );
  715. } );
  716. describe( 'other edge cases', () => {
  717. it( 'deleteContent between two nodes', () => {
  718. input( '<paragraph>fo[o</paragraph><paragraph>b]ar</paragraph>' );
  719. editor.model.deleteContent( doc.selection );
  720. output( '<paragraph>fo[]ar</paragraph>' );
  721. editor.execute( 'undo' );
  722. output( '<paragraph>fo[o</paragraph><paragraph>b]ar</paragraph>' );
  723. } );
  724. // Related to ckeditor5-engine#891 and ckeditor5-list#51.
  725. it( 'change attribute of removed node then undo and redo', () => {
  726. input( '<paragraph></paragraph>' );
  727. const gy = doc.graveyard;
  728. const p = doc.getRoot().getChild( 0 );
  729. model.change( writer => {
  730. writer.remove( p );
  731. } );
  732. model.change( writer => {
  733. writer.setAttribute( 'bold', true, p );
  734. } );
  735. editor.execute( 'undo' );
  736. editor.execute( 'redo' );
  737. expect( p.root ).to.equal( gy );
  738. expect( p.getAttribute( 'bold' ) ).to.be.true;
  739. } );
  740. it( 'undo table cells merge', () => {
  741. input(
  742. '<table>' +
  743. '<tableRow>' +
  744. '<tableCell><paragraph>00</paragraph></tableCell>' +
  745. '[<tableCell><paragraph>01</paragraph></tableCell>]' +
  746. '[<tableCell><paragraph>02</paragraph></tableCell>]' +
  747. '<tableCell><paragraph>03</paragraph></tableCell>' +
  748. '</tableRow>' +
  749. '<tableRow>' +
  750. '<tableCell><paragraph>10</paragraph></tableCell>' +
  751. '[<tableCell><paragraph>11</paragraph></tableCell>]' +
  752. '[<tableCell><paragraph>12</paragraph></tableCell>]' +
  753. '<tableCell><paragraph>13</paragraph></tableCell>' +
  754. '</tableRow>' +
  755. '<tableRow>' +
  756. '<tableCell><paragraph>20</paragraph></tableCell>' +
  757. '<tableCell><paragraph>21</paragraph></tableCell>' +
  758. '<tableCell><paragraph>22</paragraph></tableCell>' +
  759. '<tableCell><paragraph>23</paragraph></tableCell>' +
  760. '</tableRow>' +
  761. '</table>'
  762. );
  763. editor.execute( 'mergeTableCells' );
  764. output(
  765. '<table>' +
  766. '<tableRow>' +
  767. '<tableCell><paragraph>00</paragraph></tableCell>' +
  768. '<tableCell colspan="2" rowspan="2">' +
  769. '<paragraph>[01</paragraph>' +
  770. '<paragraph>02</paragraph>' +
  771. '<paragraph>11</paragraph>' +
  772. '<paragraph>12]</paragraph>' +
  773. '</tableCell>' +
  774. '<tableCell><paragraph>03</paragraph></tableCell>' +
  775. '</tableRow>' +
  776. '<tableRow>' +
  777. '<tableCell><paragraph>10</paragraph></tableCell>' +
  778. '<tableCell><paragraph>13</paragraph></tableCell>' +
  779. '</tableRow>' +
  780. '<tableRow>' +
  781. '<tableCell><paragraph>20</paragraph></tableCell>' +
  782. '<tableCell><paragraph>21</paragraph></tableCell>' +
  783. '<tableCell><paragraph>22</paragraph></tableCell>' +
  784. '<tableCell><paragraph>23</paragraph></tableCell>' +
  785. '</tableRow>' +
  786. '</table>'
  787. );
  788. editor.execute( 'undo' );
  789. output(
  790. '<table>' +
  791. '<tableRow>' +
  792. '<tableCell><paragraph>00</paragraph></tableCell>' +
  793. '[<tableCell><paragraph>01</paragraph></tableCell>]' +
  794. '[<tableCell><paragraph>02</paragraph></tableCell>]' +
  795. '<tableCell><paragraph>03</paragraph></tableCell>' +
  796. '</tableRow>' +
  797. '<tableRow>' +
  798. '<tableCell><paragraph>10</paragraph></tableCell>' +
  799. '[<tableCell><paragraph>11</paragraph></tableCell>]' +
  800. '[<tableCell><paragraph>12</paragraph></tableCell>]' +
  801. '<tableCell><paragraph>13</paragraph></tableCell>' +
  802. '</tableRow>' +
  803. '<tableRow>' +
  804. '<tableCell><paragraph>20</paragraph></tableCell>' +
  805. '<tableCell><paragraph>21</paragraph></tableCell>' +
  806. '<tableCell><paragraph>22</paragraph></tableCell>' +
  807. '<tableCell><paragraph>23</paragraph></tableCell>' +
  808. '</tableRow>' +
  809. '</table>'
  810. );
  811. } );
  812. it( 'undo table cells content wrapping', () => {
  813. model.schema.register( 'tableCellContent', {
  814. allowIn: 'tableCell',
  815. allowContentOf: 'tableCell',
  816. isLimit: true
  817. } );
  818. editor.conversion.elementToElement( { model: 'tableCellContent', view: 'td-content' } );
  819. input(
  820. '<table>' +
  821. '<tableRow>' +
  822. '<tableCell><paragraph>00</paragraph></tableCell>' +
  823. '[<tableCell><paragraph>01</paragraph></tableCell>]' +
  824. '[<tableCell><paragraph>02</paragraph></tableCell>]' +
  825. '<tableCell><paragraph>03</paragraph></tableCell>' +
  826. '</tableRow>' +
  827. '<tableRow>' +
  828. '<tableCell><paragraph>10</paragraph></tableCell>' +
  829. '[<tableCell><paragraph>11</paragraph></tableCell>]' +
  830. '[<tableCell><paragraph>12</paragraph></tableCell>]' +
  831. '<tableCell><paragraph>13</paragraph></tableCell>' +
  832. '</tableRow>' +
  833. '</table>'
  834. );
  835. model.change( writer => {
  836. const targetCell = root.getNodeByPath( [ 0, 0, 1 ] );
  837. const insertionWrapper = writer.createElement( 'tableCellContent' );
  838. const deletionWrapper = writer.createElement( 'tableCellContent' );
  839. const paragraph = writer.createElement( 'paragraph' );
  840. writer.wrap( writer.createRangeIn( targetCell ), deletionWrapper );
  841. writer.insert( insertionWrapper, targetCell, 0 );
  842. writer.insert( writer.createText( 'foobar' ), paragraph, 0 );
  843. writer.insert( paragraph, insertionWrapper, 'end' );
  844. writer.setSelection( writer.createRangeOn( targetCell ) );
  845. } );
  846. output(
  847. '<table>' +
  848. '<tableRow>' +
  849. '<tableCell><paragraph>00</paragraph></tableCell>' +
  850. '[<tableCell>' +
  851. '<tableCellContent><paragraph>foobar</paragraph></tableCellContent>' +
  852. '<tableCellContent><paragraph>01</paragraph></tableCellContent>' +
  853. '</tableCell>]' +
  854. '<tableCell><paragraph>02</paragraph></tableCell>' +
  855. '<tableCell><paragraph>03</paragraph></tableCell>' +
  856. '</tableRow>' +
  857. '<tableRow>' +
  858. '<tableCell><paragraph>10</paragraph></tableCell>' +
  859. '<tableCell><paragraph>11</paragraph></tableCell>' +
  860. '<tableCell><paragraph>12</paragraph></tableCell>' +
  861. '<tableCell><paragraph>13</paragraph></tableCell>' +
  862. '</tableRow>' +
  863. '</table>'
  864. );
  865. editor.execute( 'undo' );
  866. output(
  867. '<table>' +
  868. '<tableRow>' +
  869. '<tableCell><paragraph>00</paragraph></tableCell>' +
  870. '[<tableCell><paragraph>01</paragraph></tableCell>]' +
  871. '[<tableCell><paragraph>02</paragraph></tableCell>]' +
  872. '<tableCell><paragraph>03</paragraph></tableCell>' +
  873. '</tableRow>' +
  874. '<tableRow>' +
  875. '<tableCell><paragraph>10</paragraph></tableCell>' +
  876. '[<tableCell><paragraph>11</paragraph></tableCell>]' +
  877. '[<tableCell><paragraph>12</paragraph></tableCell>]' +
  878. '<tableCell><paragraph>13</paragraph></tableCell>' +
  879. '</tableRow>' +
  880. '</table>'
  881. );
  882. } );
  883. } );
  884. it( 'postfixers should not add another undo step when fixing undo changes', () => {
  885. input( '<paragraph>[]</paragraph>' );
  886. const paragraph = root.getChild( 0 );
  887. // 1. Step - add text and marker to the paragraph.
  888. model.change( writer => {
  889. writer.appendText( 'foo', paragraph );
  890. writer.addMarker( 'marker', { range: writer.createRangeIn( paragraph ), usingOperation: true } );
  891. } );
  892. // 2. Step - remove text from paragraph.
  893. model.change( writer => {
  894. writer.remove( writer.createRangeIn( paragraph ) );
  895. } );
  896. // Register post fixer to remove marker from non-empty paragraph.
  897. model.document.registerPostFixer( writer => {
  898. const hasMarker = model.markers.has( 'marker' );
  899. const isEmpty = paragraph.childCount === 0;
  900. // Remove marker when paragraph is empty.
  901. if ( hasMarker && !isEmpty ) {
  902. writer.removeMarker( 'marker' );
  903. return true;
  904. }
  905. return false;
  906. } );
  907. editor.execute( 'undo' );
  908. // Check if postfixer changes are executed together with undo and there is no additional undo steps.
  909. expect( model.markers.has( 'marker' ) ).to.be.false;
  910. expect( editor.commands.get( 'undo' )._stack.length ).to.equal( 1 );
  911. output( '<paragraph>foo[]</paragraph>' );
  912. } );
  913. function split( path ) {
  914. setSelection( path.slice(), path.slice() );
  915. editor.execute( 'enter' );
  916. }
  917. function merge( path ) {
  918. const selPath = path.slice();
  919. selPath.push( 0 );
  920. setSelection( selPath, selPath.slice() );
  921. editor.execute( 'delete' );
  922. }
  923. function type( path, text ) {
  924. setSelection( path.slice(), path.slice() );
  925. editor.execute( 'input', { text } );
  926. }
  927. function remove( path ) {
  928. setSelection( path.slice(), path.slice() );
  929. editor.execute( 'delete' );
  930. }
  931. } );