Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use <progress> and <svg> for browser progress indicator instead of <canvas> #5015

Merged
merged 12 commits into from
Mar 27, 2024
13 changes: 13 additions & 0 deletions lib/reporters/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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('<li class="progress-text">progress: <em>0</em>%</li>');
progressText = progressTextFallback.getElementsByTagName('em')[0];
items[0].replaceWith(progressTextFallback);
yourWaifu marked this conversation as resolved.
Show resolved Hide resolved
}

if (!root) {
Expand Down Expand Up @@ -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
Expand Down
Loading