Skip to content

fix(ui): preserve raw code text in code-block copy button - #69635

Closed
Eruditi wants to merge 20 commits into
openclaw:mainfrom
Eruditi:fix/code-block-copy-raw-text
Closed

Eruditi wants to merge 20 commits into
openclaw:mainfrom
Eruditi:fix/code-block-copy-raw-text

Conversation

@Eruditi

@Eruditi Eruditi commented Apr 21, 2026 •

Copy link
Copy Markdown

Summary

  • Problem: The data-code attribute on code-block copy buttons was using escapeHtml(), which converted special characters like <, >, and & into HTML entities. When users clicked the copy button, they got escaped text instead of the original code.
  • Why it matters: Users copying code with HTML special characters get broken, escaped code that does not work when pasted.
  • What changed: Introduced escapeHtmlAttribute() which only escapes double quotes for HTML attribute safety, preserving the original code text.

Linked Issue

Change Type

  • Bug fix

Scope

  • UI / DX

Security Impact

None. No new permissions, secrets, network calls, or data scope changes.

Eruditi and others added 2 commits April 21, 2026 15:53
Closes openclaw#69605

The data-code attribute was using escapeHtml() which converted
special characters like <, >, and & into HTML entities. When
users clicked the copy button, they got escaped text like
&lt;div&gt; instead of <div>.

This fix introduces escapeHtmlAttribute() which only escapes
double quotes for attribute safety, preserving the original
code text so it copies correctly.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 24ff1ca55e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread ui/src/ui/markdown.ts Outdated
}

