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

Commit

Permalink
Merge pull request #56 from ckeditor/t/ckeditor5/1120
Browse files Browse the repository at this point in the history
Fix: Default action of `Enter` event should be always prevented. Closes ckeditor/ckeditor5#1120.
  • Loading branch information
Reinmar authored Sep 4, 2018
2 parents facc882 + 4ae6a99 commit 8d7c75f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/enter.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ export default class Enter extends Plugin {
editor.commands.add( 'enter', new EnterCommand( editor ) );

this.listenTo( viewDocument, 'enter', ( evt, data ) => {
data.preventDefault();

// The soft enter key is handled by the ShiftEnter plugin.
if ( data.isSoft ) {
return;
}

editor.execute( 'enter' );
data.preventDefault();
view.scrollToTheSelection();
}, { priority: 'low' } );
}
Expand Down
3 changes: 2 additions & 1 deletion src/shiftenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ export default class ShiftEnter extends Plugin {
editor.commands.add( 'shiftEnter', new ShiftEnterCommand( editor ) );

this.listenTo( viewDocument, 'enter', ( evt, data ) => {
data.preventDefault();

// The hard enter key is handled by the Enter plugin.
if ( !data.isSoft ) {
return;
}

editor.execute( 'shiftEnter' );
data.preventDefault();
view.scrollToTheSelection();
}, { priority: 'low' } );
}
Expand Down
10 changes: 9 additions & 1 deletion tests/enter.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe( 'Enter feature', () => {
viewDocument.fire( 'enter', new DomEventData( viewDocument, domEvt ) );

sinon.assert.calledOnce( scrollSpy );
sinon.assert.callOrder( executeSpy, scrollSpy );
sinon.assert.callOrder( domEvt.preventDefault, executeSpy, scrollSpy );
} );

it( 'does not execute the command if soft enter should be used', () => {
Expand All @@ -65,6 +65,14 @@ describe( 'Enter feature', () => {
sinon.assert.notCalled( commandExecuteSpy );
} );

it( 'prevents default event action even if the command should not be executed', () => {
const domEvt = getDomEvent();

viewDocument.fire( 'enter', new DomEventData( viewDocument, domEvt, { isSoft: true } ) );

sinon.assert.calledOnce( domEvt.preventDefault );
} );

function getDomEvent() {
return {
preventDefault: sinon.spy()
Expand Down
10 changes: 9 additions & 1 deletion tests/shiftenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe( 'ShiftEnter feature', () => {
viewDocument.fire( 'enter', new DomEventData( viewDocument, domEvt, { isSoft: true } ) );

sinon.assert.calledOnce( scrollSpy );
sinon.assert.callOrder( executeSpy, scrollSpy );
sinon.assert.callOrder( domEvt.preventDefault, executeSpy, scrollSpy );
} );

it( 'does not execute the command if hard enter should be used', () => {
Expand All @@ -65,6 +65,14 @@ describe( 'ShiftEnter feature', () => {
sinon.assert.notCalled( commandExecuteSpy );
} );

it( 'prevents default event action even if the command should not be executed', () => {
const domEvt = getDomEvent();

viewDocument.fire( 'enter', new DomEventData( viewDocument, domEvt, { isSoft: false } ) );

sinon.assert.calledOnce( domEvt.preventDefault );
} );

function getDomEvent() {
return {
preventDefault: sinon.spy()
Expand Down

0 comments on commit 8d7c75f

Please sign in to comment.