GithubHelp home page GithubHelp logo

double-great / alt-text Goto Github PK

View Code? Open in Web Editor NEW
22.0 3.0 1.0 1.06 MB

🔍 Check for unhelpful alt text

Home Page: https://doublegreat.dev/alt-text/

License: MIT License

JavaScript 1.42% TypeScript 98.58%
accessibility alt-text

alt-text's Introduction

alt-text Build Status

Check for unhelpful alt text.

Alt text describes an image. This description provides context for people with low or no vision. The text is a fallback for users with images turned off or when an image fails to load.

The usefulness of alt text can be subjective. Context, detail, length, and relevance contribute to the quality of the description. Determining alt text quality is a manual effort, but there are patterns to avoid.

This library checks for common issues found in alt text and suggests improvements.

Are you writing in markdown? We recommend using remark-lint-alt-text.

Install

npm install @double-great/alt-text

Usage

If the library detects an issue, it will return a suggestion to help you fix it. If it doesn't detect any issues it will return undefined.

import altText from "@double-great/alt-text";

console.log(altText("A child holding a photograph."));
// undefined

console.log(altText("A photo of a dog"));
// Alt text should not contain "photo of" (https://doublegreat.dev/alt-text/#alt-text-contains-unhelpful-words). Alt text should end with punctuation (https://doublegreat.dev/alt-text/#end-with-punctuation).

The library is configured with best practices, but context is always important. You can disable any rule and pass custom configuration options for those that check against specific values.

Example:

console.log(altText("A photo of a dog", { "end-with-punctuation": false }));
// Alt text should not contain "photo of" (https://doublegreat.dev/alt-text/#alt-text-contains-unhelpful-words).

Each rule in the suggestions list below has full documentation on configuration options.

Suggestions

Alt text contains unhelpful words

Suggestion: Alt text should not contain "graphic of, image of, photo of, photo:, photograph of, photographer:, picture of, screen shot of, screenshot of"

Screen readers announce the presence of an image before reading the alt text. Adding “picture of” or “photo of” is redundant in this context.

  • ✅ Dog jumping through a hoop.
  • 🚫 Image of a dog jumping through a hoop.

Configuration:

// disable the rule:
altText("My alt text.", {"contains-unhelpful-word":false})
// adjust rule defaults:
altText("My alt text.", {"contains-unhelpful-word":{"exclude":["graphic of","image of","photo of","photo:","photograph of","photographer:","picture of","screen shot of","screenshot of"]}})

Sources:

Alt text is unhelpful

Suggestion: Alt text should not be "*, alt, arrow, artwork, blank, bullet, button, chart, diagram, drawing, empty, graph, graphic, icon, image, logo, more, painting, photo, photograph, placeholder, screen shot, screenshot, spacer, table, temp"

Usually, there’s no need to include words like “image”, “icon”, or “picture” in the alt text. People who can see will know this already, and screen readers announce the presence of an image.

  • ✅ A child holding a photograph.
  • 🚫 photograph

Configuration:

// disable the rule:
altText("My alt text.", {"is-unhelpful":false})
// adjust rule defaults:
altText("My alt text.", {"is-unhelpful":{"exclude":["*","alt","arrow","artwork","blank","bullet","button","chart","diagram","drawing","empty","graph","graphic","icon","image","logo","more","painting","photo","photograph","placeholder","screen shot","screenshot","spacer","table","temp"]}})

Sources:

Alt text should not end with

Suggestion: Alt text should not end with ".gif, .jpeg, .jpg, .png, .svg, .webp, graphic, image"

A file name in alt text does not provide helpful context.

  • ✅ A child holding a photograph.
  • 🚫 photograph.jpg

Configuration:

// disable the rule:
altText("My alt text.", {"should-not-end-with":false})
// adjust rule defaults:
altText("My alt text.", {"should-not-end-with":{"exclude":[".gif",".jpeg",".jpg",".png",".svg",".webp","graphic","image"]}})

Sources:

Alt text should not start with

Suggestion: Alt text should not start with "graphic, image, photo, photograph, photographer, picture, screen shot, screenshot, spacer"

Usually, there’s no need to include words like “image”, “icon”, or “picture” in the alt text. People who can see will know this already, and screen readers announce the presence of an image.

  • ✅ A child holding a photograph.
  • 🚫 Image of a child.

Configuration:

// disable the rule:
altText("My alt text.", {"should-not-start-with":false})
// adjust rule defaults:
altText("My alt text.", {"should-not-start-with":{"exclude":["graphic","image","photo","photograph","photographer","picture","screen shot","screenshot","spacer"]}})

Sources:

Avoid emoji

Suggestion: Replace emoji in alt text with descriptive text

Emoji have their own text descriptions. These descriptions can vary between operating systems and software. The spoken description of the emoji may not match your visual intention.

  • <img src="cat.jpg" alt="An orange cat.">
  • 🚫 <img src="cat.png" alt="An orange 🐈."/>

Hear an example: https://doublegreat.dev/listen/emoji/

Configuration:

// disable the rule:
altText("My alt text.", {"avoid-emoji":false})

Sources:

Character length

Suggestion: Alt text length should be less than 125 characters

Alt text should be less than 125 characters in length. The JAWS screen reader reads alt text in 125 character chunks. Any information about the image, such as copyright information, image source or extra information should be placed in the caption text below the image.

  • ✅ George Washington and Lafayette on horseback talking to soldiers in snow at Valley Forge.
  • 🚫 Caption: Painting “Washington and Lafayette at Valley Forge” by John Ward Dunsmore from 1907. Image courtesy of the Library of Congress.

