Skip to content

Commit 1c412fb

Browse files
committed
Update logic and unit test cases
1 parent 5beef85 commit 1c412fb

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/wp-includes/block-supports/anchor.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ function wp_apply_anchor_support( $block_type, $block_attributes ) {
5454
}
5555

5656
$has_anchor = array_key_exists( 'anchor', $block_attributes );
57-
if ( ! $has_anchor || empty( $block_attributes['anchor'] ) ) {
57+
if ( ! $has_anchor ) {
58+
return array();
59+
}
60+
61+
$anchor_value = (string) $block_attributes['anchor'];
62+
if ( '' === $anchor_value ) {
5863
return array();
5964
}
6065

tests/phpunit/tests/block-supports/anchor..php

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function test_wp_apply_anchor_support( $support, $value, $expected ) {
7070
*/
7171
public function data_anchor_block_support() {
7272
return array(
73-
'anchor id attribute is applied' => array(
73+
'anchor id attribute is applied' => array(
7474
'support' => true,
7575
'value' => 'my-anchor',
7676
'expected' => array( 'id' => 'my-anchor' ),
@@ -80,6 +80,56 @@ public function data_anchor_block_support() {
8080
'value' => 'my-anchor',
8181
'expected' => array(),
8282
),
83+
'empty anchor value returns empty array' => array(
84+
'support' => true,
85+
'value' => '',
86+
'expected' => array(),
87+
),
88+
'null anchor value returns empty array' => array(
89+
'support' => true,
90+
'value' => null,
91+
'expected' => array(),
92+
),
93+
'whitespace-only anchor value is applied' => array(
94+
'support' => true,
95+
'value' => ' ',
96+
'expected' => array( 'id' => ' ' ),
97+
),
98+
'anchor with hyphen and numbers' => array(
99+
'support' => true,
100+
'value' => 'section-123',
101+
'expected' => array( 'id' => 'section-123' ),
102+
),
103+
'anchor with underscore' => array(
104+
'support' => true,
105+
'value' => 'my_anchor_id',
106+
'expected' => array( 'id' => 'my_anchor_id' ),
107+
),
108+
'anchor with colon (valid in HTML5)' => array(
109+
'support' => true,
110+
'value' => 'my:anchor',
111+
'expected' => array( 'id' => 'my:anchor' ),
112+
),
113+
'anchor with period (valid in HTML5)' => array(
114+
'support' => true,
115+
'value' => 'my.anchor',
116+
'expected' => array( 'id' => 'my.anchor' ),
117+
),
118+
'numeric anchor value' => array(
119+
'support' => true,
120+
'value' => '123',
121+
'expected' => array( 'id' => '123' ),
122+
),
123+
'zero string anchor value is applied' => array(
124+
'support' => true,
125+
'value' => '0',
126+
'expected' => array( 'id' => '0' ),
127+
), 1Code has comments. Press enter to view.
128+
'false value is treated as empty' => array(
129+
'support' => true,
130+
'value' => false,
131+
'expected' => array(),
132+
),
83133
);
84134
}
85135
}

0 commit comments

Comments
 (0)