event-target-shim
Advanced tools
Comparing version 1.0.0 to 1.0.1
"use strict"; | ||
var _defineProperty = function (obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); }; | ||
module.exports = EventTarget; | ||
var _commons = require("./commons"); | ||
var LISTENERS = _commons.LISTENERS; | ||
var CAPTURE = _commons.CAPTURE; | ||
var BUBBLE = _commons.BUBBLE; | ||
var newNode = _commons.newNode; | ||
var defineCustomEventTarget = require("./CustomEventTarget").defineCustomEventTarget; | ||
var _EventWrapper = require("./EventWrapper"); | ||
@@ -12,25 +21,5 @@ | ||
var LISTENERS = Symbol("listeners"); | ||
var SET_ATTRIBUTE_LISTENER = Symbol("setAttributeListener"); | ||
var GET_ATTRIBUTE_LISTENER = Symbol("getAttributeListener"); | ||
var CAPTURE = 1; | ||
var BUBBLE = 2; | ||
var ATTRIBUTE = 3; | ||
var HAS_EVENTTARGET_INTERFACE = typeof window !== "undefined" && typeof window.EventTarget !== "undefined"; | ||
// Return definition of an attribute listener. | ||
function defineAttributeListener(type) { | ||
type = type.replace(/"/g, "\\\""); | ||
return "\n \"on" + type + "\": {\n get: function() { return this[GET_ATTRIBUTE_LISTENER](\"" + type + "\"); },\n set: function(value) { this[SET_ATTRIBUTE_LISTENER](\"" + type + "\", value); },\n configurable: true,\n enumerable: true\n },\n "; | ||
} | ||
// Create a LinkedList structure for EventListener. | ||
function newNode(listener, kind) { | ||
return { listener: listener, kind: kind, next: null }; | ||
} | ||
// | ||
// Class Definition. | ||
// | ||
var ET = function EventTarget() { | ||
function EventTarget() { | ||
for (var _len = arguments.length, types = Array(_len), _key = 0; _key < _len; _key++) { | ||
@@ -57,140 +46,63 @@ types[_key] = arguments[_key]; | ||
// } | ||
return Function("EventTargetBase", "GET_ATTRIBUTE_LISTENER", "SET_ATTRIBUTE_LISTENER", "\n function EventTarget() {\n EventTargetBase.call(this);\n }\n EventTarget.prototype = Object.create(EventTargetBase.prototype, {\n " + types.map(defineAttributeListener).join() + "\n constructor: {\n value: EventTarget,\n writable: true,\n configurable: true\n }\n });\n return EventTarget;\n ")(EventTarget, GET_ATTRIBUTE_LISTENER, SET_ATTRIBUTE_LISTENER); | ||
return defineCustomEventTarget(EventTarget, types); | ||
} else { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
} | ||
ET.prototype = Object.create((HAS_EVENTTARGET_INTERFACE ? window.EventTarget : Object).prototype, (function () { | ||
var _Object$create = { | ||
constructor: { | ||
value: ET, | ||
writable: true, | ||
configurable: true | ||
}, | ||
EventTarget.prototype = Object.create((HAS_EVENTTARGET_INTERFACE ? window.EventTarget : Object).prototype, { | ||
constructor: { | ||
value: EventTarget, | ||
writable: true, | ||
configurable: true | ||
}, | ||
addEventListener: { | ||
value: function addEventListener(type, listener) { | ||
var capture = arguments[2] === undefined ? false : arguments[2]; | ||
addEventListener: { | ||
value: function addEventListener(type, listener) { | ||
var capture = arguments[2] === undefined ? false : arguments[2]; | ||
if (listener == null) { | ||
return false; | ||
} | ||
if (typeof listener !== "function") { | ||
throw new TypeError("listener should be a function."); | ||
} | ||
if (listener == null) { | ||
return false; | ||
} | ||
if (typeof listener !== "function") { | ||
throw new TypeError("listener should be a function."); | ||
} | ||
var kind = capture ? CAPTURE : BUBBLE; | ||
var node = this[LISTENERS][type]; | ||
if (node == null) { | ||
this[LISTENERS][type] = newNode(listener, kind); | ||
return true; | ||
} | ||
var prev = null; | ||
while (node != null) { | ||
if (node.listener === listener && node.kind === kind) { | ||
// Should ignore a duplicated listener. | ||
return false; | ||
} | ||
prev = node; | ||
node = node.next; | ||
} | ||
prev.next = newNode(listener, kind); | ||
var kind = capture ? CAPTURE : BUBBLE; | ||
var node = this[LISTENERS][type]; | ||
if (node == null) { | ||
this[LISTENERS][type] = newNode(listener, kind); | ||
return true; | ||
}, | ||
writable: true, | ||
configurable: true | ||
}, | ||
} | ||
removeEventListener: { | ||
value: function removeEventListener(type, listener) { | ||
var capture = arguments[2] === undefined ? false : arguments[2]; | ||
if (listener == null) { | ||
var prev = null; | ||
while (node != null) { | ||
if (node.listener === listener && node.kind === kind) { | ||
// Should ignore a duplicated listener. | ||
return false; | ||
} | ||
prev = node; | ||
node = node.next; | ||
} | ||
var kind = capture ? CAPTURE : BUBBLE; | ||
var prev = null; | ||
var node = this[LISTENERS][type]; | ||
while (node != null) { | ||
if (node.listener === listener && node.kind === kind) { | ||
if (prev == null) { | ||
this[LISTENERS][type] = node.next; | ||
} else { | ||
prev.next = node.next; | ||
} | ||
return true; | ||
} | ||
prev.next = newNode(listener, kind); | ||
return true; | ||
}, | ||
configurable: true, | ||
writable: true | ||
}, | ||
prev = node; | ||
node = node.next; | ||
} | ||
removeEventListener: { | ||
value: function removeEventListener(type, listener) { | ||
var capture = arguments[2] === undefined ? false : arguments[2]; | ||
if (listener == null) { | ||
return false; | ||
}, | ||
writable: true, | ||
configurable: true | ||
}, | ||
dispatchEvent: { | ||
value: function dispatchEvent(event) { | ||
// Should check initialized flag, but impossible. | ||
if (event[DISPATCH_FLAG]) { | ||
throw new Error("InvalidStateError"); | ||
} | ||
// If listeners aren't registered, terminate. | ||
var node = this[LISTENERS][event.type]; | ||
if (node == null) { | ||
return true; | ||
} | ||
// Since we cannot rewrite several properties, so wrap object. | ||
event = createEventWrapper(event, this); | ||
// This doesn't process capturing phase and bubbling phase. | ||
// This isn't participating in a tree. | ||
while (node != null) { | ||
node.listener.call(this, event); | ||
if (event[STOP_IMMEDIATE_PROPAGATION_FLAG]) { | ||
break; | ||
} | ||
node = node.next; | ||
} | ||
return !event[CANCELED_FLAG]; | ||
}, | ||
writable: true, | ||
configurable: true | ||
} }; | ||
_defineProperty(_Object$create, GET_ATTRIBUTE_LISTENER, { | ||
value: function getAttributeListener(type) { | ||
var node = this[LISTENERS][type]; | ||
while (node != null) { | ||
if (node.kind === ATTRIBUTE) { | ||
return node.listener; | ||
} | ||
node = node.next; | ||
} | ||
return null; | ||
}, | ||
writable: true, | ||
configurable: true | ||
}); | ||
_defineProperty(_Object$create, SET_ATTRIBUTE_LISTENER, { | ||
value: function setAttributeListener(type, listener) { | ||
if (listener != null && typeof listener !== "function") { | ||
throw new TypeError("listener should be a function."); | ||
} | ||
var kind = capture ? CAPTURE : BUBBLE; | ||
var prev = null; | ||
var node = this[LISTENERS][type]; | ||
while (node != null) { | ||
if (node.kind === ATTRIBUTE) { | ||
// Remove old value. | ||
if (node.listener === listener && node.kind === kind) { | ||
if (prev == null) { | ||
@@ -201,25 +113,46 @@ this[LISTENERS][type] = node.next; | ||
} | ||
} else { | ||
prev = node; | ||
return true; | ||
} | ||
prev = node; | ||
node = node.next; | ||
} | ||
// Add new value. | ||
if (listener != null) { | ||
if (prev == null) { | ||
this[LISTENERS][type] = newNode(listener, ATTRIBUTE); | ||
} else { | ||
prev.next = newNode(listener, ATTRIBUTE); | ||
return false; | ||
}, | ||
configurable: true, | ||
writable: true | ||
}, | ||
dispatchEvent: { | ||
value: function dispatchEvent(event) { | ||
// Should check initialized flag, but impossible. | ||
if (event[DISPATCH_FLAG]) { | ||
throw new Error("InvalidStateError"); | ||
} | ||
// If listeners aren't registered, terminate. | ||
var node = this[LISTENERS][event.type]; | ||
if (node == null) { | ||
return true; | ||
} | ||
// Since we cannot rewrite several properties, so wrap object. | ||
event = createEventWrapper(event, this); | ||
// This doesn't process capturing phase and bubbling phase. | ||
// This isn't participating in a tree. | ||
while (node != null) { | ||
node.listener.call(this, event); | ||
if (event[STOP_IMMEDIATE_PROPAGATION_FLAG]) { | ||
break; | ||
} | ||
node = node.next; | ||
} | ||
return !event[CANCELED_FLAG]; | ||
}, | ||
writable: true, | ||
configurable: true | ||
}); | ||
return _Object$create; | ||
})()); | ||
module.exports = ET; | ||
configurable: true, | ||
writable: true | ||
} | ||
}); |
{ | ||
"name": "event-target-shim", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A polyfill for W3C EventTarget Constructor.", | ||
"main": "lib/EventTarget.js", | ||
"files": [ | ||
"dist", | ||
"lib" | ||
], | ||
"scripts": { | ||
"build": "babel src --out-dir lib", | ||
"test": "npm-run-all test:lint build test:karma", | ||
"test:lint": "eslint src test", | ||
"clean": "rimraf lib dist", | ||
"lint": "eslint src", | ||
"build": "npm-run-all clean lint build:lib build:dist build:dist-min", | ||
"build:lib": "babel src --out-dir lib", | ||
"build:dist": "mkdirp dist && browserify lib/EventTarget.js --standalone event-target-shim > dist/event-target-shim.js", | ||
"build:dist-min": "uglifyjs dist/event-target-shim.js --compress --mangle > dist/event-target-shim.min.js", | ||
"test": "npm-run-all clean lint build:lib test:karma", | ||
"test:karma": "karma start karma.conf.js --single-run", | ||
"testing": "npm-run-all --parallel testing:build testing:karma", | ||
"testing:build": "npm run build -- --watch", | ||
"testing": "npm run clean && npm-run-all --parallel testing:build testing:karma", | ||
"testing:build": "npm run build:lib -- --watch", | ||
"testing:karma": "karma start karma.conf.js --auto-watch" | ||
@@ -21,2 +26,3 @@ }, | ||
"babelify": "^5.0.4", | ||
"browserify": "^9.0.3", | ||
"eslint": "^0.17.1", | ||
@@ -30,6 +36,9 @@ "espowerify": "^0.10.0", | ||
"karma-mocha": "^0.1.10", | ||
"mkdirp": "^0.5.0", | ||
"mocha": "^2.2.1", | ||
"npm-run-all": "^1.0.2", | ||
"power-assert": "^0.10.2", | ||
"spy": "^0.1.3" | ||
"rimraf": "^2.3.2", | ||
"spy": "^0.1.3", | ||
"uglify-js": "^2.4.17" | ||
}, | ||
@@ -36,0 +45,0 @@ "repository": { |
# event-target-shim | ||
[![npm version](https://badge.fury.io/js/event-target-shim.svg)](http://badge.fury.io/js/event-target-shim) | ||
A polyfill for W3C EventTarget, plus few extensions. | ||
@@ -10,11 +12,16 @@ | ||
This module provides `EventTarget` constructor that can inherit for your custom object. | ||
And this module provides an utility to define properties for attribute listeners (e.g. `obj.onclick`). | ||
- This module provides `EventTarget` constructor that is possible to inherit for | ||
your custom object. | ||
- This module provides an utility in order to define properties for attribute | ||
listeners (e.g. `obj.onclick`). | ||
If `window.EventTarget` exists, `EventTarget` is inherit from `window.EventTarget`. | ||
If `window.EventTarget` exists, `EventTarget` is inherit from | ||
`window.EventTarget`. | ||
In short, `obj instanceof window.EventTarget === true`. | ||
```ts | ||
declare class EventTarget { | ||
addEventListener(type: string, listener: (event: Event) => void, capture?: boolean = false): void; | ||
removeEventListener(type: string, listener: (event: Event) => void, capture?: boolean = false): void; | ||
constructor(); | ||
addEventListener(type: string, listener: (event: Event) => void, capture?: boolean): void; | ||
removeEventListener(type: string, listener: (event: Event) => void, capture?: boolean): void; | ||
dispatchEvent(event: Event): void; | ||
@@ -24,3 +31,3 @@ } | ||
// Define EventTarget type with attribute listeners. | ||
declare function EventTarget(...types: string[]): typeof(EventTarget); | ||
declare function EventTarget(...types: string[]): typeof EventTarget; | ||
``` | ||
@@ -32,9 +39,8 @@ | ||
``` | ||
npm install event-target-shim --save | ||
npm install event-target-shim | ||
``` | ||
## Usage | ||
This module has been designed that uses together [Browserify](http://browserify.org/). | ||
```js | ||
@@ -51,15 +57,27 @@ import EventTarget from "event-target-shim"; | ||
} | ||
``` | ||
// Dispatch with an Event. | ||
{ | ||
let event = document.createEvent("Event"); | ||
event.initEvent("message", false, false); | ||
event.data = "Hello!"; | ||
obj.dispatchEvent(event); | ||
} | ||
I prefer use together with [Browserify](http://browserify.org). | ||
But we can use together with [RequireJS](http://requirejs.org/), instead. | ||
// Dispatch with a plain object. | ||
{ | ||
obj.dispatchEvent({type: "message", data: "Hello!"}); | ||
} | ||
```js | ||
define("MagicalBox", ["event-target-shim"], function (EventTarget) { | ||
function MagicalBox() { | ||
EventTarget.call(this); | ||
} | ||
MagicalBox.prototype = Object.create(EventTarget.prototype, { | ||
constructor: { | ||
value: MagicalBox, | ||
configurable: true, | ||
writable: true | ||
}, | ||
// ... | ||
}); | ||
return MagicalBox; | ||
}); | ||
``` | ||
In this case, please download a file from dist directory of repo. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
28609
9
562
80
1
18
1
4