Skip to content

Commit 18757df

Browse files
fix(shared): standalone <picture> images get standalone loading and error frames (#43722)
Co-authored-by: SAPHID <saphid@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
1 parent aea941a commit 18757df

3 files changed

Lines changed: 113 additions & 8 deletions

File tree

‎apps/web/src/markdown/wordsHast.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,11 @@ const CASES: ReadonlyArray<readonly [markdown: string, html: string]> = [
188188
],
189189
[
190190
'<picture><source media="(prefers-color-scheme: dark)" srcset="d.png"><source media="(prefers-color-scheme: light)" srcset="l.png"><img src="l.png" alt="x"></picture>',
191-
'<p><picture><img src="l.png" alt="x" data-dark-src="d.png" data-light-src="l.png"></picture></p>',
191+
'<p><picture><img src="l.png" alt="x" data-dark-src="d.png" data-light-src="l.png" data-standalone></picture></p>',
192+
],
193+
[
194+
'# H\n\n<picture>\n<source media="(prefers-color-scheme: dark)" srcset="d.png">\n<img src="l.png" alt="x">\n</picture>\n\nP',
195+
'<h1 id="user-content-h" dir="ltr">H</h1>\n<picture>\n\n<img src="l.png" alt="x" data-dark-src="d.png" data-standalone>\n</picture>\n<p dir="ltr">P</p>',
192196
],
193197
[
194198
"<details>\n<summary>⚠️ Outside diff (3)</summary><blockquote>\n\n**a**\n\n</blockquote></details>",

‎packages/shared/src/markdownHast.ts‎

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,62 @@ function soleImageDescendant(node: MarkdownImageHastNode): MarkdownImageHastNode
4141
const only = children[0];
4242
if (only?.type !== "element") return undefined;
4343
if (only.tagName === "img") return only;
44-
return only.tagName === "a" || only.tagName === "strong" || only.tagName === "em"
44+
return only.tagName === "a" ||
45+
only.tagName === "strong" ||
46+
only.tagName === "em" ||
47+
only.tagName === "picture"
4548
? soleImageDescendant(only)
4649
: undefined;
4750
}
4851

52+
const BLOCK_ELEMENTS = new Set([
53+
...STANDALONE_IMAGE_BLOCKS,
54+
"address",
55+
"article",
56+
"aside",
57+
"dd",
58+
"details",
59+
"dl",
60+
"dt",
61+
"figcaption",
62+
"footer",
63+
"h1",
64+
"h2",
65+
"h3",
66+
"h4",
67+
"h5",
68+
"h6",
69+
"header",
70+
"hr",
71+
"main",
72+
"nav",
73+
"ol",
74+
"pre",
75+
"section",
76+
"summary",
77+
"table",
78+
"ul",
79+
]);
80+
81+
const isBlockOrEdge = (node: MarkdownImageHastNode | undefined) =>
82+
!node || (node.type === "element" && BLOCK_ELEMENTS.has(node.tagName ?? ""));
83+
84+
// A raw-HTML image block between other blocks sits directly under the root, with no paragraph of
85+
// its own; it is standalone when nothing inline shares its line.
86+
function markRootImageBlocks(root: MarkdownImageHastNode) {
87+
const children = meaningfulHastChildren(root).filter((child) => child.type !== "comment");
88+
children.forEach((child, index) => {
89+
if (isBlockOrEdge(child)) return;
90+
const image = soleImageDescendant({ children: [child] });
91+
if (image && isBlockOrEdge(children[index - 1]) && isBlockOrEdge(children[index + 1])) {
92+
image.properties = { ...image.properties, dataStandalone: true };
93+
}
94+
});
95+
}
96+
4997
function markStandaloneImages(node: MarkdownImageHastNode) {
50-
if (node.type === "root" || (node.tagName && STANDALONE_IMAGE_BLOCKS.has(node.tagName))) {
98+
if (node.type === "root") markRootImageBlocks(node);
99+
else if (node.tagName && STANDALONE_IMAGE_BLOCKS.has(node.tagName)) {
51100
const image = soleImageDescendant(node);
52101
if (image) image.properties = { ...image.properties, dataStandalone: true };
53102
}

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

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,11 @@ it("keeps uppercase HTTP(S) link and image URLs by lowercasing the scheme before
577577
{ href: "http://Example.com/B" },
578578
{},
579579
{ src: "https://Example.com/Shot.png", alt: "shot", dataStandalone: true },
580-
{ src: "http://Example.com/Light.png", dataDarkSrc: "https://Example.com/Dark.png" },
580+
{
581+
src: "http://Example.com/Light.png",
582+
dataDarkSrc: "https://Example.com/Dark.png",
583+
dataStandalone: true,
584+
},
581585
]);
582586
});
583587

