diff --git a/packages/block-library/src/table-of-contents/index.php b/packages/block-library/src/table-of-contents/index.php
index 744829d3ee05d..3a6ce2e5f2a4e 100644
--- a/packages/block-library/src/table-of-contents/index.php
+++ b/packages/block-library/src/table-of-contents/index.php
@@ -129,7 +129,7 @@ function block_core_table_of_contents_build_list( $tree, $args ) {
$pagelink = ! empty( $heading['page'] ) ? add_query_arg( 'page', $heading['page'], $permalink ) : $permalink;
$content = '' . esc_html( $heading['content'] ) . '';
} else {
- $content = '' . esc_html( $heading['content'] ) . '';
+ $content = '' . esc_html( $heading['content'] ) . '';
}
$list .= '
' . $content . $children . '';
diff --git a/phpunit/blocks/render-block-table-of-contents-test.php b/phpunit/blocks/render-block-table-of-contents-test.php
index e29c2181813c8..66932e6f328b9 100644
--- a/phpunit/blocks/render-block-table-of-contents-test.php
+++ b/phpunit/blocks/render-block-table-of-contents-test.php
@@ -29,7 +29,7 @@ public static function wpSetUpBeforeClass() {
add_post_meta(
self::$page->ID,
'core_table_of_contents',
- '[{"content":"Heading text","level":2,"link":"#heading-text"},{"content":"A sub-heading","level":3,"link":"#a-sub-heading"}]',
+ '[{"content":"Heading text","level":2,"link":"#heading-text","page":null},{"content":"A sub-heading","level":3,"link":"#a-sub-heading","page":null},{"content":"Missing anchor","level":2,"link":null,"page":null}]',
true
);
}
@@ -111,5 +111,10 @@ public function test_render_table_of_contents_from_meta() {
$new_content,
'Failed to render a sub-heading element from meta'
);
+ $this->assertStringContainsString(
+ 'Missing anchor',
+ $new_content,
+ 'Failed to render a heading element with a missing anchor'
+ );
}
}