Skip to content

Commit 6e444d8

Browse files
committed
don't add baseUrl on clipPath URls when staticPlot:true
... but I'm not sure that's correct. We might still need that baseUrl for staticPlot:true graph don't get exported via Plotly.toImage or Plotly.downloadImage.
1 parent fbae9e7 commit 6e444d8

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/components/drawing/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ function nodeHash(node) {
10051005
* note! We'd better not be exporting from a page
10061006
* with a <base> or the svg will not be portable!
10071007
*/
1008-
drawing.setClipUrl = function(s, localId) {
1008+
drawing.setClipUrl = function(s, localId, gd) {
10091009
if(!localId) {
10101010
s.attr('clip-path', null);
10111011
return;
@@ -1025,7 +1025,9 @@ drawing.setClipUrl = function(s, localId) {
10251025
}
10261026
}
10271027

1028-
s.attr('clip-path', 'url(' + drawing.baseUrl + '#' + localId + ')');
1028+
var baseUrl = gd._context.staticPlot ? '' : drawing.baseUrl;
1029+
1030+
s.attr('clip-path', 'url(' + baseUrl + '#' + localId + ')');
10291031
};
10301032

10311033
drawing.getTranslate = function(element) {

test/jasmine/tests/snapshot_test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var Plotly = require('@lib/index');
22
var Lib = require('@src/lib');
3+
var Drawing = require('@src/components/drawing');
34

45
var d3 = require('d3');
56
var createGraphDiv = require('../assets/create_graph_div');
@@ -367,5 +368,37 @@ describe('Plotly.Snapshot', function() {
367368
.catch(failTest)
368369
.then(done);
369370
});
371+
372+
it('should work on pages with <base>', function(done) {
373+
delete Drawing.baseUrl;
374+
var base = d3.select('body')
375+
.append('base')
376+
.attr('href', 'https://plot.ly');
377+
378+
Plotly.plot(gd, [{ y: [1, 2, 1] }], {}, {staticPlot: true})
379+
.then(function() { return Plotly.Snapshot.toSVG(gd); })
380+
.then(function(svg) {
381+
var svgDOM = parser.parseFromString(svg, 'image/svg+xml');
382+
var gSubplot = svgDOM.getElementsByClassName('plot')[0];
383+
var clipPath = gSubplot.getAttribute('clip-path');
384+
var len = clipPath.length;
385+
386+
var head = clipPath.substr(0, 4);
387+
var tail = clipPath.substr(len - 7, len);
388+
expect(head).toBe('url(', 'subplot clipPath head');
389+
expect(tail).toBe('xyplot)', 'subplot clipPath tail');
390+
391+
var middle = clipPath.substr(5, 10);
392+
expect(middle.length).toBe(10, 'subplot clipPath uid length');
393+
expect(middle.indexOf('http://')).toBe(-1, 'no <base> URL in subplot clipPath!');
394+
expect(middle.indexOf('https://')).toBe(-1, 'no <base> URL in subplot clipPath!');
395+
})
396+
.catch(failTest)
397+
.then(function() {
398+
base.remove();
399+
delete Drawing.baseUrl;
400+
done();
401+
});
402+
});
370403
});
371404
});

0 commit comments

Comments
 (0)