Skip to content

Commit dcf051e

Browse files
MarcoDotIOclaudemaria-rcks
authored
fix(composer): copied context with an apostrophe survives the HTML clipboard (#21537)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent a17ab2e commit dcf051e

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,32 @@ describe("composerContextClipboard", () => {
7676
).toBeNull();
7777
expect(decodeComposerContextClipboardHtml("<p>Ordinary clipboard</p>")).toBeNull();
7878
});
79+
it("round-trips context whose text contains an apostrophe", () => {
80+
const raw = JSON.stringify({
81+
version: 1,
82+
source: { environmentId: "env-1" },
83+
records: [
84+
{
85+
version: 1,
86+
kind: "terminal",
87+
contextId: "terminal",
88+
label: "Build",
89+
terminalId: "main",
90+
terminalLabel: "Main",
91+
lineStart: 1,
92+
lineEnd: 1,
93+
text: "error: can't find module",
94+
},
95+
],
96+
});
97+
const html = encodeComposerContextClipboardHtml("error: can't find module", raw);
98+
expect(decodeComposerContextClipboardHtml(html)).toEqual(JSON.parse(raw));
99+
expect(
100+
decodeComposerContextClipboardHtml(
101+
`<pre data-t3-context-fragment='${encodeURIComponent(raw).replaceAll("'", "%27")}'>x</pre>`,
102+
),
103+
).toEqual(JSON.parse(raw));
104+
});
79105
it("accepts a large non-ASCII fragment through the HTML attribute", () => {
80106
// CJK code units expand to nine encoded characters despite fitting the fragment limit.
81107
const raw = JSON.stringify({

‎packages/shared/src/composerContextClipboard.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export function decodeComposerContextClipboardHtml(
3535
): ComposerContextClipboardFragment | null {
3636
// Percent encoding can expand each UTF-16 code unit to nine characters.
3737
if (!html || html.length > MAX_FRAGMENT_CHARS * 9 + HTML_WRAPPER_SLACK_CHARS) return null;
38-
const encoded = /data-t3-context-fragment=["']([^"']+)["']/.exec(html)?.[1];
38+
// `encodeURIComponent` leaves `'` as is, so each quote style must end only at its own quote.
39+
const match = /data-t3-context-fragment=(?:"([^"]+)"|'([^']+)')/.exec(html);
40+
const encoded = match?.[1] ?? match?.[2];
3941
if (!encoded) return null;
4042
try {
4143
return decodeComposerContextFragment(decodeURIComponent(encoded));

0 commit comments

Comments
 (0)