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
Merged
138 changes: 0 additions & 138 deletions lib/browser/progress.js

This file was deleted.

42 changes: 20 additions & 22 deletions lib/reporters/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

var Base = require('./base');
var utils = require('../utils');
var Progress = require('../browser/progress');
var escapeRe = require('escape-string-regexp');
var constants = require('../runner').constants;
var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
Expand Down Expand Up @@ -38,7 +37,7 @@ exports = module.exports = HTML;

var statsTemplate =
'<ul id="mocha-stats">' +
'<li class="progress"><canvas width="40" height="40"></canvas></li>' +
'<li class="progress-contain"><progress class="progress-element" max="100" value="0"></progress><svg class="progress-ring"><circle class="ring-whole"/><circle class="ring-highlight" stroke-dasharray="0%,100%"/></svg><div class="progress-text">0%</div></li>' +
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
'<li class="passes"><a href="javascript:void(0);">passes:</a> <em>0</em></li>' +
'<li class="failures"><a href="javascript:void(0);">failures:</a> <em>0</em></li>' +
'<li class="duration">duration: <em>0</em>s</li>' +
Expand Down Expand Up @@ -68,24 +67,13 @@ function HTML(runner, options) {
var failures = items[2].getElementsByTagName('em')[0];
var failuresLink = items[2].getElementsByTagName('a')[0];
var duration = items[3].getElementsByTagName('em')[0];
var canvas = stat.getElementsByTagName('canvas')[0];
var report = fragment('<ul id="mocha-report"></ul>');
var stack = [report];
var progress;
var ctx;
var progressText = items[0].getElementsByTagName('div')[0];
var progressBar = items[0].getElementsByTagName('progress')[0];
var progressRing = items[0].getElementsByClassName('ring-highlight')[0];
var root = document.getElementById('mocha');

if (canvas.getContext) {
var ratio = window.devicePixelRatio || 1;
canvas.style.width = canvas.width;
canvas.style.height = canvas.height;
canvas.width *= ratio;
canvas.height *= ratio;
ctx = canvas.getContext('2d');
ctx.scale(ratio, ratio);
progress = new Progress();
}

if (!root) {
return error('#mocha div missing, add it to your document');
}
Expand Down Expand Up @@ -115,10 +103,6 @@ function HTML(runner, options) {
root.appendChild(stat);
root.appendChild(report);

if (progress) {
progress.size(40);
}

runner.on(EVENT_SUITE_BEGIN, function (suite) {
if (suite.root) {
return;
Expand Down Expand Up @@ -234,8 +218,22 @@ function HTML(runner, options) {
function updateStats() {
// TODO: add to stats
var percent = ((stats.tests / runner.total) * 100) | 0;
if (progress) {
progress.update(percent).draw(ctx);
progressBar.value = percent;
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));
yourWaifu marked this conversation as resolved.
Show resolved Hide resolved
text(
progressText,
percent.toFixed(Math.min(Math.max(decmalPlaces, 0), 100)) + '%'
);
}
if (progressRing) {
var radius = 19; // to do, figure out how to match with css
var wholeArc = Math.PI * 2 * radius;
var highlightArc = percent * (wholeArc / 100);
progressRing.style['stroke-dasharray'] = `${highlightArc}px,${wholeArc}px`;
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
}

// update stats
Expand Down
71 changes: 54 additions & 17 deletions mocha.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
--mocha-stats-color: #888;
--mocha-stats-em-color: #000;
--mocha-stats-hover-color: #eee;
--mocha-progress-ring-color: #eee;
--mocha-progress-ring-highlight-color: #9f9f9f;
--mocha-progress-text-color: #000;
--mocha-error-color: #c00;

--mocha-code-comment: #ddd;
Expand Down Expand Up @@ -54,6 +57,9 @@
--mocha-stats-color: #aaa;
--mocha-stats-em-color: #fff;
--mocha-stats-hover-color: #444;
--mocha-progress-ring-color: #444;
--mocha-progress-ring-highlight-color: #888;
--mocha-progress-text-color: #fff;
--mocha-error-color: #f44;

--mocha-code-comment: #ddd;
Expand Down Expand Up @@ -325,6 +331,9 @@ body {
}

#mocha-stats {
--ring-size: 40px;
--ring-radius: calc(var(--ring-size) / 2);

position: fixed;
top: 15px;
right: 10px;
Expand All @@ -334,20 +343,53 @@ body {
z-index: 1;
}

#mocha-stats .progress {
#mocha-stats .progress-contain {
float: right;
padding-top: 0;
padding: 0;
}

#mocha-stats .progress-element {
visibility: hidden;
width: var(--ring-size);
height: calc(var(--ring-size) / 2);
position: absolute;
top: 11px; /* padding */
display: block;
}

#mocha-stats .progress-text {
text-align: center;
position: absolute;
width: var(--ring-size);
display: block;
top: 11px; /* padding */
text-overflow: clip;
overflow: hidden;
color: var(--mocha-stats-em-color);
}
yourWaifu marked this conversation as resolved.
Show resolved Hide resolved

/**
* Set safe initial values, so mochas .progress does not inherit these
* properties from Bootstrap .progress (which causes .progress height to
* equal line height set in Bootstrap).
*/
height: auto;
-webkit-box-shadow: none;
-moz-box-shadow: none;
box-shadow: none;
background-color: initial;
#mocha-stats .progress-ring {
width: var(--ring-size);
height: var(--ring-size);
}

#mocha-stats .ring-whole, #mocha-stats .ring-highlight {
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
--stroke-thickness: 1px;
cx: var(--ring-radius);
cy: var(--ring-radius);
r: calc(var(--ring-radius) - var(--stroke-thickness)); /* radius - stroke */
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
fill: hsla(0, 0%, 0%, 0);
stroke-width: calc(var(--stroke-thickness) * 2);
}

#mocha-stats .ring-whole {
stroke: var(--mocha-progress-ring-color);
stroke-width: calc(var(--stroke-thickness) * 1.8);
/* slightly smaller to fix strange edge issue */
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
}

#mocha-stats .ring-highlight {
stroke: var(--mocha-progress-ring-highlight-color);
}

#mocha-stats em {
Expand All @@ -370,11 +412,6 @@ body {
padding-top: 11px;
}

#mocha-stats canvas {
width: 40px;
height: 40px;
}

#mocha code .comment { color: var(--mocha-code-comment); }
#mocha code .init { color: var(--mocha-code-init); }
#mocha code .string { color: var(--mocha-code-string); }
Expand Down
Loading