Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Do not check if range/text is not set.
Browse files Browse the repository at this point in the history
  • Loading branch information
f1ames committed Mar 2, 2017
1 parent 7ce5362 commit 5690acf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
38 changes: 18 additions & 20 deletions src/inputcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default class InputCommand extends Command {
* on the beginning of the range (which after removal is a collapsed range).
*
* @param {Object} [options] The command options.
* @param {String} [options.text] Text to be inserted.
* @param {String} options.text Text to be inserted.
* @param {module:engine/model/range~Range} [options.range] Range in which the text is inserted. Defaults
* to the first range in the current selection.
* @param {module:engine/model/position~Position} [options.resultPosition] Position at which the selection
Expand All @@ -76,28 +76,26 @@ export default class InputCommand extends Command {

let textInsertions = 0;

if ( range && text !== undefined ) {
doc.enqueueChanges( () => {
const isCollapsedRange = range.isCollapsed;
doc.enqueueChanges( () => {
const isCollapsedRange = range.isCollapsed;

if ( !isCollapsedRange ) {
this._buffer.batch.remove( range );
}
if ( !isCollapsedRange ) {
this._buffer.batch.remove( range );
}

if ( text && text.length ) {
textInsertions = text.length;
this._buffer.batch.weakInsert( range.start, text );
}
if ( text && text.length ) {
textInsertions = text.length;
this._buffer.batch.weakInsert( range.start, text );
}

if ( resultPosition ) {
this.editor.data.model.selection.collapse( resultPosition );
} else if ( isCollapsedRange ) {
// If range was collapsed just shift the selection by the number of inserted characters.
this.editor.data.model.selection.collapse( range.start.getShiftedBy( textInsertions ) );
}
if ( resultPosition ) {
this.editor.data.model.selection.collapse( resultPosition );
} else if ( isCollapsedRange ) {
// If range was collapsed just shift the selection by the number of inserted characters.
this.editor.data.model.selection.collapse( range.start.getShiftedBy( textInsertions ) );
}

this._buffer.input( textInsertions );
} );
}
this._buffer.input( textInsertions );
} );
}
}
19 changes: 9 additions & 10 deletions tests/inputcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,28 +154,27 @@ describe( 'InputCommand', () => {
expect( buffer.size ).to.be.equal( 0 );
} );

it( 'does nothing when there is no range', () => {
it( 'only removes content when no text given (with default non-collapsed range)', () => {
setData( doc, '<p>[fo]obar</p>' );

testUtils.sinon.stub( editor.document.selection, 'getFirstRange' ).returns( null );
const spy = testUtils.sinon.spy( doc, 'enqueueChanges' );

editor.execute( 'input', {
text: 'baz'
} );
editor.execute( 'input' );

expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>[fo]obar</p>' );
expect( spy.callCount ).to.be.equal( 1 );
expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>[]obar</p>' );
expect( buffer.size ).to.be.equal( 0 );
} );

it( 'does nothing when there is no options object provided', () => {
setData( doc, '<p>[fo]obar</p>' );
it( 'does not change selection and content when no text given (with default collapsed range)', () => {
setData( doc, '<p>fo[]obar</p>' );

const spy = testUtils.sinon.spy( doc, 'enqueueChanges' );

editor.execute( 'input' );

expect( spy.callCount ).to.be.equal( 0 );
expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>[fo]obar</p>' );
expect( spy.callCount ).to.be.equal( 1 );
expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>fo[]obar</p>' );
expect( buffer.size ).to.be.equal( 0 );
} );
} );
Expand Down

0 comments on commit 5690acf

Please sign in to comment.