import matches from '../util/matches-selector';

function readonly (obj, prop, val) {
  Object.defineProperty(obj, prop, {
    configurable: true,
    get () {
      return val;
    }
  });
}

function parseEvent (e) {
  const parts = e.split(' ');
  const name = parts.shift();
  const selector = parts.join(' ').trim();
  return {
    name: name,
    selector: selector
  };
}

function makeDelegateHandler (elem, handler, parsed) {
  return function (e) {
    let current = e.target;
    const selector = parsed.selector;
    while (current && current !== elem.parentNode) {
      if (matches(current, selector)) {
        readonly(e, 'currentTarget', current);
        readonly(e, 'delegateTarget', elem);
        return handler(e);
      }
      current = current.parentNode;
    }
  };
}

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

function bindEvent (elem, event, handler) {
  const parsed = parseEvent(event);
  const { name, selector } = parsed;
  const capture = selector && (name === 'blur' || name === 'focus');
  handler = selector ? makeDelegateHandler(elem, handler, parsed) : makeNormalHandler(elem, handler);
  elem.addEventListener(name, handler, capture);
}

export default function events (opts) {
  const events = opts.events;
  return function (elem) {
    Object.keys(events).forEach(function (name) {
      bindEvent(elem, name, events[name].bind(elem));
    });
  };
}

@@ -224,101 +224,200 @@
 nst 
-parts = e.split(' ');%0A  const name = parts.shift();%0A  const selector = parts.join(' ').trim()
+indexOfSpace = e.indexOf(' ');%0A  const hasSpace = indexOfSpace %3E 0;%0A  const name = hasSpace ? e.substring(0, indexOfSpace) : e;%0A  const selector = hasSpace ? e.substring(indexOfSpace + 1) : ''
 ;%0A  
@@ -1429,18 +1429,24 @@
 s.events
+ %7C%7C %7B%7D
 ;%0A
-
   return
@@ -1472,50 +1472,27 @@
     
-Object.keys(events).forEach(function (name
+for (name in events
 ) %7B%0A
@@ -1549,15 +1549,13 @@
 ));%0A
+
     %7D
-);
 %0A  %7D

Improve performance when binding events and parsing event specs.