8
0

undo.js 11 KB

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