Skip to content

Commit d8a978a

Browse files
committed
initial commit
0 parents  commit d8a978a

22 files changed

+73307
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build/.sizecache.json
2+
dist/
3+
.DS_Store
4+
node_modules

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "test/qunit"]
2+
path = test/qunit
3+
url = git://github.com/jquery/qunit.git

.jshintrc

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"browser": true,
3+
"curly": true,
4+
"eqnull": true,
5+
"eqeqeq": true,
6+
"expr": true,
7+
"jquery": true,
8+
"noarg": true,
9+
"onevar": true,
10+
"quotmark": "double",
11+
"trailing": true,
12+
"undef": true,
13+
"unused": true
14+
}

MIT-LICENSE.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Copyright 2012 jQuery Foundation and other contributors,
2+
http://jquery.com
3+
4+
Permission is hereby granted, free of charge, to any person obtaining
5+
a copy of this software and associated documentation files (the
6+
"Software"), to deal in the Software without restriction, including
7+
without limitation the rights to use, copy, modify, merge, publish,
8+
distribute, sublicense, and/or sell copies of the Software, and to
9+
permit persons to whom the Software is furnished to do so, subject to
10+
the following conditions:
11+
12+
The above copyright notice and this permission notice shall be
13+
included in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# jQuery.simulate()
2+
3+
How to build and test jQuery Simulate
4+
----------------------------------
5+
6+
First, get a copy of the git repo by running:
7+
8+
```shell
9+
git clone git://github.com/jquery/jquery-simulate.git
10+
```
11+
12+
Enter the directory and install the node dependencies:
13+
14+
```shell
15+
cd jquery-simulate && npm install
16+
```
17+
18+
Make sure you have `grunt` installed by testing:
19+
20+
```shell
21+
grunt -version
22+
```
23+
24+
If not, run:
25+
26+
```shell
27+
npm install -g grunt
28+
```
29+
30+
To run tests locally, run `grunt`, and this will run the tests in PhantomJS.
31+
32+
You can also run the tests in a browser by navigating to the `test/` directory, but first run `grunt` to install submodules.

jquery.simulate.js

