Skip to content

Commit

Permalink
Merge pull request #3039 from ianks/getLabelMoment-fix
Browse files Browse the repository at this point in the history
Fix out of bounds index access in getLabelMoment
  • Loading branch information
etimberg authored Jul 27, 2016
2 parents a0d8554 + e4dd158 commit 8e2deed
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/scales/scale.time.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ module.exports = function(Chart) {
Chart.Scale.prototype.initialize.call(this);
},
getLabelMoment: function(datasetIndex, index) {
return this.labelMoments[datasetIndex][index];
if (typeof this.labelMoments[datasetIndex] != 'undefined') {
return this.labelMoments[datasetIndex][index];
}

return null;
},
getMomentStartOf: function(tick) {
var me = this;
Expand Down
29 changes: 29 additions & 0 deletions test/scale.time.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,4 +479,33 @@ describe('Time scale tests', function() {
threshold: 0.75
});
});

it("should not throw an error if the datasetIndex is out of bounds", function() {
var chart = window.acquireChart({
type: 'line',
data: {
labels: ["2016-06-26"],
datasets: [{
type: "line",
data: [5]
}]
},
options: {
scales: {
xAxes: [{
display: true,
type: "time",
}]
}
}
});

var xScale = chartInstance.scales.xScale0;

var getOutOfBoundPixelForValue = function() {
xScale.getLabelMoment(12, 0);
};

expect(getOutOfBoundPixelForValue).not.toThrow();
});
});

0 comments on commit 8e2deed

Please sign in to comment.