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.
Built from https://develop.svn.wordpress.org/trunk@38812


git-svn-id: http://core.svn.wordpress.org/trunk@38755 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Joe McGill 2016-10-19 03:06:28 +00:00
parent e8bd9645df
commit 8ed022b568
4 changed files with 6 additions and 29 deletions

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

File diff suppressed because one or more lines are too long

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 );

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.7-alpha-38811';
$wp_version = '4.7-alpha-38812';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.