+261
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
/*!
2+
* jQuery Simulate v@VERSION - simulate browser mouse and keyboard events
3+
* https://github.com/jquery/jquery-simulate
4+
*
5+
* Copyright 2012 jQuery Foundation and other contributors
6+
* Released under the MIT license.
7+
* http://jquery.org/license
8+
*
9+
* Date: @DATE
10+
*/
11+
12+
;(function( $, undefined ) {
13+
14+
var rkeyEvent = /^key/,
15+
rmouseEvent = /^(?:mouse|contextmenu)|click/;
16+
17+
$.fn.simulate = function( type, options ) {
18+
return this.each(function() {
19+
new $.simulate( this, type, options );
20+
});
21+
};
22+
23+
$.simulate = function( elem, type, options ) {
24+
var method = $.camelCase( "simulate-" + type );
25+
26+
this.target = elem;
27+
this.options = options;
28+
29+
if ( this[ method ] ) {
30+
this[ method ]();
31+
} else {
32+
this.simulateEvent( elem, type, options );
33+
}
34+
};
35+
36+
$.extend( $.simulate.prototype, {
37+
simulateEvent: function( elem, type, options ) {
38+
var event = this.createEvent( type, options );
39+
this.dispatchEvent( elem, type, event, options );
40+
},
41+
42+
createEvent: function( type, options ) {
43+
if ( rkeyEvent.test( type ) ) {
44+
return this.keyEvent( type, options );
45+
}
46+
47+
if ( rmouseEvent.test( type ) ) {
48+
return this.mouseEvent( type, options );
49+
}
50+
},
51+
52+
mouseEvent: function( type, options ) {
53+
var event, eventDoc, doc, body;
54+
options = $.extend({
55+
bubbles: true,
56+
cancelable: (type !== "mousemove"),
57+
view: window,
58+
detail: 0,
59+
screenX: 0,
60+
screenY: 0,
61+
// TODO: default clientX/Y to a position within the target element
62+
clientX: 1,
63+
clientY: 1,
64+
ctrlKey: false,
65+
altKey: false,
66+
shiftKey: false,
67+
metaKey: false,
68+
button: 0,
69+
relatedTarget: undefined
70+
}, options );
71+
72+
if ( document.createEvent ) {
73+
event = document.createEvent( "MouseEvents" );
74+
event.initMouseEvent( type, options.bubbles, options.cancelable,
75+
options.view, options.detail,
76+
options.screenX, options.screenY, options.clientX, options.clientY,
77+
options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
78+
options.button, options.relatedTarget || document.body.parentNode );
79+
80+
// IE 9+ creates events with pageX and pageY set to 0.
81+
// Trying to modify the properties throws an error,
82+
// so we define getters to return the correct values.
83+
if ( event.pageX === 0 && event.pageY === 0 && Object.defineProperty ) {
84+
eventDoc = event.relatedTarget.ownerDocument || document;
85+
doc = eventDoc.documentElement;
86+
body = eventDoc.body;
87+
88+
Object.defineProperty( event, "pageX", {
89+
get: function() {
90+
return options.clientX +
91+
( doc && doc.scrollLeft || body && body.scrollLeft || 0 ) -
92+
( doc && doc.clientLeft || body && body.clientLeft || 0 );
93+
}
94+
});
95+
Object.defineProperty( event, "pageY", {
96+
get: function() {
97+
return options.clientY +
98+
( doc && doc.scrollTop || body && body.scrollTop || 0 ) -
99+
( doc && doc.clientTop || body && body.clientTop || 0 );
100+
}
101+
});
102+
}
103+
} else if ( document.createEventObject ) {
104+
event = document.createEventObject();
105+
$.extend( event, options );
106+
// TODO: what is this mapping for?
107+
event.button = { 0:1, 1:4, 2:2 }[ event.button ] || event.button;
108+
}
109+
110+
return event;
111+
},
112+
113+
keyEvent: function( type, options ) {
114+
var event;
115+
options = $.extend({
116+
bubbles: true,
117+
cancelable: true,
118+
view: window,
119+
ctrlKey: false,
120+
altKey: false,
121+
shiftKey: false,
122+
metaKey: false,
123+
keyCode: 0,
124+
charCode: undefined
125+
}, options );
126+
127+
if ( document.createEvent ) {
128+
try {
129+
event = document.createEvent( "KeyEvents" );
130+
event.initKeyEvent( type, options.bubbles, options.cancelable, options.view,
131+
options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
132+
options.keyCode, options.charCode );
133+
// TODO: what is this supporting?
134+
} catch( err ) {
135+
event = document.createEvent( "Events" );
136+
event.initEvent( type, options.bubbles, options.cancelable );
137+
$.extend( event, {
138+
view: options.view,
139+
ctrlKey: options.ctrlKey,
140+
altKey: options.altKey,
141+
shiftKey: options.shiftKey,
142+
metaKey: options.metaKey,
143+
keyCode: options.keyCode,
144+
charCode: options.charCode
145+
});
146+
}
147+
} else if ( document.createEventObject ) {
148+
event = document.createEventObject();
149+
$.extend( event, options );
150+
}
151+
152+
// TODO: can we hook into core's logic?
153+
if ( $.ui.ie || (({}).toString.call( window.opera ) === "[object Opera]") ) {
154+
// TODO: is charCode ever <0 ? Can we just use charCode || keyCode?
155+
event.keyCode = (options.charCode > 0) ? options.charCode : options.keyCode;
156+
event.charCode = undefined;
157+
}
158+
159+
return event;
160+
},
161+
162+
// TODO: does this need type? Can't we just check event.type?
163+
dispatchEvent: function( elem, type, event ) {
164+
if ( elem.dispatchEvent ) {
165+
elem.dispatchEvent( event );
166+
} else if ( elem.fireEvent ) {
167+
elem.fireEvent( "on" + type, event );
168+
}
169+
},
170+
171+
simulateFocus: function() {
172+
var focusinEvent,
173+
triggered = false,
174+
element = $( this.target );
175+
176+
function trigger() {
177+
triggered = true;
178+
}
179+
180+
element.bind( "focus", trigger );
181+
element[ 0 ].focus();
182+
183+
if ( !triggered ) {
184+
focusinEvent = $.Event( "focusin" );
185+
focusinEvent.preventDefault();
186+
element.trigger( focusinEvent );
187+
element.triggerHandler( "focus" );
188+
}
189+
element.unbind( "focus", trigger );
190+
},
191+
192+
simulateBlur: function() {
193+
var focusoutEvent,
194+
triggered = false,
195+
element = $( this.target );
196+
197+
function trigger() {
198+
triggered = true;
199+
}
200+
201+
element.bind( "blur", trigger );
202+
element[ 0 ].blur();
203+
204+
// blur events are async in IE
205+
setTimeout(function() {
206+
// IE won't let the blur occur if the window is inactive
207+
if ( element[ 0 ].ownerDocument.activeElement === element[ 0 ] ) {
208+
element[ 0 ].ownerDocument.body.focus();
209+
}
210+
211+
// Firefox won't trigger events if the window is inactive
212+
// IE doesn't trigger events if we had to manually focus the body
213+
if ( !triggered ) {
214+
focusoutEvent = $.Event( "focusout" );
215+
focusoutEvent.preventDefault();
216+
element.trigger( focusoutEvent );
217+
element.triggerHandler( "blur" );
218+
}
219+
element.unbind( "blur", trigger );
220+
}, 1 );
221+
}
222+
});
223+
224+
225+
226+
/** complex events **/
227+
228+
function findCenter( elem ) {
229+
var offset,
230+
document = $( elem.ownerDocument );
231+
elem = $( elem );
232+
offset = elem.offset();
233+
234+
return {
235+
x: offset.left + elem.outerWidth() / 2 - document.scrollLeft(),
236+
y: offset.top + elem.outerHeight() / 2 - document.scrollTop()
237+
};
238+
}
239+
240+
$.extend( $.simulate.prototype, {
241+
simulateDrag: function() {
242+
var target = this.target,
243+
options = this.options,
244+
center = findCenter( target ),
245+
x = Math.floor( center.x ),
246+
y = Math.floor( center.y ),
247+
dx = options.dx || 0,
248+
dy = options.dy || 0,
249+
coord = { clientX: x, clientY: y };
250+
this.simulateEvent( target, "mousedown", coord );
251+
coord = { clientX: x + 1, clientY: y + 1 };
252+
this.simulateEvent( document, "mousemove", coord );
253+
coord = { clientX: x + dx, clientY: y + dy };
254+
this.simulateEvent( document, "mousemove", coord );
255+
this.simulateEvent( document, "mousemove", coord );
256+
this.simulateEvent( target, "mouseup", coord );
257+
this.simulateEvent( target, "click", coord );
258+
}
259+
});
260+
261+
})( jQuery );

