Sha256: c620d86f951372373b37a457d8efbdb6a425dd471d5b324f55050cd7d1eee4e3

Contents?: true

Size: 1.91 KB

Versions: 104

Compression:

Stored size: 1.91 KB

Contents

/* global dom, axe */
/**
 * Find the virtual node and call dom.fundUpVirtual
 *
 * **WARNING:** this should be used sparingly, as it's not even close to being performant
 * @method findUp
 * @memberof axe.commons.dom
 * @instance
 * @param {HTMLElement} element The starting HTMLElement
 * @param {String} target The selector for the HTMLElement
 * @return {HTMLElement|null} Either the matching HTMLElement or `null` if there was no match
 */
dom.findUp = function(element, target) {
	return dom.findUpVirtual(
		axe.utils.getNodeFromTree(axe._tree[0], element),
		target
	);
};

/**
 * recusively walk up the DOM, checking for a node which matches a selector
 *
 * **WARNING:** this should be used sparingly, as it's not even close to being performant
 * @method findUpVirtual
 * @memberof axe.commons.dom
 * @instance
 * @param {VirtualNode} element The starting virtualNode
 * @param {String} target The selector for the HTMLElement
 * @return {HTMLElement|null} Either the matching HTMLElement or `null` if there was no match
 */
dom.findUpVirtual = function(element, target) {
	/*eslint complexity: ["error", 12]*/
	let parent;

	parent = element.actualNode;
	// virtualNode will have a shadowId if the element lives inside a shadow DOM or is
	// slotted into a shadow DOM
	if (!element.shadowId && typeof element.actualNode.closest === 'function') {
		// non-shadow DOM elements
		let match = element.actualNode.closest(target);
		if (match) {
			return match;
		}
		return null;
	}
	// handle shadow DOM elements and older browsers
	do {
		// recursively walk up the DOM, checking each parent node
		parent = parent.assignedSlot ? parent.assignedSlot : parent.parentNode;
		if (parent && parent.nodeType === 11) {
			parent = parent.host;
		}
	} while (
		parent &&
		!axe.utils.matchesSelector(parent, target) &&
		parent !== document.documentElement
	);

	if (!axe.utils.matchesSelector(parent, target)) {
		return null;
	}
	return parent;
};

Version data entries

104 entries across 104 versions & 1 rubygems

Version Path
govuk_publishing_components-21.16.3 node_modules/axe-core/lib/commons/dom/find-up.js
govuk_publishing_components-21.16.2 node_modules/axe-core/lib/commons/dom/find-up.js
govuk_publishing_components-21.16.1 node_modules/axe-core/lib/commons/dom/find-up.js
govuk_publishing_components-21.16.0 node_modules/axe-core/lib/commons/dom/find-up.js
govuk_publishing_components-21.15.2 node_modules/axe-core/lib/commons/dom/find-up.js
govuk_publishing_components-21.15.1 node_modules/axe-core/lib/commons/dom/find-up.js
govuk_publishing_components-21.15.0 node_modules/axe-core/lib/commons/dom/find-up.js
govuk_publishing_components-21.14.0 node_modules/axe-core/lib/commons/dom/find-up.js
govuk_publishing_components-21.13.5 node_modules/axe-core/lib/commons/dom/find-up.js
govuk_publishing_components-21.13.4 node_modules/axe-core/lib/commons/dom/find-up.js
govuk_publishing_components-21.13.3 node_modules/axe-core/lib/commons/dom/find-up.js
govuk_publishing_components-21.13.2 node_modules/axe-core/lib/commons/dom/find-up.js
govuk_publishing_components-21.13.1 node_modules/axe-core/lib/commons/dom/find-up.js
govuk_publishing_components-21.13.0 node_modules/axe-core/lib/commons/dom/find-up.js
govuk_publishing_components-21.12.0 node_modules/axe-core/lib/commons/dom/find-up.js
govuk_publishing_components-21.11.0 node_modules/axe-core/lib/commons/dom/find-up.js
govuk_publishing_components-21.10.0 node_modules/axe-core/lib/commons/dom/find-up.js
govuk_publishing_components-21.9.0 node_modules/axe-core/lib/commons/dom/find-up.js
govuk_publishing_components-21.8.1 node_modules/axe-core/lib/commons/dom/find-up.js
govuk_publishing_components-21.8.0 node_modules/axe-core/lib/commons/dom/find-up.js