undocommand.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  7. import Position from '@ckeditor/ckeditor5-engine/src/model/position';
  8. import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
  9. import UndoCommand from '../src/undocommand';
  10. import { itemAt, getText } from '@ckeditor/ckeditor5-engine/tests/model/_utils/utils';
  11. describe( 'UndoCommand', () => {
  12. let editor, model, doc, root, undo;
  13. beforeEach( () => {
  14. return ModelTestEditor.create().then( newEditor => {
  15. editor = newEditor;
  16. undo = new UndoCommand( editor );
  17. model = editor.model;
  18. doc = model.document;
  19. root = doc.getRoot();
  20. } );
  21. } );
  22. afterEach( () => {
  23. return editor.destroy();
  24. } );
  25. describe( 'UndoCommand', () => {
  26. const p = pos => new Position( root, [].concat( pos ) );
  27. const r = ( a, b ) => new Range( p( a ), p( b ) );
  28. describe( 'execute()', () => {
  29. let batch0, batch1, batch2, batch3;
  30. beforeEach( () => {
  31. /*
  32. [root]
  33. - {}
  34. */
  35. model.change( writer => {
  36. writer.setSelection( r( 0, 0 ) );
  37. } );
  38. batch0 = new Batch();
  39. undo.addBatch( batch0 );
  40. model.enqueueChange( batch0, writer => {
  41. writer.insertText( 'foobar', p( 0 ) );
  42. } );
  43. /*
  44. [root]
  45. - f
  46. - o
  47. - o
  48. - b
  49. - a
  50. - r{}
  51. */
  52. // Let's make things spicy and this time, make a backward selection.
  53. model.change( writer => {
  54. writer.setSelection( r( 2, 4 ), { backward: true } );
  55. } );
  56. batch1 = new Batch();
  57. undo.addBatch( batch1 );
  58. model.enqueueChange( batch1, writer => {
  59. writer.setAttribute( 'key', 'value', r( 2, 4 ) );
  60. } );
  61. /*
  62. [root]
  63. - f
  64. - o
  65. - {o (key: value)
  66. - b} (key: value)
  67. - a
  68. - r
  69. */
  70. model.change( writer => {
  71. writer.setSelection( r( 1, 3 ) );
  72. } );
  73. batch2 = new Batch();
  74. undo.addBatch( batch2 );
  75. model.enqueueChange( batch2, writer => {
  76. writer.move( r( 1, 3 ), p( 6 ) );
  77. } );
  78. /*
  79. [root]
  80. - f
  81. - b (key: value)
  82. - a
  83. - r
  84. - {o
  85. - o} (key: value)
  86. */
  87. model.change( writer => {
  88. writer.setSelection( r( 1, 4 ) );
  89. } );
  90. batch3 = new Batch();
  91. undo.addBatch( batch3 );
  92. model.enqueueChange( batch3, writer => {
  93. writer.wrap( r( 1, 4 ), 'p' );
  94. } );
  95. /*
  96. [root]
  97. - f
  98. - [p]
  99. - {b (key: value)
  100. - a
  101. - r}
  102. - o
  103. - o (key: value)
  104. */
  105. model.change( writer => {
  106. writer.setSelection( r( 0, 1 ) );
  107. } );
  108. model.enqueueChange( batch2, writer => {
  109. writer.move( r( 0, 1 ), p( 3 ) );
  110. } );
  111. /*
  112. [root]
  113. - [p]
  114. - b (key: value)
  115. - a
  116. - r
  117. - o
  118. - f
  119. - o{} (key: value)
  120. */
  121. model.change( writer => {
  122. writer.setSelection( r( 4, 4 ) );
  123. } );
  124. } );
  125. it( 'should revert changes done by operations from the batch that was most recently added to the command stack', () => {
  126. undo.execute();
  127. // Selection is restored. Wrap is removed:
  128. /*
  129. [root]
  130. - {b (key: value)
  131. - a
  132. - r}
  133. - o
  134. - f
  135. - o (key: value)
  136. */
  137. expect( getText( root ) ).to.equal( 'barofo' );
  138. expect( itemAt( root, 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  139. expect( itemAt( root, 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
  140. expect( editor.model.document.selection.getFirstRange().isEqual( r( 0, 3 ) ) ).to.be.true;
  141. expect( editor.model.document.selection.isBackward ).to.be.false;
  142. undo.execute();
  143. // Two moves are removed:
  144. /*
  145. [root]
  146. - f
  147. - {o
  148. - o} (key: value)
  149. - b (key: value)
  150. - a
  151. - r
  152. */
  153. expect( getText( root ) ).to.equal( 'foobar' );
  154. expect( itemAt( root, 2 ).getAttribute( 'key' ) ).to.equal( 'value' );
  155. expect( itemAt( root, 3 ).getAttribute( 'key' ) ).to.equal( 'value' );
  156. // Since selection restoring is not 100% accurate, selected range is not perfectly correct
  157. // with what is expected in comment above. The correct result would be if range was [ 1 ] - [ 3 ].
  158. expect( editor.model.document.selection.getFirstRange().isEqual( r( 0, 3 ) ) ).to.be.true;
  159. expect( editor.model.document.selection.isBackward ).to.be.false;
  160. undo.execute();
  161. // Set attribute is undone:
  162. /*
  163. [root]
  164. - f
  165. - o
  166. - {o
  167. - b}
  168. - a
  169. - r
  170. */
  171. expect( getText( root ) ).to.equal( 'foobar' );
  172. expect( itemAt( root, 2 ).hasAttribute( 'key' ) ).to.be.false;
  173. expect( itemAt( root, 3 ).hasAttribute( 'key' ) ).to.be.false;
  174. expect( editor.model.document.selection.getFirstRange().isEqual( r( 2, 4 ) ) ).to.be.true;
  175. expect( editor.model.document.selection.isBackward ).to.be.true;
  176. undo.execute();
  177. // Insert is undone:
  178. /*
  179. [root]
  180. */
  181. expect( root.childCount ).to.equal( 0 );
  182. expect( editor.model.document.selection.getFirstRange().isEqual( r( 0, 0 ) ) ).to.be.true;
  183. } );
  184. it( 'should revert changes done by deltas from given batch, if parameter was passed (test: revert set attribute)', () => {
  185. undo.execute( batch1 );
  186. // Remove attribute:
  187. /*
  188. [root]
  189. - [p]
  190. - b
  191. - a
  192. - r
  193. - o
  194. - f
  195. - o
  196. */
  197. expect( itemAt( root, 0 ).name ).to.equal( 'p' );
  198. expect( getText( root.getChild( 0 ) ) ).to.equal( 'bar' );
  199. expect( root.getChild( 1 ).data ).to.equal( 'ofo' );
  200. expect( itemAt( root.getChild( 0 ), 0 ).hasAttribute( 'key' ) ).to.be.false;
  201. expect( itemAt( root, 2 ).hasAttribute( 'key' ) ).to.be.false;
  202. expect( itemAt( root, 3 ).hasAttribute( 'key' ) ).to.be.false;
  203. // Selection is only partially restored because the range got broken.
  204. // The selection would have to best on letter "b" and letter "o", but it is set only on letter "b".
  205. expect( editor.model.document.selection.getFirstRange().isEqual( r( [ 0, 0 ], [ 0, 1 ] ) ) ).to.be.true;
  206. expect( editor.model.document.selection.isBackward ).to.be.true;
  207. } );
  208. it( 'should revert changes done by deltas from given batch, if parameter was passed (test: revert insert foobar)', () => {
  209. undo.execute( batch0 );
  210. // Remove foobar:
  211. /*
  212. [root]
  213. */
  214. expect( root.childCount ).to.equal( 0 );
  215. expect( editor.model.document.selection.getFirstRange().isEqual( r( 0, 0 ) ) ).to.be.true;
  216. expect( editor.model.document.selection.isBackward ).to.be.false;
  217. undo.execute( batch1 );
  218. // Remove attributes.
  219. // This does nothing in the `root` because attributes were set on nodes that already got removed.
  220. // But those nodes should change in the graveyard and we can check them there.
  221. expect( root.childCount ).to.equal( 0 );
  222. expect( editor.model.document.selection.getFirstRange().isEqual( r( 0, 0 ) ) ).to.be.true;
  223. expect( editor.model.document.selection.isBackward ).to.be.false;
  224. expect( doc.graveyard.maxOffset ).to.equal( 4 );
  225. for ( const item of Range.createIn( doc.graveyard ).getItems() ) {
  226. expect( item.hasAttribute( 'key' ) ).to.be.false;
  227. }
  228. // Let's undo wrapping. This will remove the P element and leave us with empty root.
  229. undo.execute( batch3 );
  230. expect( root.maxOffset ).to.equal( 0 );
  231. expect( editor.model.document.selection.getFirstRange().isEqual( r( 0, 0 ) ) ).to.be.true;
  232. expect( editor.model.document.selection.isBackward ).to.be.false;
  233. } );
  234. it( 'should omit deltas with non-document operations', () => {
  235. let element;
  236. model.change( writer => {
  237. element = writer.createElement( 'p' );
  238. undo.addBatch( writer.batch );
  239. writer.setAttribute( 'foo', 'bar', element );
  240. writer.setAttribute( 'foo', 'bar', root );
  241. undo.execute();
  242. } );
  243. expect( element.getAttribute( 'foo' ) ).to.equal( 'bar' );
  244. expect( root.getAttribute( 'foo' ) ).to.not.equal( 'bar' );
  245. } );
  246. it( 'should pass undoing batch to enqueueChange method', () => {
  247. const enqueueChangeSpy = sinon.spy( model, 'enqueueChange' );
  248. const undoSpy = sinon.spy( undo, '_undo' );
  249. undo.execute();
  250. sinon.assert.calledOnce( enqueueChangeSpy );
  251. sinon.assert.calledOnce( undoSpy );
  252. const undoingBatch = enqueueChangeSpy.firstCall.args[ 0 ];
  253. expect( undoingBatch instanceof Batch ).to.be.true;
  254. expect( undoSpy.firstCall.args[ 1 ] ).to.equal( undoingBatch );
  255. } );
  256. } );
  257. it( 'merges touching ranges when restoring selection', () => {
  258. function getCaseText( root ) {
  259. let text = '';
  260. for ( let i = 0; i < root.childCount; i++ ) {
  261. const node = root.getChild( i );
  262. text += node.getAttribute( 'uppercase' ) ? node.data.toUpperCase() : node.data;
  263. }
  264. return text;
  265. }
  266. model.change( writer => {
  267. writer.appendText( 'abcdef', root );
  268. } );
  269. expect( getCaseText( root ) ).to.equal( 'abcdef' );
  270. model.change( writer => {
  271. writer.setSelection( r( 1, 4 ) );
  272. } );
  273. const batch0 = new Batch();
  274. undo.addBatch( batch0 );
  275. model.enqueueChange( batch0, writer => {
  276. writer.setAttribute( 'uppercase', true, r( 1, 4 ) );
  277. } );
  278. expect( getCaseText( root ) ).to.equal( 'aBCDef' );
  279. model.change( writer => {
  280. writer.setSelection( r( 3, 4 ) );
  281. } );
  282. const batch1 = new Batch();
  283. undo.addBatch( batch1 );
  284. model.enqueueChange( batch1, writer => {
  285. writer.move( r( 3, 4 ), p( 1 ) );
  286. } );
  287. expect( getCaseText( root ) ).to.equal( 'aDBCef' );
  288. undo.execute( batch0 );
  289. // After undo-attr: acdbef <--- "cdb" should be selected, it would look weird if only "cd" or "b" is selected
  290. // but the whole unbroken part "cdb" changed attribute.
  291. expect( getCaseText( root ) ).to.equal( 'adbcef' );
  292. expect( editor.model.document.selection.getFirstRange().isEqual( r( 1, 4 ) ) ).to.be.true;
  293. } );
  294. } );
  295. } );