phantomjs-binaries 1.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -0
- data/Gemfile.lock +18 -0
- data/Rakefile +1 -0
- data/bin/bootstrap.js +342 -0
- data/bin/casperjs +58 -0
- data/bin/phantomjs +13 -0
- data/bin/phantomjs-1.8.0-darwin-x86_64 +0 -0
- data/bin/phantomjs-1.8.0-linux-x86_64 +0 -0
- data/bin/usage.txt +11 -0
- data/lib/phantomjs-binaries/version.rb +5 -0
- data/lib/phantomjs-binaries.rb +7 -0
- data/modules/casper.js +2101 -0
- data/modules/cli.js +138 -0
- data/modules/clientutils.js +781 -0
- data/modules/colorizer.js +130 -0
- data/modules/events.js +259 -0
- data/modules/http.js +70 -0
- data/modules/mouse.js +109 -0
- data/modules/pagestack.js +166 -0
- data/modules/querystring.js +187 -0
- data/modules/tester.js +1161 -0
- data/modules/utils.js +581 -0
- data/modules/vendors/coffee-script.js +8 -0
- data/modules/xunit.js +155 -0
- data/package.json +35 -0
- data/phantomjs-binaries.gemspec +24 -0
- metadata +95 -0
@@ -0,0 +1,130 @@
|
|
1
|
+
/*!
|
2
|
+
* Casper is a navigation utility for PhantomJS.
|
3
|
+
*
|
4
|
+
* Documentation: http://casperjs.org/
|
5
|
+
* Repository: http://github.com/n1k0/casperjs
|
6
|
+
*
|
7
|
+
* Copyright (c) 2011-2012 Nicolas Perriault
|
8
|
+
*
|
9
|
+
* Part of source code is Copyright Joyent, Inc. and other Node contributors.
|
10
|
+
*
|
11
|
+
* Permission is hereby granted, free of charge, to any person obtaining a
|
12
|
+
* copy of this software and associated documentation files (the "Software"),
|
13
|
+
* to deal in the Software without restriction, including without limitation
|
14
|
+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
15
|
+
* and/or sell copies of the Software, and to permit persons to whom the
|
16
|
+
* Software is furnished to do so, subject to the following conditions:
|
17
|
+
*
|
18
|
+
* The above copyright notice and this permission notice shall be included
|
19
|
+
* in all copies or substantial portions of the Software.
|
20
|
+
*
|
21
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
22
|
+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
23
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
24
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
25
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
26
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
27
|
+
* DEALINGS IN THE SOFTWARE.
|
28
|
+
*
|
29
|
+
*/
|
30
|
+
|
31
|
+
/*global exports console require*/
|
32
|
+
|
33
|
+
var fs = require('fs');
|
34
|
+
var utils = require('utils');
|
35
|
+
|
36
|
+
exports.create = function create(type) {
|
37
|
+
"use strict";
|
38
|
+
if (!type) {
|
39
|
+
return;
|
40
|
+
}
|
41
|
+
if (!(type in exports)) {
|
42
|
+
throw new Error(utils.format('Unsupported colorizer type "%s"', type));
|
43
|
+
}
|
44
|
+
return new exports[type]();
|
45
|
+
};
|
46
|
+
|
47
|
+
/**
|
48
|
+
* This is a port of lime colorizer.
|
49
|
+
* http://trac.symfony-project.org/browser/tools/lime/trunk/lib/lime.php
|
50
|
+
*
|
51
|
+
* (c) Fabien Potencier, Symfony project, MIT license
|
52
|
+
*/
|
53
|
+
var Colorizer = function Colorizer() {
|
54
|
+
"use strict";
|
55
|
+
var options = { bold: 1, underscore: 4, blink: 5, reverse: 7, conceal: 8 };
|
56
|
+
var foreground = { black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, white: 37 };
|
57
|
+
var background = { black: 40, red: 41, green: 42, yellow: 43, blue: 44, magenta: 45, cyan: 46, white: 47 };
|
58
|
+
var styles = {
|
59
|
+
'ERROR': { bg: 'red', fg: 'white', bold: true },
|
60
|
+
'INFO': { fg: 'green', bold: true },
|
61
|
+
'TRACE': { fg: 'green', bold: true },
|
62
|
+
'PARAMETER': { fg: 'cyan' },
|
63
|
+
'COMMENT': { fg: 'yellow' },
|
64
|
+
'WARNING': { fg: 'red', bold: true },
|
65
|
+
'GREEN_BAR': { fg: 'white', bg: 'green', bold: true },
|
66
|
+
'RED_BAR': { fg: 'white', bg: 'red', bold: true },
|
67
|
+
'INFO_BAR': { bg: 'cyan', fg: 'white', bold: true },
|
68
|
+
'WARN_BAR': { bg: 'yellow', fg: 'white', bold: true }
|
69
|
+
};
|
70
|
+
|
71
|
+
/**
|
72
|
+
* Adds a style to provided text.
|
73
|
+
*
|
74
|
+
* @param String text
|
75
|
+
* @param String styleName
|
76
|
+
* @return String
|
77
|
+
*/
|
78
|
+
this.colorize = function colorize(text, styleName, pad) {
|
79
|
+
if (fs.isWindows() || !(styleName in styles)) {
|
80
|
+
return text;
|
81
|
+
}
|
82
|
+
return this.format(text, styles[styleName], pad);
|
83
|
+
};
|
84
|
+
|
85
|
+
/**
|
86
|
+
* Formats a text using a style declaration object.
|
87
|
+
*
|
88
|
+
* @param String text
|
89
|
+
* @param Object style
|
90
|
+
* @return String
|
91
|
+
*/
|
92
|
+
this.format = function format(text, style, pad) {
|
93
|
+
if (fs.isWindows() || !utils.isObject(style)) {
|
94
|
+
return text;
|
95
|
+
}
|
96
|
+
var codes = [];
|
97
|
+
if (style.fg && foreground[style.fg]) {
|
98
|
+
codes.push(foreground[style.fg]);
|
99
|
+
}
|
100
|
+
if (style.bg && background[style.bg]) {
|
101
|
+
codes.push(background[style.bg]);
|
102
|
+
}
|
103
|
+
for (var option in options) {
|
104
|
+
if (style[option] === true) {
|
105
|
+
codes.push(options[option]);
|
106
|
+
}
|
107
|
+
}
|
108
|
+
// pad
|
109
|
+
if (typeof pad === "number" && text.length < pad) {
|
110
|
+
text += new Array(pad - text.length + 1).join(' ');
|
111
|
+
}
|
112
|
+
return "\u001b[" + codes.join(';') + 'm' + text + "\u001b[0m";
|
113
|
+
};
|
114
|
+
};
|
115
|
+
exports.Colorizer = Colorizer;
|
116
|
+
|
117
|
+
/**
|
118
|
+
* Dummy colorizer. Does basically nothing.
|
119
|
+
*
|
120
|
+
*/
|
121
|
+
var Dummy = function Dummy() {
|
122
|
+
"use strict";
|
123
|
+
this.colorize = function colorize(text, styleName, pad) {
|
124
|
+
return text;
|
125
|
+
};
|
126
|
+
this.format = function format(text, style, pad){
|
127
|
+
return text;
|
128
|
+
};
|
129
|
+
};
|
130
|
+
exports.Dummy = Dummy;
|
data/modules/events.js
ADDED
@@ -0,0 +1,259 @@
|
|
1
|
+
// Copyright Joyent, Inc. and other Node contributors.
|
2
|
+
//
|
3
|
+
// Permission is hereby granted, free of charge, to any person obtaining a
|
4
|
+
// copy of this software and associated documentation files (the
|
5
|
+
// "Software"), to deal in the Software without restriction, including
|
6
|
+
// without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
8
|
+
// persons to whom the Software is furnished to do so, subject to the
|
9
|
+
// following conditions:
|
10
|
+
//
|
11
|
+
// The above copyright notice and this permission notice shall be included
|
12
|
+
// in all copies or substantial portions of the Software.
|
13
|
+
//
|
14
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
15
|
+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
17
|
+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
18
|
+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
19
|
+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
20
|
+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
22
|
+
/*global CasperError*/
|
23
|
+
|
24
|
+
var isArray = Array.isArray;
|
25
|
+
|
26
|
+
function EventEmitter() {
|
27
|
+
this._filters = {};
|
28
|
+
}
|
29
|
+
exports.EventEmitter = EventEmitter;
|
30
|
+
|
31
|
+
// By default EventEmitters will print a warning if more than
|
32
|
+
// 10 listeners are added to it. This is a useful default which
|
33
|
+
// helps finding memory leaks.
|
34
|
+
//
|
35
|
+
// Obviously not all Emitters should be limited to 10. This function allows
|
36
|
+
// that to be increased. Set to zero for unlimited.
|
37
|
+
var defaultMaxListeners = 10;
|
38
|
+
EventEmitter.prototype.setMaxListeners = function(n) {
|
39
|
+
if (!this._events) this._events = {};
|
40
|
+
this._maxListeners = n;
|
41
|
+
};
|
42
|
+
|
43
|
+
|
44
|
+
EventEmitter.prototype.emit = function() {
|
45
|
+
var type = arguments[0];
|
46
|
+
// If there is no 'error' event listener then throw.
|
47
|
+
if (type === 'error') {
|
48
|
+
if (!this._events || !this._events.error ||
|
49
|
+
(isArray(this._events.error) && !this._events.error.length))
|
50
|
+
{
|
51
|
+
if (arguments[1] instanceof Error) {
|
52
|
+
throw arguments[1]; // Unhandled 'error' event
|
53
|
+
} else {
|
54
|
+
throw new CasperError("Uncaught, unspecified 'error' event.");
|
55
|
+
}
|
56
|
+
}
|
57
|
+
}
|
58
|
+
|
59
|
+
if (!this._events) return false;
|
60
|
+
var handler = this._events[type];
|
61
|
+
if (!handler) return false;
|
62
|
+
|
63
|
+
if (typeof handler == 'function') {
|
64
|
+
switch (arguments.length) {
|
65
|
+
// fast cases
|
66
|
+
case 1:
|
67
|
+
handler.call(this);
|
68
|
+
break;
|
69
|
+
case 2:
|
70
|
+
handler.call(this, arguments[1]);
|
71
|
+
break;
|
72
|
+
case 3:
|
73
|
+
handler.call(this, arguments[1], arguments[2]);
|
74
|
+
break;
|
75
|
+
// slower
|
76
|
+
default:
|
77
|
+
var l = arguments.length;
|
78
|
+
var args = new Array(l - 1);
|
79
|
+
for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
|
80
|
+
handler.apply(this, args);
|
81
|
+
}
|
82
|
+
return true;
|
83
|
+
|
84
|
+
} else if (isArray(handler)) {
|
85
|
+
var l = arguments.length;
|
86
|
+
var args = new Array(l - 1);
|
87
|
+
for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
|
88
|
+
|
89
|
+
var listeners = handler.slice();
|
90
|
+
for (var i = 0, l = listeners.length; i < l; i++) {
|
91
|
+
listeners[i].apply(this, args);
|
92
|
+
}
|
93
|
+
return true;
|
94
|
+
|
95
|
+
} else {
|
96
|
+
return false;
|
97
|
+
}
|
98
|
+
};
|
99
|
+
|
100
|
+
// EventEmitter is defined in src/node_events.cc
|
101
|
+
// EventEmitter.prototype.emit() is also defined there.
|
102
|
+
EventEmitter.prototype.addListener = function(type, listener) {
|
103
|
+
if ('function' !== typeof listener) {
|
104
|
+
throw new CasperError('addListener only takes instances of Function');
|
105
|
+
}
|
106
|
+
|
107
|
+
if (!this._events) this._events = {};
|
108
|
+
|
109
|
+
// To avoid recursion in the case that type == "newListeners"! Before
|
110
|
+
// adding it to the listeners, first emit "newListeners".
|
111
|
+
this.emit('newListener', type, listener);
|
112
|
+
|
113
|
+
if (!this._events[type]) {
|
114
|
+
// Optimize the case of one listener. Don't need the extra array object.
|
115
|
+
this._events[type] = listener;
|
116
|
+
} else if (isArray(this._events[type])) {
|
117
|
+
|
118
|
+
// If we've already got an array, just append.
|
119
|
+
this._events[type].push(listener);
|
120
|
+
|
121
|
+
// Check for listener leak
|
122
|
+
if (!this._events[type].warned) {
|
123
|
+
var m;
|
124
|
+
if (this._maxListeners !== undefined) {
|
125
|
+
m = this._maxListeners;
|
126
|
+
} else {
|
127
|
+
m = defaultMaxListeners;
|
128
|
+
}
|
129
|
+
|
130
|
+
if (m && m > 0 && this._events[type].length > m) {
|
131
|
+
this._events[type].warned = true;
|
132
|
+
console.error('(node) warning: possible EventEmitter memory ' +
|
133
|
+
'leak detected. %d listeners added. ' +
|
134
|
+
'Use emitter.setMaxListeners() to increase limit.',
|
135
|
+
this._events[type].length);
|
136
|
+
console.trace();
|
137
|
+
}
|
138
|
+
}
|
139
|
+
} else {
|
140
|
+
// Adding the second element, need to change to array.
|
141
|
+
this._events[type] = [this._events[type], listener];
|
142
|
+
}
|
143
|
+
|
144
|
+
return this;
|
145
|
+
};
|
146
|
+
|
147
|
+
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
|
148
|
+
|
149
|
+
EventEmitter.prototype.once = function(type, listener) {
|
150
|
+
if ('function' !== typeof listener) {
|
151
|
+
throw new CasperError('.once only takes instances of Function');
|
152
|
+
}
|
153
|
+
|
154
|
+
var self = this;
|
155
|
+
function g() {
|
156
|
+
self.removeListener(type, g);
|
157
|
+
listener.apply(this, arguments);
|
158
|
+
};
|
159
|
+
|
160
|
+
g.listener = listener;
|
161
|
+
self.on(type, g);
|
162
|
+
|
163
|
+
return this;
|
164
|
+
};
|
165
|
+
|
166
|
+
EventEmitter.prototype.removeListener = function(type, listener) {
|
167
|
+
if ('function' !== typeof listener) {
|
168
|
+
throw new CasperError('removeListener only takes instances of Function');
|
169
|
+
}
|
170
|
+
|
171
|
+
// does not use listeners(), so no side effect of creating _events[type]
|
172
|
+
if (!this._events || !this._events[type]) return this;
|
173
|
+
|
174
|
+
var list = this._events[type];
|
175
|
+
|
176
|
+
if (isArray(list)) {
|
177
|
+
var position = -1;
|
178
|
+
for (var i = 0, length = list.length; i < length; i++) {
|
179
|
+
if (list[i] === listener ||
|
180
|
+
(list[i].listener && list[i].listener === listener))
|
181
|
+
{
|
182
|
+
position = i;
|
183
|
+
break;
|
184
|
+
}
|
185
|
+
}
|
186
|
+
|
187
|
+
if (position < 0) return this;
|
188
|
+
list.splice(position, 1);
|
189
|
+
if (list.length == 0)
|
190
|
+
delete this._events[type];
|
191
|
+
} else if (list === listener ||
|
192
|
+
(list.listener && list.listener === listener))
|
193
|
+
{
|
194
|
+
delete this._events[type];
|
195
|
+
}
|
196
|
+
|
197
|
+
return this;
|
198
|
+
};
|
199
|
+
|
200
|
+
EventEmitter.prototype.removeAllListeners = function(type) {
|
201
|
+
if (arguments.length === 0) {
|
202
|
+
this._events = {};
|
203
|
+
return this;
|
204
|
+
}
|
205
|
+
|
206
|
+
// does not use listeners(), so no side effect of creating _events[type]
|
207
|
+
if (type && this._events && this._events[type]) this._events[type] = null;
|
208
|
+
return this;
|
209
|
+
};
|
210
|
+
|
211
|
+
EventEmitter.prototype.listeners = function(type) {
|
212
|
+
if (!this._events) this._events = {};
|
213
|
+
if (!this._events[type]) this._events[type] = [];
|
214
|
+
if (!isArray(this._events[type])) {
|
215
|
+
this._events[type] = [this._events[type]];
|
216
|
+
}
|
217
|
+
return this._events[type];
|
218
|
+
};
|
219
|
+
|
220
|
+
// Added for CasperJS: filters a value attached to an event
|
221
|
+
EventEmitter.prototype.filter = function() {
|
222
|
+
var type = arguments[0];
|
223
|
+
if (!this._filters) {
|
224
|
+
this._filters = {};
|
225
|
+
return;
|
226
|
+
}
|
227
|
+
|
228
|
+
var filter = this._filters[type];
|
229
|
+
if (typeof filter !== 'function') {
|
230
|
+
return;
|
231
|
+
}
|
232
|
+
return filter.apply(this, Array.prototype.splice.call(arguments, 1));
|
233
|
+
};
|
234
|
+
|
235
|
+
EventEmitter.prototype.removeAllFilters = function(type) {
|
236
|
+
if (arguments.length === 0) {
|
237
|
+
this._filters = {};
|
238
|
+
return this;
|
239
|
+
}
|
240
|
+
if (type && this._filters && this._filters[type]) {
|
241
|
+
this._filters[type] = null;
|
242
|
+
}
|
243
|
+
return this;
|
244
|
+
};
|
245
|
+
|
246
|
+
EventEmitter.prototype.setFilter = function(type, filterFn) {
|
247
|
+
if (!this._filters) {
|
248
|
+
this._filters = {};
|
249
|
+
}
|
250
|
+
if ('function' !== typeof filterFn) {
|
251
|
+
throw new CasperError('setFilter only takes instances of Function');
|
252
|
+
}
|
253
|
+
if (!this._filters[type]) {
|
254
|
+
this._filters[type] = filterFn;
|
255
|
+
return true;
|
256
|
+
}
|
257
|
+
// TODO: process multiple filters? in which order? disallow?
|
258
|
+
return false;
|
259
|
+
};
|
data/modules/http.js
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
/*!
|
2
|
+
* Casper is a navigation utility for PhantomJS.
|
3
|
+
*
|
4
|
+
* Documentation: http://casperjs.org/
|
5
|
+
* Repository: http://github.com/n1k0/casperjs
|
6
|
+
*
|
7
|
+
* Copyright (c) 2011-2012 Nicolas Perriault
|
8
|
+
*
|
9
|
+
* Part of source code is Copyright Joyent, Inc. and other Node contributors.
|
10
|
+
*
|
11
|
+
* Permission is hereby granted, free of charge, to any person obtaining a
|
12
|
+
* copy of this software and associated documentation files (the "Software"),
|
13
|
+
* to deal in the Software without restriction, including without limitation
|
14
|
+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
15
|
+
* and/or sell copies of the Software, and to permit persons to whom the
|
16
|
+
* Software is furnished to do so, subject to the following conditions:
|
17
|
+
*
|
18
|
+
* The above copyright notice and this permission notice shall be included
|
19
|
+
* in all copies or substantial portions of the Software.
|
20
|
+
*
|
21
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
22
|
+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
23
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
24
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
25
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
26
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
27
|
+
* DEALINGS IN THE SOFTWARE.
|
28
|
+
*
|
29
|
+
*/
|
30
|
+
|
31
|
+
var utils = require('utils');
|
32
|
+
|
33
|
+
/*
|
34
|
+
* Building an Array subclass
|
35
|
+
*/
|
36
|
+
function responseHeaders(){}
|
37
|
+
responseHeaders.prototype = [];
|
38
|
+
|
39
|
+
/**
|
40
|
+
* Retrieves a given header based on its name
|
41
|
+
*
|
42
|
+
* @param String name A case-insensitive response header name
|
43
|
+
* @return mixed A header string or `null` if not found
|
44
|
+
*/
|
45
|
+
responseHeaders.prototype.get = function get(name){
|
46
|
+
"use strict";
|
47
|
+
var headerValue = null;
|
48
|
+
name = name.toLowerCase();
|
49
|
+
this.some(function(header){
|
50
|
+
if (header.name.toLowerCase() === name){
|
51
|
+
headerValue = header.value;
|
52
|
+
return true;
|
53
|
+
}
|
54
|
+
});
|
55
|
+
return headerValue;
|
56
|
+
};
|
57
|
+
|
58
|
+
/**
|
59
|
+
* Augment the response with proper prototypes
|
60
|
+
*
|
61
|
+
* @param mixed response Phantom response or undefined (generally with local files)
|
62
|
+
*/
|
63
|
+
exports.augmentResponse = function(response) {
|
64
|
+
"use strict";
|
65
|
+
/*jshint proto:true*/
|
66
|
+
if (!utils.isHTTPResource(response)) {
|
67
|
+
return;
|
68
|
+
}
|
69
|
+
response.headers.__proto__ = responseHeaders.prototype;
|
70
|
+
};
|
data/modules/mouse.js
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
/*!
|
2
|
+
* Casper is a navigation utility for PhantomJS.
|
3
|
+
*
|
4
|
+
* Documentation: http://casperjs.org/
|
5
|
+
* Repository: http://github.com/n1k0/casperjs
|
6
|
+
*
|
7
|
+
* Copyright (c) 2011-2012 Nicolas Perriault
|
8
|
+
*
|
9
|
+
* Part of source code is Copyright Joyent, Inc. and other Node contributors.
|
10
|
+
*
|
11
|
+
* Permission is hereby granted, free of charge, to any person obtaining a
|
12
|
+
* copy of this software and associated documentation files (the "Software"),
|
13
|
+
* to deal in the Software without restriction, including without limitation
|
14
|
+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
15
|
+
* and/or sell copies of the Software, and to permit persons to whom the
|
16
|
+
* Software is furnished to do so, subject to the following conditions:
|
17
|
+
*
|
18
|
+
* The above copyright notice and this permission notice shall be included
|
19
|
+
* in all copies or substantial portions of the Software.
|
20
|
+
*
|
21
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
22
|
+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
23
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
24
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
25
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
26
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
27
|
+
* DEALINGS IN THE SOFTWARE.
|
28
|
+
*
|
29
|
+
*/
|
30
|
+
|
31
|
+
/*global CasperError exports require*/
|
32
|
+
|
33
|
+
var utils = require('utils');
|
34
|
+
|
35
|
+
exports.create = function create(casper) {
|
36
|
+
"use strict";
|
37
|
+
return new Mouse(casper);
|
38
|
+
};
|
39
|
+
|
40
|
+
var Mouse = function Mouse(casper) {
|
41
|
+
"use strict";
|
42
|
+
if (!utils.isCasperObject(casper)) {
|
43
|
+
throw new CasperError('Mouse() needs a Casper instance');
|
44
|
+
}
|
45
|
+
|
46
|
+
var slice = Array.prototype.slice;
|
47
|
+
|
48
|
+
var nativeEvents = ['mouseup', 'mousedown', 'click', 'mousemove'];
|
49
|
+
var emulatedEvents = ['mouseover', 'mouseout'];
|
50
|
+
var supportedEvents = nativeEvents.concat(emulatedEvents);
|
51
|
+
|
52
|
+
function computeCenter(selector) {
|
53
|
+
var bounds = casper.getElementBounds(selector);
|
54
|
+
if (utils.isClipRect(bounds)) {
|
55
|
+
var x = Math.round(bounds.left + bounds.width / 2);
|
56
|
+
var y = Math.round(bounds.top + bounds.height / 2);
|
57
|
+
return [x, y];
|
58
|
+
}
|
59
|
+
}
|
60
|
+
|
61
|
+
function processEvent(type, args) {
|
62
|
+
if (!utils.isString(type) || supportedEvents.indexOf(type) === -1) {
|
63
|
+
throw new CasperError('Mouse.processEvent(): Unsupported mouse event type: ' + type);
|
64
|
+
}
|
65
|
+
if (emulatedEvents.indexOf(type) > -1) {
|
66
|
+
casper.log("Mouse.processEvent(): no native fallback for type " + type, "warning");
|
67
|
+
}
|
68
|
+
args = slice.call(args); // cast Arguments -> Array
|
69
|
+
casper.emit('mouse.' + type.replace('mouse', ''), args);
|
70
|
+
switch (args.length) {
|
71
|
+
case 0:
|
72
|
+
throw new CasperError('Mouse.processEvent(): Too few arguments');
|
73
|
+
case 1:
|
74
|
+
// selector
|
75
|
+
casper.page.sendEvent.apply(casper.page, [type].concat(computeCenter(args[0])));
|
76
|
+
break;
|
77
|
+
case 2:
|
78
|
+
// coordinates
|
79
|
+
if (!utils.isNumber(args[0]) || !utils.isNumber(args[1])) {
|
80
|
+
throw new CasperError('Mouse.processEvent(): No valid coordinates passed: ' + args);
|
81
|
+
}
|
82
|
+
casper.page.sendEvent(type, args[0], args[1]);
|
83
|
+
break;
|
84
|
+
default:
|
85
|
+
throw new CasperError('Mouse.processEvent(): Too many arguments');
|
86
|
+
}
|
87
|
+
}
|
88
|
+
|
89
|
+
this.processEvent = function() {
|
90
|
+
processEvent(arguments[0], slice.call(arguments, 1));
|
91
|
+
};
|
92
|
+
|
93
|
+
this.click = function click() {
|
94
|
+
processEvent('click', arguments);
|
95
|
+
};
|
96
|
+
|
97
|
+
this.down = function down() {
|
98
|
+
processEvent('mousedown', arguments);
|
99
|
+
};
|
100
|
+
|
101
|
+
this.move = function move() {
|
102
|
+
processEvent('mousemove', arguments);
|
103
|
+
};
|
104
|
+
|
105
|
+
this.up = function up() {
|
106
|
+
processEvent('mouseup', arguments);
|
107
|
+
};
|
108
|
+
};
|
109
|
+
exports.Mouse = Mouse;
|