Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

Commit

Permalink
Add CustomEvent polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
rianby64 authored and inexorabletash committed Jan 14, 2017
1 parent de07459 commit 40b7c32
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions tests/dom.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@

</div>

<div id="customev"></div>

<script type="text/javascript" src="../es5.js"></script>
<script type="text/javascript" src="../dom.js"></script>

Expand Down
11 changes: 11 additions & 0 deletions tests/dom_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" } }));
});

0 comments on commit 40b7c32

Please sign in to comment.