Media: Remove alt fallbacks to improve accessibility.

This removes the fallbacks in `wp_get_attachment_image()` and in
`wp.media.string.props` which attempt to generate an `alt` value
from the image caption or title if an `alt` attribute isn't explicitly
set.

This allows for image HTML to be generated that contains an empty `alt`
value, i.e., `alt=""` which is much preferable for screen readers than
reading redundant content in the case of a caption, or when reading the
image title, which is often generated from the filename and not helpful
as `alt` text.

Props odie2, joedolson, rianrietveld, afercia, iamjolly, joemcgill.
Fixes #34635.

git-svn-id: https://develop.svn.wordpress.org/trunk@38812 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Joe McGill
2016-10-19 03:05:51 +00:00
parent 500041a4bc
commit 788bb093f2
3 changed files with 33 additions and 28 deletions
+3 -22
View File
@@ -48,28 +48,9 @@
* @returns {Object} Joined props
*/
props: function( props, attachment ) {
var link, linkUrl, size, sizes, fallbacks,
var link, linkUrl, size, sizes,
defaultProps = wp.media.view.settings.defaultProps;
// Final fallbacks run after all processing has been completed.
fallbacks = function( props ) {
// Generate alt fallbacks and strip tags.
if ( 'image' === props.type && ! props.alt ) {
if ( props.caption ) {
props.alt = props.caption;
} else if ( props.title !== props.url ) {
props.alt = props.title;
} else {
props.alt = '';
}
props.alt = props.alt.replace( /<\/?[^>]+>/g, '' );
props.alt = props.alt.replace( /[\r\n]+/g, ' ' );
}
return props;
};
props = props ? _.clone( props ) : {};
if ( attachment && attachment.type ) {
@@ -87,7 +68,7 @@
// All attachment-specific settings follow.
if ( ! attachment ) {
return fallbacks( props );
return props;
}
props.title = props.title || attachment.title;
@@ -123,7 +104,7 @@
props.rel = props.rel || 'attachment wp-att-' + attachment.id;
}
return fallbacks( props );
return props;
},
/**
* Create link markup that is suitable for passing to the editor
+1 -5
View File
@@ -866,12 +866,8 @@ function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = fa
$default_attr = array(
'src' => $src,
'class' => "attachment-$size_class size-$size_class",
'alt' => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )), // Use Alt field first
'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ),
);
if ( empty($default_attr['alt']) )
$default_attr['alt'] = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption
if ( empty($default_attr['alt']) )
$default_attr['alt'] = trim(strip_tags( $attachment->post_title )); // Finally, use the title
$attr = wp_parse_args( $attr, $default_attr );