Sha256: 71336092ec05bddd664bd0f94bc9330197c8ed1f4fc963dc1c9f18dce48df48a

Contents?: true

Size: 1.8 KB

Versions: 114

Compression:

Stored size: 1.8 KB

Contents

const { dom, text } = axe.commons;

const type = axe.utils.escapeSelector(node.type);
const name = axe.utils.escapeSelector(node.name);
const doc = dom.getRootNode(node);
const data = {
	name: node.name,
	type: node.type
};

const matchingNodes = Array.from(
	doc.querySelectorAll(`input[type="${type}"][name="${name}"]`)
);
// There is only one node with this name & type, so there's no need for a group
if (matchingNodes.length <= 1) {
	this.data(data);
	return true;
}

let sharedLabels = dom.idrefs(node, 'aria-labelledby').filter(label => !!label); // Filter for "null" labels
let uniqueLabels = sharedLabels.slice();

// Figure out which labels are unique, which are shared by all items, or neither
matchingNodes.forEach(groupItem => {
	if (groupItem === node) {
		return;
	}
	// Find new labels associated with current groupItem
	const labels = dom
		.idrefs(groupItem, 'aria-labelledby')
		.filter(newLabel => newLabel);

	sharedLabels = sharedLabels.filter(sharedLabel =>
		labels.includes(sharedLabel)
	);
	uniqueLabels = uniqueLabels.filter(
		uniqueLabel => !labels.includes(uniqueLabel)
	);
});

const accessibleTextOptions = {
	// Prevent following further aria-labelledby refs:
	inLabelledByContext: true
};

// filter items with no accessible name, do this last for performance reasons
uniqueLabels = uniqueLabels.filter(labelNode =>
	text.accessibleText(labelNode, accessibleTextOptions)
);
sharedLabels = sharedLabels.filter(labelNode =>
	text.accessibleText(labelNode, accessibleTextOptions)
);

if (uniqueLabels.length > 0 && sharedLabels.length > 0) {
	this.data(data);
	return true;
}

if (uniqueLabels.length > 0 && sharedLabels.length === 0) {
	data.failureCode = 'no-shared-label';
} else if (uniqueLabels.length === 0 && sharedLabels.length > 0) {
	data.failureCode = 'no-unique-label';
}

this.data(data);
return false;

Version data entries

114 entries across 114 versions & 1 rubygems

Version Path
govuk_publishing_components-21.22.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.21.3 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.21.2 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.21.1 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.21.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.20.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.19.1 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.19.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.18.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.17.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.16.3 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.16.2 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.16.1 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.16.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.15.2 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.15.1 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.15.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.14.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.13.5 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.13.4 node_modules/axe-core/lib/checks/forms/group-labelledby.js