diff --git a/apps/desktop/src/main/services/projects/projectIconResolver.test.ts b/apps/desktop/src/main/services/projects/projectIconResolver.test.ts index 63a8c10e73..1b9e286187 100644 --- a/apps/desktop/src/main/services/projects/projectIconResolver.test.ts +++ b/apps/desktop/src/main/services/projects/projectIconResolver.test.ts @@ -71,6 +71,54 @@ describe("projectIconResolver", () => { const iconPath = writeFile(root, "public/brand/logo.svg", "brand"); expect(resolveProjectIconPath(root)).toBe(iconPath); + + // `rel` is an unordered, whitespace-separated token list; any `icon` token + // marks an icon link, wherever it sits in the list. + const tokenListRoot = makeProjectRoot(); + writeFile(tokenListRoot, "index.html", ''); + const tokenListIconPath = writeFile(tokenListRoot, "public/brand/logo.svg", "brand"); + expect(resolveProjectIconPath(tokenListRoot)).toBe(tokenListIconPath); + + // The same token list in the `__root.tsx` head-config object form. + const objectRoot = makeProjectRoot(); + writeFile( + objectRoot, + "app/routes/__root.tsx", + 'export const Route = { head: () => ({ links: [{ rel: "icon shortcut", href: "/brand/logo.svg" }] }) };', + ); + const objectIconPath = writeFile(objectRoot, "public/brand/logo.svg", "brand"); + expect(resolveProjectIconPath(objectRoot)).toBe(objectIconPath); + + // The closing quote pairs with the opening one, so `icon's` has no `icon` token. + const possessiveRoot = makeProjectRoot(); + writeFile(possessiveRoot, "index.html", ''); + writeFile(possessiveRoot, "public/brand/logo.svg", "brand"); + expect(resolveProjectIconPath(possessiveRoot)).toBeNull(); + + // A matched link whose href cannot be used (missing file, remote URL) must + // not mask a later usable icon link in the same file. + const maskedRoot = makeProjectRoot(); + writeFile( + maskedRoot, + "index.html", + [ + '', + '', + '', + ].join("\n"), + ); + const maskedIconPath = writeFile(maskedRoot, "public/brand/logo.svg", "brand"); + expect(resolveProjectIconPath(maskedRoot)).toBe(maskedIconPath); + + // The same fallback across head-config link objects. + const maskedObjectRoot = makeProjectRoot(); + writeFile( + maskedObjectRoot, + "app/routes/__root.tsx", + 'export const Route = { head: () => ({ links: [{ rel: "icon shortcut", href: "/brand/missing.png" }, { rel: "icon", href: "/brand/logo.svg" }] }) };', + ); + const maskedObjectIconPath = writeFile(maskedObjectRoot, "public/brand/logo.svg", "brand"); + expect(resolveProjectIconPath(maskedObjectRoot)).toBe(maskedObjectIconPath); }); it("detects nested app icons in monorepos", () => { diff --git a/apps/desktop/src/main/services/projects/projectIconResolver.ts b/apps/desktop/src/main/services/projects/projectIconResolver.ts index 908ba148ba..e95b72db44 100644 --- a/apps/desktop/src/main/services/projects/projectIconResolver.ts +++ b/apps/desktop/src/main/services/projects/projectIconResolver.ts @@ -76,19 +76,30 @@ const IOS_ASSET_CATALOG_CANDIDATE_DIRS = [ const IOS_ASSET_ICONSET_EXTENSIONS = new Set([".appiconset", ".imageset"]); -const LINK_ICON_HTML_RE = - /]*\brel=["'](?:icon|shortcut icon)["'])(?=[^>]*\bhref=["']([^"'?]+))[^>]*>/i; -const LINK_ICON_OBJ_RE = - /(?=[^}]*\brel\s*:\s*["'](?:icon|shortcut icon)["'])(?=[^}]*\bhref\s*:\s*["']([^"'?]+))[^}]*/i; +// `rel` is an unordered, whitespace-separated token list; any `icon` token marks an icon link. +const ICON_REL_VALUE = String.raw`(?:"(?:[^"]*[\t\n\f\r ])?icon(?=[\t\n\f\r "])|'(?:[^']*[\t\n\f\r ])?icon(?=[\t\n\f\r ']))`; +const LINK_ICON_HTML_RE = new RegExp( + String.raw`]*\brel=${ICON_REL_VALUE})(?=[^>]*\bhref=["']([^"'?]+))[^>]*>`, + "gi", +); +const LINK_ICON_OBJ_RE = new RegExp( + String.raw`(?=[^}]*\brel\s*:\s*${ICON_REL_VALUE})(?=[^}]*\bhref\s*:\s*["']([^"'?]+))[^}]*`, + "gi", +); type ProjectIconOverride = string | null | undefined; -function extractIconHref(source: string): string | null { - const htmlMatch = source.match(LINK_ICON_HTML_RE); - if (htmlMatch?.[1]) return htmlMatch[1]; - const objMatch = source.match(LINK_ICON_OBJ_RE); - if (objMatch?.[1]) return objMatch[1]; - return null; +function extractIconHrefs(source: string): string[] { + // Every icon link in order, so one unusable href (a missing file, a CDN URL, + // a `data:` URL) does not mask a later usable icon in the same file. + const hrefs: string[] = []; + for (const match of source.matchAll(LINK_ICON_HTML_RE)) { + if (match[1]) hrefs.push(match[1]); + } + for (const match of source.matchAll(LINK_ICON_OBJ_RE)) { + if (match[1]) hrefs.push(match[1]); + } + return hrefs; } function isLocalIconHref(href: string): boolean { @@ -810,11 +821,12 @@ export function resolveProjectIconPath( } catch { continue; } - const href = extractIconHref(source); - if (!href || !isLocalIconHref(href)) continue; - const existing = findExistingFile(root, resolveIconHref(root, href)); - if (existing && isSupportedIconPath(existing) && isInlineableIconFile(existing)) { - return cacheValue(existing); + for (const href of extractIconHrefs(source)) { + if (!isLocalIconHref(href)) continue; + const existing = findExistingFile(root, resolveIconHref(root, href)); + if (existing && isSupportedIconPath(existing) && isInlineableIconFile(existing)) { + return cacheValue(existing); + } } }