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

Conversation

antpb
Copy link
Contributor

@antpb antpb commented Oct 29, 2018

NEW PR TO CLEAN UP REBASE

Description

Sets default alt values to fix #1520
Previous conversation can be found here: #10482

How to test:
create image block
add image with no alt value
inspect image to see that alt value is now set as a readable string "This image has an empty alt attribute, its file name is "FILENAME""

@antpb antpb requested review from ocean90 and aduth October 29, 2018 20:03
@mtias mtias added the [Feature] Media Anything that impacts the experience of managing media label Nov 1, 2018
@catehstn
Copy link

catehstn commented Nov 1, 2018

@noisysocks @talldan Can you please have a look at this? It's a relatively minor change and would be good to get it merged. Thank you!

Copy link
Contributor

@talldan talldan left a comment

Choose a reason for hiding this comment

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

Hi @antpb - thanks so much for working on this, looks like a really good change to get in, and the functionality worked well in testing.

I think there are a couple of code quality improvements that could be made, so I've left a some comments.

I can also help out adding an e2e test once this is merged.

// 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 imgNoAlt = <img src={ url } alt={ defaultAlt } onClick={ this.onImageClick } />;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this could have been done in a much smaller change, something along the lines of:

const defaultedAlt = alt ? alt : sprintf( __( 'This image has an empty alt attribute; its file name is %s' ), this.getFilename( url ) );
const img = <img src={ url } alt={ defaultedAlt } onClick={ this.onImageClick } />;

Would avoid the need for the if statement on line 486 and the ternary on 574.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

great suggestion. that makes this much more readable.

@@ -245,6 +246,16 @@ class ImageEdit extends Component {
};
}

getFilename( url ) {
if ( url ) {
const fileName = url.toString().match( /.*\/(.+?)$/ );
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like the regular expression doesn't take query string parameters into account, and I think it should strip them.

For example, the image for my gh avatar has some params in the url -> https://avatars2.githubusercontent.com/u/677833?s=60&v=4

I also think the toString might be unnecessary, I'd expect url to be a string or undefined given that's its attribute type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Nice callout. My regex is horrible so I would love a review on my recent commit. I tested with a regular media library image and also the avatar string you provided. Seems to be working! 😬

Copy link
Contributor

Choose a reason for hiding this comment

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

One change I'd make is to for the second group to be a non-capturing group, since we're not concerned with using its matched content:

/.*\/(.+?)(?:\?.*)?$/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool! Good call. Thanks!

packages/block-library/src/image/edit.js Outdated Show resolved Hide resolved
@talldan talldan added the [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). label Nov 2, 2018
@antpb antpb force-pushed the add-default-alt-values-to-images-in-editing-context-#1520 branch 2 times, most recently from 5a4d6ea to 6f0a542 Compare November 5, 2018 19:20
@antpb antpb force-pushed the add-default-alt-values-to-images-in-editing-context-#1520 branch from 6f0a542 to 338eb64 Compare November 5, 2018 19:28
@@ -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 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.

@@ -246,6 +247,11 @@ class ImageEdit extends Component {
};
}

getFilename( url ) {
const fileName = url.match( /.*\/(.+?)(?:\?.*)?$/ );
Copy link
Member

Choose a reason for hiding this comment

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

The main reason I'd see to author this way vs. directly return the value (return url.match( /* ... */ )[ 1 ]) would be to have the variable serve as an explaining variable, but in this case its not well-explained in that the return of String#match is not the file name, it's a match Array.

Copy link
Member

Choose a reason for hiding this comment

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

This could be made more durable (especially the query parameter) bit if we were to rely on the new getPath from @wordpress/url.

const path = getPath( url );
if ( path ) {
	return last( path.split( '/' ) );
}

Copy link
Member

Choose a reason for hiding this comment

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

To our previous conversation about the certainty of the file name being matched; is there any guarantee that the input here is in-fact a url? It's making me a bit nervous.

packages/block-library/src/image/edit.js Outdated Show resolved Hide resolved
Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

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

Looks good 👍

@aduth aduth merged commit bcc4fa5 into WordPress:master Nov 12, 2018
@youknowriad youknowriad added this to the 4.4 milestone Nov 12, 2018
@afercia afercia mentioned this pull request Feb 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Media Anything that impacts the experience of managing media [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Images alt attribute in an editing context
6 participants