diff --git a/src/plot_api/helpers.js b/src/plot_api/helpers.js index 6a4208e1cde..7dbe5145365 100644 --- a/src/plot_api/helpers.js +++ b/src/plot_api/helpers.js @@ -490,6 +490,17 @@ exports.coerceTraceIndices = function(gd, traceIndices) { else if(!Array.isArray(traceIndices) || !traceIndices.length) { return gd.data.map(function(_, i) { return i; }); } + else if(Array.isArray(traceIndices)) { + var traceIndicesOut = []; + for(var i = 0; i < traceIndices.length; i++) { + if(Lib.isIndex(traceIndices[i], gd.data.length)) { + traceIndicesOut.push(traceIndices[i]); + } else { + Lib.warn('trace index (', traceIndices[i], ') is not a number or is out of bounds'); + } + } + return traceIndicesOut; + } return traceIndices; }; diff --git a/test/jasmine/tests/plot_api_test.js b/test/jasmine/tests/plot_api_test.js index 53af9b349d5..e5ab15363ed 100644 --- a/test/jasmine/tests/plot_api_test.js +++ b/test/jasmine/tests/plot_api_test.js @@ -921,6 +921,18 @@ describe('Test plot api', function() { expect(gd._fullData[0].marker.color).toBe('blue'); }); + it('ignores invalid trace indices', function() { + var gd = { + data: [{x: [1, 2, 3], y: [1, 2, 3], type: 'scatter'}], + layout: {} + }; + + mockDefaultsAndCalc(gd); + + // Call restyle on an invalid trace indice + Plotly.restyle(gd, {'type': 'scatter', 'marker.color': 'red'}, [1]); + }); + it('restores null values to defaults', function() { var gd = { data: [{x: [1, 2, 3], y: [1, 2, 3], type: 'scatter'}], @@ -2580,6 +2592,11 @@ describe('Test plot api', function() { .catch(failTest) .then(done); }); + + it('ignores invalid trace indices', function() { + // Call update on an invalid trace indice + Plotly.update(gd, {'type': 'scatter', 'marker.color': 'red'}, {}, [1]); + }); }); describe('@noCIdep Plotly.react', function() {