Skip to content

Commit e046495

Browse files
fix(client-runtime): media tables keep quoted attributes and spans intact
A quoted `>` in a cell attribute ended the tag early and leaked into the cell text; the tag pattern now skips quoted values. Spanning cells fall back to the line-by-line split, since rows are laid out independently. Mobile builds the image gallery once per body instead of once per markdown cell. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 097d324 commit e046495

3 files changed

Lines changed: 36 additions & 28 deletions

File tree

‎apps/mobile/src/features/pull-requests/PullRequestMarkdown.tsx‎

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -92,33 +92,33 @@ export function PullRequestMarkdown(props: {
9292
readonly markdown: string;
9393
readonly imageGroupMarkdown?: string;
9494
}) {
95+
const mediaContext = useContext(PullRequestMediaContext);
9596
const segments = useMemo(() => splitPullRequestBody(props.markdown), [props.markdown]);
96-
const imageGroupMarkdown = props.imageGroupMarkdown ?? props.markdown;
97+
const gallery: FilePreviewItem[] = markdownImageSources(
98+
props.imageGroupMarkdown ?? props.markdown,
99+
)
100+
.filter(
101+
(uri) =>
102+
/^https?:\/\//i.test(uri) &&
103+
videoMimeType({ name: uri.split(/[?#]/)[0] ?? uri, mimeType: "" }) === null,
104+
)
105+
.map((uri) => pullRequestImageSource(uri, mediaContext));
97106
return (
98107
<View style={{ gap: 12 }}>
99108
{segments.map((segment) => (
100-
<PullRequestBodySegmentView
101-
key={segment.id}
102-
segment={segment}
103-
imageGroupMarkdown={imageGroupMarkdown}
104-
/>
109+
<PullRequestBodySegmentView key={segment.id} segment={segment} gallery={gallery} />
105110
))}
106111
</View>
107112
);
108113
}
109114

110115
function PullRequestBodySegmentView(props: {
111116
readonly segment: PullRequestBodySegment;
112-
readonly imageGroupMarkdown: string;
117+
readonly gallery: FilePreviewItem[];
113118
}) {
114119
const { segment } = props;
115120
if (segment.kind === "markdown") {
116-
return (
117-
<PullRequestMarkdownContent
118-
markdown={segment.text}
119-
imageGroupMarkdown={props.imageGroupMarkdown}
120-
/>
121-
);
121+
return <PullRequestMarkdownContent markdown={segment.text} gallery={props.gallery} />;
122122
}
123123
if (segment.kind === "attachment") {
124124
return <PullRequestMedia url={segment.url} kind={segment.media} />;
@@ -135,7 +135,7 @@ function PullRequestBodySegmentView(props: {
135135
<PullRequestBodySegmentView
136136
key={cellSegment.id}
137137
segment={cellSegment}
138-
imageGroupMarkdown={props.imageGroupMarkdown}
138+
gallery={props.gallery}
139139
/>
140140
))}
141141
</View>
@@ -148,7 +148,7 @@ function PullRequestBodySegmentView(props: {
148148

149149
function PullRequestMarkdownContent(props: {
150150
readonly markdown: string;
151-
readonly imageGroupMarkdown?: string;
151+
readonly gallery: FilePreviewItem[];
152152
}) {
153153
const autolinks = useContext(AutolinksContext);
154154
const mediaContext = useContext(PullRequestMediaContext);
@@ -157,22 +157,13 @@ function PullRequestMarkdownContent(props: {
157157
const openLink = (href: string) => {
158158
void openPullRequest(href);
159159
};
160-
const gallery: FilePreviewItem[] = markdownImageSources(
161-
props.imageGroupMarkdown ?? props.markdown,
162-
)
163-
.filter(
164-
(uri) =>
165-
/^https?:\/\//i.test(uri) &&
166-
videoMimeType({ name: uri.split(/[?#]/)[0] ?? uri, mimeType: "" }) === null,
167-
)
168-
.map((uri) => pullRequestImageSource(uri, mediaContext));
169160
const [preview, setPreview] = useState<FilePreviewSource | null>(null);
170161
const renderImage: MarkdownImageRenderer = (image) => (
171162
<MarkdownImage href={image.href} alt={image.alt} onPreview={setPreview} />
172163
);
173164
const styles = useMarkdownPreviewStyles();
174165
return (
175-
<FilePreviewGallery value={gallery}>
166+
<FilePreviewGallery value={props.gallery}>
176167
<SelectableMarkdownText
177168
markdown={markdown}
178169
textStyle={styles.nativeTextStyle}

‎packages/client-runtime/src/pullRequestMarkdown.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ describe("media tables", () => {
155155
]);
156156
});
157157

158+
it("keeps a quoted `>` inside a cell's attributes out of its text", () => {
159+
const [table] = splitPullRequestBody(
160+
`<table>\n<tr><td title="Before > after">Caption</td><td>\n\n${video}\n\n</td></tr>\n</table>`,
161+
);
162+
expect(table).toMatchObject({
163+
kind: "table",
164+
rows: [[{ segments: [{ kind: "markdown", text: "Caption" }] }, {}]],
165+
});
166+
});
167+
158168
it("leaves a table without media links as markdown", () => {
159169
const body = "<table>\n<tr><td>a</td><td>b</td></tr>\n</table>";
160170
expect(splitPullRequestBody(body)).toEqual([
@@ -176,6 +186,7 @@ describe("media tables", () => {
176186
"</td></tr>\n</table>\n</td></tr>\n</table>",
177187
],
178188
["an unclosed table", "<table>\n<tr><td>", ""],
189+
["a spanning cell", '<table>\n<tr><td colspan="2">', "</td></tr>\n</table>"],
179190
])("splits a media table with %s line by line, as before", (_name, open, close) => {
180191
expect(splitPullRequestBody(`${open}\n\n${video}\n\n${close}`)).toEqual([
181192
{ id: "markdown:0", kind: "markdown", text: open },

‎packages/client-runtime/src/pullRequestMarkdown.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,14 @@ const TABLE_START_PATTERN = /^\s*<table\b/iu;
4444
const TABLE_END_PATTERN = /<\/table>\s*$/iu;
4545
const TABLE_OPEN_PATTERN = /<table\b/giu;
4646
const TABLE_CLOSE_PATTERN = /<\/table\s*>/giu;
47-
/** `[^<>]` stops each attempt at the next `<`, so a run of unterminated tags stays linear. */
48-
const TABLE_STRUCTURE_TAG_PATTERN = /<(\/?)(table|thead|tbody|tfoot|tr|td|th)\b[^<>]*>/giu;
47+
/**
48+
* Quoted attribute values may hold `>`. The alternatives start on disjoint characters and an
49+
* unquoted run stops at the next `<`, so a run of unterminated tags stays linear.
50+
*/
51+
const TABLE_STRUCTURE_TAG_PATTERN =
52+
/<(\/?)(table|thead|tbody|tfoot|tr|td|th)\b((?:[^<>"']|"[^"]*"|'[^']*')*)>/giu;
53+
/** Rows are laid out independently, so a spanning cell would shift the cells after it. */
54+
const CELL_SPAN_PATTERN = /\b(?:col|row)span\b/iu;
4955

5056
function isWebUrl(url: string): boolean {
5157
try {
@@ -104,7 +110,7 @@ function tableRows(html: string): TableRows | null {
104110
if (closing) rows.push(row!);
105111
row = closing ? null : [];
106112
} else if (tag === "td" || tag === "th") {
107-
if (closing || row === null) return null;
113+
if (closing || row === null || CELL_SPAN_PATTERN.test(match[3]!)) return null;
108114
cell = { tag, contentStart: match.index + match[0].length };
109115
} else if (!tableOpen || row !== null) {
110116
return null;

0 commit comments

Comments
 (0)