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.


git-svn-id: https://develop.svn.wordpress.org/trunk@44767 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrea Fercia 2019-02-23 18:31:05 +00:00
parent 3086279986
commit 4e02973bc1
2 changed files with 53 additions and 25 deletions

View File

@ -175,34 +175,57 @@ class WP_Widget_Media_Gallery extends WP_Widget_Media {
parent::render_control_template_scripts();
?>
<script type="text/html" id="tmpl-wp-media-widget-gallery-preview">
<# var describedById = 'describedBy-' + String( Math.random() ); #>
<#
var ids = _.filter( data.ids, function( id ) {
return ( id in data.attachments );
} );
#>
<# if ( ids.length ) { #>
<div class="gallery media-widget-gallery-preview">
<ul class="gallery media-widget-gallery-preview" role="list">
<# _.each( ids, function( id, index ) { #>
<# var attachment = data.attachments[ id ]; #>
<# if ( index < 6 ) { #>
<dl class="gallery-item">
<dt class="gallery-icon">
<# if ( attachment.sizes.thumbnail ) { #>
<img src="{{ attachment.sizes.thumbnail.url }}" width="{{ attachment.sizes.thumbnail.width }}" height="{{ attachment.sizes.thumbnail.height }}" alt="" />
<# } else { #>
<img src="{{ attachment.url }}" alt="" />
<# } #>
<# if ( index === 5 && ids.length > 6 ) { #>
<li class="gallery-item">
<div class="gallery-icon">
<img alt="{{ attachment.alt }}"
<# if ( index === 5 && data.ids.length > 6 ) { #> aria-hidden="true" <# } #>
<# if ( attachment.sizes.thumbnail ) { #>
src="{{ attachment.sizes.thumbnail.url }}" width="{{ attachment.sizes.thumbnail.width }}" height="{{ attachment.sizes.thumbnail.height }}"
<# } else { #>
src="{{ attachment.url }}"
<# } #>
<# 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">
<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>
<# } #>
</dt>
</dl>
<# } #>
</div>
</li>
<# } #>
<# } ); #>
</div>
</ul>
<# } else { #>
<div class="attachment-media-view">
<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 type="text/html" id="tmpl-wp-media-widget-image-preview">
<# var describedById = 'describedBy-' + String( Math.random() ); #>
<# if ( data.error && 'missing_attachment' === data.error ) { #>
<div class="notice notice-error notice-alt notice-missing-attachment">
<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>
</div>
<# } else if ( data.url ) { #>
<img class="attachment-thumb" src="{{ data.url }}" draggable="false" alt="{{ data.alt }}" <# if ( ! data.alt && data.currentFilename ) { #> aria-describedby="{{ describedById }}" <# } #> />
<# if ( ! data.alt && data.currentFilename ) { #>
<p class="hidden" id="{{ describedById }}">
<?php
/* translators: %s: image filename */
echo sprintf( __( 'Current image: %s' ), '{{ data.currentFilename }}' );
?>
</p>
<# } #>
<img class="attachment-thumb" src="{{ data.url }}" draggable="false" alt="{{ data.alt }}"
<# if ( ! data.alt && data.currentFilename ) { #>
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' ),
'{{ data.currentFilename }}'
)
);
?>
"
<# } #>
/>
<# } #>
</script>
<?php