Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RNMobile: Avoid crashes by ensuring RichText value exists prior to toString calls #58088

Merged
merged 3 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fluiddot marked this conversation as resolved.
Show resolved Hide resolved
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
1 change: 1 addition & 0 deletions packages/react-native-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ For each user feature we should also add a importance categorization label to i

## Unreleased
- [**] Video block: Fix logic for displaying empty state based on source presence [#58015]
- [**] Fix crash when RichText values are not defined [#58088]

## 1.111.0
- [**] Image block media uploads display a custom error message when there is no internet connection [#56937]
Expand Down
Loading