Move the bannerStateForResults helper into the SignaturePropertiesManager class (PR 21247 follow-up)

This has a couple of advantages:
 - It allows accessing and iterating `this.#results` directly, without having to (needlessly) create a temporary array.

 - By moving the "worst status" computation into its own getter, it can be re-used from the `#updateButtonState` method as well which reduces code duplication.
This commit is contained in:
Jonas Jenwald 2026-07-03 11:49:23 +02:00
parent 0cc1718b02
commit 14f4cce29d

View File

@ -80,30 +80,6 @@ const CERT_L10N_IDS = {
const CERT_EXPIRED_WITH_DATE_L10N_ID = const CERT_EXPIRED_WITH_DATE_L10N_ID =
"pdfjs-digital-signature-properties-certificate-expired-with-date"; "pdfjs-digital-signature-properties-certificate-expired-with-date";
function bannerStateForResults(results) {
if (results.length === 0) {
return { worst: "unknown", severity: "error", count: 0 };
}
let worst = "verified";
for (const r of results) {
if (
r?.status &&
STATUS_INFO[r.status].priority > STATUS_INFO[worst].priority
) {
worst = r.status;
}
}
// Count how many signatures are at the worst level — this drives the
// singular/plural variant of the banner message.
let count = 0;
for (const r of results) {
if (r?.status === worst) {
count++;
}
}
return { worst, severity: STATUS_INFO[worst].severity, count };
}
// For an `untrusted` certificate, pick the most specific Fluent label. // For an `untrusted` certificate, pick the most specific Fluent label.
// When the error code matches one of the recognised cases we have a // When the error code matches one of the recognised cases we have a
// structured "Certificate: <reason> (<issuer>)" string; otherwise we // structured "Certificate: <reason> (<issuer>)" string; otherwise we
@ -365,6 +341,35 @@ class SignaturePropertiesManager {
); );
} }
get #worst() {
let worst = "verified";
for (const r of this.#results.values()) {
if (
r?.status &&
STATUS_INFO[r.status].priority > STATUS_INFO[worst].priority
) {
worst = r.status;
}
}
return worst;
}
get #bannerState() {
if (this.#results.size === 0) {
return { worst: "unknown", severity: "error", count: 0 };
}
const worst = this.#worst;
let count = 0;
// Count how many signatures are at the worst level — this drives the
// singular/plural variant of the banner message.
for (const r of this.#results.values()) {
if (r?.status === worst) {
count++;
}
}
return { worst, severity: STATUS_INFO[worst].severity, count };
}
#render() { #render() {
if (!this.#isOpen) { if (!this.#isOpen) {
// Defer DOM work until the user actually opens the panel. // Defer DOM work until the user actually opens the panel.
@ -387,9 +392,7 @@ class SignaturePropertiesManager {
} }
// Banner. // Banner.
const { worst, severity, count } = bannerStateForResults([ const { worst, severity, count } = this.#bannerState;
...this.#results.values(),
]);
banner.replaceChildren(); banner.replaceChildren();
banner.hidden = false; banner.hidden = false;
banner.className = `sigBanner ${severity}`; banner.className = `sigBanner ${severity}`;
@ -672,16 +675,7 @@ class SignaturePropertiesManager {
button.classList.add("state-loading"); button.classList.add("state-loading");
return; return;
} }
let worst = "verified"; switch (this.#worst) {
for (const r of this.#results.values()) {
if (!r) {
continue;
}
if (STATUS_INFO[r.status].priority > STATUS_INFO[worst].priority) {
worst = r.status;
}
}
switch (worst) {
case "invalid": case "invalid":
case "revoked": case "revoked":
case "unknown": case "unknown":