Skip to content

Commit 816df16

Browse files
committed
purge cloneGd before resolving/rejecting convert promise
- so that even 'svg' formats (which bypass svgToImg) clear the clone graph div - no need to remove *all* graph div in toImage test anymore 🎉
1 parent 4ec3e0f commit 816df16

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

src/plot_api/to_image.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ function toImage(gd, opts) {
143143
function convert() {
144144
return new Promise(function(resolve, reject) {
145145
var svg = toSVG(clonedGd);
146+
var width = clonedGd._fullLayout.width;
147+
var height = clonedGd._fullLayout.height;
148+
149+
Plotly.purge(clonedGd)
150+
document.body.removeChild(clonedGd)
146151

147152
if(format === 'svg') {
148153
if(imageDataOnly) {
@@ -157,8 +162,8 @@ function toImage(gd, opts) {
157162

158163
svgToImg({
159164
format: format,
160-
width: clonedGd._fullLayout.width,
161-
height: clonedGd._fullLayout.height,
165+
width: width,
166+
height: height,
162167
canvas: canvas,
163168
svg: svg,
164169
// ask svgToImg to return a Promise
@@ -167,14 +172,8 @@ function toImage(gd, opts) {
167172
// compatibility
168173
promise: true
169174
})
170-
.then(function(url) {
171-
Plotly.purge(clonedGd);
172-
document.body.removeChild(clonedGd);
173-
resolve(url);
174-
})
175-
.catch(function(err) {
176-
reject(err);
177-
});
175+
.then(resolve)
176+
.catch(reject)
178177
});
179178
}
180179

test/jasmine/tests/toimage_test.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var Lib = require('@src/lib');
33

44
var d3 = require('d3');
55
var createGraphDiv = require('../assets/create_graph_div');
6+
var destroyGraphDiv = require('../assets/destroy_graph_div');
67
var fail = require('../assets/fail_test');
78
var customMatchers = require('../assets/custom_matchers');
89
var subplotMock = require('@mocks/multiple_subplots.json');
@@ -20,12 +21,7 @@ describe('Plotly.toImage', function() {
2021
gd = createGraphDiv();
2122
});
2223

23-
afterEach(function() {
24-
// make sure ALL graph divs are deleted,
25-
// even the ones generated by Plotly.toImage
26-
d3.selectAll('.js-plotly-plot').remove();
27-
d3.selectAll('#graph').remove();
28-
});
24+
afterEach(destroyGraphDiv)
2925

3026
function createImage(url) {
3127
return new Promise(function(resolve, reject) {

0 commit comments

Comments
 (0)