Skip to content

Commit

Permalink
Blocks: Serialize fallback block without comment delimiters
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Aug 25, 2017
1 parent 0118972 commit 44b8464
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 9 deletions.
23 changes: 18 additions & 5 deletions blocks/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Component, createElement, renderToString, cloneElement, Children } from
/**
* Internal dependencies
*/
import { getBlockType } from './registration';
import { getBlockType, getUnknownTypeHandlerName } from './registration';

/**
* Returns the block's default classname from its name
Expand Down Expand Up @@ -163,6 +163,13 @@ export function getCommentDelimitedContent( blockName, attributes, content ) {
);
}

/**
* 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
* @return {String} Serialized block
*/
export function serializeBlock( block ) {
const blockName = block.name;
const blockType = getBlockType( blockName );
Expand All @@ -178,11 +185,17 @@ export function serializeBlock( block ) {

const saveAttributes = getCommentAttributes( block.attributes, blockType.attributes );

if ( 'core/more' === blockName ) {
return `<!--more${ saveAttributes.text ? ` ${ saveAttributes.text }` : '' }-->${ saveAttributes.noTeaser ? '\n<!--noteaser-->' : '' }`;
}
switch ( blockName ) {
case 'core/more':
const { text, noTeaser } = saveAttributes;
return `<!--more${ text ? ` ${ text }` : '' }-->${ noTeaser ? '\n<!--noteaser-->' : '' }`;

case getUnknownTypeHandlerName():
return saveContent;

return getCommentDelimitedContent( blockName, saveAttributes, saveContent );
default:
return getCommentDelimitedContent( blockName, saveAttributes, saveContent );
}
}

/**
Expand Down
77 changes: 76 additions & 1 deletion blocks/api/test/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ import serialize, {
getSaveContent,
serializeAttributes,
getCommentDelimitedContent,
serializeBlock,
} from '../serializer';
import { getBlockTypes, registerBlockType, unregisterBlockType } from '../registration';
import {
getBlockTypes,
registerBlockType,
unregisterBlockType,
setUnknownTypeHandlerName,
} from '../registration';
import { createBlock } from '../';

describe( 'block serializer', () => {
afterEach( () => {
setUnknownTypeHandlerName( undefined );
getBlockTypes().forEach( block => {
unregisterBlockType( block.name );
} );
Expand Down Expand Up @@ -257,6 +264,74 @@ describe( 'block serializer', () => {
} );
} );

describe( 'serializeBlock()', () => {
describe( '"more" block', () => {
beforeEach( () => {
registerBlockType( 'core/more', {
category: 'layout',

attributes: {
text: {
type: 'string',
},
noTeaser: {
type: 'boolean',
default: false,
},
},

save: ( { attributes } ) => attributes.text,
} );
} );

it( 'serializes without text', () => {
const block = createBlock( 'core/more', {} );

const content = serializeBlock( block );

expect( content ).toBe( '<!--more-->' );
} );

it( 'serializes with text', () => {
const block = createBlock( 'core/more', {
text: 'Read more!',
} );

const content = serializeBlock( block );

expect( content ).toBe( '<!--more Read more!-->' );
} );

it( 'serializes with no teaser', () => {
const block = createBlock( 'core/more', {
noTeaser: true,
} );

const content = serializeBlock( block );

expect( content ).toBe( '<!--more-->\n<!--noteaser-->' );
} );
} );

it( 'serializes the fallback block without comment delimiters', () => {
registerBlockType( 'core/unknown-block', {
category: 'common',
attributes: {
fruit: {
type: 'string',
},
},
save: ( { attributes } ) => attributes.fruit,
} );
setUnknownTypeHandlerName( 'core/unknown-block' );
const block = createBlock( 'core/unknown-block', { fruit: 'Bananas' } );

const content = serializeBlock( block );

expect( content ).toBe( 'Bananas' );
} );
} );

describe( 'serialize()', () => {
it( 'should serialize the post content properly', () => {
const blockType = {
Expand Down
4 changes: 1 addition & 3 deletions blocks/test/fixtures/core__freeform.serialized.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<!-- wp:core/freeform -->
Testing freeform block with some
<div class="wp-some-class">
HTML <span style="color: red;">content</span>
HTML <span style="color: red;">content</span>
</div>
<!-- /wp:core/freeform -->

0 comments on commit 44b8464

Please sign in to comment.