Skip to content

Commit cca465a

Browse files
fix(translation): skip github PR review diff tables (#1175)
Co-authored-by: GuaGua <[email protected]>
1 parent 93415c1 commit cca465a

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@read-frog/extension": patch
3+
---
4+
5+
fix(translation): skip GitHub PR review diff tables during page translation

src/utils/constants/dom-rules.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ export const CUSTOM_DONT_WALK_INTO_ELEMENT_SELECTOR_MAP: Record<string, string[]
151151
"header *",
152152
"#repository-container-header *",
153153
"[class*=\"OverviewContent-module__Box_1--\"] *",
154+
"table.diff-table", // https://github.com/mengxi-ream/read-frog/issues/1174
154155
],
155156
}
156157

src/utils/host/__tests__/translate.integration.test.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ const TRANSLATION_ONLY_CONFIG: Config = {
4343
},
4444
}
4545

46+
function setHost(host: string) {
47+
Object.defineProperty(window, "location", {
48+
value: new URL(`https://${host}/some/path`),
49+
writable: true,
50+
configurable: true,
51+
})
52+
}
53+
4654
describe("translate", () => {
4755
// Setup and teardown for getComputedStyle mock
4856
const originalGetComputedStyle = window.getComputedStyle
@@ -1320,6 +1328,41 @@ describe("translate", () => {
13201328
})
13211329
})
13221330

1331+
describe("github diff table - should not translate review code snippets", () => {
1332+
it("bilingual mode: should skip github diff-table content entirely", async () => {
1333+
const originalLocation = window.location
1334+
setHost("github.com")
1335+
vi.mocked(translateTextForPage).mockClear()
1336+
1337+
try {
1338+
render(
1339+
<div data-testid="test-node">
1340+
<table className="diff-table">
1341+
<tbody>
1342+
<tr>
1343+
<td>const foo = 1</td>
1344+
</tr>
1345+
</tbody>
1346+
</table>
1347+
</div>,
1348+
)
1349+
const node = screen.getByTestId("test-node")
1350+
await removeOrShowPageTranslation("bilingual", true)
1351+
1352+
expect(node.querySelector(`.${CONTENT_WRAPPER_CLASS}`)).toBeFalsy()
1353+
expect(node.textContent).toBe("const foo = 1")
1354+
expect(translateTextForPage).not.toHaveBeenCalled()
1355+
}
1356+
finally {
1357+
Object.defineProperty(window, "location", {
1358+
value: originalLocation,
1359+
writable: true,
1360+
configurable: true,
1361+
})
1362+
}
1363+
})
1364+
})
1365+
13231366
describe("code tag - should not walk into but translate as child", () => {
13241367
it("bilingual mode: should translate text with code elements", async () => {
13251368
render(

src/utils/host/dom/__tests__/custom-dont-walk.test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @vitest-environment jsdom
22
import { afterEach, describe, expect, it } from "vitest"
33
import { DEFAULT_CONFIG } from "@/utils/constants/config"
4-
import { isCustomDontWalkIntoElement, isDontWalkIntoAndDontTranslateAsChildElement } from "../filter"
4+
import { hasNoWalkAncestor, isCustomDontWalkIntoElement, isDontWalkIntoAndDontTranslateAsChildElement } from "../filter"
55

66
function setHost(host: string) {
77
// jsdom exposes location as read-only; override via defineProperty
@@ -104,4 +104,25 @@ describe("isCustomDontWalkIntoElement", () => {
104104
expect(isCustomDontWalkIntoElement(postFlair)).toBe(true)
105105
expect(isDontWalkIntoAndDontTranslateAsChildElement(postFlair, DEFAULT_CONFIG)).toBe(true)
106106
})
107+
108+
it("matches github review diff table and blocks its descendants", () => {
109+
setHost("github.com")
110+
111+
const diffTable = document.createElement("table")
112+
diffTable.classList.add("diff-table")
113+
114+
const tbody = document.createElement("tbody")
115+
const tr = document.createElement("tr")
116+
const td = document.createElement("td")
117+
td.textContent = "const foo = 1"
118+
119+
tr.appendChild(td)
120+
tbody.appendChild(tr)
121+
diffTable.appendChild(tbody)
122+
document.body.appendChild(diffTable)
123+
124+
expect(isCustomDontWalkIntoElement(diffTable)).toBe(true)
125+
expect(isDontWalkIntoAndDontTranslateAsChildElement(diffTable, DEFAULT_CONFIG)).toBe(true)
126+
expect(hasNoWalkAncestor(td, DEFAULT_CONFIG)).toBe(true)
127+
})
107128
})

0 commit comments

Comments
 (0)