From 29014c4d98c734643ddddc9f35074aeb37ea4392 Mon Sep 17 00:00:00 2001 From: Mu-An Chiou Date: Thu, 21 Sep 2023 17:11:25 +0800 Subject: [PATCH] Cap levels, resolves #1 --- index.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 43c0758..4d2c448 100644 --- a/index.js +++ b/index.js @@ -24,8 +24,7 @@ if ('level' in HTMLHeadingElement.prototype) { observer.observe(targetNode, { childList: true, subtree: true, attributes: true }) function setHeadingLevels(el) { - const startLevel = Number(el.getAttribute(attributeName)) - // TODO: Cap headingstartlevel here + const startLevel = Math.min(Number(el.getAttribute(attributeName)), 7) if (startLevel === NaN || startLevel === 0) return for (const heading of el.querySelectorAll('h1, h2, h3, h4, h5, h6')) { @@ -34,8 +33,7 @@ if ('level' in HTMLHeadingElement.prototype) { if (heading.hasAttribute('aria-level') && notSetBefore) continue if (notSetBefore) headingMap.push(heading) - // TODO: Cap max aria-label here - const level = startLevel + Number(heading.tagName[1]) - 1 + const level = Math.min(startLevel + Number(heading.tagName[1]) - 1, 9) heading.setAttribute('aria-level', level) } }