undocommand.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from 'tests/core/_utils/modeltesteditor.js';
  6. import Range from 'ckeditor5/engine/model/range.js';
  7. import Position from 'ckeditor5/engine/model/position.js';
  8. import Text from 'ckeditor5/engine/model/text.js';
  9. import UndoCommand from 'ckeditor5/undo/undocommand.js';
  10. import AttributeDelta from 'ckeditor5/engine/model/delta/attributedelta.js';
  11. import { itemAt, getText } from 'tests/engine/model/_utils/utils.js';
  12. describe( 'UndoCommand', () => {
  13. let editor, doc, root, undo;
  14. beforeEach( () => {
  15. editor = new ModelTestEditor();
  16. undo = new UndoCommand( editor );
  17. doc = editor.document;
  18. root = doc.getRoot();
  19. } );
  20. afterEach( () => {
  21. undo.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. editor.document.selection.setRanges( [ r( 0, 0 ) ] );
  34. batch0 = doc.batch();
  35. undo.addBatch( batch0 );
  36. batch0.insert( p( 0 ), 'foobar' );
  37. /*
  38. [root]
  39. - f
  40. - o
  41. - o
  42. - b
  43. - a
  44. - r{}
  45. */
  46. // Let's make things spicy and this time, make a backward selection.
  47. editor.document.selection.setRanges( [ r( 2, 4 ) ], true );
  48. batch1 = doc.batch();
  49. undo.addBatch( batch1 );
  50. batch1.setAttribute( r( 2, 4 ), 'key', 'value' );
  51. /*
  52. [root]
  53. - f
  54. - o
  55. - {o (key: value)
  56. - b} (key: value)
  57. - a
  58. - r
  59. */
  60. editor.document.selection.setRanges( [ r( 1, 3 ) ] );
  61. batch2 = doc.batch();
  62. undo.addBatch( batch2 );
  63. batch2.move( r( 1, 3 ), p( 6 ) );
  64. /*
  65. [root]
  66. - f
  67. - b (key: value)
  68. - a
  69. - r
  70. - {o
  71. - o} (key: value)
  72. */
  73. editor.document.selection.setRanges( [ r( 1, 4 ) ] );
  74. batch3 = doc.batch();
  75. undo.addBatch( batch3 );
  76. batch3.wrap( r( 1, 4 ), 'p' );
  77. /*
  78. [root]
  79. - f
  80. - [p]
  81. - {b (key: value)
  82. - a
  83. - r}
  84. - o
  85. - o (key: value)
  86. */
  87. editor.document.selection.setRanges( [ r( 0, 1 ) ] );
  88. batch2.move( r( 0, 1 ), p( 3 ) );
  89. /*
  90. [root]
  91. - [p]
  92. - b (key: value)
  93. - a
  94. - r
  95. - o
  96. - f
  97. - o{} (key: value)
  98. */
  99. editor.document.selection.setRanges( [ r( 4, 4 ) ] );
  100. } );
  101. it( 'should revert changes done by deltas from the batch that was most recently added to the command stack', () => {
  102. undo._execute();
  103. // Selection is restored. Wrap is removed:
  104. /*
  105. [root]
  106. - {b (key: value)
  107. - a
  108. - r}
  109. - o
  110. - f
  111. - o (key: value)
  112. */
  113. expect( getText( root ) ).to.equal( 'barofo' );
  114. expect( itemAt( root, 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  115. expect( itemAt( root, 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
  116. expect( editor.document.selection.getFirstRange().isEqual( r( 0, 3 ) ) ).to.be.true;
  117. expect( editor.document.selection.isBackward ).to.be.false;
  118. undo._execute();
  119. // Two moves are removed:
  120. /*
  121. [root]
  122. - f
  123. - {o
  124. - o} (key: value)
  125. - b (key: value)
  126. - a
  127. - r
  128. */
  129. expect( getText( root ) ).to.equal( 'foobar' );
  130. expect( itemAt( root, 2 ).getAttribute( 'key' ) ).to.equal( 'value' );
  131. expect( itemAt( root, 3 ).getAttribute( 'key' ) ).to.equal( 'value' );
  132. expect( editor.document.selection.getFirstRange().isEqual( r( 1, 3 ) ) ).to.be.true;
  133. expect( editor.document.selection.isBackward ).to.be.false;
  134. undo._execute();
  135. // Set attribute is undone:
  136. /*
  137. [root]
  138. - f
  139. - o
  140. - {o
  141. - b}
  142. - a
  143. - r
  144. */
  145. expect( getText( root ) ).to.equal( 'foobar' );
  146. expect( itemAt( root, 2 ).hasAttribute( 'key' ) ).to.be.false;
  147. expect( itemAt( root, 3 ).hasAttribute( 'key' ) ).to.be.false;
  148. expect( editor.document.selection.getFirstRange().isEqual( r( 2, 4 ) ) ).to.be.true;
  149. expect( editor.document.selection.isBackward ).to.be.true;
  150. undo._execute();
  151. // Insert is undone:
  152. /*
  153. [root]
  154. */
  155. expect( root.childCount ).to.equal( 0 );
  156. expect( editor.document.selection.getFirstRange().isEqual( r( 0, 0 ) ) ).to.be.true;
  157. } );
  158. it( 'should revert changes done by deltas from given batch, if parameter was passed (test: revert set attribute)', () => {
  159. undo._execute( batch1 );
  160. // Remove attribute:
  161. /*
  162. [root]
  163. - [p]
  164. - b
  165. - a
  166. - r
  167. - o
  168. - f
  169. - o
  170. */
  171. expect( itemAt( root, 0 ).name ).to.equal( 'p' );
  172. expect( getText( root.getChild( 0 ) ) ).to.equal( 'bar' );
  173. expect( root.getChild( 1 ).data ).to.equal( 'ofo' );
  174. expect( itemAt( root.getChild( 0 ), 0 ).hasAttribute( 'key' ) ).to.be.false;
  175. expect( itemAt( root, 2 ).hasAttribute( 'key' ) ).to.be.false;
  176. expect( itemAt( root, 3 ).hasAttribute( 'key' ) ).to.be.false;
  177. // Selection is only partially restored because the range got broken.
  178. // The selection would have to best on letter "b" and letter "o", but it is set only on letter "b".
  179. expect( editor.document.selection.getFirstRange().isEqual( r( [ 0, 0 ], [ 0, 1 ] ) ) ).to.be.true;
  180. expect( editor.document.selection.isBackward ).to.be.true;
  181. } );
  182. it( 'should revert changes done by deltas from given batch, if parameter was passed (test: revert insert foobar)', () => {
  183. undo._execute( batch0 );
  184. // Remove foobar:
  185. /*
  186. [root]
  187. - [p]
  188. */
  189. // The `P` element wasn't removed because it wasn`t added by undone batch.
  190. // It would be perfect if the `P` got removed aswell because wrapping was on removed nodes.
  191. // But this would need a lot of logic / hardcoded ifs or a post-fixer.
  192. expect( root.childCount ).to.equal( 1 );
  193. expect( itemAt( root, 0 ).name ).to.equal( 'p' );
  194. expect( editor.document.selection.getFirstRange().isEqual( r( 0, 0 ) ) ).to.be.true;
  195. expect( editor.document.selection.isBackward ).to.be.false;
  196. undo._execute( batch1 );
  197. // Remove attributes.
  198. // This does nothing in the `root` because attributes were set on nodes that already got removed.
  199. // But those nodes should change in the graveyard and we can check them there.
  200. expect( root.childCount ).to.equal( 1 );
  201. expect( itemAt( root, 0 ).name ).to.equal( 'p' );
  202. // Operations for undoing that batch were working on graveyard so document selection should not change.
  203. expect( editor.document.selection.getFirstRange().isEqual( r( 0, 0 ) ) ).to.be.true;
  204. expect( editor.document.selection.isBackward ).to.be.false;
  205. expect( doc.graveyard.getChild( 0 ).maxOffset ).to.equal( 6 );
  206. for ( let char of doc.graveyard._children ) {
  207. expect( char.hasAttribute( 'key' ) ).to.be.false;
  208. }
  209. // Let's undo wrapping. This should leave us with empty root.
  210. undo._execute( batch3 );
  211. expect( root.maxOffset ).to.equal( 0 );
  212. // Once again transformed range ends up in the graveyard.
  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. // Some tests to ensure 100% CC and proper behavior in edge cases.
  218. describe( 'edge cases', () => {
  219. function getCaseText( root ) {
  220. let text = '';
  221. for ( let i = 0; i < root.childCount; i++ ) {
  222. let node = root.getChild( i );
  223. text += node.getAttribute( 'uppercase' ) ? node.data.toUpperCase() : node.data;
  224. }
  225. return text;
  226. }
  227. it( 'correctly handles deltas in compressed history that were earlier updated into multiple deltas (or split when undoing)', () => {
  228. // In this case we assume that one of the deltas in compressed history was updated to two deltas.
  229. // This is a tricky edge case because it is almost impossible to come up with convincing scenario that produces it.
  230. // At the moment of writing this test and comment, only Undo feature uses `CompressedHistory#updateDelta`.
  231. // Because deltas that "stays" in history are transformed with `isStrong` flag set to `false`, `MoveOperation`
  232. // won't get split and `AttributeDelta` can hold multiple `AttributeOperation` in it. So using most common deltas
  233. // (`InsertDelta`, `RemoveDelta`, `MoveDelta`, `AttributeDelta`) and undo it's impossible to get to this edge case.
  234. // Still there might be some weird scenarios connected with OT / Undo / Collaborative Editing / other deltas /
  235. // fancy 3rd party plugin where it may come up, so it's better to be safe than sorry.
  236. root.appendChildren( new Text( 'abcdef' ) );
  237. expect( getCaseText( root ) ).to.equal( 'abcdef' );
  238. editor.document.selection.setRanges( [ r( 1, 4 ) ] );
  239. let batch0 = doc.batch();
  240. undo.addBatch( batch0 );
  241. batch0.move( r( 1, 4 ), p( 5 ) );
  242. expect( getCaseText( root ) ).to.equal( 'aebcdf' );
  243. editor.document.selection.setRanges( [ r( 1, 1 ) ] );
  244. let batch1 = doc.batch();
  245. undo.addBatch( batch1 );
  246. batch1.remove( r( 0, 1 ) );
  247. expect( getCaseText( root ) ).to.equal( 'ebcdf' );
  248. editor.document.selection.setRanges( [ r( 0, 3 ) ] );
  249. let batch2 = doc.batch();
  250. undo.addBatch( batch2 );
  251. batch2.setAttribute( r( 0, 3 ), 'uppercase', true );
  252. expect( getCaseText( root ) ).to.equal( 'EBCdf' );
  253. undo._execute( batch0 );
  254. expect( getCaseText( root ) ).to.equal( 'BCdEf' );
  255. // Let's simulate splitting the delta by updating the history by hand.
  256. let attrHistoryDelta = doc.history.getDelta( 2 )[ 0 ];
  257. let attrDelta1 = new AttributeDelta();
  258. attrDelta1.addOperation( attrHistoryDelta.operations[ 0 ] );
  259. let attrDelta2 = new AttributeDelta();
  260. attrDelta2.addOperation( attrHistoryDelta.operations[ 1 ] );
  261. doc.history.updateDelta( 2, [ attrDelta1, attrDelta2 ] );
  262. undo._execute( batch1 );
  263. // After this execution, undo algorithm should update both `attrDelta1` and `attrDelta2` with new
  264. // versions, that have incremented offsets.
  265. expect( getCaseText( root ) ).to.equal( 'aBCdEf' );
  266. undo._execute( batch2 );
  267. // This execution checks whether undo algorithm correctly updated deltas in previous execution
  268. // and also whether it correctly "reads" both deltas from history.
  269. expect( getCaseText( root ) ).to.equal( 'abcdef' );
  270. } );
  271. it( 'merges touching ranges when restoring selection', () => {
  272. root.appendChildren( new Text( 'abcdef' ) );
  273. expect( getCaseText( root ) ).to.equal( 'abcdef' );
  274. editor.document.selection.setRanges( [ r( 1, 4 ) ] );
  275. let batch0 = doc.batch();
  276. undo.addBatch( batch0 );
  277. batch0.setAttribute( r( 1, 4 ), 'uppercase', true );
  278. expect( getCaseText( root ) ).to.equal( 'aBCDef' );
  279. editor.document.selection.setRanges( [ r( 3, 4 ) ] );
  280. let batch1 = doc.batch();
  281. undo.addBatch( batch1 );
  282. batch1.move( r( 3, 4 ), p( 1 ) );
  283. expect( getCaseText( root ) ).to.equal( 'aDBCef' );
  284. undo._execute( batch0 );
  285. // After undo-attr: acdbef <--- "cdb" should be selected, it would look weird if only "cd" or "b" is selected
  286. // but the whole unbroken part "cdb" changed attribute.
  287. expect( getCaseText( root ) ).to.equal( 'adbcef' );
  288. expect( editor.document.selection.getFirstRange().isEqual( r( 1, 4 ) ) ).to.be.true;
  289. } );
  290. it( 'does nothing (and not crashes) if delta to undo is no longer in history', () => {
  291. // Also an edgy situation but it may come up if other plugins use `CompressedHistory` API.
  292. root.appendChildren( new Text( 'abcdef' ) );
  293. expect( getCaseText( root ) ).to.equal( 'abcdef' );
  294. editor.document.selection.setRanges( [ r( 0, 1 ) ] );
  295. let batch0 = doc.batch();
  296. undo.addBatch( batch0 );
  297. batch0.setAttribute( r( 0, 1 ), 'uppercase', true );
  298. expect( getCaseText( root ) ).to.equal( 'Abcdef' );
  299. doc.history.removeDelta( 0 );
  300. root.getChild( 0 ).removeAttribute( 'uppercase' );
  301. expect( getCaseText( root ) ).to.equal( 'abcdef' );
  302. undo._execute();
  303. // Nothing happened. We are still alive.
  304. expect( getCaseText( root ) ).to.equal( 'abcdef' );
  305. } );
  306. } );
  307. } );
  308. } );