Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

63.7 KB
Raw Blame
/**
 * UI utility functions
 */

// Configuration constants
const UI_CONFIG = {
    NOTIFICATION_DURATION_MS: 6000,
    NOTIFICATION_Z_INDEX: 9999999,
    ALERT_DURATION_MS: 5000
};

// Note: sanitizer functions are available via Vite bundle or we use safe fallbacks

/**
 * Safe HTML setter.
 *
 * Fails CLOSED when a supported DOMPurify or window.sanitizeHtml is
 * missing: the value is rendered as text rather than assigned raw, the
 * same textContent fallback its near-namesake safeSetInnerHTML
 * (security/xss-protection.js) uses.
 *
 * Sanitizes through window.sanitizeHtml (security/xss-protection.js): the
 * sanitizeStable() reparse-fixpoint loop under the restrictive
 * SANITIZE_CONFIG allowlist that safeSetInnerHTML also uses, plus this
 * function's SAFE_FOR_TEMPLATES override described below. Other sanitize
 * sites differ: renderMarkdown() also runs sanitizeStable(), but with its
 * own MARKDOWN_SANITIZE_CONFIG and GENERATED_IMAGE_SANITIZE_CONFIG
 * allowlists, and news.js safeRenderHTML() runs DOMPurify once, with its
 * default allowlist, and appends the DOM fragment it returns. A single
 * DOMPurify.sanitize(html) call with no config, as this used to do, runs
 * under DOMPurify's permissive default profile -- which allows SVG/MathML
 * foreign-content parsing no caller of this function needs -- and inspects
 * only one parse, not the reparsed tree the browser actually builds from the
 * serialized result. window.sanitizeHtml is populated by
 * security/xss-protection.js, a sibling <script defer>; by the time any
 * caller invokes safeSetHTML the page has finished loading and both scripts
 * have executed.
 *
 * Passes { SAFE_FOR_TEMPLATES: false } to override SANITIZE_CONFIG's
 * default of true. SAFE_FOR_TEMPLATES makes DOMPurify blank out any text
 * node matching mustache (}}), template-literal (${...}), or ERB (%>)
 * syntax -- a guard against re-interpolating sanitizer
 * output through a client-side template engine. Nothing rendered via this
 * function is a client-side template; every caller here is plain UI markup
 * (spinners, error banners, alerts) that regularly wraps server error text
 * (error.message, data.error, etc.), and nested-JSON error text ending in
 * `}}`, a shell-style `${VAR}` example, or anything with `%>` in it is
 * ordinary content, not a template expression. With the default left on,
 * that content was silently blanked instead of shown.
 */
function safeSetHTML(element, html) {
    if (!element) return;
    if (typeof DOMPurify !== 'undefined' && DOMPurify?.isSupported === true &&
        typeof window.sanitizeHtml === 'function') {
        // no-unsanitized needs no suppression here: window.sanitizeHtml is
        // an allowlisted escape method in eslint.config.js.
        // This assignment is the sanitization boundary safeSetHTML exists to
        // provide: html passes through window.sanitizeHtml -- the
        // sanitizeStable() reparse-fixpoint under the SANITIZE_CONFIG
        // allowlist documented above -- and the no-sanitizer branch fails
        // closed to textContent. Bearer's sanitizer detection only matches
        // third-party library calls, not this wrapper, so the intentional
        // sanitization sink is suppressed inline.
        // bearer:disable javascript_lang_dangerous_insert_html
        element.innerHTML = window.sanitizeHtml(html, { SAFE_FOR_TEMPLATES: false });
        return;
    }
    SafeLogger.warn('DOMPurify not available — rendering as text instead of HTML');
    element.textContent = html === null || html === undefined ? '' : String(html);
}

/**
 * Update a progress bar UI element
 * @param {string|Element} fillElementId - The ID or element to fill
 * @param {string|Element} percentageElementId - The ID or element to show percentage
 * @param {number} percentage - The percentage to set
 */
function updateProgressBar(fillElementId, percentageElementId, percentage) {
    const progressFill = typeof fillElementId === 'string' ? document.getElementById(fillElementId) : fillElementId;
    const progressPercentage = typeof percentageElementId === 'string' ? document.getElementById(percentageElementId) : percentageElementId;

    if (progressFill && progressPercentage) {
        // Convert any value to a percentage between 0-100
        const safePercentage = Math.min(100, Math.max(0, percentage || 0));

        // Update the width of the fill element
        progressFill.style.width = `${safePercentage}%`;

        // Update the percentage text
        progressPercentage.textContent = `${Math.round(safePercentage)}%`;

        // Add classes for visual feedback
        if (safePercentage >= 100) {
            progressFill.classList.add('ldr-complete');
        } else {
            progressFill.classList.remove('ldr-complete');
        }
    }
}

/**
 * GLOBAL fallback for HTML escaping - provides XSS protection even if
 * xss-protection.js fails to load. Loaded via base.html on ALL pages.
 *
 * WARNING: DO NOT redeclare this variable in other top-level scripts.
 * Declaring `const`, `let`, or `var` escapeHtmlFallback at top-level
 * in another <script> tag will cause: SyntaxError: "Identifier
 * 'escapeHtmlFallback' has already been declared" — crashing that
 * script entirely.
 *
 * If you need escapeHtmlFallback in another file, either:
 *   1. Reference this global directly (it's available everywhere)
 *   2. Wrap your code in an IIFE and declare a local copy inside it
 */
// bearer:disable javascript_lang_manual_html_sanitization
const escapeHtmlFallback = (str) => String(str).replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]);

/**
 * Show a loading spinner
 * @param {string|Element} container - The container ID or element to add the spinner to
 * @param {string} message - Optional message to show with the spinner
 */
function showSpinner(container, message = 'Loading...') {
    const containerEl = typeof container === 'string' ? document.getElementById(container) : container;

    if (containerEl) {
        containerEl.innerHTML = '';

        // Escape message before including in HTML template
        const escapedMessage = message ? (window.escapeHtml || escapeHtmlFallback)(message) : '';
        const spinnerHTML = `
            <div class="ldr-loading-spinner ldr-centered">
                <div class="ldr-spinner"></div>
                ${escapedMessage ? `<div class="ldr-spinner-message">${escapedMessage}</div>` : ''}
            </div>
        `;

        // Safe: spinner HTML is controlled/static, message is escaped above
        safeSetHTML(containerEl, spinnerHTML);
    }
}

/**
 * Hide a loading spinner
 * @param {string|Element} container - The container ID or element with the spinner
 */
function hideSpinner(container) {
    const containerEl = typeof container === 'string' ? document.getElementById(container) : container;

    if (containerEl) {
        const spinner = containerEl.querySelector('.ldr-loading-spinner');
        if (spinner) {
            spinner.remove();
        }
    }
}

/**
 * Show an error message
 * @param {string|Element} container - The container ID or element to add the error to
 * @param {string} message - The error message
 */
function showError(container, message) {
    const containerEl = typeof container === 'string' ? document.getElementById(container) : container;

    if (containerEl) {
        // Escape message before including in HTML template
        const escapedMessage = (window.escapeHtml || escapeHtmlFallback)(message);
        const errorHTML = `
            <div class="ldr-error-message">
                <i class="fas fa-exclamation-circle"></i>
                <span>${escapedMessage}</span>
            </div>
        `;

        // Safe: error HTML is controlled/static, message is escaped above
        safeSetHTML(containerEl, errorHTML);
    }
}

/**
 * Get CSS variable value from document root
 * @param {string} varName - CSS variable name (e.g., '--success-color')
 * @param {string} fallback - Fallback value if variable not found
 * @returns {string} The CSS variable value
 */
function getCSSVariable(varName, fallback) {
    const value = getComputedStyle(document.documentElement).getPropertyValue(varName).trim();
    return value || fallback;
}

/**
 * Show a notification message
 * @param {string} message - The message to display
 * @param {string} type - Message type: 'success', 'error', 'info', 'warning'
 * @param {number} duration - How long to show the message in ms
 */
