From e19d6fcb6c2d79c90413e3558f68475ac75c7fad Mon Sep 17 00:00:00 2001 From: Huzaifa Al Mesbah Date: Sat, 19 Sep 2026 01:57:17 +0600 Subject: [PATCH 1/2] Add table caption validation check for core/table block - Register check_table_caption under core/table in Core_Block_Validation_Rules at warning level. - Implement validation in core-block-rules/table.ts to check for non-empty caption text (WCAG 1.3.1). Closes #28 --- .../Features/Core_Block_Validation_Rules.php | 12 ++++++ src/editor/core-block-rules/table.ts | 37 +++++++++++++------ 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/includes/Modules/Features/Core_Block_Validation_Rules.php b/includes/Modules/Features/Core_Block_Validation_Rules.php index bb23b35..d1212df 100644 --- a/includes/Modules/Features/Core_Block_Validation_Rules.php +++ b/includes/Modules/Features/Core_Block_Validation_Rules.php @@ -269,6 +269,18 @@ private function register_table_checks(): void { 'warning_msg' => __( 'Consider adding table headers.', 'accessibility-lab' ), ) ); + validation_api_register_block_check( + 'core/table', + array( + 'namespace' => self::NS, + 'name' => 'check_table_caption', + 'title' => __( 'Table caption recommended', 'accessibility-lab' ), + 'level' => 'warning', + 'description' => __( 'Tables should have a caption to describe their purpose or content to screen reader users.', 'accessibility-lab' ), + 'error_msg' => __( 'This table is missing a caption.', 'accessibility-lab' ), + 'warning_msg' => __( 'Consider adding a caption to this table.', 'accessibility-lab' ), + ) + ); } /** diff --git a/src/editor/core-block-rules/table.ts b/src/editor/core-block-rules/table.ts index 60478c8..63161a9 100644 --- a/src/editor/core-block-rules/table.ts +++ b/src/editor/core-block-rules/table.ts @@ -12,6 +12,10 @@ type TableAttributes = { caption?: string; }; +function stripHtml( html: string ): string { + return html.replace( /<[^>]*>/g, '' ).trim(); +} + addFilter( 'editor.validateBlock', 'accessibility-lab-core-blocks/table', @@ -25,19 +29,28 @@ addFilter( return isValid; } - if ( checkName !== 'check_table_headers' ) { - return isValid; - } + switch ( checkName ) { + case 'check_table_headers': { + const hasHead = + Array.isArray( attributes.head ) && + attributes.head.length > 0; + if ( hasHead ) { + return true; + } + const firstRow = attributes.body?.[ 0 ]; + if ( firstRow?.cells?.every( ( c ) => c.tag === 'th' ) ) { + return true; + } + return false; + } - const hasHead = - Array.isArray( attributes.head ) && attributes.head.length > 0; - if ( hasHead ) { - return true; - } - const firstRow = attributes.body?.[ 0 ]; - if ( firstRow?.cells?.every( ( c ) => c.tag === 'th' ) ) { - return true; + case 'check_table_caption': { + const caption = stripHtml( String( attributes.caption ?? '' ) ); + return caption.length > 0; + } + + default: + return isValid; } - return false; } ); From a257d82c2d8587377504a944cb0803a48e14067f Mon Sep 17 00:00:00 2001 From: Huzaifa Al Mesbah Date: Fri, 9 Oct 2026 10:15:25 +0600 Subject: [PATCH 2/2] Default table caption check to none and ignore non-breaking spaces Table captions are a sufficient technique (H39) for WCAG 1.3.1 rather than a requirement, so leave the check disabled by default and let site owners enable it in Validation settings. Also treat captions containing only non-breaking spaces as empty. --- includes/Modules/Features/Core_Block_Validation_Rules.php | 2 +- src/editor/core-block-rules/table.ts | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/includes/Modules/Features/Core_Block_Validation_Rules.php b/includes/Modules/Features/Core_Block_Validation_Rules.php index d1212df..0dabd55 100644 --- a/includes/Modules/Features/Core_Block_Validation_Rules.php +++ b/includes/Modules/Features/Core_Block_Validation_Rules.php @@ -275,7 +275,7 @@ private function register_table_checks(): void { 'namespace' => self::NS, 'name' => 'check_table_caption', 'title' => __( 'Table caption recommended', 'accessibility-lab' ), - 'level' => 'warning', + 'level' => 'none', 'description' => __( 'Tables should have a caption to describe their purpose or content to screen reader users.', 'accessibility-lab' ), 'error_msg' => __( 'This table is missing a caption.', 'accessibility-lab' ), 'warning_msg' => __( 'Consider adding a caption to this table.', 'accessibility-lab' ), diff --git a/src/editor/core-block-rules/table.ts b/src/editor/core-block-rules/table.ts index 63161a9..e9ffa55 100644 --- a/src/editor/core-block-rules/table.ts +++ b/src/editor/core-block-rules/table.ts @@ -13,7 +13,10 @@ type TableAttributes = { }; function stripHtml( html: string ): string { - return html.replace( /<[^>]*>/g, '' ).trim(); + return html + .replace( /<[^>]*>/g, '' ) + .replace( / |\u00a0/g, ' ' ) + .trim(); } addFilter(