function closest(element, nodes, until) {
  var elementToCheck = element;

  while (elementToCheck !== null && elementToCheck !== until) {
    if (elementToCheck.nodeType === Node.ELEMENT_NODE && (nodes.indexOf(elementToCheck.nodeName) > -1 || nodes.indexOf(elementToCheck) > -1)) {
      return elementToCheck;
    }

    if (elementToCheck.host && elementToCheck.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
      elementToCheck = elementToCheck.host;
    } else {
      elementToCheck = elementToCheck.parentNode;
    }
  }

  return null;
}
Goes up the DOM tree (including given element) until it finds an element that matches the nodes or nodes name.
This method goes up through web components.

@param {HTMLElement} element Element from which traversing is started
@param {Array} nodes Array of elements or Array of elements name
@param {HTMLElement} [until]
@returns {HTMLElement|null}