undocommand.js 9.6 KB

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