Sha256: 5679048c62d70bf7b9d00ec723c909cc4a4714c3f9fec57fcc40641d4a943707

Contents?: true

Size: 1.44 KB

Versions: 104

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

104 entries across 104 versions & 1 rubygems

Version Path
govuk_publishing_components-21.16.3 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-21.16.2 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-21.16.1 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-21.16.0 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-21.15.2 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-21.15.1 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-21.15.0 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-21.14.0 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-21.13.5 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-21.13.4 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-21.13.3 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-21.13.2 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-21.13.1 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-21.13.0 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-21.12.0 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-21.11.0 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-21.10.0 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-21.9.0 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-21.8.1 node_modules/axe-core/lib/commons/text/label-text.js
govuk_publishing_components-21.8.0 node_modules/axe-core/lib/commons/text/label-text.js