undocommand.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: browser-only */
  6. 'use strict';
  7. import Editor from '/ckeditor5/editor.js';
  8. import ModelDocument from '/ckeditor5/engine/model/document.js';
  9. import Range from '/ckeditor5/engine/model/range.js';
  10. import Position from '/ckeditor5/engine/model/position.js';
  11. import UndoCommand from '/ckeditor5/undo/undocommand.js';
  12. let element, editor, doc, root, undo;
  13. beforeEach( () => {
  14. element = document.createElement( 'div' );
  15. document.body.appendChild( element );
  16. editor = new Editor( element );
  17. undo = new UndoCommand( editor );
  18. doc = new ModelDocument();
  19. editor.document = doc;
  20. root = doc.createRoot( 'root' );
  21. } );
  22. afterEach( () => {
  23. undo.destroy();
  24. } );
  25. describe( 'UndoCommand', () => {
  26. describe( 'constructor', () => {
  27. it( 'should create undo command with empty batch stack', () => {
  28. expect( undo._checkEnabled() ).to.be.false;
  29. } );
  30. } );
  31. describe( 'clearStack', () => {
  32. it( 'should remove all batches from the stack', () => {
  33. undo.addBatch( doc.batch() );
  34. expect( undo._checkEnabled() ).to.be.true;
  35. undo.clearStack();
  36. expect( undo._checkEnabled() ).to.be.false;
  37. } );
  38. } );
  39. describe( '_checkEnabled', () => {
  40. it( 'should return false if there are no batches in command stack', () => {
  41. expect( undo._checkEnabled() ).to.be.false;
  42. } );
  43. it( 'should return true if there are batches in command stack', () => {
  44. undo.addBatch( doc.batch() );
  45. expect( undo._checkEnabled() ).to.be.true;
  46. } );
  47. } );
  48. describe( '_execute', () => {
  49. const p = pos => new Position( root, [].concat( pos ) );
  50. const r = ( a, b ) => new Range( p( a ), p( b ) );
  51. let batch0, batch1, batch2, batch3;
  52. beforeEach( () => {
  53. /*
  54. [root]
  55. - {}
  56. */
  57. editor.document.selection.setRanges( [ r( 0, 0 ) ] );
  58. batch0 = doc.batch();
  59. undo.addBatch( batch0 );
  60. batch0.insert( p( 0 ), 'foobar' );
  61. /*
  62. [root]
  63. - f
  64. - o
  65. - o
  66. - b
  67. - a
  68. - r{}
  69. */
  70. // Let's make things spicy and this time, make a backward selection.
  71. editor.document.selection.setRanges( [ r( 2, 4 ) ], true );
  72. batch1 = doc.batch();
  73. undo.addBatch( batch1 );
  74. batch1.setAttr( 'key', 'value', r( 2, 4 ) );
  75. /*
  76. [root]
  77. - f
  78. - o
  79. - {o (key: value)
  80. - b} (key: value)
  81. - a
  82. - r
  83. */
  84. editor.document.selection.setRanges( [ r( 1, 3 ) ] );
  85. batch2 = doc.batch();
  86. undo.addBatch( batch2 );
  87. batch2.move( r( 1, 3 ), p( 6 ) );
  88. /*
  89. [root]
  90. - f
  91. - b (key: value)
  92. - a
  93. - r
  94. - {o
  95. - o} (key: value)
  96. */
  97. editor.document.selection.setRanges( [ r( 1, 4 ) ] );
  98. batch3 = doc.batch();
  99. undo.addBatch( batch3 );
  100. batch3.wrap( r( 1, 4 ), 'p' );
  101. /*
  102. [root]
  103. - f
  104. - [p]
  105. - {b (key: value)
  106. - a
  107. - r}
  108. - o
  109. - o (key: value)
  110. */
  111. editor.document.selection.setRanges( [ r( 0, 1 ) ] );
  112. batch2.move( r( 0, 1 ), p( 3 ) );
  113. /*
  114. [root]
  115. - [p]
  116. - b (key: value)
  117. - a
  118. - r
  119. - o
  120. - f
  121. - o{} (key: value)
  122. */
  123. editor.document.selection.setRanges( [ r( 4, 4 ) ] );
  124. } );
  125. it( 'should revert changes done by deltas 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( Array.from( root._children._nodes.map( node => node.text ) ).join( '' ) ).to.equal( 'barofo' );
  138. expect( root.getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  139. expect( root.getChild( 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
  140. expect( editor.document.selection.getRanges().next().value.isEqual( r( 0, 3 ) ) ).to.be.true;
  141. expect( editor.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( Array.from( root._children._nodes.map( node => node.text ) ).join( '' ) ).to.equal( 'foobar' );
  154. expect( root.getChild( 2 ).getAttribute( 'key' ) ).to.equal( 'value' );
  155. expect( root.getChild( 3 ).getAttribute( 'key' ) ).to.equal( 'value' );
  156. expect( editor.document.selection.getRanges().next().value.isEqual( r( 1, 3 ) ) ).to.be.true;
  157. expect( editor.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( Array.from( root._children._nodes.map( node => node.text ) ).join( '' ) ).to.equal( 'foobar' );
  170. expect( root.getChild( 2 ).hasAttribute( 'key' ) ).to.be.false;
  171. expect( root.getChild( 3 ).hasAttribute( 'key' ) ).to.be.false;
  172. expect( editor.document.selection.getRanges().next().value.isEqual( r( 2, 4 ) ) ).to.be.true;
  173. expect( editor.document.selection.isBackward ).to.be.true;
  174. undo._execute();
  175. // Insert is undone:
  176. /*
  177. [root]
  178. */
  179. expect( root.getChildCount() ).to.equal( 0 );
  180. expect( editor.document.selection.getRanges().next().value.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. // editor.document.selection.setRanges( [ r( [ 0, 1 ], [ 0, 2 ] ) ] );
  184. undo._execute( batch1 );
  185. // Remove attribute:
  186. /*
  187. [root]
  188. - [p]
  189. - b
  190. - a
  191. - r
  192. - o
  193. - f
  194. - o
  195. */
  196. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  197. expect( Array.from( root.getChild( 0 )._children._nodes.map( node => node.text ) ).join( '' ) ).to.equal( 'bar' );
  198. expect( root.getChild( 0 ).getChild( 0 ).hasAttribute( 'key' ) ).to.be.false;
  199. expect( root.getChild( 2 ).hasAttribute( 'key' ) ).to.be.false;
  200. expect( root.getChild( 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.document.selection.getRanges().next().value.isEqual( r( [ 0, 0 ], [ 0, 1 ] ) ) ).to.be.true;
  204. expect( editor.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. // The `P` element wasn't removed because it wasn`t added by undone batch.
  214. // It would be perfect if the `P` got removed aswell because wrapping was on removed nodes.
  215. // But this would need a lot of logic / hardcoded ifs or a post-fixer.
  216. expect( root.getChildCount() ).to.equal( 1 );
  217. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  218. // Because P element was inserted in the middle of removed text and it was not removed,
  219. // the selection is set after it.
  220. expect( editor.document.selection.getRanges().next().value.isEqual( r( 1, 1 ) ) ).to.be.true;
  221. expect( editor.document.selection.isBackward ).to.be.false;
  222. undo._execute( batch1 );
  223. // Remove attributes.
  224. // This does nothing in the `root` because attributes were set on nodes that already got removed.
  225. // But those nodes should change in they graveyard and we can check them there.
  226. expect( root.getChildCount() ).to.equal( 1 );
  227. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  228. // Operations for undoing that batch were working on graveyard so document selection should not change.
  229. expect( editor.document.selection.getRanges().next().value.isEqual( r( 1, 1 ) ) ).to.be.true;
  230. expect( editor.document.selection.isBackward ).to.be.false;
  231. expect( doc.graveyard.getChildCount() ).to.equal( 6 );
  232. for ( let char of doc.graveyard._children ) {
  233. expect( char.hasAttribute( 'key' ) ).to.be.false;
  234. }
  235. // Let's undo wrapping. This should leave us with empty root.
  236. undo._execute( batch3 );
  237. expect( root.getChildCount() ).to.equal( 0 );
  238. // Once again transformed range ends up in the graveyard.
  239. // So we do not restore it. But since Selection is a LiveRange itself it will update
  240. // because the node before it (P element) got removed.
  241. expect( editor.document.selection.getRanges().next().value.isEqual( r( 0, 0 ) ) ).to.be.true;
  242. expect( editor.document.selection.isBackward ).to.be.false;
  243. } );
  244. it( 'should fire undo event with the undone batch', () => {
  245. const batch = doc.batch();
  246. const spy = sinon.spy();
  247. undo.on( 'revert', spy );
  248. undo._execute();
  249. expect( spy.calledOnce ).to.be.true;
  250. expect( spy.calledWith( batch ) );
  251. } );
  252. } );
  253. } );