From a52980bd0f8dad5dad66f20715bd1b2c5aa9a434 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Thu, 1 Jun 2017 16:18:57 -0400 Subject: [PATCH] Change attribute serialization to explicit opt-in --- blocks/api/serializer.js | 43 +++++++++++++++------------------- blocks/api/test/serializer.js | 39 ++++++++++++++++-------------- blocks/library/button/index.js | 5 ++++ blocks/library/image/index.js | 5 ++++ blocks/library/quote/index.js | 5 ++++ 5 files changed, 56 insertions(+), 41 deletions(-) diff --git a/blocks/api/serializer.js b/blocks/api/serializer.js index 3d114e4aa3183..7c025e59eab35 100644 --- a/blocks/api/serializer.js +++ b/blocks/api/serializer.js @@ -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 @@ -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( ' ' ); } /** @@ -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, @@ -83,10 +81,7 @@ export default function serialize( blocks ) { '' + ( saveContent ? '\n' + beautifyHtml( saveContent, beautifyOptions ) + '\n' : '' ) + '