Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
`CustomEvent` polyfill for Firefox. Bug at https://bugzilla.mozilla.org/show_bug.cgi?id=427537
/*global self, Document, XMLDocument, HTMLDocument, DataContainerEvent*/
!self.CustomEvent && self.DataContainerEvent &&
(function () {
"use strict";
var
$proto = "prototype"
, Doc = Document[$proto]
, XML_Doc = XMLDocument[$proto]
, HTML_Doc = HTMLDocument[$proto]
// expose a CustomEvent interface alias to DataContainerEvent
, DCE = (self.CustomEvent = DataContainerEvent)[$proto]
, $detail = "detail"
, $create_event = "createEvent"
, $real_create_event = "_" + $create_event
, get_detail = function () {
return this.getData($detail);
}
;
// store the real createEvent funcs as _createEvent
Doc[$real_create_event] = Doc[$create_event];
XML_Doc[$real_create_event] = XML_Doc[$create_event];
HTML_Doc[$real_create_event] = HTML_Doc[$create_event];
Doc[$create_event] =
XML_Doc[$create_event] =
HTML_Doc[$create_event] =
function (event) {
// "CustomEvents" is explicitly unsupported; I don't agree with Mozilla's "s" leniancy
if (event === "CustomEvent") {
event = "DataContainerEvent";
}
return this[$real_create_event].call(this, event);
};
DCE.initCustomEvent = function (type, can_bubble, cancelable, detail) {
DCE.initEvent.call(this, type, can_bubble, cancelable);
this.setData($detail, detail);
};
if (Object.defineProperty) {
Object.defineProperty(DCE, $detail, {
get: get_detail
, enumerable: true
, configurable: true
});
} else if (DCE.__defineGetter__) {
DCE.__defineGetter__($detail, get_detail);
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment