From 40b7c32fb89409368a21e43ad9a87ee68865d197 Mon Sep 17 00:00:00 2001 From: Andres Rios Date: Sat, 14 Jan 2017 06:57:52 +0300 Subject: [PATCH] Add CustomEvent polyfill --- README.md | 3 ++- dom.js | 17 +++++++++++++++++ tests/dom.html | 2 ++ tests/dom_tests.js | 11 +++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ec9f52b..6d903d0 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,8 @@ DOM * `EventTarget.removeEventListener(event, handler)` - for IE8+ * Event: `target`, `currentTarget`, `eventPhase`, `bubbles`, `cancelable`, `timeStamp`, `defaultPrevented`, `stopPropagation()`, `cancelBubble()` * Non-standard Event helpers for IE7- - adapted from -[QuirksMode](http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html) +* [Custom Events](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent) +* [QuirksMode](http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html) * `window.addEvent(EventTarget, event, handler)` * `window.removeEvent(EventTarget, event, handler)` * [DOMTokenList](https://dom.spec.whatwg.org/#interface-domtokenlist) - `classList`[spec](https://dom.spec.whatwg.org/#dom-element-classlist), `relList`[spec](https://html.spec.whatwg.org/multipage/semantics.html#the-link-element) diff --git a/dom.js b/dom.js index b580447..3d6ce7c 100644 --- a/dom.js +++ b/dom.js @@ -213,6 +213,23 @@ }); }()); + // CustomEvent + // https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent + // Needed for: IE + (function () { + if ('CustomEvent' in global) { + if ( typeof global.CustomEvent === "function" ) return false; + } + function CustomEvent ( event, params ) { + params = params || { bubbles: false, cancelable: false, detail: undefined }; + var evt = document.createEvent( 'CustomEvent' ); + evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail ); + return evt; + } + CustomEvent.prototype = window.Event.prototype; + global.CustomEvent = CustomEvent; + })(); + // Shim for DOM Events for IE7- // http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html // Use addEvent(object, event, handler) instead of object.addEventListener(event, handler) diff --git a/tests/dom.html b/tests/dom.html index 7d275bd..18b7b35 100644 --- a/tests/dom.html +++ b/tests/dom.html @@ -43,6 +43,8 @@ +
+ diff --git a/tests/dom_tests.js b/tests/dom_tests.js index a1cfd75..63f610a 100644 --- a/tests/dom_tests.js +++ b/tests/dom_tests.js @@ -469,3 +469,14 @@ QUnit.test('Mixin ChildNode: before, after, replaceWith - Behavior when nodes co s3.after(s4, s3, s2); assert.equal(div.textContent, '1432', 'after should work when successor is removed'); }); + +QUnit.test('CustomEvent: addEventListener and dispatchEvent', function(assert) { + var done = assert.async(); + var div = document.getElementById('customev'); + div.addEventListener('customevent', function(e) { + assert.equal(e.detail.teststring, "test string"); + done(); + }); + assert.ok(CustomEvent instanceof Function); + div.dispatchEvent(new CustomEvent("customevent", { detail: { teststring: "test string" } })); +});