-
Notifications
You must be signed in to change notification settings - Fork 3.2k
get_block_wrapper_attributes(): Preserve zero value #10663
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
Open
t-hamano
wants to merge
5
commits into
WordPress:trunk
Choose a base branch
from
t-hamano:64452-preserve-zero-value
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+135
−8
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
af0bdc5
get_block_wrapper_attributes(): Preserve zero value
t-hamano e08f442
Fix phpcs error
t-hamano 1b4f2ed
Update tests/phpunit/tests/blocks/getBlockWrapperAttributes.php
t-hamano d6ac965
Fix PHP lint error
t-hamano 88e61a5
Restore the function location
t-hamano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
tests/phpunit/tests/blocks/getBlockWrapperAttributes.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| <?php | ||
| /** | ||
| * Tests for get_block_wrapper_attributes function. | ||
| * | ||
| * @package WordPress | ||
| * @subpackage Blocks | ||
| * | ||
| * @since 7.0.0 | ||
| * | ||
| * @group blocks | ||
| * @covers ::get_block_wrapper_attributes | ||
| */ | ||
| class Tests_Blocks_GetBlockWrapperAttributes extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Tear down after each test. | ||
| * | ||
| * @since 7.0.0 | ||
| */ | ||
| public function tear_down() { | ||
| $registry = WP_Block_Type_Registry::get_instance(); | ||
| if ( $registry->is_registered( 'core/example' ) ) { | ||
| $registry->unregister( 'core/example' ); | ||
| } | ||
| if ( $registry->is_registered( 'core/example' ) ) { | ||
| $registry->unregister( 'core/example' ); | ||
| } | ||
|
|
||
| parent::tear_down(); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64452 | ||
| */ | ||
| public function test_preserves_zero_values() { | ||
| WP_Block_Supports::init(); | ||
| register_block_type( | ||
| 'core/example', | ||
| array( | ||
| 'supports' => array( | ||
| 'customClassName' => true, | ||
| 'ariaLabel' => true, | ||
| ), | ||
| ) | ||
| ); | ||
| WP_Block_Supports::$block_to_render = array( | ||
| 'blockName' => 'core/example', | ||
| 'attrs' => array( | ||
| 'className' => '0', | ||
| 'ariaLabel' => 0, | ||
| ), | ||
| ); | ||
|
|
||
| $result = get_block_wrapper_attributes(); | ||
t-hamano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $this->assertSame( 'class="0 wp-block-example" aria-label="0"', $result ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64452 | ||
| */ | ||
| public function test_preserves_zero_values_from_extra_attributes() { | ||
| WP_Block_Supports::init(); | ||
| register_block_type( 'core/example' ); | ||
| WP_Block_Supports::$block_to_render = array( 'blockName' => 'core/example' ); | ||
|
|
||
| $result = get_block_wrapper_attributes( | ||
| array( | ||
| 'class' => '0', | ||
| 'aria-label' => 0, | ||
| 'data-foo' => 0, | ||
| 'data-var' => '0', | ||
| ) | ||
| ); | ||
| $this->assertSame( 'class="0 wp-block-example" aria-label="0" data-foo="0" data-var="0"', $result ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64452 | ||
| */ | ||
| public function test_excludes_falsy_values_except_zero() { | ||
| WP_Block_Supports::init(); | ||
| register_block_type( | ||
| 'core/example', | ||
| array( | ||
| 'supports' => array( | ||
| 'customClassName' => true, | ||
| 'ariaLabel' => true, | ||
| ), | ||
| ) | ||
| ); | ||
| WP_Block_Supports::$block_to_render = array( | ||
| 'blockName' => 'core/example', | ||
| 'attrs' => array( | ||
| 'className' => false, | ||
| 'ariaLabel' => null, | ||
| ), | ||
| ); | ||
|
|
||
| $result = get_block_wrapper_attributes(); | ||
| $this->assertSame( 'class="wp-block-example"', $result ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64452 | ||
| */ | ||
| public function test_excludes_falsy_values_except_zero_from_extra_attributes() { | ||
| WP_Block_Supports::init(); | ||
| register_block_type( 'core/example' ); | ||
| WP_Block_Supports::$block_to_render = array( 'blockName' => 'core/example' ); | ||
|
|
||
| $result = get_block_wrapper_attributes( | ||
| array( | ||
| 'class' => false, | ||
| 'aria-label' => null, | ||
| 'data-var' => false, | ||
| 'data-baz' => null, | ||
| ) | ||
| ); | ||
| $this->assertSame( 'class="wp-block-example"', $result ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is the meat of the fix, right? It looks right to me, and cover edge cases:
0(integer):(string) 0="0"≠""→ treated as valid"0"(string):(string) "0"="0"≠""→ treated as valid""(empty string):(string) ""=""=""→ treated as empty (correct)false:(string) false=""=""→ treated as empty (acceptable for HTML attributes)null:(string) null=""=""→ treated as empty (acceptable)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.
That's right. The main reason for using a string cast is to allow zero as a number or a string, but to exclude false or null.