All files / packages/theme-selector/src theme-loader.ts

94% Statements 94/100
87.87% Branches 29/33
100% Functions 17/17
93.93% Lines 93/99

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284                                        744x 744x 744x                     65x 65x     65x     32x           32x 1x 1x     31x   31x 65x     65x         28x   3x                           89x               26x             44x 44x                     44x 44x 1x 1x     44x 34x 34x 34x     44x 9x 9x 9x                 2x 2x 1x 1x     1x               35x 5x   35x 3x       35x   35x 35x               35x 26x 26x 2x                                               7x 7x           7x 7x 7x       7x 1x 1x     6x 6x 6x       3x 3x 3x 2x   1x   3x 3x     3x 3x                             46x 46x   46x   1x 1x       45x 45x 7x     38x 38x 38x 38x 38x   38x 38x   3x 3x     35x   35x 35x     5x 5x 5x       30x 30x    
// SPDX-License-Identifier: MIT
/**
 * Theme CSS loading utilities
 */
 
import { resolveThemeAppearance } from './appearance.js';
import { CSS_LINK_ID, DOM_SELECTORS } from './constants.js';
import { ThemeErrors, logThemeError } from './errors.js';
 
export interface ThemeInfo {
  id: string;
  cssFile: string;
  icon?: string | undefined;
}
 
/**
 * Resolves an asset path relative to the site's base URL.
 */
export function resolveAssetPath(assetPath: string, baseUrl: string): string {
  // Normalize baseUrl - remove trailing slash if present, then add one
  const normalizedBase = baseUrl.replace(/\/$/, '');
  const base = normalizedBase ? `${window.location.origin}${normalizedBase}/` : `${window.location.origin}/`;
  return new URL(assetPath, base).pathname;
}
 
/**
 * Gets the base URL from the document's data-baseurl attribute.
 * Validates the URL to prevent injection attacks:
 * - Rejects protocol-relative URLs (//example.com)
 * - Rejects non-HTTPS absolute URLs (except localhost)
 * - Only allows same-origin or relative paths
 */
export function getBaseUrl(doc: Document): string {
  const baseElement = doc.documentElement;
  const raw = baseElement?.getAttribute('data-baseurl') || '';
 
  // Empty base URL is valid (use site root)
  if (!raw) return '';
 
  // Reject protocol-relative URLs (security risk)
  Iif (raw.startsWith('//')) {
    logThemeError(ThemeErrors.PROTOCOL_REJECTED());
    return '';
  }
 
  // Reject non-HTTPS absolute URLs (except localhost for development)
  if (raw.startsWith('http://') && !raw.startsWith('http://localhost')) {
    logThemeError(ThemeErrors.INSECURE_HTTP_REJECTED());
    return '';
  }
 
  try {
    // Parse relative to current origin to validate
    const currentOrigin = typeof window !== 'undefined' ? window.location.origin : 'http://localhost';
    const u = new URL(raw, currentOrigin);
 
    // Only allow same-origin URLs or relative paths
    Iif (u.origin !== currentOrigin) {
      logThemeError(ThemeErrors.CROSS_ORIGIN_REJECTED(u.origin));
      return '';
    }
 
    return u.pathname.replace(/\/$/, '');
  } catch {
    return '';
  }
}
 
/**
 * Builds the element ID of a theme's stylesheet link.
 *
 * Single source of truth for the `theme-<id>-css` convention shared by
 * the loader and the lazy-CSS helpers.
 *
 * @param themeId - Theme whose stylesheet link ID to build
 * @returns The stylesheet link element ID
 */
export function themeLinkId(themeId: string): string {
  return `theme-${themeId}-css`;
}
 
/**
 * Extracts the theme ID from a theme link element's ID.
 * Strips a leading "theme-" prefix and trailing "-css" suffix.
 */
function extractThemeIdFromLinkId(linkId: string): string {
  return linkId.replace(/^theme-/, '').replace(/-css$/, '');
}
 
/**
 * Clears onload/onerror handlers from a link element to prevent memory leaks.
 */
function clearLinkHandlers(link: HTMLLinkElement): void {
  link.onload = null;
  link.onerror = null;
}
 
/**
 * Loads a CSS file with a timeout, returning a promise that resolves when loaded.
 */
export function loadCSSWithTimeout(
  link: HTMLLinkElement,
  themeId: string,
  timeoutMs = 10000
): Promise<void> {
  return new Promise((resolve, reject) => {
    const timeoutId = setTimeout(() => {
      clearLinkHandlers(link);
      reject(new Error(`Theme ${themeId} load timeout`));
    }, timeoutMs);
 
    link.onload = () => {
      clearTimeout(timeoutId);
      clearLinkHandlers(link);
      resolve();
    };
 
    link.onerror = () => {
      clearTimeout(timeoutId);
      clearLinkHandlers(link);
      reject(new Error(`Failed to load theme ${themeId}`));
    };
  });
}
 
/**
 * Gets the current theme from document element classes
 */
export function getCurrentThemeFromClasses(element: HTMLElement): string | null {
  const classList = Array.from(element.classList);
  for (const className of classList) {
    Eif (className.startsWith('theme-')) {
      return className.substring(6); // Remove 'theme-' prefix
    }
  }
  return null;
}
 
