Skip to content

fix: 段落识别被 outerHTML 体积误伤,剔除属性噪声后再判断大小 - #226

Open
mugaaaaa wants to merge 1 commit into
Bistutu:mainfrom
mugaaaaa:fix/size-check-ignore-attribute-noise
Open

fix: 段落识别被 outerHTML 体积误伤,剔除属性噪声后再判断大小#226
mugaaaaa wants to merge 1 commit into
Bistutu:mainfrom
mugaaaaa:fix/size-check-ignore-attribute-noise

Conversation

@mugaaaaa

@mugaaaaa mugaaaaa commented Aug 2, 2026

Copy link
Copy Markdown

问题

checkTextSize 用原始 outerHTML 长度判断节点是否过大。维基百科等站点的引用 <sup> 携带巨型 data-mw JSON 属性,导致正文正常但 outerHTML 超过 4096 的段落被误判为过长而跳过。正常使用悬浮翻译以争端时,无法识别段落。而使用全文翻译时,观察到其退化为如下图所示的逐段碎片翻译(如[13]、[c]等引用链接和其他词条的超链接被单独翻译),整段正文反而无法翻译。

鼠标悬浮翻译在图中的 Etymology 下的段落时,点击 ctrl 无法正常识别并翻译。与 Issue #128 中展示的是同一个问题。
image

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

Fixes #128

修复

文本量仍以 textContent(>3072)为准;outerHTML 超过 4096 时先剔除属性值,再按真实的 HTML 结构体积判断,避免冗长属性虚高误伤含大量引用的正常段落。

验证

https://en.wikipedia.org/wiki/Inanna 真实页面用 jsdom 复现,修复前段落被拆成 14 个碎片、全页 109 个段落中 64 个被误拒;修复后整段识别、全页仅 1 个空段落仍跳过,且 0 回归。

运行浏览器测试,对同样文段进行翻译:
image

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

Summary by Sourcery

Bug Fixes:

  • Prevent normal paragraphs with large attribute payloads (e.g. Wikipedia data-mw JSON on references) from being incorrectly rejected by the text size filter.

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 回归。
@sourcery-ai

sourcery-ai Bot commented Aug 2, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts 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 logic

flowchart 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])
Loading

File-Level Changes

Change Details Files
Refine text length heuristic to strip HTML attribute values from oversized outerHTML before deciding to skip a node, keeping the textContent threshold and short-text check intact.
  • Change checkTextSize logic from a single compound boolean expression to explicit stepwise checks with early returns.
  • Retain the textContent length >3072 rule as the primary over-length condition.
  • When outerHTML length exceeds 4096, compute a structureLength by removing HTML tags’ attribute segments (spaces plus attribute name and optional quoted value) and use this cleaned length to decide if the node is structurally too large.
  • Preserve the short-text rule by returning true when textContent length <3, only after size-based checks.
entrypoints/main/dom.ts

Possibly linked issues

  • #(not provided): PR adjusts text size checks to avoid skipping MediaWiki paragraphs, resolving the partial paragraph translation issue

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • 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.
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.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

基于MediaWiki的wiki部分文字/段落翻译不全

1 participant