@@ -608,6 +612,7 @@ it("keeps uppercase FILE link and image URLs by lowercasing the scheme before sa
608612
src: "file://host/tmp/Fallback.png",
609613
dataDarkSrc: "file:///tmp/Dark.png",
610614
dataLightSrc: "file:///tmp/Light.png",
615+
dataStandalone: true,
611616
},
612617
]);
613618
expect(chatMarkdownImageSources(markdown)).toHaveLength(4);
@@ -1024,17 +1029,64 @@ it("moves a <picture>'s color-scheme sources onto its <img>, which the renderer
10241029
"img",
10251030
]);
10261031
expect(images(tree).map((image) => image.properties)).toEqual([
1027-
{ src: "docs/light.webp", alt: "Studio", dataDarkSrc: "docs/dark.webp" },
1028-
{ src: "docs/dark.webp" },
1029-
{ dataLocalSrc: String.raw`C:\shots\light.png`, dataDarkSrc: "file:///C:/shots/dark.png" },
1030-
{ src: "https://example.com/light.png", dataDarkSrc: "https://example.com/a\u00a0b.png" },
1032+
{ src: "docs/light.webp", alt: "Studio", dataDarkSrc: "docs/dark.webp", dataStandalone: true },
1033+
{ src: "docs/dark.webp", dataStandalone: true },
1034+
{
1035+
dataLocalSrc: String.raw`C:\shots\light.png`,
1036+
dataDarkSrc: "file:///C:/shots/dark.png",
1037+
dataStandalone: true,
1038+
},
1039+
{
1040+
src: "https://example.com/light.png",
1041+
dataDarkSrc: "https://example.com/a\u00a0b.png",
1042+
dataStandalone: true,
1043+
},
10311044
{
10321045
src: "https://example.com/dark.png",
10331046
dataLightSrc: "https://example.com/\u00a0light\u00a0v2.png",
1047+
dataStandalone: true,
10341048
},
10351049
]);
10361050
});
10371051

1052+
it("marks a <picture> image standalone exactly where a bare <img> would be", () => {
1053+
const processor = unified()
1054+
.use(remarkParse)
1055+
.use(CHAT_MARKDOWN_REMARK_PLUGINS)
1056+
.use(remarkRehype, { allowDangerousHtml: true })
1057+
.use(CHAT_MARKDOWN_REHYPE_PLUGINS);
1058+
type Node = { tagName?: string; properties?: Record<string, unknown>; children?: Node[] };
1059+
const images = (node: Node): Node[] =>
1060+
node.tagName === "img" ? [node] : (node.children ?? []).flatMap(images);
1061+
const standalone = (markdown: string) =>
1062+
images(processor.runSync(processor.parse(markdown)) as Node).map(
1063+
(image) => image.properties?.dataStandalone === true,
1064+
);
1065+
const image = '<img src="https://example.com/light.png" alt="Logo">';
1066+
const picture = `<picture><source media="(prefers-color-scheme: dark)" srcset="https://example.com/dark.png">${image}</picture>`;
1067+
1068+
for (const wrap of [
1069+
(img: string) => `<p align="center">${img}</p>`,
1070+
(img: string) => `<div align="center"><a href="https://example.com">${img}</a></div>`,
1071+
(img: string) => `Inline ${img} icon`,
1072+
(img: string) => `<p>${img}${img}</p>`,
1073+
]) {
1074+
expect(standalone(wrap(picture))).toEqual(standalone(wrap(image)));
1075+
}
1076+
// Raw-HTML blocks between other blocks, as an agent's README-style reply writes them.
1077+
const multilinePicture = `<picture>\n${image}\n</picture>`;
1078+
for (const [block, expected] of [
1079+
[multilinePicture, [true]],
1080+
[image, [true]],
1081+
[`${multilinePicture} caption`, [false]],
1082+
[`${multilinePicture}${multilinePicture}`, [false, false]],
1083+
] as const) {
1084+
expect(standalone(`# Heading\n\n${block}\n\nParagraph`)).toEqual(expected);
1085+
}
1086+
expect(standalone(`<p align="center">${picture}</p>`)).toEqual([true]);
1087+
expect(standalone(`Inline ${picture} icon`)).toEqual([false]);
1088+
});
1089+
10381090
it.each([
10391091
["default", CHAT_MARKDOWN_REMARK_PLUGINS],
10401092
["with breaks", CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS],

0 commit comments

Comments
 (0)