/**
 * Get or lazily create the two persistent notification banners.
 *
 * Live regions need to exist in the DOM before their text content
 * changes, otherwise NVDA/VoiceOver will not reliably announce the
 * message. The previous implementation removed and re-created the
 * banner on every `showMessage` call, which made announcements
 * inconsistent across browsers and screen readers.
 *
 * Two regions are used so that errors/warnings get assertive
 * announcement (interrupts current speech) while success/info get
 * polite (waits for the user to be idle).
 */
function _ensureNotificationBanners() {
    let polite = document.getElementById('notification-banner-polite');
    let assertive = document.getElementById(
        'notification-banner-assertive',
    );

    const buildBanner = (id, role, liveness) => {
        const el = document.createElement('div');
        el.id = id;
        el.className = 'ldr-notification-banner';
        el.setAttribute('role', role);
        el.setAttribute('aria-live', liveness);
        el.setAttribute('aria-atomic', 'true');
        // Stable internal structure: <i class="fas …"></i><span></span>
        // The span's textContent is what gets announced; mutating it
        // (rather than the element itself) is what makes the live
        // region work reliably.
        el.appendChild(document.createElement('i'));
        el.appendChild(document.createElement('span'));
        document.body.appendChild(el);
        return el;
    };

    if (!polite) {
        polite = buildBanner('notification-banner-polite', 'status', 'polite');
    }
    if (!assertive) {
        assertive = buildBanner(
            'notification-banner-assertive',
            'alert',
            'assertive',
        );
    }
    return { polite, assertive };
}

function showMessage(message, type = 'success', duration = UI_CONFIG.NOTIFICATION_DURATION_MS) {
    // Get theme colors from CSS variables with fallbacks
    let accentColor, iconClass;
    switch (type) {
        case 'success':
            accentColor = getCSSVariable('--success-color', '#0acf97');
            iconClass = 'fa-check-circle';
            break;
        case 'error':
            accentColor = getCSSVariable('--error-color', '#fa5c7c');
            iconClass = 'fa-exclamation-circle';
            break;
        case 'warning':
            accentColor = getCSSVariable('--warning-color', '#f9bc0b');
            iconClass = 'fa-exclamation-triangle';
            break;
        case 'info':
        default:
            accentColor = getCSSVariable('--accent-primary', '#6e4ff6');
            iconClass = 'fa-info-circle';
            break;
    }

    const { polite, assertive } = _ensureNotificationBanners();
    const banner =
        type === 'error' || type === 'warning' ? assertive : polite;
    const otherBanner = banner === polite ? assertive : polite;

    // Hide the other banner so it doesn't overlap visually.
    otherBanner.style.transform = 'translateY(-100%)';

    // Apply dynamic accent color for border (static styles handled by CSS class)
    banner.style.borderBottom = `3px solid ${accentColor}`;

    // Mutate the existing live region's children rather than rebuilding
    // the host element. Updating textContent on the inner span is what
    // triggers the screen reader announcement.
    const icon = banner.firstElementChild;
    const textSpan = banner.lastElementChild;
    icon.className = `fas ${iconClass}`;
    icon.style.color = accentColor;
    icon.style.fontSize = '1.1rem';
    textSpan.textContent = message;

    // Slide in
    requestAnimationFrame(() => {
        banner.style.transform = 'translateY(0)';
    });

    // Slide out after duration; clear any previous timer so a rapid
    // second call doesn't slide this one out early.
    clearTimeout(banner._hideTimer);
    banner._hideTimer = setTimeout(() => {
        banner.style.transform = 'translateY(-100%)';
    }, duration);
}

// Explicit DOMPurify allowlist for renderer-generated Markdown output. Raw
// author-supplied HTML is escaped at Marked's renderer.html hook below; this
// second boundary constrains HTML produced by Markdown/GFM and KaTeX.
// Executable/embedded-content tags remain excluded, event-handler
// attributes are excluded, and URL attributes keep DOMPurify's built-in scheme
// filter so javascript:/data:/vbscript: hrefs are dropped.
const MARKDOWN_ALLOWED_TAGS = [
    // Standard markdown / GFM output
    'p', 'br', 'hr', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'ul',
    'ol', 'li', 'strong', 'em', 'b', 'i', 'u', 'del', 's', 'strike', 'sub',
    'sup', 'a', 'code', 'pre', 'span', 'div', 'table', 'thead', 'tbody',
    'tfoot', 'tr', 'th', 'td',
    'input',  // GFM task-list checkboxes
    // `![alt](url)` images. DOMPurify's own tag/attr allowlist below cannot
    // express the src MIME/scheme sub-policy a safe <img> needs, so
    // hardenSanitizedMarkdown() removes any img whose src fails
    // isSafeMarkdownImageSrc (a Markdown-only policy built from URLValidator
    // helpers, security/url-validator.js) and strips every attribute but
    // src/alt/title from the ones that remain.
    'img',
    // KaTeX MathML output (katex-mathml block)
    'math', 'semantics', 'annotation', 'annotation-xml', 'mrow', 'mi', 'mn',
    'mo', 'ms', 'mtext', 'mfrac', 'msqrt', 'mroot', 'msub', 'msup', 'msubsup',
    'munder', 'mover', 'munderover', 'mmultiscripts', 'mprescripts', 'none',
    'mtable', 'mtr', 'mlabeledtr', 'mtd', 'mspace', 'mpadded', 'mphantom',
    'mstyle', 'merror', 'maligngroup', 'malignmark', 'maction', 'menclose',
    'mfenced', 'mglyph', 'mlongdiv', 'msline', 'mscarries', 'msgroup',
    'msrow', 'mstack',
    // Inline SVG generated by KaTeX for radicals, stretchy arrows/braces,
    // wide accents, delimiters, and cancellation lines. Raw Markdown HTML
    // is escaped by the renderer below, so these tags cannot originate in
    // user-authored HTML.
    'svg', 'path', 'line',
];

const MARKDOWN_ALLOWED_ATTR = [
    'href', 'title', 'target', 'rel',   // links (scheme-filtered)
    'src', 'alt',                       // images (src is policy-checked below)
    'class', 'id', 'style', 'role',     // layout, code languages, KaTeX styling
    'align', 'colspan', 'rowspan',      // tables
    'type', 'checked', 'disabled',      // GFM task-list inputs
    'start',                            // ordered lists
    // KaTeX SVG geometry. Attribute names are lowercase because DOMPurify
    // normalizes the HTML-string input before applying this allowlist.
    'xmlns', 'width', 'height', 'viewbox', 'preserveaspectratio', 'd',
    'x1', 'y1', 'x2', 'y2', 'stroke-width',
    // KaTeX's accessibility tree carries mathematical meaning in attributes,
    // including upright/double-struck identifiers and the TeX annotation.
    // Raw author HTML is escaped before this renderer-only allowlist runs.
    'accent', 'accentunder', 'columnalign', 'columnlines', 'columnspacing',
    'depth', 'display', 'displaystyle', 'encoding', 'fence', 'largeop',
    'linebreak', 'linethickness', 'lspace', 'mathbackground', 'mathcolor',
    'mathsize', 'mathvariant', 'maxsize', 'minsize', 'notation', 'rowlines',
    'rowspacing', 'rspace', 'scriptlevel', 'separator', 'stretchy', 'valign',
    'voffset',
];

const MARKDOWN_SANITIZE_CONFIG = {
    ALLOWED_TAGS: MARKDOWN_ALLOWED_TAGS,
    ALLOWED_ATTR: MARKDOWN_ALLOWED_ATTR,
    // KaTeX uses aria-hidden="true", but with trust:false (the application
    // default) it emits no data-* attributes. Renderer output must not be
    // able to manufacture Bootstrap/news dispatcher controls through data-*.
    ALLOW_ARIA_ATTR: true,
    ALLOW_DATA_ATTR: false,
};

