All files index.ts

92% Statements 253/275
88.48% Branches 123/139
100% Functions 35/35
92.19% Lines 248/269

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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561                  4x                                                                                                                 4x 4x     61x 61x 61x 61x   61x   4x         93x 24x 24x   24x   21x 21x 21x 21x           24x     24x   24x   20x 1x     20x   20x 20x 20x 20x 20x       20x 20x 20x 20x 20x                                           24x 24x 2x 1x 1x   1x 1x           19x 19x       12x 12x     12x 12x 12x 7x 7x 6x 6x   6x 6x   6x 6x 4x                 12x 7x 1x 1x   1x 1x           12x 4x 4x   4x 1x         12x 12x 5x 5x 5x 10x     5x 4x   1x                       4x           37x 37x 37x 37x 37x   37x 1x   1x         36x 36x       36x         37x 324x 324x 324x 324x 324x 324x 324x 324x 324x 324x 12x     324x 324x 324x 324x 324x 324x       324x 324x 324x 324x 324x                       324x 324x 324x 324x 324x 324x 324x 324x 324x 324x 324x 324x   324x 6x 6x 6x 6x     324x 324x       37x 59x       37x 14x   14x     14x 126x       14x 14x 14x     37x 1x 1x 1x     37x 12x 12x 12x 12x 108x   12x 12x   10x         37x     1x           37x       2x 2x             37x 6x 6x 6x 5x 5x 45x 45x   1x     1x         37x     1x 1x           37x     3x   2x             37x     1x 1x             37x     15x 15x 5x 5x 5x   1x     4x   10x 5x 5x 2x 2x 2x     3x 1x   2x 2x     5x 4x 4x 2x 2x   2x     2x 2x 2x               37x 324x     7x 7x 1x 1x 1x 6x 1x 1x 1x 5x 1x 1x 4x 2x 2x 2x 1x 1x 1x 1x 1x               37x   37x   2x           4x 4x 2x 2x 2x         4x 4x 2x 2x 2x 2x 2x     2x 1x 1x   2x   2x   1x        
export type ThemeMode = 'theme';
 
interface ThemeFlavor {
  id: string;
  name: string;
  cssFile: string;
  icon?: string;
}
 
const THEMES: ThemeFlavor[] = [
  {
    id: 'bulma-light',
    name: 'Bulma Light',
    cssFile: 'assets/css/themes/bulma-light.css',
    icon: 'assets/img/bulma-logo.png',
  },
  {
    id: 'bulma-dark',
    name: 'Bulma Dark',
    cssFile: 'assets/css/themes/bulma-dark.css',
    icon: 'assets/img/bulma-logo-dark.png',
  },
  {
    id: 'catppuccin-latte',
    name: 'Catppuccin Latte',
    cssFile: 'assets/css/themes/catppuccin-latte.css',
    icon: 'assets/img/catppuccin-logo-latte.png',
  },
  {
    id: 'catppuccin-frappe',
    name: 'Catppuccin Frappé',
    cssFile: 'assets/css/themes/catppuccin-frappe.css',
    icon: 'assets/img/catppuccin-logo-latte.png',
  },
  {
    id: 'catppuccin-macchiato',
    name: 'Catppuccin Macchiato',
    cssFile: 'assets/css/themes/catppuccin-macchiato.css',
    icon: 'assets/img/catppuccin-logo-macchiato.png',
  },
  {
    id: 'catppuccin-mocha',
    name: 'Catppuccin Mocha',
    cssFile: 'assets/css/themes/catppuccin-mocha.css',
    icon: 'assets/img/catppuccin-logo-macchiato.png',
  },
  {
    id: 'dracula',
    name: 'Dracula',
    cssFile: 'assets/css/themes/dracula.css',
    icon: 'assets/img/dracula-logo.png',
  },
  {
    id: 'github-light',
    name: 'GitHub Light',
    cssFile: 'assets/css/themes/github-light.css',
    icon: 'assets/img/github-logo-light.png',
  },
  {
    id: 'github-dark',
    name: 'GitHub Dark',
    cssFile: 'assets/css/themes/github-dark.css',
    icon: 'assets/img/github-logo-dark.png',
  },
];
 
