From d272cbd1a116a07cb952ba36ba7bae1d3173e1ed Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 14 Jul 2017 22:44:01 -0700 Subject: [PATCH 1/3] Prevent serializing block attributes to comment which match defaults --- blocks/api/serializer.js | 9 +++++++-- blocks/api/test/serializer.js | 7 ++++++- blocks/library/text/index.js | 4 ++++ blocks/test/fixtures/core__cover-image.serialized.html | 2 +- blocks/test/fixtures/core__latest-posts.html | 2 +- blocks/test/fixtures/core__latest-posts.json | 2 +- blocks/test/fixtures/core__latest-posts.parsed.json | 2 +- blocks/test/fixtures/core__latest-posts.serialized.html | 2 +- blocks/test/fixtures/core__text__align-right.json | 3 ++- 9 files changed, 24 insertions(+), 9 deletions(-) diff --git a/blocks/api/serializer.js b/blocks/api/serializer.js index 9c56e2b703f2e1..438e143aa1ce98 100644 --- a/blocks/api/serializer.js +++ b/blocks/api/serializer.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { isEmpty, reduce, isObject } from 'lodash'; +import { isEmpty, reduce, isObject, isEqual, pickBy } from 'lodash'; import { html as beautifyHtml } from 'js-beautify'; import classnames from 'classnames'; @@ -138,7 +138,12 @@ export function serializeBlock( block ) { saveContent = block.originalContent; } - const saveAttributes = getCommentAttributes( block.attributes, parseBlockAttributes( saveContent, blockType.attributes ) ); + let saveAttributes = getCommentAttributes( block.attributes, parseBlockAttributes( saveContent, blockType.attributes ) ); + + // Remove attributes that are the same as the defaults. + if ( blockType.defaultAttributes ) { + saveAttributes = pickBy( saveAttributes, ( value, key ) => ! isEqual( value, blockType.defaultAttributes[ key ] ) ); + } if ( 'core/more' === blockName ) { return `${ saveAttributes.noTeaser ? '\n' : '' }`; diff --git a/blocks/api/test/serializer.js b/blocks/api/test/serializer.js index 97dda9b7ea30a3..ae477c0e5e2306 100644 --- a/blocks/api/test/serializer.js +++ b/blocks/api/test/serializer.js @@ -181,6 +181,10 @@ describe( 'block serializer', () => { describe( 'serialize()', () => { it( 'should serialize the post content properly', () => { const blockType = { + defaultAttributes: { + foo: true, + bar: false, + }, attributes: ( rawContent ) => { return { content: rawContent, @@ -195,13 +199,14 @@ describe( 'block serializer', () => { { name: 'core/test-block', attributes: { + foo: false, content: 'Ribs & Chicken', stuff: 'left & right -- but ', }, isValid: true, }, ]; - const expectedPostContent = '\n

Ribs & Chicken

\n'; + const expectedPostContent = '\n

Ribs & Chicken

\n'; expect( serialize( blockList ) ).toEqual( expectedPostContent ); } ); diff --git a/blocks/library/text/index.js b/blocks/library/text/index.js index 14b91331d218fd..3286a89d682a47 100644 --- a/blocks/library/text/index.js +++ b/blocks/library/text/index.js @@ -25,6 +25,10 @@ registerBlockType( 'core/text', { category: 'common', + defaultAttributes: { + dropCap: false, + }, + className: false, attributes: { diff --git a/blocks/test/fixtures/core__cover-image.serialized.html b/blocks/test/fixtures/core__cover-image.serialized.html index e9643ec6638976..9a31a18f8cf4c3 100644 --- a/blocks/test/fixtures/core__cover-image.serialized.html +++ b/blocks/test/fixtures/core__cover-image.serialized.html @@ -1,4 +1,4 @@ - +

Guten Berg!

