Sha256: 87c4997727ea937ebabf732bfc401350710847a2749e5501526103fffa87d1eb

Contents?: true

Size: 1.98 KB

Versions: 10

Compression:

Stored size: 1.98 KB

Contents

const { dom, color, text } = axe.commons;

if (!dom.isVisible(node, false)) {
	return true;
}

const noScroll = !!(options || {}).noScroll;
const bgNodes = [];
const bgColor = color.getBackgroundColor(node, bgNodes, noScroll);
const fgColor = color.getForegroundColor(node, noScroll, bgColor);

const nodeStyle = window.getComputedStyle(node);
const fontSize = parseFloat(nodeStyle.getPropertyValue('font-size'));
const fontWeight = nodeStyle.getPropertyValue('font-weight');
const bold =
	['bold', 'bolder', '600', '700', '800', '900'].indexOf(fontWeight) !== -1;

const cr = color.hasValidContrastRatio(bgColor, fgColor, fontSize, bold);

// truncate ratio to three digits while rounding down
// 4.499 = 4.49, 4.019 = 4.01
const truncatedResult = Math.floor(cr.contrastRatio * 100) / 100;

// if fgColor or bgColor are missing, get more information.
let missing;
if (bgColor === null) {
	missing = color.incompleteData.get('bgColor');
}

const equalRatio = truncatedResult === 1;
const shortTextContent =
	text.visibleVirtual(virtualNode, false, true).length === 1;
if (equalRatio) {
	missing = color.incompleteData.set('bgColor', 'equalRatio');
} else if (shortTextContent) {
	// Check that the text content is a single character long
	missing = 'shortTextContent';
}

// need both independently in case both are missing
const data = {
	fgColor: fgColor ? fgColor.toHexString() : undefined,
	bgColor: bgColor ? bgColor.toHexString() : undefined,
	contrastRatio: cr ? truncatedResult : undefined,
	fontSize: `${((fontSize * 72) / 96).toFixed(1)}pt (${fontSize}px)`,
	fontWeight: bold ? 'bold' : 'normal',
	missingData: missing,
	expectedContrastRatio: cr.expectedContrastRatio + ':1'
};

this.data(data);

// We don't know, so we'll put it into Can't Tell
if (
	fgColor === null ||
	bgColor === null ||
	equalRatio ||
	(shortTextContent && !cr.isValid)
) {
	missing = null;
	color.incompleteData.clear();
	this.relatedNodes(bgNodes);
	return undefined;
}

if (!cr.isValid) {
	this.relatedNodes(bgNodes);
}

return cr.isValid;

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
govuk_publishing_components-21.22.0 node_modules/axe-core/lib/checks/color/color-contrast.js
govuk_publishing_components-21.21.3 node_modules/axe-core/lib/checks/color/color-contrast.js
govuk_publishing_components-21.21.2 node_modules/axe-core/lib/checks/color/color-contrast.js
govuk_publishing_components-21.21.1 node_modules/axe-core/lib/checks/color/color-contrast.js
govuk_publishing_components-21.21.0 node_modules/axe-core/lib/checks/color/color-contrast.js
govuk_publishing_components-21.20.0 node_modules/axe-core/lib/checks/color/color-contrast.js
govuk_publishing_components-21.19.1 node_modules/axe-core/lib/checks/color/color-contrast.js
govuk_publishing_components-21.19.0 node_modules/axe-core/lib/checks/color/color-contrast.js
govuk_publishing_components-21.18.0 node_modules/axe-core/lib/checks/color/color-contrast.js
govuk_publishing_components-21.17.0 node_modules/axe-core/lib/checks/color/color-contrast.js