Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/commons/text/accessible-text-virtual.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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;
}
Expand Down
41 changes: 41 additions & 0 deletions test/commons/text/accessible-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,47 @@ 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 = `<div id="t1" aria-labelledby="el-id"></div><${tag} id="el-id">content</${tag}>`;
axe.testUtils.flatTreeSetup(fixture);
const target = axe.utils.querySelectorAll(axe._tree, '#t1')[0];
assert.equal(axe.commons.text.accessibleTextVirtual(target), '', tag);
});
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this is worth having a test for too. This isn't about hiding content. The element itself should be ignored. There are elements with hidden content that aren't themselves hidden (canvas, iframe). The fix here is specifically about hiding the element.

Suggested change
it('should ignore aria-label as accessible name when directly referenced by aria-labelledby', () => {
tags.forEach(tag => {
fixture.innerHTML = `<div id="t1" aria-labelledby="el-id"></div><${tag} id="el-id" aria-label="aria-label"></${tag}>`;
axe.testUtils.flatTreeSetup(fixture);
const target = axe.utils.querySelectorAll(axe._tree, '#t1')[0];
assert.equal(axe.commons.text.accessibleTextVirtual(target), '', tag);
});
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added the test case.

it('should not use content when inside a hidden container referenced by aria-labelledby', () => {
tags.forEach(tag => {
fixture.innerHTML = `
<div id="t1" aria-labelledby="hidden-container"></div>
<div id="hidden-container" aria-hidden="true">
<${tag}>content</${tag}>
Expected label
</div>
`;
axe.testUtils.flatTreeSetup(fixture);
const target = axe.utils.querySelectorAll(axe._tree, '#t1')[0];
assert.equal(
axe.commons.text.accessibleTextVirtual(target),
'Expected label',
tag
);
});
});

it('should ignore aria-label as accessible name when directly referenced by aria-labelledby', () => {
tags.forEach(tag => {
fixture.innerHTML = `<div id="t1" aria-labelledby="el-id"></div><${tag} id="el-id" aria-label="aria-label"></${tag}>`;
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', () => {
'use strict';
// Tests borrowed from the AccName 1.1 testing docs
Expand Down