Sha256: b7bf2d2c921fd843d7a471794c343eadde8113e17290863eefaae51183bc4f52

Contents?: true

Size: 1.8 KB

Versions: 261

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.messageKey = 'no-shared-label';
} else if (uniqueLabels.length === 0 && sharedLabels.length > 0) {
	data.messageKey = 'no-unique-label';
}

this.data(data);
return false;

Version data entries

261 entries across 261 versions & 1 rubygems

Version Path
govuk_publishing_components-28.0.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-27.20.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-27.19.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-27.18.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-27.17.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-27.16.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-27.15.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-27.14.2 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-27.14.1 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-27.14.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-27.13.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-27.12.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-27.11.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-27.10.5 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-27.10.4 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-27.10.3 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-27.10.2 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-27.10.1 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-27.10.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-27.9.2 node_modules/axe-core/lib/checks/forms/group-labelledby.js