Skip to content

Commit 762bbf2

Browse files
committedMay 31, 2014
Spec utils.clone, bugfix dont clone Blobs
1 parent 0a6f16b commit 762bbf2

File tree

4 files changed

+77
-10
lines changed

4 files changed

+77
-10
lines changed
 

‎demos/Collage/index.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ <h1>Hi, welcome to Collage</h1>
7777
// Register with Hello
7878
function loadImages(network){
7979

80-
hello.login(network, function(){
80+
hello(network).login(function(){
8181

8282
// Does it have siblings?
83-
hello.api(network+':me/albums', function(r){
83+
hello(network).api('me/albums', function(r){
8484

8585
if(!r||r.error){
8686
alert(r.error.message);
@@ -90,7 +90,7 @@ <h1>Hi, welcome to Collage</h1>
9090
// Loop through and add photos to the canvas
9191
for(var i=0;i<r.data.length;i++){
9292

93-
hello.api(network+':'+r.data[i].photos, function(r){
93+
hello(network).api('me/album', {id : r.data[i].id}, function(r){
9494

9595
if(!r||r.error){
9696
alert(r.error.message);

‎src/hello.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -1989,9 +1989,11 @@ hello.utils.extend( hello.utils, {
19891989
// Clone
19901990
// Create a clone of an object
19911991
clone : function(obj){
1992-
if("nodeName" in obj){
1992+
// Does not clone Dom elements, nor Binary data, e.g. Blobs, Filelists
1993+
if("nodeName" in obj || this.isBinary( obj ) ){
19931994
return obj;
19941995
}
1996+
// But does clone everything else.
19951997
var clone = {}, x;
19961998
for(x in obj){
19971999
if(typeof(obj[x]) === 'object'){
@@ -2478,17 +2480,21 @@ hello.utils.extend( hello.utils, {
24782480
// Some of the providers require that only MultiPart is used with non-binary forms.
24792481
// This function checks whether the form contains binary data
24802482
hasBinary : function (data){
2481-
var w = window;
24822483
for(var x in data ) if(data.hasOwnProperty(x)){
2483-
if( (this.domInstance('input', data[x]) && data[x].type === 'file') ||
2484-
("FileList" in w && data[x] instanceof w.FileList) ||
2485-
("File" in w && data[x] instanceof w.File) ||
2486-
("Blob" in w && data[x] instanceof w.Blob)
2487-
){
2484+
if( this.isBinary(data[x]) ){
24882485
return true;
24892486
}
24902487
}
24912488
return false;
2489+
},
2490+
2491+
isBinary : function(data){
2492+
2493+
return (this.domInstance('input', data) && data.type === 'file') ||
2494+
("FileList" in window && data instanceof window.FileList) ||
2495+
("File" in window && data instanceof window.File) ||
2496+
("Blob" in window && data instanceof window.Blob);
2497+
24922498
}
24932499
});
24942500

‎tests/specs/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
require([
3434
// UTILS
3535
'./unit/utils/args',
36+
'./unit/utils/clone',
3637
'./unit/utils/dataToJSON',
3738
'./unit/utils/diff',
3839
'./unit/utils/domInstance',

‎tests/specs/unit/utils/clone.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
define([
2+
// '../../../../src/utils/dataToJSON'
3+
], function(
4+
// dataToJSON
5+
){
6+
7+
var utils = hello.utils;
8+
9+
//
10+
// Are errors thrown if an invalid network is provided
11+
//
12+
describe('utils / clone', function(){
13+
14+
it('should clone a simple object', function(){
15+
16+
var test = {
17+
prop : 'prop'
18+
};
19+
20+
var value = utils.clone(test);
21+
22+
// Assert that its the same but different.
23+
expect( value ).to.be.eql( test ).and.not.to.be.equal( test );
24+
25+
});
26+
27+
it('should not clone Blob values', function(){
28+
29+
var blob = new Blob();
30+
31+
var test = {
32+
prop : blob
33+
};
34+
35+
var value = utils.clone(test);
36+
37+
// Assert that its the same but different.
38+
expect( value.prop ).to.be.a( Blob ).and.to.be.equal( blob );
39+
40+
});
41+
42+
it('should not clone DOM element', function(){
43+
44+
var node = document.createElement('input');
45+
46+
var test = {
47+
prop : node
48+
};
49+
50+
var value = utils.clone(test);
51+
52+
// Assert that its the same but different.
53+
expect( value.prop ).to.be.a( HTMLElement ).and.to.be.equal( node );
54+
55+
});
56+
57+
});
58+
59+
60+
});

0 commit comments

Comments
 (0)
Please sign in to comment.