Accessibility: Media: Make screen readers announce the Media search results.

- adds audible messages via `wp.a11y.speak()` to announce the search results 
- removes a `keyup` event that was there only to support old browsers 
- removes `aria-modal="true"` from the media modal dialog as it prevents the ARIA live regions to be perceived by screen readers
- triggers the search only after 2 ASCII characters have been typed

Props anevins, antpb, aduth, loreleiaurora, afercia.
Fixes #47146.

Built from https://develop.svn.wordpress.org/trunk@46239


git-svn-id: http://core.svn.wordpress.org/trunk@46051 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrea Fercia 2019-09-23 12:19:56 +00:00
parent 69bb09df6e
commit 9e26d98d7e
8 changed files with 58 additions and 15 deletions

View File

@ -817,6 +817,8 @@ var Attachments = Backbone.Collection.extend(/** @lends wp.media.model.Attachmen
this.reset( [], { silent: true } ); this.reset( [], { silent: true } );
this.observe( attachments ); this.observe( attachments );
// Used for the search results.
this.trigger( 'attachments:received', this );
return this; return this;
}, },
/** /**
@ -856,6 +858,9 @@ var Attachments = Backbone.Collection.extend(/** @lends wp.media.model.Attachmen
if ( this === attachments.mirroring ) { if ( this === attachments.mirroring ) {
deferred.resolveWith( this ); deferred.resolveWith( this );
} }
// Used for the search results.
attachments.trigger( 'attachments:received', this );
}); });
return deferred.promise(); return deferred.promise();

File diff suppressed because one or more lines are too long

View File

@ -4593,8 +4593,9 @@ var FocusManager = wp.media.View.extend(/** @lends wp.media.view.FocusManager.pr
* provided element and other elements that should not be hidden. * provided element and other elements that should not be hidden.
* *
* The reason why we use `aria-hidden` is that `aria-modal="true"` is buggy * The reason why we use `aria-hidden` is that `aria-modal="true"` is buggy
* in Safari 11.1 and support is spotty in other browsers. In the future we * in Safari 11.1 and support is spotty in other browsers. Also, `aria-modal="true"`
* should consider to remove this helper function and only use `aria-modal="true"`. * prevents the `wp.a11y.speak()` ARIA live regions to work as they're outside
* of the modal dialog and get hidden from assistive technologies.
* *
* @since 5.2.3 * @since 5.2.3
* *
@ -7291,8 +7292,7 @@ Search = wp.media.View.extend(/** @lends wp.media.view.Search.prototype */{
}, },
events: { events: {
'input': 'search', 'input': 'search'
'keyup': 'search'
}, },
/** /**
@ -7304,12 +7304,15 @@ Search = wp.media.View.extend(/** @lends wp.media.view.Search.prototype */{
}, },
search: _.debounce( function( event ) { search: _.debounce( function( event ) {
if ( event.target.value ) { var searchTerm = event.target.value.trim();
this.model.set( 'search', event.target.value );
// Trigger the search only after 2 ASCII characters.
if ( searchTerm && searchTerm.length > 1 ) {
this.model.set( 'search', searchTerm );
} else { } else {
this.model.unset( 'search' ); this.model.unset( 'search' );
} }
}, 300 ) }, 500 )
}); });
module.exports = Search; module.exports = Search;
@ -7732,8 +7735,40 @@ AttachmentsBrowser = View.extend(/** @lends wp.media.view.AttachmentsBrowser.pro
} }
this.collection.on( 'add remove reset', this.updateContent, this ); this.collection.on( 'add remove reset', this.updateContent, this );
// The non-cached or cached attachments query has completed.
this.collection.on( 'attachments:received', this.announceSearchResults, this );
}, },
/**
* Updates the `wp.a11y.speak()` ARIA live region with a message to communicate
* the number of search results to screen reader users. This function is
* debounced because the collection updates multiple times.
*
* @since 5.3.0
*
* @returns {void}
*/
announceSearchResults: _.debounce( function() {
var count;
if ( this.collection.mirroring.args.s ) {
count = this.collection.length;
if ( 0 === count ) {
wp.a11y.speak( l10n.noMediaTryNewSearch );
return;
}
if ( this.collection.hasMore() ) {
wp.a11y.speak( l10n.mediaFoundHasMoreResults.replace( '%d', count ) );
return;
}
wp.a11y.speak( l10n.mediaFound.replace( '%d', count ) );
}
}, 200 ),
editSelection: function( modal ) { editSelection: function( modal ) {
// When editing a selection, move focus to the "Return to library" button. // When editing a selection, move focus to the "Return to library" button.
modal.$( '.media-button-backToLibrary' ).focus(); modal.$( '.media-button-backToLibrary' ).focus();

File diff suppressed because one or more lines are too long

View File

@ -187,7 +187,7 @@ function wp_print_media_templates() {
<?php // Template for the media modal. ?> <?php // Template for the media modal. ?>
<script type="text/html" id="tmpl-media-modal"> <script type="text/html" id="tmpl-media-modal">
<div tabindex="0" class="<?php echo $class; ?>" role="dialog" aria-modal="true" aria-labelledby="media-frame-title"> <div tabindex="0" class="<?php echo $class; ?>" role="dialog" aria-labelledby="media-frame-title">
<# if ( data.hasCloseButton ) { #> <# if ( data.hasCloseButton ) { #>
<button type="button" class="media-modal-close"><span class="media-modal-icon"><span class="screen-reader-text"><?php _e( 'Close dialog' ); ?></span></span></button> <button type="button" class="media-modal-close"><span class="media-modal-icon"><span class="screen-reader-text"><?php _e( 'Close dialog' ); ?></span></span></button>
<# } #> <# } #>

View File

@ -3854,7 +3854,10 @@ function wp_enqueue_media( $args = array() ) {
'filterByType' => __( 'Filter by type' ), 'filterByType' => __( 'Filter by type' ),
'searchMediaLabel' => __( 'Search Media' ), 'searchMediaLabel' => __( 'Search Media' ),
'searchMediaPlaceholder' => __( 'Search media items...' ), // placeholder (no ellipsis) 'searchMediaPlaceholder' => __( 'Search media items...' ), // placeholder (no ellipsis)
'noMedia' => __( 'No media files found.' ), 'mediaFound' => __( 'Number of media items found: %d' ),
'mediaFoundHasMoreResults' => __( 'Number of media items displayed: %d. Scroll the page for more results.' ),
'noMedia' => __( 'No media items found.' ),
'noMediaTryNewSearch' => __( 'No media items found. Try a different search.' ),
// Library Details // Library Details
'attachmentDetails' => __( 'Attachment Details' ), 'attachmentDetails' => __( 'Attachment Details' ),

View File

@ -1624,7 +1624,7 @@ function wp_default_scripts( &$scripts ) {
// To enqueue media-views or media-editor, call wp_enqueue_media(). // To enqueue media-views or media-editor, call wp_enqueue_media().
// Both rely on numerous settings, styles, and templates to operate correctly. // Both rely on numerous settings, styles, and templates to operate correctly.
$scripts->add( 'media-views', "/wp-includes/js/media-views$suffix.js", array( 'utils', 'media-models', 'wp-plupload', 'jquery-ui-sortable', 'wp-mediaelement', 'wp-api-request' ), false, 1 ); $scripts->add( 'media-views', "/wp-includes/js/media-views$suffix.js", array( 'utils', 'media-models', 'wp-plupload', 'jquery-ui-sortable', 'wp-mediaelement', 'wp-api-request', 'wp-a11y' ), false, 1 );
$scripts->add( 'media-editor', "/wp-includes/js/media-editor$suffix.js", array( 'shortcode', 'media-views' ), false, 1 ); $scripts->add( 'media-editor', "/wp-includes/js/media-editor$suffix.js", array( 'shortcode', 'media-views' ), false, 1 );
$scripts->add( 'media-audiovideo', "/wp-includes/js/media-audiovideo$suffix.js", array( 'media-editor' ), false, 1 ); $scripts->add( 'media-audiovideo', "/wp-includes/js/media-audiovideo$suffix.js", array( 'media-editor' ), false, 1 );
$scripts->add( 'mce-view', "/wp-includes/js/mce-view$suffix.js", array( 'shortcode', 'jquery', 'media-views', 'media-audiovideo' ), false, 1 ); $scripts->add( 'mce-view', "/wp-includes/js/mce-view$suffix.js", array( 'shortcode', 'jquery', 'media-views', 'media-audiovideo' ), false, 1 );

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.3-alpha-46238'; $wp_version = '5.3-alpha-46239';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.