Skip to content

Commit

Permalink
Merge pull request #13874 from ckeditor/ck/13867
Browse files Browse the repository at this point in the history
Internal (engine): Composing in an empty paragraph should not throw an error. Closes #13867.
  • Loading branch information
scofalik authored Apr 12, 2023
2 parents 1845ec5 + 6fe6119 commit 64adabd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/ckeditor5-engine/src/controller/editingcontroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import {
CKEditorError,
ObservableMixin,
env,
type GetCallback
} from '@ckeditor/ckeditor5-utils';

Expand Down Expand Up @@ -125,7 +126,7 @@ export default class EditingController extends ObservableMixin() {

// Fix `beforeinput` target ranges so that they map to the valid model ranges.
this.listenTo<ViewDocumentInputEvent>( this.view.document, 'beforeinput',
fixTargetRanges( this.mapper, this.model.schema ),
fixTargetRanges( this.mapper, this.model.schema, this.view ),
{ priority: 'high' }
);

Expand Down Expand Up @@ -252,8 +253,14 @@ export default class EditingController extends ObservableMixin() {
*
* This is using the same logic as the selection post-fixer.
*/
function fixTargetRanges( mapper: Mapper, schema: Schema ): GetCallback<ViewDocumentInputEvent> {
function fixTargetRanges( mapper: Mapper, schema: Schema, view: View ): GetCallback<ViewDocumentInputEvent> {
return ( evt, data ) => {
// The Renderer is disabled while composing on non-android browsers, so we can't be sure that target ranges
// could be properly mapped to view and model because the DOM and view tree drifted apart.
if ( view.document.isComposing && !env.isAndroid ) {
return;
}

for ( let i = 0; i < data.targetRanges.length; i++ ) {
const viewRange = data.targetRanges[ i ];
const modelRange = mapper.toModelRange( viewRange );
Expand Down
22 changes: 22 additions & 0 deletions packages/ckeditor5-engine/tests/controller/editingcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,28 @@ describe( 'EditingController', () => {
sinon.assert.calledOnce( enterSpy );
} );

it( 'should not crash while trying to fix null range while composing', () => {
setModelData( model, '<paragraph>a</paragraph>' );

const targetRanges = [ null ];

const eventData = {
inputType: 'insertCompositionText',
data: 'ab',
targetRanges
};

viewDocument.isComposing = true;
fireBeforeInputEvent( eventData );

// Verify beforeinput range.
sinon.assert.calledOnce( beforeInputSpy );

const inputData = beforeInputSpy.args[ 0 ][ 1 ];
expect( inputData.inputType ).to.equal( eventData.inputType );
expect( inputData.targetRanges[ 0 ] ).to.be.null;
} );

function fireBeforeInputEvent( eventData ) {
viewDocument.fire( 'beforeinput', {
domEvent: {
Expand Down

0 comments on commit 64adabd

Please sign in to comment.