Skip to content

Commit

Permalink
RNMobile: Avoid crashes by ensuring RichText value exists prior to `t…
Browse files Browse the repository at this point in the history
…oString` calls (#58088)
  • Loading branch information
Siobhan Bamber committed Jan 23, 2024
1 parent 07a3b3c commit 32aa57d
Showing 1 changed file with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export class RichText extends Component {
onCreateUndoLevel() {
const { __unstableOnCreateUndoLevel: onCreateUndoLevel } = this.props;
// If the content is the same, no level needs to be created.
if ( this.lastHistoryValue.toString() === this.value.toString() ) {
if ( this.lastHistoryValue?.toString() === this.value?.toString() ) {
return;
}

Expand Down Expand Up @@ -317,7 +317,7 @@ export class RichText extends Component {
event.nativeEvent.text
);
// On iOS, onChange can be triggered after selection changes, even though there are no content changes.
if ( contentWithoutRootTag === this.value.toString() ) {
if ( contentWithoutRootTag === this.value?.toString() ) {
return;
}
this.lastEventCount = event.nativeEvent.eventCount;
Expand All @@ -333,7 +333,7 @@ export class RichText extends Component {
);

this.debounceCreateUndoLevel();
const refresh = this.value.toString() !== contentWithoutRootTag;
const refresh = this.value?.toString() !== contentWithoutRootTag;
this.value = contentWithoutRootTag;

// We don't want to refresh if our goal is just to create a record.
Expand Down Expand Up @@ -564,7 +564,7 @@ export class RichText extends Component {
// Check if value is up to date with latest state of native AztecView.
if (
event.nativeEvent.text &&
event.nativeEvent.text !== this.props.value.toString()
event.nativeEvent.text !== this.props.value?.toString()
) {
this.onTextUpdate( event );
}
Expand All @@ -589,7 +589,7 @@ export class RichText extends Component {
// this approach is not perfectly reliable.
const isManual =
this.lastAztecEventType !== 'input' &&
this.props.value.toString() === this.value.toString();
this.props.value?.toString() === this.value?.toString();
if ( hasChanged && isManual ) {
const value = this.createRecord();
const activeFormats = getActiveFormats( value );
Expand Down Expand Up @@ -659,7 +659,7 @@ export class RichText extends Component {
event.nativeEvent.text
);
if (
contentWithoutRootTag === this.value.toString() &&
contentWithoutRootTag === this.value?.toString() &&
realStart === this.selectionStart &&
realEnd === this.selectionEnd
) {
Expand Down Expand Up @@ -756,7 +756,7 @@ export class RichText extends Component {
typeof nextProps.value !== 'undefined' &&
typeof this.props.value !== 'undefined' &&
( ! this.comesFromAztec || ! this.firedAfterTextChanged ) &&
nextProps.value.toString() !== this.props.value.toString()
nextProps.value?.toString() !== this.props.value?.toString()
) {
// Gutenberg seems to try to mirror the caret state even on events that only change the content so,
// let's force caret update if state has selection set.
Expand Down Expand Up @@ -824,7 +824,7 @@ export class RichText extends Component {
const { style, tagName } = this.props;
const { currentFontSize } = this.state;

if ( this.props.value.toString() !== this.value.toString() ) {
if ( this.props.value?.toString() !== this.value?.toString() ) {
this.value = this.props.value;
}
const { __unstableIsSelected: prevIsSelected } = prevProps;
Expand Down Expand Up @@ -884,8 +884,8 @@ export class RichText extends Component {
// On android if content is empty we need to send no content or else the placeholder will not show.
if (
! this.isIOS &&
( value.toString() === '' ||
value.toString() === EMPTY_PARAGRAPH_TAGS )
( value?.toString() === '' ||
value?.toString() === EMPTY_PARAGRAPH_TAGS )
) {
return '';
}
Expand Down

0 comments on commit 32aa57d

Please sign in to comment.