From bd60fa2dfd25bae95520e7968a72eecac218f7ba Mon Sep 17 00:00:00 2001 From: Simon Brunel Date: Sat, 18 Mar 2017 11:54:56 +0100 Subject: [PATCH] Correctly handle decimal display size (#4009) Fix the `readUsedSize` regular expression to correctly parse (truncate) pixel decimal values. --- src/platforms/platform.dom.js | 2 +- test/specs/platform.dom.tests.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/platforms/platform.dom.js b/src/platforms/platform.dom.js index 761337b061c..317d4bfed73 100644 --- a/src/platforms/platform.dom.js +++ b/src/platforms/platform.dom.js @@ -33,7 +33,7 @@ module.exports = function(Chart) { */ function readUsedSize(element, property) { var value = helpers.getStyle(element, property); - var matches = value && value.match(/(\d+)px/); + var matches = value && value.match(/^(\d+)(\.\d+)?px$/); return matches? Number(matches[1]) : undefined; } diff --git a/test/specs/platform.dom.tests.js b/test/specs/platform.dom.tests.js index 06562c9191f..898de9c4842 100644 --- a/test/specs/platform.dom.tests.js +++ b/test/specs/platform.dom.tests.js @@ -226,6 +226,24 @@ describe('Platform.dom', function() { rw: 165, rh: 85, }); }); + + // https://github.com/chartjs/Chart.js/issues/3860 + it('should support decimal display width and/or height', function() { + var chart = acquireChart({ + options: { + responsive: false + } + }, { + canvas: { + style: 'width: 345.42px; height: 125.42px;' + } + }); + + expect(chart).toBeChartOfSize({ + dw: 345, dh: 125, + rw: 345, rh: 125, + }); + }); }); describe('config.options.responsive: true (maintainAspectRatio: true)', function() {