WebKit Bugzilla
New
Browse
Search+
Log In
×
Sign in with GitHub
or
Remember my login
Create Account
·
Forgot Password
Forgotten password account recovery
NEW
315573
Handle favicon, apple-touch-icon and web manifest icons correctly for pixel-perfect rendering.
https://bugs.webkit.org/show_bug.cgi?id=315573
Summary
Handle favicon, apple-touch-icon and web manifest icons correctly for pixel-p...
Fuwei Chin
Reported
2026-05-26 09:05:32 PDT
Created
attachment 479830
[details]
Icons with watermark to test which image was chosen and rendered, glyph taken from Twemoji For favicon (shown in page titlebar), Safari 16/17/18/26 prefers ICO format to PNG/SVG formats, and always chooses the largest-size image (e.g. 48x48) in ICO regardless of screen devicePixelRatio and <link> sizes attribute (e.g. even when devicePixelRatio is 2 and sizes of ICO link is set to "16x16 32x32"). Expected Safari to choose 32x32 ICO entry for devices with devicePixelRatio of 2. <link rel="icon" href="icons/icon.svg" type="image/svg" sizes="16x16 32x32 48x48" /> <link rel="icon" href="icons/icon-16x16.png" type="image/png" sizes="16x16" /> <link rel="icon" href="icons/icon-32x32.png" type="image/png" sizes="32x32" /> <link rel="icon" href="icons/icon-48x48.png" type="image/png" sizes="48x48" /> <link rel="icon" href="icons/favicon.ico" type="image/x-icon" sizes="16x16 32x32 48x48" /> For web app icon generated (from apple-touch-icon) for iOS/iPadOS (shown in HomeScreen), Safri 16/17//18(and maybe 26) always chooses 120x120 for iOS (no matter iPhone @2x or iPhone @3x), always chooses 152x152 for iPadOS (no matter iPad/iPad mini @2x or larger iPad Pro @2x), even when all pixel-perfect sizes {180x180,167x167,152x152,120x120} are provided. Expected Safari to choose 180x180 for iPhone @3x, to choose 167x167 for iPad Pro @2x. <link rel="apple-touch-icon" href="icons/apple-touch-icon-180x180.png" sizes="180x180" /> <link rel="apple-touch-icon" href="icons/apple-touch-icon-167x167.png" sizes="167x167" /> <link rel="apple-touch-icon" href="icons/apple-touch-icon-152x152.png" sizes="152x152" /> <link rel="apple-touch-icon" href="icons/apple-touch-icon-120x120.png" sizes="120x120" /> For web app icon generated (from PWA manifest icons) for macOS, Safari 26 prefers 512x512 to a larger size (e.g. 1024x1024) as primary image to generate ICNS if both provided. Chrome 148 prefers a larger size (e.g. 1024x1024) to 512x512 to generate ICNS if both provided, but the rest sizes are ignored. { "icons": [ { "src": "icons/maskable-icon-512x512.png", "sizes": "512x512", "type": "image/png", "purpose": "maskable" }, { "src": "icons/maskable-icon-1024x1024.png", "sizes": "1024x1024", "type": "image/png", "purpose": "maskable" }, ], }
Attachments
Icons with watermark to test which image was chosen and rendered, glyph taken from Twemoji
(113.72 KB, application/zip)
2026-05-26 09:05 PDT
,
Fuwei Chin
no flags
Details
View All
Add attachment
proposed patch, testcase, etc.
Radar WebKit Bug Importer
Comment 1
2026-06-02 09:06:12 PDT
<
rdar://problem/178532218
>
Karl Dubost
Comment 2
2026-06-03 02:46:42 PDT
The responsibility is shared in between WebKit code and Safari code. * WebCore collects and pre-sorts every <link rel="icon"> / apple-touch-icon / apple-touch-icon-precomposed declared on a page, plus the manifest icons[] array. * Safari's UI process is what actually decides which entry to download for a given purpose (tab favicon, web clip, Web app). WebKit WebCore does not make a final pick. It reads sizes partially and uses it for sorting only. Source/WebCore/html/LinkRelAttribute.cpp classifies the rel token. Three icon types are recognised: Source/WebCore/html/LinkIconType.h rel= value LinkIconType icon (and legacy shortcut icon) LinkIconType::Favicon apple-touch-icon LinkIconType::TouchIcon apple-touch-icon-precomposed LinkIconType::TouchPrecomposedIcon (<link rel="mask-icon"> is not parsed at all. It is not in the SortedArrayMap of recognised tokens in LinkRelAttribute.cpp, so it never produces a LinkIconType and never reaches LinkIconCollector. )
https://searchfox.org/wubkat/rev/d221d71a6a560cb45bb3e9b23e526b6bcfa9e4b3/Source/WebCore/html/LinkIconCollector.cpp#39-75
```cpp constexpr unsigned defaultTouchIconWidth = 60; static unsigned NODELETE iconSize(const LinkIcon& icon) { if (icon.size) return *icon.size; if (icon.type == LinkIconType::TouchIcon || icon.type == LinkIconType::TouchPrecomposedIcon) return defaultTouchIconWidth; return 0; } static int NODELETE compareIcons(const LinkIcon& a, const LinkIcon& b) { // Apple Touch icons always come first. if (a.type == LinkIconType::Favicon && b.type != LinkIconType::Favicon) return 1; if (b.type == LinkIconType::Favicon && a.type != LinkIconType::Favicon) return -1; unsigned aSize = iconSize(a); unsigned bSize = iconSize(b); if (bSize > aSize) return 1; if (bSize < aSize) return -1; // A Precomposed icon should come first if both icons have the same size. if (a.type != LinkIconType::TouchPrecomposedIcon && b.type == LinkIconType::TouchPrecomposedIcon) return 1; if (b.type != LinkIconType::TouchPrecomposedIcon && a.type == LinkIconType::TouchPrecomposedIcon) return -1; return 0; } ``` Three priority tiers, applied in order: (touch > favicon) → larger declared size → (precomposed > non-precomposed). There is no window.devicePixelRatio lookup, no UIScreen.scale, no consultation of the rendering target. The sort is purely by static metadata. Then about sizes=""
https://searchfox.org/wubkat/rev/d221d71a6a560cb45bb3e9b23e526b6bcfa9e4b3/Source/WebCore/html/LinkIconCollector.cpp#97-102
```cpp // This icon size parsing is a little wonky - it only parses the first // part of the size, "60x70" becomes "60". This is for compatibility reasons // and is probably good enough for now. std::optional<unsigned> iconSize; if (linkElement->sizes().length()) iconSize = parseIntegerAllowingTrailingJunk<unsigned>(linkElement->sizes().item(0)); ``` A <link rel="icon" sizes="16x16 32x32 48x48" href="favicon.ico"> gets recorded as a 16-pixel icon, and any 32x32 PNG declared alongside it will outrank it on the size sort even though the ICO actually contains the 32x32 entry too. The "first token wins" rule means the order in which the developer writes the tokens inside sizes= silently changes the sort outcome. <link rel="apple-touch-icon" sizes="180x180"> markup is fine because there's only one token. The misbehaviour is specifically when one <link> advertises several sizes for a multi-resolution asset. If a <link rel="apple-touch-icon"> has no sizes attribute at all, iconSize() returns 60. A 180x180 candidate with sizes="180x180" will outrank a 60-default candidate; an unsized touch icon will tie with one declared sizes="60x60". LinkIconCollector::iconsOfTypes(OptionSet<LinkIconType>) walks the document head (only direct children of <head>), filters by requested types, builds the LinkIcon list, and sorts via compareIcons. The returned Vector<LinkIcon> is in best-first order, so the top entry is what the consumer typically loads. struct LinkIcon { URL url; LinkIconType type; String mimeType; std::optional<unsigned> size; Vector<std::pair<String, String>> attributes; }; Source/WebCore/loader/DocumentLoader.cpp (around lines 2479–2513) calls LinkIconCollector::iconsOfTypes() ICO multi-resolution decoding If the chosen icon is an .ico containing several sizes (16/32/48), the decoder is the deciding factor for what bitmap the renderer actually sees.
https://searchfox.org/wubkat/rev/d221d71a6a560cb45bb3e9b23e526b6bcfa9e4b3/Source/WebCore/platform/image-decoders/ico/ICOImageDecoder.cpp#131-138
```cpp // static bool ICOImageDecoder::compareEntries(const IconDirectoryEntry& a, const IconDirectoryEntry& b) { // Larger icons are better. After that, higher bit-depth icons are better. const int aEntryArea = a.m_size.width() * a.m_size.height(); const int bEntryArea = b.m_size.width() * b.m_size.height(); return (aEntryArea == bEntryArea) ? (a.m_bitCount > b.m_bitCount) : (aEntryArea > bEntryArea); } ```
https://searchfox.org/wubkat/rev/d221d71a6a560cb45bb3e9b23e526b6bcfa9e4b3/Source/WebCore/platform/image-decoders/ico/ICOImageDecoder.cpp#246-270
processDirectoryEntries() then sorts the whole directory and reports the image size as the size of the largest entry (line 266: "The image size is the size of the largest entry."). a multi-resolution .ico with 16/32/48 entries advertises itself as a 48x48 image regardless of devicePixelRatio. There is no API surface to tell the decoder "give me the 32x32 frame for a 2x display." Individual frames can be requested by index, but no consumer in the WebKit icon pipeline does. They take frame 0, which is post-sort the largest. Manifest icons (ApplicationManifest.icons[]) Source/WebCore/Modules/applicationmanifest/ApplicationManifestParser.cpp::parseIcons (lines 321–397) does the JSON parse, with no scoring:
https://searchfox.org/wubkat/rev/d221d71a6a560cb45bb3e9b23e526b6bcfa9e4b3/Source/WebCore/Modules/applicationmanifest/ApplicationManifestParser.cpp#351-355
```cpp currentIcon.src = srcURL; currentIcon.sizes = parseGenericString(*iconJSON, "sizes"_s).split(' '); currentIcon.type = parseGenericString(*iconJSON, "type"_s); ``` The sizes field on a manifest icon is preserved as a full Vector<String> (unlike LinkIcon::size), so the per-purpose downstream code in Safari has more to work with than the per-<link> path does. The rest is in Safari code.
Karl Dubost
Comment 3
2026-06-03 02:48:17 PDT
The smallest fix for "Favicon: prefers ICO, picks largest entry" is in LinkIconCollector.cpp: * replace linkElement->sizes().item(0) with a loop over the full DOMTokenList, * store all (width, height) pairs (the struct field needs to grow), * have compareIcons choose the closest match to a target (devicePixelRatio × intrinsic UI size) instead of monotone-largest.
Note
You need to
log in
before you can comment on or make changes to this bug.
Top of Page
Format For Printing
XML
Clone This Bug