// Applied only after processSpecialMarkdown() has constructed a validated
// local generated-image reference with DOM APIs. `img`/`src`/`alt` are
// already in the base config (author-supplied `![alt](url)` images survive
// the strict pass too, subject to hardenSanitizedMarkdown's src policy);
// this restores the sanitizer boundary after serialization while adding
// only the extra attributes a generated image's own markup requires.
const GENERATED_IMAGE_SANITIZE_CONFIG = {
    ...MARKDOWN_SANITIZE_CONFIG,
    ALLOWED_ATTR: [
        ...MARKDOWN_ALLOWED_ATTR,
        'loading',
        // Added by processSpecialMarkdown() only after the strict pass has
        // removed every author-supplied data-* attribute.
        'data-image-id',
    ],
};

// DOMPurify's default SANITIZE_DOM protects browser-provided properties, but
// app-defined window globals need an explicit namespace boundary.
//
// WHAT THIS LAYER ACTUALLY COVERS: nothing reachable through the current
// Markdown pipeline. `name` is not in MARKDOWN_ALLOWED_ATTR, so DOMPurify
// strips it before hardenSanitizedMarkdown ever runs; `id` is allowed, but
// the only ways an `id` could carry an author-chosen value are raw HTML in
// the Markdown source -- which renderMarkdown's `renderer.html` hook escapes
// to literal text before marked emits it -- and heading anchors, which
// marked 18 no longer generates (`headerIds` was removed upstream). Deleting
// the `[id], [name]` namespacing loop below therefore fails no test today.
// It is kept purely as defence-in-depth, so that a future change to the
// allowlist, the renderer hook or the Markdown extensions cannot silently
// re-open a DOM-clobbering path. This is the same situation the
// ALLOWED_INPUT_TYPES pass is in; see the note on
// "keeps only renderer-generated GFM checkboxes ..." in
// tests/js/security/markdown-render-xss.test.js.
//
// Consequently there is NO automated check that keeps this list in step with
// the app's real window globals -- the clobbering tests in
// tests/js/security/clobbering-prototype-pollution.test.js derive their own
// global list from source, but they exercise the Markdown path, where the
// values never arrive, so a name missing from here does not turn them red.
// Treat the list as a best-effort snapshot; it was last reconciled against
// the `window.<name> =` assignments in static/js on 2026-09-04.
const MARKDOWN_PROHIBITED_NAMED_PROPS = new Set([
    // Sensitive Window/Document properties.
    'close', 'document', 'frames', 'location', 'name', 'opener', 'top',
    // Application globals.
    '__annotationSurfaceTest', '__documentNotesTest', '__LDR_MOBILE_BREAKPOINT_PX',
    '__noteDetailTest', '__notesTest', '__researchNotesTest', '__unifiedSearchTest',
    '_logAutoExpandTimer', '_socketAddLogEntry', 'addConsoleLog', 'api',
    'applyCustomTemplate', 'audio', 'bindReindexButtons', 'bootstrap', 'Chart',
    'chatComponent', 'checkAndDisplayWarnings', 'checkboxHandler', 'clearAllFilters',
    'clearAllWarnings', 'clearTopicFilter', 'collapseAll', 'collectionApiUrl',
    'contextOverflowController', 'contextOverflowShared', 'copyNewsLink',
    'copyQueryTemplate', 'createSafeAlertElement', 'createSafeLoadingOverlay',
    'createSubscription', 'createSubscriptionFromItem', 'createTag',
    'createTopicSubscription', 'debounce', 'DeleteConfirmation', 'DeleteManager',
    'displayCollectionEmbeddingSettings', 'displayWarnings', 'DOMPurify',
    'escapeHtml', 'escapeHtmlAttribute', 'expandAll',
    'exportToMarkdown', 'filterBySubscription', 'filterByTopic', 'filterLogsByType',
    'followUpResearch', 'FollowUpResearch', 'formatBytes', 'formatNextUpdate',
    'formatSettingsSnapshot', 'formatSettingValue', 'formatTimeAgo', 'formatting',
    'FormValidator', 'formValidators', 'getProviderLabel', 'HelpService', 'hideModal',
    'hideNewsItem', 'hideProgressUI', 'hideQueryTemplate', 'HistorySearch', 'hljs',
    'html2canvas', 'HTTPError', 'indexStatusMarkup', 'io', 'jsPDF', 'KeyboardService',
    'LDR_CONSTANTS', 'LdrAlertHelpers', 'LDRAnnotationSurface', 'LdrLogHelpers',
    'LdrProviderOptions', 'LdrValueHelpers', 'LibrarySearch',
    'loadBackgroundSweepSetting', 'loadNewsFeed', 'loadNewsTableQuery',
    'loadSearchHistory', 'logPanel', 'markAllAsRead', 'markAsReadOnClick', 'marked',
    'mobileNav', 'MobileNavigation', 'modelDropdownsInitialized',
    'modelProvidersRequestInProgress', 'NewsUtils', 'NotesShared', 'onerror',
    'pdfService', 'pdfUploadHandler', 'pollIntervals', 'pollResearchStatus',
    'progressComponent', 'refetchSettingsAndUpdateWarnings', 'refreshFeed',
    'renderCollections', 'renderIndexingFailure', 'rerunSearch', 'ResearchStates',
    'safeCreateElement', 'safeFetch', 'safeFetchJson', 'safeFetchWithAuth', 'safeLog',
    'safeLogDebug', 'safeLogError', 'SafeLogger', 'safeLogInfo', 'safeLogWarn',
    'safeSetInnerHTML', 'safeSetStyles', 'safeSetTextContent', 'safeUpdateButton',
    'sanitizeHtml', 'sanitizeUserInput', 'saveBackgroundSweepSetting', 'saveItem',
    'saveProviderSetting', 'searchEngineDropdownInitialized',
    'searchEnginesRequestInProgress', 'selectSubscription', 'SemanticSearch',
    'setCollectionDataForTesting',
    'setupCustomDropdown', 'setupSettingsDropdowns', 'shareNews',
    'showCreateSubscriptionModal', 'showEmptyState', 'showErrorState',
    'showLoadingState', 'showModal', 'showNotification', 'showSafeAlert', 'socket',
    'showUploadResults', 'startPolling', 'subscriptionManager', 'themeService', 'toggleExpanded',
    'toggleReadStatus', 'toggleSaveItem', 'triggerReindex', 'ui',
    'updateDropdownOptions', 'updateEnhancedDomainList', 'URLBuilder', 'URLS',
    'URLValidator', 'useNewsTemplate', 'useQueryTemplate', 'validatePasswordViaAPI',
    'vote', 'XSSProtection', 'yamlEscape',
]);

/**
 * Post-sanitize hardening for the value layer of controls and styles.
 *
 * The explicit ALLOWED_TAGS allowlist intentionally keeps <input> (so GFM
 * task-list checkboxes survive) and the style attribute (so KaTeX inline
 * layout works), but DOMPurify's config cannot express the value-level
 * restrictions we also want:
 *
 *   * interactive controls other than GFM checkboxes are stripped, and
 *     every surviving checkbox is forced into a display-only state — it
 *     can no longer receive focus, submit, or carry form-association data;
 *   * inline styles are parsed by the browser and rebuilt from a narrow
 *     KaTeX property/value allowlist. Anything else is dropped wholesale,
 *     including custom properties, network-bearing image functions and
 *     overlay positioning.
 *
 * Implemented on top of a detached DOMParser document so the sanitized
 * string is parsed into its body, mutated, then serialized back.
 * The final sanitizeStable() pass precedes insertion into the live document.
 */
const ALLOWED_INPUT_TYPES = new Set(['checkbox']); // every other type is stripped in the hardening pass
const ALLOWED_CHECKBOX_ATTRS = new Set([
    'type', 'checked', 'disabled', 'class',
]);

