8
0

deletecommand.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  7. import DeleteCommand from '../src/deletecommand';
  8. import Delete from '../src/delete';
  9. import ChangeBuffer from '../src/utils/changebuffer';
  10. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  12. describe( 'DeleteCommand', () => {
  13. let editor, model, doc;
  14. testUtils.createSinonSandbox();
  15. beforeEach( () => {
  16. return ModelTestEditor.create()
  17. .then( newEditor => {
  18. editor = newEditor;
  19. model = editor.model;
  20. doc = model.document;
  21. const command = new DeleteCommand( editor, 'backward' );
  22. editor.commands.add( 'delete', command );
  23. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  24. model.schema.register( 'heading1', { inheritAllFrom: '$block' } );
  25. } );
  26. } );
  27. afterEach( () => {
  28. return editor.destroy();
  29. } );
  30. it( 'has direction', () => {
  31. const command = new DeleteCommand( editor, 'forward' );
  32. expect( command ).to.have.property( 'direction', 'forward' );
  33. } );
  34. describe( 'buffer', () => {
  35. it( 'has buffer getter', () => {
  36. expect( editor.commands.get( 'delete' ).buffer ).to.be.an.instanceof( ChangeBuffer );
  37. } );
  38. it( 'has a buffer limit configured to default value of 20', () => {
  39. expect( editor.commands.get( 'delete' ).buffer ).to.have.property( 'limit', 20 );
  40. } );
  41. it( 'has a buffer configured to config.typing.undoStep', () => {
  42. return VirtualTestEditor
  43. .create( {
  44. plugins: [ Delete ],
  45. typing: {
  46. undoStep: 5
  47. }
  48. } )
  49. .then( editor => {
  50. expect( editor.commands.get( 'delete' ).buffer ).to.have.property( 'limit', 5 );
  51. } );
  52. } );
  53. } );
  54. describe( 'execute()', () => {
  55. it( 'uses enqueueChange', () => {
  56. setData( model, '<paragraph>foo[]bar</paragraph>' );
  57. model.enqueueChange( () => {
  58. editor.execute( 'delete' );
  59. // We expect that command is executed in enqueue changes block. Since we are already in
  60. // an enqueued block, the command execution will be postponed. Hence, no changes.
  61. expect( getData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  62. } );
  63. // After all enqueued changes are done, the command execution is reflected.
  64. expect( getData( model ) ).to.equal( '<paragraph>fo[]bar</paragraph>' );
  65. } );
  66. it( 'locks buffer when executing', () => {
  67. setData( model, '<paragraph>foo[]bar</paragraph>' );
  68. const buffer = editor.commands.get( 'delete' )._buffer;
  69. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  70. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  71. editor.execute( 'delete' );
  72. expect( lockSpy.calledOnce ).to.be.true;
  73. expect( unlockSpy.calledOnce ).to.be.true;
  74. } );
  75. it( 'deletes previous character when selection is collapsed', () => {
  76. setData( model, '<paragraph>foo[]bar</paragraph>' );
  77. editor.execute( 'delete' );
  78. expect( getData( model ) ).to.equal( '<paragraph>fo[]bar</paragraph>' );
  79. } );
  80. it( 'deletes selection contents', () => {
  81. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  82. editor.execute( 'delete' );
  83. expect( getData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  84. } );
  85. it( 'deletes contents of selection passed in options', () => {
  86. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  87. const selection = model.createSelection( model.createRangeIn( model.document.getRoot() ) );
  88. editor.execute( 'delete', { selection } );
  89. expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  90. } );
  91. it( 'merges elements', () => {
  92. setData( model, '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>' );
  93. editor.execute( 'delete' );
  94. expect( getData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  95. } );
  96. it( 'does not try to delete when selection is at the boundary', () => {
  97. const spy = sinon.spy();
  98. editor.model.on( 'deleteContent', spy );
  99. setData( model, '<paragraph>[]foo</paragraph>' );
  100. editor.execute( 'delete' );
  101. expect( getData( model ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  102. expect( spy.callCount ).to.equal( 0 );
  103. } );
  104. it( 'passes options to modifySelection', () => {
  105. const spy = sinon.spy();
  106. editor.model.on( 'modifySelection', spy );
  107. setData( model, '<paragraph>foo[]bar</paragraph>' );
  108. editor.commands.get( 'delete' ).direction = 'forward';
  109. editor.execute( 'delete', { unit: 'word' } );
  110. expect( spy.callCount ).to.equal( 1 );
  111. const modifyOpts = spy.args[ 0 ][ 1 ][ 1 ];
  112. expect( modifyOpts ).to.have.property( 'direction', 'forward' );
  113. expect( modifyOpts ).to.have.property( 'unit', 'word' );
  114. } );
  115. it( 'passes options to deleteContent #1', () => {
  116. const spy = sinon.spy();
  117. editor.model.on( 'deleteContent', spy );
  118. setData( model, '<paragraph>foo[]bar</paragraph>' );
  119. editor.execute( 'delete' );
  120. expect( spy.callCount ).to.equal( 1 );
  121. const deleteOpts = spy.args[ 0 ][ 1 ][ 1 ];
  122. expect( deleteOpts ).to.have.property( 'doNotResetEntireContent', true );
  123. } );
  124. it( 'passes options to deleteContent #2', () => {
  125. const spy = sinon.spy();
  126. editor.model.on( 'deleteContent', spy );
  127. setData( model, '<paragraph>[foobar]</paragraph>' );
  128. editor.execute( 'delete' );
  129. expect( spy.callCount ).to.equal( 1 );
  130. const deleteOpts = spy.args[ 0 ][ 1 ][ 1 ];
  131. expect( deleteOpts ).to.have.property( 'doNotResetEntireContent', false );
  132. } );
  133. it( 'should pass the "direction" option to Model#deleteContent method', () => {
  134. const spy = sinon.spy();
  135. const forwardCommand = new DeleteCommand( editor, 'forward' );
  136. editor.commands.add( 'forwardDelete', forwardCommand );
  137. editor.model.on( 'deleteContent', spy );
  138. setData( model, '<paragraph>foo[]bar</paragraph>' );
  139. editor.execute( 'delete' );
  140. expect( spy.callCount ).to.equal( 1 );
  141. let deleteOpts = spy.args[ 0 ][ 1 ][ 1 ];
  142. expect( deleteOpts ).to.have.property( 'direction', 'backward' );
  143. editor.execute( 'forwardDelete' );
  144. expect( spy.callCount ).to.equal( 2 );
  145. deleteOpts = spy.args[ 1 ][ 1 ][ 1 ];
  146. expect( deleteOpts ).to.have.property( 'direction', 'forward' );
  147. } );
  148. it( 'leaves an empty paragraph after removing the whole content from editor', () => {
  149. setData( model, '<heading1>[Header 1</heading1><paragraph>Some text.]</paragraph>' );
  150. editor.execute( 'delete' );
  151. expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  152. } );
  153. it( 'leaves an empty paragraph after removing the whole content inside limit element', () => {
  154. model.schema.register( 'section', {
  155. inheritAllFrom: '$root',
  156. allowIn: '$root',
  157. isLimit: true
  158. } );
  159. setData( model,
  160. '<heading1>Foo</heading1>' +
  161. '<section>' +
  162. '<heading1>[Header 1</heading1>' +
  163. '<paragraph>Some text.]</paragraph>' +
  164. '</section>' +
  165. '<paragraph>Bar.</paragraph>'
  166. );
  167. editor.execute( 'delete' );
  168. expect( getData( model ) ).to.equal(
  169. '<heading1>Foo</heading1>' +
  170. '<section>' +
  171. '<paragraph>[]</paragraph>' +
  172. '</section>' +
  173. '<paragraph>Bar.</paragraph>'
  174. );
  175. } );
  176. it( 'leaves an empty paragraph after removing another paragraph from block element', () => {
  177. model.schema.register( 'section', {
  178. inheritAllFrom: '$block',
  179. isLimit: true
  180. } );
  181. model.schema.register( 'blockQuote', { inheritAllFrom: '$block' } );
  182. model.schema.extend( 'section', { allowIn: '$root' } );
  183. model.schema.extend( 'paragraph', { allowIn: 'section' } );
  184. model.schema.extend( 'blockQuote', { allowIn: 'section' } );
  185. model.schema.extend( 'paragraph', { allowIn: 'blockQuote' } );
  186. setData( model, '<section><blockQuote><paragraph>[]</paragraph></blockQuote></section>' );
  187. editor.execute( 'delete' );
  188. expect( getData( model ) ).to.equal( '<section><paragraph>[]</paragraph></section>' );
  189. } );
  190. it( 'leaves an empty paragraph after removing the whole content when root element was not added as Schema limit', () => {
  191. model.schema.extend( '$root', {
  192. isLimit: false
  193. } );
  194. expect( model.schema.isLimit( '$root' ) ).to.be.false;
  195. setData( model, '<heading1>[]</heading1>' );
  196. editor.execute( 'delete' );
  197. expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  198. } );
  199. it( 'replaces an empty element with paragraph', () => {
  200. setData( model, '<heading1>[]</heading1>' );
  201. editor.execute( 'delete' );
  202. expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  203. } );
  204. it( 'does not replace an element when Backspace or Delete key is held', () => {
  205. setData( model, '<heading1>Bar[]</heading1>' );
  206. for ( let sequence = 1; sequence < 10; ++sequence ) {
  207. editor.execute( 'delete', { sequence } );
  208. }
  209. expect( getData( model ) ).to.equal( '<heading1>[]</heading1>' );
  210. } );
  211. it( 'does not replace with paragraph in another paragraph already occurs in limit element', () => {
  212. setData( model, '<paragraph>[]</paragraph>' );
  213. const element = doc.getRoot().getNodeByPath( [ 0 ] );
  214. editor.execute( 'delete' );
  215. expect( element ).is.equal( doc.getRoot().getNodeByPath( [ 0 ] ) );
  216. } );
  217. it( 'does not replace an element if a paragraph is not allowed in current position', () => {
  218. model.schema.addChildCheck( ( ctx, childDef ) => {
  219. if ( ctx.endsWith( '$root' ) && childDef.name == 'paragraph' ) {
  220. return false;
  221. }
  222. } );
  223. setData( model, '<heading1>[]</heading1>' );
  224. editor.execute( 'delete' );
  225. expect( getData( model ) ).to.equal( '<heading1>[]</heading1>' );
  226. } );
  227. } );
  228. } );