Skip to content

Commit 5b2fd3f

Browse files
committed
fix: handle square bracket HTML entities in Gemini responses
- Added support for [, ], [, and ] entities - Fixes issue where Gemini API responses were missing square brackets in C# array syntax - Added comprehensive tests for square bracket entity handling Fixes #7576
1 parent 97cfb96 commit 5b2fd3f

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/utils/__tests__/text-normalization.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,30 @@ describe("Text normalization utilities", () => {
7575
expect(unescapeHtmlEntities("")).toBe("")
7676
expect(unescapeHtmlEntities(undefined as unknown as string)).toBe(undefined)
7777
})
78+
79+
it("unescapes square bracket entities (numeric)", () => {
80+
expect(unescapeHtmlEntities("string[]")).toBe("string[]")
81+
expect(unescapeHtmlEntities("array[0]")).toBe("array[0]")
82+
expect(unescapeHtmlEntities("matrix[i][j]")).toBe("matrix[i][j]")
83+
})
84+
85+
it("unescapes square bracket entities (named)", () => {
86+
expect(unescapeHtmlEntities("string[]")).toBe("string[]")
87+
expect(unescapeHtmlEntities("array[0]")).toBe("array[0]")
88+
expect(unescapeHtmlEntities("matrix[i][j]")).toBe("matrix[i][j]")
89+
})
90+
91+
it("handles C# array syntax with escaped square brackets", () => {
92+
// Test case based on the reported issue
93+
const input = "string[] myArray = new string[5];\nmyArray[2] = "hello";"
94+
const expected = 'string[] myArray = new string[5];\nmyArray[2] = "hello";'
95+
expect(unescapeHtmlEntities(input)).toBe(expected)
96+
})
97+
98+
it("handles mixed square bracket entities", () => {
99+
const input = "array[0] and [1]"
100+
const expected = "array[0] and [1]"
101+
expect(unescapeHtmlEntities(input)).toBe(expected)
102+
})
78103
})
79104
})

src/utils/text-normalization.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,9 @@ export function unescapeHtmlEntities(text: string): string {
9191
.replace(/"/g, '"')
9292
.replace(/'/g, "'")
9393
.replace(/'/g, "'")
94+
.replace(/[/g, "[")
95+
.replace(/]/g, "]")
96+
.replace(/[/g, "[")
97+
.replace(/]/g, "]")
9498
.replace(/&/g, "&")
9599
}

0 commit comments

Comments
 (0)