import { onMounted, onBeforeUnmount } from "vue"; import { IMAGE_REPAIR_PATTERN, IMAGE_REPAIR_PATTERN_ENCODED, IMAGE_REPAIR_INLINE_SCRIPT, findRepairTarget } from "../utils/image/imageRepairInlineScript.js"; // Re-exported from the pure module so existing callers keep working. // New callers (server/index.ts splice, future iframe-injection // surfaces) should import from `../utils/image/imageRepairInlineScript.js` // directly to avoid pulling Vue lifecycle hooks into Node code paths. export { IMAGE_REPAIR_PATTERN, IMAGE_REPAIR_PATTERN_ENCODED, IMAGE_REPAIR_INLINE_SCRIPT, findRepairTarget }; // Whitespace- and comma-bounded URL token inside a `srcset` value. // `srcset` is a comma-list of ` [descriptor]` entries; the // regex picks each non-whitespace, non-comma run so the descriptor // (`1x`, `2x`, `100w`, …) survives the repair pass untouched. const SRCSET_TOKEN_RE = /[^\s,]+/g; // Pure srcset transform — runs `findRepairTarget` per URL token and // returns the rewritten srcset string. Descriptors (`1x`, `2x`, // `100w`, …) survive untouched because the token regex only matches // non-whitespace, non-comma runs. Extracted to keep `repairSourceSrc` // under the 20-line cap and to make the per-token logic independently // unit-testable. (CodeRabbit iter-1 nit.) function repairSrcsetTokens(srcset: string): string { return srcset.replace(SRCSET_TOKEN_RE, (token) => { const target = findRepairTarget(token); return target ? `/${target}` : token; }); } export function repairImageSrc(img: HTMLImageElement): boolean { if (img.dataset.imageRepairTried) return false; // Set the one-shot marker only AFTER confirming the URL carries a // recoverable artifacts/images segment (either unencoded or // percent-encoded — see `findRepairTarget`). Otherwise an unrelated // 404 would pin the marker and silently block a future repair // attempt on the same DOM element when the src is later replaced // with a repairable one. const target = findRepairTarget(img.src); if (!target) return false; img.dataset.imageRepairTried = "1"; img.src = `/${target}`; return true; } // Repair a `` element used inside `` / `