8
0

deletecommand.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. const spy = testUtils.sinon.spy( doc, 'enqueueChanges' );
  34. editor.execute( 'delete' );
  35. expect( spy.calledOnce ).to.be.true;
  36. } );
  37. it( 'locks buffer when executing', () => {
  38. setData( doc, '<paragraph>foo[]bar</paragraph>' );
  39. const buffer = editor.commands.get( 'delete' )._buffer;
  40. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  41. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  42. editor.execute( 'delete' );
  43. expect( lockSpy.calledOnce ).to.be.true;
  44. expect( unlockSpy.calledOnce ).to.be.true;
  45. } );
  46. it( 'deletes previous character when selection is collapsed', () => {
  47. setData( doc, '<paragraph>foo[]bar</paragraph>' );
  48. editor.execute( 'delete' );
  49. expect( getData( doc, { selection: true } ) ).to.equal( '<paragraph>fo[]bar</paragraph>' );
  50. } );
  51. it( 'deletes selection contents', () => {
  52. setData( doc, '<paragraph>fo[ob]ar</paragraph>' );
  53. editor.execute( 'delete' );
  54. expect( getData( doc, { selection: true } ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  55. } );
  56. it( 'merges elements', () => {
  57. setData( doc, '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>' );
  58. editor.execute( 'delete' );
  59. expect( getData( doc, { selection: true } ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  60. } );
  61. it( 'does not try to delete when selection is at the boundary', () => {
  62. const spy = sinon.spy();
  63. editor.data.on( 'deleteContent', spy );
  64. setData( doc, '<paragraph>[]foo</paragraph>' );
  65. editor.execute( 'delete' );
  66. expect( getData( doc, { selection: true } ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  67. expect( spy.callCount ).to.equal( 0 );
  68. } );
  69. it( 'passes options to modifySelection', () => {
  70. const spy = sinon.spy();
  71. editor.data.on( 'modifySelection', spy );
  72. setData( doc, '<paragraph>foo[]bar</paragraph>' );
  73. editor.commands.get( 'delete' ).direction = 'forward';
  74. editor.execute( 'delete', { unit: 'word' } );
  75. expect( spy.callCount ).to.equal( 1 );
  76. const modifyOpts = spy.args[ 0 ][ 1 ][ 1 ];
  77. expect( modifyOpts ).to.have.property( 'direction', 'forward' );
  78. expect( modifyOpts ).to.have.property( 'unit', 'word' );
  79. } );
  80. it( 'passes options to deleteContent #1', () => {
  81. const spy = sinon.spy();
  82. editor.data.on( 'deleteContent', spy );
  83. setData( doc, '<paragraph>foo[]bar</paragraph>' );
  84. editor.execute( 'delete' );
  85. expect( spy.callCount ).to.equal( 1 );
  86. const deleteOpts = spy.args[ 0 ][ 1 ][ 2 ];
  87. expect( deleteOpts ).to.have.property( 'doNotResetEntireContent', true );
  88. } );
  89. it( 'passes options to deleteContent #2', () => {
  90. const spy = sinon.spy();
  91. editor.data.on( 'deleteContent', spy );
  92. setData( doc, '<paragraph>[foobar]</paragraph>' );
  93. editor.execute( 'delete' );
  94. expect( spy.callCount ).to.equal( 1 );
  95. const deleteOpts = spy.args[ 0 ][ 1 ][ 2 ];
  96. expect( deleteOpts ).to.have.property( 'doNotResetEntireContent', false );
  97. } );
  98. it( 'leaves an empty paragraph after removing the whole content from editor', () => {
  99. setData( doc, '<heading1>[Header 1</heading1><paragraph>Some text.]</paragraph>' );
  100. editor.execute( 'delete' );
  101. expect( getData( doc, { selection: true } ) ).to.equal( '<paragraph>[]</paragraph>' );
  102. } );
  103. it( 'leaves an empty paragraph after removing the whole content inside limit element', () => {
  104. doc.schema.registerItem( 'section', '$root' );
  105. doc.schema.limits.add( 'section' );
  106. doc.schema.allow( { name: 'section', inside: '$root' } );
  107. setData( doc,
  108. '<heading1>Foo</heading1>' +
  109. '<section>' +
  110. '<heading1>[Header 1</heading1>' +
  111. '<paragraph>Some text.]</paragraph>' +
  112. '</section>' +
  113. '<paragraph>Bar.</paragraph>'
  114. );
  115. editor.execute( 'delete' );
  116. expect( getData( doc, { selection: true } ) ).to.equal(
  117. '<heading1>Foo</heading1>' +
  118. '<section>' +
  119. '<paragraph>[]</paragraph>' +
  120. '</section>' +
  121. '<paragraph>Bar.</paragraph>'
  122. );
  123. } );
  124. it( 'leaves an empty paragraph after removing the whole content when root element was not added as Schema.limits', () => {
  125. doc.schema.limits.delete( '$root' );
  126. setData( doc, '<heading1>[]</heading1>' );
  127. editor.execute( 'delete' );
  128. expect( getData( doc ) ).to.equal( '<paragraph>[]</paragraph>' );
  129. } );
  130. it( 'replaces an empty element with paragraph', () => {
  131. setData( doc, '<heading1>[]</heading1>' );
  132. editor.execute( 'delete' );
  133. expect( getData( doc, { selection: true } ) ).to.equal( '<paragraph>[]</paragraph>' );
  134. } );
  135. it( 'does not replace an element when Backspace or Delete key is held', () => {
  136. setData( doc, '<heading1>Bar[]</heading1>' );
  137. for ( let sequence = 1; sequence < 10; ++sequence ) {
  138. editor.execute( 'delete', { sequence } );
  139. }
  140. expect( getData( doc, { selection: true } ) ).to.equal( '<heading1>[]</heading1>' );
  141. } );
  142. it( 'does not replace an element if a paragraph is a common ancestor', () => {
  143. setData( doc, '<paragraph>[]</paragraph>' );
  144. const element = doc.selection.getFirstRange().getCommonAncestor();
  145. editor.execute( 'delete' );
  146. expect( element ).is.equal( doc.selection.getFirstRange().getCommonAncestor() );
  147. } );
  148. it( 'does not replace an element if a paragraph is not allowed in current position', () => {
  149. doc.schema.disallow( { name: 'paragraph', inside: '$root' } );
  150. setData( doc, '<heading1>[]</heading1>' );
  151. editor.execute( 'delete' );
  152. // Returned data: '[]' instead of the heading element.
  153. expect( getData( doc, { selection: true } ) ).to.equal( '<heading1>[]</heading1>' );
  154. } );
  155. } );
  156. } );