Skip to content

Commit

Permalink
fix(color-contrast): skip ligature icons (#3867)
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoFiers authored Jan 23, 2023
1 parent ec9f507 commit 9486288
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
34 changes: 26 additions & 8 deletions lib/rules/color-contrast-matches.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/* global document */
import { getAccessibleRefs } from '../commons/aria';
import { findUpVirtual, visuallyOverlaps, getRootNode } from '../commons/dom';
import { visibleVirtual, removeUnicode, sanitize } from '../commons/text';
import {
visibleVirtual,
removeUnicode,
sanitize,
isIconLigature
} from '../commons/text';
import { isDisabled } from '../commons/forms';
import { getNodeFromTree, querySelectorAll, tokenList } from '../core/utils';

Expand Down Expand Up @@ -121,13 +126,7 @@ function colorContrastMatches(node, virtualNode) {
return false;
}

const visibleText = visibleVirtual(virtualNode, false, true);
const removeUnicodeOptions = {
emoji: true,
nonBmp: false,
punctuations: true
};
if (!visibleText || !removeUnicode(visibleText, removeUnicodeOptions)) {
if (!hasRealTextChildren(virtualNode)) {
return false;
}

Expand All @@ -154,3 +153,22 @@ function colorContrastMatches(node, virtualNode) {
}

export default colorContrastMatches;

const removeUnicodeOptions = {
emoji: true,
nonBmp: false,
punctuations: true
};

function hasRealTextChildren(virtualNode) {
const visibleText = visibleVirtual(virtualNode, false, true);
if (
visibleText === '' ||
removeUnicode(visibleText, removeUnicodeOptions) === ''
) {
return false;
}
return virtualNode.children.some(
vChild => vChild.props.nodeName === '#text' && !isIconLigature(vChild)
);
}
28 changes: 28 additions & 0 deletions test/rule-matches/color-contrast-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,4 +537,32 @@ describe('color-contrast-matches', function () {
assert.isTrue(rule.matches(input, axe.utils.getNodeFromTree(input)));
});
}

describe('with font icons', () => {
before(async () => {
const materialFont = new FontFace(
'Material Icons',
'url(/test/assets/MaterialIcons.woff2)'
);
await materialFont.load();
document.fonts.add(materialFont);
});

it('is false for a single icon', () => {
fixture.innerHTML =
'<div id="target" style="font-family: \'Material Icons\'">delete</div>';
const target = fixture.querySelector('#target');
axe.testUtils.flatTreeSetup(fixture);
assert.isFalse(rule.matches(target, axe.utils.getNodeFromTree(target)));
});

it('is false for multiple icons', () => {
fixture.innerHTML = `<div id="target" style="font-family: \'Material Icons\'">
check star favorite
</div>`;
const target = fixture.querySelector('#target');
axe.testUtils.flatTreeSetup(fixture);
assert.isFalse(rule.matches(target, axe.utils.getNodeFromTree(target)));
});
});
});

0 comments on commit 9486288

Please sign in to comment.