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

fix(link-name): pass landmark content as link text #2617

Merged
merged 3 commits into from
Nov 9, 2020
Merged
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
22 changes: 20 additions & 2 deletions lib/commons/text/subtree-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,32 @@ import getOwnedVirtual from '../aria/get-owned-virtual';
function subtreeText(virtualNode, context = {}) {
const { alreadyProcessed } = accessibleTextVirtual;
context.startNode = context.startNode || virtualNode;
const { strict } = context;
const { strict, inControlContext, inLabelledByContext } = context;
if (
alreadyProcessed(virtualNode, context) ||
!namedFromContents(virtualNode, { strict })
virtualNode.props.nodeType !== 1
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Even when subtreeDescendant is true, we need to return an empty string for things that aren't nodes.

) {
return '';
}

if (
!namedFromContents(virtualNode, { strict }) &&
!context.subtreeDescendant
) {
return '';
}

/**
* Note: Strictly speaking if a child isn't named from content and it has no accessible name
* accName says to ignore it. Browsers do this fairly consistently, but screen readers have
* chosen to ignore this, but only for direct content, not for labels / aria-labelledby.
* That way in `a[href] > article > #text` the text is used for the accessible name,
* See: https://github.com/dequelabs/axe-core/issues/1461
*/
if (!strict) {
const subtreeDescendant = !inControlContext && !inLabelledByContext;
context = { subtreeDescendant, ...context };
}
return getOwnedVirtual(virtualNode).reduce((contentText, child) => {
return appendAccessibleText(contentText, child, context);
}, '');
Expand Down
8 changes: 4 additions & 4 deletions test/commons/text/accessible-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,12 @@ describe('text.accessibleTextVirtual', function() {
axe.testUtils.flatTreeSetup(fixture);

var target = axe.utils.querySelectorAll(axe._tree, '#t2label')[0];
// Chrome 72: This is This is a label of
// Firefox 62: This is ARIA Label
// Safari 12.0: This is This is a label of
// Chrome 86: This is This is a label of
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we marking what the browser's accessibility tree says or what screen readers with those browsers say?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the browser's name in the acc tree.

// Firefox 82: This is ARIA Label everything
// Safari 14.0: This is This is a label of everything
assert.equal(
axe.commons.text.accessibleTextVirtual(target),
'This is This is a label of'
'This is This is a label of everything'
);
});

Expand Down
3 changes: 3 additions & 0 deletions test/integration/rules/link-name/link-name.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<a href="#" id="pass2" aria-label="link text"></a>
<a href="#" id="pass3" aria-labelledby="linklabel"></a>
<a href="#" id="pass4" title="title text"></a>
<a href="#" id="pass5">
<article>Some sectioning content</article>
</a>

<div id="linklabel">Text</div>

Expand Down
2 changes: 1 addition & 1 deletion test/integration/rules/link-name/link-name.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
["#violation4"],
["#violation5"]
],
"passes": [["#pass1"], ["#pass2"], ["#pass3"], ["#pass4"]]
"passes": [["#pass1"], ["#pass2"], ["#pass3"], ["#pass4"], ["#pass5"]]
}