undocommand.js 12 KB

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