undocommand.js 8.4 KB

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