Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/core/tools/applyDiffTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export async function applyDiffToolLegacy(
const relPath: string | undefined = block.params.path
let diffContent: string | undefined = block.params.diff

// Unescape HTML entities for non-Claude models (e.g., Gemini, DeepSeek, Llama)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Minor suggestion: Could we make this comment more generic? Instead of listing specific models, perhaps: "Unescape HTML entities for non-Claude models that may return content with escaped characters"

// These models may return content with escaped characters that need to be unescaped
if (diffContent && !cline.api.getModel().id.includes("claude")) {
diffContent = unescapeHtmlEntities(diffContent)
}
Expand Down
8 changes: 6 additions & 2 deletions src/core/tools/writeToFileTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,20 @@ export async function writeToFileTool(
cline.diffViewProvider.editType = fileExists ? "modify" : "create"
}

// pre-processing newContent for cases where weaker models might add artifacts like markdown codeblock markers (deepseek/llama) or extra escape characters (gemini)
// Pre-processing newContent for cases where models might add artifacts
// Some models (DeepSeek/Llama) add markdown codeblock markers
// Others (Gemini) return content with HTML-escaped characters
if (newContent.startsWith("```")) {
// cline handles cases where it includes language specifiers like ```python ```js
// Handle cases where it includes language specifiers like ```python ```js
newContent = newContent.split("\n").slice(1).join("\n")
}

if (newContent.endsWith("```")) {
newContent = newContent.split("\n").slice(0, -1).join("\n")
}

// Unescape HTML entities for non-Claude models (e.g., Gemini, DeepSeek, Llama)
// These models may return content with escaped characters that need to be unescaped
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same comment consistency suggestion here - could be more generic rather than listing specific model names.

if (!cline.api.getModel().id.includes("claude")) {
newContent = unescapeHtmlEntities(newContent)
}
Expand Down
57 changes: 57 additions & 0 deletions src/utils/__tests__/text-normalization-extended.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { describe, it, expect } from "vitest"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could we consider merging this test file with the existing text-normalization.spec.ts? Both test the same function, and having them in one file would improve maintainability and discoverability.

import { unescapeHtmlEntities } from "../text-normalization"

describe("Extended HTML entity unescaping", () => {
describe("unescapeHtmlEntities", () => {
it("unescapes alternative apostrophe encoding", () => {
const input = "It's working"
const expected = "It's working"
expect(unescapeHtmlEntities(input)).toBe(expected)
})

it("unescapes forward slash", () => {
const input = "path/to/file"
const expected = "path/to/file"
expect(unescapeHtmlEntities(input)).toBe(expected)
})

it("unescapes backslash", () => {
const input = "C:\Users\file"
const expected = "C:\\Users\\file"
expect(unescapeHtmlEntities(input)).toBe(expected)
})

it("unescapes backtick", () => {
const input = "`code`"
const expected = "`code`"
expect(unescapeHtmlEntities(input)).toBe(expected)
})

it("unescapes non-breaking space", () => {
const input = "Hello World"
const expected = "Hello World"
expect(unescapeHtmlEntities(input)).toBe(expected)
})

it("handles complex mixed content with all entity types", () => {
const input =
"<div class="test">It's a  test/path\file with `code` & more</div>"
const expected = '<div class="test">It\'s a test/path\\file with `code` & more</div>'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this double space intentional? The input has &nbsp;test (one non-breaking space) but the expected output shows two spaces between "It's a" and "test". Should we verify this is the intended behavior?

expect(unescapeHtmlEntities(input)).toBe(expected)
})

it("handles Gemini-style escaped markdown content", () => {
const input =
"```python\n&lt;search&gt;\ndef old_function():\n return &#x27;old&#x27;\n&lt;/search&gt;\n&lt;replace&gt;\ndef new_function():\n return &#x27;new&#x27;\n&lt;/replace&gt;\n```"
const expected =
"```python\n<search>\ndef old_function():\n return 'old'\n</search>\n<replace>\ndef new_function():\n return 'new'\n</replace>\n```"
expect(unescapeHtmlEntities(input)).toBe(expected)
})

it("correctly orders ampersand unescaping to avoid double-unescaping", () => {
const input = "&amp;lt;&amp;gt;&amp;amp;"
const expected = "&lt;&gt;&amp;"
expect(unescapeHtmlEntities(input)).toBe(expected)
})
})
})
7 changes: 6 additions & 1 deletion src/utils/text-normalization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,10 @@ export function unescapeHtmlEntities(text: string): string {
.replace(/&quot;/g, '"')
.replace(/&#39;/g, "'")
.replace(/&apos;/g, "'")
.replace(/&amp;/g, "&")
.replace(/&#x27;/g, "'") // Alternative apostrophe encoding
.replace(/&#x2F;/g, "/") // Forward slash
.replace(/&#x5C;/g, "\\") // Backslash
.replace(/&#x60;/g, "`") // Backtick
.replace(/&nbsp;/g, " ") // Non-breaking space
.replace(/&amp;/g, "&") // Must be last to avoid double-unescaping
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For high-frequency usage, would it be worth considering a single regex with a replacement function instead of chaining multiple .replace() calls? This could improve performance for frequently called functions.

}
Loading