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

feat(image): throw if alt text is missing #4511

Merged
merged 11 commits into from
Sep 1, 2022
Merged

Conversation

DerYeger
Copy link
Contributor

@DerYeger DerYeger commented Aug 27, 2022

Changes

Testing

This change was tested by adding fixtures that have images without alt text.
The tests assert that an error is thrown.

Docs

PR: withastro/docs#1421

@github-actions github-actions bot added the pkg: integration Related to any renderer integration (scope) label Aug 27, 2022
@DerYeger DerYeger marked this pull request as ready for review August 27, 2022 11:01
@changeset-bot
Copy link

changeset-bot bot commented Aug 28, 2022

🦋 Changeset detected

Latest commit: 618834e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@astrojs/image Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@bluwy bluwy linked an issue Aug 29, 2022 that may be closed by this pull request
1 task
Comment on lines 93 to 96
Astro's `<Image />` and `<Picture />` components will throw an error if no `alt` text is provided.

If the image is merely decorative (i.e. doesn't contribute to the understanding of the page), set `alt=""` so it is ignored by screen readers.

Copy link
Member

Choose a reason for hiding this comment

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

LOVE this new feature, @DerYeger! I have installed some Chrome extensions myself to make sure I'm not allowed to Tweet without alt-text on my images. 😄

Please ALSO check the "Usage" heading further down the page where the attributes, and whether or not they are required are listed for both components. There should be an alt heading and description for each one of them. It looks like it's currently not there for <Image /> but is there for <Picture /> though only saying "if it's provided."

These should both prominently display the new info, and double check all the examples to make sure they all show alt= on every component.

Lastly, @DerYeger & @tony-navillus, what exactly is the error message that is thrown? (Sorry, hard to look through everything on my phone here) If it's not, can it be an explicit message to add alt text rather than a generic error?

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi, I used the suggestion of #4291 (comment) as the error message, replacing image with picture for the respective component.

Copy link
Member

Choose a reason for hiding this comment

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

Great! I've merged the PR for the docs "Images Guide" page just now. Just let me know if you're up for making the above edits under "Usage" (as an attribute, like all the other aspectRatio, src etc.) in addition to your note here, or whether you'd like me to take care of that! But, it DOES need to be there as an attribute for each component.

Copy link
Member

Choose a reason for hiding this comment

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

These changes don't appear to have been made, so I'm making them now here!

Copy link
Contributor

@tony-sull tony-sull left a comment

Choose a reason for hiding this comment

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

This is excellent, thanks for the thorough test coverage!

Consider the feature work here 🎉 @sarah11918 I'll leave the docs approval up to you if you don't mind, once it has your approval as well I'll get this merged in and ready for release 🚀

@sarah11918
Copy link
Member

Yay! Will look closely when I am back at a laptop!

@tony-sull
Copy link
Contributor

@DerYeger Let me know if you have any issues with the merge conflict!

@tony-sull
Copy link
Contributor

I'll get those tests passing, looks like the tests in main are too aggressive with hard-coded filename hashes

@sarah11918
Copy link
Member

@tony-sull Ooh, good! I need to make a fix to this README anyway. Please do not merge this until I've updated the docs here. 💜

@sarah11918
Copy link
Member

OK @tony-sull PLEASE check all my alt-text stuff carefully! Code samples! etc! Please make sure it's actually correct and no typos. 💜 Otherwise, I'm happy.

@DerYeger
Copy link
Contributor Author

I'll get those tests passing, looks like the tests in main are too aggressive with hard-coded filename hashes

Thanks! Let me know I can help out anywhere else.

@tony-sull
Copy link
Contributor

tony-sull commented Aug 31, 2022

Just opened #4579 with a big cleanup of the image tests. This PR was a perfect kick in the pants for me to stop dragging my feet on refactoring all the test code 😄

Copy link
Member

@FredKSchott FredKSchott left a comment

Choose a reason for hiding this comment

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

Looks great! I had a few comments, mainly around documentation. Thanks for pushing us to add this, I'm a big +1 for this feature.

packages/integrations/image/README.md Outdated Show resolved Hide resolved
packages/integrations/image/README.md Outdated Show resolved Hide resolved
@@ -21,6 +23,10 @@ export type Props = LocalImageProps | RemoteImageProps;

const { loading = 'lazy', decoding = 'async', ...props } = Astro.props as Props;

if (props.alt === undefined || props.alt === null) {
throw new Error('The <Image> component requires you provide alt text. If this image does not require an accessible label, set alt="".')
Copy link
Member

Choose a reason for hiding this comment

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

link to more reading in case this is a new concept for the reader! https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-alt would probably be good.

FYI no need to worry too much about the final formatting here for this error message, as long as all of the info is correct. @tony-sull or myself will plan to finalize this ourselves with correct formatting before merging the PR, to match other errors in our codebase.

}

interface RemoteImageProps extends TransformOptions, astroHTML.JSX.ImgHTMLAttributes {
src: string;
alt: string;
Copy link
Member

Choose a reason for hiding this comment

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

Can we add a helpful warning here as JSDoc as well? Something like:

/** Defines an alternative text description of the image. Set to an empty string (alt="") if the image is not a key part of the content (it's decoration or a tracking pixel). **/
alt: string;

Ditto for the LocalImageProps type above.

@FredKSchott
Copy link
Member

@tony-sull @DerYeger I'm realizing that this will break most existing users who didn't add alt text, and my guess is that this is a lot of them. Even if we make this error helpful, that's still going to be a real pain to upgrade given that we won't even render the page anymore if alt is missing.

Thoughts on making this a TS-only change first, and then adding the runtime error in a future minor release? This will still provide a very visible error in astro check and visible inline errors in their code editor, but it will also give them some time to update before we escalate.

FYI to implement this change, everything about this PR can stay as-is except for removing the ~3 lines that throw the error at runtime.

@matthewp
Copy link
Contributor

matthewp commented Sep 1, 2022

@FredKSchott are you proposing essentially we warn in 1 "major" (minor in this case because < 1.0) release and then break in the next? If so I think that's fine and in line with some other changes we've done.

@tony-sull
Copy link
Contributor

@FredKSchott That's a good callout, I like the idea of warning for now and throwing the error in a later release (either a future minor or 1.0)

@DerYeger The tests should be almost there now, I refactored all the image tests to avoid using hard-coded filename hashes but it looks like I missed an alt tag in the with-mdx test suite 🤦

@tony-sull
Copy link
Contributor

@FredKSchott @sarah11918 I pushed a few updates here to include the comments above. Any additional feedback before merging this in?

For the warning, I wrapped it in a helper to make sure it's only logged once per astro dev or astro build. Logging a warning for every component could get very frustrating 😅

@DerYeger
Copy link
Contributor Author

DerYeger commented Sep 1, 2022

@FredKSchott @sarah11918 I pushed a few updates here to include the comments above. Any additional feedback before merging this in?

For the warning, I wrapped it in a helper to make sure it's only logged once per astro dev or astro build. Logging a warning for every component could get very frustrating 😅

That’s a great change, thanks for cleaning up this PR!

@tony-sull tony-sull merged commit 72c760e into withastro:main Sep 1, 2022
@tony-sull
Copy link
Contributor

Thanks for this PR, @DerYeger! Excellent test coverage and docs updates 🎉

Very excited to see this flipped over to a build error in the future, I'm always a fan of build tools making sure I don't accidentally create a11y issues!

@astrobot-houston astrobot-houston mentioned this pull request Sep 1, 2022
@FredKSchott
Copy link
Member

@FredKSchott are you proposing essentially we warn in 1 "major" (minor in this case because < 1.0) release and then break in the next? If so I think that's fine and in line with some other changes we've done.

Yup, pretty much! Glad everyone was on board for this, sgtm and excited to see this merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: integration Related to any renderer integration (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@astrojs/image should throw an error if alt text is missing
5 participants