function escapeHtmlAttribute(value: string): string {
return value.replace(/"/g, "&quot;");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Escape ampersands in code-copy attribute values

Limiting escapeHtmlAttribute to only " causes HTML entities inside code blocks to be decoded by the browser before copy. For example, a code block containing &lt;div&gt; or &copy; will be stored in data-code as-is, parsed into <div>/©, and then copied via dataset.code, so users no longer get the original source text. This regresses copy fidelity for any snippet that intentionally includes entity text.

Useful? React with 👍 / 👎.

@greptile-apps

greptile-apps Bot commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR introduces escapeHtmlAttribute() to replace the full escapeHtml() call when building the data-code button attribute, so that <, >, and & are preserved as raw characters rather than HTML entities. The intent is to fix copy-button output for code containing HTML special characters.

  • Missing & escape (P1): escapeHtmlAttribute only escapes \", leaving & unescaped. Code that contains literal HTML entity sequences (e.g. &lt;, &amp;) will have those sequences decoded by the HTML parser, so dataset.code returns the wrong text when the user clicks Copy.
  • Broken test assertion (P1): The new test checks for raw < and & inside the data-code attribute value, but DOMPurify serializes attribute values through the DOM (re-encoding those characters), so the assertion will not match the actual output string in a browser environment.

Confidence Score: 4/5

Not safe to merge as-is: the missing & escape causes incorrect copy output for code containing HTML entity sequences, and the new test assertion is likely wrong after DOMPurify serialization.

Two P1 findings: the escapeHtmlAttribute function omits &-escaping (required for correct HTML attribute encoding), and the test assertion targets a string form that DOMPurify would re-encode. Both need to be fixed before this PR reliably improves the copy behavior it targets.

ui/src/ui/markdown.ts (escapeHtmlAttribute) and ui/src/ui/markdown.test.ts (test assertion)

Prompt To Fix All With AI
This is a comment left during a code review.
Path: ui/src/ui/markdown.ts
Line: 148-150

Comment:
**Missing `&` escape causes entity sequences to be decoded**

`escapeHtmlAttribute` skips `&`, so any code that literally contains an HTML entity sequence (e.g. `&lt;`, `&amp;`, `&gt;`) will have that sequence decoded when the HTML is parsed. Concretely, if `token.content` is `"&lt;div&gt;"` (a user's literal text), the generated attribute is `data-code="&lt;div&gt;"`, the browser decodes it to `<div>`, and `dataset.code` returns `<div>` — the wrong value.

`&` must be escaped to `&amp;` first, before `"` is escaped, to prevent this:

```suggestion
function escapeHtmlAttribute(value: string): string {
  return value.replace(/&/g, "&amp;").replace(/"/g, "&quot;");
}
```

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: ui/src/ui/markdown.test.ts
Line: 320-325

Comment:
**Test assertion checks unencoded form that won't survive DOMPurify serialization**

`toSanitizedMarkdownHtml` runs the rendered HTML through DOMPurify, which parses it into a DOM and serializes back via `innerHTML`. During that round-trip, literal `<` and `&` in attribute values are re-encoded to `&lt;` and `&amp;`, so the final string will contain `data-code="&lt;div&gt;hello &amp; world&lt;/div&gt;"` — not the literal characters the assertion expects. The test may pass coincidentally under jsdom but would fail in a real browser.

The assertion should reflect the HTML-encoded form that DOMPurify produces:
```suggestion
      expect(html).toContain('data-code="&lt;div&gt;hello &amp; world&lt;/div&gt;"');
```
(With the `&` fix applied to `escapeHtmlAttribute`, `<` and `&` are properly encoded before DOMPurify, so the round-trip is deterministic.)

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "Merge branch 'openclaw:main' into fix/co..." | Re-trigger Greptile

Comment thread ui/src/ui/markdown.ts
Comment on lines +148 to +150
function escapeHtmlAttribute(value: string): string {
return value.replace(/"/g, "&quot;");
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Missing & escape causes entity sequences to be decoded

escapeHtmlAttribute skips &, so any code that literally contains an HTML entity sequence (e.g. &lt;, &amp;, &gt;) will have that sequence decoded when the HTML is parsed. Concretely, if token.content is "&lt;div&gt;" (a user's literal text), the generated attribute is data-code="&lt;div&gt;", the browser decodes it to <div>, and dataset.code returns <div> — the wrong value.

& must be escaped to &amp; first, before " is escaped, to prevent this:

Suggested change
function escapeHtmlAttribute(value: string): string {
return value.replace(/"/g, "&quot;");
}
function escapeHtmlAttribute(value: string): string {
return value.replace(/&/g, "&amp;").replace(/"/g, "&quot;");
}
Prompt To Fix With AI
This is a comment left during a code review.
Path: ui/src/ui/markdown.ts
Line: 148-150

Comment:
**Missing `&` escape causes entity sequences to be decoded**

`escapeHtmlAttribute` skips `&`, so any code that literally contains an HTML entity sequence (e.g. `&lt;`, `&amp;`, `&gt;`) will have that sequence decoded when the HTML is parsed. Concretely, if `token.content` is `"&lt;div&gt;"` (a user's literal text), the generated attribute is `data-code="&lt;div&gt;"`, the browser decodes it to `<div>`, and `dataset.code` returns `<div>` — the wrong value.

`&` must be escaped to `&amp;` first, before `"` is escaped, to prevent this:

```suggestion
function escapeHtmlAttribute(value: string): string {
  return value.replace(/&/g, "&amp;").replace(/"/g, "&quot;");
}
```

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +320 to +325
it("preserves special characters in data-code attribute", () => {
const code = '<div>hello & world</div>';
const html = toSanitizedMarkdownHtml("```\n" + code + "\n```");
// data-code should contain the original code, not HTML-escaped
expect(html).toContain('data-code="' + code + '"');
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Test assertion checks unencoded form that won't survive DOMPurify serialization

toSanitizedMarkdownHtml runs the rendered HTML through DOMPurify, which parses it into a DOM and serializes back via innerHTML. During that round-trip, literal < and & in attribute values are re-encoded to &lt; and &amp;, so the final string will contain data-code="&lt;div&gt;hello &amp; world&lt;/div&gt;" — not the literal characters the assertion expects. The test may pass coincidentally under jsdom but would fail in a real browser.

The assertion should reflect the HTML-encoded form that DOMPurify produces:

Suggested change
it("preserves special characters in data-code attribute", () => {
const code = '<div>hello & world</div>';
const html = toSanitizedMarkdownHtml("```\n" + code + "\n```");
// data-code should contain the original code, not HTML-escaped
expect(html).toContain('data-code="' + code + '"');
});
expect(html).toContain('data-code="&lt;div&gt;hello &amp; world&lt;/div&gt;"');

(With the & fix applied to escapeHtmlAttribute, < and & are properly encoded before DOMPurify, so the round-trip is deterministic.)

Prompt To Fix With AI
This is a comment left during a code review.
Path: ui/src/ui/markdown.test.ts
Line: 320-325

Comment:
**Test assertion checks unencoded form that won't survive DOMPurify serialization**

`toSanitizedMarkdownHtml` runs the rendered HTML through DOMPurify, which parses it into a DOM and serializes back via `innerHTML`. During that round-trip, literal `<` and `&` in attribute values are re-encoded to `&lt;` and `&amp;`, so the final string will contain `data-code="&lt;div&gt;hello &amp; world&lt;/div&gt;"` — not the literal characters the assertion expects. The test may pass coincidentally under jsdom but would fail in a real browser.

The assertion should reflect the HTML-encoded form that DOMPurify produces:
```suggestion
      expect(html).toContain('data-code="&lt;div&gt;hello &amp; world&lt;/div&gt;"');
```
(With the `&` fix applied to `escapeHtmlAttribute`, `<` and `&` are properly encoded before DOMPurify, so the round-trip is deterministic.)

How can I resolve this? If you propose a fix, please make it concise.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e13755b48f

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread ui/src/ui/markdown.ts Outdated
Comment on lines +148 to +149
function escapeHtmlAttribute(value: string): string {
return value.replace(/"/g, "&quot;");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Escape ampersands in code-copy attribute values

Fresh evidence from this commit: escapeHtmlAttribute now escapes only ", and both copy-button renderers rely on it. In HTML attribute parsing, unescaped &...; sequences are decoded as character references, so code blocks containing literal entity text like &lt;div&gt; or &copy; will be copied as <div>/© instead of the original bytes. That makes the copy button lose fidelity for a common class of snippets (escaped HTML/docs examples).

Useful? React with 👍 / 👎.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1b5a0efa1b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread ui/src/ui/markdown.ts Outdated
Comment on lines +148 to +149
function escapeHtmlAttribute(value: string): string {
return value.replace(/"/g, "&quot;");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Escape ampersands in code-copy attribute values

escapeHtmlAttribute now escapes only quotes, so data-code values that contain entity-like text (for example &lt;div&gt;, &copy;, or &#10;) are decoded by the HTML parser before dataset.code is read. In those cases the copy button returns transformed text (<div>, ©, newline, etc.) instead of the original snippet bytes, which breaks copy fidelity for common docs/examples that intentionally include literal entities.

Useful? React with 👍 / 👎.

Eruditi and others added 4 commits April 21, 2026 16:39
- escapeHtmlAttribute: add &-escaping to prevent entity sequences
  from being decoded by HTML parser in attribute values
- test: update assertion to match DOMPurify serialized output
  (innerHTML round-trip re-encodes <, >, & to entities)
@Eruditi

Eruditi commented Apr 21, 2026

Copy link
Copy Markdown
Author

Thank you for the reviews! The fix addresses the issue by introducing escapeHtmlAttribute() which only escapes double quotes for HTML attribute safety, while preserving the original code text. This ensures that users can copy the exact code without any HTML entity escaping.

The change is minimal and focused, only affecting the code block copy functionality. Please let me know if there are any further adjustments needed, or if this can be merged.

@prtags

prtags Bot commented Apr 23, 2026 •

Copy link
Copy Markdown

Related work from PRtags group major-octopus-wnlz

Title: Open PR duplicate: code block copy overlap in Control UI

Number Title
#69634 fix(ui): code block copy button overlaps message-level copy action (#69605)
#69635* fix(ui): preserve raw code text in code-block copy button
#69689 fix(ui): reserve bubble space for code block copy (#69605)

* This PR

@steipete

Copy link
Copy Markdown
Contributor

Closing this as not reproducible on current main after Codex review.

Current main already round-trips code-block copy text correctly through data-code/dataset.code, and the linked issue #69605 is about clicking the wrong overlapping copy control rather than HTML-entity corruption.

What I checked:

  • Current renderer already HTML-escapes the copied payload: Both fenced and indented code blocks store data-code using escapeHtml(text), which escapes &, <, >, ", and ' before inserting the attribute. (ui/src/ui/markdown.ts:139, 2cd2732ab669)
  • Clipboard copy reads the decoded dataset value: The click handler copies (btn as HTMLElement).dataset.code, so the browser reads the parsed attribute value rather than the serialized HTML entities. (ui/src/ui/views/chat.ts:804, 2cd2732ab669)
  • Read-only parser repro shows current encoding preserves original bytes: A local HTMLParser check decoded data-code="&lt;div&gt;hello &amp; world&lt;/div&gt;" back to <div>hello & world</div> and decoded data-code="&amp;lt;div&amp;gt;" back to literal &lt;div&gt;, matching the intended raw-copy behavior on current main.
  • Linked issue describes a different bug: Issue [Bug]: Web UI copy button copies entire message instead of code block content #69605 says the code-block button copies the entire assistant message instead of just the block. The related work linked by PRtags, PR fix(ui): reserve bubble space for code block copy (#69605) #69689, targets reserved bubble space / overlap for that symptom, not HTML-entity preservation.

Codex Review notes: reviewed against 2cd2732ab669.

@steipete steipete closed this Apr 25, 2026
@clawsweeper clawsweeper Bot mentioned this pull request Apr 26, 2026
@Eruditi
Eruditi deleted the fix/code-block-copy-raw-text branch April 27, 2026 00:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Web UI copy button copies entire message instead of code block content

2 participants