Tests for labelsDiv option
[dygraphs.git] / auto_tests / tests / plugins_legend.js
CommitLineData
89fdcedb 1describe("plugins-legend", function() {
8b38c21f 2
89fdcedb 3beforeEach(function() {
457deb39 4 document.body.innerHTML = "<div id='graph'></div><div id='label'></div>";
89fdcedb 5});
8b38c21f 6
89fdcedb
DV
7afterEach(function() {
8});
8b38c21f 9
89fdcedb 10it('testLegendEscape', function() {
8b38c21f
A
11 var opts = {
12 width: 480,
13 height: 320
14 };
15 var data = "X,<script>alert('XSS')</script>\n" +
16 "0,-1\n" +
17 "1,0\n" +
18 "2,1\n" +
19 "3,0\n"
20 ;
21
22 var graph = document.getElementById("graph");
23 var g = new Dygraph(graph, data, opts);
24
25 var legendPlugin = new Dygraph.Plugins.Legend();
26 legendPlugin.activate(g);
27 var e = {
28 selectedX: 'selectedX',
29 selectedPoints: [{
30 canvasy: 100,
31 name: "<script>alert('XSS')</script>",
32 yval: 10,
33 }],
34 dygraph: g
35 }
36 legendPlugin.select(e);
37
dc910fce
DV
38 var legendSpan = legendPlugin.legend_div_.querySelector("span b span");
39 assert.equal(legendSpan.innerHTML, "&lt;script&gt;alert('XSS')&lt;/script&gt;");
89fdcedb 40});
8b38c21f 41
89fdcedb 42
457deb39
DV
43it('should let labelsDiv be a string', function() {
44 var labelsDiv = document.getElementById('label');
45 var g = new Dygraph('graph', 'X,Y\n1,2\n', {labelsDiv: 'label'});
46null
47 g.setSelection(0);
48 assert.equal('1: Y: 2', Util.nbspToSpace(labelsDiv.textContent));
49});
50
51it('should let labelsDiv be an Element', function() {
52 var labelsDiv = document.getElementById('label');
53 var g = new Dygraph('graph', 'X,Y\n1,2\n', { labelsDiv: labelsDiv });
54 assert.isNull(labelsDiv.getAttribute('class')); // dygraph-legend not added.
55 g.setSelection(0);
56 assert.equal('1: Y: 2', Util.nbspToSpace(labelsDiv.textContent));
57});
58
59
89fdcedb 60});