Skip to content

Commit

Permalink
fix(link-name): pass landmark content as link text (#2617)
Browse files Browse the repository at this point in the history
* fix(link-name): pass landmark content as link text

* Update lib/commons/text/subtree-text.js

* test(integration): add link-name with article
  • Loading branch information
WilcoFiers authored Nov 9, 2020
1 parent 6a677b9 commit e77992e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
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
) {
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
// 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"]]
}

0 comments on commit e77992e

Please sign in to comment.