-
Notifications
You must be signed in to change notification settings - Fork 408
✨(frontend) fix rgaa 1.9.1: convert to figure/figcaption structure if… #1426
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
base: main
Are you sure you want to change the base?
Conversation
4c63e44
to
b775c5c
Compare
Size Change: +214 B (+0.01%) Total Size: 3.65 MB
|
ensure html structure by using figure/figcaption when captions are present Signed-off-by: Cyril <[email protected]>
b775c5c
to
965c208
Compare
const captionElement = dom.querySelector('.bn-file-caption'); | ||
|
||
if (captionElement) { | ||
const figureElement = document.createElement('figure'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
imgSelector?.setAttribute('aria-hidden', 'true'); | ||
imgSelector?.setAttribute('tabindex', '-1'); | ||
// Set accessibility attributes for the image | ||
if (block.props.caption) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This double if on block.props.caption can maybe be improved a bit ?
const withCaption = block.props.caption && dom.querySelector('.bn-file-caption');
return (withCaption) ? accessibleImageWithCaption() : accessibleImage();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes very good idea 💯
figureElement.setAttribute('role', 'img'); | ||
figureElement.setAttribute( | ||
'aria-label', | ||
`Image: ${figcaptionElement.textContent}`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`Image: ${figcaptionElement.textContent}`, | |
t(`Image: {{title}}`, {title: figcaptionElement.textContent}), |
39d4f4c
to
0ab6394
Compare
const accessibleImageWithCaption = () => { | ||
imgSelector?.setAttribute('alt', block.props.caption); | ||
imgSelector?.removeAttribute('aria-hidden'); | ||
imgSelector?.setAttribute('tabindex', '0'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can add all this part in this function as well:
Lines 49 to 92 in 0ab6394
if (block.props.caption) { | |
const captionElement = dom.querySelector('.bn-file-caption'); | |
if (captionElement) { | |
const figureElement = document.createElement('figure'); | |
// Copy all attributes from the original div | |
figureElement.className = dom.className; | |
const styleAttr = dom.getAttribute('style'); | |
if (styleAttr) { | |
figureElement.setAttribute('style', styleAttr); | |
} | |
figureElement.style.setProperty('margin', '0'); | |
Array.from(dom.children).forEach((child) => { | |
figureElement.appendChild(child.cloneNode(true)); | |
}); | |
// Replace the <p> caption with <figcaption> | |
const figcaptionElement = document.createElement('figcaption'); | |
const originalCaption = figureElement.querySelector('.bn-file-caption'); | |
if (originalCaption) { | |
figcaptionElement.className = originalCaption.className; | |
figcaptionElement.textContent = originalCaption.textContent; | |
originalCaption.parentNode?.replaceChild( | |
figcaptionElement, | |
originalCaption, | |
); | |
// Add explicit role and aria-label for better screen reader support | |
figureElement.setAttribute('role', 'img'); | |
figureElement.setAttribute( | |
'aria-label', | |
t(`Image: {{title}}`, { title: figcaptionElement.textContent }), | |
); | |
} | |
// Return the figure element as the new dom | |
return { | |
...imageRenderComputed, | |
dom: figureElement, | |
}; | |
} | |
} |
Purpose
Improve accessibility and semantic HTML by applying the appropriate structure when captions are present, to comply with RGAA 1.9.1.
issue : 1074
Proposal
figcaption
tagfigure
tagfigcaption
tag