Problem/Motivation
When embedding an image entity, the wrapper <div> from Entity Embed contains an alt and title tag, both of whic duplicate the same attributes from the inner <img> tag.
The title attribute is considered harmful to accessibility, and should be removed. See #3083583: [META] Discourage non-inclusive use of the HTML title attribute.
The alt attribute is not allowed on <div> elements in HTML5. This causes errors in HTML validation. If you provide it to Google Search, AMP pages will not appear in search results.
<div data-embed-button="media_browser"
data-entity-embed-display="media_image"
data-entity-embed-display-settings="small"
data-entity-type="media"
data-entity-uuid="XXXXXXXXXXXX"
alt="myImageAlt" //This causes errors
title="myImageTitle"
data-langcode="ja"
class="embedded-entity align-center">
<amp-img src="/sites/default/files/styles/small/public/images/myImage.jpg?itok=XXXXXXXX"
width="700"
height="510"
alt="myImageAlt"
title="myImageTitle"
layout="responsive">
</amp-img>
</div>
Proposed resolution
Remove alt attribute from div element that rendered from <drupal-entity> .
<div data-embed-button="media_browser"
data-entity-embed-display="media_image"
data-entity-embed-display-settings="small"
data-entity-type="media"
data-entity-uuid="XXXXXXXXXXXX"
// just removed alt with `attributes.removeAttribute('alt')` in twig.
title="myImageTitle"
data-langcode="ja"
class="embedded-entity align-center">
<amp-img src="/sites/default/files/styles/small/public/images/myImage.jpg?itok=XXXXXXXX"
width="700"
height="510"
alt="myImageAlt"
title="myImageTitle"
layout="responsive">
</amp-img>
</div>
Remaining tasks
reviews needed
| Comment | File | Size | Author |
|---|---|---|---|
| #28 | entity_embed-remove_image_attributes_from_div-3086162-28-D8.patch | 1.17 KB | jwilson3 |
Issue fork entity_embed-3086162
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
Tomotsugu Kaneko commentedComment #3
Tomotsugu Kaneko commentedComment #4
oknateThe
<drupal-entity>tag shouldn't display on the front-end. It should be converted to an entity by EntityEmbedFilter. So this issue doesn't really make sense.Comment #5
Tomotsugu Kaneko commentedYes. <drupal-entity> render to <div> with the alt attribute.
I can display <drupal-entity> with ckeditor's source mode in admin area.
Comment #6
gdaw commentedThis patch fixes the issue for me, RTBC +1.
Comment #7
gdaw commentedTomotsugu I found this ticket before I submitted my own patch and ticket, but I used a slightly different approach
Instead of ... attributes.removeAttribute('alt') ... I used this ... attributes|without('alt')
Both methods will fix the validation error, but perhaps it is more efficient to display|without as opposed to display.adding/removing stuff?
I also have a second validation error for the image_style attribute, so I will provide a patch that takes care of both using |without()
This new patch will fix both W3C validation Erros ...
Attribute alt not allowed on element div at this point.
Attribute image_style not allowed on element div at this point.
Comment #8
gdaw commentedComment #9
gdaw commentedComment #10
gdaw commentedComment #12
joseph.olstad@gdaw you need to also update the tests as was done in the previous patch that passed testing.
Comment #13
gdaw commentedThanks for the reminder @joseph.olstad ... and while updating the test I spotted a third image attribute (title) we don't want to see on the div. Uploading patch 13
Comment #14
gdaw commentedComment #15
gdaw commentedComment #16
gdaw commentedComment #17
joseph.olstadWCAG was also complaining about the title attribute?
Hmm:
w3schools says any element can have a title attribute
https://www.w3schools.com/tags/att_global_title.asp
but another article warns against using it:
https://developer.paciellogroup.com/blog/2012/01/html5-accessibility-cho...
hmm, ya some people say the title attribute is problematic with tools like jaws. (should we all change because of Jaws? apparently?)
https://www.24a11y.com/2017/the-trials-and-tribulations-of-the-title-att...
Comment #18
gdaw commentedIn this case the title would be on the div as well as on the image itself, so a screen reader would 'stutter' and repeat the same title twice. This fix would prevent that from happening, plus resolve the other invalid attributes being added to the div.
Comment #19
gdaw commentedComment #20
joseph.olstad3 years later and we're still using this for WCAG compliance
Comment #21
spadxiii commenteddid a quick re-roll on the latest 8.x-1.x
Comment #22
tostinni commented@spadxiii I don't think your 8.x-1.x reference is accurate as your patch contains code that has been removed in #2881745: Wrapping embedded entities in <article> is bad for accessibility, use <div> instead, so for me the correct patch is still #14
Comment #23
jwilson3Setting back to NW per #22. This would also benefit from being moved to an MR.
Comment #24
jwilson3image_style attribute no longer appears in the latest 8.x-1.x version.
Comment #26
jwilson3We should also remove the title attribute, which has known accessibility issues.
See #3083583: [META] Discourage non-inclusive use of the HTML title attribute.
Comment #27
jwilson3Comment #28
jwilson3Here is an updated and versioned patch from the current state of the MR.
Comment #29
peri22 commentedHello, reviewed and works correctly. Thanks.
Comment #30
macsim commentedRTBC +1
Thanks