From aae9c06d8e7df98de3a2ed1bc35834f57a7cac0e Mon Sep 17 00:00:00 2001 From: John Godley Date: Mon, 29 Oct 2018 14:45:38 +0000 Subject: [PATCH] Always check selection range when creating undo levels Sometimes `getSelection` can have an empty range, causing `getRangeAt` to throw an error. This catches that situation and adds checks in all places that use it. --- .../editor/src/components/rich-text/index.js | 34 ++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/packages/editor/src/components/rich-text/index.js b/packages/editor/src/components/rich-text/index.js index 49574450ccb8c4..a3127715fbd1f5 100644 --- a/packages/editor/src/components/rich-text/index.js +++ b/packages/editor/src/components/rich-text/index.js @@ -234,7 +234,13 @@ export class RichText extends Component { } createRecord() { - const range = window.getSelection().getRangeAt( 0 ); + const selection = window.getSelection(); + + if ( selection.rangeCount === 0 ) { + return null; + } + + const range = selection.getRangeAt( 0 ); return create( { element: this.editableRef, @@ -407,6 +413,11 @@ export class RichText extends Component { */ onInput() { const record = this.createRecord(); + + if ( ! record ) { + return; + } + const transformed = this.patterns.reduce( ( accumlator, transform ) => transform( accumlator ), record ); // Don't apply changes if there's no transform. Content will be up to @@ -424,7 +435,12 @@ export class RichText extends Component { return; } - const { start, end, formats } = this.createRecord(); + const record = this.createRecord(); + if ( ! record ) { + return; + } + + const { start, end, formats } = record; if ( formats[ start ] ) { this.props.onEnterFormattedText(); @@ -473,7 +489,11 @@ export class RichText extends Component { // input event. Avoid dispatching an action if the original event is // blur because the content will already be up-to-date. if ( ! event || ! event.originalEvent || event.originalEvent.type !== 'blur' ) { - this.onChange( this.createRecord(), true ); + const record = this.createRecord(); + + if ( record ) { + this.onChange( record, true ); + } } this.props.onCreateUndoLevel(); @@ -662,7 +682,11 @@ export class RichText extends Component { // The input event does not fire when the whole field is selected and // BACKSPACE is pressed. if ( keyCode === BACKSPACE ) { - this.onChange( this.createRecord(), true ); + const record = this.createRecord(); + + if ( record ) { + this.onChange( record, true ); + } } // `scrollToRect` is called on `nodechange`, whereas calling it on @@ -714,7 +738,7 @@ export class RichText extends Component { const { onSplit } = this.props; const record = this.createRecord(); - if ( ! onSplit ) { + if ( ! onSplit || ! record ) { return; }