Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds default alt values in editing context #11218

17 changes: 14 additions & 3 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import { Component, Fragment } from '@wordpress/element';
import { getBlobByURL, revokeBlobURL, isBlobURL } from '@wordpress/blob';
import {
Expand Down Expand Up @@ -99,6 +99,7 @@ class ImageEdit extends Component {
this.updateDimensions = this.updateDimensions.bind( this );
this.onSetCustomHref = this.onSetCustomHref.bind( this );
this.onSetLinkDestination = this.onSetLinkDestination.bind( this );
this.getFilename = this.getFilename.bind( this );
this.toggleIsEditing = this.toggleIsEditing.bind( this );

this.state = {
Expand Down Expand Up @@ -246,6 +247,16 @@ class ImageEdit extends Component {
};
}

getFilename( url ) {
if ( url ) {
const fileName = url.match( /.*\/(.+?)(\?.*)?$/ );
if ( fileName ) {
return fileName[ 1 ];
}
}
return '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this produce a nonsense alt value of 'This image has an empty alt attribute; its file name is ' if this return is reached?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aduth Thanks! I do not believe there is an instance that url will not pass given the Media Library is where this is passed back from. I can't imagine how it would not return url. I only added this check as an edge case precaution of a graceful return instead of an error of using an undefined url. Can you think of any edge cases where url would return false? Happy to remove the check; I just want to make sure that I'm not taking away a safety net. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, ideally we're certain the sort of data we're expecting. For something like URL user input, it's a bit harder to nail down. But maybe it's safe to assume that every URL should at least have a / followed by a "filename" (this is what the pattern is checking, yeah? The fact I'm asking is maybe a hint it's not very obvious on its own). If we are going to be uncertain, we should at least follow-through on our uncertainty, where defaultedAlt doesn't perform the substitution at all if the URL can't be determined.

}

getImageSizes() {
antpb marked this conversation as resolved.
Show resolved Hide resolved
return get( this.props.image, [ 'media_details', 'sizes' ], {} );
}
Expand Down Expand Up @@ -475,10 +486,11 @@ class ImageEdit extends Component {
imageHeight,
} = sizes;

const defaultedAlt = alt ? alt : sprintf( __( 'This image has an empty alt attribute; its file name is %s' ), this.getFilename( url ) );
antpb marked this conversation as resolved.
Show resolved Hide resolved
// Disable reason: Image itself is not meant to be
// interactive, but should direct focus to block
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
const img = <img src={ url } alt={ alt } onClick={ this.onImageClick } />;
const img = <img src={ url } alt={ defaultedAlt } onClick={ this.onImageClick } />;

if ( ! isResizable || ! imageWidthWithinContainer ) {
return (
Expand Down Expand Up @@ -525,7 +537,6 @@ class ImageEdit extends Component {
showRightHandle = true;
}
}
/* eslint-enable no-lonely-if */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be changed. We need to reenable any ESLint immediately after it is no longer necessary to be disabled.


return (
<Fragment>
Expand Down