wjdp / htmltest

:white_check_mark: Test generated HTML for problems
MIT License
323 stars 54 forks source link

empty alt may be OK in some situations #153

Closed tcurdt closed 1 year ago

tcurdt commented 3 years ago

It seems in a situations where there is a related/sibling figcaption the alt attribute may be OK to be left empty for img tags.

https://stackoverflow.com/questions/58447538/accessibility-difference-between-img-alt-and-figcaption

It probably should be configurable whether that situation is considered an error or not.

wjdp commented 3 years ago

Current behaviour:

Wasn't quite sure of the best solution for this but after a bit of reading I've come to this conclusion:

I'm undecided on whether IgnoreAltEmpty should be enabled by default. I'm leaning towards no. This both preserves existing behaviour and makes user think about what they're doing.


Some articles for future reference:

tcurdt commented 3 years ago

I'm undecided on whether IgnoreAltEmpty should be enabled by default. I'm leaning towards no. This both preserves existing behaviour and makes user think about what they're doing.

I agree with that - but IMO also the context matters. IIUC these are very general options while the figcaption seems to be a very specific situation. But I would only want to allow empty/missing alt attributes in this very case.

wjdp commented 3 years ago

Context does matter and this extends beyond the usage of figcaption. An intentional empty alt tag can be valid in a few contexts. The two I know of:

Both require a conscious decision to omit the alt text. I had two options in mind. Am a little undecided.

Attribute

Look for a data-htmltest-allow-missing-alt (or similar name) attribute which would disable the check, would look like:

<img src="..." alt="" data-htmltest-allow-empty-alt />
<img src="..." data-htmltest-allow-missing-alt />

This is very similar to the data-proofer-ignore attribute we already have that just disables every check on a tag.

Use alt="" as the indicator combined with IgnoreAltEmpty

My initial thought. An empty alt attribute can be a marker of intent. If you intentionally produced <img src="..." alt="" /> then we can allow it.

Thinking about this now you could very easily produce that tag from a faulty templating system which is what this tool is used to catch! I'm now less fond of this idea.

tcurdt commented 3 years ago

In my case the img is being generated by a markup conversion. So the data-htmltest-allow-missing-alt wouldn't really work for me. But on the other hand I wouldn't want to allow empty alt attributes everywhere.