Skip to content

Commit d12b097

Browse files
committed
stash <base> href on gd._context
... now that we pass 'gd' to Drawing.setClipUrl
1 parent 6e444d8 commit d12b097

File tree

5 files changed

+18
-32
lines changed

5 files changed

+18
-32
lines changed

src/components/drawing/index.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,21 +1011,8 @@ drawing.setClipUrl = function(s, localId, gd) {
10111011
return;
10121012
}
10131013

1014-
if(drawing.baseUrl === undefined) {
1015-
var base = d3.select('base');
1016-
1017-
// Stash base url once and for all!
1018-
// We may have to stash this elsewhere when
1019-
// we'll try to support for child windows
1020-
// more info -> https://github.com/plotly/plotly.js/issues/702
1021-
if(base.size() && base.attr('href')) {
1022-
drawing.baseUrl = window.location.href.split('#')[0];
1023-
} else {
1024-
drawing.baseUrl = '';
1025-
}
1026-
}
1027-
1028-
var baseUrl = gd._context.staticPlot ? '' : drawing.baseUrl;
1014+
var context = gd._context;
1015+
var baseUrl = context.staticPlot ? '' : (context.baseUrl || '');
10291016

10301017
s.attr('clip-path', 'url(' + baseUrl + '#' + localId + ')');
10311018
};

src/plot_api/plot_api.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,6 @@ exports.plot = function(gd, data, layout, config) {
110110
// so we can share cached text across tabs
111111
Drawing.makeTester();
112112

113-
// clear stashed base url
114-
delete Drawing.baseUrl;
115-
116113
// collect promises for any async actions during plotting
117114
// any part of the plotting code can push to gd._promises, then
118115
// before we move to the next step, we check that they're all
@@ -419,7 +416,16 @@ function opaqueSetBackground(gd, bgColor) {
419416
}
420417

421418
function setPlotContext(gd, config) {
422-
if(!gd._context) gd._context = Lib.extendDeep({}, defaultConfig);
419+
if(!gd._context) {
420+
gd._context = Lib.extendDeep({}, defaultConfig);
421+
422+
// stash <base> href, used to make robust clipPath URLs
423+
var base = d3.select('base');
424+
gd._context.baseUrl = base.size() && base.attr('href') ?
425+
window.location.href.split('#')[0] :
426+
'';
427+
}
428+
423429
var context = gd._context;
424430

425431
var i, keys, key;

test/jasmine/tests/drawing_test.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@ describe('Drawing', function() {
1919
afterEach(function() {
2020
this.svg.remove();
2121
this.g.remove();
22-
23-
// unstash base url from Drawing module object
24-
delete Drawing.baseUrl;
2522
});
2623

2724
it('should set the clip-path attribute', function() {
2825
expect(this.g.attr('clip-path')).toBe(null);
2926

30-
Drawing.setClipUrl(this.g, 'id1');
27+
Drawing.setClipUrl(this.g, 'id1', {_context: {}});
3128

3229
expect(this.g.attr('clip-path')).toEqual('url(#id1)');
3330
});
@@ -49,7 +46,7 @@ describe('Drawing', function() {
4946
// grab window URL
5047
var href = window.location.href;
5148

52-
Drawing.setClipUrl(this.g, 'id3');
49+
Drawing.setClipUrl(this.g, 'id3', {_context: {baseUrl: href}});
5350

5451
expect(this.g.attr('clip-path'))
5552
.toEqual('url(' + href + '#id3)');
@@ -63,10 +60,12 @@ describe('Drawing', function() {
6360
.attr('href', 'https://plot.ly/#hash');
6461

6562
window.location.hash = 'hash';
63+
var href = window.location.href;
64+
var href2 = href.split('#')[0];
6665

67-
Drawing.setClipUrl(this.g, 'id4');
66+
Drawing.setClipUrl(this.g, 'id4', {_context: {baseUrl: href2}});
6867

69-
var expected = 'url(' + window.location.href.split('#')[0] + '#id4)';
68+
var expected = 'url(' + href2 + '#id4)';
7069

7170
expect(this.g.attr('clip-path')).toEqual(expected);
7271

test/jasmine/tests/plot_interact_test.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ var d3 = require('d3');
22

33
var Plotly = require('@lib/index');
44
var Lib = require('@src/lib');
5-
var Drawing = require('@src/components/drawing');
65

76
var createGraphDiv = require('../assets/create_graph_div');
87
var destroyGraphDiv = require('../assets/destroy_graph_div');
@@ -552,7 +551,6 @@ describe('plot svg clip paths', function() {
552551
d3.selectAll('[clip-path]').each(function() {
553552
var cp = d3.select(this).attr('clip-path');
554553

555-
expect(Drawing.baseUrl).toBe('');
556554
expect(cp.substring(0, 5)).toEqual('url(#');
557555
expect(cp.substring(cp.length - 1)).toEqual(')');
558556
});
@@ -578,7 +576,6 @@ describe('plot svg clip paths', function() {
578576
d3.selectAll('[clip-path]').each(function() {
579577
var cp = d3.select(this).attr('clip-path');
580578

581-
expect(Drawing.baseUrl).toBe(href);
582579
expect(cp.substring(0, 5 + href.length)).toEqual('url(' + href + '#');
583580
expect(cp.substring(cp.length - 1)).toEqual(')');
584581
});

test/jasmine/tests/snapshot_test.js

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

54
var d3 = require('d3');
65
var createGraphDiv = require('../assets/create_graph_div');
@@ -370,7 +369,6 @@ describe('Plotly.Snapshot', function() {
370369
});
371370

372371
it('should work on pages with <base>', function(done) {
373-
delete Drawing.baseUrl;
374372
var base = d3.select('body')
375373
.append('base')
376374
.attr('href', 'https://plot.ly');
@@ -396,7 +394,6 @@ describe('Plotly.Snapshot', function() {
396394
.catch(failTest)
397395
.then(function() {
398396
base.remove();
399-
delete Drawing.baseUrl;
400397
done();
401398
});
402399
});

0 commit comments

Comments
 (0)