Sha256: bf371e128e340d5fc912967c8a3394701f534ce53ea89e08a6e2913167f75f97

Contents?: true

Size: 1.42 KB

Versions: 375

Compression:

Stored size: 1.42 KB

Contents

/* global text, aria */

/**
 * Get the accessible text using native HTML methods only
 * @param {VirtualNode} element
 * @param {Object} context
 * @property {Bool} debug Enable logging for formControlValue
 * @return {String} Accessible text
 */
text.nativeTextAlternative = function nativeTextAlternative(
	virtualNode,
	context = {}
) {
	const { actualNode } = virtualNode;
	if (
		actualNode.nodeType !== 1 ||
		['presentation', 'none'].includes(aria.getRole(actualNode))
	) {
		return '';
	}

	const textMethods = findTextMethods(virtualNode);
	// Find the first step that returns a non-empty string
	let accName = textMethods.reduce((accName, step) => {
		return accName || step(virtualNode, context);
	}, '');

	if (context.debug) {
		axe.log(accName || '{empty-value}', actualNode, context);
	}
	return accName;
};

/**
 * Get accessible text functions for a specific native HTML element
 * @private
 * @param {VirtualNode} element
 * @return {Function[]} Array of native accessible name computation methods
 */
function findTextMethods(virtualNode) {
	const { nativeElementType, nativeTextMethods } = text;
	const nativeType = nativeElementType.find(({ matches }) => {
		return axe.commons.matches(virtualNode, matches);
	});

	// Use concat because namingMethods can be a string or an array of strings
	const methods = nativeType ? [].concat(nativeType.namingMethods) : [];

	return methods.map(methodName => nativeTextMethods[methodName]);
}

Version data entries

375 entries across 375 versions & 1 rubygems

Version Path
govuk_publishing_components-25.3.0 node_modules/axe-core/lib/commons/text/native-text-alternative.js
govuk_publishing_components-25.2.3 node_modules/axe-core/lib/commons/text/native-text-alternative.js
govuk_publishing_components-25.2.2 node_modules/axe-core/lib/commons/text/native-text-alternative.js
govuk_publishing_components-25.2.1 node_modules/axe-core/lib/commons/text/native-text-alternative.js
govuk_publishing_components-25.2.0 node_modules/axe-core/lib/commons/text/native-text-alternative.js
govuk_publishing_components-25.1.0 node_modules/axe-core/lib/commons/text/native-text-alternative.js
govuk_publishing_components-25.0.0 node_modules/axe-core/lib/commons/text/native-text-alternative.js
govuk_publishing_components-24.21.1 node_modules/axe-core/lib/commons/text/native-text-alternative.js
govuk_publishing_components-24.21.0 node_modules/axe-core/lib/commons/text/native-text-alternative.js
govuk_publishing_components-24.20.0 node_modules/axe-core/lib/commons/text/native-text-alternative.js
govuk_publishing_components-24.19.0 node_modules/axe-core/lib/commons/text/native-text-alternative.js
govuk_publishing_components-24.18.5 node_modules/axe-core/lib/commons/text/native-text-alternative.js
govuk_publishing_components-24.18.4 node_modules/axe-core/lib/commons/text/native-text-alternative.js
govuk_publishing_components-24.18.3 node_modules/axe-core/lib/commons/text/native-text-alternative.js
govuk_publishing_components-24.18.2 node_modules/axe-core/lib/commons/text/native-text-alternative.js
govuk_publishing_components-24.18.1 node_modules/axe-core/lib/commons/text/native-text-alternative.js
govuk_publishing_components-24.18.0 node_modules/axe-core/lib/commons/text/native-text-alternative.js
govuk_publishing_components-24.17.0 node_modules/axe-core/lib/commons/text/native-text-alternative.js
govuk_publishing_components-24.16.1 node_modules/axe-core/lib/commons/text/native-text-alternative.js
govuk_publishing_components-24.16.0 node_modules/axe-core/lib/commons/text/native-text-alternative.js