undocommand.js 9.5 KB

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