deletecommand.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 DeleteCommand from '../src/deletecommand';
  7. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  9. describe( 'DeleteCommand', () => {
  10. let editor, model, doc;
  11. testUtils.createSinonSandbox();
  12. beforeEach( () => {
  13. return ModelTestEditor.create()
  14. .then( newEditor => {
  15. editor = newEditor;
  16. model = editor.model;
  17. doc = model.document;
  18. const command = new DeleteCommand( editor, 'backward' );
  19. editor.commands.add( 'delete', command );
  20. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  21. model.schema.register( 'heading1', { inheritAllFrom: '$block' } );
  22. } );
  23. } );
  24. afterEach( () => {
  25. return editor.destroy();
  26. } );
  27. it( 'has direction', () => {
  28. const command = new DeleteCommand( editor, 'forward' );
  29. expect( command ).to.have.property( 'direction', 'forward' );
  30. } );
  31. describe( 'execute()', () => {
  32. it( 'uses enqueueChange', () => {
  33. setData( model, '<paragraph>foo[]bar</paragraph>' );
  34. model.enqueueChange( () => {
  35. editor.execute( 'delete' );
  36. // We expect that command is executed in enqueue changes block. Since we are already in
  37. // an enqueued block, the command execution will be postponed. Hence, no changes.
  38. expect( getData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  39. } );
  40. // After all enqueued changes are done, the command execution is reflected.
  41. expect( getData( model ) ).to.equal( '<paragraph>fo[]bar</paragraph>' );
  42. } );
  43. it( 'locks buffer when executing', () => {
  44. setData( model, '<paragraph>foo[]bar</paragraph>' );
  45. const buffer = editor.commands.get( 'delete' )._buffer;
  46. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  47. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  48. editor.execute( 'delete' );
  49. expect( lockSpy.calledOnce ).to.be.true;
  50. expect( unlockSpy.calledOnce ).to.be.true;
  51. } );
  52. it( 'deletes previous character when selection is collapsed', () => {
  53. setData( model, '<paragraph>foo[]bar</paragraph>' );
  54. editor.execute( 'delete' );
  55. expect( getData( model ) ).to.equal( '<paragraph>fo[]bar</paragraph>' );
  56. } );
  57. it( 'deletes selection contents', () => {
  58. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  59. editor.execute( 'delete' );
  60. expect( getData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  61. } );
  62. it( 'merges elements', () => {
  63. setData( model, '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>' );
  64. editor.execute( 'delete' );
  65. expect( getData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  66. } );
  67. it( 'does not try to delete when selection is at the boundary', () => {
  68. const spy = sinon.spy();
  69. editor.model.on( 'deleteContent', spy );
  70. setData( model, '<paragraph>[]foo</paragraph>' );
  71. editor.execute( 'delete' );
  72. expect( getData( model ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  73. expect( spy.callCount ).to.equal( 0 );
  74. } );
  75. it( 'passes options to modifySelection', () => {
  76. const spy = sinon.spy();
  77. editor.model.on( 'modifySelection', spy );
  78. setData( model, '<paragraph>foo[]bar</paragraph>' );
  79. editor.commands.get( 'delete' ).direction = 'forward';
  80. editor.execute( 'delete', { unit: 'word' } );
  81. expect( spy.callCount ).to.equal( 1 );
  82. const modifyOpts = spy.args[ 0 ][ 1 ][ 1 ];
  83. expect( modifyOpts ).to.have.property( 'direction', 'forward' );
  84. expect( modifyOpts ).to.have.property( 'unit', 'word' );
  85. } );
  86. it( 'passes options to deleteContent #1', () => {
  87. const spy = sinon.spy();
  88. editor.model.on( 'deleteContent', spy );
  89. setData( model, '<paragraph>foo[]bar</paragraph>' );
  90. editor.execute( 'delete' );
  91. expect( spy.callCount ).to.equal( 1 );
  92. const deleteOpts = spy.args[ 0 ][ 1 ][ 1 ];
  93. expect( deleteOpts ).to.have.property( 'doNotResetEntireContent', true );
  94. } );
  95. it( 'passes options to deleteContent #2', () => {
  96. const spy = sinon.spy();
  97. editor.model.on( 'deleteContent', spy );
  98. setData( model, '<paragraph>[foobar]</paragraph>' );
  99. editor.execute( 'delete' );
  100. expect( spy.callCount ).to.equal( 1 );
  101. const deleteOpts = spy.args[ 0 ][ 1 ][ 1 ];
  102. expect( deleteOpts ).to.have.property( 'doNotResetEntireContent', false );
  103. } );
  104. it( 'leaves an empty paragraph after removing the whole content from editor', () => {
  105. setData( model, '<heading1>[Header 1</heading1><paragraph>Some text.]</paragraph>' );
  106. editor.execute( 'delete' );
  107. expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  108. } );
  109. it( 'leaves an empty paragraph after removing the whole content inside limit element', () => {
  110. model.schema.register( 'section', {
  111. inheritAllFrom: '$root',
  112. allowIn: '$root',
  113. isLimit: true
  114. } );
  115. setData( model,
  116. '<heading1>Foo</heading1>' +
  117. '<section>' +
  118. '<heading1>[Header 1</heading1>' +
  119. '<paragraph>Some text.]</paragraph>' +
  120. '</section>' +
  121. '<paragraph>Bar.</paragraph>'
  122. );
  123. editor.execute( 'delete' );
  124. expect( getData( model ) ).to.equal(
  125. '<heading1>Foo</heading1>' +
  126. '<section>' +
  127. '<paragraph>[]</paragraph>' +
  128. '</section>' +
  129. '<paragraph>Bar.</paragraph>'
  130. );
  131. } );
  132. it( 'leaves an empty paragraph after removing another paragraph from block element', () => {
  133. model.schema.register( 'section', {
  134. inheritAllFrom: '$block',
  135. isLimit: true
  136. } );
  137. model.schema.register( 'blockQuote', { inheritAllFrom: '$block' } );
  138. model.schema.extend( 'section', { allowIn: '$root' } );
  139. model.schema.extend( 'paragraph', { allowIn: 'section' } );
  140. model.schema.extend( 'blockQuote', { allowIn: 'section' } );
  141. model.schema.extend( 'paragraph', { allowIn: 'blockQuote' } );
  142. setData( model, '<section><blockQuote><paragraph>[]</paragraph></blockQuote></section>' );
  143. editor.execute( 'delete' );
  144. expect( getData( model ) ).to.equal( '<section><paragraph>[]</paragraph></section>' );
  145. } );
  146. it( 'leaves an empty paragraph after removing the whole content when root element was not added as Schema limit', () => {
  147. model.schema.extend( '$root', {
  148. isLimit: false
  149. } );
  150. expect( model.schema.isLimit( '$root' ) ).to.be.false;
  151. setData( model, '<heading1>[]</heading1>' );
  152. editor.execute( 'delete' );
  153. expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  154. } );
  155. it( 'replaces an empty element with paragraph', () => {
  156. setData( model, '<heading1>[]</heading1>' );
  157. editor.execute( 'delete' );
  158. expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  159. } );
  160. it( 'does not replace an element when Backspace or Delete key is held', () => {
  161. setData( model, '<heading1>Bar[]</heading1>' );
  162. for ( let sequence = 1; sequence < 10; ++sequence ) {
  163. editor.execute( 'delete', { sequence } );
  164. }
  165. expect( getData( model ) ).to.equal( '<heading1>[]</heading1>' );
  166. } );
  167. it( 'does not replace with paragraph in another paragraph already occurs in limit element', () => {
  168. setData( model, '<paragraph>[]</paragraph>' );
  169. const element = doc.getRoot().getNodeByPath( [ 0 ] );
  170. editor.execute( 'delete' );
  171. expect( element ).is.equal( doc.getRoot().getNodeByPath( [ 0 ] ) );
  172. } );
  173. it( 'does not replace an element if a paragraph is not allowed in current position', () => {
  174. model.schema.on( 'checkChild', ( evt, args ) => {
  175. const def = model.schema.getDefinition( args[ 1 ] );
  176. if ( args[ 0 ].endsWith( '$root' ) && def.name == 'paragraph' ) {
  177. evt.stop();
  178. evt.return = false;
  179. }
  180. } );
  181. setData( model, '<heading1>[]</heading1>' );
  182. editor.execute( 'delete' );
  183. expect( getData( model ) ).to.equal( '<heading1>[]</heading1>' );
  184. } );
  185. } );
  186. } );