Skip to content

Commit

Permalink
fix(link-in-text-block): allow links with identical colors (#3861)
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoFiers authored and straker committed Jan 23, 2023
1 parent aaf44e9 commit 6761f36
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 22 deletions.
10 changes: 10 additions & 0 deletions doc/check-options.md
Expand Up @@ -36,6 +36,7 @@
- [region](#region)
- [inline-style-property](#inline-style-property)
- [invalid-children](#invalid-children)
- [link-in-text-block](#link-in-text-block)

## How Checks Work

Expand Down Expand Up @@ -538,3 +539,12 @@ This evaluation method is used in the `list` and `definition-list` rule to deter
| `validNodeNames` | Nodes without role allowed as children |
| `validRoles` | Roles allowed on child elements |
| `divGroups` | Whether the child nodes can be grouped in a div without any role (false by default) |

### link-in-text-block

This evaluation method is used in the `link-in-text-block` rule and tests that either the foreground color or the background color has sufficient contrast between the link text and the surrounding text.

| Option | Default | Description |
| ----------------------- | :------ | :-------------------------------------------------------------------------- |
| `requiredContrastRatio` | `3` | Minimum contrast needed to pass the check between text or background colors |
| `allowSameColor` | `true` | Whether links with colors identical to its surroundings should pass |
6 changes: 5 additions & 1 deletion lib/checks/color/link-in-text-block-evaluate.js
Expand Up @@ -25,7 +25,7 @@ function isBlock(elm) {
}

function linkInTextBlockEvaluate(node, options) {
const { requiredContrastRatio } = options;
const { requiredContrastRatio, allowSameColor } = options;

if (isBlock(node)) {
return false;
Expand Down Expand Up @@ -86,6 +86,10 @@ function linkInTextBlockEvaluate(node, options) {
return undefined;
}

if (allowSameColor && textContrast === 1 && backgroundContrast === 1) {
return true;
}

// Report bgContrast only if the background changes but text color stays the same
if (textContrast === 1 && backgroundContrast > 1) {
this.data({
Expand Down
3 changes: 2 additions & 1 deletion lib/checks/color/link-in-text-block.json
Expand Up @@ -2,7 +2,8 @@
"id": "link-in-text-block",
"evaluate": "link-in-text-block-evaluate",
"options": {
"requiredContrastRatio": 3
"requiredContrastRatio": 3,
"allowSameColor": true
},
"metadata": {
"impact": "serious",
Expand Down
57 changes: 38 additions & 19 deletions test/checks/color/link-in-text-block.js
Expand Up @@ -98,7 +98,7 @@ describe('link-in-text-block', function () {

it('passes the selected node and closest ancestral block element', function () {
fixture.innerHTML =
'<div> <span style="display:block; color: #100" id="parent">' +
'<div> <span style="display:block; color: #010" id="parent">' +
' <p style="display:inline"><a href="" id="link">' +
' link text ' +
' </a> inside block </p> inside block' +
Expand Down Expand Up @@ -192,24 +192,6 @@ describe('link-in-text-block', function () {
assert.equal(checkContext._relatedNodes[0], linkElm.parentNode);
});

it('returns false with fgContrast key if nodes have same foreground color and same background color', function () {
var linkElm = getLinkElm(
{
color: 'black'
},
{
color: '#000'
}
);
assert.isFalse(
axe.testUtils
.getCheckEvaluate('link-in-text-block')
.call(checkContext, linkElm)
);
assert.equal(checkContext._data.messageKey, 'fgContrast');
assert.equal(checkContext._relatedNodes[0], linkElm.parentNode);
});

it('returns false with fgContrast key if nodes have insufficient foreground contrast and same background color', function () {
var linkElm = getLinkElm(
{
Expand Down Expand Up @@ -353,5 +335,42 @@ describe('link-in-text-block', function () {
parentBackgroundColor: '#f0f0f0'
});
});

describe('options.allowSameColor', () => {
it('when true, passes when link and text colors are identical', () => {
var linkElm = getLinkElm(
{
color: 'black'
},
{
color: '#000'
}
);
assert.isTrue(
axe.testUtils
.getCheckEvaluate('link-in-text-block')
.call(checkContext, linkElm, { allowSameColor: true })
);
assert.equal(checkContext._relatedNodes[0], linkElm.parentNode);
});

it('when false, fails when link and text colors are identical', () => {
var linkElm = getLinkElm(
{
color: 'black'
},
{
color: '#000'
}
);
assert.isFalse(
axe.testUtils
.getCheckEvaluate('link-in-text-block')
.call(checkContext, linkElm, { allowSameColor: false })
);
assert.equal(checkContext._data.messageKey, 'fgContrast');
assert.equal(checkContext._relatedNodes[0], linkElm.parentNode);
});
});
});
});
Expand Up @@ -105,6 +105,13 @@ <h1>Color change tests</h1>
>
</p>

<p style="color: black">
paragraph of text (pass: text color has sufficient contrast against paragraph)
<a style="text-decoration: none; color: black" href="#" id="pass-same-colors">
Link text</a
>
</p>

<p style="color: black">
paragraph of text (fail: text color has insufficient contrast against
paragraph)
Expand Down
Expand Up @@ -11,7 +11,8 @@
["#pass-different-weight"],
["#pass-different-size"],
["#pass-background-color"],
["#pass-text-color"]
["#pass-text-color"],
["#pass-same-colors"]
],
"incomplete": [["#incomplete-low-contrast-parent-has-gradient"]]
}

0 comments on commit 6761f36

Please sign in to comment.