package.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "jquery-simulate",
3+
"title": "jQuery Simulate",
4+
"description": "jQuery plugin for simulating browser mouse and keyboard events",
5+
"version": "0.0.1",
6+
"homepage": "https://github.com/jquery/jquery-simulate",
7+
"author": {
8+
"name": "jQuery Foundation and other contributors",
9+
"url": "https://github.com/jquery/jquery-simulate/blob/master/AUTHORS.txt"
10+
},
11+
"maintainers": [
12+
{
13+
"name": "Mike Sherov",
14+
"email": "mike.sherov@gmail.com",
15+
"url": "http://mike.sherov.com"
16+
}
17+
],
18+
"repository": {
19+
"type": "git",
20+
"url": "git://github.com/jquery/jquery-simulate.git"
21+
},
22+
"bugs": "https://github.com/jquery/jquery-simulate/issues",
23+
"licenses": [
24+
{
25+
"type": "MIT",
26+
"url": "https://github.com/jquery/jquery-simulate/blob/master/MIT-LICENSE.txt"
27+
}
28+
],
29+
"dependencies": {},
30+
"devDependencies": {
31+
"grunt": "~0.3.17",
32+
"grunt-compare-size": ">=0.1.4",
33+
"grunt-git-authors": "1.0.0",
34+
"testswarm": "0.2.2"
35+
},
36+
"keywords": [ "simulate", "events", "keyboard", "mouse", "jquery" ]
37+
}

test/data/swarminject.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// load testswarm agent
2+
(function() {
3+
var url = window.location.search;
4+
url = decodeURIComponent( url.slice( url.indexOf("swarmURL=") + 9 ) );
5+
if ( !url || url.indexOf("http") !== 0 ) {
6+
return;
7+
}
8+
document.write("<scr" + "ipt src='http://swarm.jquery.org/js/inject.js?" + (new Date).getTime() + "'></scr" + "ipt>");
9+
})();

test/data/testinit.js

Whitespace-only changes.

0 commit comments

Comments
 (0)