undocommand.js 9.6 KB

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