8
0

deletecommand.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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( 'merges elements', () => {
  86. setData( model, '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>' );
  87. editor.execute( 'delete' );
  88. expect( getData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  89. } );
  90. it( 'does not try to delete when selection is at the boundary', () => {
  91. const spy = sinon.spy();
  92. editor.model.on( 'deleteContent', spy );
  93. setData( model, '<paragraph>[]foo</paragraph>' );
  94. editor.execute( 'delete' );
  95. expect( getData( model ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  96. expect( spy.callCount ).to.equal( 0 );
  97. } );
  98. it( 'passes options to modifySelection', () => {
  99. const spy = sinon.spy();
  100. editor.model.on( 'modifySelection', spy );
  101. setData( model, '<paragraph>foo[]bar</paragraph>' );
  102. editor.commands.get( 'delete' ).direction = 'forward';
  103. editor.execute( 'delete', { unit: 'word' } );
  104. expect( spy.callCount ).to.equal( 1 );
  105. const modifyOpts = spy.args[ 0 ][ 1 ][ 1 ];
  106. expect( modifyOpts ).to.have.property( 'direction', 'forward' );
  107. expect( modifyOpts ).to.have.property( 'unit', 'word' );
  108. } );
  109. it( 'passes options to deleteContent #1', () => {
  110. const spy = sinon.spy();
  111. editor.model.on( 'deleteContent', spy );
  112. setData( model, '<paragraph>foo[]bar</paragraph>' );
  113. editor.execute( 'delete' );
  114. expect( spy.callCount ).to.equal( 1 );
  115. const deleteOpts = spy.args[ 0 ][ 1 ][ 1 ];
  116. expect( deleteOpts ).to.have.property( 'doNotResetEntireContent', true );
  117. } );
  118. it( 'passes options to deleteContent #2', () => {
  119. const spy = sinon.spy();
  120. editor.model.on( 'deleteContent', spy );
  121. setData( model, '<paragraph>[foobar]</paragraph>' );
  122. editor.execute( 'delete' );
  123. expect( spy.callCount ).to.equal( 1 );
  124. const deleteOpts = spy.args[ 0 ][ 1 ][ 1 ];
  125. expect( deleteOpts ).to.have.property( 'doNotResetEntireContent', false );
  126. } );
  127. it( 'leaves an empty paragraph after removing the whole content from editor', () => {
  128. setData( model, '<heading1>[Header 1</heading1><paragraph>Some text.]</paragraph>' );
  129. editor.execute( 'delete' );
  130. expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  131. } );
  132. it( 'leaves an empty paragraph after removing the whole content inside limit element', () => {
  133. model.schema.register( 'section', {
  134. inheritAllFrom: '$root',
  135. allowIn: '$root',
  136. isLimit: true
  137. } );
  138. setData( model,
  139. '<heading1>Foo</heading1>' +
  140. '<section>' +
  141. '<heading1>[Header 1</heading1>' +
  142. '<paragraph>Some text.]</paragraph>' +
  143. '</section>' +
  144. '<paragraph>Bar.</paragraph>'
  145. );
  146. editor.execute( 'delete' );
  147. expect( getData( model ) ).to.equal(
  148. '<heading1>Foo</heading1>' +
  149. '<section>' +
  150. '<paragraph>[]</paragraph>' +
  151. '</section>' +
  152. '<paragraph>Bar.</paragraph>'
  153. );
  154. } );
  155. it( 'leaves an empty paragraph after removing another paragraph from block element', () => {
  156. model.schema.register( 'section', {
  157. inheritAllFrom: '$block',
  158. isLimit: true
  159. } );
  160. model.schema.register( 'blockQuote', { inheritAllFrom: '$block' } );
  161. model.schema.extend( 'section', { allowIn: '$root' } );
  162. model.schema.extend( 'paragraph', { allowIn: 'section' } );
  163. model.schema.extend( 'blockQuote', { allowIn: 'section' } );
  164. model.schema.extend( 'paragraph', { allowIn: 'blockQuote' } );
  165. setData( model, '<section><blockQuote><paragraph>[]</paragraph></blockQuote></section>' );
  166. editor.execute( 'delete' );
  167. expect( getData( model ) ).to.equal( '<section><paragraph>[]</paragraph></section>' );
  168. } );
  169. it( 'leaves an empty paragraph after removing the whole content when root element was not added as Schema limit', () => {
  170. model.schema.extend( '$root', {
  171. isLimit: false
  172. } );
  173. expect( model.schema.isLimit( '$root' ) ).to.be.false;
  174. setData( model, '<heading1>[]</heading1>' );
  175. editor.execute( 'delete' );
  176. expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  177. } );
  178. it( 'replaces an empty element with paragraph', () => {
  179. setData( model, '<heading1>[]</heading1>' );
  180. editor.execute( 'delete' );
  181. expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  182. } );
  183. it( 'does not replace an element when Backspace or Delete key is held', () => {
  184. setData( model, '<heading1>Bar[]</heading1>' );
  185. for ( let sequence = 1; sequence < 10; ++sequence ) {
  186. editor.execute( 'delete', { sequence } );
  187. }
  188. expect( getData( model ) ).to.equal( '<heading1>[]</heading1>' );
  189. } );
  190. it( 'does not replace with paragraph in another paragraph already occurs in limit element', () => {
  191. setData( model, '<paragraph>[]</paragraph>' );
  192. const element = doc.getRoot().getNodeByPath( [ 0 ] );
  193. editor.execute( 'delete' );
  194. expect( element ).is.equal( doc.getRoot().getNodeByPath( [ 0 ] ) );
  195. } );
  196. it( 'does not replace an element if a paragraph is not allowed in current position', () => {
  197. model.schema.addChildCheck( ( ctx, childDef ) => {
  198. if ( ctx.endsWith( '$root' ) && childDef.name == 'paragraph' ) {
  199. return false;
  200. }
  201. } );
  202. setData( model, '<heading1>[]</heading1>' );
  203. editor.execute( 'delete' );
  204. expect( getData( model ) ).to.equal( '<heading1>[]</heading1>' );
  205. } );
  206. } );
  207. } );