From 1876f4e6b2c92f1d8b2faaeb5f331ea5104217d9 Mon Sep 17 00:00:00 2001 From: Sleepy Flower Date: Thu, 12 Oct 2023 17:33:18 -0400 Subject: [PATCH] Fallback for progress when canvas isn't available --- lib/reporters/html.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/reporters/html.js b/lib/reporters/html.js index 034fb07f01..0472e5b733 100644 --- a/lib/reporters/html.js +++ b/lib/reporters/html.js @@ -73,6 +73,7 @@ function HTML(runner, options) { var stack = [report]; var progress; var ctx; + var progressText; var root = document.getElementById('mocha'); if (canvas.getContext) { @@ -84,6 +85,12 @@ function HTML(runner, options) { ctx = canvas.getContext('2d'); ctx.scale(ratio, ratio); progress = new Progress(); + } else { + // On some broswers, canvas might be unavailable for whatever reason. + // As such, we need a text version as a fallback + var progressTextFallback = fragment('0%'); + progressText = stat.getElementsByTagName('em')[0]; + items[0].appendChild(progressTextFallback); } if (!root) { @@ -236,6 +243,12 @@ function HTML(runner, options) { var percent = ((stats.tests / runner.total) * 100) | 0; if (progress) { progress.update(percent).draw(ctx); + } else if (progressText) { + // setting a toFixed that is too low, makes small changes to progress not shown + // setting it too high, makes the progress text longer then it needs to + // to address this, calculate the toFixed based on the magnitude of total + var decmalPlaces = Math.ceil(Math.log10(runner.total/100)); + text(progressText, percent.toFixed(Math.min(Math.max(decmalPlaces), 0), 100)); } // update stats