From 0ae699559fbd845c3f6453b7ef07270a1aa69e26 Mon Sep 17 00:00:00 2001 From: jonathan343 <43360731+jonathan343@users.noreply.github.com> Date: Thu, 30 Mar 2023 09:45:01 -0700 Subject: [PATCH] Accessibility Issues (#3645) Fixes a few accessibility issues including: 1. Allowing code blocks to be tab-able. 2. Converts implicit headings to actual headings. --- docs/source/_static/js/custom.js | 66 +++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/docs/source/_static/js/custom.js b/docs/source/_static/js/custom.js index 77f12ee819..c2a85d5621 100644 --- a/docs/source/_static/js/custom.js +++ b/docs/source/_static/js/custom.js @@ -76,10 +76,12 @@ function isValidResource(name, serviceDocName) { // Before:
  • ACM
  • // After:
  • ACM
  • function makeServiceLinkCurrent(serviceName) { - const servicesSection = $("a:contains('Available Services')")[0].parentElement; + const servicesSection = [...document.querySelectorAll('a')].find( + e => e.innerHTML.includes('Available Services') + ).parentElement; var linkElement = servicesSection.querySelectorAll(`a[href*="../${ serviceName }.html"]`); if (linkElement.length === 0) { - linkElement = sideBarElement.querySelectorAll(`a[href="#"]`)[0]; + linkElement = servicesSection.querySelectorAll(`a[href="#"]`)[0]; } else { linkElement = linkElement[0]; } @@ -87,14 +89,58 @@ function makeServiceLinkCurrent(serviceName) { linkParent.classList.add('current'); linkParent.classList.add('current-page'); } +const currentPagePath = window.location.pathname.split('/'); +const codeBlockSelector = 'div.highlight pre'; +const boldTextSelector = 'strong'; +const boldElements = document.querySelectorAll(boldTextSelector); +const headings = [ + 'Example', + 'Examples', + 'Exceptions', + 'Request Syntax', + 'Response Structure', + 'Response Syntax', + 'Structure', + 'Syntax' +]; // Expands the "Available Services" sub-menu in the side-bar when viewing // nested doc pages and highlights the corresponding service list item. -const currentPagePath = window.location.pathname.split('/'); -const currentPagePathLength = currentPagePath.length; -if (currentPagePath.includes('services')) { - document.getElementById('toctree-checkbox-11').checked = true; - // Example Nested Path: /reference/services//client/.html - const serviceNameIndex = currentPagePath.indexOf('services') + 1; - const serviceName = currentPagePath[serviceNameIndex]; - makeServiceLinkCurrent(serviceName); +function expandSubMenu() { + if (currentPagePath.includes('services')) { + document.getElementById('toctree-checkbox-11').checked = true; + // Example Nested Path: /reference/services//client/.html + const serviceNameIndex = currentPagePath.indexOf('services') + 1; + const serviceName = currentPagePath[serviceNameIndex]; + makeServiceLinkCurrent(serviceName); + } +} +// Allows code blocks to be scrollable by keyboard only users. +function makeCodeBlocksScrollable() { + const codeCells = document.querySelectorAll(codeBlockSelector); + codeCells.forEach(codeCell => { + codeCell.tabIndex = 0; + }); +} +// Converts implicit bold headings into actual headings with h3 tags. +function convertImplicitHeadings() { + boldElements.forEach(boldElement => { + if (headings.includes(boldElement.innerHTML)) { + boldElement.parentElement.outerHTML = `

    ${ boldElement.innerHTML }

    `; + } + }); +} +// Functions to run after the DOM loads. +function runAfterDOMLoads() { + expandSubMenu(); + convertImplicitHeadings(); + makeCodeBlocksScrollable(); +} +// Run a function after the DOM loads. +function ready(fn) { + if (document.readyState !== 'loading') { + fn(); + } else { + document.addEventListener('DOMContentLoaded', fn); + } } +ready(runAfterDOMLoads);