Skip to content

Commit b46a4ed

Browse files
Vikas-MaurVikasMaur-2ndclaudemaria-rcks
authored
fix(web): dark-mode <picture> images load instead of showing "Image unavailable" (pingdotgg#26398)
Co-authored-by: Vikas - AWS Auto <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 510aa5a commit b46a4ed

5 files changed

Lines changed: 150 additions & 5 deletions

File tree

‎apps/server/src/guest/guestAttachments.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ describe("guest host images", () => {
1616
["![a](file:///w/url%20form.png)", "/w/url form.png", "image/png"],
1717
["![a](C:\\w\\win.png)", "C:\\w\\win.png", "image/png"],
1818
["![a][r]\n\n[r]: C:\\w\\ref.png", "C:/w/ref.png", "image/png"],
19+
[
20+
'<picture><source media="(prefers-color-scheme: dark)" srcset="/w/dark.png"><img src="/w/light.png"></picture>',
21+
"/w/dark.png",
22+
"image/png",
23+
],
24+
[
25+
'<picture><source media="(prefers-color-scheme: dark)" srcset="C:\\w\\dark.png"><img src="C:\\w\\light.png"></picture>',
26+
"C:/w/dark.png",
27+
"image/png",
28+
],
1929
// Shown as a link, in code, or not at all: the guest sees no image, so none loads.
2030
["[a](/w/link.png)", "/w/link.png", null],
2131
["```\n![a](/w/code.png)\n```", "/w/code.png", null],

‎apps/web/src/components/ChatMarkdown.browser.test.tsx‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ vi.mock(import("./chat/MermaidDiagram"), async (original) => {
7474
};
7575
});
7676

77+
const appearance = vi.hoisted(() => ({
78+
value: "light" as "light" | "dark",
79+
listeners: new Set<() => void>(),
80+
}));
81+
vi.mock(import("../hooks/useTheme"), async (original) => {
82+
const { useSyncExternalStore } = await import("react");
83+
const subscribe = (listener: () => void) => {
84+
appearance.listeners.add(listener);
85+
return () => appearance.listeners.delete(listener);
86+
};
87+
return {
88+
...(await original()),
89+
useRenderedAppearance: () => useSyncExternalStore(subscribe, () => appearance.value),
90+
};
91+
});
92+
7793
const settingsOverrides = vi.hoisted(() => ({ mathRenderingEnabled: false }));
7894
vi.mock(import("../hooks/useSettings"), async (original) => {
7995
const actual = await original();
@@ -111,6 +127,7 @@ it("keeps compact audio playback loaded across signed URL renewals", async () =>
111127
});
112128

113129
function setup() {
130+
appearance.value = "light";
114131
parsedSources.length = 0;
115132
settingsOverrides.mathRenderingEnabled = false;
116133
renderDiagram.mockReset();
@@ -152,6 +169,34 @@ it("loads a host image through the page's own route when there is no environment
152169
);
153170
});
154171

172+
it("loads the <picture> source for the current theme through the image route", async () => {
173+
setup();
174+
const pixel =
175+
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=";
176+
const hostImageUrl = (path: string) => `${pixel}#${path}`;
177+
await using screen = await render(
178+
<HostImageUrlContext value={hostImageUrl}>
179+
<ChatMarkdown
180+
cwd={undefined}
181+
text={
182+
'<picture><source media="(prefers-color-scheme: dark)" srcset="/work/dark.png"><img src="/work/light.png" alt="Shot"></picture>'
183+
}
184+
/>
185+
</HostImageUrlContext>,
186+
);
187+
await expect
188+
.element(screen.getByAltText("Shot"))
189+
.toHaveAttribute("src", `${pixel}#/work/light.png`);
190+
191+
await act(() => {
192+
appearance.value = "dark";
193+
for (const listener of appearance.listeners) listener();
194+
});
195+
await expect
196+
.element(screen.getByAltText("Shot"))
197+
.toHaveAttribute("src", `${pixel}#/work/dark.png`);
198+
});
199+
155200
it("reuses the parse of a streaming reply's finished blocks when it remounts", async () => {
156201
setup();
157202
const message = (

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,7 @@ const CHAT_MARKDOWN_COMPONENTS = {
13801380
workspaceMutationId,
13811381
renderContextReference,
13821382
fileLinkChip,
1383+
resolvedTheme,
13831384
} = use(ChatMarkdownRendererContext);
13841385
const imageExpand = use(MarkdownLinkContext) ? undefined : expandMedia;
13851386
const contextReference = typeof src === "string" ? parseComposerContextHref(src) : null;
@@ -1391,10 +1392,13 @@ const CHAT_MARKDOWN_COMPONENTS = {
13911392
<span>{label}</span>
13921393
);
13931394
}
1394-
const localSrc = node?.properties?.dataLocalSrc;
1395+
// A `<picture>`'s source for this theme, resolved like any other image.
1396+
const themeSrc = node?.properties?.[resolvedTheme === "dark" ? "dataDarkSrc" : "dataLightSrc"];
1397+
const localSrc = typeof themeSrc === "string" ? undefined : node?.properties?.dataLocalSrc;
13951398
const markdownTitle = node?.properties?.dataMarkdownTitle;
13961399
const standalone = node?.properties?.dataStandalone === true;
1397-
const authoredSrc = typeof localSrc === "string" ? localSrc : src;
1400+
const authoredSrc =
1401+
typeof themeSrc === "string" ? themeSrc : typeof localSrc === "string" ? localSrc : src;
13981402
const authoredTitle = typeof markdownTitle === "string" ? markdownTitle : title;
13991403
const srcString =
14001404
typeof authoredSrc === "string" ? normalizeMarkdownLinkDestination(authoredSrc) : "";

‎packages/shared/src/markdownPipeline.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,41 @@ it("keeps an image on its own line under a label but leaves inline images inline
139139
expect(render("Built [![CI](/ci.svg)](/ci)\nand wrapped")).toBe("Built <img>\nand wrapped");
140140
expect(render("Hard-wrapped\nprose")).toBe("Hard-wrapped\nprose");
141141
});
142+
143+
it("moves a <picture>'s color-scheme sources onto its <img>, which the renderer resolves", () => {
144+
const processor = unified()
145+
.use(remarkParse)
146+
.use(CHAT_MARKDOWN_REMARK_PLUGINS)
147+
.use(remarkRehype, { allowDangerousHtml: true })
148+
.use(CHAT_MARKDOWN_REHYPE_PLUGINS);
149+
type Node = { type: string; tagName?: string; properties?: object; children?: Node[] };
150+
const tags = (node: Node): string[] => [
151+
...(node.tagName ? [node.tagName] : []),
152+
...(node.children ?? []).flatMap(tags),
153+
];
154+
const images = (node: Node): Node[] =>
155+
node.tagName === "img" ? [node] : (node.children ?? []).flatMap(images);
156+
const tree = processor.runSync(
157+
processor.parse(
158+
[
159+
"<picture>",
160+
' <source media="(prefers-color-scheme: dark)" srcset="docs/dark.webp 1x, docs/[email protected] 2x">',
161+
' <source media="(prefers-color-scheme: dark)" srcset="docs/dark-unused.webp">',
162+
' <source media="(min-width: 600px)" srcset="docs/wide.webp">',
163+
' <img src="docs/light.webp" alt="Studio">',
164+
"</picture>",
165+
"",
166+
'<picture><source media="(prefers-color-scheme: light)" srcset="javascript:alert(1)"><img src="docs/dark.webp"></picture>',
167+
"",
168+
String.raw`<picture><source media="(prefers-color-scheme: dark)" srcset="C:\shots\dark.png"><img src="C:\shots\light.png"></picture>`,
169+
].join("\n"),
170+
),
171+
) as Node;
172+
173+
expect(tags(tree)).toEqual(["picture", "img", "p", "picture", "img", "p", "picture", "img"]);
174+
expect(images(tree).map((image) => image.properties)).toEqual([
175+
{ src: "docs/light.webp", alt: "Studio", dataDarkSrc: "docs/dark.webp" },
176+
{ src: "docs/dark.webp" },
177+
{ dataLocalSrc: String.raw`C:\shots\light.png`, dataDarkSrc: "file:///C:/shots/dark.png" },
178+
]);
179+
});

‎packages/shared/src/markdownPipeline.ts‎

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,44 @@ function rehypePreserveBareAnchorPlaceholders() {
109109
};
110110
}
111111

112+
const COLOR_SCHEME_MEDIA = /^\(\s*prefers-color-scheme\s*:\s*(dark|light)\s*\)$/i;
113+
114+
/**
115+
* The renderer resolves an `<img>`'s `src` (workspace files, host media) but never a `<source>`'s
116+
* `srcset`, which would load unresolved whenever its media matched. A color-scheme source moves
117+
* onto the `<img>` for the renderer to pick by theme; every source is then dropped.
118+
*/
119+
function moveColorSchemeSourcesToImage(picture: MarkdownImageHastNode) {
120+
if (!picture.children) return;
121+
const image = picture.children.find((child) => child.tagName === "img");
122+
for (const source of picture.children) {
123+
if (source.tagName !== "source" || !image) continue;
124+
const media = source.properties?.media;
125+
const scheme = typeof media === "string" ? COLOR_SCHEME_MEDIA.exec(media)?.[1] : undefined;
126+
const srcSet = source.properties?.srcSet;
127+
const candidate = Array.isArray(srcSet) ? srcSet[0] : srcSet;
128+
// A candidate's URL runs to the first space (a descriptor such as `2x` may follow).
129+
const url =
130+
typeof candidate === "string" ? candidate.trim().split(/\s/, 1)[0]?.replace(/,+$/, "") : "";
131+
const key = scheme?.toLowerCase() === "dark" ? "dataDarkSrc" : "dataLightSrc";
132+
// As in a browser, the first source that matches a scheme is the one shown.
133+
if (scheme && url && image.properties?.[key] === undefined) {
134+
// The sanitizer reads a drive letter as a protocol; as a file URL it resolves to the same path.
135+
const value = isWindowsDrivePathHref(url) ? `file:///${url.replaceAll("\\", "/")}` : url;
136+
image.properties = { ...image.properties, [key]: value };
137+
}
138+
}
139+
picture.children = picture.children.filter((child) => child.tagName !== "source");
140+
}
141+
112142
function rehypePreserveImageSourceMeta() {
113143
return (tree: MarkdownImageHastNode) => {
114144
const visit = (node: MarkdownImageHastNode) => {
115145
const src = node.properties?.src;
116146
const title = node.properties?.title;
147+
if (node.type === "element" && node.tagName === "picture") {
148+
moveColorSchemeSourcesToImage(node);
149+
}
117150
if (node.type === "element" && node.tagName === "img") {
118151
node.properties = {
119152
...node.properties,
@@ -129,6 +162,8 @@ function rehypePreserveImageSourceMeta() {
129162
};
130163
}
131164

165+
const IMAGE_SRC_PROTOCOLS = [...(defaultSchema.protocols?.src ?? []), "file", "t3-context"];
166+
132167
const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
133168
...defaultSchema,
134169
attributes: {
@@ -145,6 +180,8 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
145180
a: [...(defaultSchema.attributes?.a ?? []), "dataPullRequestAutolink"],
146181
img: [
147182
...(defaultSchema.attributes?.img ?? []),
183+
"dataDarkSrc",
184+
"dataLightSrc",
148185
"dataLocalSrc",
149186
"dataMarkdownTitle",
150187
"dataStandalone",
@@ -159,7 +196,9 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
159196
"t3-context",
160197
THREAD_LINK_PROTOCOL,
161198
],
162-
src: [...(defaultSchema.protocols?.src ?? []), "file", "t3-context"],
199+
src: IMAGE_SRC_PROTOCOLS,
200+
dataDarkSrc: IMAGE_SRC_PROTOCOLS,
201+
dataLightSrc: IMAGE_SRC_PROTOCOLS,
163202
},
164203
} satisfies Parameters<typeof rehypeSanitize>[0];
165204

@@ -388,7 +427,12 @@ const imageSourceProcessor = unified()
388427
interface ImageSourceNode {
389428
readonly type: string;
390429
readonly tagName?: string;
391-
readonly properties?: { readonly src?: unknown; readonly dataLocalSrc?: unknown };
430+
readonly properties?: {
431+
readonly src?: unknown;
432+
readonly dataLocalSrc?: unknown;
433+
readonly dataDarkSrc?: unknown;
434+
readonly dataLightSrc?: unknown;
435+
};
392436
readonly children?: ReadonlyArray<ImageSourceNode>;
393437
}
394438

@@ -400,13 +444,17 @@ export function chatMarkdownImageSources(markdown: string): Array<string> {
400444
const sources: Array<string> = [];
401445
const visit = (node: ImageSourceNode) => {
402446
if (node.type === "element" && node.tagName === "img") {
403-
const { src, dataLocalSrc } = node.properties ?? {};
447+
const { src, dataLocalSrc, dataDarkSrc, dataLightSrc } = node.properties ?? {};
404448
// The sanitizer drops a Windows drive path from `src`; the renderer reads it from here.
405449
if (typeof dataLocalSrc === "string") {
406450
sources.push(normalizeMarkdownLinkDestination(dataLocalSrc).replaceAll("\\", "/"));
407451
} else if (typeof src === "string") {
408452
sources.push(normalizeMarkdownLinkDestination(src));
409453
}
454+
// A `<picture>` shows one of these instead, by the viewer's theme.
455+
for (const variant of [dataDarkSrc, dataLightSrc]) {
456+
if (typeof variant === "string") sources.push(normalizeMarkdownLinkDestination(variant));
457+
}
410458
}
411459
node.children?.forEach(visit);
412460
};

0 commit comments

Comments
 (0)