Skip to content

Commit be87e14

Browse files
committed
harden locationToFeature against non-string country names
1 parent 9bfbabf commit be87e14

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

src/lib/geo_location_utils.js

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ var locationmodeToIdFinder = {
2323
};
2424

2525
exports.locationToFeature = function(locationmode, location, features) {
26+
if(!location || typeof location !== 'string') return false;
27+
2628
var locationId = getLocationId(locationmode, location);
2729

2830
if(locationId) {

test/jasmine/tests/choropleth_test.js

+26
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,29 @@ describe('Test choropleth hover:', function() {
160160
.then(done);
161161
});
162162
});
163+
164+
describe('choropleth bad data', function() {
165+
var gd;
166+
167+
beforeEach(function() {
168+
gd = createGraphDiv();
169+
});
170+
171+
afterEach(destroyGraphDiv);
172+
173+
it('should not throw an error with bad locations', function(done) {
174+
spyOn(Lib, 'warn');
175+
Plotly.newPlot(gd, [{
176+
locations: ['canada', 0, null, '', 'utopia'],
177+
z: [1, 2, 3, 4, 5],
178+
locationmode: 'country names',
179+
type: 'choropleth'
180+
}])
181+
.then(function() {
182+
// only utopia warns - others are silently ignored
183+
expect(Lib.warn).toHaveBeenCalledTimes(1);
184+
})
185+
.catch(fail)
186+
.then(done);
187+
});
188+
});

test/jasmine/tests/scattergeo_test.js

+33
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var mouseEvent = require('../assets/mouse_event');
1313
var customAssertions = require('../assets/custom_assertions');
1414
var assertHoverLabelStyle = customAssertions.assertHoverLabelStyle;
1515
var assertHoverLabelContent = customAssertions.assertHoverLabelContent;
16+
var fail = require('../assets/fail_test');
1617

1718
describe('Test scattergeo defaults', function() {
1819
var traceIn,
@@ -246,6 +247,7 @@ describe('Test scattergeo hover', function() {
246247
lat: [10, 20, 30],
247248
text: ['A', 'B', 'C']
248249
}])
250+
.catch(fail)
249251
.then(done);
250252
});
251253

@@ -280,27 +282,31 @@ describe('Test scattergeo hover', function() {
280282
Plotly.restyle(gd, 'hoverinfo', 'lon+lat+text+name').then(function() {
281283
check([381, 221], ['(10°, 10°)\nA', 'trace 0']);
282284
})
285+
.catch(fail)
283286
.then(done);
284287
});
285288

286289
it('should generate hover label info (\'text\' single value case)', function(done) {
287290
Plotly.restyle(gd, 'text', 'text').then(function() {
288291
check([381, 221], ['(10°, 10°)\ntext', null]);
289292
})
293+
.catch(fail)
290294
.then(done);
291295
});
292296

293297
it('should generate hover label info (\'hovertext\' single value case)', function(done) {
294298
Plotly.restyle(gd, 'hovertext', 'hovertext').then(function() {
295299
check([381, 221], ['(10°, 10°)\nhovertext', null]);
296300
})
301+
.catch(fail)
297302
.then(done);
298303
});
299304

300305
it('should generate hover label info (\'hovertext\' array case)', function(done) {
301306
Plotly.restyle(gd, 'hovertext', ['Apple', 'Banana', 'Orange']).then(function() {
302307
check([381, 221], ['(10°, 10°)\nApple', null]);
303308
})
309+
.catch(fail)
304310
.then(done);
305311
});
306312

@@ -318,13 +324,40 @@ describe('Test scattergeo hover', function() {
318324
fontFamily: 'Arial'
319325
});
320326
})
327+
.catch(fail)
321328
.then(done);
322329
});
323330

324331
it('should generate hover label with arrayOk \'hoverinfo\' settings', function(done) {
325332
Plotly.restyle(gd, 'hoverinfo', [['lon', null, 'lat+name']]).then(function() {
326333
check([381, 221], ['lon: 10°', null]);
327334
})
335+
.catch(fail)
336+
.then(done);
337+
});
338+
});
339+
340+
describe('scattergeo bad data', function() {
341+
var gd;
342+
343+
beforeEach(function() {
344+
gd = createGraphDiv();
345+
});
346+
347+
afterEach(destroyGraphDiv);
348+
349+
it('should not throw an error with bad locations', function(done) {
350+
spyOn(Lib, 'warn');
351+
Plotly.newPlot(gd, [{
352+
locations: ['canada', 0, null, '', 'utopia'],
353+
locationmode: 'country names',
354+
type: 'scattergeo'
355+
}])
356+
.then(function() {
357+
// only utopia warns - others are silently ignored
358+
expect(Lib.warn).toHaveBeenCalledTimes(1);
359+
})
360+
.catch(fail)
328361
.then(done);
329362
});
330363
});

0 commit comments

Comments
 (0)