Skip to content

Commit

Permalink
Parser / Site Editor: Ensure autop is not run when freeform block is …
Browse files Browse the repository at this point in the history
…set to core/html (#52716)

* Parse / Site Editor: Ensure autop is not run when freeform block is set to core/html

* Switch to equals freeform instead of not equals core/html

* Rename core/test-freeform to core/freeform in tests
  • Loading branch information
andrewserong authored Jul 21, 2023
1 parent 7811801 commit f2a685f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 33 deletions.
10 changes: 5 additions & 5 deletions packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe( 'selectors', () => {
parent: [ 'core/test-block-b' ],
} );

registerBlockType( 'core/test-freeform', {
registerBlockType( 'core/freeform', {
save: ( props ) => <RawHTML>{ props.attributes.content }</RawHTML>,
category: 'text',
title: 'Test Freeform Content Handler',
Expand Down Expand Up @@ -177,7 +177,7 @@ describe( 'selectors', () => {
ancestor: [ 'core/test-block-ancestor' ],
} );

setFreeformContentHandlerName( 'core/test-freeform' );
setFreeformContentHandlerName( 'core/freeform' );

cachedSelectors.forEach( ( { clear } ) => clear() );
} );
Expand All @@ -187,7 +187,7 @@ describe( 'selectors', () => {
unregisterBlockType( 'core/test-block-a' );
unregisterBlockType( 'core/test-block-b' );
unregisterBlockType( 'core/test-block-c' );
unregisterBlockType( 'core/test-freeform' );
unregisterBlockType( 'core/freeform' );
unregisterBlockType( 'core/post-content-child' );
unregisterBlockType( 'core/test-block-ancestor' );
unregisterBlockType( 'core/test-block-parent' );
Expand Down Expand Up @@ -3450,7 +3450,7 @@ describe( 'selectors', () => {
expect( firstBlockFirstCall.map( ( item ) => item.id ) ).toEqual( [
'core/test-block-a',
'core/test-block-b',
'core/test-freeform',
'core/freeform',
'core/test-block-ancestor',
'core/test-block-parent',
'core/block/1',
Expand All @@ -3466,7 +3466,7 @@ describe( 'selectors', () => {
expect( secondBlockFirstCall.map( ( item ) => item.id ) ).toEqual( [
'core/test-block-a',
'core/test-block-b',
'core/test-freeform',
'core/freeform',
'core/test-block-ancestor',
'core/test-block-parent',
'core/block/1',
Expand Down
1 change: 1 addition & 0 deletions packages/blocks/src/api/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export function normalizeRawBlock( rawBlock, options ) {
// meaning there are no negative consequences to repeated autop calls.
if (
rawBlockName === fallbackBlockName &&
rawBlockName === 'core/freeform' &&
! options?.__unstableSkipAutop
) {
rawInnerHTML = autop( rawInnerHTML ).trim();
Expand Down
23 changes: 17 additions & 6 deletions packages/blocks/src/api/parser/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,30 @@ describe( 'block parser', () => {
} );

it( 'should fall back to the freeform content handler if block type not specified', () => {
registerBlockType( 'core/freeform-block', unknownBlockSettings );
setFreeformContentHandlerName( 'core/freeform-block' );
registerBlockType( 'core/freeform', unknownBlockSettings );
setFreeformContentHandlerName( 'core/freeform' );

const block = parseRawBlock( {
innerHTML: 'content',
} );
expect( block.name ).toEqual( 'core/freeform-block' );
expect( block.name ).toEqual( 'core/freeform' );
expect( block.attributes ).toEqual( { content: '<p>content</p>' } );
} );

it( 'skips adding paragraph tags if freeform block is set to core/html', () => {
registerBlockType( 'core/html', unknownBlockSettings );
setFreeformContentHandlerName( 'core/html' );

const block = parseRawBlock( {
innerHTML: 'content',
} );
expect( block.name ).toEqual( 'core/html' );
expect( block.attributes ).toEqual( { content: 'content' } );
} );

it( 'skips adding paragraph tags if __unstableSkipAutop is passed as an option', () => {
registerBlockType( 'core/freeform-block', unknownBlockSettings );
setFreeformContentHandlerName( 'core/freeform-block' );
registerBlockType( 'core/freeform', unknownBlockSettings );
setFreeformContentHandlerName( 'core/freeform' );

const block = parseRawBlock(
{
Expand All @@ -112,7 +123,7 @@ describe( 'block parser', () => {
__unstableSkipAutop: true,
}
);
expect( block.name ).toEqual( 'core/freeform-block' );
expect( block.name ).toEqual( 'core/freeform' );
expect( block.attributes ).toEqual( { content: 'content' } );
} );

Expand Down
3 changes: 2 additions & 1 deletion packages/blocks/src/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ export function __unstableSerializeAndClean( blocks ) {
// pre-block-editor removep'd content formatting.
if (
blocks.length === 1 &&
blocks[ 0 ].name === getFreeformContentHandlerName()
blocks[ 0 ].name === getFreeformContentHandlerName() &&
blocks[ 0 ].name === 'core/freeform'
) {
content = removep( content );
}
Expand Down
16 changes: 8 additions & 8 deletions packages/blocks/src/api/test/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ describe( 'block serializer', () => {

describe( 'serializeBlock()', () => {
it( 'serializes the freeform content fallback block without comment delimiters', () => {
registerBlockType( 'core/freeform-block', {
registerBlockType( 'core/freeform', {
category: 'text',
title: 'freeform block',
attributes: {
Expand All @@ -254,8 +254,8 @@ describe( 'block serializer', () => {
},
save: ( { attributes } ) => attributes.fruit,
} );
setFreeformContentHandlerName( 'core/freeform-block' );
const block = createBlock( 'core/freeform-block', {
setFreeformContentHandlerName( 'core/freeform' );
const block = createBlock( 'core/freeform', {
fruit: 'Bananas',
} );

Expand All @@ -264,7 +264,7 @@ describe( 'block serializer', () => {
expect( content ).toBe( 'Bananas' );
} );
it( 'serializes the freeform content fallback block with comment delimiters in nested context', () => {
registerBlockType( 'core/freeform-block', {
registerBlockType( 'core/freeform', {
category: 'text',
title: 'freeform block',
attributes: {
Expand All @@ -274,17 +274,17 @@ describe( 'block serializer', () => {
},
save: ( { attributes } ) => attributes.fruit,
} );
setFreeformContentHandlerName( 'core/freeform-block' );
const block = createBlock( 'core/freeform-block', {
setFreeformContentHandlerName( 'core/freeform' );
const block = createBlock( 'core/freeform', {
fruit: 'Bananas',
} );

const content = serializeBlock( block, { isInnerBlocks: true } );

expect( content ).toBe(
'<!-- wp:freeform-block {"fruit":"Bananas"} -->\n' +
'<!-- wp:freeform {"fruit":"Bananas"} -->\n' +
'Bananas\n' +
'<!-- /wp:freeform-block -->'
'<!-- /wp:freeform -->'
);
} );
it( 'serializes the unregistered fallback block without comment delimiters', () => {
Expand Down
26 changes: 13 additions & 13 deletions packages/editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ describe( 'selectors', () => {
parent: [ 'core/test-block-b' ],
} );

registerBlockType( 'core/test-freeform', {
registerBlockType( 'core/freeform', {
save: ( props ) => <RawHTML>{ props.attributes.content }</RawHTML>,
category: 'text',
title: 'Test Freeform Content Handler',
Expand All @@ -287,7 +287,7 @@ describe( 'selectors', () => {
save: () => null,
} );

setFreeformContentHandlerName( 'core/test-freeform' );
setFreeformContentHandlerName( 'core/freeform' );
setDefaultBlockName( 'core/test-default' );

cachedSelectors.forEach( ( { clear } ) => clear() );
Expand All @@ -298,7 +298,7 @@ describe( 'selectors', () => {
unregisterBlockType( 'core/test-block-a' );
unregisterBlockType( 'core/test-block-b' );
unregisterBlockType( 'core/test-block-c' );
unregisterBlockType( 'core/test-freeform' );
unregisterBlockType( 'core/freeform' );
unregisterBlockType( 'core/test-default' );

setFreeformContentHandlerName( undefined );
Expand Down Expand Up @@ -1337,7 +1337,7 @@ describe( 'selectors', () => {
value: [
{
clientId: 123,
name: 'core/test-freeform',
name: 'core/freeform',
isValid: true,
attributes: {
content: '',
Expand All @@ -1364,7 +1364,7 @@ describe( 'selectors', () => {
value: [
{
clientId: 123,
name: 'core/test-freeform',
name: 'core/freeform',
isValid: true,
attributes: {
content: '',
Expand Down Expand Up @@ -1741,7 +1741,7 @@ describe( 'selectors', () => {
value: [
{
clientId: 123,
name: 'core/test-freeform',
name: 'core/freeform',
isValid: true,
attributes: {
content: '',
Expand Down Expand Up @@ -1769,7 +1769,7 @@ describe( 'selectors', () => {
value: [
{
clientId: 123,
name: 'core/test-freeform',
name: 'core/freeform',
isValid: true,
attributes: {
content: '',
Expand Down Expand Up @@ -1797,7 +1797,7 @@ describe( 'selectors', () => {
value: [
{
clientId: 123,
name: 'core/test-freeform',
name: 'core/freeform',
isValid: true,
attributes: {
content: 'Test Data',
Expand Down Expand Up @@ -1825,15 +1825,15 @@ describe( 'selectors', () => {
value: [
{
clientId: 123,
name: 'core/test-freeform',
name: 'core/freeform',
isValid: true,
attributes: {
content: '',
},
},
{
clientId: 456,
name: 'core/test-freeform',
name: 'core/freeform',
isValid: true,
attributes: {
content: '',
Expand Down Expand Up @@ -2426,7 +2426,7 @@ describe( 'selectors', () => {
} );

it( "returns removep'd serialization of blocks for single unknown", () => {
const unknownBlock = createBlock( 'core/test-freeform', {
const unknownBlock = createBlock( 'core/freeform', {
content: '<p>foo</p>',
} );
const state = {
Expand All @@ -2448,10 +2448,10 @@ describe( 'selectors', () => {
} );

it( "returns non-removep'd serialization of blocks for multiple unknown", () => {
const firstUnknown = createBlock( 'core/test-freeform', {
const firstUnknown = createBlock( 'core/freeform', {
content: '<p>foo</p>',
} );
const secondUnknown = createBlock( 'core/test-freeform', {
const secondUnknown = createBlock( 'core/freeform', {
content: '<p>bar</p>',
} );
const state = {
Expand Down

0 comments on commit f2a685f

Please sign in to comment.