Accessibility: Comments: In `comment_form()`, remove `aria-describedby` attribute from the email field if there's no associated description in the `comment_notes_before` argument.

The attribute is meant to communicate to user agents and assistive technologies that the element has a description. If the referenced description is missing, it will be flagged as failure by any automated accessibility checker tool and, most importantly, is potentially confusing for assistive technologies users.

Props afercia, joedolson, dinhtungdu, donmhico, SergeyBiryukov.
Fixes #47975.

git-svn-id: https://develop.svn.wordpress.org/trunk@46090 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2019-09-11 01:50:23 +00:00
parent 26ae8b54d1
commit 8005d291a3
2 changed files with 33 additions and 1 deletions

View File

@ -2428,6 +2428,15 @@ function comment_form( $args = array(), $post_id = null ) {
// Ensure that the filtered args contain all required default values.
$args = array_merge( $defaults, $args );
// Remove aria-describedby from the email field if there's no associated description.
if ( false === strpos( $args['comment_notes_before'], 'id="email-notes"' ) ) {
$args['fields']['email'] = str_replace(
' aria-describedby="email-notes"',
'',
$args['fields']['email']
);
}
/**
* Fires before the comment form.
*
@ -2509,7 +2518,7 @@ function comment_form( $args = array(), $post_id = null ) {
endif;
// Prepare an array of all fields, including the textarea
// Prepare an array of all fields, including the textarea.
$comment_fields = array( 'comment' => $args['comment_field'] ) + (array) $args['fields'];
/**

View File

@ -13,6 +13,7 @@ class Tests_Comment_CommentForm extends WP_UnitTestCase {
'class_submit' => 'foo-class',
'label_submit' => 'foo-label',
);
$form = get_echo( 'comment_form', array( $args, $p ) );
$button = '<input name="foo-name" type="submit" id="foo-id" class="foo-class" value="foo-label" />';
@ -30,6 +31,7 @@ class Tests_Comment_CommentForm extends WP_UnitTestCase {
'label_submit' => 'foo-label',
'submit_button' => '<input name="custom-%1$s" type="submit" id="custom-%2$s" class="custom-%3$s" value="custom-%4$s" />',
);
$form = get_echo( 'comment_form', array( $args, $p ) );
$button = '<input name="custom-foo-name" type="submit" id="custom-foo-id" class="custom-foo-class" value="custom-foo-label" />';
@ -46,6 +48,7 @@ class Tests_Comment_CommentForm extends WP_UnitTestCase {
'label_submit' => 'foo-label',
'submit_field' => '<p class="my-custom-submit-field">%1$s %2$s</p>',
);
$form = get_echo( 'comment_form', array( $args, $p ) );
$button = '<input name="foo-name" type="submit" id="foo-id" class="foo-class" value="foo-label" />';
@ -94,10 +97,30 @@ class Tests_Comment_CommentForm extends WP_UnitTestCase {
'author' => 'Hello World!',
),
);
$form = get_echo( 'comment_form', array( $args, $p ) );
remove_filter( 'option_show_comments_cookies_opt_in', '__return_true' );
$this->assertRegExp( '|<p class="comment\-form\-cookies\-consent">.*?</p>|', $form );
}
/**
* @ticket 47975
*/
public function test_aria_describedby_email_notes_should_not_be_added_if_no_email_notes() {
$p = self::factory()->post->create();
$form_with_aria = get_echo( 'comment_form', array( array(), $p ) );
$this->assertContains( 'aria-describedby="email-notes"', $form_with_aria );
$args = array(
'comment_notes_before' => '',
);
$form_without_aria = get_echo( 'comment_form', array( $args, $p ) );
$this->assertNotContains( 'aria-describedby="email-notes"', $form_without_aria );
}
}