8
0

undocommand.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 UndoCommand from '/ckeditor5/undo/undocommand.js';
  10. let editor, doc, root, undo;
  11. beforeEach( () => {
  12. editor = new ModelTestEditor();
  13. undo = new UndoCommand( editor );
  14. doc = editor.document;
  15. root = doc.getRoot();
  16. } );
  17. afterEach( () => {
  18. undo.destroy();
  19. } );
  20. describe( 'UndoCommand', () => {
  21. describe( 'constructor', () => {
  22. it( 'should create undo command with empty batch stack', () => {
  23. expect( undo._checkEnabled() ).to.be.false;
  24. } );
  25. } );
  26. describe( 'clearStack', () => {
  27. it( 'should remove all batches from the stack', () => {
  28. undo.addBatch( doc.batch() );
  29. expect( undo._checkEnabled() ).to.be.true;
  30. undo.clearStack();
  31. expect( undo._checkEnabled() ).to.be.false;
  32. } );
  33. } );
  34. describe( '_checkEnabled', () => {
  35. it( 'should return false if there are no batches in command stack', () => {
  36. expect( undo._checkEnabled() ).to.be.false;
  37. } );
  38. it( 'should return true if there are batches in command stack', () => {
  39. undo.addBatch( doc.batch() );
  40. expect( undo._checkEnabled() ).to.be.true;
  41. } );
  42. } );
  43. describe( '_execute', () => {
  44. const p = pos => new Position( root, [].concat( pos ) );
  45. const r = ( a, b ) => new Range( p( a ), p( b ) );
  46. let batch0, batch1, batch2, batch3;
  47. beforeEach( () => {
  48. /*
  49. [root]
  50. - {}
  51. */
  52. editor.document.selection.setRanges( [ r( 0, 0 ) ] );
  53. batch0 = doc.batch();
  54. undo.addBatch( batch0 );
  55. batch0.insert( p( 0 ), 'foobar' );
  56. /*
  57. [root]
  58. - f
  59. - o
  60. - o
  61. - b
  62. - a
  63. - r{}
  64. */
  65. // Let's make things spicy and this time, make a backward selection.
  66. editor.document.selection.setRanges( [ r( 2, 4 ) ], true );
  67. batch1 = doc.batch();
  68. undo.addBatch( batch1 );
  69. batch1.setAttr( 'key', 'value', r( 2, 4 ) );
  70. /*
  71. [root]
  72. - f
  73. - o
  74. - {o (key: value)
  75. - b} (key: value)
  76. - a
  77. - r
  78. */
  79. editor.document.selection.setRanges( [ r( 1, 3 ) ] );
  80. batch2 = doc.batch();
  81. undo.addBatch( batch2 );
  82. batch2.move( r( 1, 3 ), p( 6 ) );
  83. /*
  84. [root]
  85. - f
  86. - b (key: value)
  87. - a
  88. - r
  89. - {o
  90. - o} (key: value)
  91. */
  92. editor.document.selection.setRanges( [ r( 1, 4 ) ] );
  93. batch3 = doc.batch();
  94. undo.addBatch( batch3 );
  95. batch3.wrap( r( 1, 4 ), 'p' );
  96. /*
  97. [root]
  98. - f
  99. - [p]
  100. - {b (key: value)
  101. - a
  102. - r}
  103. - o
  104. - o (key: value)
  105. */
  106. editor.document.selection.setRanges( [ r( 0, 1 ) ] );
  107. batch2.move( r( 0, 1 ), p( 3 ) );
  108. /*
  109. [root]
  110. - [p]
  111. - b (key: value)
  112. - a
  113. - r
  114. - o
  115. - f
  116. - o{} (key: value)
  117. */
  118. editor.document.selection.setRanges( [ r( 4, 4 ) ] );
  119. } );
  120. it( 'should revert changes done by deltas from the batch that was most recently added to the command stack', () => {
  121. undo._execute();
  122. // Selection is restored. Wrap is removed:
  123. /*
  124. [root]
  125. - {b (key: value)
  126. - a
  127. - r}
  128. - o
  129. - f
  130. - o (key: value)
  131. */
  132. expect( Array.from( root._children._nodes.map( node => node.text ) ).join( '' ) ).to.equal( 'barofo' );
  133. expect( root.getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  134. expect( root.getChild( 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
  135. expect( editor.document.selection.getRanges().next().value.isEqual( r( 0, 3 ) ) ).to.be.true;
  136. expect( editor.document.selection.isBackward ).to.be.false;
  137. undo._execute();
  138. // Two moves are removed:
  139. /*
  140. [root]
  141. - f
  142. - {o
  143. - o} (key: value)
  144. - b (key: value)
  145. - a
  146. - r
  147. */
  148. expect( Array.from( root._children._nodes.map( node => node.text ) ).join( '' ) ).to.equal( 'foobar' );
  149. expect( root.getChild( 2 ).getAttribute( 'key' ) ).to.equal( 'value' );
  150. expect( root.getChild( 3 ).getAttribute( 'key' ) ).to.equal( 'value' );
  151. expect( editor.document.selection.getRanges().next().value.isEqual( r( 1, 3 ) ) ).to.be.true;
  152. expect( editor.document.selection.isBackward ).to.be.false;
  153. undo._execute();
  154. // Set attribute is undone:
  155. /*
  156. [root]
  157. - f
  158. - o
  159. - {o
  160. - b}
  161. - a
  162. - r
  163. */
  164. expect( Array.from( root._children._nodes.map( node => node.text ) ).join( '' ) ).to.equal( 'foobar' );
  165. expect( root.getChild( 2 ).hasAttribute( 'key' ) ).to.be.false;
  166. expect( root.getChild( 3 ).hasAttribute( 'key' ) ).to.be.false;
  167. expect( editor.document.selection.getRanges().next().value.isEqual( r( 2, 4 ) ) ).to.be.true;
  168. expect( editor.document.selection.isBackward ).to.be.true;
  169. undo._execute();
  170. // Insert is undone:
  171. /*
  172. [root]
  173. */
  174. expect( root.getChildCount() ).to.equal( 0 );
  175. expect( editor.document.selection.getRanges().next().value.isEqual( r( 0, 0 ) ) ).to.be.true;
  176. } );
  177. it( 'should revert changes done by deltas from given batch, if parameter was passed (test: revert set attribute)', () => {
  178. // editor.document.selection.setRanges( [ r( [ 0, 1 ], [ 0, 2 ] ) ] );
  179. undo._execute( batch1 );
  180. // Remove attribute:
  181. /*
  182. [root]
  183. - [p]
  184. - b
  185. - a
  186. - r
  187. - o
  188. - f
  189. - o
  190. */
  191. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  192. expect( Array.from( root.getChild( 0 )._children._nodes.map( node => node.text ) ).join( '' ) ).to.equal( 'bar' );
  193. expect( root.getChild( 0 ).getChild( 0 ).hasAttribute( 'key' ) ).to.be.false;
  194. expect( root.getChild( 2 ).hasAttribute( 'key' ) ).to.be.false;
  195. expect( root.getChild( 3 ).hasAttribute( 'key' ) ).to.be.false;
  196. // Selection is only partially restored because the range got broken.
  197. // The selection would have to best on letter "b" and letter "o", but it is set only on letter "b".
  198. expect( editor.document.selection.getRanges().next().value.isEqual( r( [ 0, 0 ], [ 0, 1 ] ) ) ).to.be.true;
  199. expect( editor.document.selection.isBackward ).to.be.true;
  200. } );
  201. it( 'should revert changes done by deltas from given batch, if parameter was passed (test: revert insert foobar)', () => {
  202. undo._execute( batch0 );
  203. // Remove foobar:
  204. /*
  205. [root]
  206. - [p]
  207. */
  208. // The `P` element wasn't removed because it wasn`t added by undone batch.
  209. // It would be perfect if the `P` got removed aswell because wrapping was on removed nodes.
  210. // But this would need a lot of logic / hardcoded ifs or a post-fixer.
  211. expect( root.getChildCount() ).to.equal( 1 );
  212. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  213. // Because P element was inserted in the middle of removed text and it was not removed,
  214. // the selection is set after it.
  215. expect( editor.document.selection.getRanges().next().value.isEqual( r( 1, 1 ) ) ).to.be.true;
  216. expect( editor.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 they graveyard and we can check them there.
  221. expect( root.getChildCount() ).to.equal( 1 );
  222. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  223. // Operations for undoing that batch were working on graveyard so document selection should not change.
  224. expect( editor.document.selection.getRanges().next().value.isEqual( r( 1, 1 ) ) ).to.be.true;
  225. expect( editor.document.selection.isBackward ).to.be.false;
  226. expect( doc.graveyard.getChildCount() ).to.equal( 6 );
  227. for ( let char of doc.graveyard._children ) {
  228. expect( char.hasAttribute( 'key' ) ).to.be.false;
  229. }
  230. // Let's undo wrapping. This should leave us with empty root.
  231. undo._execute( batch3 );
  232. expect( root.getChildCount() ).to.equal( 0 );
  233. // Once again transformed range ends up in the graveyard.
  234. // So we do not restore it. But since Selection is a LiveRange itself it will update
  235. // because the node before it (P element) got removed.
  236. expect( editor.document.selection.getRanges().next().value.isEqual( r( 0, 0 ) ) ).to.be.true;
  237. expect( editor.document.selection.isBackward ).to.be.false;
  238. } );
  239. it( 'should fire undo event with the undone batch', () => {
  240. const batch = doc.batch();
  241. const spy = sinon.spy();
  242. undo.on( 'revert', spy );
  243. undo._execute();
  244. expect( spy.calledOnce ).to.be.true;
  245. expect( spy.calledWith( batch ) );
  246. } );
  247. } );
  248. } );