Skip to content

Commit e48bc1f

Browse files
committed
test click events in shadow dom
1 parent fe62eb8 commit e48bc1f

File tree

4 files changed

+88
-4
lines changed

4 files changed

+88
-4
lines changed

Diff for: test/jasmine/assets/create_shadow_graph_div.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
module.exports = function createShadowGraphDiv() {
4+
var container = document.createElement('div');
5+
container.id = 'shadowcontainer';
6+
document.body.appendChild(container);
7+
var root = container.attachShadow({mode: 'open'});
8+
var gd = document.createElement('div');
9+
gd.id = 'graph2';
10+
root.appendChild(gd);
11+
12+
// force the shadow container to be at position 0,0 no matter what
13+
container.style.position = 'fixed';
14+
container.style.left = 0;
15+
container.style.top = 0;
16+
17+
var style = document.createElement('style');
18+
root.appendChild(style);
19+
20+
for (var plotlyStyle of document.querySelectorAll('[id^="plotly.js-"]')) {
21+
for (var rule of plotlyStyle.sheet.rules) {
22+
style.sheet.insertRule(rule.cssText);
23+
}
24+
}
25+
return gd;
26+
};

Diff for: test/jasmine/assets/destroy_graph_div.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
'use strict';
22

33
module.exports = function destroyGraphDiv() {
4-
var gd = document.getElementById('graph');
5-
6-
if(gd) document.body.removeChild(gd);
4+
// remove both plain graphs and shadow DOM graph containers
5+
['graph', 'shadowcontainer'].forEach(function(id) {
6+
var el = document.getElementById(id);
7+
if(el) document.body.removeChild(el);
8+
});
79
};

Diff for: test/jasmine/assets/mouse_event.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ module.exports = function(type, x, y, opts) {
3333
fullOpts.shiftKey = opts.shiftKey;
3434
}
3535

36-
var el = (opts && opts.element) || document.elementFromPoint(x, y);
36+
var shadowContainer = document.getElementById('shadowcontainer');
37+
var elementRoot = (opts && opts.elementRoot) || (
38+
shadowContainer ? shadowContainer.shadowRoot : document
39+
);
40+
41+
var el = (opts && opts.element) || elementRoot.elementFromPoint(x, y);
3742
var ev;
3843

3944
if(type === 'scroll' || type === 'wheel') {

Diff for: test/jasmine/tests/click_test.js

+51
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var DBLCLICKDELAY = require('../../../src/plot_api/plot_config').dfltConfig.doub
77
var d3Select = require('../../strict-d3').select;
88
var d3SelectAll = require('../../strict-d3').selectAll;
99
var createGraphDiv = require('../assets/create_graph_div');
10+
var createShadowGraphDiv = require('../assets/create_shadow_graph_div');
1011
var destroyGraphDiv = require('../assets/destroy_graph_div');
1112

1213
var mouseEvent = require('../assets/mouse_event');
@@ -1164,6 +1165,56 @@ describe('Test click interactions:', function() {
11641165
});
11651166
});
11661167

1168+
describe('Click events in Shadow DOM', function() {
1169+
afterEach(destroyGraphDiv);
1170+
1171+
function fig() {
1172+
var x = [];
1173+
var y = [];
1174+
for (var i = 0; i <= 20; i++) {
1175+
for (var j = 0; j <= 20; j++) {
1176+
x.push(i);
1177+
y.push(j);
1178+
}
1179+
}
1180+
return {
1181+
data: [{x: x, y: y, mode: 'markers'}],
1182+
layout: {
1183+
width: 400,
1184+
height: 400,
1185+
margin: {l: 100, r: 100, t: 100, b: 100},
1186+
xaxis: {range: [0, 20]},
1187+
yaxis: {range: [0, 20]},
1188+
}
1189+
};
1190+
}
1191+
1192+
it('should select the same point in regular and shadow DOM', function(done) {
1193+
var clickData;
1194+
var clickX = 120;
1195+
var clickY = 150;
1196+
var expectedX = 2; // counts up 1 every 10px from 0 at 100px
1197+
var expectedY = 15; // counts down 1 every 10px from 20 at 100px
1198+
1199+
function check(gd) {
1200+
gd.on('plotly_click', function(event) { clickData = event; });
1201+
click(clickX, clickY);
1202+
expect(clickData.points.length).toBe(1);
1203+
var pt = clickData.points[0];
1204+
expect(pt.x).toBe(expectedX);
1205+
expect(pt.y).toBe(expectedY);
1206+
clickData = null;
1207+
}
1208+
1209+
Plotly.newPlot(createGraphDiv(), fig())
1210+
.then(check)
1211+
.then(destroyGraphDiv)
1212+
.then(function() { return Plotly.newPlot(createShadowGraphDiv(), fig()) })
1213+
.then(check)
1214+
.then(done, done.fail);
1215+
});
1216+
});
1217+
11671218

11681219
describe('dragbox', function() {
11691220
afterEach(destroyGraphDiv);

0 commit comments

Comments
 (0)