Commit 93dcfbd4 93dcfbd4ef31e52e86ba4a0bd164ec295f027f23 by Nicolas Perriault

jshint configuration, code cleaning

1 parent bcaf4e30
{
"asi": true,
"browser": true,
"debug": true,
"devel": true,
"eqeqeq": true,
"evil": true,
"maxparams": 5,
"maxdepth": 3,
"maxstatements": 15,
"maxcomplexity": 7,
"regexdash": true,
"strict": true,
"sub": true,
"trailing": true,
"undef": true,
"predef" : [
"exports",
"phantom",
"require",
"window"
]
}
docs
modules/vendors
modules/events.js
modules/querystring.js
samples
tests/site
tests/testdir
tests/suites
......@@ -208,12 +208,6 @@ function bootstrap(global) {
// custom global CasperError
global.CasperError = function CasperError(msg) {
Error.call(this);
try {
// let's get where this error has been thrown from, if we can
this._from = arguments.callee.caller.name;
} catch (e) {
this._from = "anonymous";
}
this.message = msg;
this.name = 'CasperError';
};
......
......@@ -75,6 +75,7 @@
* @return string
*/
this.decode = function decode(str) {
/*jshint maxstatements:30 maxcomplexity:30 */
var c1, c2, c3, c4, i = 0, len = str.length, out = "";
while (i < len) {
do {
......@@ -123,6 +124,7 @@
* @return string
*/
this.encode = function encode(str) {
/*jshint maxstatements:30 */
var out = "", i = 0, len = str.length, c1, c2, c3;
while (i < len) {
c1 = str.charCodeAt(i++) & 0xff;
......@@ -295,37 +297,14 @@
* Retrieves string contents from a binary file behind an url. Silently
* fails but log errors.
*
* @param String url
* @param String method
* @param Object data
* @return string
* @param String url Url.
* @param String method HTTP method.
* @param Object data Request parameters.
* @return String
*/
this.getBinary = function getBinary(url, method, data) {
try {
var xhr = new XMLHttpRequest(), dataString = "";
if (typeof method !== "string" || ["GET", "POST"].indexOf(method.toUpperCase()) === -1) {
method = "GET";
} else {
method = method.toUpperCase();
}
xhr.open(method, url, false);
this.log("getBinary(): Using HTTP method: '" + method + "'", "debug");
xhr.overrideMimeType("text/plain; charset=x-user-defined");
if (method === "POST") {
if (typeof data === "object") {
var dataList = [];
for (var k in data) {
dataList.push(encodeURIComponent(k) + "=" + encodeURIComponent(data[k].toString()));
}
dataString = dataList.join('&');
this.log("getBinary(): Using request data: '" + dataString + "'", "debug");
} else if (typeof data === "string") {
dataString = data;
}
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
}
xhr.send(method === "POST" ? dataString : null);
return xhr.responseText;
return this.sendAJAX(url, method, data, false);
} catch (e) {
if (e.name === "NETWORK_ERR" && e.code === 101) {
this.log("getBinary(): Unfortunately, casperjs cannot make cross domain ajax requests", "warning");
......@@ -529,6 +508,42 @@
};
/**
* Performs an AJAX request.
*
* @param String url Url.
* @param String method HTTP method.
* @param Object data Request parameters.
* @param Boolean async Asynchroneous request? (default: false)
* @return String Response text.
*/
this.sendAJAX = function sendAJAX(url, method, data, async) {
var xhr = new XMLHttpRequest(), dataString = "";
if (typeof method !== "string" || ["GET", "POST"].indexOf(method.toUpperCase()) === -1) {
method = "GET";
} else {
method = method.toUpperCase();
}
xhr.open(method, url, !!async);
this.log("getBinary(): Using HTTP method: '" + method + "'", "debug");
xhr.overrideMimeType("text/plain; charset=x-user-defined");
if (method === "POST") {
if (typeof data === "object") {
var dataList = [];
for (var k in data) {
dataList.push(encodeURIComponent(k) + "=" + encodeURIComponent(data[k].toString()));
}
dataString = dataList.join('&');
this.log("sendAJAX(): Using request data: '" + dataString + "'", "debug");
} else if (typeof data === "string") {
dataString = data;
}
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
}
xhr.send(method === "POST" ? dataString : null);
return xhr.responseText;
};
/**
* Sets a field (or a set of fields) value. Fails silently, but log
* error messages.
*
......
......@@ -28,6 +28,8 @@
*
*/
var utils = require('utils');
/*
* Building an Array subclass
*/
......@@ -59,7 +61,8 @@ responseHeaders.prototype.get = function get(name){
* @param mixed response Phantom response or undefined (generally with local files)
*/
exports.augmentResponse = function(response) {
if (response === undefined) {
"use strict";
if (!utils.isHTTPResource(response)) {
return;
}
response.headers.__proto__ = responseHeaders.prototype;
......
......@@ -33,6 +33,36 @@
var utils = require('utils');
var fs = require('fs');
/**
* Generates a value for 'classname' attribute of the JUnit XML report.
*
* Uses the (relative) file name of the current casper script without file
* extension as classname.
*
* @param String classname
* @return String
*/
function generateClassName(classname) {
"use strict";
classname = classname.replace(phantom.casperPath, "").trim();
var script = classname || phantom.casperScript;
if (script.indexOf(fs.workingDirectory) === 0) {
script = script.substring(fs.workingDirectory.length + 1);
}
if (script.indexOf('/') === 0) {
script = script.substring(1, script.length);
}
if (~script.indexOf('.')) {
script = script.substring(0, script.lastIndexOf('.'));
}
return script || "unknown";
}
/**
* Creates a XUnit instance
*
* @return XUnit
*/
exports.create = function create() {
"use strict";
return new XUnitExporter();
......@@ -88,31 +118,6 @@ XUnitExporter.prototype.addFailure = function addFailure(classname, name, messag
};
/**
* Generates a value for 'classname' attribute of the JUnit XML report.
*
* Uses the (relative) file name of the current casper script without file
* extension as classname.
*
* @param String classname
* @return String
*/
function generateClassName(classname) {
"use strict";
classname = classname.replace(phantom.casperPath, "").trim();
var script = classname || phantom.casperScript;
if (script.indexOf(fs.workingDirectory) === 0) {
script = script.substring(fs.workingDirectory.length + 1);
}
if (script.indexOf('/') === 0) {
script = script.substring(1, script.length);
}
if (~script.indexOf('.')) {
script = script.substring(0, script.lastIndexOf('.'));
}
return script || "unknown";
}
/**
* Retrieves generated XML object - actually an HTMLElement.
*
* @return HTMLElement
......
/*global phantom*/
if (!phantom.casperLoaded) {
console.log('This script must be invoked using the casperjs executable');
phantom.exit(1);
......@@ -15,6 +17,7 @@ var casper = require('casper').create({
// local utils
function checkSelfTest(tests) {
"use strict";
var isCasperTest = false;
tests.forEach(function(test) {
var testDir = fs.absolute(fs.dirname(test));
......@@ -28,6 +31,7 @@ function checkSelfTest(tests) {
}
function checkIncludeFile(include) {
"use strict";
var absInclude = fs.absolute(include.trim());
if (!fs.exists(absInclude)) {
casper.warn("%s file not found, can't be included", absInclude);
......@@ -60,6 +64,7 @@ if (casper.cli.get('no-colors') === true) {
// test paths are passed as args
if (casper.cli.args.length) {
tests = casper.cli.args.filter(function(path) {
"use strict";
return fs.isFile(path) || fs.isDirectory(path);
});
} else {
......@@ -75,6 +80,7 @@ if (!phantom.casperSelfTest && checkSelfTest(tests)) {
// includes handling
this.loadIncludes.forEach(function(include){
"use strict";
var container;
if (casper.cli.has(include)) {
container = casper.cli.get(include).split(',').map(function(file) {
......@@ -89,6 +95,7 @@ this.loadIncludes.forEach(function(include){
// test suites completion listener
casper.test.on('tests.complete', function() {
"use strict";
this.renderResults(true, undefined, casper.cli.get('xunit') || undefined);
});
......
/**
* CasperJS local HTTP test server
*/
/*global phantom casper require*/
var colorizer = require('colorizer').create('Colorizer');
var fs = require('fs');
var utils = require('utils');
......@@ -9,10 +12,12 @@ var service;
var testServerPort = 54321;
function info(message) {
"use strict";
console.log(colorizer.colorize('INFO', 'INFO_BAR') + ' ' + message);
}
service = server.listen(testServerPort, function(request, response) {
"use strict";
var pageFile = fs.pathJoin(phantom.casperPath, request.url);
if (!fs.exists(pageFile) || !fs.isFile(pageFile)) {
response.statusCode = 404;
......@@ -26,16 +31,18 @@ service = server.listen(testServerPort, function(request, response) {
// overriding Casper.open to prefix all test urls
casper.setFilter('open.location', function(location) {
"use strict";
if (/^file/.test(location)) {
return location;
}
if (!/^http/.test(location)) {
return f('http://localhost:%d/%s', testServerPort, location);
return utils.format('http://localhost:%d/%s', testServerPort, location);
}
return location;
});
// test suites completion listener
casper.test.on('tests.complete', function() {
"use strict";
server.close();
});
......