Configuration:

// disable the rule:
altText("My alt text.", {"character-length":false})
// adjust rule defaults:
altText("My alt text.", {"character-length":{"length":125}})

Sources:

Empty alt text

Suggestion: Alt text should not be a single space

If you use null (empty) alt text (alt="") to hide decorative images, make sure that there is no space character in between the quotes. If a space character is present, the image may not be effectively hidden from assistive technologies. For instance, some screen readers will still announce the presence of an image if a space character is put between the quotes.

  • <img src="photo.png" alt="">
  • 🚫 <img src="photo.png" alt=" ">

Configuration:

// disable the rule:
altText("My alt text.", {"empty-alt-text":false})

Sources:

End with punctuation

Suggestion: Alt text should end with punctuation

End the alt text with a period, exclamation point, or question mark. This will make screen readers pause a bit after the last word in the alt text, which creates a more pleasant reading experience for the user.

  • ✅ A child holding a photograph.
  • 🚫 A child holding a photograph

Hear an example: https://doublegreat.dev/listen/punctuation-in-alt-text/

Configuration:

// disable the rule:
altText("My alt text.", {"end-with-punctuation":false})

Sources:

Image is decorative

Suggestion: Empty alt text should only be used for decorative images

Provide "null" alt attributes (using alt="") for images which do not provide information or do not require alternative text because the image is described in the page content. Some developers will mistakenly leave off the alt attribute altogether on images which they deem do not need alternatives. This is not helpful to assistive technology users because the assistive technology, such as screen reader, will often read the source attribute (i.e., file name) as the alternative text. To tell assistive technology to ignore an image, use a "blank alternative text" attribute: alt="".

  • <img src="decorative-photo.jpg" alt="">
  • 🚫 <img src="quarterly-earnings-chart.png" alt=""/>

Configuration:

// disable the rule:
altText("My alt text.", {"image-is-decorative":false})

Sources:

Image is link

Suggestion: Images inside a link tag require alt text that describes the purpose of the link

Images inside a link tag require alt text that describes the purpose of the link.

  • <a href="https://github.com/double-great"><img src="logo.png" alt="double great on GitHub"></a>
  • 🚫 <a href="https://github.com/double-great"><img src="logo.png" alt="double great logo"></a>

Configuration:

// disable the rule:
altText("My alt text.", {"image-is-link":false})

Sources:

Missing alt attribute

Suggestion: Missing "alt" attribute

All images must have alternate text to convey their purpose and meaning to screen reader users.

  • <img src="photograph.jpg" alt="A child holding a photograph.">
  • 🚫 <img src="photograph.jpg">

Configuration:

// disable the rule:
altText("My alt text.", {"no-alt":false})

Sources:

alt-text's People

Contributors

dependabot[bot] avatar github-actions[bot] avatar jsnmrs avatar katydecorah avatar muescha avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

muescha

alt-text's Issues

Git.io deprecation effective immediately

@katydecorah looks like we need to find a replacement for https://git.io

Git.io is a URL shortening website that GitHub created in 2011 for redirecting to GitHub domains like github.com and github.io. What began as an experiment was only lightly documented and thus not heavily used.

GitHub is not uniquely qualified to run this type of service. There are many URL shortening services available today that have more capabilities than git.io. At GitHub, we want to focus on building great developer tools and cede URL shortening to companies and teams who provide it as a core offering.

For these reasons, we have disallowed new link creation on git.io. We encourage using one of the many URL shortening services that are available, instead of git.io, as it likely will be fully deprecated at some yet to be determined date.

via https://github.blog/changelog/2022-01-11-git-io-no-longer-accepts-new-urls

add test for alt = filename

add test for:

alt text matches filename

![myimage.png](./images/myimage.png)

![https://raw.githubusercontent.com/strapi/strapi-starter-gatsby-blog/master/medias/skip.png](https://raw.githubusercontent.com/strapi/strapi-starter-gatsby-blog/master/medias/skip.png "screen shot of sample cut and paste webhook url") 

Group tests by rule

We should group tests by rule and add labels to better organize and document intent for each test.

Include sounds files in documentation

We should consider adding sounds files of a screen reader reading alt text in the documentation. This can help the developer hear the differences between the recommended and discouraged alt text.

How to respond to empty string or undefined/null?

We'll need to catch empty string and undefined values as right now they'll fail with TypeError: Cannot read property 'toLowerCase' of undefined.

@jsnmrs how should alt-text respond to:

  • an empty string altText('') - an empty string is technically correct for decorative images, should we still warn and provide that feedback?
  • undefined altText() - should this return the same message regarding missing alt attribute? in thinking how how someone could supply undefined, it's likely there isn't an alt attribute at all 🤔

Create exactMatch list

separate list for exact (full) matches. List should include:

  • image
  • graphic
  • photo
  • photograph
  • drawing
  • painting
  • artwork
  • logo
  • bullet
  • button
  • arrow
  • more
  • spacer
  • blank
  • chart
  • table
  • diagram
  • graph
  • "*" (bullet)
  • " " (space)

The standalone test for a single space can't be removed afterwards.

Provide passing versus warning examples in documentation

It could be helpful to provide a passing versus warning example for some of the rules, for example:


Alt text should not end with

Warning: Alt text should not end with "<.jpg|.jpeg|.gif|.png|.svg|.webp|image|graphic>"

  • ✅ A child holding a photograph.
  • 🚫 child.jpg

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.