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

Blocks: Preserves source of unrecognized blocks inside of Code Editor #39523

Merged
merged 3 commits into from
Mar 28, 2022
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
11 changes: 9 additions & 2 deletions packages/blocks/src/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ import {
getFreeformContentHandlerName,
getUnregisteredTypeHandlerName,
} from './registration';
import { serializeRawBlock } from './parser/serialize-raw-block';
import { isUnmodifiedDefaultBlock, normalizeBlockType } from './utils';

/** @typedef {import('./parser').WPBlock} WPBlock */

/**
* @typedef {Object} WPBlockSerializationOptions Serialization Options.
*
Expand Down Expand Up @@ -337,12 +340,16 @@ export function getCommentDelimitedContent(
* Returns the content of a block, including comment delimiters, determining
* serialized attributes and content form from the current state of the block.
*
* @param {Object} block Block instance.
* @param {WPBlockSerializationOptions} options Serialization options.
* @param {WPBlock} block Block instance.
* @param {WPBlockSerializationOptions} options Serialization options.
*
* @return {string} Serialized block.
*/
export function serializeBlock( block, { isInnerBlocks = false } = {} ) {
if ( ! block.isValid && block.__unstableBlockSource ) {
return serializeRawBlock( block.__unstableBlockSource );
}

const blockName = block.name;
const saveContent = getBlockInnerHTML( block );

Expand Down
42 changes: 42 additions & 0 deletions packages/blocks/src/api/test/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,48 @@ describe( 'block serializer', () => {

expect( content ).toBe( 'Bananas' );
} );
it( 'preserves content from invalid blocks when source information is present', () => {
registerBlockType( 'core/quote', {
category: 'text',
title: 'Quote',
attributes: { content: 'string' },
save: ( { attributes } ) =>
createElement( 'blockquote', {}, attributes.content ),
} );

const block = {
...createBlock( 'core/quote' ),
isValid: false,
__unstableBlockSource: {
blockName: 'quote',
attrs: {},
innerHTML: '<p>Not a quote</p>',
innerBlocks: [],
innerContent: [ '<p>Not a quote</p>' ],
},
};

expect( serializeBlock( block ) ).toBe(
'<!-- wp:quote -->\n<p>Not a quote</p>\n<!-- /wp:quote -->'
);
} );
it( 're-generates content from invalid blocks when source information is missing (losing content)', () => {
registerBlockType( 'core/quote', {
category: 'text',
title: 'Quote',
attributes: { content: 'string' },
save: ( { attributes } ) =>
createElement( 'blockquote', {}, attributes.content ),
} );

// missing attributes as a result of a failed parse
const block = {
...createBlock( 'core/quote' ),
isValid: false,
};

expect( serializeBlock( block ) ).toBe( '<!-- wp:quote /-->' );
} );
} );

describe( 'serialize()', () => {
Expand Down