<commit_before>/*global aria, lookupTables, axe */

/**
 * Check if a given role is valid
 * @param  {String}  role The role to check
 * @return {Boolean}
 */
aria.isValidRole = function (role) {
	'use strict';
	if (lookupTables.role[role]) {
		return true;
	}

	return false;
};

/**
 * Get the roles that get name from contents
 * @return {Array}           Array of roles that match the type
 */
aria.getRolesWithNameFromContents = function () {
	return Object.keys(lookupTables.role).filter(function (r) {
		return lookupTables.role[r].nameFrom &&
			lookupTables.role[r].nameFrom.indexOf('contents') !== -1;
	});
};

/**
 * Get the roles that have a certain "type"
 * @param  {String} roleType The roletype to check
 * @return {Array}           Array of roles that match the type
 */
aria.getRolesByType = function (roleType) {
	return Object.keys(lookupTables.role).filter(function (r) {
		return lookupTables.role[r].type === roleType;
	});
};

/**
 * Get the "type" of role; either widget, composite, abstract, landmark or `null`
 * @param  {String} role The role to check
 * @return {Mixed}       String if a matching role and its type are found, otherwise `null`
 */
aria.getRoleType = function (role) {
	var r = lookupTables.role[role];

	return (r && r.type) || null;
};

/**
 * Get the required owned (children) roles for a given role
 * @param  {String} role The role to check
 * @return {Mixed}       Either an Array of required owned elements or `null` if there are none
 */
aria.requiredOwned = function (role) {
	'use strict';
	var owned = null,
		roles = lookupTables.role[role];

	if (roles) {
		owned = axe.utils.clone(roles.owned);
	}
	return owned;
};

/**
 * Get the required context (parent) roles for a given role
 * @param  {String} role The role to check
 * @return {Mixed}       Either an Array of required context elements or `null` if there are none
 */
aria.requiredContext = function (role) {
	'use strict';
	var context = null,
		roles = lookupTables.role[role];

	if (roles) {
		context = axe.utils.clone(roles.context);
	}
	return context;
};

/**
 * Get a list of CSS selectors of nodes that have an implicit role
 * @param  {String} role The role to check
 * @return {Mixed}       Either an Array of CSS selectors or `null` if there are none
 */
aria.implicitNodes = function (role) {
	'use strict';

	var implicit = null,
		roles = lookupTables.role[role];

	if (roles && roles.implicit) {
		implicit = axe.utils.clone(roles.implicit);
	}
	return implicit;
};

/**
 * Get the implicit role for a given node
 * @param  {HTMLElement} node The node to test
 * @return {Mixed}      Either the role or `null` if there is none
 */

