Skip to content

Commit 9de2f77

Browse files
committed
make annotation/shapes cleanLayout blocks more robust
- so that we *only* loop over arrays.
1 parent 8ed1957 commit 9de2f77

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/plot_api/helpers.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ exports.cleanLayout = function(layout) {
102102
}
103103
}
104104

105-
var annotationsLen = (layout.annotations || []).length;
105+
var annotationsLen = Array.isArray(layout.annotations) ? layout.annotations.length : 0;
106106
for(i = 0; i < annotationsLen; i++) {
107107
var ann = layout.annotations[i];
108108

@@ -124,7 +124,7 @@ exports.cleanLayout = function(layout) {
124124
cleanAxRef(ann, 'yref');
125125
}
126126

127-
var shapesLen = (layout.shapes || []).length;
127+
var shapesLen = Array.isArray(layout.shapes) ? layout.shapes.length : 0;
128128
for(i = 0; i < shapesLen; i++) {
129129
var shape = layout.shapes[i];
130130

test/jasmine/tests/plot_api_test.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ describe('Test plot api', function() {
896896
});
897897
});
898898

899-
describe('cleanData', function() {
899+
describe('cleanData & cleanLayout', function() {
900900
var gd;
901901

902902
beforeEach(function() {
@@ -1039,6 +1039,36 @@ describe('Test plot api', function() {
10391039
expect(trace1.transforms.length).toEqual(1);
10401040
expect(trace1.transforms[0].target).toEqual('y');
10411041
});
1042+
1043+
it('should cleanup annotations / shapes refs', function() {
1044+
var data = [{}];
1045+
1046+
var layout = {
1047+
annotations: [
1048+
{ ref: 'paper' },
1049+
null,
1050+
{ xref: 'x02', yref: 'y1' }
1051+
],
1052+
shapes: [
1053+
{ xref: 'y', yref: 'x' },
1054+
null,
1055+
{ xref: 'x03', yref: 'y1' }
1056+
]
1057+
};
1058+
1059+
Plotly.plot(gd, data, layout);
1060+
1061+
expect(gd.layout.annotations[0]).toEqual({ xref: 'paper', yref: 'paper' });
1062+
expect(gd.layout.annotations[1]).toEqual(null);
1063+
expect(gd.layout.annotations[2]).toEqual({ xref: 'x2', yref: 'y' });
1064+
1065+
expect(gd.layout.shapes[0].xref).toBeUndefined();
1066+
expect(gd.layout.shapes[0].yref).toBeUndefined();
1067+
expect(gd.layout.shapes[1]).toEqual(null);
1068+
expect(gd.layout.shapes[2].xref).toEqual('x3');
1069+
expect(gd.layout.shapes[2].yref).toEqual('y');
1070+
1071+
});
10421072
});
10431073

10441074
describe('Plotly.newPlot', function() {

0 commit comments

Comments
 (0)