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

Omit serializing block attributes that match any default attribute values #1910

Merged
merged 3 commits into from
Jul 29, 2017

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented Jul 15, 2017

Fixes #1768.

Before the patch, the Text block that gets dropCap toggled from true to false gets serialized as:

image

With the changes, it gets serialized as (note the absence of the default value among the serialized attributes), and the fixed class in the Text block:

image

When dropCap is toggled back on, it gets serialized to:

image

@aduth aduth self-requested a review July 20, 2017 12:54
@westonruter westonruter force-pushed the fix/omit-serializing-default-attributes branch from d14e0b5 to 1b10e54 Compare July 25, 2017 20:35
@westonruter
Copy link
Member Author

Rebased. d14e0b5 rewritten to 1b10e54.

@westonruter
Copy link
Member Author

Working on resolving the conflicts…

@aduth
Copy link
Member

aduth commented Jul 27, 2017

I'd been waiting to review this pending #1905, but I think it might be good to get this in and I can rebase #1905 separately. Let me know when conflicts are resolved and I'll take a look.

@westonruter
Copy link
Member Author

@aduth before I work on fixing the conflict, does this change get a 👍?

Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

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

Seems like a good idea.


// 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.

Copy link
Contributor

@timmyc timmyc left a comment

Choose a reason for hiding this comment

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

I gave this branch a spin too and it seems like a great change. One minor comment.

@@ -108,7 +112,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.

@westonruter westonruter force-pushed the fix/omit-serializing-default-attributes branch from 1b10e54 to 03e92f1 Compare July 27, 2017 23:42
@westonruter
Copy link
Member Author

Rebased. 1b10e54 rewritten as 03e92f1.

@codecov-io
Copy link

codecov-io commented Jul 27, 2017

Codecov Report

Merging #1910 into master will increase coverage by 0.03%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1910      +/-   ##
==========================================
+ Coverage   20.21%   20.25%   +0.03%     
==========================================
  Files         135      135              
  Lines        4225     4227       +2     
  Branches      717      718       +1     
==========================================
+ Hits          854      856       +2     
  Misses       2842     2842              
  Partials      529      529
Impacted Files Coverage Δ
blocks/library/text/index.js 41.17% <100%> (ø) ⬆️
blocks/api/serializer.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b6c3a4f...d6c8fd4. Read the comment docs.

aduth
aduth previously requested changes Jul 28, 2017

// 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.

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.

@@ -195,13 +199,14 @@ describe( 'block serializer', () => {
{
name: 'core/test-block',
attributes: {
foo: false,
Copy link
Member

Choose a reason for hiding this comment

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

One of two things should be done here:

  • We should set a bar: false value in the attributes here. As is we're not really testing that the default value is omitted, because the attribute isn't set anyways.
  • We should use createBlock to construct the block object and rely on its behavior to set the defaults into attributes.

My preference is toward the latter.

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 thanks for the feedback. Done in 71a07fc9bac6348a28545ad9cfe23e91c6c50a26.

Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

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

By failing test, it appears we need to update markup for cover image fixture.

Otherwise LGTM 👍

@westonruter westonruter force-pushed the fix/omit-serializing-default-attributes branch from 71a07fc to d6c8fd4 Compare July 29, 2017 02:59
@westonruter
Copy link
Member Author

Thanks, I amended that cover-image block fix onto d272cbd.

@westonruter westonruter dismissed aduth’s stale review July 29, 2017 03:05

Fixed failing test.

@westonruter westonruter merged commit 03f8fe6 into master Jul 29, 2017
@westonruter westonruter deleted the fix/omit-serializing-default-attributes branch July 29, 2017 03:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dropcap tag doesn't get cleaned up
4 participants