aria.implicitRole = function (node) {
	'use strict';

	/*
	 * Filter function to reduce a list of roles to a valid list of roles for a nodetype
	 */
	var isValidImplicitRole = function (set, role) {
		if (!role.implicit) { return set; }

		if (role.implicit.some( function (implicitNodeType) {
			return axe.utils.matchesSelector(node, implicitNodeType);
		})) {
			set.push(role.name);
			return set;
		}

		return set;
	};

	/*
	 * Score a set of roles and aria-attributes by its optimal score
	 * E.g. score: 0, name: aria-required
	 */
	var sortRolesByOptimalAriaContext = function (roles, ariaAttributes) {
		var getScore = function (role) {
			var allowedAriaAttributes = aria.allowedAttr(role);
			return allowedAriaAttributes.reduce( function (score, attribute) {
				return score + (ariaAttributes.indexOf(attribute) > -1 ? 1 : 0);
			}, 0);
		};

		var scored = roles.map( function (role) {
			return { score: getScore(role), name: role };
		});

		var sorted = scored.sort( function (scoredRoleA, scoredRoleB) {
			return scoredRoleB.score - scoredRoleA.score;
		});

		return sorted.map( function (sortedRole) {
			return sortedRole.name;
		});
	};

	/*
	 * Create a list of { name / implicit } role mappings to filter on
	 */
	var roles = Object.keys(lookupTables.role).map( function (role) {
		var lookup = lookupTables.role[role];
		return { name: role, implicit: lookup && lookup.implicit };
	});

	/* Build a list of valid implicit roles for this node */<commit_msg>Corrected code comment, cleaned up filter function<commit_after>/*global aria, lookupTables, axe */

/**
 * Check if a given role is valid
 * @param  {String}  role The role to check
 * @return {Boolean}
 */
aria.isValidRole = function (role) {
	'use strict';
	if (lookupTables.role[role]) {
		return true;
	}

	return false;
};

/**
 * Get the roles that get name from contents
 * @return {Array}           Array of roles that match the type
 */
aria.getRolesWithNameFromContents = function () {
	return Object.keys(lookupTables.role).filter(function (r) {
		return lookupTables.role[r].nameFrom &&
			lookupTables.role[r].nameFrom.indexOf('contents') !== -1;
	});
};

/**
 * Get the roles that have a certain "type"
 * @param  {String} roleType The roletype to check
 * @return {Array}           Array of roles that match the type
 */
aria.getRolesByType = function (roleType) {
	return Object.keys(lookupTables.role).filter(function (r) {
		return lookupTables.role[r].type === roleType;
	});
};

/**
 * Get the "type" of role; either widget, composite, abstract, landmark or `null`
 * @param  {String} role The role to check
 * @return {Mixed}       String if a matching role and its type are found, otherwise `null`
 */
aria.getRoleType = function (role) {
	var r = lookupTables.role[role];

	return (r && r.type) || null;
};

/**
 * Get the required owned (children) roles for a given role
 * @param  {String} role The role to check
 * @return {Mixed}       Either an Array of required owned elements or `null` if there are none
 */
aria.requiredOwned = function (role) {
	'use strict';
	var owned = null,
		roles = lookupTables.role[role];

	if (roles) {
		owned = axe.utils.clone(roles.owned);
	}
	return owned;
};

/**
 * Get the required context (parent) roles for a given role
 * @param  {String} role The role to check
 * @return {Mixed}       Either an Array of required context elements or `null` if there are none
 */
aria.requiredContext = function (role) {
	'use strict';
	var context = null,
		roles = lookupTables.role[role];

	if (roles) {
		context = axe.utils.clone(roles.context);
	}
	return context;
};

/**
 * Get a list of CSS selectors of nodes that have an implicit role
 * @param  {String} role The role to check
 * @return {Mixed}       Either an Array of CSS selectors or `null` if there are none
 */
aria.implicitNodes = function (role) {
	'use strict';

	var implicit = null,
		roles = lookupTables.role[role];

	if (roles && roles.implicit) {
		implicit = axe.utils.clone(roles.implicit);
	}
	return implicit;
};

/**
 * Get the implicit role for a given node
 * @param  {HTMLElement} node The node to test
 * @return {Mixed}      Either the role or `null` if there is none
 */

aria.implicitRole = function (node) {
	'use strict';

	/*
	 * Filter function to reduce a list of roles to a valid list of roles for a nodetype
	 */
	var isValidImplicitRole = function (set, role) {
		var validForNodeType = function (implicitNodeTypeSelector) {
			return axe.utils.matchesSelector(node, implicitNodeTypeSelector);
		};

		if (role.implicit && role.implicit.some(validForNodeType)) {
			set.push(role.name);
		}

		return set;
	};

	/*
	 * Score a set of roles and aria-attributes by its optimal score
	 * E.g. [{score: 2, name: button}, {score: 1, name: main}]
	 */
	var sortRolesByOptimalAriaContext = function (roles, ariaAttributes) {
		var getScore = function (role) {
			var allowedAriaAttributes = aria.allowedAttr(role);
			return allowedAriaAttributes.reduce( function (score, attribute) {
				return score + (ariaAttributes.indexOf(attribute) > -1 ? 1 : 0);
			}, 0);
		};

		var scored = roles.map( function (role) {
			return { score: getScore(role), name: role };
		});

		var sorted = scored.sort( function (scoredRoleA, scoredRoleB) {
			return scoredRoleB.score - scoredRoleA.score;
		});

		return sorted.map( function (sortedRole) {
			return sortedRole.name;
		});
	};

	/*
	 * Create a list of { name / implicit } role mappings to filter on
	 */
	var roles = Object.keys(lookupTables.role).map( function (role) {
		var lookup = lookupTables.role[role];
		return { name: role, implicit: lookup && lookup.implicit };
	});

	/* Build a list of valid implicit roles for this node */
{'fraction_non_alphanumeric': 0.12526868879866251, 'fraction_numerical': 0.0015524241700501553, 'mean_word_length': 3.4882100750267955, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1173989', 'n_tokens_mistral': 2767, 'n_tokens_neox': 2606, 'n_words': 905}