Skip to content
Open
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
11 changes: 11 additions & 0 deletions packages/block-library/src/code/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import { __ } from '@wordpress/i18n';
import { RichText, useBlockProps } from '@wordpress/block-editor';
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';

const SUPPORTS_BREAK_SPACES =
typeof CSS !== 'undefined' &&
// eslint-disable-next-line no-undef
CSS.supports &&
// eslint-disable-next-line no-undef
CSS.supports( 'white-space', 'break-spaces' );
Comment on lines +8 to +13
Copy link
Member

Choose a reason for hiding this comment

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

I think we can simplify this with optional chaining - !! CSS?.supports?.( 'white-space', 'break-spaces' );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It think it is safer as the suggested expression will short-circuit and return undefined if either CSS or CSS.supports is not defined.

Copy link
Member

Choose a reason for hiding this comment

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

The suggested expression will always return a Boolean, because of the !! operator.

if either CSS or CSS.supports is not defined.

Then SUPPORTS_BREAK_SPACES should be false, this is what we want, no?


export default function CodeEdit( {
attributes,
setAttributes,
Expand All @@ -13,6 +20,9 @@ export default function CodeEdit( {
mergeBlocks,
} ) {
const blockProps = useBlockProps();

const whiteSpaceStyle = SUPPORTS_BREAK_SPACES ? 'break-spaces' : 'pre-wrap';

return (
<pre { ...blockProps }>
<RichText
Expand All @@ -29,6 +39,7 @@ export default function CodeEdit( {
__unstableOnSplitAtDoubleLineEnd={ () =>
insertBlocksAfter( createBlock( getDefaultBlockName() ) )
}
style={ { whiteSpace: whiteSpaceStyle } }
/>
</pre>
);
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/code/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@
direction: ltr;
text-align: initial;
/*!rtl:end:ignore*/

@supports (white-space: break-spaces) {
white-space: break-spaces;
}
}
}
Loading