From bd0483996125aa63f54e816ff7af07189d7dd7ad Mon Sep 17 00:00:00 2001 From: "yugo.innami" Date: Wed, 15 Apr 2026 16:07:15 +0900 Subject: [PATCH 1/2] fix(commons/text): exclude natively hidden elements from aria-labelledby accessible name style, script, noscript, and template elements are natively hidden and should not contribute to the accessible name even when referenced via aria-labelledby with includeHidden context. Closes issue #4704 --- lib/commons/text/accessible-text-virtual.js | 10 +++++-- test/commons/text/accessible-text.js | 32 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/lib/commons/text/accessible-text-virtual.js b/lib/commons/text/accessible-text-virtual.js index c1ce96f69e..597f07ee70 100644 --- a/lib/commons/text/accessible-text-virtual.js +++ b/lib/commons/text/accessible-text-virtual.js @@ -7,6 +7,7 @@ import titleText from './title-text'; import sanitize from './sanitize'; import isVisibleToScreenReaders from '../dom/is-visible-to-screenreader'; import isIconLigature from '../text/is-icon-ligature'; +import { nativelyHidden } from '../dom/visibility-methods'; /** * Finds virtual node and calls accessibleTextVirtual() @@ -84,11 +85,14 @@ function shouldIgnoreHidden(virtualNode, context) { return false; } + // When traversing aria-labelledby references, include hidden nodes except natively hidden elements + if (context.includeHidden && !nativelyHidden(virtualNode)) { + return false; + } + if ( // If the parent isn't ignored, the text node should not be either - virtualNode.props.nodeType !== 1 || - // If the target of aria-labelledby is hidden, ignore all descendents - context.includeHidden + virtualNode.props.nodeType !== 1 ) { return false; } diff --git a/test/commons/text/accessible-text.js b/test/commons/text/accessible-text.js index fd0552f0cd..c718fcf00b 100644 --- a/test/commons/text/accessible-text.js +++ b/test/commons/text/accessible-text.js @@ -1649,6 +1649,38 @@ describe('text.accessibleTextVirtual', () => { }); }); + describe('natively hidden elements', () => { + const tags = ['style', 'script', 'noscript', 'template']; + + it('should not use content as accessible name when directly referenced by aria-labelledby', () => { + tags.forEach(tag => { + fixture.innerHTML = `
<${tag} id="el-id">content`; + axe.testUtils.flatTreeSetup(fixture); + const target = axe.utils.querySelectorAll(axe._tree, '#t1')[0]; + assert.equal(axe.commons.text.accessibleTextVirtual(target), '', tag); + }); + }); + + it('should not use content when inside a hidden container referenced by aria-labelledby', () => { + tags.forEach(tag => { + fixture.innerHTML = ` +
+ + `; + axe.testUtils.flatTreeSetup(fixture); + const target = axe.utils.querySelectorAll(axe._tree, '#t1')[0]; + assert.equal( + axe.commons.text.accessibleTextVirtual(target), + 'Expected label', + tag + ); + }); + }); + }); + describe('text.accessibleText acceptance tests', () => { 'use strict'; // Tests borrowed from the AccName 1.1 testing docs From c864c8db93f6a16cc53eaf6da6cf5a6eae371490 Mon Sep 17 00:00:00 2001 From: "yugo.innami" Date: Thu, 16 Apr 2026 21:02:41 +0900 Subject: [PATCH 2/2] test(commons/text): add aria-label test for natively hidden aria-labelledby refs --- test/commons/text/accessible-text.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/commons/text/accessible-text.js b/test/commons/text/accessible-text.js index c718fcf00b..2dca9f026f 100644 --- a/test/commons/text/accessible-text.js +++ b/test/commons/text/accessible-text.js @@ -1679,6 +1679,15 @@ describe('text.accessibleTextVirtual', () => { ); }); }); + + it('should ignore aria-label as accessible name when directly referenced by aria-labelledby', () => { + tags.forEach(tag => { + fixture.innerHTML = `
<${tag} id="el-id" aria-label="aria-label">`; + axe.testUtils.flatTreeSetup(fixture); + const target = axe.utils.querySelectorAll(axe._tree, '#t1')[0]; + assert.equal(axe.commons.text.accessibleTextVirtual(target), '', tag); + }); + }); }); describe('text.accessibleText acceptance tests', () => {