Skip to content
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

Add aria-label to code block #16053

Closed
CaitBarnard opened this issue Mar 18, 2024 · 6 comments · Fixed by #16197 · 4 remaining pull requests
Closed

Add aria-label to code block #16053

CaitBarnard opened this issue Mar 18, 2024 · 6 comments · Fixed by #16197 · 4 remaining pull requests
Assignees
Labels
domain:accessibility This issue reports an accessibility problem. package:code-block type:improvement This issue reports a possible enhancement of an existing feature.

Comments

@CaitBarnard
Copy link

📝 Provide a description of the improvement

When you add a code block to the editor, there is a small label on the right that says which language the code block is. This is inaccessible as an assistive technology user may not necessarily be able to pick that label up.

Is it possible to have some sort of event listener on the code blocks which updates the aria-label on this element?

Screenshot 2024-03-18 at 13 19 16

<pre data-language="Python" spellcheck="false" aria-label="python"><code class="language-python">Test code block</code></pre>

📃 Other details

This is a snippet of how I'm solving the issue currently. However it only fires on change and so doesn't provide an aria-label when loading a page that already has an editor with a code block. It's also not sensible to have a setTimeout which is there as the change trigger takes a moment to generate pre elements and prevents it being null before it's had a moment to update.

for (let id in editors) {
        editors[id].model.document.on('change', () => {
          setTimeout(() => {
            const preElements = document.querySelectorAll('pre');

            preElements.forEach(function (pre) {
              const code = pre.querySelector('code[class^="language-"]');
              if (code) {
                const language = code.className.replace('language-', '');
                pre.setAttribute('aria-label', language);
              }
            });
          }, 300);
        });
      }
  • Browser: Chrome
  • OS: Mac OS
  • CKEditor version: 5
  • Installed CKEditor plugins: …

If you'd like to see this improvement implemented, add a 👍 reaction to this post.

@CaitBarnard CaitBarnard added the type:improvement This issue reports a possible enhancement of an existing feature. label Mar 18, 2024
@Witoso Witoso added domain:accessibility This issue reports an accessibility problem. package:code-block labels Mar 18, 2024
@Mati365 Mati365 self-assigned this Mar 18, 2024
@Mati365
Copy link
Member

Mati365 commented Mar 19, 2024

Hi @CaitBarnard! Thanks for reporting the issue, I added aria-label attribute to the code element in this PR: #16056. Can you take a look and let us know if this solves the reported problem?

@CaitBarnard
Copy link
Author

Hi @Mati365. That solves the issue perfectly. Thank you for sorting that.

@Mati365
Copy link
Member

Mati365 commented Mar 19, 2024

@CaitBarnard We checked docs, and it seems that aria-label is ignored in most of non-interactive elements (including code tag). Are you using these aria-labels with a specific screen reader that supports it? Which one should we use to test it?

@CaitBarnard
Copy link
Author

CaitBarnard commented Mar 19, 2024

@Mati365 My bad, that's a good point that I completely overlooked. I believe the pre element can have an aria label. Is it possible to add it there instead? I've seen a few examples where a pre tag has an aria-label but I've yet to test it myself.

Another possibility I've seen is wrapping the pre/code in a figure with a figcaption which gets read by the screenreader instead but that feels a bit more hacky.

What do you think?

@Mati365
Copy link
Member

Mati365 commented Mar 19, 2024

@CaitBarnard No problem. I'm not sure if we can do that using pre aria tags in a way that is respected by all screen readers. We discussed internally that the aria-live mechanism used in other plugins would be sufficient, and after testing it looks like it works.

@CaitBarnard
Copy link
Author

@Mati365 Thank you. I had a look at the PR and it makes sense to me. Should be sufficient :)

oleq added a commit that referenced this issue Apr 16, 2024
Feature (code-block): Introduced screen reader announcements for entering or exiting code blocks in the editor content. Closes #16053.

Feature (image, list, ui): Introduced screen reader announcements for various actions and events in the editor.

Other (ui): Refactored the `AriaLiveAnnouncer` utility to use the `aria-relevant` attribute and make concurrent announcements queued by screen readers.

MINOR BREAKING CHANGE (ui): The region name argument of the `AriaLiveAnnouncer#announce()`  method has been dropped. Please check out the latest API documentation for more information. 

---------

Co-authored-by: Aleksander Nowodzinski <a.nowodzinski@cksource.com>
@CKEditorBot CKEditorBot added this to the iteration 74 milestone Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment