Sha256: a4a6107753f661b39066c77e7b445ae810ca9b797d14b65cd0df5a56914a0dfb

Contents?: true

Size: 1.8 KB

Versions: 104

Compression:

Stored size: 1.8 KB

Contents

/* global text, aria */
/**
 * Get the accessible text for an element that can get its name from content
 *
 * @param {VirtualNode} element
 * @param {Object} context
 * @property {Bool} strict Should the name computation strictly follow AccName 1.1
 * @return {String} Accessible text
 */
text.subtreeText = function subtreeText(virtualNode, context = {}) {
	const { alreadyProcessed } = text.accessibleTextVirtual
	context.startNode = context.startNode || virtualNode;
	const { strict } = context;
	if (
		alreadyProcessed(virtualNode, context ) ||
		!aria.namedFromContents(virtualNode, { strict })
	) {
		return '';
	}

	return aria.getOwnedVirtual(virtualNode).reduce((contentText, child) => {
		return appendAccessibleText(contentText, child, context);
	}, '');
};

// TODO: Could do with an "HTML" lookup table, similar to ARIA,
//  where this sort of stuff can live.
const phrasingElements = [
	'A',
	'EM',
	'STRONG',
	'SMALL',
	'MARK',
	'ABBR',
	'DFN',
	'I',
	'B',
	'S',
	'U',
	'CODE',
	'VAR',
	'SAMP',
	'KBD',
	'SUP',
	'SUB',
	'Q',
	'CITE',
	'SPAN',
	'BDO',
	'BDI',
	'WBR',
	'INS',
	'DEL',
	'MAP',
	'AREA',
	'NOSCRIPT',
	'RUBY',
	'BUTTON',
	'LABEL',
	'OUTPUT',
	'DATALIST',
	'KEYGEN',
	'PROGRESS',
	'COMMAND',
	'CANVAS',
	'TIME',
	'METER',
	'#TEXT'
];

function appendAccessibleText(contentText, virtualNode, context) {
	const nodeName = virtualNode.actualNode.nodeName.toUpperCase();
	let contentTextAdd = text.accessibleTextVirtual(virtualNode, context);
	if (!contentTextAdd) {
		return contentText
	}

	if (!phrasingElements.includes(nodeName)) {
		// Append space, if necessary
		if (contentTextAdd[0] !== ' ') {
			contentTextAdd += ' ';
		}
		// Prepend space, if necessary
		if (contentText && contentText[contentText.length - 1] !== ' ') {
			contentTextAdd = ' ' + contentTextAdd;
		}
	}
	return contentText + contentTextAdd;
}

Version data entries

104 entries across 104 versions & 1 rubygems

Version Path
govuk_publishing_components-21.7.0 node_modules/axe-core/lib/commons/text/subtree-text.js
govuk_publishing_components-21.6.1 node_modules/axe-core/lib/commons/text/subtree-text.js
govuk_publishing_components-21.6.0 node_modules/axe-core/lib/commons/text/subtree-text.js
govuk_publishing_components-21.5.0 node_modules/axe-core/lib/commons/text/subtree-text.js
govuk_publishing_components-21.4.1 node_modules/axe-core/lib/commons/text/subtree-text.js
govuk_publishing_components-21.4.0 node_modules/axe-core/lib/commons/text/subtree-text.js
govuk_publishing_components-21.3.0 node_modules/axe-core/lib/commons/text/subtree-text.js
govuk_publishing_components-21.2.0 node_modules/axe-core/lib/commons/text/subtree-text.js
govuk_publishing_components-21.1.1 node_modules/axe-core/lib/commons/text/subtree-text.js
govuk_publishing_components-21.1.0 node_modules/axe-core/lib/commons/text/subtree-text.js
govuk_publishing_components-21.0.0 node_modules/axe-core/lib/commons/text/subtree-text.js
govuk_publishing_components-20.5.2 node_modules/axe-core/lib/commons/text/subtree-text.js
govuk_publishing_components-20.5.1 node_modules/axe-core/lib/commons/text/subtree-text.js
govuk_publishing_components-20.5.0 node_modules/axe-core/lib/commons/text/subtree-text.js
govuk_publishing_components-20.4.0 node_modules/axe-core/lib/commons/text/subtree-text.js
govuk_publishing_components-20.3.0 node_modules/axe-core/lib/commons/text/subtree-text.js
govuk_publishing_components-20.2.2 node_modules/axe-core/lib/commons/text/subtree-text.js
govuk_publishing_components-20.2.1 node_modules/axe-core/lib/commons/text/subtree-text.js
govuk_publishing_components-20.2.0 node_modules/axe-core/lib/commons/text/subtree-text.js
govuk_publishing_components-20.1.0 node_modules/axe-core/lib/commons/text/subtree-text.js