// Attributes a surviving <img> keeps. Pinned to this exact set (rather than
// letting MARKDOWN_ALLOWED_ATTR's general 'class'/'id'/'style' reach img
// too) so a future addition to that shared list cannot silently widen this
// element's surface; srcset/onerror/onload are never in MARKDOWN_ALLOWED_ATTR
// to begin with, so DOMPurify already strips them, but this keeps the
// invariant explicit and independent of that list.
const SAFE_MARKDOWN_IMG_ATTRS = new Set(['src', 'alt', 'title']);

/**
 * Decide whether an `<img src>` produced by sanitized Markdown may be kept.
 *
 * A Markdown-only policy assembled from URLValidator helpers
 * (security/url-validator.js). It is not shared with any other URL check:
 * `isSafeHref` (security/xss-protection.js) is a separate private helper of
 * safeCreateElement, Markdown link hrefs rely on DOMPurify's built-in URI
 * filter, and URLValidator.safeAssign's `data:` carve-out has its own
 * rules (the note above its data: branch lists the differences). The rules:
 *
 *   * `javascript:`/`vbscript:` and the rest of URLValidator.UNSAFE_SCHEMES
 *     are rejected outright (belt-and-braces: DOMPurify's own URI filter
 *     already drops these schemes on a `src` attribute).
 *   * `data:` URIs are restricted to URLValidator.SAFE_DATA_MIME_TYPES, a
 *     raster-only allowlist (png/jpeg/gif/webp/bmp) that deliberately
 *     excludes `image/svg+xml`. The MIME type is read from the
 *     normalizeUrlForSchemeCheck() form, and `;base64,` is not required.
 *
 *     To be precise about WHY SVG is excluded, because the obvious reason
 *     is wrong: an SVG referenced from `<img src>` is loaded in the image
 *     "secure static mode" -- scripting, external references and
 *     interactivity are all disabled -- so `data:image/svg+xml` in *this*
 *     position does not run `<script>` or event handlers. The exclusion is
 *     a conservative default, not an execution fix. This predicate is the
 *     only MIME gate in the pipeline: DOMPurify's `data:` allowance for
 *     `<img>` (DATA_URI_TAGS) is scheme-only, not MIME-aware, so without
 *     the check here ANY `data:` payload -- `data:text/html`,
 *     `data:image/svg+xml`, anything -- reaches the DOM on a `src`
 *     attribute untouched. Keeping that one gate raster-only matches
 *     safeAssign's separate data: check, which admits no SVG either.
 *   * Anything else must pass URLValidator.isSafeUrl: http, https, ftp or
 *     ftps, or a relative path resolved against the page. Unlike
 *     isSafeHref, a `//host/path` protocol-relative source is NOT rejected
 *     here: isSafeHref's rejection is about the auxiliary-browsing-context/
 *     phishing risk of *navigating* somewhere unexpected, which does not
 *     apply to an <img> loading pixels, and `//host/x.png` carries no more
 *     risk than `https://host/x.png`.
 *
 * Remote sources pass this predicate, but the shipped Content-Security-
 * Policy (`img-src 'self' data:`, web/fastapi_app.py) blocks them in the
 * browser, so the images that actually display today are same-origin ones
 * and the raster `data:` images allowed above.
 *
 * @param {string|null} src - The img element's `src` attribute value.
 * @returns {boolean} - True if the image may be kept.
 */
function isSafeMarkdownImageSrc(src) {
    if (typeof src !== 'string' || !src) {
        return false;
    }
    if (typeof URLValidator === 'undefined') {
        // Fail closed if URLValidator (security/url-validator.js) did not load.
        return false;
    }

    // Detect the scheme and MIME type on the normalized form, so a control
    // character the browser would ignore cannot dodge either check.
    // safeAssign's data: carve-out does not share this step (see
    // URLValidator.normalizeUrlForSchemeCheck).
    const normalized = URLValidator.normalizeUrlForSchemeCheck(src);
    if (!normalized) {
        return false;
    }

    if (/^data:/i.test(normalized)) {
        const mime = /^data:([^;,]*)/i.exec(normalized);
        return !!(mime && URLValidator.SAFE_DATA_MIME_TYPES.includes(mime[1].toLowerCase()));
    }

    if (URLValidator.isUnsafeScheme(normalized)) {
        return false;
    }

    return URLValidator.isSafeUrl(normalized, { allowFragments: false });
}

// KaTeX emits a small, stable set of inline metrics. Keep the maximum generous
// enough for large equations while bounding anomalous renderer output as a
// defense in depth; author-supplied raw styles never reach this pass.
const MAX_KATEX_INLINE_LENGTH_EM = 50;

function isSafeKatexLength(value, allowNegative = false) {
    const normalized = value.trim().toLowerCase();
    // CSSOM serializes a unitless zero as 0px in some implementations.
    if (/^-?0(?:\.0+)?(?:em|px)?$/.test(normalized)) {
        return true;
    }
    const match = /^(-?(?:\d+(?:\.\d*)?|\.\d+))em$/.exec(normalized);
    if (!match) {
        return false;
    }
    const number = Number(match[1]);
    return Number.isFinite(number)
        && (allowNegative || number >= 0)
        && Math.abs(number) <= MAX_KATEX_INLINE_LENGTH_EM;
}

function isSafeKatexMargin(value) {
    const parts = value.trim().split(/\s+/);
    return parts.length >= 1 && parts.length <= 4
        && parts.every((part) => isSafeKatexLength(part, true));
}

// Matches a single rgb()/rgba() numeric or percentage component: an
// integer/decimal, optionally followed by `%`. No calc(), no var(), no
// exponents -- just the plain numeric grammar getPropertyValue() emits.
const RGB_COMPONENT = String.raw`-?(?:\d+(?:\.\d+)?|\.\d+)%?`;
// The optional alpha group starts with its own comma (not a leading `\s*`)
// so it never sits directly adjacent to another `\s*` -- two `\s*` quantifiers
// that can both match the same whitespace is a superlinear-backtracking
// shape (regexp/no-super-linear-backtracking), not just a style nit.
const RGB_COLOR_RE = new RegExp(
    `^rgba?\\(\\s*(${RGB_COMPONENT})\\s*,\\s*(${RGB_COMPONENT})\\s*,\\s*(${RGB_COMPONENT})`
    + `(?:\\s*,\\s*(${RGB_COMPONENT}))?\\s*\\)$`,
    'i',
);

function isSafeRgbColorComponent(component, max) {
    if (component.endsWith('%')) {
        const percent = Number(component.slice(0, -1));
        return Number.isFinite(percent) && percent >= 0 && percent <= 100;
    }
    const number = Number(component);
    return Number.isFinite(number) && number >= 0 && number <= max;
}

function isSafeKatexColor(value) {
    const normalized = value.trim();

    // This matches KaTeX's color-token grammar plus currentColor, which the
    // application uses for theme-aware parse errors.
    if (/^(?:#[a-f0-9]{3,4}|#[a-f0-9]{6}|#[a-f0-9]{8}|[a-z]+)$/i.test(normalized)) {
        return true;
    }

    // KaTeX only ever authors hex colors, but CSSOM never echoes a hex (or
    // keyword) color back verbatim: reading a `color:#df0030` declaration
    // through element.style.getPropertyValue() returns the browser's
    // SERIALIZED form, which every current engine renders as `rgb(r, g, b)`
    // / `rgba(r, g, b, a)` -- never the original hex text. Without accepting
    // that serialized shape, every KaTeX color declaration fails validation
    // on real browsers (happy-dom does not perform this normalization,
    // which is why no existing vitest suite caught this). Parse strictly:
    // exactly three/four numeric-or-percentage components in range, nothing
    // else -- this cannot match url(), var(), calc(), or any other
    // function-valued or custom-property-referencing form.
    const rgbMatch = RGB_COLOR_RE.exec(normalized);
    if (rgbMatch) {
        const [, r, g, b, a] = rgbMatch;
        if (![r, g, b].every((component) => isSafeRgbColorComponent(component, 255))) {
            return false;
        }
        return a === undefined || isSafeRgbColorComponent(a, 1);
    }

    // color()/oklch()/lab()/lch() etc: deliberately rejected, not merely
    // unhandled. KaTeX's only color inputs are the hex/keyword literals
    // matched above, and every engine serializes those (and their rgb()
    // equivalents) through the sRGB rgb()/rgba() form matched above --
    // never through a wide-gamut color function -- so a real KaTeX render
    // can never legitimately produce one. Accepting them would widen the
    // parser surface (nested legacy color notation, `none` angle/percent
    // components, `var()`-only-inside-color() variants) for a shape this
    // pipeline never needs to pass, so they stay outside the boundary.
    return false;
}

const SAFE_KATEX_STYLE_VALIDATORS = new Map([
    ['background-color', isSafeKatexColor],
    ['border-bottom-color', isSafeKatexColor],
    // `border-style` is the shorthand KaTeX authors (e.g. \fcolorbox's
    // border, katex.js sets `borderStyle = "solid"`); no browser's CSSOM
    // enumerates a shorthand parsed from an inline style attribute -- it
    // is always expanded into the four border-*-style longhands, same as
    // border-color/border-width below. Without all four, three sides
    // silently fall back to their default style and the box renders with
    // only one visible edge.
    ['border-bottom-style', (value) => /^(?:solid|dashed)$/i.test(value.trim())],
    ['border-bottom-width', isSafeKatexLength],
    // `border-color` is the shorthand KaTeX authors (e.g. \fcolorbox's
    // border), but no browser's CSSOM enumerates a shorthand parsed from an
    // inline style attribute -- it is always expanded into the four
    // border-*-color longhands below. The shorthand entry is kept too
    // (harmless, and a safety net for any engine that behaves otherwise),
    // but without the longhands a real browser silently drops the color
    // and \fcolorbox's border falls back to currentColor.
    ['border-color', isSafeKatexColor],
    ['border-left-color', isSafeKatexColor],
    ['border-left-style', (value) => /^(?:solid|dashed)$/i.test(value.trim())],
    ['border-left-width', isSafeKatexLength],
    ['border-right-color', isSafeKatexColor],
    ['border-right-style', (value) => /^(?:solid|dashed)$/i.test(value.trim())],
    ['border-right-width', isSafeKatexLength],
    ['border-style', (value) => /^solid$/i.test(value.trim())],
    ['border-top-color', isSafeKatexColor],
    ['border-top-style', (value) => /^(?:solid|dashed)$/i.test(value.trim())],
    ['border-top-width', isSafeKatexLength],
    ['border-width', isSafeKatexLength],
    ['bottom', (value) => isSafeKatexLength(value, true)],
    ['color', isSafeKatexColor],
    ['height', isSafeKatexLength],
    ['left', (value) => isSafeKatexLength(value, true)],
    // Browsers differ on whether CSSOM exposes KaTeX's `margin` shorthand or
    // its four expanded longhands, so safely accept both representations.
    ['margin', isSafeKatexMargin],
    ['margin-bottom', (value) => isSafeKatexLength(value, true)],
    ['margin-left', (value) => isSafeKatexLength(value, true)],
    ['margin-right', (value) => isSafeKatexLength(value, true)],
    ['margin-top', (value) => isSafeKatexLength(value, true)],
    ['min-width', isSafeKatexLength],
    ['padding-left', isSafeKatexLength],
    ['position', (value) => /^relative$/i.test(value.trim())],
    ['text-shadow', (value) => value.trim().replace(/\s+/g, ' ')
        === '0.02em 0.01em 0.04px'],
    ['top', (value) => isSafeKatexLength(value, true)],
    ['vertical-align', (value) => isSafeKatexLength(value, true)],
    ['width', isSafeKatexLength],
]);

// `class` is needed for KaTeX, GFM tasks and fenced-code language labels.
// Historical highlighting classes remain for compatibility, not Marked 18 output.
// Keep this set narrow so extensions cannot invoke unrelated global utilities.
const SAFE_MARKDOWN_CLASS_NAMES = new Set([
    // GFM task lists.
    'contains-task-list', 'task-list-item', 'task-list-item-checkbox',
    // Retained Prism token classes. Language identifiers use the safe prefix below.
    'token', 'atrule', 'attr-name', 'attr-value', 'bold', 'boolean', 'builtin',
    'cdata', 'char', 'class-name', 'comment', 'constant', 'decorator', 'deleted',
    'doctype', 'entity', 'function', 'function-variable', 'hashbang', 'important',
    'inserted', 'interpolation', 'interpolation-punctuation', 'italic', 'keyword',
    'known-class-name', 'literal-property', 'namespace', 'number', 'operator',
    'parameter', 'prolog', 'property', 'punctuation', 'regex', 'selector', 'string',
    'symbol', 'tag', 'template-punctuation', 'template-string', 'triple-quoted-string',
    'url', 'variable',
    // KaTeX renderer classes (HTML and MathML wrappers). KaTeX 0.18
    // prefixes several layout classes; keep those alongside legacy names.
    'katex-accent', 'katex-base', 'katex-fix', 'katex-hdashline', 'katex-hline',
    'katex-inner', 'katex-newline', 'katex-overlay', 'katex-overline',
    'katex-root', 'katex-rule', 'katex-sizing', 'katex-smash', 'katex-sout',
    'katex-stretchy', 'katex-strut', 'katex-tag', 'katex-thinbox',
    'katex-underline', 'katex-vbox',
    'accent', 'accent-body', 'accent-full', 'amsrm', 'angl', 'anglpad',
    'arraycolsep', 'base', 'boldsymbol', 'boxpad', 'brace-center', 'brace-left',
    'brace-right', 'cancel-lap', 'cancel-pad', 'cd-arrow-pad', 'cd-label-left',
    'cd-label-right', 'cd-vert-arrow', 'clap', 'col-align-c', 'col-align-l',
    'col-align-r', 'colorbox', 'delim-size1', 'delim-size4', 'delimcenter',
    'delimsizing', 'eqn-num', 'fbox', 'fcolorbox', 'fix', 'fleqn',
    'fontsize-ensurer', 'frac-line', 'halfarrow-left', 'halfarrow-right', 'hbox',
    'hdashline', 'hide-tail', 'hline', 'inner', 'katex', 'katex-display',
    'katex-error', 'katex-html', 'katex-mathml', 'katex-version', 'large-op',
    'leqno', 'llap',
    'mainrm', 'mathbb', 'mathbf', 'mathboldfrak', 'mathboldsf', 'mathcal',
    'mathfrak', 'mathit', 'mathitsf', 'mathnormal', 'mathrm', 'mathscr', 'mathsf',
    'mathsfit', 'mathtt', 'mbin', 'mclose', 'mfrac', 'minner', 'mml-eqn-num',
    'mop', 'mopen', 'mord', 'mover', 'mpunct', 'mrel', 'mspace', 'msupsub',
    'mtable', 'mtight', 'mtr-glue', 'mult', 'munder', 'newline', 'nulldelimiter',
    'op-limits', 'op-symbol', 'overlay', 'overline', 'overline-line', 'pstrut',
    'reset-size1', 'reset-size2', 'reset-size3', 'reset-size4', 'reset-size5',
    'reset-size6', 'reset-size7', 'reset-size8', 'reset-size9', 'reset-size10',
    'reset-size11', 'rlap', 'root', 'rule', 'size1', 'size2', 'size3', 'size4',
    'size5', 'size6', 'size7', 'size8', 'size9', 'size10', 'size11', 'sizing',
    'small-op', 'smash', 'sout', 'sqrt', 'stretchy', 'strut', 'svg-align', 'tag',
    'text', 'textbb', 'textbf', 'textboldfrak', 'textboldsf', 'textfrak', 'textit',
    'textitsf', 'textrm', 'textscr', 'textsf', 'texttt', 'thinbox', 'ttf',
    'underline', 'underline-line', 'vbox', 'vertical-separator', 'vlist', 'vlist-r',
    'vlist-s', 'vlist-t', 'vlist-t2', 'x-arrow', 'x-arrow-pad',
]);

function isSafeMarkdownClassName(className) {
    return SAFE_MARKDOWN_CLASS_NAMES.has(className)
        // Marked places the requested lexer name on fenced code blocks;
        // Retain the highlight.js class prefixes for compatibility.
        || /^language-[\w-]+$/i.test(className)
        || /^hljs(?:-[\w-]+)?$/i.test(className);
}

function getSafeKatexInlineStyle(element) {
    // Filter, don't fail-all: an unrecognized/invalid declaration is
    // dropped on its own, never taken as license to drop every declaration
    // on the element. KaTeX packs geometry (height/vertical-align/top/
    // margin) onto the same span as color, so a fail-all policy lets one
    // rejected color property (see isSafeKatexColor above) silently strip
    // the positioning that makes the math render correctly -- a rendering
    // regression, not a safety win: the security property this function
    // exists for is that no unsafe VALUE survives, and per-property
    // filtering keeps that while shrinking the blast radius of a rejection
    // from "the whole style" to "that one property".
    const declarations = [];
    for (let i = 0; i < element.style.length; i++) {
        const property = element.style.item(i).toLowerCase();
        const value = element.style.getPropertyValue(property).trim();
        const validator = SAFE_KATEX_STYLE_VALIDATORS.get(property);
        if (!validator || element.style.getPropertyPriority(property) || !validator(value)) {
            continue;
        }
        declarations.push(`${property}: ${value}`);
    }
    return declarations.length > 0 ? `${declarations.join('; ')};` : null;
}

function hardenSanitizedMarkdown(html) {
    if (!html) {
        return html;
    }
    // html has passed sanitizeStable() under the explicit allowlist.
    // Parse it into a detached document before applying the value filters.
    // Scripts are inert here; the sanitizer and final sanitizeStable()
    // pass, rather than DOMParser itself, establish the HTML boundary.
    // Raw author HTML is escaped upstream, and marked emits complete GFM
    // tables; regression tests cover those shapes and the supported
    // KaTeX markup through this document-parse path.
    const root = new window.DOMParser().parseFromString(html, 'text/html').body;
    // Interactive controls: keep only display-only checkboxes. Everything
    // else (password/text/button/form/etc.) is dropped wholesale, and any
    // surviving checkbox is forced into a non-interactive state carrying
    // only display-relevant attrs (type/checked/disabled/class/aria-*).
    for (const input of [...root.querySelectorAll('input')]) {
        const type = (input.getAttribute('type') || '').toLowerCase();
        if (!ALLOWED_INPUT_TYPES.has(type)) {
            input.remove();
            continue;
        }
        input.setAttribute('disabled', '');
        for (const name of input.getAttributeNames()) {
            if (ALLOWED_CHECKBOX_ATTRS.has(name) || name.startsWith('aria-')) {
                continue;
            }
            input.removeAttribute(name);
        }
    }
    // Images: DOMPurify's tag/attr allowlist gets `<img>` and its
    // attribute NAMES this far, but not the src-value policy (MIME/scheme)
    // a safe image needs -- see isSafeMarkdownImageSrc(). Drop any image
    // whose src does not clear that policy, and pin every surviving one
    // down to exactly src/alt/title so it cannot carry a class/id/style
    // value the generic loops below would otherwise have to re-validate.
    for (const img of [...root.querySelectorAll('img')]) {
        if (!isSafeMarkdownImageSrc(img.getAttribute('src'))) {
            img.remove();
            continue;
        }
        for (const name of img.getAttributeNames()) {
            if (!SAFE_MARKDOWN_IMG_ATTRS.has(name)) {
                img.removeAttribute(name);
            }
        }
    }
    // Inline styles: rebuild the browser-parsed declaration list from a
    // positive KaTeX allowlist. Re-serialization is important: unsupported
    // or malformed syntax cannot hitch a ride beside declarations CSSOM did
    // understand, and CSS escapes/custom properties never reach the output.
    for (const el of root.querySelectorAll('[style]')) {
        const safeStyle = getSafeKatexInlineStyle(el);
        if (!safeStyle) {
            el.removeAttribute('style');
        } else {
            el.setAttribute('style', safeStyle);
        }
    }
    // Class values are another CSS execution surface. Filter individual
    // tokens so an allowlisted class can survive without carrying a
    // Bootstrap utility class alongside it.
    for (const el of root.querySelectorAll('[class]')) {
        const safeClasses = [...el.classList].filter(isSafeMarkdownClassName);
        if (safeClasses.length === 0) {
            el.removeAttribute('class');
        } else {
            el.setAttribute('class', safeClasses.join(' '));
        }
    }
    // Namespace only dangerous id/name values. Unlike DOMPurify's strict
    // SANITIZE_NAMED_PROPS mode, this leaves benign heading anchors unchanged.
    for (const el of root.querySelectorAll('[id], [name]')) {
        for (const attributeName of ['id', 'name']) {
            const value = el.getAttribute(attributeName);
            if (MARKDOWN_PROHIBITED_NAMED_PROPS.has(value)) {
                el.setAttribute(attributeName, `user-content-${value}`);
            }
        }
    }
    return root.innerHTML;
}


/**
 * Fail-closed markdown rendering: escape everything, show as text.
 * Shared by the marked-missing and DOMPurify-missing branches --
 * neither missing library may ever yield raw parsed markup.
 */
function _escapedPlaintextFallback(markdown) {
    const escaped = typeof window.escapeHtml === 'function'
        ? window.escapeHtml(markdown)
        // bearer:disable javascript_lang_manual_html_sanitization
        : String(markdown).replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]);

    return `<div class="markdown-content">
            <div class="alert alert-warning" style="margin-bottom: 1rem;">
                <i class="fas fa-exclamation-triangle"></i> Markdown rendering unavailable. Displaying as plaintext.
            </div>
            <pre style="white-space: pre-wrap; word-wrap: break-word; font-family: inherit;">${escaped}</pre>
        </div>`;
}

/**
 * Format and render Markdown content
 * @param {string} markdown - The markdown content
 * @returns {string} The rendered HTML
 */
function renderMarkdown(markdown) {
    if (!markdown) {
        return '<div class="alert alert-warning">No content available</div>';
    }

    // security/xss-protection.js publishes window.XSSProtection. base.html
    // loads it on every page, and renderMarkdown only ever runs from event
    // handlers and render callbacks — long after every deferred script has
    // executed — so this resolves in production. It is read defensively
    // anyway: an unguarded `window.XSSProtection.sanitizeStable(...)` would
    // throw when that file is missing (404, CSP, a page that stops extending
    // base.html) and the catch below would turn a missing sanitizer into an
    // "Error rendering content" banner. Treat it like marked/DOMPurify
    // instead, so a missing sanitizer degrades to the escaped-plaintext
    // fallback that already exists for exactly this case.
    const sanitizeStable = window.XSSProtection && window.XSSProtection.sanitizeStable;

    try {
        // Use marked library if available. DOMPurify is required too: this
        // branch produces HTML from untrusted markdown, and without a
        // sanitizer there is nothing between marked's output and innerHTML.
        // It previously fell through with `sanitized = html` (raw) — a
        // fail-OPEN, and inconsistent with the marked-missing branch below,
        // which correctly escapes.
        if (typeof marked !== 'undefined' && typeof DOMPurify !== 'undefined' && DOMPurify?.isSupported === true
            && typeof sanitizeStable === 'function') {
            // Configure marked options and custom renderer for links and special elements
            const renderer = new marked.Renderer();
            const _origLink = renderer.link.bind(renderer);
            renderer.link = function(token) {
                return _origLink(token).replace(/^<a /, '<a target="_blank" rel="noopener noreferrer" '); // DevSkim: ignore DS610000
            };
            // Raw HTML has no trustworthy provenance: if it reaches the
            // final sanitizer as active markup, it can impersonate the
            // KaTeX classes and inline metrics that generated output
            // legitimately needs. Escaping at Marked's raw-HTML hook keeps
            // ordinary Markdown, GFM, fenced code, and KaTeX rendering
            // intact while making author-supplied tags visible as text.
            // Renderer extensions such as marked-katex-extension bypass
            // this hook, so their generated markup still reaches the tight
            // tag/attribute/value allowlists below.
            renderer.html = function(token) {
                const rawHtml = typeof token === 'string'
                    ? token
                    : token?.raw ?? token?.text ?? '';
                return escapeHtmlFallback(rawHtml);
            };
            marked.setOptions({
                breaks: true,
                gfm: true,
                headerIds: true,
                // smartypants:false — converting `--` to en/em-dashes
                // mangles code blocks that contain CLI flags (e.g.
                // `--verbose`) or Python `**kwargs`. Match app.js so notes
                // rendering preserves verbatim code for copy-paste.
                smartypants: false,
            });

            // Parse markdown and return HTML
            const html = marked.parse(markdown, { renderer });

            // Sanitize to prevent XSS from markdown content. This runs
            // BEFORE processSpecialMarkdown so the strictly-validated
            // generated-image markup it constructs (fixed attribute set,
            // GENERATED_IMAGE_DIRECTIVE-guarded) still produces its <img>
            // while all markdown-derived HTML is forced through the
            // allowlist first.
            const sanitized = sanitizeStable(html, MARKDOWN_SANITIZE_CONFIG);
            // Value-layer hardening of interactive controls and inline styles.
            // DOMPurify's tag/attr allowlist cannot express the value-level
            // restrictions (input type filter, style token filter) we want,
            // so a detached inert-document mutation handles them. Runs BEFORE
            // processSpecialMarkdown for the same reason as DOMPurify above.
            const hardened = hardenSanitizedMarkdown(sanitized);

            // Process any special elements like image references
            const processedHtml = processSpecialMarkdown(hardened);

            // processSpecialMarkdown serializes trusted nodes back to a
            // string, so sanitize to a fixpoint once more before callers
            // assign the result to innerHTML. Ordinary Markdown images have
            // already passed the URL and attribute checks above.
            const finalHtml = sanitizeStable(
                processedHtml,
                GENERATED_IMAGE_SANITIZE_CONFIG,
            );

            return `<div class="ldr-markdown-content">${finalHtml}</div>`;
        }
        // Fallback if marked is not available - display as plaintext for security
        // Using regex-based partial markdown is fragile and a security risk,
        // so we escape all HTML and display as preformatted text with a warning
        SafeLogger.warn('marked, supported DOMPurify and/or XSSProtection.sanitizeStable not available. Displaying as plaintext for security.');
        return _escapedPlaintextFallback(markdown);
    } catch (error) {
        SafeLogger.error('Error rendering markdown:', error);
        const escapedMessage = typeof window.escapeHtml === 'function'
            ? window.escapeHtml(error.message)
            // bearer:disable javascript_lang_manual_html_sanitization
            : String(error.message).replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]);
        return `<div class="alert alert-danger">Error rendering content: ${escapedMessage}</div>`;
    }
}

/**
 * Process special markdown elements
 * @param {string} html - HTML content to process
 * @returns {string} - Processed HTML
 */
function processSpecialMarkdown(html) {
    // Generated-image references are block directives. Only a marker that is
    // the sole text child of a paragraph produced by marked is eligible. This
    // deliberately excludes attributes, links, code, KaTeX, and inline prose.
    // Searching the serialized HTML with a regex would lose that context and
    // could turn harmless attribute text into executable markup after the
    // final browser parse.
    const GENERATED_IMAGE_DIRECTIVE = /^\s*!\[ref:(image-[\w-]+)\]\s*$/;
    // html has passed sanitizeStable() and the value hardening above.
    // Parse it into a detached document; build generated-image markup
    // with DOM APIs, then sanitize the serialized result once more in
    // renderMarkdown() before any caller inserts it into the live document.
    const root = new window.DOMParser().parseFromString(html, 'text/html').body;

    for (const paragraph of [...root.querySelectorAll('p')]) {
        if (paragraph.childNodes.length !== 1 ||
            paragraph.firstChild.nodeType !== Node.TEXT_NODE) {
            continue;
        }

        const match = GENERATED_IMAGE_DIRECTIVE.exec(paragraph.textContent);
        if (!match) {
            continue;
        }

        const ref = match[1];
        const wrapper = document.createElement('div');
        wrapper.className = 'ldr-generated-image';
        wrapper.setAttribute('data-image-id', ref);

        const image = document.createElement('img');
        // Keep the path component explicitly encoded even though the
        // directive regex already limits it to image-[\w-]+. Besides making
        // the URL boundary self-contained, this lets static taint analysis
        // verify that DOM text cannot become an executable URL.
        const encodedRef = encodeURIComponent(ref);
        image.setAttribute('src', `/static/img/generated/${encodedRef}.png`);
        image.setAttribute('alt', `Generated image ${ref}`);
        image.className = 'img-fluid';
        image.setAttribute('loading', 'lazy');

        const caption = document.createElement('div');
        caption.className = 'ldr-image-caption';
        caption.textContent = `Generated image (${ref})`;

        wrapper.appendChild(image);
        wrapper.appendChild(caption);
        // A pure node swap on wrapper, which was built above exclusively
        // with DOM APIs. Spelled replaceChild rather than the shorter
        // Node.replaceWith for scanner-sink consistency (alert 9657):
        // neither API parses HTML -- replaceWith stringifies a non-node
        // argument into a Text node -- so this is hygiene, not an
        // escaping fix.
        paragraph.parentNode.replaceChild(wrapper, paragraph);
    }

    return root.innerHTML;
}

/**
 * Create a dynamic favicon
 * @param {string} emoji - The emoji to use for the favicon
 */
function createDynamicFavicon(emoji = '⚡') {
    // Create a canvas element
    const canvas = document.createElement('canvas');
    canvas.width = 64;
    canvas.height = 64;

    // Get the 2D drawing context
    const ctx = canvas.getContext('2d');

    // Clear the canvas
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    // Set font
    ctx.font = '48px Arial';
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';

    // Draw the emoji
    ctx.fillText(emoji, 32, 32);

    // Convert to data URL
    const dataUrl = canvas.toDataURL('image/png');

    // Create or update the favicon link
    let link = document.querySelector('link[rel="icon"]');
    if (!link) {
        link = document.createElement('link');
        link.rel = 'icon';
        document.head.appendChild(link);
    }

    // Set the new favicon
    // Data URLs from canvas are safe for favicon use
    if (typeof URLValidator !== 'undefined' && URLValidator.safeAssign) {
        URLValidator.safeAssign(link, 'href', dataUrl);
    } else {
        link.href = dataUrl;
    }
}

/**
 * Update favicon based on status
 * @param {string} status - The research status
 */
function updateFavicon(status) {
    try {
        // Find favicon link or create it if it doesn't exist
        let link = document.querySelector("link[rel='icon']") ||
                document.querySelector("link[rel='shortcut icon']");

        if (!link) {
            SafeLogger.log('Favicon link not found, creating a new one');
            link = document.createElement('link');
            link.rel = 'icon';
            link.type = 'image/x-icon';
            document.head.appendChild(link);
        }

        // Create dynamic favicon using canvas
        const canvas = document.createElement('canvas');
        canvas.width = 32;
        canvas.height = 32;
        const ctx = canvas.getContext('2d');

        // Get theme colors from CSS variables
        const style = getComputedStyle(document.documentElement);
        const successColor = style.getPropertyValue('--success-color').trim() || '#28a745';
        const errorColor = style.getPropertyValue('--error-color').trim() || '#dc3545';
        const mutedColor = style.getPropertyValue('--text-muted').trim() || '#6c757d';
        const accentColor = style.getPropertyValue('--accent-primary').trim() || '#007bff';
        const bgPrimary = style.getPropertyValue('--bg-primary').trim() || '#ffffff';

        // Background color based on status
        let bgColor = accentColor; // Default accent

        if (ResearchStates.isCompleted(status)) {
            bgColor = successColor;
        } else if (ResearchStates.isFailed(status)) {
            bgColor = errorColor;
        } else if (ResearchStates.isCancelled(status)) {
            bgColor = mutedColor;
        }

        // Draw circle background
        ctx.fillStyle = bgColor;
        ctx.beginPath();
        ctx.arc(16, 16, 16, 0, 2 * Math.PI);
        ctx.fill();

        // Draw inner circle
        ctx.fillStyle = bgPrimary;
        ctx.beginPath();
        ctx.arc(16, 16, 10, 0, 2 * Math.PI);
        ctx.fill();

        // Draw letter R
        ctx.fillStyle = bgColor;
        ctx.font = 'bold 16px Arial';
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';
        ctx.fillText('R', 16, 16);

        // Set the favicon to the canvas data URL
        link.href = canvas.toDataURL('image/png');

        SafeLogger.log('Updated favicon to:', status);
    } catch (error) {
        SafeLogger.error('Error updating favicon:', error);
    }
}

/**
 * Show an alert message in a container on the page
 * @param {string} message - The message to display
 * @param {string} type - The alert type: success, error, warning, info
 * @param {boolean} skipIfToastShown - Whether to skip showing this alert if a toast was already shown
 */
function showAlert(message, type = 'info', skipIfToastShown = true) {
    // If we're showing a toast and we want to skip the regular alert, just return
    if (skipIfToastShown && window.ui && window.ui.showMessage) {
        return;
    }

    // Find the alert container - look for different possible alert containers
    let alertContainer = document.getElementById('filtered-settings-alert');

    // If not found, try other common alert containers
    if (!alertContainer) {
        alertContainer = document.getElementById('settings-alert');
    }

    if (!alertContainer) {
        alertContainer = document.getElementById('research-alert');
    }

    if (!alertContainer) return;

    // Clear any existing alerts
    alertContainer.innerHTML = '';

    // Escape message before including in HTML template
    const escapedMessage = (window.escapeHtml || escapeHtmlFallback)(message);

    // Create alert element
    const alert = document.createElement('div');
    const alertType = window.LdrAlertHelpers.mapAlertType(type);
    alert.className = `alert alert-${alertType}`;
    safeSetHTML(alert, `<i class="fas ${type === 'success' ? 'fa-check-circle' : 'fa-exclamation-circle'}"></i> ${escapedMessage}`);

    // Add a close button
    const closeBtn = document.createElement('button');
    closeBtn.className = 'ldr-alert-close';
    closeBtn.type = 'button';
    closeBtn.setAttribute('aria-label', 'Dismiss alert');
    closeBtn.textContent = '×';
    closeBtn.addEventListener('click', () => {
        alert.remove();
        alertContainer.style.display = 'none';
    });

    alert.appendChild(closeBtn);

    // Add to container
    alertContainer.appendChild(alert);
    alertContainer.style.display = 'block';

    // Auto-hide after configured duration
    setTimeout(() => {
        alert.remove();
        if (alertContainer.children.length === 0) {
            alertContainer.style.display = 'none';
        }
    }, UI_CONFIG.ALERT_DURATION_MS);
}

/**
 * Show an inline error message inside a container element
 * Uses DOM API exclusively (no innerHTML) for XSS safety.
 * @param {string|Element} container - The container ID or element
 * @param {string} message - The error message (used via textContent, inherently safe)
 * @param {Object} options - Options { dismissible: true }
 * @returns {HTMLElement|null} The created error element, or null if container not found
 */
function showInlineError(container, message, options = {}) {
    const containerEl = typeof container === 'string' ? document.getElementById(container) : container;
    if (!containerEl) {
        SafeLogger.warn('[ui.showInlineError] Container not found:', container);
        return null;
    }

    const { dismissible = true } = options || {};

    // Remove any existing inline error in this container
    clearInlineError(containerEl);

    // Build error element entirely via DOM API
    const errorEl = document.createElement('div');
    errorEl.className = 'ldr-inline-error';
    errorEl.setAttribute('role', 'alert');

    const icon = document.createElement('i');
    icon.className = 'fas fa-exclamation-circle';
    errorEl.appendChild(icon);

    const span = document.createElement('span');
    span.textContent = message;
    errorEl.appendChild(span);

    if (dismissible) {
        const closeBtn = document.createElement('button');
        closeBtn.type = 'button';
        closeBtn.className = 'ldr-inline-error-close';
        closeBtn.setAttribute('aria-label', 'Dismiss error');
        closeBtn.textContent = '\u00d7';
        closeBtn.addEventListener('click', () => {
            errorEl.remove();
        });
        errorEl.appendChild(closeBtn);
    }

    containerEl.appendChild(errorEl);
    return errorEl;
}

/**
 * Clear inline errors from a container
 * @param {string|Element} container - The container ID or element
 */
function clearInlineError(container) {
    const containerEl = typeof container === 'string' ? document.getElementById(container) : container;
    if (containerEl) {
        const errors = containerEl.querySelectorAll('.ldr-inline-error');
        errors.forEach(el => el.remove());
    }
}

// Add CSS for alert and notification styles
function addAlertStyles() {
    if (document.getElementById('alert-styles')) return;

    const styleEl = document.createElement('style');
    styleEl.nonce = window.LDRTemplateEvents?.styleNonce || '';
    styleEl.id = 'alert-styles';
    styleEl.textContent = `
        .alert {
            padding: 12px 16px;
            margin-bottom: 1rem;
            border-radius: 8px;
            display: flex;
            align-items: center;
            position: relative;
        }

        .alert i {
            margin-right: 12px;
            font-size: 1.2rem;
        }

        .alert-success {
            background-color: rgba(var(--success-color-rgb), 0.15);
            color: var(--success-color);
            border-left: 4px solid var(--success-color);
        }

        .alert-danger {
            background-color: rgba(var(--error-color-rgb), 0.15);
            color: var(--error-color);
            border-left: 4px solid var(--error-color);
        }

        .alert-info {
            background-color: rgba(var(--accent-tertiary-rgb), 0.15);
            color: var(--accent-tertiary);
            border-left: 4px solid var(--accent-tertiary);
        }

        .alert-warning {
            background-color: rgba(var(--warning-color-rgb), 0.15);
            color: var(--warning-color);
            border-left: 4px solid var(--warning-color);
        }

        .alert-close {
            position: absolute;
            right: 10px;
            top: 8px;
            font-size: 1.2rem;
            font-weight: bold;
            cursor: pointer;
            opacity: 0.7;
        }

        .alert-close:hover {
            opacity: 1;
        }

        /* Notification banner styles */
        .ldr-notification-banner {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            padding: 14px 20px;
            background: var(--bg-secondary);
            color: var(--text-primary);
            text-align: center;
            z-index: ${UI_CONFIG.NOTIFICATION_Z_INDEX};
            font-weight: 500;
            font-size: 14px;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 12px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4), 0 0 15px rgba(var(--accent-primary-rgb), 0.15);
            transform: translateY(-100%);
            transition: transform 0.3s ease;
            word-wrap: break-word;
            max-width: 100%;
        }
    `;

    document.head.appendChild(styleEl);
}

// Add alert styles when the script loads
addAlertStyles();

// Export the UI functions
window.ui = {
    updateProgressBar,
    showSpinner,
    hideSpinner,
    showError,
    showMessage,
    renderMarkdown,
    createDynamicFavicon,
    updateFavicon,
    showAlert,
    showInlineError,
    clearInlineError,
    // Exposed for testing and debugging (see window.progressComponent for
    // the same pattern in components/progress.js): the KaTeX inline-style
    // hardening pass has no other reachable entry point from outside this
    // file (it runs as a private step inside renderMarkdown), and its
    // rgb()/rgba() color handling is specifically the shape happy-dom
    // cannot exercise on its own -- a real browser serializes a hex color
    // read back from CSSOM as rgb()/rgba(), happy-dom does not, so tests
    // must be able to call these directly with an explicitly-constructed
    // rgb() value.
    isSafeKatexColor,
    getSafeKatexInlineStyle
};