Repository navigation
fix(desktop): read icon links from an HTML rel token list #2474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,19 +76,30 @@ const IOS_ASSET_CATALOG_CANDIDATE_DIRS = [ | |
|
|
||
| const IOS_ASSET_ICONSET_EXTENSIONS = new Set([".appiconset", ".imageset"]); | ||
|
|
||
| const LINK_ICON_HTML_RE = | ||
| /<link\b(?=[^>]*\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`<link\b(?=[^>]*\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]); | ||
| } | ||
|
Comment on lines
+99
to
+101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
An 80 KB HTML input with a valid first icon link took about 2.24 seconds, versus under 1 ms for the previous extraction path. This runs synchronously in Electron's main process, so opening such a project stalls other app requests. Yield hrefs lazily so the caller can stop after a usable HTML link, or limit the object search to individual objects. Prompt To Fix With AIThis is a comment left during a code review.
Path: apps/desktop/src/main/services/projects/projectIconResolver.ts
Line: 99-101
Comment:
**Icon lookup freezes ADE**
`extractIconHrefs` now runs `LINK_ICON_OBJ_RE` over the whole file before the caller can use a valid HTML link. When the file has no `}` or object-style `rel:` field, the regex scans the remaining text again at each position.
An 80 KB HTML input with a valid first icon link took about 2.24 seconds, versus under 1 ms for the previous extraction path. This runs synchronously in Electron's main process, so opening such a project stalls other app requests. Yield hrefs lazily so the caller can stop after a usable HTML link, or limit the object search to individual objects.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly. |
||
| 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); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both new success cases put lowercase
iconfirst and use a space. They do not exercise the preceding-token branch inICON_REL_VALUE, tabs, mixed case, or single-quoted values. Those paths could break while these tests still pass.Add table-driven cases such as
rel="shortcut Icon",rel="Icon\tshortcut", and single-quoted values, including the object form, so the committed tests cover the behavior described by this fix.Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!