const STORAGE_KEY = 'bulma-theme-flavor';
const DEFAULT_THEME = 'catppuccin-mocha';
 
function getBaseUrl(doc: Document): string {
  const baseElement = doc.documentElement;
  const raw = baseElement?.getAttribute('data-baseurl') || '';
  try {
    const u = new URL(raw, 'http://localhost');
    // Only allow same-origin relative paths; strip origin used for parsing
    return u.origin === 'http://localhost' ? u.pathname.replace(/\/$/, '') : '';
  } catch {
    return '';
  }
}
 
function applyTheme(doc: Document, themeId: string): void {
  const theme = THEMES.find((t) => t.id === themeId) || THEMES.find((t) => t.id === DEFAULT_THEME)!;
  const baseUrl = getBaseUrl(doc);
  const flavorLink = doc.getElementById('theme-flavor-css') as HTMLLinkElement | null;
 
  if (flavorLink) {
    // Build a safe URL relative to base by prepending baseUrl to relative path
    try {
      const fullPath = baseUrl ? `${baseUrl}/${theme.cssFile}` : theme.cssFile;
      const url = new URL(fullPath, 'http://localhost');
      flavorLink.href = url.pathname;
    } catch {
      // Ignore invalid URL
    }
  }
 
  doc.documentElement.setAttribute('data-flavor', themeId);
 
  // Update trigger button icon
  const triggerIcon = doc.getElementById('theme-flavor-trigger-icon');
 
  if (triggerIcon) {
    // Clear existing content first
    while (triggerIcon.firstChild) {
      triggerIcon.removeChild(triggerIcon.firstChild);
    }
 
    if (theme.icon) {
      // Create and append img element (CSP-friendly)
      const img = doc.createElement('img');
      try {
        const fullPath = baseUrl ? `${baseUrl}/${theme.icon}` : theme.icon;
        const url = new URL(fullPath, 'http://localhost');
        img.src = url.pathname;
      } catch {
        // Ignore invalid URL
      }
      img.alt = theme.name;
      img.title = theme.name; // Tooltip on hover
      img.width = 28;
      img.height = 28;
      triggerIcon.appendChild(img);
    } else E{
      // Fallback: show first two letters with circular background
      const span = doc.createElement('span');
      span.textContent = theme.name.substring(0, 2).toUpperCase();
      span.style.fontSize = '12px';
      span.style.fontWeight = 'bold';
      span.style.color = 'var(--theme-text, inherit)';
      span.style.display = 'flex';
      span.style.alignItems = 'center';
      span.style.justifyContent = 'center';
      span.style.width = '28px';
      span.style.height = '28px';
      span.style.borderRadius = '50%';
      span.style.backgroundColor = 'var(--theme-surface-1, #f5f5f5)';
      span.style.border = '1px solid var(--theme-border, #ddd)';
      span.title = theme.name; // Tooltip on hover
      triggerIcon.appendChild(span);
    }
  }
 
  // Update active state in dropdown
  const dropdownItems = doc.querySelectorAll('#theme-flavor-items .dropdown-item');
  dropdownItems.forEach((item) => {
    if (item.getAttribute('data-theme-id') === themeId) {
      item.classList.add('is-active');
      item.setAttribute('aria-checked', 'true');
    } else {
      item.classList.remove('is-active');
      item.setAttribute('aria-checked', 'false');
    }
  });
}
 
export function initTheme(documentObj: Document, windowObj: Window): void {
  const savedTheme = windowObj.localStorage.getItem(STORAGE_KEY) || DEFAULT_THEME;
  applyTheme(documentObj, savedTheme);
}
 
