Skip to content
Merged
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
9 changes: 7 additions & 2 deletions blocks/api/serializer.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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 ] ) );
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need (or want) this to be a deep equality isEqual, but rather just strict equality !==

Copy link
Member Author

Choose a reason for hiding this comment

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

What about when an attribute contain an object? Then this will fail !== matching.

Copy link
Member Author

Choose a reason for hiding this comment

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

In other words, value !== blockType.defaultAttributes[ key ] will always be true for objects.

Copy link
Member Author

Choose a reason for hiding this comment

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

@aduth 👍?

Copy link
Member

Choose a reason for hiding this comment

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

In other words, value !== blockType.defaultAttributes[ key ] will always be true for objects.

Well, not necessarily; not if the attribute value is never updated. But it's a fair point that if they are updated, and their nested structure matches the default value, it should probably be omitted all the same. I'm fine with isEqual.

}

if ( 'core/more' === blockName ) {
return `<!--more${ saveAttributes.text ? ` ${ saveAttributes.text }` : '' }-->${ saveAttributes.noTeaser ? '\n<!--noteaser-->' : '' }`;
Expand Down
25 changes: 13 additions & 12 deletions blocks/api/test/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import serialize, {
serializeAttributes,
} from '../serializer';
import { getBlockTypes, registerBlockType, unregisterBlockType } from '../registration';
import { createBlock } from '../';

describe( 'block serializer', () => {
afterEach( () => {
Expand Down Expand Up @@ -181,6 +182,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,
Expand All @@ -191,19 +196,15 @@ describe( 'block serializer', () => {
},
};
registerBlockType( 'core/test-block', blockType );
const blockList = [
{
name: 'core/test-block',
attributes: {
content: 'Ribs & Chicken',
stuff: 'left & right -- but <not>',
},
isValid: true,
},
];
const expectedPostContent = '<!-- wp:core/test-block {"stuff":"left \\u0026 right \\u002d\\u002d but \\u003cnot\\u003e"} -->\n<p class="wp-block-test-block">Ribs & Chicken</p>\n<!-- /wp:core/test-block -->';

expect( serialize( blockList ) ).toEqual( expectedPostContent );
const block = createBlock( 'core/test-block', {
foo: false,
content: 'Ribs & Chicken',
stuff: 'left & right -- but <not>',
} );
const expectedPostContent = '<!-- wp:core/test-block {"foo":false,"stuff":"left \\u0026 right \\u002d\\u002d but \\u003cnot\\u003e"} -->\n<p class="wp-block-test-block">Ribs & Chicken</p>\n<!-- /wp:core/test-block -->';

expect( serialize( [ block ] ) ).toEqual( expectedPostContent );
} );
} );
} );
6 changes: 5 additions & 1 deletion blocks/library/text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ registerBlockType( 'core/text', {

category: 'common',

defaultAttributes: {
dropCap: false,
},

className: false,

attributes: {
Expand Down Expand Up @@ -109,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;
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason to not just do this inline like return <p className={ dropCap ? 'has-drop-cap' : null }

Copy link
Member Author

Choose a reason for hiding this comment

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

No reason, other than it was using a className var to begin with.


if ( ! align ) {
return <p className={ className }>{ content }</p>;
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core__cover-image.serialized.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- wp:core/cover-image {"hasBackgroundDim":true,"url":"https://cldup.com/uuUqE_dXzy.jpg"} -->
<!-- wp:core/cover-image {"url":"https://cldup.com/uuUqE_dXzy.jpg"} -->
<section class="wp-block-cover-image has-background-dim" style="background-image:url(https://cldup.com/uuUqE_dXzy.jpg);">
<h2>Guten Berg!</h2>
</section>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core__latest-posts.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!-- wp:core/latest-posts {"postsToShow":5,"displayPostDate":false} /-->
<!-- wp:core/latest-posts {"postsToShow":5,"displayPostDate":true} /-->
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core__latest-posts.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"attributes": {
"columns": 3,
"postsToShow": 5,
"displayPostDate": false,
"displayPostDate": true,
"layout": "list"
}
}
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core__latest-posts.parsed.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"blockName": "core/latest-posts",
"attrs": {
"postsToShow": 5,
"displayPostDate": false
"displayPostDate": true
},
"rawContent": ""
},
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core__latest-posts.serialized.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!-- wp:core/latest-posts {"postsToShow":5,"displayPostDate":false,"layout":"list","columns":3} /-->
<!-- wp:core/latest-posts {"displayPostDate":true} /-->
3 changes: 2 additions & 1 deletion blocks/test/fixtures/core__text__align-right.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
[
"... like this one, which is separate from the above and right aligned."
]
]
],
"dropCap": false
}
}
]