undo.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import Editor from '/ckeditor5/editor.js';
  7. import ModelDocument from '/ckeditor5/engine/treemodel/document.js';
  8. import Range from '/ckeditor5/engine/treemodel/range.js';
  9. import Position from '/ckeditor5/engine/treemodel/position.js';
  10. import Undo from '/ckeditor5/undo/undo.js';
  11. import Creator from '/ckeditor5/creator/creator.js';
  12. import { getData, setData } from '/tests/engine/_utils/model.js';
  13. // import deleteContents from '/ckeditor5/engine/treemodel/composer/deletecontents.js';
  14. let element, editor, doc, root;
  15. beforeEach( () => {
  16. element = document.createElement( 'div' );
  17. document.body.appendChild( element );
  18. doc = new ModelDocument();
  19. root = doc.createRoot( 'root' );
  20. editor = new Editor( element, {
  21. creator: Creator,
  22. features: [ Undo ]
  23. } );
  24. editor.document = doc;
  25. return editor.init();
  26. } );
  27. function setSelection( pathA, pathB ) {
  28. doc.selection.setRanges( [ new Range( new Position( root, pathA ), new Position( root, pathB ) ) ] );
  29. }
  30. function input( input ) {
  31. setData( doc, 'root', input );
  32. }
  33. function output( output ) {
  34. expect( getData( doc, 'root', { selection: true } ) ).to.equal( output );
  35. }
  36. function undoDisabled() {
  37. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false;
  38. }
  39. describe( 'undo integration', () => {
  40. describe( 'adding and removing content', () => {
  41. it( 'add and undo', () => {
  42. input( '<p>fo<selection />o</p><p>bar</p>' );
  43. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  44. output( '<p>fozzz<selection />o</p><p>bar</p>' );
  45. editor.execute( 'undo' );
  46. output( '<p>fo<selection />o</p><p>bar</p>' );
  47. undoDisabled();
  48. } );
  49. it( 'multiple adding and undo', () => {
  50. input( '<p>fo<selection />o</p><p>bar</p>' );
  51. doc.batch()
  52. .insert( doc.selection.getFirstPosition(), 'zzz' )
  53. .insert( new Position( root, [ 1, 0 ] ), 'xxx' );
  54. output( '<p>fozzz<selection />o</p><p>xxxbar</p>' );
  55. setSelection( [ 1, 0 ], [ 1, 0 ] );
  56. doc.batch().insert( doc.selection.getFirstPosition(), 'yyy' );
  57. output( '<p>fozzzo</p><p>yyy<selection />xxxbar</p>' );
  58. editor.execute( 'undo' );
  59. output( '<p>fozzzo</p><p><selection />xxxbar</p>' );
  60. editor.execute( 'undo' );
  61. output( '<p>fo<selection />o</p><p>bar</p>' );
  62. undoDisabled();
  63. } );
  64. it( 'multiple adding mixed with undo', () => {
  65. input( '<p>fo<selection />o</p><p>bar</p>' );
  66. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  67. output( '<p>fozzz<selection />o</p><p>bar</p>' );
  68. setSelection( [ 1, 0 ], [ 1, 0 ] );
  69. doc.batch().insert( doc.selection.getFirstPosition(), 'yyy' );
  70. output( '<p>fozzzo</p><p>yyy<selection />bar</p>' );
  71. editor.execute( 'undo' );
  72. output( '<p>fozzzo</p><p><selection />bar</p>' );
  73. setSelection( [ 0, 0 ], [ 0, 0 ] );
  74. doc.batch().insert( doc.selection.getFirstPosition(), 'xxx' );
  75. output( '<p>xxx<selection />fozzzo</p><p>bar</p>' );
  76. editor.execute( 'undo' );
  77. output( '<p><selection />fozzzo</p><p>bar</p>' );
  78. editor.execute( 'undo' );
  79. output( '<p>fo<selection />o</p><p>bar</p>' );
  80. undoDisabled();
  81. } );
  82. it( 'multiple remove and undo', () => {
  83. input( '<p><selection />foo</p><p>bar</p>' );
  84. doc.batch().remove( Range.createFromPositionAndShift( doc.selection.getFirstPosition(), 2 ) );
  85. output( '<p><selection />o</p><p>bar</p>' );
  86. setSelection( [ 1, 1 ], [ 1, 1 ] );
  87. doc.batch().remove( Range.createFromPositionAndShift( doc.selection.getFirstPosition(), 2 ) );
  88. output( '<p>o</p><p>b<selection /></p>' );
  89. editor.execute( 'undo' );
  90. // Here is an edge case that selection could be before or after `ar` but selection always ends up after.
  91. output( '<p>o</p><p>bar<selection /></p>' );
  92. editor.execute( 'undo' );
  93. // As above.
  94. output( '<p>fo<selection />o</p><p>bar</p>' );
  95. undoDisabled();
  96. } );
  97. it( 'add and remove different parts and undo', () => {
  98. input( '<p>fo<selection />o</p><p>bar</p>' );
  99. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  100. output( '<p>fozzz<selection />o</p><p>bar</p>' );
  101. setSelection( [ 1, 2 ], [ 1, 2 ] );
  102. doc.batch().remove( Range.createFromPositionAndShift( new Position( root, [ 1, 1 ] ) , 1 ) );
  103. output( '<p>fozzzo</p><p>b<selection />r</p>' );
  104. editor.execute( 'undo' );
  105. output( '<p>fozzzo</p><p>ba<selection />r</p>' );
  106. editor.execute( 'undo' );
  107. output( '<p>fo<selection />o</p><p>bar</p>' );
  108. undoDisabled();
  109. } );
  110. //it( 'add and remove same part and undo', () => {
  111. // // This test case fails because some operations are transformed to NoOperations incorrectly.
  112. // input( '<p>fo<selection />o</p><p>bar</p>' );
  113. //
  114. // doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  115. // output( '<p>fozzz<selection />o</p><p>bar</p>' );
  116. //
  117. // doc.batch().remove( Range.createFromPositionAndShift( new Position( root, [ 0, 2 ] ) , 3 ) );
  118. // output( '<p>fo<selection />o</p><p>bar</p>' );
  119. //
  120. // editor.execute( 'undo' );
  121. // output( '<p>fozzz<selection />o</p><p>bar</p>' );
  122. //
  123. // editor.execute( 'undo' );
  124. // output( '<p>fo<selection />o</p><p>bar</p>' );
  125. //
  126. // undoDisabled();
  127. //} );
  128. } );
  129. describe( 'moving', () => {
  130. //it( 'move same content twice then undo', () => {
  131. // // This test case fails because some operations are transformed to NoOperations incorrectly.
  132. // input( '<p>f<selection>o</selection>z</p><p>bar</p>' );
  133. //
  134. // doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) );
  135. // output( '<p>fz</p><p><selection>o</selection>bar</p>' );
  136. //
  137. // doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 0, 2 ] ) );
  138. // output( '<p>fz<selection>o</selection></p><p>bar</p>' );
  139. //
  140. // editor.execute( 'undo' );
  141. // output( '<p>fz</p><p><selection>o</selection>bar</p>' );
  142. //
  143. // editor.execute( 'undo' );
  144. // output( '<p>f<selection>o</selection>z</p><p>bar</p>' );
  145. //
  146. // undoDisabled();
  147. //} );
  148. it( 'move content and new parent then undo', () => {
  149. input( '<p>f<selection>o</selection>z</p><p>bar</p>' );
  150. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) );
  151. output( '<p>fz</p><p><selection>o</selection>bar</p>' );
  152. setSelection( [ 1 ], [ 2 ] );
  153. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 0 ] ) );
  154. output( '<selection><p>obar</p></selection><p>fz</p>' );
  155. editor.execute( 'undo' );
  156. output( '<p>fz</p><selection><p>obar</p></selection>' );
  157. editor.execute( 'undo' );
  158. output( '<p>f<selection>o</selection>z</p><p>bar</p>' );
  159. undoDisabled();
  160. } );
  161. } );
  162. describe( 'attributes with other', () => {
  163. it( 'attributes then insert inside then undo', () => {
  164. input( '<p>fo<selection>ob</selection>ar</p>' );
  165. doc.batch().setAttr( 'bold', true, doc.selection.getFirstRange() );
  166. output( '<p>fo<selection><$text bold=true>ob</$text></selection>ar</p>' );
  167. setSelection( [ 0, 3 ], [ 0, 3 ] );
  168. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  169. output( '<p>fo<$text bold=true>o</$text>zzz<selection bold=true /><$text bold=true>b</$text>ar</p>' );
  170. editor.execute( 'undo' );
  171. output( '<p>fo<$text bold=true>o<selection bold=true />b</$text>ar</p>' );
  172. editor.execute( 'undo' );
  173. output( '<p>fo<selection>ob</selection>ar</p>' );
  174. undoDisabled();
  175. } );
  176. } );
  177. describe( 'wrapping, unwrapping, merging, splitting', () => {
  178. it( 'wrap and undo', () => {
  179. input( 'fo<selection>zb</selection>ar' );
  180. doc.batch().wrap( doc.selection.getFirstRange(), 'p' );
  181. output( 'fo<p><selection>zb</selection></p>ar' );
  182. editor.execute( 'undo' );
  183. output( 'fo<selection>zb</selection>ar' );
  184. undoDisabled();
  185. } );
  186. //it( 'wrap, move and undo', () => {
  187. // input( 'fo<selection>zb</selection>ar' );
  188. //
  189. // doc.batch().wrap( doc.selection.getFirstRange(), 'p' );
  190. // output( 'fo<p><selection>zb</selection></p>ar' );
  191. //
  192. // setSelection( [ 2, 0 ], [ 2, 1 ] );
  193. // doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 0 ] ) );
  194. // output( '<selection>z</selection>fo<p>b</p>ar' );
  195. //
  196. // editor.execute( 'undo' );
  197. // output( 'fo<p><selection>z</selection>b</p>ar' );
  198. //
  199. // // This test case fails here for unknown reason, but "z" letter magically disappears.
  200. // // AssertionError: expected 'fo<selection>b</selection>ar' to equal 'fo<selection>zb</selection>ar'
  201. // editor.execute( 'undo' );
  202. // output( 'fo<selection>zb</selection>ar' );
  203. //
  204. // undoDisabled();
  205. //} );
  206. it( 'unwrap and undo', () => {
  207. input( '<p>foo<selection />bar</p>' );
  208. doc.batch().unwrap( doc.selection.getFirstPosition().parent );
  209. output( 'foo<selection />bar' );
  210. editor.execute( 'undo' );
  211. output( '<p>foo<selection />bar</p>' );
  212. undoDisabled();
  213. } );
  214. //it( 'merge and undo', () => {
  215. // input( '<p>foo</p><p><selection />bar</p>' );
  216. //
  217. // doc.batch().merge( new Position( root, [ 1 ] ) );
  218. // // This test fails here because selection is stuck with <p> element and ends up in graveyard.
  219. // // AssertionError: expected '<p>foobar</p>' to equal '<p>foo<selection />bar</p>'
  220. // output( '<p>foo<selection />bar</p>' );
  221. //
  222. // editor.execute( 'undo' );
  223. // // This test fails because when selection is transformed it is first in empty <p> but when
  224. // // "bar" is inserted, it gets moved to the right.
  225. // // AssertionError: expected '<p>foo</p><p>bar<selection /></p>' to equal '<p>foo</p><p><selection />bar</p>'
  226. // output( '<p>foo</p><p><selection />bar</p>' );
  227. //
  228. // undoDisabled();
  229. //} );
  230. //it( 'split and undo', () => {
  231. // input( '<p>foo<selection />bar</p>' );
  232. //
  233. // doc.batch().split( doc.selection.getFirstPosition() );
  234. // // This test fails because selection ends up in wrong node after splitting.
  235. // // AssertionError: expected '<p>foo<selection /></p><p>bar</p>' to equal '<p>foo</p><p><selection />bar</p>'
  236. // output( '<p>foo</p><p><selection />bar</p>' );
  237. //
  238. // editor.execute( 'undo' );
  239. // // This test fails because selection after transforming ends up after inserted test.
  240. // // AssertionError: expected '<p>foobar<selection /></p>' to equal '<p>foo<selection />bar</p>'
  241. // output( '<p>foo<selection />bar</p>' );
  242. //
  243. // undoDisabled();
  244. //} );
  245. } );
  246. describe( 'other edge cases', () => {
  247. //it( 'deleteContents between two nodes', () => {
  248. // input( '<p>fo<selection>o</p><p>b</selection>ar</p>' );
  249. //
  250. // deleteContents( doc.batch(), doc.selection, { merge: true } );
  251. // output( '<p>fo<selection />ar</p>' );
  252. //
  253. // // This test case fails because of OT problems.
  254. // // When the batch is undone, first MergeDelta is reversed to SplitDelta and it is undone.
  255. // // Then RemoveOperations are reversed to ReinsertOperation.
  256. // // Unfortunately, ReinsertOperation that inserts "o" points to the same position were split happened.
  257. // // Then, when ReinsertOperation is transformed by operations of SplitDelta, it ends up in wrong <p>.
  258. // editor.execute( 'undo' );
  259. // output( '<p>fo<selection>o</p><p>b</selection>ar</p>' );
  260. //} );
  261. } );
  262. } );