question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Accessibility requirement: capacity to have an empty alt output in the HTML from an empty or null string variable passed into a th:alt property

See original GitHub issue
  • Nature of the issue according to me: BUG
  • Problem description: Web accessibility for images that are decorative (that is most images on the web) requires that they have an empty alt property, see: https://www.w3.org/WAI/tutorials/images/decorative/
  • Work-around: duplicate img tag with two versions, one with an alt=“” when variable is an empty string or null, the other with the th:alt=“”, when the variable is a non nempty, non null string
  • Version of Thymeleaf: 3
  • Environment: not relevant
  • Add an empty th:alt property to an image tag if variable passed in the th:alt property resolves to null or empty string without having to manage empty or null string and duplicating tag

HTML in thymeleaf, case: string altText is not empty ( set to ‘test’)

<th:block th:with=" altText = 'test' ">
    <img alt=""
        src="https://via.placeholder.com/90x32?text=NonEmptynonNullString"
        th:alt="${altText}"
        th:if="${not #strings.isEmpty(altText)}"
    >
    <img alt=""
        src="https://via.placeholder.com/90x32?text=emptyOrNullString"
        th:if="${altText == '' or altText == null}"
    >
</th:block>

Outputs

<img alt="" src="https://via.placeholder.com/90x32?text=NonEmptynonNullString">

HTML in thymeleaf, case: string altText is empty ( set to ‘’)

<th:block th:with=" altText = '' ">
    <img alt=""
        src="https://via.placeholder.com/90x32?text=NonEmptynonNullString"
        th:alt="${altText}"
        th:if="${not #strings.isEmpty(altText)}"
    >
    <img alt=""
        src="https://via.placeholder.com/90x32?text=emptyOrNullString"
        th:if="${altText == '' or altText == null}"
    >
</th:block>

Outputs

<img alt="" src="https://via.placeholder.com/90x32?text=emptyOrNullString">

HTML in thymeleaf, case: string altText is empty ( set to null)

<th:block th:with=" altText = null ">
    <img alt=""
        src="https://via.placeholder.com/90x32?text=NonEmptynonNullString"
        th:alt="${altText}"
        th:if="${not #strings.isEmpty(altText)}"
    >
    <img alt=""
        src="https://via.placeholder.com/90x32?text=emptyOrNullString"
        th:if="${altText == '' or altText == null}"
    >
</th:block>

Outputs

<img alt="" src="https://via.placeholder.com/90x32?text=emptyOrNullString">

HTML in thymeleaf, case: string altText is empty (set to null or ‘’) It should be possible to output

<th:block th:with=" altText = null ">
    <img
        src="https://via.placeholder.com/90x32?text=emptyOrNullString"
        th:alt="${altText}"
    >
</th:block>
<th:block th:with=" altText = '' ">
    <img
        src="https://via.placeholder.com/90x32?text=emptyOrNullString"
        th:alt="${altText}"
    >
</th:block>

Outputs

<img src="https://via.placeholder.com/90x32?text=emptyOrNullString">

Thymeleaf SHOULD output, for web accessibility needs

<img alt="" src="https://via.placeholder.com/90x32?text=emptyOrNullString">

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Sven-WEBDESIGNcommented, Oct 18, 2019

Sorry, i closed the issue by error. Have a good day.

1reaction
ultraqcommented, Oct 11, 2019

Yeah, the th:alt="" thing does bother me - I feel it should at least emit an empty alt value if the expression in it is empty itself or resolves to some “falsey” value. I think we have other attributes that do this, so I can check those.

I’ll leave this issue open and tag it as appropriate so that the other devs can see this and we can discuss if necessary. Thanks for bringing this up @Sven-WEBDESIGN.

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTML Test Suite for WCAG 2.0 Test 16 - W3C
Change the Alt text to an empty string ("") or confirm that the image is not decorative. Pass Instructions. The following tests are...
Read more >
Accessibility: Image Alt text best practices - Siteimprove Support
Adding an image or a graphic to your content without using proper or empty alternative attributes (alt tags), can be extremely frustrating for ......
Read more >
What is the benefit to add empty alt=""? and which alt should ...
To get your XHTML validated. The alt is a required attribute on images. Adding it empty is however a sign of laziness from...
Read more >
<input>: The Input (Form Input) element - HTML
The HTML element is used to create interactive controls for web-based ... displaying the value of the value attribute, empty by default.
Read more >
Built-in template tags and filters - Django documentation
If the variable evaluates to a string, Django will use that string as the name of the ... The for tag can take...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found