Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

events-polyfill

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

events-polyfill - npm Package Compare versions

Comparing version 1.1.1 to 1.1.2

event-constructor-polyfill.min.js

380

event-listener-polyfill.js
(function() {
var EventListenerHelper = {};
/**
* Create EventTarget
*/
EventListenerHelper.polyfillEventTarget = function() {
if(typeof EventTarget === 'undefined') {
window.EventTarget = Node;
}
};
if(typeof EventTarget === 'undefined') {
window.EventTarget = Node;
}

@@ -18,9 +14,3 @@ /**

EventListenerHelper.prefixEventType = function(type) {
return ['', 'webkit', 'moz', 'ms', 'o'].map(function(prefix) {
return prefix + type;
});
};
EventListenerHelper.eventTypes = {
var eventTypes = {
wheel: ['wheel', 'mousewheel', 'DOMMouseScroll']

@@ -35,133 +25,11 @@ };

].forEach(function(type) {
EventListenerHelper.eventTypes[type] = EventListenerHelper.prefixEventType(type);
EventListenerHelper.eventTypes[type] = ['', 'webkit', 'moz', 'ms', 'o']
.map(function(prefix) {
return prefix + type;
});
});
/**
* Check supported options
*/
EventListenerHelper.supportedOptions = {
once: false,
passive: false,
capture: false
};
EventListenerHelper.getSupportedOptions = function() {
document.createDocumentFragment().addEventListener('test', function() {}, {
get once() {
EventListenerHelper.supportedOptions.once = true;
return false;
},
get passive() {
EventListenerHelper.supportedOptions.passive = true;
return false;
},
get capture() {
EventListenerHelper.supportedOptions.capture = true;
return false;
}
});
// useful shortcuts to detect if options are all/some supported
EventListenerHelper.supportedOptions.all = EventListenerHelper.supportedOptions.once && EventListenerHelper.supportedOptions.passive && EventListenerHelper.supportedOptions.capture;
EventListenerHelper.supportedOptions.some = EventListenerHelper.supportedOptions.once || EventListenerHelper.supportedOptions.passive || EventListenerHelper.supportedOptions.capture;
};
/**
* Polyfill listener
*/
EventListenerHelper.getFormattedListener = function(listener) {
if(typeof listener === 'function') {
return listener;
} else if((typeof listener === 'object') && (typeof listener.handleEvent === 'function')) {
return listener.handleEvent;
} else {
return listener;
// throw new Error('Unsupported listener type for addEventListener');
}
};
EventListenerHelper.getFormattedOptions = function(options) {
switch(typeof options) {
case 'boolean':
options = { capture: options };
break;
case 'undefined':
options = { capture: false };
break;
case 'object':
break;
default:
throw new Error('Unsupported options type for addEventListener');
break;
}
options.once = (typeof options.once === 'boolean') ? options.once : false;
options.passive = (typeof options.passive === 'boolean') ? options.passive : false;
options.capture = (typeof options.capture === 'boolean') ? options.capture : false;
options.polyfill = (typeof options.polyfill === 'boolean') ? options.polyfill : true;
return options;
};
EventListenerHelper.getFormattedArguments = function(type, listener, options) {
return {
type: type,
listener: EventListenerHelper.getFormattedListener(listener),
options: EventListenerHelper.getFormattedOptions(options)
};
};
EventListenerHelper.registerEventListener = function(target, formattedArguments) {
var key = formattedArguments.type + '-' + (formattedArguments.options.capture ? '1' : '0');
if(typeof target.__eventListeners === 'undefined') {
target.__eventListeners = {};
}
if(typeof target.__eventListeners[key] === 'undefined') {
target.__eventListeners[key] = [];
}
target.__eventListeners[key].push(formattedArguments);
};
EventListenerHelper.unregisterEventListener = function(target, formattedArguments) {
var key = formattedArguments.type + '-' + (formattedArguments.options.capture ? '1' : '0');
if(
(typeof target.__eventListeners !== 'undefined') &&
(typeof target.__eventListeners[key] !== 'undefined')
) {
var map = target.__eventListeners[key];
for(var i = 0; i < map.length; i++) {
if(map[i].listener === formattedArguments.listener) {
map.splice(i, 1);
}
}
if(map.length === 0) {
delete target.__eventListeners[key];
}
}
};
EventListenerHelper.getRegisteredEventListener = function(target, formattedArguments) {
var key = formattedArguments.type + '-' + (formattedArguments.options.capture ? '1' : '0');
if(
(typeof target.__eventListeners !== 'undefined') &&
(typeof target.__eventListeners[key] !== 'undefined')
) {
var map = target.__eventListeners[key];
for(var i = 0; i < map.length; i++) {
if(map[i].listener === formattedArguments.listener) {
return map[i];
}
}
}
return null;
};
EventListenerHelper.polyfillEventTypesName = function(type, target) {
var eventTypesPolyfiller = EventListenerHelper.eventTypes[type];
if(typeof eventTypesPolyfiller === 'undefined') {
var polyfillEventTypesName = function(type, target) {
var eventTypesPolyfiller = eventTypes[type];
if(eventTypesPolyfiller === void 0) {
return type;

@@ -183,3 +51,38 @@ } else {

EventListenerHelper.keyCodes = [
/**
* Check supported options
*/
var supportedOptions = {
once: false,
passive: false,
capture: false
};
document.createDocumentFragment().addEventListener('test', function() {}, {
get once() {
supportedOptions.once = true;
return false;
},
get passive() {
supportedOptions.passive = true;
return false;
},
get capture() {
supportedOptions.capture = true;
return false;
}
});
// useful shortcuts to detect if options are all/some supported
supportedOptions.all = supportedOptions.once && supportedOptions.passive && supportedOptions.capture;
supportedOptions.some = supportedOptions.once || supportedOptions.passive || supportedOptions.capture;
/**
* Check supported events attributes
*/
var keyCodes = [
null,

@@ -414,43 +317,126 @@ null,

window.buildKeyCodes = function() {
window.addEventListener('keydown', function(event) {
if(EventListenerHelper.keyCodes[event.keyCode] && EventListenerHelper.keyCodes[event.keyCode] !== event.code) {
console.warn('Detect same keyCode for 2 different codes : ' + event.code + ' (current) vs ' + EventListenerHelper.keyCodes[event.keyCode] + '(old)');
}
EventListenerHelper.keyCodes[event.keyCode] = event.code;
event.preventDefault();
event.stopPropagation();
});
// window.buildKeyCodes = function() {
// window.addEventListener('keydown', function(event) {
// if(EventListenerHelper.keyCodes[event.keyCode] && EventListenerHelper.keyCodes[event.keyCode] !== event.code) {
// console.warn('Detect same keyCode for 2 different codes : ' + event.code + ' (current) vs ' + EventListenerHelper.keyCodes[event.keyCode] + '(old)');
// }
// EventListenerHelper.keyCodes[event.keyCode] = event.code;
// event.preventDefault();
// event.stopPropagation();
// });
// };
//
// window.endBuildKeyCodes = function() {
// console.log(JSON.stringify(EventListenerHelper.keyCodes, null, '\t'));
// };
/**
* Polyfill listener
*/
/**
* Formatter functions
*/
var getFormattedListener = function(listener) {
if(typeof listener === 'function') {
return listener;
} else if((typeof listener === 'object') && (typeof listener.handleEvent === 'function')) {
return listener.handleEvent;
} else {
// to support Symbol
return function(event) {
listener(event);
};
}
};
window.endBuildKeyCodes = function() {
console.log(JSON.stringify(EventListenerHelper.keyCodes, null, '\t'));
var getFormattedOptions = function(options) {
switch(typeof options) {
case 'boolean':
options = { capture: options };
break;
case 'undefined':
options = { capture: false };
break;
case 'object':
break;
default:
throw new Error('Unsupported options type for addEventListener');
break;
}
options.once = (typeof options.once === 'boolean') ? options.once : false;
options.passive = (typeof options.passive === 'boolean') ? options.passive : false;
options.capture = (typeof options.capture === 'boolean') ? options.capture : false;
options.polyfill = (typeof options.polyfill === 'boolean') ? options.polyfill : true;
return options;
};
var getFormattedArguments = function(type, listener, options) {
return {
type: type,
listener:getFormattedListener(listener),
options: getFormattedOptions(options)
};
};
EventListenerHelper.polyfillEventTypesObject = function(event) {
if(event instanceof KeyboardEvent) {
if(!('code' in event)) {
event.code = EventListenerHelper.keyCodes[event.keyCode];
}
/**
* Event listener stack functions
*/
var registerEventListener = function(target, formattedArguments) {
var key = formattedArguments.type + '-' + (formattedArguments.options.capture ? '1' : '0');
if(target.__eventListeners === void 0) {
target.__eventListeners = {};
}
if(arget.__eventListeners[key] === void 0) {
target.__eventListeners[key] = [];
}
target.__eventListeners[key].push(formattedArguments);
};
EventListenerHelper.polyfilledConstructors = {};
var unregisterEventListener = function(target, formattedArguments) {
var key = formattedArguments.type + '-' + (formattedArguments.options.capture ? '1' : '0');
if(
(target.__eventListeners !== void 0) &&
(target.__eventListeners[key] !== void 0)
) {
var map = target.__eventListeners[key];
for(var i = 0; i < map.length; i++) {
if(map[i].listener === formattedArguments.listener) {
map.splice(i, 1);
}
}
EventListenerHelper.polyfillListener = function() {
EventListenerHelper.polyfillListenerConstructor(EventTarget);
if(!(window instanceof EventTarget)) { EventListenerHelper.polyfillListenerConstructor(Window); }
if(map.length === 0) {
delete target.__eventListeners[key];
}
}
};
EventListenerHelper.polyfillListenerConstructor = function(constructor, name) {
var polyfilledConstructor = {
name: name,
addEventListener: constructor.prototype.addEventListener,
removeEventListener: constructor.prototype.removeEventListener
};
var getRegisteredEventListener = function(target, formattedArguments) {
var key = formattedArguments.type + '-' + (formattedArguments.options.capture ? '1' : '0');
if(
(target.__eventListeners !== void 0) &&
(target.__eventListeners[key] !== void 0)
) {
var map = target.__eventListeners[key];
for(var i = 0; i < map.length; i++) {
if(map[i].listener === formattedArguments.listener) {
return map[i];
}
}
}
return null;
};
var polyfillListenerConstructor = function(constructor) {
var addEventListener = constructor.prototype.addEventListener;
constructor.prototype.addEventListener = function(type, listener, options) {
var formattedArguments = EventListenerHelper.getFormattedArguments(type, listener, options);
var registeredEventListener = EventListenerHelper.getRegisteredEventListener(this, formattedArguments);
var formattedArguments = getFormattedArguments(type, listener, options);
var registeredEventListener = getRegisteredEventListener(this, formattedArguments);

@@ -463,3 +449,3 @@ if(!registeredEventListener) {

vendorArguments.type = formattedArguments.options.polyfill ?
EventListenerHelper.polyfillEventTypesName(formattedArguments.type) :
polyfillEventTypesName(formattedArguments.type) :
formattedArguments.type

@@ -470,3 +456,3 @@ ;

// once
if(formattedArguments.options.once && !EventListenerHelper.supportedOptions.once) {
if(formattedArguments.options.once && !supportedOptions.once) {
this.removeEventListener(type, listener, options);

@@ -476,3 +462,3 @@ }

// passive
if(formattedArguments.options.passive && !EventListenerHelper.supportedOptions.passive) {
if(formattedArguments.options.passive && !supportedOptions.passive) {
event.preventDefault = function() {

@@ -486,3 +472,10 @@ throw new Error('Unable to preventDefault inside passive event listener invocation.');

event.type = formattedArguments.type;
EventListenerHelper.polyfillEventTypesObject(event);
// polyfill event attributes
if(event instanceof KeyboardEvent) {
if(!('code' in event)) {
//noinspection JSAnnotator
event.code = keyCodes[event.keyCode];
}
}
}

@@ -493,3 +486,3 @@

vendorArguments.options = EventListenerHelper.supportedOptions.some ?
vendorArguments.options = supportedOptions.some ?
formattedArguments.options : formattedArguments.options.capture;

@@ -501,5 +494,5 @@

EventListenerHelper.registerEventListener(this, formattedArguments);
registerEventListener(this, formattedArguments);
polyfilledConstructor.addEventListener.call(
addEventListener.call(
this,

@@ -513,9 +506,10 @@ vendorArguments.type,

var removeEventListener = constructor.prototype.removeEventListener;
constructor.prototype.removeEventListener = function(type, listener, options) {
var formattedArguments = EventListenerHelper.getFormattedArguments(type, listener, options);
var registeredEventListener = EventListenerHelper.getRegisteredEventListener(this, formattedArguments);
var formattedArguments = getFormattedArguments(type, listener, options);
var registeredEventListener = getRegisteredEventListener(this, formattedArguments);
if(registeredEventListener) {
EventListenerHelper.unregisterEventListener(this, formattedArguments);
polyfilledConstructor.removeEventListener.call(
unregisterEventListener(this, formattedArguments);
removeEventListener.call(
this,

@@ -527,24 +521,14 @@ registeredEventListener.vendorArguments.type,

} else {
polyfilledConstructor.removeEventListener.call(this, type, listener, options);
removeEventListener.call(this, type, listener, options);
}
};
EventListenerHelper.polyfilledConstructors[name] = polyfilledConstructor;
};
/**
* Polyfill all
*/
EventListenerHelper.polyfillAll = function() {
EventListenerHelper.polyfillEventTarget();
EventListenerHelper.getSupportedOptions();
EventListenerHelper.polyfillListener();
};
polyfillListenerConstructor(EventTarget);
if(!(window instanceof EventTarget)) {
polyfillListenerConstructor(Window);
}
EventListenerHelper.polyfillAll();
window.EventListenerHelper = EventListenerHelper;
// var div = document.createElement('div');

@@ -551,0 +535,0 @@ // document.body.innerHTML = '';

{
"name": "events-polyfill",
"version": "1.1.1",
"version": "1.1.2",
"description": "Polyfill event : EventListener, EventTarget, CustomEvent, MouseEvent, KeyboardEvent",
"main": "event-constructor-polyfill.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"start": "npm run min.listener && npm run min.constructor",
"min.listener": "uglifyjs event-listener-polyfill.js --stats -m -o event-listener-polyfill.min.js",
"min.constructor": "uglifyjs event-constructor-polyfill.js --stats -m -o event-constructor-polyfill.min.js"
},

@@ -14,3 +16,6 @@ "repository": {

"author": "valentin",
"license": "MIT"
"license": "MIT",
"devDependencies": {
"uglify-js": "^2.7.5"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc