npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lycoris-nubila/event-constructor-polyfill

v1.0.4

Published

- [Introduction](#introduction) - [Usage](#usage) - [Affected browsers](#affected-browsers) - [Notes](#notes) - [Argument validation](#argument-validation) - [`MouseEvent` key modifiers](#mouseevent-key-modifiers) - [Bypassing the check for th

Downloads

2

Readme

event-constructor-polyfill

Table of Contents

Introduction

The original method of dynamically creating events in JavaScript used an API inspired by Java:

var event = document.createEvent('MouseEvent');
event.initMouseEvent('click', false, false, window, null, null, null, 100, 0);

Many event interfaces have their own initializer methods with wildly differing argument lists. For example, initMouseEvent() takes more arguments than initKeyboardEvent().

The new method of creating events (part of the DOM Living Standard) is to use a simple constructor function.

var event = new MouseEvent('click', {
    clientX: 100,
    clientY: 0
});

The constructor functions work the same way across all event interfaces, each taking only two arguments:

  1. A string describing the type of the event, and
  2. an optional plain object which provides the values of the event's properties.

This polyfill enables the use of this constructor syntax in browsers that don't natively support it.

Usage

Include the script:

HTML

<script src="main.min.js"></script>

Or

NPM

npm i --save @lycoris-nubila/event-constructor-polyfill

Javascript

required('@lycoris-nubila/event-constructor-polyfill');

Angular polyfill (polyfills.ts)

import '@lycoris-nubila/event-constructor-polyfill';

Then, use the polyfilled constructor functions as documented:

var simpleEvent = new CustomEvent('foo');
var detailedEvent = new CustomEvent('bar', { /* ... */ });

Affected browsers

The main goal of this polyfill is to allow the use of event constructors in Chrome 15 and above, Firefox 11 and above, Safari 6 and above, and Internet Explorer 9 and above. It has no effect in IE8 or below.

Notes

Argument validation

In accordance with the spec, the constructor functions created by this polyfill will throw a TypeError under the following scenarios:

  • No arguments are provided
  • The second argument is not an Object (and is not null or undefined)
  • The new keyword is not used

MouseEvent key modifiers

When creating a MouseEvent, KeyboardEvent, or WheelEvent, there are extra key modifiers that can be provided (in addition to altKey, ctrlKey, metaKey, and shiftKey). These are defined in the EventModifierInit dictionary. For example:

var k = new KeyboardEvent('keydown', { modifierCapsLock: true });
k.getModifierState('CapsLock');
// => true

These extra modifiers work as expected for the polyfilled KeyboardEvent and WheelEvent constructors, but are ignored in the polyfilled MouseEvent constructor. This is because initKeyboardEvent() and initWheelEvent() accept these extra modifiers, but initMouseEvent() only accepts altKey, ctrlKey, metaKey, and shiftKey.

Bypassing the check for the new keyword

As mentioned above, there are checks in place to ensure that the polyfilled constructors can't be invoked without the new keyword:

var g = GamepadEvent('gamepadconnected'); // TypeError
var k = KeyboardEvent.call(new KeyboardEvent('keydown'), 'keyup'); // TypeError
var c = CustomEvent.call(CustomEvent.prototype, 'foo') // TypeError

The test for usage of the new keyword can be fooled, but only by statements that are specifically crafted to do so, such as the following:

var dummy = Object.create(MouseEvent.prototype);
MouseEvent.call(dummy, 'click'); // No TypeError is thrown.

However, the constructor functions always return a new object; they never interact with the this object on which they are called. In the example above, trying to access dummy.type will throw a TypeError, because dummy was not initialized by the constructor; it's just a copy of MouseEvent.prototype.

List of polyfilled constructors

Some of the events below don't have any public documentation for their initializer method. These events were still polyfilled because (a) their initializer method would only require a single unique argument, and / or (b) some other evidence was found for the existence of the initializer method.

Event interface | Initializer method | Notes ---|---|--- AnimationEvent | initAnimationEvent() (MDN) ClipboardEvent | initClipboardEvent() (W3C) | In Chrome (tested in 52 & 53), the native constructor throws a TypeError ("illegal constructor"). CloseEvent | initCloseEvent() (MDN) CompositionEvent | initCompositionEvent() (MDN) CustomEvent | initCustomEvent() (MDN) DeviceMotionEvent | initDeviceMotionEvent() (MSDN) DeviceOrientationEvent | initDeviceOrientationEvent() (MSDN) DragEvent | initDragEvent() (MSDN) ErrorEvent | initErrorEvent() (MSDN) FocusEvent | initFocusEvent() (MSDN) GamepadEvent | initGamepadEvent() HashChangeEvent | initHashChangeEvent() KeyboardEvent | initKeyboardEvent() (MSDN) | initKeyboardEvent() is always used; never initKeyEvent(). MediaStreamEvent | initMediaStreamEvent() MessageEvent | initMessageEvent() (MSDN) MouseEvent | initMouseEvent() (MDN) PageTransitionEvent | initPageTransitionEvent() PointerEvent | initPointerEvent() (MSDN) | Accounts for the fact that the interface is prefixed in IE 10 as MSPointerEvent (the constructor is still polyfilled and made available as PointerEvent). As of September 2016, PointerEvent is not implemented in Chrome (but is under development). PopStateEvent | initPopStateEvent() (MSDN) ProgressEvent | initProgressEvent() (MDN) StorageEvent | initStorageEvent() (MDN) TouchEvent | initTouchEvent() (Apple) | Accounts for the fact that Chrome does not follow the W3C spec and expects a different argument list to initTouchEvent. TransitionEvent | initTransitionEvent() (MDN) UIEvent | initUIEvent() (MDN) UserProximityEvent | initUserProximityEvent() WebGLContextEvent | initWebGLContextEvent() WheelEvent | initWheelEvent() (MSDN)

List of omitted constructors

The following event constructors are not polyfilled by this script.

Event interface | Reason for omission ---|--- GestureEvent | Non-standard. MouseScrollEvent | Non-standard. WheelEvent is the standards-based interface for mouse wheel scrolling events, and initWheelEvent() is supported in IE9+. MouseWheelEvent | Non-standard. WheelEvent is the standards-based interface for mouse wheel scrolling events, and initWheelEvent() is supported in IE9+. MutationEvent | Deprecated; inconsistent implementation across different browsers. Use MutationObserver instead.

License

event-constructor-polyfill uses the MIT License.