Skip to content

Commit

Permalink
[Blocks] Preserve source markup in invalid blocks
Browse files Browse the repository at this point in the history
Previously we have been re-generating a block's save content when
we fail to properly validate that block. While this can work in many
cases it breaks down quickly because of the invalidly-parsed attributes.

Consider a list block whose `<ul>` tag has been removed. The block will
fail to validate and then also fail to read the content of the list items
because the CSS selector used to source the attribute won't work. The
attribute for the list's content will be empty and `getSaveContent` will
return an empty list, this despite the fact that the contents were largely
fine in the post HTML when loaded.

In this patch we're threading a new property into the block node called
`sourceMarkup` whose purpose is to preserve the original HTML in a post
so that we can choose to preserve that on save instead of corrupting or
losing content through the HTML re-generation.

With `sourceMarkup` we get a less-processed version of the source to
relay into save which should eliminate a common point of frustration:
saving a post erases content.
  • Loading branch information
dmsnell committed Feb 2, 2022
1 parent d257d32 commit 259f024
Show file tree
Hide file tree
Showing 244 changed files with 764 additions and 437 deletions.
166 changes: 81 additions & 85 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 9 additions & 14 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ import {
useCallback,
RawHTML,
} from '@wordpress/element';
import {
getBlockType,
getSaveContent,
isUnmodifiedDefaultBlock,
} from '@wordpress/blocks';
import { getBlockType, isUnmodifiedDefaultBlock } from '@wordpress/blocks';
import { withFilters } from '@wordpress/components';
import { withDispatch, withSelect, useDispatch } from '@wordpress/data';
import { compose, pure, ifCondition } from '@wordpress/compose';
Expand Down Expand Up @@ -69,6 +65,7 @@ function Block( { children, isHtml, ...props } ) {
}

function BlockListBlock( {
block,
mode,
isLocked,
canRemove,
Expand Down Expand Up @@ -134,21 +131,19 @@ function BlockListBlock( {
);
}

let block;
let BlockComponent;

if ( ! isValid ) {
const saveContent = getSaveContent( blockType, attributes );

block = (
BlockComponent = (
<Block className="has-warning">
<BlockInvalidWarning clientId={ clientId } />
<RawHTML>{ safeHTML( saveContent ) }</RawHTML>
<RawHTML>{ safeHTML( block.sourceMarkup ) }</RawHTML>
</Block>
);
} else if ( mode === 'html' ) {
// Render blockEdit so the inspector controls don't disappear.
// See #8969.
block = (
BlockComponent = (
<>
<div style={ { display: 'none' } }>{ blockEdit }</div>
<Block isHtml>
Expand All @@ -157,9 +152,9 @@ function BlockListBlock( {
</>
);
} else if ( blockType?.apiVersion > 1 ) {
block = blockEdit;
BlockComponent = blockEdit;
} else {
block = <Block { ...wrapperProps }>{ blockEdit }</Block>;
BlockComponent = <Block { ...wrapperProps }>{ blockEdit }</Block>;
}

const value = {
Expand All @@ -179,7 +174,7 @@ function BlockListBlock( {
</Block>
}
>
{ block }
{ BlockComponent }
</BlockCrashBoundary>
</BlockListBlockContext.Provider>
);
Expand Down
Loading

0 comments on commit 259f024

Please sign in to comment.