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-16.25.0 node_modules/axe-core/lib/checks/lists/only-listitems.js
govuk_publishing_components-16.24.0 node_modules/axe-core/lib/checks/lists/only-listitems.js
govuk_publishing_components-16.23.0 node_modules/axe-core/lib/checks/lists/only-listitems.js
govuk_publishing_components-16.22.0 node_modules/axe-core/lib/checks/lists/only-listitems.js
govuk_publishing_components-16.21.0 node_modules/axe-core/lib/checks/lists/only-listitems.js
govuk_publishing_components-16.20.1 node_modules/axe-core/lib/checks/lists/only-listitems.js
govuk_publishing_components-16.20.0 node_modules/axe-core/lib/checks/lists/only-listitems.js
govuk_publishing_components-16.19.0 node_modules/axe-core/lib/checks/lists/only-listitems.js
govuk_publishing_components-16.18.0 node_modules/axe-core/lib/checks/lists/only-listitems.js
govuk_publishing_components-16.17.0 node_modules/axe-core/lib/checks/lists/only-listitems.js
govuk_publishing_components-16.16.0 node_modules/axe-core/lib/checks/lists/only-listitems.js
govuk_publishing_components-16.15.0 node_modules/axe-core/lib/checks/lists/only-listitems.js
govuk_publishing_components-16.14.1 node_modules/axe-core/lib/checks/lists/only-listitems.js
govuk_publishing_components-16.14.0 node_modules/axe-core/lib/checks/lists/only-listitems.js
govuk_publishing_components-16.13.0 node_modules/axe-core/lib/checks/lists/only-listitems.js
govuk_publishing_components-16.12.0 node_modules/axe-core/lib/checks/lists/only-listitems.js
govuk_publishing_components-16.11.0 node_modules/axe-core/lib/checks/lists/only-listitems.js
govuk_publishing_components-16.10.1 node_modules/axe-core/lib/checks/lists/only-listitems.js
govuk_publishing_components-16.10.0 node_modules/axe-core/lib/checks/lists/only-listitems.js
govuk_publishing_components-16.9.2 node_modules/axe-core/lib/checks/lists/only-listitems.js