-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Conversation
d14e0b5
to
1b10e54
Compare
Rebased. d14e0b5 rewritten to 1b10e54. |
Working on resolving the conflicts… |
@aduth before I work on fixing the conflict, does this change get a 👍? |
There was a problem hiding this 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 ] ) ); |
There was a problem hiding this comment.
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 !==
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aduth 👍?
There was a problem hiding this comment.
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 betrue
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
.
There was a problem hiding this 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; |
There was a problem hiding this comment.
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 }
There was a problem hiding this comment.
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.
1b10e54
to
03e92f1
Compare
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
|
||
// Remove attributes that are the same as the defaults. | ||
if ( blockType.defaultAttributes ) { | ||
saveAttributes = pickBy( saveAttributes, ( value, key ) => ! isEqual( value, blockType.defaultAttributes[ key ] ) ); |
There was a problem hiding this comment.
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 betrue
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
.
blocks/api/test/serializer.js
Outdated
@@ -195,13 +199,14 @@ describe( 'block serializer', () => { | |||
{ | |||
name: 'core/test-block', | |||
attributes: { | |||
foo: false, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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 👍
71a07fc
to
d6c8fd4
Compare
Thanks, I amended that |
Fixes #1768.
Before the patch, the Text block that gets
dropCap
toggled fromtrue
tofalse
gets serialized as: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:When dropCap is toggled back on, it gets serialized to: