Sha256: b16f30b03416db1277c7b4b7d97e0a5304b4586063a224eca5b99aac49daa2ce

Contents?: true

Size: 1.44 KB

Versions: 271

Compression:

Stored size: 1.44 KB

Contents

/* global dom, text */

/**
 * Return accessible text for an implicit and/or explicit HTML label element
 * @param {VirtualNode} element
 * @param {Object} context
 * @property {Bool} inControlContext
 * @property {Bool} inLabelledByContext
 * @return {String} Label text
 */
text.labelText = function labelText(virtualNode, context = {}) {
	const { alreadyProcessed } = text.accessibleTextVirtual;
	if (
		context.inControlContext ||
		context.inLabelledByContext ||
		alreadyProcessed(virtualNode, context)
	) {
		return '';
	}
	if (!context.startNode) {
		context.startNode = virtualNode;
	}

	const labelContext = { inControlContext: true, ...context };
	const explicitLabels = getExplicitLabels(virtualNode);
	const implicitLabel = dom.findUpVirtual(virtualNode, 'label');

	let labels;
	if (implicitLabel) {
		labels = [...explicitLabels, implicitLabel];
		labels.sort(axe.utils.nodeSorter);
	} else {
		labels = explicitLabels;
	}

	return labels
		.map(label => text.accessibleText(label, labelContext))
		.filter(text => text !== '')
		.join(' ');
};

/**
 * Find a non-ARIA label for an element
 * @private
 * @param {VirtualNode} element The VirtualNode instance whose label we are seeking
 * @return {HTMLElement} The label element, or null if none is found
 */
function getExplicitLabels({ actualNode }) {
	if (!actualNode.id) {
		return [];
	}
	return dom.findElmsInContext({
		elm: 'label',
		attr: 'for',
		value: actualNode.id,
		context: actualNode
	});
}

Version data entries

271 entries across 271 versions & 1 rubygems

Version Path
govuk_publishing_components-30.4.1 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-30.4.0 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-30.3.0 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-30.2.1 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-30.2.0 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-30.1.0 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-30.0.0 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-29.15.3 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-29.15.2 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-29.15.1 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-29.15.0 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-29.14.0 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-29.13.0 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-29.12.1 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-29.12.0 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-29.11.0 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-29.10.0 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-29.9.0 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-29.8.0 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-29.7.0 node_modules/axe-core/lib/commons/text/label-text.js