const EventKit = require('event-kit')

module.exports =
function listen (element, eventName, selector, handler) {
  var innerHandler = function (event) {
    if (selector) {
      var currentTarget = event.target
      while (currentTarget) {
        if (currentTarget.matches && currentTarget.matches(selector)) {
          handler({
            type: event.type,
            currentTarget: currentTarget,
            target: event.target,
            preventDefault: function () {
              event.preventDefault()
            },
            originalEvent: event
          })
        }
        if (currentTarget === element) break
        currentTarget = currentTarget.parentNode
      }
    } else {
      handler({
        type: event.type,
        currentTarget: event.currentTarget,
        target: event.target,
        preventDefault: function () {
          event.preventDefault()
        },
        originalEvent: event
      })
    }
  }

  element.addEventListener(eventName, innerHandler)

  return new EventKit.Disposable(function () {
    element.removeEventListener(eventName, innerHandler)
  })
}

const EventKit = require('event-kit')

module.exports =
function listen (element, eventName, selector, handler) {
  var innerHandler = function (event) {
    if (selector) {
      var currentTarget = event.target
      while (true) {
        if (currentTarget.matches && currentTarget.matches(selector)) {
          handler({
            type: event.type,
            currentTarget: currentTarget,
            target: event.target,
            preventDefault: function () {
              event.preventDefault()
            },
            originalEvent: event
          })
        }
        if (currentTarget === element) break
        currentTarget = currentTarget.parentNode
      }
    } else {
      handler({
        type: event.type,
        currentTarget: event.currentTarget,
        target: event.target,
        preventDefault: function () {
          event.preventDefault()
        },
        originalEvent: event
      })
    }
  }

  element.addEventListener(eventName, innerHandler)

  return new EventKit.Disposable(function () {
    element.removeEventListener(eventName, innerHandler)
  })
}

--- 
+++ 
@@ -5,7 +5,7 @@
   var innerHandler = function (event) {
     if (selector) {
       var currentTarget = event.target
-      while (currentTarget) {
+      while (true) {
         if (currentTarget.matches && currentTarget.matches(selector)) {
           handler({
             type: event.type,
Stop looping when currentTarget becomes undefined