/**
 * Applies theme class and root data attributes to the document element.
 */
export function applyThemeClass(doc: Document, themeId: string): void {
  // Remove existing theme classes in a single batch operation
  const themeClasses = Array.from(doc.documentElement.classList).filter((className) =>
    className.startsWith('theme-'),
  );
  if (themeClasses.length > 0) {
    doc.documentElement.classList.remove(...themeClasses);
  }
 
  // Add the new theme class
  doc.documentElement.classList.add(`theme-${themeId}`);
 
  doc.documentElement.setAttribute('data-theme', themeId);
  doc.documentElement.setAttribute('data-appearance', resolveThemeAppearance(themeId));
}
 
/**
 * Removes stale theme stylesheet links, keeping the given theme's link
 * and the shared base stylesheet.
 */
function removeStaleThemeLinks(doc: Document, keepThemeId: string): void {
  doc.querySelectorAll(DOM_SELECTORS.THEME_CSS_LINKS).forEach((link) => {
    const linkThemeId = extractThemeIdFromLinkId(link.id);
    if (linkThemeId !== keepThemeId && linkThemeId !== 'base') {
      link.remove();
    }
  });
}
 
/**
 * Adopts the blocking script's link element for a theme (prevents
 * duplicate CSS loads).
 *
 * The repoint stays synchronous to preserve the blocking script's
 * FOUC-avoidance behaviour; this only makes its outcome observable by
 * awaiting the repointed link's load/error settlement. On load failure
 * the link is rolled back to its previous identity and href so the
 * document keeps the stylesheet that was already working.
 *
 * @returns true when the theme's CSS is confirmed loaded
 */
async function adoptBlockingLink(
  doc: Document,
  blockingLink: HTMLLinkElement,
  theme: ThemeInfo,
  baseUrl: string
): Promise<boolean> {
  let resolvedHref: string;
  try {
    resolvedHref = resolveAssetPath(theme.cssFile, baseUrl);
  } catch {
    logThemeError(ThemeErrors.INVALID_CSS_PATH(theme.id));
    return false;
  }
 
  const previousHref = blockingLink.getAttribute('href');
  blockingLink.id = themeLinkId(theme.id);
  blockingLink.setAttribute('data-theme-id', theme.id);
 
  // Already pointing at the theme's stylesheet (e.g. the blocking script
  // loaded it before first paint) — no fetch to await.
  if (previousHref === resolvedHref) {
    removeStaleThemeLinks(doc, theme.id);
    return true;
  }
 
  blockingLink.href = resolvedHref;
  try {
    await loadCSSWithTimeout(blockingLink, theme.id);
  } catch (error) {
    // Loading failed — restore the link's prior identity and href so the
    // previously working stylesheet stays in effect.
    blockingLink.id = CSS_LINK_ID;
    blockingLink.removeAttribute('data-theme-id');
    if (previousHref !== null) {
      blockingLink.href = previousHref;
    } else {
      blockingLink.removeAttribute('href');
    }
    logThemeError(ThemeErrors.CSS_LOAD_FAILED(theme.id, error));
    return false;
  }
 
  removeStaleThemeLinks(doc, theme.id);
  return true;
}
 
/**
 * Loads theme CSS file if not already loaded.
 *
 * @returns true when the theme's stylesheet is present and confirmed
 *   loaded (or was already linked), false when path resolution or the
 *   network fetch failed
 */
export async function loadThemeCSS(
  doc: Document,
  theme: ThemeInfo,
  baseUrl: string
): Promise<boolean> {
  const linkId = themeLinkId(theme.id);
  const existingThemeLink = doc.getElementById(linkId) as HTMLLinkElement | null;
 
  if (existingThemeLink) {
    // Link already exists — clean up any other stale theme links
    removeStaleThemeLinks(doc, theme.id);
    return true;
  }
 
  // Adopt the blocking script's link element if present (prevents duplicate CSS loads)
  const blockingLink = doc.getElementById(CSS_LINK_ID) as HTMLLinkElement | null;
  if (blockingLink) {
    return adoptBlockingLink(doc, blockingLink, theme, baseUrl);
  }
 
  const themeLink = doc.createElement('link');
  themeLink.id = linkId;
  themeLink.rel = 'stylesheet';
  themeLink.type = 'text/css';
  themeLink.setAttribute('data-theme-id', theme.id);
 
  try {
    themeLink.href = resolveAssetPath(theme.cssFile, baseUrl);
  } catch {
    logThemeError(ThemeErrors.INVALID_CSS_PATH(theme.id));
    return false;
  }
 
  doc.head.appendChild(themeLink);
 
  try {
    await loadCSSWithTimeout(themeLink, theme.id);
  } catch (error) {
    // Loading failed — remove the new link and keep prior theme intact
    themeLink.remove();
    logThemeError(ThemeErrors.CSS_LOAD_FAILED(theme.id, error));
    return false;
  }
 
  // Only remove old theme links after successful load
  removeStaleThemeLinks(doc, theme.id);
  return true;
}