Skip to content

Commit 0a059ce

Browse files
address line notes
1 parent c0c1f27 commit 0a059ce

File tree

4 files changed

+54
-39
lines changed

4 files changed

+54
-39
lines changed

src/plot_api/to_image.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
/*eslint dot-notation: [2, {"allowPattern": "^catch$"}]*/
10-
119
'use strict';
1210

1311
var Plotly = require('../plotly');
@@ -30,10 +28,18 @@ function toImage(gd, opts) {
3028
// default to png
3129
opts.format = opts.format || 'png';
3230

33-
if(
34-
(opts.width && isNumeric(opts.width) && opts.width < 1) ||
35-
(opts.height && isNumeric(opts.height) && opts.height < 1)
36-
) {
31+
var isSizeGood = function(size) {
32+
// undefined and null are valid options
33+
if(size === undefined || size === null) {
34+
return true;
35+
}
36+
37+
if(isNumeric(size) && size > 1) {
38+
return true;
39+
}
40+
};
41+
42+
if(!isSizeGood(opts.width) || !isSizeGood(opts.height)) {
3743
reject(new Error('Height and width should be pixel values.'));
3844
}
3945

src/snapshot/svgtoimg.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
var EventEmitter = require('events').EventEmitter;
10-
119
'use strict';
1210

11+
var EventEmitter = require('events').EventEmitter;
12+
1313
function svgToImg(opts) {
14-
14+
1515
var ev = opts.emitter || new EventEmitter();
1616

1717
var promise = new Promise(function(resolve, reject) {
@@ -55,15 +55,15 @@ function svgToImg(opts) {
5555
reject(new Error('Image format is not jpeg, png or svg'));
5656
// eventually remove the ev
5757
// in favor of promises
58-
if(!opts.promise){
59-
return ev.emit('error', 'Image format is not jpeg, png or svg');
58+
if(!opts.promise) {
59+
return ev.emit('error', 'Image format is not jpeg, png or svg');
6060
}
6161
}
6262
resolve(imgData);
6363
// eventually remove the ev
6464
// in favor of promises
65-
if(!opts.promise){
66-
ev.emit('success', imgData);
65+
if(!opts.promise) {
66+
ev.emit('success', imgData);
6767
}
6868
};
6969

@@ -72,8 +72,8 @@ function svgToImg(opts) {
7272
reject(err);
7373
// eventually remove the ev
7474
// in favor of promises
75-
if(!opts.promise){
76-
return ev.emit('error', err);
75+
if(!opts.promise) {
76+
return ev.emit('error', err);
7777
}
7878
};
7979

@@ -84,9 +84,9 @@ function svgToImg(opts) {
8484
// move to only Promise in 2.0.0
8585
// and eliminate the EventEmitter
8686
if(opts.promise) {
87-
return promise;
87+
return promise;
8888
}
89-
89+
9090
return ev;
9191
}
9292

test/jasmine/tests/download_test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ describe('Plotly.downloadImage', function() {
5454

5555
// create an observer instance
5656
var observer = new MutationObserver(function(mutations) {
57-
mutations.forEach(function(mutation) {
58-
domchanges.push(mutation);
59-
});
57+
mutations.forEach(function(mutation) {
58+
domchanges.push(mutation);
59+
});
6060
});
6161

6262
Plotly.plot(gd, textchartMock.data, textchartMock.layout).then(function(gd) {
@@ -68,17 +68,17 @@ describe('Plotly.downloadImage', function() {
6868
observer.observe(target, config);
6969

7070
return Plotly.downloadImage(gd, {format: 'jpeg', height: 300, width: 300, filename: 'plotly_download'});
71-
}).then(function() {
71+
}).then(function(filename) {
7272
// stop observing
7373
observer.disconnect();
7474
// look for an added and removed link
7575
var linkadded = domchanges[domchanges.length-2].addedNodes[0].outerHTML;
7676
var linkdeleted = domchanges[domchanges.length-1].removedNodes[0].outerHTML;
7777

7878
// check for a <a element and proper file type
79-
expect(linkadded.substr(0,25)).toBe('<a href="data:image/jpeg;');
79+
expect(linkadded.split('href="')[1].split('jpeg;')[0]).toEqual('data:image/');
8080
// check that filename option handled properly
81-
expect(linkadded.substr(linkadded.length-26,15)).toBe('plotly_download');
81+
expect(filename).toBe('plotly_download.jpeg');
8282

8383
// check that link removed
8484
expect(linkadded).toBe(linkdeleted);

test/jasmine/tests/toimage_test.js

+25-16
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ var Plotly = require('@lib/index');
55
var createGraphDiv = require('../assets/create_graph_div');
66
var destroyGraphDiv = require('../assets/destroy_graph_div');
77
var subplotMock = require('../../image/mocks/multiple_subplots.json');
8-
var plot3dMock = require('../../image/mocks/gl3d_bunny.json');
98

109

1110
describe('Plotly.toImage', function() {
@@ -35,24 +34,28 @@ describe('Plotly.toImage', function() {
3534
returnValue.then(done);
3635
});
3736

38-
it('should throw error with unsupported file type', function(done) {
37+
it('should throw error with unsupported file type',function(done) {
3938
// error should actually come in the svgToImg step
4039

4140
Plotly.plot(gd, subplotMock.data, subplotMock.layout)
4241
.then(function(gd) {
43-
Plotly.toImage(gd, {format: 'x'}).catch(function(err) {
42+
Plotly.toImage(gd,{format: 'x'}).catch(function(err) {
4443
expect(err.message).toEqual('Image format is not jpeg, png or svg');
4544
done();
4645
});
4746
});
4847

4948
});
5049

51-
it('should throw error with height and width < 1', function(done) {
50+
it('should throw error with height and/or width < 1', function(done) {
5251
// let user know that Plotly expects pixel values
5352
Plotly.plot(gd, subplotMock.data, subplotMock.layout)
5453
.then(function(gd) {
55-
Plotly.toImage(gd, {height: 0.5}).catch(function(err){
54+
return Plotly.toImage(gd, {height: 0.5}).catch(function(err) {
55+
expect(err.message).toEqual('Height and width should be pixel values.');
56+
});
57+
}).then(function() {
58+
Plotly.toImage(gd, {width: 0.5}).catch(function(err) {
5659
expect(err.message).toEqual('Height and width should be pixel values.');
5760
done();
5861
});
@@ -69,16 +72,22 @@ describe('Plotly.toImage', function() {
6972
Plotly.plot(gd, subplotMock.data, subplotMock.layout).then(function(gd) {
7073
return Plotly.toImage(gd);
7174
}).then(function(url) {
72-
img.src = url;
73-
expect(img.height).toBe(600);
74-
expect(img.width).toBe(700);
75-
// now provide height and width in opts
76-
return Plotly.toImage(gd, {height: 400, width: 400});
75+
return new Promise(function(resolve) {
76+
img.src = url;
77+
img.onload = function() {
78+
expect(img.height).toBe(600);
79+
expect(img.width).toBe(700);
80+
};
81+
// now provide height and width in opts
82+
resolve(Plotly.toImage(gd, {height: 400, width: 400}));
83+
});
7784
}).then(function(url) {
7885
img.src = url;
79-
expect(img.height).toBe(400);
80-
expect(img.width).toBe(400);
81-
done();
86+
img.onload = function() {
87+
expect(img.height).toBe(400);
88+
expect(img.width).toBe(400);
89+
done();
90+
};
8291
});
8392
});
8493

@@ -88,15 +97,15 @@ describe('Plotly.toImage', function() {
8897
plot.then(function(gd) {
8998
return Plotly.toImage(gd, {format: 'png'});
9099
}).then(function(url) {
91-
expect(url.substr(0,15)).toBe('data:image/png;');
100+
expect(url.split('png')[0]).toBe('data:image/');
92101
// now do jpeg
93102
return Plotly.toImage(gd, {format: 'jpeg'});
94103
}).then(function(url) {
95-
expect(url.substr(0,16)).toBe('data:image/jpeg;');
104+
expect(url.split('jpeg')[0]).toBe('data:image/');
96105
// now do webp
97106
return Plotly.toImage(gd, {format: 'webp'});
98107
}).then(function(url) {
99-
expect(url.substr(0,16)).toBe('data:image/webp;');
108+
expect(url.split('webp')[0]).toBe('data:image/');
100109
// now do svg
101110
return Plotly.toImage(gd, {format: 'svg'});
102111
}).then(function(url) {

0 commit comments

Comments
 (0)