export function initNavbar(documentObj: Document): void {
  const currentPath = documentObj.location.pathname;
  const navbarItems = documentObj.querySelectorAll('.navbar-item');
 
  // Find the matching link first
  let matchingItem: Element | null = null;
  const checkedItems = new Set<Element>();
  navbarItems.forEach((item) => {
    const link = item as HTMLAnchorElement;
    if (link.href) {
      try {
        const linkPath = new URL(link.href).pathname;
        // Remove trailing slashes for comparison
        const normalizedCurrentPath = currentPath.replace(/\/$/, '') || '/';
        const normalizedLinkPath = linkPath.replace(/\/$/, '') || '/';
 
        checkedItems.add(item);
        if (normalizedCurrentPath === normalizedLinkPath) {
          matchingItem = item;
        }
      } catch {
        // Ignore invalid URLs - don't add to checkedItems
      }
    }
  });
 
  // Clear all active states except the matching one (only for items that were checked)
  navbarItems.forEach((item) => {
    if (item !== matchingItem && checkedItems.has(item)) {
      item.classList.remove('is-active');
      const link = item as HTMLAnchorElement;
      // Check if removeAttribute exists (for test mocks that might not have it)
      Eif (link && 'removeAttribute' in link && typeof link.removeAttribute === 'function') {
        link.removeAttribute('aria-current');
      }
    }
  });
 
  // Set active state for the matching link
  if (matchingItem) {
    (matchingItem as HTMLElement).classList.add('is-active');
    const link = matchingItem as HTMLAnchorElement;
    // Check if setAttribute exists (for test mocks that might not have it)
    if (link && 'setAttribute' in link && typeof link.setAttribute === 'function') {
      link.setAttribute('aria-current', 'page');
    }
  }
 
  // Handle Reports dropdown highlighting
  const reportsLink = documentObj.querySelector<HTMLElement>('[data-testid="nav-reports"]');
  if (reportsLink) {
    const reportPaths = ['/coverage', '/playwright', '/lighthouse'];
    const normalizedCurrentPath = currentPath.replace(/\/$/, '') || '/';
    const isOnReportsPage = reportPaths.some(
      (path) => normalizedCurrentPath === path || normalizedCurrentPath.startsWith(path + '/')
    );
 
    if (isOnReportsPage) {
      reportsLink.classList.add('is-active');
    } else {
      reportsLink.classList.remove('is-active');
    }
  }
}
 
// Expose functions to global scope for use in HTML
declare global {
  interface Window {
    initNavbar: typeof initNavbar;
  }
}
 
window.initNavbar = initNavbar;
 
