Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesherov committed Dec 2, 2012
0 parents commit d8a978a
Show file tree
Hide file tree
Showing 22 changed files with 73,307 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
build/.sizecache.json
dist/
.DS_Store
node_modules
3 changes: 3 additions & 0 deletions .gitmodules
@@ -0,0 +1,3 @@
[submodule "test/qunit"]
path = test/qunit
url = git://github.com/jquery/qunit.git
14 changes: 14 additions & 0 deletions .jshintrc
@@ -0,0 +1,14 @@
{
"browser": true,
"curly": true,
"eqnull": true,
"eqeqeq": true,
"expr": true,
"jquery": true,
"noarg": true,
"onevar": true,
"quotmark": "double",
"trailing": true,
"undef": true,
"unused": true
}
21 changes: 21 additions & 0 deletions MIT-LICENSE.txt
@@ -0,0 +1,21 @@
Copyright 2012 jQuery Foundation and other contributors,
http://jquery.com

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 changes: 32 additions & 0 deletions README.md
@@ -0,0 +1,32 @@
# jQuery.simulate()

How to build and test jQuery Simulate
----------------------------------

First, get a copy of the git repo by running:

```shell
git clone git://github.com/jquery/jquery-simulate.git
```

Enter the directory and install the node dependencies:

```shell
cd jquery-simulate && npm install
```

Make sure you have `grunt` installed by testing:

```shell
grunt -version
```

If not, run:

```shell
npm install -g grunt
```

To run tests locally, run `grunt`, and this will run the tests in PhantomJS.

You can also run the tests in a browser by navigating to the `test/` directory, but first run `grunt` to install submodules.
261 changes: 261 additions & 0 deletions jquery.simulate.js
@@ -0,0 +1,261 @@
/*!
* jQuery Simulate v@VERSION - simulate browser mouse and keyboard events
* https://github.com/jquery/jquery-simulate
*
* Copyright 2012 jQuery Foundation and other contributors
* Released under the MIT license.
* http://jquery.org/license
*
* Date: @DATE
*/

