Skip to content

fix: 修复空文本被翻译的问题#206

Open
Alkacid wants to merge 5 commits intoBistutu:mainfrom
Alkacid:main
Open

fix: 修复空文本被翻译的问题#206
Alkacid wants to merge 5 commits intoBistutu:mainfrom
Alkacid:main

Conversation

@Alkacid
Copy link
Copy Markdown

@Alkacid Alkacid commented Apr 8, 2026

在多处添加了trim并检查待翻译文本是否为空的判断,解决了reddit帖子、github 贡献者头像处等网站出现大量类似“请提供翻译文本”的bug

前:
Image_2026-04-08_21-58-52_kolw51dn agp
Image_2026-04-08_21-59-18_uffq122t 2ee

后:
Image_2026-04-08_21-56-57_llhsa0ix ep4

Image_2026-04-08_21-56-34_4xyz31pn y3d

Summary by Sourcery

Prevent translation of empty or whitespace-only text nodes across the DOM traversal and translation pipeline.

Bug Fixes:

  • Skip empty or whitespace-only text nodes when collecting DOM nodes for translation to avoid generating placeholder translations.
  • Avoid invoking single or bilingual translations when the target node content is empty after trimming whitespace.
  • Short-circuit translateText calls when the input is empty or whitespace-only, returning the original string instead of calling the translation service.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Apr 8, 2026

Reviewer's Guide

This PR prevents empty or whitespace-only DOM text from being sent for translation by normalizing text nodes and adding early-return guards around translation entry points, eliminating bogus “请提供翻译文本” output on elements like Reddit posts and GitHub contributor avatars.

Sequence diagram for updated DOM text translation flow with empty-text guard

sequenceDiagram
    actor User
    participant Page as WebPage
    participant Dom as grabAllNode
    participant Trans as handleSingleTranslation
    participant API as translateText
    participant Service as TranslationService

    User->>Page: Trigger page translation
    Page->>Dom: grabAllNode(rootNode)
    Dom->>Dom: Iterate TreeWalker nodes
    alt Text node with nonempty cleanedText
        Dom-->>Page: Element list includes node
    else Text node empty or whitespace-only
        Dom-->>Page: Text node skipped (FILTER_SKIP)
    end

    loop For each node in element list
        Page->>Trans: handleSingleTranslation(node, slide)
        Trans->>Trans: cleanedText = node.textContent.replace(/\s|\u3000/g, '')
        alt cleanedText is empty
            Trans-->>Page: Return without translation
        else cleanedText not empty
            Trans->>API: translateText(origin, context, options)
            API->>API: cleanedOrigin = origin.replace(/\s|\u3000/g, '')
            alt cleanedOrigin empty
                API-->>Trans: Return origin (no request)
            else cleanedOrigin not empty
                API->>Service: Request translation
                Service-->>API: Translated text
                API-->>Trans: Translated text
                Trans-->>Page: Replace node text with translation
            end
        end
    end
Loading

File-Level Changes

Change Details Files
Filter out empty/whitespace-only DOM text nodes when collecting nodes to translate.
  • Update TreeWalker acceptNode logic so Text nodes are only accepted if their content, after removing standard and full-width spaces, has non-zero length.
  • Refine block-level node inspection to treat child elements/text as non-empty only if their text content remains non-empty after removing standard and full-width spaces.
entrypoints/main/dom.ts
Short‑circuit translation flows when the source node’s text is empty or whitespace-only after normalization.
  • Normalize node.textContent by removing standard and full-width spaces, reuse it via a cleanedText variable, and return early from bilingualTranslate if it is empty.
  • Apply the same cleanedText normalization and emptiness check in singleTranslate before language detection and spinner insertion.
  • Ensure language detection uses the cleaned, whitespace-stripped text to avoid mis-detection on empty strings.
entrypoints/main/trans.ts
Guard the core translateText API against empty or whitespace-only input.
  • Normalize origin by stripping standard and full-width spaces into cleanedOrigin.
  • If cleanedOrigin is empty, immediately return the original origin string (or empty string) without calling detectlang or any translation service.
  • Retain the existing language-equality short‑circuit, now effectively only applied to non-empty origin text.
entrypoints/utils/translateApi.ts

Possibly linked issues

  • #(unknown): Issue 反馈翻译界面异常,PR 通过忽略空文本翻译修复界面显示错误,应关联。

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

Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot left a comment

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:

  • In grabAllNode you now run replace on every text node inside the TreeWalker and again inside the child loop; consider centralizing the non-empty-text detection for nodes to avoid duplicated work and make it clearer what qualifies as 'translatable' content.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `grabAllNode` you now run `replace` on every text node inside the TreeWalker and again inside the child loop; consider centralizing the non-empty-text detection for nodes to avoid duplicated work and make it clearer what qualifies as 'translatable' content.

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.

1 participant