Accessibility: Improve the image and gallery widgets preview accessibility.

In an authoring context, the image `alt` attribute purpose is different from the one for the front end. For example, screen reader users need to know what the selected image is, even when the original `alt` value is empty.

This change introduces a new pattern for the `alt` text in an authoring context:
- uses the `alt` text if not empty
- when there's no `alt` text, informs users the image has no alternative text and provides a reference to the image filename

Also, makes the gallery media widget preview an unordered list to make screen readers announce the number of images automatically.

Fixes #43137.

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


git-svn-id: http://core.svn.wordpress.org/trunk@44599 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrea Fercia 2019-02-23 18:32:53 +00:00
parent 029fcf7791
commit adc7f9fe54
3 changed files with 54 additions and 26 deletions

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.2-alpha-44762'; $wp_version = '5.2-alpha-44767';
/** /**
* 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.

View File

@ -175,34 +175,57 @@ class WP_Widget_Media_Gallery extends WP_Widget_Media {
parent::render_control_template_scripts(); parent::render_control_template_scripts();
?> ?>
<script type="text/html" id="tmpl-wp-media-widget-gallery-preview"> <script type="text/html" id="tmpl-wp-media-widget-gallery-preview">
<# var describedById = 'describedBy-' + String( Math.random() ); #>
<# <#
var ids = _.filter( data.ids, function( id ) { var ids = _.filter( data.ids, function( id ) {
return ( id in data.attachments ); return ( id in data.attachments );
} ); } );
#> #>
<# if ( ids.length ) { #> <# if ( ids.length ) { #>
<div class="gallery media-widget-gallery-preview"> <ul class="gallery media-widget-gallery-preview" role="list">
<# _.each( ids, function( id, index ) { #> <# _.each( ids, function( id, index ) { #>
<# var attachment = data.attachments[ id ]; #> <# var attachment = data.attachments[ id ]; #>
<# if ( index < 6 ) { #> <# if ( index < 6 ) { #>
<dl class="gallery-item"> <li class="gallery-item">
<dt class="gallery-icon"> <div class="gallery-icon">
<img alt="{{ attachment.alt }}"
<# if ( index === 5 && data.ids.length > 6 ) { #> aria-hidden="true" <# } #>
<# if ( attachment.sizes.thumbnail ) { #> <# if ( attachment.sizes.thumbnail ) { #>
<img src="{{ attachment.sizes.thumbnail.url }}" width="{{ attachment.sizes.thumbnail.width }}" height="{{ attachment.sizes.thumbnail.height }}" alt="" /> src="{{ attachment.sizes.thumbnail.url }}" width="{{ attachment.sizes.thumbnail.width }}" height="{{ attachment.sizes.thumbnail.height }}"
<# } else { #> <# } else { #>
<img src="{{ attachment.url }}" alt="" /> src="{{ attachment.url }}"
<# } #> <# } #>
<# if ( index === 5 && ids.length > 6 ) { #> <# if ( ! attachment.alt && attachment.filename ) { #>
aria-label="
<?php
echo esc_attr(
sprintf(
/* translators: %s: the image file name. */
__( 'The current image has no alternative text. The file name is: %s' ),
'{{ attachment.filename }}'
)
);
?>
"
<# } #>
/>
<# if ( index === 5 && data.ids.length > 6 ) { #>
<div class="gallery-icon-placeholder"> <div class="gallery-icon-placeholder">
<p class="gallery-icon-placeholder-text">+{{ ids.length - 5 }}</p> <p class="gallery-icon-placeholder-text" aria-label="
<?php
printf(
/* translators: %s: the amount of additional, not visible images in the gallery widget preview. */
__( 'Additional images added to this gallery: %s' ),
'{{ data.ids.length - 5 }}'
);
?>
">+{{ data.ids.length - 5 }}</p>
</div> </div>
<# } #> <# } #>
</dt> </div>
</dl> </li>
<# } #> <# } #>
<# } ); #> <# } ); #>
</div> </ul>
<# } else { #> <# } else { #>
<div class="attachment-media-view"> <div class="attachment-media-view">
<p class="placeholder"><?php echo esc_html( $this->l10n['no_media_selected'] ); ?></p> <p class="placeholder"><?php echo esc_html( $this->l10n['no_media_selected'] ); ?></p>

View File

@ -339,7 +339,6 @@ class WP_Widget_Media_Image extends WP_Widget_Media {
<# } #> <# } #>
</script> </script>
<script type="text/html" id="tmpl-wp-media-widget-image-preview"> <script type="text/html" id="tmpl-wp-media-widget-image-preview">
<# var describedById = 'describedBy-' + String( Math.random() ); #>
<# if ( data.error && 'missing_attachment' === data.error ) { #> <# if ( data.error && 'missing_attachment' === data.error ) { #>
<div class="notice notice-error notice-alt notice-missing-attachment"> <div class="notice notice-error notice-alt notice-missing-attachment">
<p><?php echo $this->l10n['missing_attachment']; ?></p> <p><?php echo $this->l10n['missing_attachment']; ?></p>
@ -349,15 +348,21 @@ class WP_Widget_Media_Image extends WP_Widget_Media {
<p><?php _e( 'Unable to preview media due to an unknown error.' ); ?></p> <p><?php _e( 'Unable to preview media due to an unknown error.' ); ?></p>
</div> </div>
<# } else if ( data.url ) { #> <# } else if ( data.url ) { #>
<img class="attachment-thumb" src="{{ data.url }}" draggable="false" alt="{{ data.alt }}" <# if ( ! data.alt && data.currentFilename ) { #> aria-describedby="{{ describedById }}" <# } #> /> <img class="attachment-thumb" src="{{ data.url }}" draggable="false" alt="{{ data.alt }}"
<# if ( ! data.alt && data.currentFilename ) { #> <# if ( ! data.alt && data.currentFilename ) { #>
<p class="hidden" id="{{ describedById }}"> aria-label="
<?php <?php
/* translators: %s: image filename */ echo esc_attr(
echo sprintf( __( 'Current image: %s' ), '{{ data.currentFilename }}' ); sprintf(
/* translators: %s: the image file name. */
__( 'The current image has no alternative text. The file name is: %s' ),
'{{ data.currentFilename }}'
)
);
?> ?>
</p> "
<# } #> <# } #>
/>
<# } #> <# } #>
</script> </script>
<?php <?php