undocommand.js 9.4 KB

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