simulate-event
Advanced tools
Comparing version 0.1.0 to 1.0.0
{ | ||
"name": "simulate-event", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"description": "A library for triggering DOM events.", | ||
"main": "simulate-event.js", | ||
"typings": "simulate-event.d.ts", | ||
"files": [ | ||
"simulate-event.js", | ||
"lib/" | ||
"simulate-event.d.ts" | ||
], | ||
@@ -13,4 +14,15 @@ "scripts": { | ||
"dev": "grunt watch:build & karma start", | ||
"test": "npm run build && karma start --single-run --browsers PhantomJS" | ||
"lint": "standard", | ||
"test:debug": "npm run build && karma start --browsers=Firefox --singleRun=false --debug=true", | ||
"test:chrome": "npm run build && karma start --browsers=Chrome", | ||
"test:ie": "npm run build && karma start --browsers=IE", | ||
"test": "npm run build && npm run lint && karma start --browsers=Firefox" | ||
}, | ||
"standard": { | ||
"ignore": [ | ||
"dist/", | ||
"node_modules/", | ||
"coverage/" | ||
] | ||
}, | ||
"repository": "https://github.com/blakeembrey/simulate-event", | ||
@@ -46,5 +58,5 @@ "keywords": [ | ||
"karma-html2js-preprocessor": "^0.1.0", | ||
"karma-ie-launcher": "^0.2.0", | ||
"karma-jasmine": "^0.1.5", | ||
"karma-mocha": "^0.1.1", | ||
"karma-phantomjs-launcher": "^0.1.1", | ||
"karma-requirejs": "^0.2.1", | ||
@@ -56,7 +68,8 @@ "karma-script-launcher": "^0.1.0", | ||
"requirejs": "^2.1.9", | ||
"standard": "^7.0.1", | ||
"uglifyify": "^1.0.1" | ||
}, | ||
"dependencies": { | ||
"variadic": "0.0.1" | ||
"xtend": "^4.0.1" | ||
} | ||
} |
@@ -19,3 +19,7 @@ # Simulate Event | ||
```javascript | ||
simulateEvent(document.body, 'click'); | ||
// Simulate an event on an element | ||
simulateEvent.simulate(document.body, 'click'); | ||
// Generate an event for custom use | ||
var evt = simulateEvent.generate('click', { clientX: 10 }); | ||
``` | ||
@@ -22,0 +26,0 @@ |
@@ -1,3 +0,2 @@ | ||
var extend = require('./lib/extend'); | ||
var result = require('./lib/result'); | ||
var extend = require('xtend') | ||
@@ -10,37 +9,37 @@ /** | ||
var eventOptions = { | ||
UIEvent: function (el) { | ||
UIEvent: function () { | ||
return { | ||
view: el.ownerDocument.defaultView | ||
}; | ||
view: document.defaultView | ||
} | ||
}, | ||
FocusEvent: function () { | ||
return eventOptions.UIEvent.apply(this, arguments); | ||
return eventOptions.UIEvent.apply(this, arguments) | ||
}, | ||
MouseEvent: function (el, type) { | ||
MouseEvent: function (type) { | ||
return { | ||
button: 0, | ||
cancelable: (type !== 'mousemove'), | ||
ctrlKey: false, | ||
altKey: false, | ||
shiftKey: false, | ||
metaKey: false, | ||
clientX: 1, | ||
clientY: 1, | ||
screenX: 0, | ||
screenY: 0, | ||
view: el.ownerDocument.defaultView, | ||
relatedTarget: el.ownerDocument.documentElement | ||
}; | ||
button: 0, | ||
cancelable: (type !== 'mousemove'), | ||
ctrlKey: false, | ||
altKey: false, | ||
shiftKey: false, | ||
metaKey: false, | ||
clientX: 1, | ||
clientY: 1, | ||
screenX: 0, | ||
screenY: 0, | ||
view: document.defaultView, | ||
relatedTarget: document.documentElement | ||
} | ||
}, | ||
KeyboardEvent: function (el) { | ||
KeyboardEvent: function () { | ||
return { | ||
view: el.ownerDocument.defaultView, | ||
ctrlKey: false, | ||
altKey: false, | ||
view: document.defaultView, | ||
ctrlKey: false, | ||
altKey: false, | ||
shiftKey: false, | ||
metaKey: false, | ||
keyCode: 0 | ||
}; | ||
metaKey: false, | ||
keyCode: 0 | ||
} | ||
} | ||
}; | ||
} | ||
@@ -53,106 +52,106 @@ /** | ||
var eventTypes = { | ||
beforeprint: 'Event', | ||
afterprint: 'Event', | ||
beforeunload: 'Event', | ||
abort: 'Event', | ||
error: 'Event', | ||
change: 'Event', | ||
submit: 'Event', | ||
reset: 'Event', | ||
cached: 'Event', | ||
canplay: 'Event', | ||
canplaythrough: 'Event', | ||
chargingchange: 'Event', | ||
beforeprint: 'Event', | ||
afterprint: 'Event', | ||
beforeunload: 'Event', | ||
abort: 'Event', | ||
error: 'Event', | ||
change: 'Event', | ||
submit: 'Event', | ||
reset: 'Event', | ||
cached: 'Event', | ||
canplay: 'Event', | ||
canplaythrough: 'Event', | ||
chargingchange: 'Event', | ||
chargingtimechange: 'Event', | ||
checking: 'Event', | ||
close: 'Event', | ||
downloading: 'Event', | ||
durationchange: 'Event', | ||
emptied: 'Event', | ||
ended: 'Event', | ||
fullscreenchange: 'Event', | ||
fullscreenerror: 'Event', | ||
input: 'Event', | ||
invalid: 'Event', | ||
levelchange: 'Event', | ||
loadeddata: 'Event', | ||
loadedmetadata: 'Event', | ||
noupdate: 'Event', | ||
obsolete: 'Event', | ||
offline: 'Event', | ||
online: 'Event', | ||
open: 'Event', | ||
orientationchange: 'Event', | ||
pause: 'Event', | ||
pointerlockchange: 'Event', | ||
pointerlockerror: 'Event', | ||
copy: 'Event', | ||
cut: 'Event', | ||
paste: 'Event', | ||
play: 'Event', | ||
playing: 'Event', | ||
ratechange: 'Event', | ||
readystatechange: 'Event', | ||
seeked: 'Event', | ||
seeking: 'Event', | ||
stalled: 'Event', | ||
success: 'Event', | ||
suspend: 'Event', | ||
timeupdate: 'Event', | ||
updateready: 'Event', | ||
visibilitychange: 'Event', | ||
volumechange: 'Event', | ||
waiting: 'Event', | ||
load: 'UIEvent', | ||
unload: 'UIEvent', | ||
resize: 'UIEvent', | ||
scroll: 'UIEvent', | ||
select: 'UIEvent', | ||
drag: 'UIEvent', | ||
dragenter: 'UIEvent', | ||
dragleave: 'UIEvent', | ||
dragover: 'UIEvent', | ||
dragstart: 'UIEvent', | ||
dragend: 'UIEvent', | ||
drop: 'UIEvent', | ||
touchcancel: 'UIEvent', | ||
touchend: 'UIEvent', | ||
touchenter: 'UIEvent', | ||
touchleave: 'UIEvent', | ||
touchmove: 'UIEvent', | ||
touchstart: 'UIEvent', | ||
blur: 'UIEvent', | ||
focus: 'UIEvent', | ||
focusin: 'UIEvent', | ||
focusout: 'UIEvent', | ||
show: 'MouseEvent', | ||
click: 'MouseEvent', | ||
dblclick: 'MouseEvent', | ||
mouseenter: 'MouseEvent', | ||
mouseleave: 'MouseEvent', | ||
mousedown: 'MouseEvent', | ||
mouseup: 'MouseEvent', | ||
mouseover: 'MouseEvent', | ||
mousemove: 'MouseEvent', | ||
mouseout: 'MouseEvent', | ||
contextmenu: 'MouseEvent', | ||
wheel: 'WheelEvent', | ||
message: 'MessageEvent', | ||
storage: 'StorageEvent', | ||
timeout: 'StorageEvent', | ||
keydown: 'KeyboardEvent', | ||
keypress: 'KeyboardEvent', | ||
keyup: 'KeyboardEvent', | ||
progress: 'ProgressEvent', | ||
loadend: 'ProgressEvent', | ||
loadstart: 'ProgressEvent', | ||
popstate: 'PopStateEvent', | ||
hashchange: 'HashChangeEvent', | ||
transitionend: 'TransitionEvent', | ||
compositionend: 'CompositionEvent', | ||
compositionstart: 'CompositionEvent', | ||
compositionupdate: 'CompositionEvent', | ||
pagehide: 'PageTransitionEvent', | ||
pageshow: 'PageTransitionEvent' | ||
}; | ||
checking: 'Event', | ||
close: 'Event', | ||
downloading: 'Event', | ||
durationchange: 'Event', | ||
emptied: 'Event', | ||
ended: 'Event', | ||
fullscreenchange: 'Event', | ||
fullscreenerror: 'Event', | ||
input: 'Event', | ||
invalid: 'Event', | ||
levelchange: 'Event', | ||
loadeddata: 'Event', | ||
loadedmetadata: 'Event', | ||
noupdate: 'Event', | ||
obsolete: 'Event', | ||
offline: 'Event', | ||
online: 'Event', | ||
open: 'Event', | ||
orientationchange: 'Event', | ||
pause: 'Event', | ||
pointerlockchange: 'Event', | ||
pointerlockerror: 'Event', | ||
copy: 'Event', | ||
cut: 'Event', | ||
paste: 'Event', | ||
play: 'Event', | ||
playing: 'Event', | ||
ratechange: 'Event', | ||
readystatechange: 'Event', | ||
seeked: 'Event', | ||
seeking: 'Event', | ||
stalled: 'Event', | ||
success: 'Event', | ||
suspend: 'Event', | ||
timeupdate: 'Event', | ||
updateready: 'Event', | ||
visibilitychange: 'Event', | ||
volumechange: 'Event', | ||
waiting: 'Event', | ||
load: 'UIEvent', | ||
unload: 'UIEvent', | ||
resize: 'UIEvent', | ||
scroll: 'UIEvent', | ||
select: 'UIEvent', | ||
drag: 'UIEvent', | ||
dragenter: 'UIEvent', | ||
dragleave: 'UIEvent', | ||
dragover: 'UIEvent', | ||
dragstart: 'UIEvent', | ||
dragend: 'UIEvent', | ||
drop: 'UIEvent', | ||
touchcancel: 'UIEvent', | ||
touchend: 'UIEvent', | ||
touchenter: 'UIEvent', | ||
touchleave: 'UIEvent', | ||
touchmove: 'UIEvent', | ||
touchstart: 'UIEvent', | ||
blur: 'UIEvent', | ||
focus: 'UIEvent', | ||
focusin: 'UIEvent', | ||
focusout: 'UIEvent', | ||
show: 'MouseEvent', | ||
click: 'MouseEvent', | ||
dblclick: 'MouseEvent', | ||
mouseenter: 'MouseEvent', | ||
mouseleave: 'MouseEvent', | ||
mousedown: 'MouseEvent', | ||
mouseup: 'MouseEvent', | ||
mouseover: 'MouseEvent', | ||
mousemove: 'MouseEvent', | ||
mouseout: 'MouseEvent', | ||
contextmenu: 'MouseEvent', | ||
wheel: 'WheelEvent', | ||
message: 'MessageEvent', | ||
storage: 'StorageEvent', | ||
timeout: 'StorageEvent', | ||
keydown: 'KeyboardEvent', | ||
keypress: 'KeyboardEvent', | ||
keyup: 'KeyboardEvent', | ||
progress: 'ProgressEvent', | ||
loadend: 'ProgressEvent', | ||
loadstart: 'ProgressEvent', | ||
popstate: 'PopStateEvent', | ||
hashchange: 'HashChangeEvent', | ||
transitionend: 'TransitionEvent', | ||
compositionend: 'CompositionEvent', | ||
compositionstart: 'CompositionEvent', | ||
compositionupdate: 'CompositionEvent', | ||
pagehide: 'PageTransitionEvent', | ||
pageshow: 'PageTransitionEvent' | ||
} | ||
@@ -165,19 +164,19 @@ /** | ||
var eventInit = { | ||
Event: 'initEvent', | ||
UIEvent: 'initUIEvent', | ||
FocusEvent: 'initUIEvent', | ||
MouseEvent: 'initMouseEvent', | ||
WheelEvent: 'initMouseEvent', | ||
MessageEvent: 'initMessageEvent', | ||
StorageEvent: 'initStorageEvent', | ||
KeyboardEvent: 'initKeyboardEvent', | ||
ProgressEvent: 'initEvent', | ||
PopStateEvent: 'initEvent', | ||
TransitionEvent: 'initEvent', | ||
HashChangeEvent: 'initHashChangeEvent', | ||
CompositionEvent: 'initCompositionEvent', | ||
DeviceMotionEvent: 'initDeviceMotionEvent', | ||
PageTransitionEvent: 'initEvent', | ||
Event: 'initEvent', | ||
UIEvent: 'initUIEvent', | ||
FocusEvent: 'initUIEvent', | ||
MouseEvent: 'initMouseEvent', | ||
WheelEvent: 'initMouseEvent', | ||
MessageEvent: 'initMessageEvent', | ||
StorageEvent: 'initStorageEvent', | ||
KeyboardEvent: 'initKeyboardEvent', | ||
ProgressEvent: 'initEvent', | ||
PopStateEvent: 'initEvent', | ||
TransitionEvent: 'initEvent', | ||
HashChangeEvent: 'initHashChangeEvent', | ||
CompositionEvent: 'initCompositionEvent', | ||
DeviceMotionEvent: 'initDeviceMotionEvent', | ||
PageTransitionEvent: 'initEvent', | ||
DeviceOrientationEvent: 'initDeviceOrientationEvent' | ||
}; | ||
} | ||
@@ -204,2 +203,11 @@ /** | ||
], | ||
initKeyEvent: [ | ||
'view', | ||
'ctrlKey', | ||
'altKey', | ||
'shiftKey', | ||
'metaKey', | ||
'keyCode', | ||
'charCode' | ||
], | ||
initMouseEvent: [ | ||
@@ -253,27 +261,88 @@ 'view', | ||
] | ||
}; | ||
} | ||
/** | ||
* Exports the similate functionality. | ||
* Map the event types to constructors. | ||
* | ||
* @param {Element} element | ||
* @type {Object} | ||
*/ | ||
var eventConstructors = { | ||
UIEvent: window.UIEvent, | ||
FocusEvent: window.FocusEvent, | ||
MouseEvent: window.MouseEvent, | ||
KeyboardEvent: window.KeyboardEvent | ||
} | ||
/** | ||
* Get attributes which must be overriden manually. | ||
* | ||
* @param {String} eventType | ||
* @param {Object} options. | ||
*/ | ||
function getOverrides (eventType, options) { | ||
if (eventType === 'KeyboardEvent' && options) { | ||
return { | ||
keyCode: options.keyCode || 0, | ||
key: options.key || 0, | ||
which: options.which || options.keyCode || 0 | ||
} | ||
} | ||
} | ||
/** | ||
* Generate an event. | ||
* | ||
* @param {String} type | ||
* @param {Object} options | ||
* @return {Boolean} | ||
* @return {Event} | ||
*/ | ||
module.exports = function (element, type, options) { | ||
exports.generate = function (type, options) { | ||
// Immediately throw an error when the event name does not translate. | ||
if (!eventTypes.hasOwnProperty(type)) { | ||
throw new SyntaxError('Unsupported event type'); | ||
throw new SyntaxError('Unsupported event type') | ||
} | ||
var eventType = eventTypes[type]; | ||
var initEvent = eventInit[eventType]; | ||
var event; | ||
var eventType = eventTypes[type] | ||
var event | ||
var key | ||
// Handle parameters which must be manually overridden using | ||
// `Object.defineProperty`. | ||
var overrides = getOverrides(eventType, options) | ||
// Attempt the Event Constructors DOM API. | ||
var Constructor = eventConstructors[eventType] | ||
try { | ||
event = new Constructor(type, options) | ||
// Add the override properties. | ||
for (key in overrides) { | ||
Object.defineProperty(event, key, { | ||
value: overrides[key] | ||
}) | ||
} | ||
return event | ||
} catch (e) { | ||
// Continue. | ||
} | ||
// In IE11, the Keyboard event does not allow setting the | ||
// keyCode property, even with Object.defineProperty, | ||
// so we have to use a UIEvent. | ||
var ua = window.navigator.userAgent | ||
var msie = ua.indexOf('MSIE ') | ||
if (msie > 0 && eventType === 'KeyboardEvent') { | ||
eventType = 'UIEvent' | ||
} | ||
var initEvent = eventInit[eventType] | ||
// Extend a new object with the default and passed in options. | ||
options = extend({ | ||
bubbles: true, | ||
bubbles: true, | ||
cancelable: true | ||
}, result(eventOptions, eventType, element, type, options), options); | ||
}, eventOptions[eventType](type, options), options) | ||
@@ -283,12 +352,34 @@ // In < IE9, the `createEvent` function is not available and we have to | ||
if (!document.createEvent) { | ||
event = extend(document.createEventObject(), options); | ||
return element.fireEvent('on' + type, event); | ||
event = extend(document.createEventObject(), options) | ||
// Add the override properties. | ||
for (key in overrides) { | ||
Object.defineProperty(event, key, { | ||
value: overrides[key] | ||
}) | ||
} | ||
return event | ||
} | ||
event = extend(document.createEvent(eventType), options); | ||
event = extend(document.createEvent(eventType), options) | ||
// Handle differences between `initKeyboardEvent` and `initKeyEvent`. | ||
if (initEvent === 'initKeyboardEvent') { | ||
if (event[initEvent] === void 0) { | ||
initEvent = 'initKeyEvent' | ||
} else if (!('modifiersList' in options)) { | ||
var mods = [] | ||
if (options.metaKey) mods.push('Meta') | ||
if (options.altKey) mods.push('Alt') | ||
if (options.shiftKey) mods.push('Shift') | ||
if (options.ctrlKey) mods.push('Control') | ||
options['modifiersList'] = mods.join(' ') | ||
} | ||
} | ||
// Map argument names to the option values. | ||
var args = eventParameters[initEvent].map(function (parameter) { | ||
return options[parameter]; | ||
}); | ||
return options[parameter] | ||
}) | ||
@@ -298,5 +389,31 @@ // Initialize the event using the built-in method. | ||
event, [type, event.bubbles, event.cancelable].concat(args) | ||
); | ||
) | ||
return element.dispatchEvent(event); | ||
}; | ||
// Add the override properties. | ||
for (key in overrides) { | ||
Object.defineProperty(event, key, { | ||
value: overrides[key] | ||
}) | ||
} | ||
return event | ||
} | ||
/** | ||
* Simulate an event which is dispatched on the given element. | ||
* | ||
* @param {Element} element | ||
* @param {String} type | ||
* @param {Object} options | ||
* @return {Boolean} | ||
*/ | ||
exports.simulate = function (element, type, options) { | ||
var event = exports.generate(type, options) | ||
// In < IE9, the `createEvent` function is not available and we have to | ||
// resort to using `fireEvent`. | ||
if (!document.createEvent) { | ||
return element.fireEvent('on' + type, event) | ||
} | ||
return element.dispatchEvent(event) | ||
} |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
13507
388
1
38
26
5
2
+ Addedxtend@^4.0.1
+ Addedxtend@4.0.2(transitive)
- Removedvariadic@0.0.1
- Removedvariadic@0.0.1(transitive)