;(function( $, undefined ) {

var rkeyEvent = /^key/,
rmouseEvent = /^(?:mouse|contextmenu)|click/;

$.fn.simulate = function( type, options ) {
return this.each(function() {
new $.simulate( this, type, options );
});
};

$.simulate = function( elem, type, options ) {
var method = $.camelCase( "simulate-" + type );

this.target = elem;
this.options = options;

if ( this[ method ] ) {
this[ method ]();
} else {
this.simulateEvent( elem, type, options );
}
};

$.extend( $.simulate.prototype, {
simulateEvent: function( elem, type, options ) {
var event = this.createEvent( type, options );
this.dispatchEvent( elem, type, event, options );
},

createEvent: function( type, options ) {
if ( rkeyEvent.test( type ) ) {
return this.keyEvent( type, options );
}

if ( rmouseEvent.test( type ) ) {
return this.mouseEvent( type, options );
}
},

mouseEvent: function( type, options ) {
var event, eventDoc, doc, body;
options = $.extend({
bubbles: true,
cancelable: (type !== "mousemove"),
view: window,
detail: 0,
screenX: 0,
screenY: 0,
// TODO: default clientX/Y to a position within the target element
clientX: 1,
clientY: 1,
ctrlKey: false,
altKey: false,
shiftKey: false,
metaKey: false,
button: 0,
relatedTarget: undefined
}, options );

if ( document.createEvent ) {
event = document.createEvent( "MouseEvents" );
event.initMouseEvent( type, options.bubbles, options.cancelable,
options.view, options.detail,
options.screenX, options.screenY, options.clientX, options.clientY,
options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
options.button, options.relatedTarget || document.body.parentNode );

// IE 9+ creates events with pageX and pageY set to 0.
// Trying to modify the properties throws an error,
// so we define getters to return the correct values.
if ( event.pageX === 0 && event.pageY === 0 && Object.defineProperty ) {
eventDoc = event.relatedTarget.ownerDocument || document;
doc = eventDoc.documentElement;
body = eventDoc.body;

Object.defineProperty( event, "pageX", {
get: function() {
return options.clientX +
( doc && doc.scrollLeft || body && body.scrollLeft || 0 ) -
( doc && doc.clientLeft || body && body.clientLeft || 0 );
}
});
Object.defineProperty( event, "pageY", {
get: function() {
return options.clientY +
( doc && doc.scrollTop || body && body.scrollTop || 0 ) -
( doc && doc.clientTop || body && body.clientTop || 0 );
}
});
}
} else if ( document.createEventObject ) {
event = document.createEventObject();
$.extend( event, options );
// TODO: what is this mapping for?
event.button = { 0:1, 1:4, 2:2 }[ event.button ] || event.button;
}

return event;
},

keyEvent: function( type, options ) {
var event;
options = $.extend({
bubbles: true,
cancelable: true,
view: window,
ctrlKey: false,
altKey: false,
shiftKey: false,
metaKey: false,
keyCode: 0,
charCode: undefined
}, options );

if ( document.createEvent ) {
try {
event = document.createEvent( "KeyEvents" );
event.initKeyEvent( type, options.bubbles, options.cancelable, options.view,
options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
options.keyCode, options.charCode );
// TODO: what is this supporting?
} catch( err ) {
event = document.createEvent( "Events" );
event.initEvent( type, options.bubbles, options.cancelable );
$.extend( event, {
view: options.view,
ctrlKey: options.ctrlKey,
altKey: options.altKey,
shiftKey: options.shiftKey,
metaKey: options.metaKey,
keyCode: options.keyCode,
charCode: options.charCode
});
}
} else if ( document.createEventObject ) {
event = document.createEventObject();
$.extend( event, options );
}

// TODO: can we hook into core's logic?
if ( $.ui.ie || (({}).toString.call( window.opera ) === "[object Opera]") ) {
// TODO: is charCode ever <0 ? Can we just use charCode || keyCode?
event.keyCode = (options.charCode > 0) ? options.charCode : options.keyCode;
event.charCode = undefined;
}

return event;
},

// TODO: does this need type? Can't we just check event.type?
dispatchEvent: function( elem, type, event ) {
if ( elem.dispatchEvent ) {
elem.dispatchEvent( event );
} else if ( elem.fireEvent ) {
elem.fireEvent( "on" + type, event );
}
},

simulateFocus: function() {
var focusinEvent,
triggered = false,
element = $( this.target );

function trigger() {
triggered = true;
}

element.bind( "focus", trigger );
element[ 0 ].focus();

if ( !triggered ) {
focusinEvent = $.Event( "focusin" );
focusinEvent.preventDefault();
element.trigger( focusinEvent );
element.triggerHandler( "focus" );
}
element.unbind( "focus", trigger );
},

simulateBlur: function() {
var focusoutEvent,
triggered = false,
element = $( this.target );

function trigger() {
triggered = true;
}

element.bind( "blur", trigger );
element[ 0 ].blur();

// blur events are async in IE
setTimeout(function() {
// IE won't let the blur occur if the window is inactive
if ( element[ 0 ].ownerDocument.activeElement === element[ 0 ] ) {
element[ 0 ].ownerDocument.body.focus();
}

// Firefox won't trigger events if the window is inactive
// IE doesn't trigger events if we had to manually focus the body
if ( !triggered ) {
focusoutEvent = $.Event( "focusout" );
focusoutEvent.preventDefault();
element.trigger( focusoutEvent );
element.triggerHandler( "blur" );
}
element.unbind( "blur", trigger );
}, 1 );
}
});



/** complex events **/

function findCenter( elem ) {
var offset,
document = $( elem.ownerDocument );
elem = $( elem );
offset = elem.offset();

return {
x: offset.left + elem.outerWidth() / 2 - document.scrollLeft(),
y: offset.top + elem.outerHeight() / 2 - document.scrollTop()
};
}

$.extend( $.simulate.prototype, {
simulateDrag: function() {
var target = this.target,
options = this.options,
center = findCenter( target ),
x = Math.floor( center.x ),
y = Math.floor( center.y ),
dx = options.dx || 0,
dy = options.dy || 0,
coord = { clientX: x, clientY: y };
this.simulateEvent( target, "mousedown", coord );
coord = { clientX: x + 1, clientY: y + 1 };
this.simulateEvent( document, "mousemove", coord );
coord = { clientX: x + dx, clientY: y + dy };
this.simulateEvent( document, "mousemove", coord );
this.simulateEvent( document, "mousemove", coord );
this.simulateEvent( target, "mouseup", coord );
this.simulateEvent( target, "click", coord );
}
});

})( jQuery );
37 changes: 37 additions & 0 deletions package.json
@@ -0,0 +1,37 @@
{
"name": "jquery-simulate",
"title": "jQuery Simulate",
"description": "jQuery plugin for simulating browser mouse and keyboard events",
"version": "0.0.1",
"homepage": "https://github.com/jquery/jquery-simulate",
"author": {
"name": "jQuery Foundation and other contributors",
"url": "https://github.com/jquery/jquery-simulate/blob/master/AUTHORS.txt"
},
"maintainers": [
{
"name": "Mike Sherov",
"email": "mike.sherov@gmail.com",
"url": "http://mike.sherov.com"
}
],
"repository": {
"type": "git",
"url": "git://github.com/jquery/jquery-simulate.git"
},
"bugs": "https://github.com/jquery/jquery-simulate/issues",
"licenses": [
{
"type": "MIT",
"url": "https://github.com/jquery/jquery-simulate/blob/master/MIT-LICENSE.txt"
}
],
"dependencies": {},
"devDependencies": {
"grunt": "~0.3.17",
"grunt-compare-size": ">=0.1.4",
"grunt-git-authors": "1.0.0",
"testswarm": "0.2.2"
},
"keywords": [ "simulate", "events", "keyboard", "mouse", "jquery" ]
}
9 changes: 9 additions & 0 deletions test/data/swarminject.js
@@ -0,0 +1,9 @@
// load testswarm agent
(function() {
var url = window.location.search;
url = decodeURIComponent( url.slice( url.indexOf("swarmURL=") + 9 ) );
if ( !url || url.indexOf("http") !== 0 ) {
return;
}
document.write("<scr" + "ipt src='http://swarm.jquery.org/js/inject.js?" + (new Date).getTime() + "'></scr" + "ipt>");
})();
Empty file added test/data/testinit.js
Empty file.

0 comments on commit d8a978a

Please sign in to comment.