Skip to content

Conversation

Ovgodd
Copy link
Collaborator

@Ovgodd Ovgodd commented Sep 23, 2025

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

  • Detect if a caption exists in the component/template
  • Place the caption inside a figcaption tag
  • Wrap media elements in a figure tag
  • Place the caption inside a figcaption tag

@Ovgodd Ovgodd requested a review from AntoLC September 23, 2025 11:06
@Ovgodd Ovgodd self-assigned this Sep 23, 2025
@Ovgodd Ovgodd force-pushed the fix/1074-a11y-image-caption branch 2 times, most recently from 4c63e44 to b775c5c Compare September 23, 2025 11:08
@Ovgodd Ovgodd marked this pull request as ready for review September 23, 2025 11:08
Copy link

github-actions bot commented Sep 23, 2025

Size Change: +214 B (+0.01%)

Total Size: 3.65 MB

Filename Size Change
apps/impress/out/_next/static/fa80b17a/_buildManifest.js 0 B -865 B (removed) 🏆
apps/impress/out/_next/static/b64167df/_buildManifest.js 866 B +866 B (new file) 🆕

compressed-size-action

ensure  html structure by using figure/figcaption when captions are present

Signed-off-by: Cyril <[email protected]>
@Ovgodd Ovgodd force-pushed the fix/1074-a11y-image-caption branch from b775c5c to 965c208 Compare September 23, 2025 11:12
const captionElement = dom.querySelector('.bn-file-caption');

if (captionElement) {
const figureElement = document.createElement('figure');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

figure has a margin by default:
image

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, I fixed it:
image

imgSelector?.setAttribute('aria-hidden', 'true');
imgSelector?.setAttribute('tabindex', '-1');
// Set accessibility attributes for the image
if (block.props.caption) {
Copy link
Collaborator

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();

Copy link
Collaborator Author

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}`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`Image: ${figcaptionElement.textContent}`,
t(`Image: {{title}}`, {title: figcaptionElement.textContent}),

@Ovgodd Ovgodd force-pushed the fix/1074-a11y-image-caption branch from 39d4f4c to 0ab6394 Compare September 24, 2025 11:42
@Ovgodd Ovgodd requested a review from AntoLC September 24, 2025 11:43
const accessibleImageWithCaption = () => {
imgSelector?.setAttribute('alt', block.props.caption);
imgSelector?.removeAttribute('aria-hidden');
imgSelector?.setAttribute('tabindex', '0');
Copy link
Collaborator

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:

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,
};
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants