krambuhl / custom-event-polyfill

Polyfill for creating CustomEvents on IE9/10/11 if native implementation is missing.
MIT License
112 stars 19 forks source link

Safari feature detection fails #2

Closed dgraham closed 9 years ago

dgraham commented 9 years ago

The typeof window.CustomEvent === 'function' test returns false for Safari. This Safari bug incorrectly reports constructable types as "object" rather than "function", which causes the polyfill to override Safari's native support for the CustomEvent constructor.

Another way to detect support for the constructor is to call it and trap any resulting errors. This would work around the bug in Safari.

function supported() {
  try {
    var event = new CustomEvent('test', {detail: 'supported'})
    return event.detail === 'supported'
  } catch (error) {
    return false
  }
}
Paul-Browne commented 9 years ago

Isn't this a WebKit bug?

If so, you could just change the if statement to...

if ((!window.CustomEvent || typeof window.CustomEvent !== 'function')&&!( 'WebkitAppearance' in document.documentElement.style)) {
krambuhl commented 9 years ago

Updated the polyfill with behavior as suggested by @dgraham, thanks! Added some test cases that can be run against sauce labs locally (cant get their badges working correctly). These are currently passing against ie 9-11 on win7 and safari 7, 8 and 8.2 (ios).

All changes are in effect on v0.2.1

dgraham commented 9 years ago

:zap: