From 78ab4c6d5947246a45777752fac537b26f2fe866 Mon Sep 17 00:00:00 2001 From: Antoine Roy-Gobeil Date: Tue, 16 Oct 2018 15:15:57 -0400 Subject: [PATCH 1/2] ignore invalid trace indices in restyle and update --- src/plot_api/helpers.js | 3 +++ test/jasmine/tests/plot_api_test.js | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/plot_api/helpers.js b/src/plot_api/helpers.js index 6a4208e1cde..f55e5f0f116 100644 --- a/src/plot_api/helpers.js +++ b/src/plot_api/helpers.js @@ -490,6 +490,9 @@ 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)) { + return traceIndices.filter(function(i) {return i < gd.data.length;}); + } return traceIndices; }; diff --git a/test/jasmine/tests/plot_api_test.js b/test/jasmine/tests/plot_api_test.js index 53af9b349d5..5d6bef24ab8 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': 'scatter'}, [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': 'scatter'}, {}, [1]); + }); }); describe('@noCIdep Plotly.react', function() { From bcbb898f75037c6e4c5884ded3ae349579479cce Mon Sep 17 00:00:00 2001 From: Antoine Roy-Gobeil Date: Tue, 16 Oct 2018 15:56:51 -0400 Subject: [PATCH 2/2] warn user if trace index is invalid or out of bounds also fix attribute in test --- src/plot_api/helpers.js | 10 +++++++++- test/jasmine/tests/plot_api_test.js | 4 ++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/plot_api/helpers.js b/src/plot_api/helpers.js index f55e5f0f116..7dbe5145365 100644 --- a/src/plot_api/helpers.js +++ b/src/plot_api/helpers.js @@ -491,7 +491,15 @@ exports.coerceTraceIndices = function(gd, traceIndices) { return gd.data.map(function(_, i) { return i; }); } else if(Array.isArray(traceIndices)) { - return traceIndices.filter(function(i) {return i < gd.data.length;}); + 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 5d6bef24ab8..e5ab15363ed 100644 --- a/test/jasmine/tests/plot_api_test.js +++ b/test/jasmine/tests/plot_api_test.js @@ -930,7 +930,7 @@ describe('Test plot api', function() { mockDefaultsAndCalc(gd); // Call restyle on an invalid trace indice - Plotly.restyle(gd, {'type': 'scatter', 'marker.color': 'scatter'}, [1]); + Plotly.restyle(gd, {'type': 'scatter', 'marker.color': 'red'}, [1]); }); it('restores null values to defaults', function() { @@ -2595,7 +2595,7 @@ describe('Test plot api', function() { it('ignores invalid trace indices', function() { // Call update on an invalid trace indice - Plotly.update(gd, {'type': 'scatter', 'marker.color': 'scatter'}, {}, [1]); + Plotly.update(gd, {'type': 'scatter', 'marker.color': 'red'}, {}, [1]); }); });