Skip to content

Commit

Permalink
stash base url on Drawing module object
Browse files Browse the repository at this point in the history
... so that we don't have to traverse the DOM whenever
    we add a clipPath url 🐎
  • Loading branch information
etpinard committed Mar 15, 2018
1 parent fc8c386 commit e22aac5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/components/drawing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -910,15 +910,21 @@ drawing.setClipUrl = function(s, localId) {
return;
}

var url = '#' + localId,
base = d3.select('base');

// add id to location href w/o hashes if any)
if(base.size() && base.attr('href')) {
url = window.location.href.split('#')[0] + url;
if(drawing.baseUrl === undefined) {
var base = d3.select('base');

// Stash base url once and for all!
// We may have to stash this elsewhere when
// we'll try to support for child windows
// more info -> https://github.com/plotly/plotly.js/issues/702
if(base.size() && base.attr('href')) {
drawing.baseUrl = window.location.href.split('#')[0];
} else {
drawing.baseUrl = '';
}
}

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

drawing.getTranslate = function(element) {
Expand Down
3 changes: 3 additions & 0 deletions test/jasmine/tests/drawing_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ describe('Drawing', function() {
beforeEach(function() {
this.svg = d3.select('body').append('svg');
this.g = this.svg.append('g');

// unstash base url from Drawing module object
delete Drawing.baseUrl;
});

afterEach(function() {
Expand Down
6 changes: 6 additions & 0 deletions test/jasmine/tests/plot_interact_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var d3 = require('d3');

var Plotly = require('@lib/index');
var Lib = require('@src/lib');
var Drawing = require('@src/components/drawing');

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

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

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

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

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

base.remove();
delete Drawing.baseUrl;
})
.catch(failTest)
.then(done);
Expand Down

0 comments on commit e22aac5

Please sign in to comment.