forked from dequelabs/axe-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclosest.js
More file actions
30 lines (25 loc) · 713 Bytes
/
closest.js
File metadata and controls
30 lines (25 loc) · 713 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import matches from './matches';
/**
* closest implementation that operates on a VirtualNode
*
* @method closest
* @memberof axe.utils
* @param {VirtualNode} vNode VirtualNode to match
* @param {String} selector CSS selector string
* @return {VirtualNode | null}
*/
function closest(vNode, selector) {
while (vNode) {
if (matches(vNode, selector)) {
return vNode;
}
// the top node of the tree will have parent === null, so a
// undefined parent means we are in a disconnected tree
if (typeof vNode.parent === 'undefined') {
throw new TypeError('Cannot resolve parent for non-DOM nodes');
}
vNode = vNode.parent;
}
return null;
}
export default closest;