deletecommand.js 7.4 KB

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