Skip to content

Commit

Permalink
Fix: Typing on mobile device will not throw after each typed character.
Browse files Browse the repository at this point in the history
  • Loading branch information
scofalik committed Aug 21, 2019
1 parent 3a38fc7 commit 1c3aba2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ export default class Delete extends Plugin {
}, { priority: 'lowest' } );

this.listenTo( viewDocument, 'keyup', ( evt, data ) => {
const domSelection = data.domTarget.ownerDocument.defaultView.getSelection();
if ( domSelectionAfterDeletion ) {
const domSelection = data.domTarget.ownerDocument.defaultView.getSelection();

domSelection.collapse( domSelectionAfterDeletion.anchorNode, domSelectionAfterDeletion.anchorOffset );
domSelection.extend( domSelectionAfterDeletion.focusNode, domSelectionAfterDeletion.focusOffset );
domSelection.collapse( domSelectionAfterDeletion.anchorNode, domSelectionAfterDeletion.anchorOffset );
domSelection.extend( domSelectionAfterDeletion.focusNode, domSelectionAfterDeletion.focusOffset );

domSelectionAfterDeletion = null;
domSelectionAfterDeletion = null;
}
} );
}
}
Expand Down
27 changes: 26 additions & 1 deletion tests/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import DomEventData from '@ckeditor/ckeditor5-engine/src/view/observer/domeventdata';
import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
import env from '@ckeditor/ckeditor5-utils/src/env';
import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';

/* globals window, document */

Expand Down Expand Up @@ -146,7 +147,7 @@ describe( 'Delete feature - Android', () => {
env.isAndroid = oldEnvIsAndroid;
} );

it( 'should re-set selection on keyup event if it was changed after deletion but before the input was fired (Android)', () => {
it( 'should re-set selection on keyup event if it was changed after deletion but before the input was fired', () => {
// This test covers a quirk on Android. We will recreate what browser does in this scenario.
// The test is not perfect because there are difficulties converting model selection to DOM in unit tests.
const view = editor.editing.view;
Expand Down Expand Up @@ -192,4 +193,28 @@ describe( 'Delete feature - Android', () => {
expect( domSelection.focusNode ).to.equal( focusNodeBefore );
expect( domSelection.focusOffset ).to.equal( focusOffsetBefore );
} );

it( 'should not crash on keyup event if it was not changed after typing', () => {
// This test covers a quirk on Android. We will recreate what browser does in this scenario.
const view = editor.editing.view;
const viewDocument = view.document;

const domEvt = {
preventDefault: sinon.spy()
};

const domRoot = view.getDomRoot();
const domEvent = {
preventDefault: sinon.spy()
};

viewDocument.fire( 'input', domEvent );
viewDocument.fire( 'keydown', new DomEventData( viewDocument, domEvent, { keyCode: getCode( 'A' ) } ) );

expect( () => {
viewDocument.fire( 'keyup', new DomEventData( viewDocument, domEvt, {
domTarget: domRoot
} ) );
} ).not.to.throw();
} );
} );

0 comments on commit 1c3aba2

Please sign in to comment.