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

Change attribute serialization to explicit opt-in #988

Closed
wants to merge 1 commit into from
Closed
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
43 changes: 19 additions & 24 deletions blocks/api/serializer.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/**
* External dependencies
*/
import { difference } from 'lodash';
import { reduce } from 'lodash';
import { html as beautifyHtml } from 'js-beautify';

/**
* Internal dependencies
*/
import { getBlockType } from './registration';
import { parseBlockAttributes } from './parser';

/**
* Given a block's save render implementation and attributes, returns the
Expand Down Expand Up @@ -37,30 +36,28 @@ export function getSaveContent( save, attributes ) {
}

/**
* Returns comment attributes as serialized string, determined by subset of
* difference between actual attributes of a block and those expected based
* on its settings.
* Returns comment attributes as serialized string, determined by the return
* value object of the block's `encodeAttributes` implementation. If a block
* does not provide an implementation, an empty string is returned.
*
* @param {Object} realAttributes Actual block attributes
* @param {Object} expectedAttributes Expected block attributes
* @return {string} Comment attributes
* @param {?Function} encodeAttributes Attribute encoding implementation
* @param {Object} attributes Block attributes
* @return {String} Comment attributes
*/
export function getCommentAttributes( realAttributes, expectedAttributes ) {
// Find difference and build into object subset of attributes.
const keys = difference(
Object.keys( realAttributes ),
Object.keys( expectedAttributes )
);
export function getCommentAttributes( encodeAttributes, attributes ) {
let encodedAttributes;
if ( encodeAttributes ) {
encodedAttributes = encodeAttributes( attributes );
}

// Serialize the comment attributes as `key="value"`.
return keys.reduce( ( memo, key ) => {
const value = realAttributes[ key ];
if ( undefined === value ) {
return memo;
return reduce( encodedAttributes, ( result, value, key ) => {
if ( undefined !== value ) {
result.push( `${ key }="${ value }"` );
}

return memo + `${ key }="${ value }" `;
}, '' );
return result;
}, [] ).join( ' ' );
}

/**
Expand All @@ -74,6 +71,7 @@ export default function serialize( blocks ) {
const blockName = block.name;
const blockType = getBlockType( blockName );
const saveContent = getSaveContent( blockType.save, block.attributes );
const commentAttributes = getCommentAttributes( blockType.encodeAttributes, block.attributes );
const beautifyOptions = {
indent_inner_html: true,
wrap_line_length: 0,
Expand All @@ -83,10 +81,7 @@ export default function serialize( blocks ) {
'<!-- wp:' +
blockName +
' ' +
getCommentAttributes(
block.attributes,
parseBlockAttributes( saveContent, blockType )
) +
( commentAttributes ? commentAttributes + ' ' : '' ) +
'-->' +
( saveContent ? '\n' + beautifyHtml( saveContent, beautifyOptions ) + '\n' : '' ) +
'<!-- /wp:' +
Expand Down
39 changes: 22 additions & 17 deletions blocks/api/test/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import { expect } from 'chai';
import { pick, identity } from 'lodash';

/**
* Internal dependencies
Expand Down Expand Up @@ -56,34 +57,34 @@ describe( 'block serializer', () => {
} );

describe( 'getCommentAttributes()', () => {
it( 'should return empty string if no difference', () => {
const attributes = getCommentAttributes( {}, {} );
it( 'should return empty string if no encodeAttributes implementation', () => {
const commentAttributes = getCommentAttributes( undefined, {
fruit: 'bananas',
} );

expect( attributes ).to.equal( '' );
expect( commentAttributes ).to.equal( '' );
} );

it( 'should return joined string of key:value pairs by difference subset', () => {
const attributes = getCommentAttributes( {
fruit: 'bananas',
category: 'food',
ripeness: 'ripe',
}, {
fruit: 'bananas',
} );
it( 'should return joined string of `key="value"` pairs by difference subset', () => {
const commentAttributes = getCommentAttributes(
( attributes ) => pick( attributes, 'category', 'ripeness' ),
{
fruit: 'bananas',
category: 'food',
ripeness: 'ripe',
}
);

expect( attributes ).to.equal( 'category="food" ripeness="ripe" ' );
expect( commentAttributes ).to.equal( 'category="food" ripeness="ripe"' );
} );

it( 'should not append an undefined attribute value', () => {
const attributes = getCommentAttributes( {
fruit: 'bananas',
const commentAttributes = getCommentAttributes( identity, {
category: 'food',
ripeness: undefined,
}, {
fruit: 'bananas',
} );

expect( attributes ).to.equal( 'category="food" ' );
expect( commentAttributes ).to.equal( 'category="food"' );
} );
} );

Expand All @@ -95,6 +96,10 @@ describe( 'block serializer', () => {
content: rawContent,
};
},
encodeAttributes( attributes ) {
const { align } = attributes;
return { align };
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't you think the metadata( 'align' ) is better for consistency? It's just another way of parsing the attribute.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't you think the metadata( 'align' ) is better for consistency? It's just another way of parsing the attribute.

I suppose it depends where we take attributes initialization. I tried to communicate my current state of mind earlier at #391 (comment) . If matching included encoded attributes, I'd agree we wouldn't need this function at all. I might try to spend some time tomorrow exploring AST selectors like the ESLint ones I mentioned in that comment. If those work out, I don't think we'd really want to use them for comment sourcing.

save( { attributes } ) {
return <p dangerouslySetInnerHTML={ { __html: attributes.content } } />;
},
Expand Down
5 changes: 5 additions & 0 deletions blocks/library/button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ registerBlockType( 'core/button', {
text: children( 'a' ),
},

encodeAttributes( attributes ) {
const { align } = attributes;
return { align };
},

controls: [
{
icon: 'align-left',
Expand Down
5 changes: 5 additions & 0 deletions blocks/library/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ registerBlockType( 'core/image', {
caption: children( 'figcaption' ),
},

encodeAttributes( attributes ) {
const { align } = attributes;
return { align };
},

controls: [
{
icon: 'align-left',
Expand Down
5 changes: 5 additions & 0 deletions blocks/library/quote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ registerBlockType( 'core/quote', {
citation: children( 'footer' ),
},

encodeAttributes( attributes ) {
const { style } = attributes;
return { style };
},

controls: [ 1, 2 ].map( ( variation ) => ( {
icon: 'format-quote',
title: wp.i18n.sprintf( wp.i18n.__( 'Quote style %d' ), variation ),
Expand Down