8
0

undoengine-integration.js 11 KB

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