import type {} from '../../../types/globals'; /* Svelte HMR update handler */ import { SVELTE_CSS_LOAD_TIMEOUT_MS } from '../constants'; import { saveDOMState, restoreDOMState, saveScrollState, restoreScrollState } from '../domState'; import { detectCurrentFramework, findIndexPath } from '../frameworkDetect'; type SvelteHmrWindow = Window & { __SVELTE_HMR_ACCEPT__?: Record void>; }; /* Swap a stylesheet link by matching cssBaseName or framework name */ const swapStylesheet = ( cssUrl: string, cssBaseName: string, framework: string ) => { let existingLink: HTMLLinkElement | null = null; document .querySelectorAll('link[rel="stylesheet"]') .forEach((link) => { const href = link.getAttribute('href') ?? ''; if (href.includes(cssBaseName) || href.includes(framework)) { existingLink = link; } }); if (!existingLink) { return; } const capturedExisting: HTMLLinkElement = existingLink; const newLink = document.createElement('link'); newLink.rel = 'stylesheet'; newLink.href = `${cssUrl}?t=${Date.now()}`; newLink.onload = () => { if (capturedExisting && capturedExisting.parentNode) { capturedExisting.remove(); } }; document.head.appendChild(newLink); }; const extractCountFromDOM = () => { const countButton = document.querySelector('button'); if (!countButton || !countButton.textContent) { return {}; } const countMatch = countButton.textContent.match(/(\d+)/); if (!countMatch) { return {}; } return { initialCount: parseInt(countMatch[1] ?? '0', 10) }; }; const loadStateFromSession = () => { try { const stored = sessionStorage.getItem('__SVELTE_HMR_STATE__'); if (!stored) { return {}; } const parsed: Record = JSON.parse(stored); if (parsed && Object.keys(parsed).length > 0) { return parsed; } return {}; } catch { return {}; } }; const saveStateToSession = (preservedState: Record) => { if (Object.keys(preservedState).length === 0) { return; } try { sessionStorage.setItem( '__SVELTE_HMR_STATE__', JSON.stringify(preservedState) ); } catch { /* ignore */ } }; const collectCssRules = (sheet: CSSStyleSheet) => { let rules = ''; for (let idx = 0; idx < sheet.cssRules.length; idx++) { const rule = sheet.cssRules[idx]; if (!rule) continue; rules += `${rule.cssText}\n`; } return rules; }; const preserveLinkAsInlineStyle = (link: HTMLLinkElement) => { try { const { sheet } = link; if (!sheet || sheet.cssRules.length === 0) { return null; } const style = document.createElement('style'); style.dataset.hmrPreserved = 'true'; style.textContent = collectCssRules(sheet); document.head.appendChild(style); return style; } catch { /* Cross-origin sheets (e.g. Google Fonts) — clone as fallback */ const clone = document.createElement('link'); clone.rel = link.rel; clone.href = link.href; clone.dataset.hmrPreserved = 'true'; document.head.appendChild(clone); return null; } }; const preserveAllStylesheets = () => { const preservedStyles: HTMLStyleElement[] = []; document .querySelectorAll('head link[rel="stylesheet"]') .forEach((link) => { const style = preserveLinkAsInlineStyle(link); if (style) { preservedStyles.push(style); } }); /* Also preserve Svelte injected