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.13.3 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.13.2 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.13.1 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.13.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.12.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.11.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.10.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.9.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.8.1 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.8.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.7.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.6.1 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.6.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.5.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.4.1 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.4.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.3.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.2.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.1.1 node_modules/axe-core/lib/checks/forms/group-labelledby.js
govuk_publishing_components-21.1.0 node_modules/axe-core/lib/checks/forms/group-labelledby.js