fix: 段落识别被 outerHTML 体积误伤,剔除属性噪声后再判断大小 - #226
Open
mugaaaaa wants to merge 1 commit into
Open
Conversation
checkTextSize 用原始 outerHTML 长度判断节点是否过大。维基百科等站点 的引用 <sup> 携带巨型 data-mw JSON 属性,导致正文正常但 outerHTML 超过 4096 的段落被误判为过长而跳过,进而退化为逐段碎片翻译([13]、[c]、链接 被单独翻译),整段正文反而无法翻译。 修复:文本量仍以 textContent(>3072)为准;outerHTML 超过 4096 时先 剔除属性值,再按真实的 HTML 结构体积判断,避免冗长属性虚高误伤含大量 引用的正常段落。 验证:对 https://en.wikipedia.org/wiki/Inanna 真实页面用 jsdom 复现, 修复前段落被拆成 14 个碎片、全页 109 个段落中 64 个被误拒;修复后整段 识别、全页仅 1 个空段落仍跳过,且 0 回归。
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdjusts paragraph length checks to use cleaned HTML structure length instead of raw outerHTML size, preventing large attribute values (like Wikipedia’s data-mw JSON) from causing normal content to be skipped. Flow diagram for updated checkTextSize length logicflowchart TD
Start([checkTextSize node]) --> A{node.textContent.length > 3072}
A -->|true| TooLong1([return true])
A -->|false| B{node.outerHTML && node.outerHTML.length > 4096}
B -->|false| D{node.textContent.length < 3}
B -->|true| C[compute structureLength via outerHTML.replace]
C --> E{structureLength > 4096}
E -->|true| TooLong2([return true])
E -->|false| D
D -->|true| TooShort([return true])
D -->|false| OK([return false])
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Consider extracting the attribute-stripping and structure-length logic in
checkTextSizeinto a small helper function to improve readability and make future adjustments easier. - The regex used to strip attributes (
\s[a-zA-Z-]+(=...)?) may miss valid attribute names that contain characters outside[A-Za-z-](e.g.,data:foo, non‑ASCII), so consider broadening the character class or leveraging the DOM API for attribute removal. - For very large
outerHTMLstrings this doublereplacepass could be relatively expensive; if performance becomes a concern, consider short-circuiting earlier or limiting this work to known problematic tag types.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider extracting the attribute-stripping and structure-length logic in `checkTextSize` into a small helper function to improve readability and make future adjustments easier.
- The regex used to strip attributes (`\s[a-zA-Z-]+(=...)?`) may miss valid attribute names that contain characters outside `[A-Za-z-]` (e.g., `data:foo`, non‑ASCII), so consider broadening the character class or leveraging the DOM API for attribute removal.
- For very large `outerHTML` strings this double `replace` pass could be relatively expensive; if performance becomes a concern, consider short-circuiting earlier or limiting this work to known problematic tag types.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
checkTextSize用原始outerHTML长度判断节点是否过大。维基百科等站点的引用<sup>携带巨型 data-mw JSON 属性,导致正文正常但outerHTML超过 4096 的段落被误判为过长而跳过。正常使用悬浮翻译以争端时,无法识别段落。而使用全文翻译时,观察到其退化为如下图所示的逐段碎片翻译(如[13]、[c]等引用链接和其他词条的超链接被单独翻译),整段正文反而无法翻译。鼠标悬浮翻译在图中的 Etymology 下的段落时,点击 ctrl 无法正常识别并翻译。与 Issue #128 中展示的是同一个问题。

使用全文翻译时,会观察到其退化为逐段碎片翻译,且只有引用标记、其他词条链接等超链接识别为可翻译部分:

Fixes #128
修复
文本量仍以 textContent(>3072)为准;outerHTML 超过 4096 时先剔除属性值,再按真实的 HTML 结构体积判断,避免冗长属性虚高误伤含大量引用的正常段落。
验证
对 https://en.wikipedia.org/wiki/Inanna 真实页面用 jsdom 复现,修复前段落被拆成 14 个碎片、全页 109 个段落中 64 个被误拒;修复后整段识别、全页仅 1 个空段落仍跳过,且 0 回归。
运行浏览器测试,对同样文段进行翻译:

最开始发现该问题的地方是英文维基百科 https://en.wikipedia.org/wiki/Inanna 的 Etymology 段落,在原 main 分支上,该 bug 可以稳定复现。经测试,确认上述问题已被该 PR 修复。
Summary by Sourcery
Bug Fixes: