<commit_before>import chain from '../util/chain';
import matchesSelector from '../util/matches-selector';

var isShadowSelectorRegex = /(:host|::content|::shadow|\/deep\/)/;
var ShadowRoot = window.ShadowRoot;

function parseEvent (e) {
  var parts = e.split(/\s+/);
  return {
    name: parts.shift(),
    delegate: parts.join(' ')
  };
}

function makeDelegateHandler (elem, handler, delegate) {
  var isShadowSelector = isShadowSelectorRegex.test(delegate);
  return function (e) {
    var current = isShadowSelector ? e.path[0] : e.target;
    while (current && current !== document && current !== elem.parentNode) {
      if (matchesSelector(current, delegate)) {
        return handler(elem, e, current);
      }

      current = current.parentNode;

      // This ensures that we pass by any ShadowRoots as calling matches() on
      // them doesn't work.
      if (isShadowSelector && current instanceof ShadowRoot) {
        current = current.host;
      }
    }
  };
}

function makeNormalHandler (elem, handler) {
  return function (e) {
    handler(elem, e, elem);
  };
}

export default function (elem, evts) {
  Object.keys(evts || {}).forEach(function (evt) {
    var handler = chain(evts[evt]);<commit_msg>More robust way of checking if the current element is a shadow root.<commit_after>import chain from '../util/chain';
import matchesSelector from '../util/matches-selector';

var isShadowSelectorRegex = /(:host|::content|::shadow|\/deep\/)/;
var ShadowRoot = window.ShadowRoot;

function parseEvent (e) {
  var parts = e.split(/\s+/);
  return {
    name: parts.shift(),
    delegate: parts.join(' ')
  };
}

function makeDelegateHandler (elem, handler, delegate) {
  var isShadowSelector = isShadowSelectorRegex.test(delegate);
  return function (e) {
    var current = isShadowSelector ? e.path[0] : e.target;
    while (current && current !== document && current !== elem.parentNode) {
      if (matchesSelector(current, delegate)) {
        return handler(elem, e, current);
      }

      current = current.parentNode;

      if (current && ShadowRoot && current instanceof ShadowRoot) {
        current = current.host;
      }
    }
  };
}

function makeNormalHandler (elem, handler) {
  return function (e) {
    handler(elem, e, elem);
  };
}

export default function (elem, evts) {
  Object.keys(evts || {}).forEach(function (evt) {
    var handler = chain(evts[evt]);
{'fraction_non_alphanumeric': 0.14303586321934947, 'fraction_numerical': 0.0008340283569641367, 'mean_word_length': 3.377737226277372, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1843006', 'n_tokens_mistral': 703, 'n_tokens_neox': 703, 'n_words': 195}