Skip to content
TypeScript

fix(composer): copied context with an apostrophe survives the HTML clipboard (#21537)

MarcoDotIOdcf051e861f0
diff --git a/packages/shared/src/composerContextClipboard.test.ts b/packages/shared/src/composerContextClipboard.test.ts
index 59918d57b42..6b8b50df97c 100644
--- a/packages/shared/src/composerContextClipboard.test.ts
+++ b/packages/shared/src/composerContextClipboard.test.ts
@@ -76,6 +76,32 @@ describe("composerContextClipboard", () => {
).toBeNull();
expect(decodeComposerContextClipboardHtml("<p>Ordinary clipboard</p>")).toBeNull();
});
+ it("round-trips context whose text contains an apostrophe", () => {
+ const raw = JSON.stringify({
+ version: 1,
+ source: { environmentId: "env-1" },
+ records: [
+ {
+ version: 1,
+ kind: "terminal",
+ contextId: "terminal",
+ label: "Build",
+ terminalId: "main",
+ terminalLabel: "Main",
+ lineStart: 1,
+ lineEnd: 1,
+ text: "error: can't find module",
+ },
+ ],
+ });
+ const html = encodeComposerContextClipboardHtml("error: can't find module", raw);
+ expect(decodeComposerContextClipboardHtml(html)).toEqual(JSON.parse(raw));
+ expect(
+ decodeComposerContextClipboardHtml(
+ `<pre data-t3-context-fragment='${encodeURIComponent(raw).replaceAll("'", "%27")}'>x</pre>`,
+ ),
+ ).toEqual(JSON.parse(raw));
+ });
it("accepts a large non-ASCII fragment through the HTML attribute", () => {
// CJK code units expand to nine encoded characters despite fitting the fragment limit.
const raw = JSON.stringify({
diff --git a/packages/shared/src/composerContextClipboard.ts b/packages/shared/src/composerContextClipboard.ts
index c6a83f727d5..6036795a780 100644
--- a/packages/shared/src/composerContextClipboard.ts
+++ b/packages/shared/src/composerContextClipboard.ts
@@ -35,7 +35,9 @@ export function decodeComposerContextClipboardHtml(
): ComposerContextClipboardFragment | null {
// Percent encoding can expand each UTF-16 code unit to nine characters.
if (!html || html.length > MAX_FRAGMENT_CHARS * 9 + HTML_WRAPPER_SLACK_CHARS) return null;
- const encoded = /data-t3-context-fragment=["']([^"']+)["']/.exec(html)?.[1];
+ // `encodeURIComponent` leaves `'` as is, so each quote style must end only at its own quote.
+ const match = /data-t3-context-fragment=(?:"([^"]+)"|'([^']+)')/.exec(html);
+ const encoded = match?.[1] ?? match?.[2];
if (!encoded) return null;
try {
return decodeComposerContextFragment(decodeURIComponent(encoded));