/**
 * 判断元素是否与选择器匹配
 *
 * @param {Element} element
 * @param {String} selector
 * @return {Boolean}
 * @api public
 *
 * source by `dom-matches`
 * https://github.com/necolas/dom-matches.git
 */
function isElementMatch(element, selector) {
  // Vendor-specific implementations of `Element.prototype.matches()`.
  var proto = window.Element.prototype;
  var nativeMatches = proto.matches || proto.mozMatchesSelector || proto.msMatchesSelector || proto.oMatchesSelector || proto.webkitMatchesSelector;

  if (!element || element.nodeType !== 1) {
    return false;
  }

  var parentElem = element.parentNode;

  // use native 'matches'
  if (nativeMatches) {
    return nativeMatches.call(element, selector);
  }

  // native support for `matches` is missing and a fallback is required
  var nodes = parentElem.querySelectorAll(selector);

  for (var i = 0; i < nodes.length; i++) {
    if (nodes[i] === element) return true;
  }

  return false;
}

export default isElementMatch;
