deletecommand.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from 'tests/core/_utils/modeltesteditor.js';
  6. import DeleteCommand from 'ckeditor5/typing/deletecommand.js';
  7. import { getData, setData } from 'ckeditor5/engine/dev-utils/model.js';
  8. describe( 'DeleteCommand', () => {
  9. let editor, doc;
  10. beforeEach( () => {
  11. return ModelTestEditor.create( )
  12. .then( newEditor => {
  13. editor = newEditor;
  14. doc = editor.document;
  15. const command = new DeleteCommand( editor, 'backward' );
  16. editor.commands.set( 'delete', command );
  17. doc.schema.registerItem( 'p', '$block' );
  18. } );
  19. } );
  20. it( 'has direction', () => {
  21. const command = new DeleteCommand( editor, 'forward' );
  22. expect( command ).to.have.property( 'direction', 'forward' );
  23. } );
  24. describe( 'execute', () => {
  25. it( 'uses enqueueChanges', () => {
  26. setData( doc, '<p>foo[]bar</p>' );
  27. const spy = sinon.spy( doc, 'enqueueChanges' );
  28. editor.execute( 'delete' );
  29. expect( spy.calledOnce ).to.be.true;
  30. } );
  31. it( 'deletes previous character when selection is collapsed', () => {
  32. setData( doc, '<p>foo[]bar</p>' );
  33. editor.execute( 'delete' );
  34. expect( getData( doc, { selection: true } ) ).to.equal( '<p>fo[]bar</p>' );
  35. } );
  36. it( 'deletes selection contents', () => {
  37. setData( doc, '<p>fo[ob]ar</p>' );
  38. editor.execute( 'delete' );
  39. expect( getData( doc, { selection: true } ) ).to.equal( '<p>fo[]ar</p>' );
  40. } );
  41. it( 'merges elements', () => {
  42. setData( doc, '<p>foo</p><p>[]bar</p>' );
  43. editor.execute( 'delete' );
  44. expect( getData( doc, { selection: true } ) ).to.equal( '<p>foo[]bar</p>' );
  45. } );
  46. it( 'does not try to delete when selection is at the boundary', () => {
  47. const spy = sinon.spy();
  48. doc.composer.on( 'deleteContents', spy );
  49. setData( doc, '<p>[]foo</p>' );
  50. editor.execute( 'delete' );
  51. expect( getData( doc, { selection: true } ) ).to.equal( '<p>[]foo</p>' );
  52. expect( spy.callCount ).to.equal( 0 );
  53. } );
  54. it( 'passes options to modifySelection', () => {
  55. const spy = sinon.spy();
  56. doc.composer.on( 'modifySelection', spy );
  57. setData( doc, '<p>foo[]bar</p>' );
  58. editor.commands.get( 'delete' ).direction = 'forward';
  59. editor.execute( 'delete', { unit: 'word' } );
  60. expect( spy.callCount ).to.equal( 1 );
  61. const modifyOpts = spy.args[ 0 ][ 1 ].options;
  62. expect( modifyOpts ).to.have.property( 'direction', 'forward' );
  63. expect( modifyOpts ).to.have.property( 'unit', 'word' );
  64. } );
  65. } );
  66. } );