Commit 0de06390 0de06390093ccff388f022388b7066db945e6bfe by Laurent Jouanneau

Refs #482: fixes issues in the module utils

- Type of window is "window", not "domwindow" in Gecko
- The name of an exception for a syntax error is "SyntaxError"  in gecko
- fixes utils.mergeObjects: it seems that the object constructor is undefined
  because of a bug in the security layer of sandboxes or a bug in slimerjs?
  Didn't really found the origin of this issue.
1 parent 157408c5
......@@ -58,6 +58,10 @@ function betterTypeOf(input) {
'__type' in input) {
type = input.__type;
}
// gecko returns window instead of domwindow
else if (type === 'window') {
return 'domwindow';
}
return type;
} catch (e) {
return typeof input;
......@@ -532,7 +536,7 @@ function isValidSelector(value) {
// phantomjs env has a working document object, let's use it
document.querySelector(value);
} catch(e) {
if ('name' in e && e.name === 'SYNTAX_ERR') {
if ('name' in e && (e.name === 'SYNTAX_ERR' || e.name === 'SyntaxError')) {
return false;
}
}
......@@ -565,6 +569,32 @@ function isWebPage(what) {
}
exports.isWebPage = isWebPage;
function isPlainObject(obj) {
"use strict";
if (!obj || typeof(obj) !== 'object')
return false;
var type = Object.prototype.toString.call(obj).match(/^\[object\s(.*)\]$/)[1].toLowerCase();
return (type === 'object');
}
function mergeObjectsInSlimerjs(origin, add) {
"use strict";
for (var p in add) {
if (isPlainObject(add[p])) {
if (isPlainObject(origin[p])) {
origin[p] = mergeObjects(origin[p], add[p]);
} else {
origin[p] = clone(add[p]);
}
} else {
origin[p] = add[p];
}
}
return origin;
}
/**
* Object recursive merging utility.
*
......@@ -574,6 +604,13 @@ exports.isWebPage = isWebPage;
*/
function mergeObjects(origin, add) {
"use strict";
if (phantom.casperEngine === 'slimerjs') {
// Because of an issue in the module system of slimerjs (security membranes?)
// constructor is undefined.
// let's use an other algorithm
return mergeObjectsInSlimerjs(origin, add);
}
for (var p in add) {
if (add[p] && add[p].constructor === Object) {
if (origin[p] && origin[p].constructor === Object) {
......
......@@ -288,7 +288,8 @@ casper.test.begin('isJsFile() tests', 5, function(test) {
test.done();
});
casper.test.begin('mergeObjects() tests', 7, function(test) {
casper.test.begin('mergeObjects() tests', 8, function(test) {
var testCases = [
{
obj1: {a: 1}, obj2: {b: 2}, merged: {a: 1, b: 2}
......@@ -326,8 +327,9 @@ casper.test.begin('mergeObjects() tests', 7, function(test) {
var merged1 = utils.mergeObjects({}, {a: obj});
var merged2 = utils.mergeObjects({a: {}}, {a: obj});
merged1.a.x = 2;
test.assertEquals(obj.x, 1, 'mergeObjects() creates deep clones #1');
merged2.a.x = 2;
test.assertEquals(obj.x, 1, 'mergeObjects() creates deep clones');
test.assertEquals(obj.x, 1, 'mergeObjects() creates deep clones #2');
test.done();
});
......