diff --git a/blocks/test/fixtures/core__latest-posts.html b/blocks/test/fixtures/core__latest-posts.html index 99d215f58affad..d3128a66820632 100644 --- a/blocks/test/fixtures/core__latest-posts.html +++ b/blocks/test/fixtures/core__latest-posts.html @@ -1 +1 @@ - + diff --git a/blocks/test/fixtures/core__latest-posts.json b/blocks/test/fixtures/core__latest-posts.json index 153471c7c68270..303d110f0f9215 100644 --- a/blocks/test/fixtures/core__latest-posts.json +++ b/blocks/test/fixtures/core__latest-posts.json @@ -6,7 +6,7 @@ "attributes": { "columns": 3, "postsToShow": 5, - "displayPostDate": false, + "displayPostDate": true, "layout": "list" } } diff --git a/blocks/test/fixtures/core__latest-posts.parsed.json b/blocks/test/fixtures/core__latest-posts.parsed.json index ac3a8dba1cc674..c0b53e30ca56b6 100644 --- a/blocks/test/fixtures/core__latest-posts.parsed.json +++ b/blocks/test/fixtures/core__latest-posts.parsed.json @@ -3,7 +3,7 @@ "blockName": "core/latest-posts", "attrs": { "postsToShow": 5, - "displayPostDate": false + "displayPostDate": true }, "rawContent": "" }, diff --git a/blocks/test/fixtures/core__latest-posts.serialized.html b/blocks/test/fixtures/core__latest-posts.serialized.html index fba664016ae3c6..ab37368489fdaf 100644 --- a/blocks/test/fixtures/core__latest-posts.serialized.html +++ b/blocks/test/fixtures/core__latest-posts.serialized.html @@ -1 +1 @@ - + diff --git a/blocks/test/fixtures/core__text__align-right.json b/blocks/test/fixtures/core__text__align-right.json index b3d85a9604391d..73496b8ab977a3 100644 --- a/blocks/test/fixtures/core__text__align-right.json +++ b/blocks/test/fixtures/core__text__align-right.json @@ -9,7 +9,8 @@ [ "... like this one, which is separate from the above and right aligned." ] - ] + ], + "dropCap": false } } ] From 35f81dd10a8b5bf1f92ccb8d4c21f56ab63e3c8f Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 14 Jul 2017 22:44:35 -0700 Subject: [PATCH 2/3] Prevent Text block from getting false or empty className --- blocks/library/text/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blocks/library/text/index.js b/blocks/library/text/index.js index 3286a89d682a47..723ebcb20b0ef8 100644 --- a/blocks/library/text/index.js +++ b/blocks/library/text/index.js @@ -113,7 +113,7 @@ registerBlockType( 'core/text', { save( { attributes } ) { const { align, content, dropCap } = attributes; - const className = dropCap && 'has-drop-cap'; + const className = dropCap ? 'has-drop-cap' : null; if ( ! align ) { return

{ content }

; From d6c8fd40614f9e75962ea6c9737fc79b1b6b8bf2 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 28 Jul 2017 12:28:29 -0700 Subject: [PATCH 3/3] Use createBlock to define default attributes --- blocks/api/test/serializer.js | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/blocks/api/test/serializer.js b/blocks/api/test/serializer.js index ae477c0e5e2306..9e80e5981c42e4 100644 --- a/blocks/api/test/serializer.js +++ b/blocks/api/test/serializer.js @@ -13,6 +13,7 @@ import serialize, { serializeAttributes, } from '../serializer'; import { getBlockTypes, registerBlockType, unregisterBlockType } from '../registration'; +import { createBlock } from '../'; describe( 'block serializer', () => { afterEach( () => { @@ -195,20 +196,15 @@ describe( 'block serializer', () => { }, }; registerBlockType( 'core/test-block', blockType ); - const blockList = [ - { - name: 'core/test-block', - attributes: { - foo: false, - content: 'Ribs & Chicken', - stuff: 'left & right -- but ', - }, - isValid: true, - }, - ]; + + const block = createBlock( 'core/test-block', { + foo: false, + content: 'Ribs & Chicken', + stuff: 'left & right -- but ', + } ); const expectedPostContent = '\n

Ribs & Chicken

\n'; - expect( serialize( blockList ) ).toEqual( expectedPostContent ); + expect( serialize( [ block ] ) ).toEqual( expectedPostContent ); } ); } ); } );