Skip to content

Commit c6f4b57

Browse files
committed
fix(cli): rank a pinned-tab mask-icon below real favicons
`link[rel*="icon"]` also matches `rel="mask-icon"`, Safari's pinned-tab asset: a single-colour silhouette drawn in a browser-chosen tint, not the site mark. It is served as an SVG, so ranking by format alone promoted the outline above the page's actual colour favicon. Give mask-icon its own lowest tier rather than dropping it, so a page that declares nothing else still lands an icon instead of none.
1 parent e582356 commit c6f4b57

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

‎packages/cli/src/capture/faviconRanker.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ describe("rankIconCandidates", () => {
8484
]);
8585
});
8686

87+
it("does not let a Safari pinned-tab silhouette beat the real favicon", () => {
88+
// A mask-icon is an SVG, so ranking on format alone would promote the silhouette.
89+
const masked: IconCandidate[] = [
90+
{ rel: "mask-icon", href: "https://x.test/pinned.svg", sizes: null, type: null },
91+
{ rel: "icon", href: "https://x.test/favicon.png", sizes: "32x32", type: "image/png" },
92+
{ rel: "icon", href: "https://x.test/favicon.svg", sizes: null, type: "image/svg+xml" },
93+
];
94+
expect(hrefs(masked)).toEqual([
95+
"https://x.test/favicon.svg",
96+
"https://x.test/favicon.png",
97+
"https://x.test/pinned.svg",
98+
]);
99+
});
100+
101+
it("still returns a mask-icon when the page declares nothing else", () => {
102+
// Ranked last, not dropped: a silhouette on disk beats no icon at all.
103+
const only: IconCandidate[] = [{ rel: "mask-icon", href: "https://x.test/pinned.svg" }];
104+
expect(hrefs(only)).toEqual(["https://x.test/pinned.svg"]);
105+
});
106+
87107
it("drops candidates with no href", () => {
88108
expect(hrefs([{ rel: "icon", href: "" }, ...NOTION])).toHaveLength(2);
89109
});

‎packages/cli/src/capture/faviconRanker.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ function isSvg(c: IconCandidate): boolean {
4949
return c.type?.toLowerCase() === "image/svg+xml" || pathnameOf(c.href).endsWith(".svg");
5050
}
5151

52+
/**
53+
* `rel="mask-icon"` is Safari's pinned-tab asset: a single-colour silhouette, drawn in whatever
54+
* tint the browser picks. It is not the site mark, and it is served as an SVG, so ranking by
55+
* format alone would promote a monochrome outline over the page's real colour favicon.
56+
*/
57+
function isMaskIcon(c: IconCandidate): boolean {
58+
return c.rel.toLowerCase().split(/\s+/).includes("mask-icon");
59+
}
60+
5261
function isIco(c: IconCandidate): boolean {
5362
const t = c.type?.toLowerCase();
5463
return (
@@ -62,14 +71,22 @@ function declaredSize(c: IconCandidate): number {
6271
return c.rel.toLowerCase().split(/\s+/).includes("apple-touch-icon") ? APPLE_TOUCH_DEFAULT_PX : 0;
6372
}
6473

74+
function tierOf(c: IconCandidate): number {
75+
// ponytail: mask-icon is ranked last rather than filtered out, so a page that declares
76+
// nothing else still lands an icon instead of none.
77+
if (isMaskIcon(c)) return 3;
78+
if (isSvg(c)) return 0;
79+
return isIco(c) ? 2 : 1;
80+
}
81+
6582
/**
66-
* Best-first order: SVG, then largest declared size, then `.ico` last.
67-
* Stable within a tier, so DOM order breaks ties.
83+
* Best-first order: SVG, then largest declared size, then `.ico`, then a pinned-tab `mask-icon`
84+
* as a last resort. Stable within a tier, so DOM order breaks ties.
6885
*/
6986
export function rankIconCandidates(candidates: IconCandidate[]): IconCandidate[] {
7087
return candidates
7188
.filter((c) => !!c.href)
72-
.map((c, i) => ({ c, i, tier: isSvg(c) ? 0 : isIco(c) ? 2 : 1, size: declaredSize(c) }))
89+
.map((c, i) => ({ c, i, tier: tierOf(c), size: declaredSize(c) }))
7390
.sort((a, b) => a.tier - b.tier || b.size - a.size || a.i - b.i)
7491
.map((e) => e.c);
7592
}

0 commit comments

Comments
 (0)