export function wireFlavorSelector(
  documentObj: Document,
  windowObj: Window
): { cleanup: () => void } {
  const abortController = new AbortController();
  const baseUrl = getBaseUrl(documentObj);
  const dropdownContent = documentObj.getElementById('theme-flavor-items');
  const trigger = documentObj.querySelector<HTMLButtonElement>('.theme-flavor-trigger');
  const dropdown = documentObj.getElementById('theme-flavor-dd');
 
  if (!dropdownContent || !trigger || !dropdown) {
    return {
      cleanup: () => {
        abortController.abort();
      },
    };
  }
 
  let currentIndex = -1;
  const menuItems: HTMLElement[] = [];
 
  // Get current theme to set initial aria-checked state
  const currentThemeId =
    windowObj.localStorage.getItem(STORAGE_KEY) ||
    documentObj.documentElement.getAttribute('data-flavor') ||
    DEFAULT_THEME;
 
  // Populate dropdown with theme options
  THEMES.forEach((theme) => {
    const item = documentObj.createElement('a');
    item.href = '#';
    item.className = 'dropdown-item';
    item.setAttribute('data-theme-id', theme.id);
    item.setAttribute('role', 'menuitemradio');
    item.setAttribute('aria-label', theme.name);
    item.setAttribute('tabindex', '-1');
    const isActive = theme.id === currentThemeId;
    item.setAttribute('aria-checked', String(isActive));
    if (isActive) {
      item.classList.add('is-active');
    }
 
    if (theme.icon) {
      const img = documentObj.createElement('img');
      try {
        const fullPath = baseUrl ? `${baseUrl}/${theme.icon}` : theme.icon;
        const url = new URL(fullPath, 'http://localhost');
        img.src = url.pathname;
      } catch {
        // Ignore invalid URL
      }
      img.alt = theme.name;
      img.title = theme.name;
      img.width = 28;
      img.height = 28;
      item.appendChild(img);
    } else E{
      // Fallback: show first two letters with styled background
      const span = documentObj.createElement('span');
      span.textContent = theme.name.substring(0, 2);
      span.style.fontSize = '14px';
      span.style.fontWeight = 'bold';
      span.style.color = 'var(--theme-text, inherit)';
      item.appendChild(span);
    }
 
    // Always include a visually hidden full name for screen readers
    const srOnly = documentObj.createElement('span');
    srOnly.textContent = theme.name;
    srOnly.style.position = 'absolute';
    srOnly.style.width = '1px';
    srOnly.style.height = '1px';
    srOnly.style.padding = '0';
    srOnly.style.margin = '-1px';
    srOnly.style.overflow = 'hidden';
    srOnly.style.clip = 'rect(0, 0, 0, 0)';
    srOnly.style.whiteSpace = 'nowrap';
    srOnly.style.border = '0';
    item.appendChild(srOnly);
 
    item.addEventListener('click', (e) => {
      e.preventDefault();
      applyTheme(documentObj, theme.id);
      windowObj.localStorage.setItem(STORAGE_KEY, theme.id);
      closeDropdown({ restoreFocus: true });
    });
 
    menuItems.push(item);
    dropdownContent.appendChild(item);
  });
 
  // Update aria-expanded on trigger
  const updateAriaExpanded = (expanded: boolean) => {
    trigger.setAttribute('aria-expanded', String(expanded));
  };
 
  // Focus management
  const focusMenuItem = (index: number) => {
    Iif (index < 0 || index >= menuItems.length) return;
 
    const item = menuItems[index]!;
 
    // Set tabindex to -1 for all items
    menuItems.forEach((menuItem) => {
      menuItem.setAttribute('tabindex', '-1');
    });
 
    // Focus and set tabindex to 0 on current item
    item.setAttribute('tabindex', '0');
    item.focus();
    currentIndex = index;
  };
 
  const openDropdown = () => {
    dropdown.classList.add('is-active');
    updateAriaExpanded(true);
    currentIndex = -1;
  };
 
  const closeDropdown = (options: { restoreFocus?: boolean } = {}): void => {
    const { restoreFocus = true } = options;
    dropdown.classList.remove('is-active');
    updateAriaExpanded(false);
    menuItems.forEach((menuItem) => {
      menuItem.setAttribute('tabindex', '-1');
    });
    currentIndex = -1;
    if (restoreFocus) {
      // Only restore focus to trigger when explicitly requested (e.g., selection or Esc)
      trigger.focus();
    }
  };
 
  // Open dropdown on hover
  dropdown.addEventListener(
    'mouseenter',
    () => {
      openDropdown();
    },
    { signal: abortController.signal }
  );
 
  // Close dropdown when mouse leaves
  dropdown.addEventListener(
    'mouseleave',
    () => {
      // Only close if not keyboard navigating
      Eif (currentIndex === -1) {
        closeDropdown();
      }
    },
    { signal: abortController.signal }
  );
 
  // Toggle dropdown helper function
  const toggleDropdown = (focusFirst = false) => {
    const isActive = dropdown.classList.toggle('is-active');
    updateAriaExpanded(isActive);
    if (!isActive) {
      currentIndex = -1;
      menuItems.forEach((menuItem) => {
        menuItem.setAttribute('tabindex', '-1');
        menuItem.setAttribute('aria-checked', String(menuItem.classList.contains('is-active')));
      });
    E} else if (focusFirst && menuItems.length > 0) {
      // ...rest of the existing logic
      // When opening via keyboard, focus first item
      focusMenuItem(0);
    }
  };
 
  // Toggle dropdown on trigger click (for touch devices)
  trigger.addEventListener(
    'click',
    (e) => {
      e.preventDefault();
      toggleDropdown();
    },
    { signal: abortController.signal }
  );
 
  // Close dropdown when clicking outside
  documentObj.addEventListener(
    'click',
    (e: MouseEvent) => {
      if (!dropdown.contains(e.target as Node)) {
        // Close on any outside click; do not steal focus from the newly clicked element
        closeDropdown({ restoreFocus: false });
      }
    },
    { signal: abortController.signal }
  );
 
  // Handle Escape key globally to close dropdown
  documentObj.addEventListener(
    'keydown',
    (e: KeyboardEvent) => {
      Eif (e.key === 'Escape' && dropdown.classList.contains('is-active')) {
        closeDropdown({ restoreFocus: true });
      }
    },
    { signal: abortController.signal }
  );
 
  // Keyboard navigation
  trigger.addEventListener(
    'keydown',
    (e: KeyboardEvent) => {
      const key = e.key;
      if (key === 'Enter' || key === ' ') {
        e.preventDefault();
        const wasActive = dropdown.classList.contains('is-active');
        if (wasActive) {
          // If already open, close it
          toggleDropdown(false);
        } else {
          // If closed, open and focus first item
          toggleDropdown(true);
        }
      } else if (key === 'ArrowDown') {
        e.preventDefault();
        if (!dropdown.classList.contains('is-active')) {
          dropdown.classList.add('is-active');
          updateAriaExpanded(true);
          focusMenuItem(0); // Focus first item when opening
        } else {
          // If currentIndex is -1 (dropdown opened via mouse or not yet initialized), focus first item
          if (currentIndex < 0) {
            focusMenuItem(0);
          } else {
            const nextIndex = currentIndex < menuItems.length - 1 ? currentIndex + 1 : 0;
            focusMenuItem(nextIndex);
          }
        }
      } else if (key === 'ArrowUp') {
        e.preventDefault();
        if (!dropdown.classList.contains('is-active')) {
          dropdown.classList.add('is-active');
          updateAriaExpanded(true);
          // Start from last item when opening with ArrowUp
          focusMenuItem(menuItems.length - 1);
        } else {
          // If currentIndex is -1 (dropdown opened via mouse), start from last
          const startIndex = currentIndex < 0 ? menuItems.length - 1 : currentIndex;
          const prevIndex = startIndex > 0 ? startIndex - 1 : menuItems.length - 1;
          focusMenuItem(prevIndex);
        }
      }
    },
    { signal: abortController.signal }
  );
 
  // Keyboard navigation on menu items
  menuItems.forEach((item, index) => {
    item.addEventListener(
      'keydown',
      (e: KeyboardEvent) => {
        const key = e.key;
        if (key === 'ArrowDown') {
          e.preventDefault();
          const nextIndex = index < menuItems.length - 1 ? index + 1 : 0;
          focusMenuItem(nextIndex);
        } else if (key === 'ArrowUp') {
          e.preventDefault();
          const prevIndex = index > 0 ? index - 1 : menuItems.length - 1;
          focusMenuItem(prevIndex);
        } else if (key === 'Escape') {
          e.preventDefault();
          closeDropdown();
        } else if (key === 'Enter' || key === ' ') {
          e.preventDefault();
          item.click();
        } else if (key === 'Home') {
          e.preventDefault();
          focusMenuItem(0);
        E} else if (key === 'End') {
          e.preventDefault();
          focusMenuItem(menuItems.length - 1);
        }
      },
      { signal: abortController.signal }
    );
  });
 
  // Initialize aria-expanded
  updateAriaExpanded(false);
 
  return {
    cleanup: () => {
      abortController.abort();
    },
  };
}
 
export function enhanceAccessibility(documentObj: Document): void {
  const pres = documentObj.querySelectorAll('.highlight > pre');
  pres.forEach((pre) => {
    if (!pre.hasAttribute('tabindex')) pre.setAttribute('tabindex', '0');
    if (!pre.hasAttribute('role')) pre.setAttribute('role', 'region');
    if (!pre.hasAttribute('aria-label')) pre.setAttribute('aria-label', 'Code block');
  });
}
 
// Auto-initialize on DOMContentLoaded
Eif (typeof document !== 'undefined' && typeof window !== 'undefined') {
  document.addEventListener('DOMContentLoaded', () => {
    console.warn('Theme switcher initializing...');
    try {
      initTheme(document, window);
      const { cleanup } = wireFlavorSelector(document, window);
      enhanceAccessibility(document);
 
      // Register cleanup to run on teardown
      const pagehideHandler = () => {
        cleanup();
        window.removeEventListener('pagehide', pagehideHandler);
      };
      window.addEventListener('pagehide', pagehideHandler);
 
      console.warn('Theme switcher initialized successfully');
    } catch (error) {
      console.error('Theme switcher initialization failed:', error);
    }
  });
}