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) { ...@@ -58,6 +58,10 @@ function betterTypeOf(input) {
58 '__type' in input) { 58 '__type' in input) {
59 type = input.__type; 59 type = input.__type;
60 } 60 }
61 // gecko returns window instead of domwindow
62 else if (type === 'window') {
63 return 'domwindow';
64 }
61 return type; 65 return type;
62 } catch (e) { 66 } catch (e) {
63 return typeof input; 67 return typeof input;
...@@ -532,7 +536,7 @@ function isValidSelector(value) { ...@@ -532,7 +536,7 @@ function isValidSelector(value) {
532 // phantomjs env has a working document object, let's use it 536 // phantomjs env has a working document object, let's use it
533 document.querySelector(value); 537 document.querySelector(value);
534 } catch(e) { 538 } catch(e) {
535 if ('name' in e && e.name === 'SYNTAX_ERR') { 539 if ('name' in e && (e.name === 'SYNTAX_ERR' || e.name === 'SyntaxError')) {
536 return false; 540 return false;
537 } 541 }
538 } 542 }
...@@ -565,6 +569,32 @@ function isWebPage(what) { ...@@ -565,6 +569,32 @@ function isWebPage(what) {
565 } 569 }
566 exports.isWebPage = isWebPage; 570 exports.isWebPage = isWebPage;
567 571
572
573
574 function isPlainObject(obj) {
575 "use strict";
576 if (!obj || typeof(obj) !== 'object')
577 return false;
578 var type = Object.prototype.toString.call(obj).match(/^\[object\s(.*)\]$/)[1].toLowerCase();
579 return (type === 'object');
580 }
581
582 function mergeObjectsInSlimerjs(origin, add) {
583 "use strict";
584 for (var p in add) {
585 if (isPlainObject(add[p])) {
586 if (isPlainObject(origin[p])) {
587 origin[p] = mergeObjects(origin[p], add[p]);
588 } else {
589 origin[p] = clone(add[p]);
590 }
591 } else {
592 origin[p] = add[p];
593 }
594 }
595 return origin;
596 }
597
568 /** 598 /**
569 * Object recursive merging utility. 599 * Object recursive merging utility.
570 * 600 *
...@@ -574,6 +604,13 @@ exports.isWebPage = isWebPage; ...@@ -574,6 +604,13 @@ exports.isWebPage = isWebPage;
574 */ 604 */
575 function mergeObjects(origin, add) { 605 function mergeObjects(origin, add) {
576 "use strict"; 606 "use strict";
607
608 if (phantom.casperEngine === 'slimerjs') {
609 // Because of an issue in the module system of slimerjs (security membranes?)
610 // constructor is undefined.
611 // let's use an other algorithm
612 return mergeObjectsInSlimerjs(origin, add);
613 }
577 for (var p in add) { 614 for (var p in add) {
578 if (add[p] && add[p].constructor === Object) { 615 if (add[p] && add[p].constructor === Object) {
579 if (origin[p] && origin[p].constructor === Object) { 616 if (origin[p] && origin[p].constructor === Object) {
......
...@@ -288,7 +288,8 @@ casper.test.begin('isJsFile() tests', 5, function(test) { ...@@ -288,7 +288,8 @@ casper.test.begin('isJsFile() tests', 5, function(test) {
288 test.done(); 288 test.done();
289 }); 289 });
290 290
291 casper.test.begin('mergeObjects() tests', 7, function(test) { 291
292 casper.test.begin('mergeObjects() tests', 8, function(test) {
292 var testCases = [ 293 var testCases = [
293 { 294 {
294 obj1: {a: 1}, obj2: {b: 2}, merged: {a: 1, b: 2} 295 obj1: {a: 1}, obj2: {b: 2}, merged: {a: 1, b: 2}
...@@ -326,8 +327,9 @@ casper.test.begin('mergeObjects() tests', 7, function(test) { ...@@ -326,8 +327,9 @@ casper.test.begin('mergeObjects() tests', 7, function(test) {
326 var merged1 = utils.mergeObjects({}, {a: obj}); 327 var merged1 = utils.mergeObjects({}, {a: obj});
327 var merged2 = utils.mergeObjects({a: {}}, {a: obj}); 328 var merged2 = utils.mergeObjects({a: {}}, {a: obj});
328 merged1.a.x = 2; 329 merged1.a.x = 2;
330 test.assertEquals(obj.x, 1, 'mergeObjects() creates deep clones #1');
329 merged2.a.x = 2; 331 merged2.a.x = 2;
330 test.assertEquals(obj.x, 1, 'mergeObjects() creates deep clones'); 332 test.assertEquals(obj.x, 1, 'mergeObjects() creates deep clones #2');
331 test.done(); 333 test.done();
332 }); 334 });
333 335
......