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(color-contrast): handle text that is outside overflow: hidden ancestor #4357

Merged
merged 4 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
ignore element from matches
  • Loading branch information
straker committed Mar 12, 2024
commit 714ee9d2f535c942fddf1abbf86aa57e0d8d2b25
24 changes: 17 additions & 7 deletions lib/rules/color-contrast-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import {
findUpVirtual,
visuallyOverlaps,
getRootNode,
isInert
isInert,
getOverflowHiddenAncestors
} from '../commons/dom';
import {
visibleVirtual,
removeUnicode,
sanitize,
isIconLigature
} from '../commons/text';
import { rectsOverlap } from '../commons/math';
import { isDisabled } from '../commons/forms';
import { getNodeFromTree, querySelectorAll, tokenList } from '../core/utils';

Expand Down Expand Up @@ -147,14 +149,22 @@ function colorContrastMatches(node, virtualNode) {
}
}

const rects = range.getClientRects();
for (let index = 0; index < rects.length; index++) {
const rects = Array.from(range.getClientRects());
const overflowNodes = getOverflowHiddenAncestors(virtualNode);
straker marked this conversation as resolved.
Show resolved Hide resolved
return rects.some(rect => {
//check to see if the rectangle impinges
if (visuallyOverlaps(rects[index], node)) {
return true;
const overlapps = visuallyOverlaps(rect, node);
straker marked this conversation as resolved.
Show resolved Hide resolved

if (!overflowNodes.length) {
return overlapps;
straker marked this conversation as resolved.
Show resolved Hide resolved
}
}
return false;

const withinOverflow = overflowNodes.some(overflowNode => {
return rectsOverlap(rect, overflowNode.boundingClientRect);
});

return overlapps && withinOverflow;
});
}

export default colorContrastMatches;
Expand Down
6 changes: 6 additions & 0 deletions test/integration/rules/color-contrast/color-contrast.html
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,9 @@
>
Hello world
</div>

<div style="overflow: hidden; width: 50px">
<div style="overflow: hidden; width: 25px">
<div id="ignore13" style="padding-left: 65px">Hello World</div>
</div>
</div>
13 changes: 13 additions & 0 deletions test/rule-matches/color-contrast-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,19 @@ describe('color-contrast-matches', function () {
assert.isFalse(rule.matches(target, axe.utils.getNodeFromTree(target)));
});

it('should not match text outside overflow', () => {
fixture.innerHTML = `
<div style="overflow: hidden; width: 50px;">
<div style="overflow: hidden; width: 25px">
<div id="target" style="padding-left: 65px;">Hello World</div>
</div>
</div>
`;
var target = fixture.querySelector('#target');
axe.testUtils.flatTreeSetup(fixture);
assert.isFalse(rule.matches(target, axe.utils.getNodeFromTree(target)));
});

if (shadowSupport) {
it('should match a descendant of an element across a shadow boundary', function () {
fixture.innerHTML =
Expand Down
Loading