Repository navigation
Conversation
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 <div> instead of <div>. This fix introduces escapeHtmlAttribute() which only escapes double quotes for attribute safety, preserving the original code text so it copies correctly.
There was a problem hiding this comment.
💡 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".
| } | ||
|
|
||
| function escapeHtmlAttribute(value: string): string { | ||
| return value.replace(/"/g, """); |
There was a problem hiding this comment.
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 <div> or © 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 SummaryThis PR introduces
Confidence Score: 4/5Not safe to merge as-is: the missing Two P1 findings: the
Prompt To Fix All With AIThis 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. `<`, `&`, `>`) will have that sequence decoded when the HTML is parsed. Concretely, if `token.content` is `"<div>"` (a user's literal text), the generated attribute is `data-code="<div>"`, the browser decodes it to `<div>`, and `dataset.code` returns `<div>` — the wrong value.
`&` must be escaped to `&` first, before `"` is escaped, to prevent this:
```suggestion
function escapeHtmlAttribute(value: string): string {
return value.replace(/&/g, "&").replace(/"/g, """);
}
```
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 `<` and `&`, so the final string will contain `data-code="<div>hello & world</div>"` — 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="<div>hello & world</div>"');
```
(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 |
| function escapeHtmlAttribute(value: string): string { | ||
| return value.replace(/"/g, """); | ||
| } |
There was a problem hiding this comment.
Missing
& escape causes entity sequences to be decoded
escapeHtmlAttribute skips &, so any code that literally contains an HTML entity sequence (e.g. <, &, >) will have that sequence decoded when the HTML is parsed. Concretely, if token.content is "<div>" (a user's literal text), the generated attribute is data-code="<div>", the browser decodes it to <div>, and dataset.code returns <div> — the wrong value.
& must be escaped to & first, before " is escaped, to prevent this:
| function escapeHtmlAttribute(value: string): string { | |
| return value.replace(/"/g, """); | |
| } | |
| function escapeHtmlAttribute(value: string): string { | |
| return value.replace(/&/g, "&").replace(/"/g, """); | |
| } |
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. `<`, `&`, `>`) will have that sequence decoded when the HTML is parsed. Concretely, if `token.content` is `"<div>"` (a user's literal text), the generated attribute is `data-code="<div>"`, the browser decodes it to `<div>`, and `dataset.code` returns `<div>` — the wrong value.
`&` must be escaped to `&` first, before `"` is escaped, to prevent this:
```suggestion
function escapeHtmlAttribute(value: string): string {
return value.replace(/&/g, "&").replace(/"/g, """);
}
```
How can I resolve this? If you propose a fix, please make it concise.| 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 + '"'); | ||
| }); |
There was a problem hiding this 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 < and &, so the final string will contain data-code="<div>hello & world</div>" — 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:
| 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="<div>hello & world</div>"'); |
(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 `<` and `&`, so the final string will contain `data-code="<div>hello & world</div>"` — 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="<div>hello & world</div>"');
```
(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.There was a problem hiding this comment.
💡 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".
| function escapeHtmlAttribute(value: string): string { | ||
| return value.replace(/"/g, """); |
There was a problem hiding this comment.
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 <div> or © 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 👍 / 👎.
There was a problem hiding this comment.
💡 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".
| function escapeHtmlAttribute(value: string): string { | ||
| return value.replace(/"/g, """); |
There was a problem hiding this comment.
Escape ampersands in code-copy attribute values
escapeHtmlAttribute now escapes only quotes, so data-code values that contain entity-like text (for example <div>, ©, or ) 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 👍 / 👎.
- 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)
|
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. |
|
Related work from PRtags group Title: Open PR duplicate: code block copy overlap in Control UI
|
|
Closing this as not reproducible on current Current What I checked:
Codex Review notes: reviewed against 2cd2732ab669. |
Summary
Linked Issue
Change Type
Scope
Security Impact
None. No new permissions, secrets, network calls, or data scope changes.