Skip to content

Commit

Permalink
Support save serialization by component type
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Apr 6, 2017
1 parent 0dfc163 commit 20b7b94
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
20 changes: 13 additions & 7 deletions blocks/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@ import { getBlockAttributes } from './parser';
* Given a block's save render implementation and attributes, returns the
* static markup to be saved.
*
* @param {Function} save Save render implementation
* @param {Object} attributes Block attributes
* @return {string} Save content
* @param {Function|WPComponent} save Save render implementation
* @param {Object} attributes Block attributes
* @return {string} Save content
*/
export function getSaveContent( save, attributes ) {
const rawContent = save( { attributes } );
let rawContent;

// Support string return values from save, e.g. raw HTML attribute value
if ( 'string' === typeof rawContent ) {
return rawContent;
if ( save.prototype instanceof wp.element.Component ) {
rawContent = wp.element.createElement( save, { attributes } );
} else {
rawContent = save( { attributes } );

// Special-case function render implementation to allow raw HTML return
if ( 'string' === typeof rawContent ) {
return rawContent;
}
}

// Otherwise, infer as element
Expand Down
44 changes: 31 additions & 13 deletions blocks/test/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,41 @@ describe( 'block serializer', () => {
} );

describe( 'getSaveContent()', () => {
it( 'should return string verbatim', () => {
const saved = getSaveContent(
( { attributes } ) => attributes.fruit,
{ fruit: 'Bananas' }
);
context( 'function save', () => {
it( 'should return string verbatim', () => {
const saved = getSaveContent(
( { attributes } ) => attributes.fruit,
{ fruit: 'Bananas' }
);

expect( saved ).to.equal( 'Bananas' );
expect( saved ).to.equal( 'Bananas' );
} );

it( 'should return element as string if save returns element', () => {
const { createElement } = wp.element;
const saved = getSaveContent(
( { attributes } ) => createElement( 'div', null, attributes.fruit ),
{ fruit: 'Bananas' }
);

expect( saved ).to.equal( '<div>Bananas</div>' );
} );
} );

it( 'should return element as string if save returns element', () => {
const { createElement } = wp.element;
const saved = getSaveContent(
( { attributes } ) => createElement( 'div', null, attributes.fruit ),
{ fruit: 'Bananas' }
);
context( 'component save', () => {
it( 'should return element as string', () => {
const { Component, createElement } = wp.element;
const saved = getSaveContent(
class extends Component {
render() {
return createElement( 'div', null, this.props.attributes.fruit );
}
},
{ fruit: 'Bananas' }
);

expect( saved ).to.equal( '<div>Bananas</div>' );
expect( saved ).to.equal( '<div>Bananas</div>' );
} );
} );
} );

Expand Down

0 comments on commit 20b7b94

Please sign in to comment.