Skip to content

Commit

Permalink
fix: Guard against a block styles crash due to null block values (#56903
Browse files Browse the repository at this point in the history
)

* fix: Guard against a block styles crash due to null block values

In certain scenarios, e.g., when the editor hangs due to poor
performance, the `getBlock` value unexpectedly returns `null`. To guard
against a crash in this scenario, we now conditionally access attributes
on the block.

A ideal resolution would be improving editor performance to avoid the
scenario where a race condition causes a crash, but the fact remains
that `getBlock` _can_ return `null` values and that we should not
presume that it will not.

* docs: Add change log entry
  • Loading branch information
dcalhoun authored Dec 8, 2023
1 parent 6211c7a commit cfa3fd6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ import StylePreview from './preview';
import containerStyles from './style.scss';
import { store as blockEditorStore } from '../../store';

const EMPTY_ARRAY = [];

function BlockStyles( { clientId, url } ) {
const selector = ( select ) => {
const { getBlock } = select( blockEditorStore );
const { getBlockStyles } = select( blocksStore );
const block = getBlock( clientId );
return {
styles: getBlockStyles( block.name ),
className: block.attributes.className || '',
styles: getBlockStyles( block?.name ) || EMPTY_ARRAY,
className: block?.attributes?.className || '',
};
};

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 @@ -12,6 +12,7 @@ For each user feature we should also add a importance categorization label to i
## Unreleased
- [*] [internal] Move InserterButton from components package to block-editor package [#56494]
- [*] [internal] Move ImageLinkDestinationsScreen from components package to block-editor package [#56775]
- [*] Guard against an Image block styles crash due to null block values [#56903]

## 1.109.2
- [**] Fix issue related to text color format and receiving in rare cases an undefined ref from `RichText` component [#56686]
Expand Down

1 comment on commit cfa3fd6

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in cfa3fd6.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7144278702
📝 Reported issues:

Please sign in to comment.