Skip to content

Commit 063b7dc

Browse files
[2.9] FitBoxes recursion when dimensions are NaN (#7853)
* Infinite recursion when dimensions are NaN Adding a verification on updateDims that handles a case when dimensions are both NaN. This caused an infinite recursion on fitBoxes when calculating the layout for a chart that is mounted on an element that is not yet in DOM. Fixes #7761
1 parent 2493cb5 commit 063b7dc

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/core/core.layouts.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ function updateDims(chartArea, params, layout) {
9999
chartArea.h = newHeight;
100100

101101
// return true if chart area changed in layout's direction
102-
return layout.horizontal ? newWidth !== chartArea.w : newHeight !== chartArea.h;
102+
var sizes = layout.horizontal ? [newWidth, chartArea.w] : [newHeight, chartArea.h];
103+
return sizes[0] !== sizes[1] && (!isNaN(sizes[0]) || !isNaN(sizes[1]));
103104
}
104105
}
105106

test/specs/core.layouts.tests.js

+40
Original file line numberDiff line numberDiff line change
@@ -653,5 +653,45 @@ describe('Chart.layouts', function() {
653653
expect(yAxis.width).toBeCloseToPixel(33);
654654
expect(yAxis.ticks).toEqual(['2.5', '2.0', '1.5', '1.0', '0.5', '0']);
655655
});
656+
657+
it('should correctly handle NaN dimensions', function() {
658+
659+
// issue #7761: Maximum call stack size exceeded
660+
var chartContainer = document.createElement('div');
661+
chartContainer.style.width = '600px';
662+
chartContainer.style.height = '400px';
663+
664+
var chartCanvas = document.createElement('canvas');
665+
chartContainer.appendChild(chartCanvas);
666+
667+
var chart = new Chart(chartCanvas, {
668+
type: 'line',
669+
responsive: true,
670+
data: {
671+
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
672+
datasets: [{
673+
label: '# of Votes',
674+
data: [12, 19, 3, 5, 2, 3]
675+
}]
676+
},
677+
options: {
678+
scales: {
679+
yAxes: [{
680+
type: 'linear',
681+
label: 'first axis',
682+
position: 'right'
683+
}, {
684+
type: 'linear',
685+
label: 'second axis',
686+
position: 'right'
687+
}]
688+
}
689+
}
690+
});
691+
692+
expect(chart.width).toBeNaN();
693+
expect(chart.height).toBeNaN();
694+
695+
});
656696
});
657697
});

0 commit comments

Comments
 (0)