consolidating scripts
[dygraphs.git] / auto_tests / tests / plugins_legend.js
CommitLineData
e8c70e4e
DV
1import Dygraph from '../../src/dygraph';
2import LegendPlugin from '../../src/plugins/legend';
3import Util from './Util';
4
89fdcedb 5describe("plugins-legend", function() {
8b38c21f 6
e8c70e4e 7var graph;
8b38c21f 8
e8c70e4e
DV
9cleanupAfterEach();
10beforeEach(function() {
11 var testDiv = document.getElementById('graph');
12 testDiv.innerHTML = "<div id='inner-graph'></div><div id='label'></div>";
13 graph = document.getElementById('inner-graph');
89fdcedb 14});
8b38c21f 15
89fdcedb 16it('testLegendEscape', function() {
8b38c21f
A
17 var opts = {
18 width: 480,
19 height: 320
20 };
21 var data = "X,<script>alert('XSS')</script>\n" +
22 "0,-1\n" +
23 "1,0\n" +
24 "2,1\n" +
25 "3,0\n"
26 ;
27
8b38c21f
A
28 var g = new Dygraph(graph, data, opts);
29
e8c70e4e 30 var legendPlugin = new LegendPlugin();
8b38c21f
A
31 legendPlugin.activate(g);
32 var e = {
33 selectedX: 'selectedX',
34 selectedPoints: [{
35 canvasy: 100,
36 name: "<script>alert('XSS')</script>",
37 yval: 10,
38 }],
39 dygraph: g
40 }
41 legendPlugin.select(e);
42
dc910fce
DV
43 var legendSpan = legendPlugin.legend_div_.querySelector("span b span");
44 assert.equal(legendSpan.innerHTML, "&lt;script&gt;alert('XSS')&lt;/script&gt;");
89fdcedb 45});
8b38c21f 46
89fdcedb 47
457deb39
DV
48it('should let labelsDiv be a string', function() {
49 var labelsDiv = document.getElementById('label');
e8c70e4e 50 var g = new Dygraph(graph, 'X,Y\n1,2\n', {labelsDiv: 'label'});
457deb39
DV
51null
52 g.setSelection(0);
53 assert.equal('1: Y: 2', Util.nbspToSpace(labelsDiv.textContent));
54});
55
56it('should let labelsDiv be an Element', function() {
57 var labelsDiv = document.getElementById('label');
e8c70e4e 58 var g = new Dygraph(graph, 'X,Y\n1,2\n', { labelsDiv: labelsDiv });
457deb39
DV
59 assert.isNull(labelsDiv.getAttribute('class')); // dygraph-legend not added.
60 g.setSelection(0);
61 assert.equal('1: Y: 2', Util.nbspToSpace(labelsDiv.textContent));
62});
63
5f01c8b6 64it('should render dashed patterns', function() {
e8c70e4e 65 var g = new Dygraph(graph, 'X,Y\n1,2\n', {
5f01c8b6
DV
66 strokePattern: [5, 5],
67 color: 'red',
68 legend: 'always'
69 });
70
71 // The legend has a dashed line and a label.
72 var legendEl = document.querySelector('.dygraph-legend > span');
73 assert.equal(' Y', legendEl.textContent);
74 var dashEl = document.querySelector('.dygraph-legend > span > div');
75 assert.equal(window.getComputedStyle(dashEl)['border-bottom-color'],
76 'rgb(255, 0, 0)');
77});
457deb39 78
89fdcedb 79});