Sha256: 5567464fa93a8163a1ea981983dba06af5cd81f622dd81fce431c5eceae7b420

Contents?: true

Size: 1.7 KB

Versions: 104

Compression:

Stored size: 1.7 KB

Contents

const { dom } = axe.commons;
const getIsListItemRole = (role, tagName) => {
	return role === 'listitem' || (tagName === 'LI' && !role);
};

const getHasListItem = (hasListItem, tagName, isListItemRole) => {
	return hasListItem || (tagName === 'LI' && isListItemRole) || isListItemRole;
};

let base = {
	badNodes: [],
	isEmpty: true,
	hasNonEmptyTextNode: false,
	hasListItem: false,
	liItemsWithRole: 0
};

let out = virtualNode.children.reduce((out, { actualNode }) => {
	/*eslint 
		max-statements: ["error", 20]
		complexity: ["error", 12]
		*/
	const tagName = actualNode.nodeName.toUpperCase();

	if (actualNode.nodeType === 1 && dom.isVisible(actualNode, true, false)) {
		const role = (actualNode.getAttribute('role') || '').toLowerCase();
		const isListItemRole = getIsListItemRole(role, tagName);

		out.hasListItem = getHasListItem(out.hasListItem, tagName, isListItemRole);

		if (isListItemRole) {
			out.isEmpty = false;
		}
		if (tagName === 'LI' && !isListItemRole) {
			out.liItemsWithRole++;
		}
		if (tagName !== 'LI' && !isListItemRole) {
			out.badNodes.push(actualNode);
		}
	}
	if (actualNode.nodeType === 3) {
		if (actualNode.nodeValue.trim() !== '') {
			out.hasNonEmptyTextNode = true;
		}
	}

	return out;
}, base);

const virtualNodeChildrenOfTypeLi = virtualNode.children.filter(
	({ actualNode }) => {
		return actualNode.nodeName.toUpperCase() === 'LI';
	}
);

const allLiItemsHaveRole =
	out.liItemsWithRole > 0 &&
	virtualNodeChildrenOfTypeLi.length === out.liItemsWithRole;

if (out.badNodes.length) {
	this.relatedNodes(out.badNodes);
}

const isInvalidListItem = !(
	out.hasListItem ||
	(out.isEmpty && !allLiItemsHaveRole)
);
return isInvalidListItem || !!out.badNodes.length || out.hasNonEmptyTextNode;

Version data entries

104 entries across 104 versions & 1 rubygems

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