Skip to content

Commit 5887104

Browse files
committed
stash base url on Drawing module object
... so that we don't have to traverse the DOM whenever we add a clipPath url 🐎
1 parent fc8c386 commit 5887104

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

src/components/drawing/index.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -910,15 +910,21 @@ drawing.setClipUrl = function(s, localId) {
910910
return;
911911
}
912912

913-
var url = '#' + localId,
914-
base = d3.select('base');
915-
916-
// add id to location href w/o hashes if any)
917-
if(base.size() && base.attr('href')) {
918-
url = window.location.href.split('#')[0] + url;
913+
if(drawing.baseUrl === undefined) {
914+
var base = d3.select('base');
915+
916+
// Stash base url once and for all!
917+
// We may have to stash this elsewhere when
918+
// we'll try to support for child windows
919+
// more info -> https://github.com/plotly/plotly.js/issues/702
920+
if(base.size() && base.attr('href')) {
921+
drawing.baseUrl = window.location.href.split('#')[0];
922+
} else {
923+
drawing.baseUrl = '';
924+
}
919925
}
920926

921-
s.attr('clip-path', 'url(' + url + ')');
927+
s.attr('clip-path', 'url(' + drawing.baseUrl + '#' + localId + ')');
922928
};
923929

924930
drawing.getTranslate = function(element) {

test/jasmine/tests/drawing_test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ 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;
2225
});
2326

2427
it('should set the clip-path attribute', function() {
@@ -38,7 +41,6 @@ describe('Drawing', function() {
3841
});
3942

4043
it('should append window URL to clip-path if <base> is present', function() {
41-
4244
// append <base> with href
4345
var base = d3.select('body')
4446
.append('base')

test/jasmine/tests/plot_interact_test.js

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var d3 = require('d3');
22

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

67
var createGraphDiv = require('../assets/create_graph_div');
78
var destroyGraphDiv = require('../assets/destroy_graph_div');
@@ -551,6 +552,7 @@ describe('plot svg clip paths', function() {
551552
d3.selectAll('[clip-path]').each(function() {
552553
var cp = d3.select(this).attr('clip-path');
553554

555+
expect(Drawing.baseUrl).toBe('');
554556
expect(cp.substring(0, 5)).toEqual('url(#');
555557
expect(cp.substring(cp.length - 1)).toEqual(')');
556558
});
@@ -560,6 +562,8 @@ describe('plot svg clip paths', function() {
560562
});
561563

562564
it('should set clip path url to ids appended to window url', function(done) {
565+
// unstash base url from Drawing module object
566+
delete Drawing.baseUrl;
563567

564568
// this case occurs in some past versions of AngularJS
565569
// https://github.com/angular/angular.js/issues/8934
@@ -577,11 +581,13 @@ describe('plot svg clip paths', function() {
577581
d3.selectAll('[clip-path]').each(function() {
578582
var cp = d3.select(this).attr('clip-path');
579583

584+
expect(Drawing.baseUrl).toBe(href);
580585
expect(cp.substring(0, 5 + href.length)).toEqual('url(' + href + '#');
581586
expect(cp.substring(cp.length - 1)).toEqual(')');
582587
});
583588

584589
base.remove();
590+
delete Drawing.baseUrl;
585591
})
586592
.catch(failTest)
587593
.then(done);

0 commit comments

Comments
 (0)