Skip to content

Fix issue where HTML file parsing may lose content. - #11536

Merged
KevinHuSh merged 1 commit into
infiniflow:mainfrom
myoldcat:fix-html-parser
Nov 27, 2025
Merged

KevinHuSh merged 1 commit into
infiniflow:mainfrom
myoldcat:fix-html-parser

Conversation

@myoldcat

@myoldcat myoldcat commented Nov 26, 2025 •

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Problem Description

When parsing HTML files, some page content may be lost.
For example, text inside nested <font> tags within multiple <div> elements (e.g., <div><font>Text_1</font></div><div><font>Text_2</font></div>) fails to be preserved correctly.

Root Cause #1: Block ID propagation is interrupted
  1. Block ID generation: When the parser encounters a <div>, it generates a new block_id because <div> belongs to BLOCK_TAGS.
  2. Recursive processing: This block_id is passed down recursively to process the <div>’s child nodes.
  3. Interruption occurs: When processing a child <font> tag, the code enters the else branch of read_text_recursively (since <font> is a Tag).
  4. Bug location: The first line in this else branch explicitly sets block_id = None.
    • This discards the valid block_id inherited from the parent <div>.
    • Since <font> is not in BLOCK_TAGS, it does not generate a new block_id, so it passes None to its child text nodes.
  5. Consequence: The extracted text nodes have an empty block_id in their metadata. During the subsequent merge_block_text step, these texts cannot be correctly associated with their original <div> block due to the missing ID. As a result, all text from <font> tags gets merged together, which then triggers a second issue during concatenation.
  6. Solution: Remove the forced reset of block_id to None. When the current tag (e.g., <font>) is not a block-level element, it should inherit the block_id passed down from its parent. This ensures consistent ownership across the hierarchy: div → font → text.
Root Cause #2: Data loss during text concatenation
  1. The line current_content += (" " if current_content else "" + content) has a misplaced parenthesis. When current_content is non-empty (True):
    • The ternary expression evaluates to " " (a single space).
    • The code executes current_content += " ".
    • Result: Only a space is appended—the new content string is completely discarded.

Type of change

  • Bug Fix (non-breaking change which fixes an issue)

@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. 🐞 bug Something isn't working, pull request that fix bug. labels Nov 26, 2025
@myoldcat

Copy link
Copy Markdown
Contributor Author

Here is a test file.
test_1.html

@KevinHuSh KevinHuSh added the ci Continue Integration label Nov 26, 2025
@KevinHuSh
KevinHuSh marked this pull request as draft November 26, 2025 11:59
@KevinHuSh
KevinHuSh marked this pull request as ready for review November 26, 2025 11:59
@KevinHuSh
KevinHuSh merged commit 8c28587 into infiniflow:main Nov 27, 2025
@myoldcat
myoldcat deleted the fix-html-parser branch November 27, 2025 01:47
clifftseng pushed a commit to clifftseng/ragflow that referenced this pull request Feb 9, 2026
### What problem does this PR solve?

##### Problem Description
When parsing HTML files, some page content may be lost.  
For example, text inside nested `<font>` tags within multiple `<div>`
elements (e.g.,
`<div><font>Text_1</font></div><div><font>Text_2</font></div>`) fails to
be preserved correctly.

###### Root Cause infiniflow#1: Block ID propagation is interrupted
1. **Block ID generation**: When the parser encounters a `<div>`, it
generates a new `block_id` because `<div>` belongs to `BLOCK_TAGS`.
2. **Recursive processing**: This `block_id` is passed down recursively
to process the `<div>`’s child nodes.
3. **Interruption occurs**: When processing a child `<font>` tag, the
code enters the `else` branch of `read_text_recursively` (since `<font>`
is a Tag).
4. **Bug location**: The first line in this `else` branch explicitly
sets **`block_id = None`**.
- This discards the valid `block_id` inherited from the parent `<div>`.
- Since `<font>` is not in `BLOCK_TAGS`, it does not generate a new
`block_id`, so it passes `None` to its child text nodes.
5. **Consequence**: The extracted text nodes have an empty `block_id` in
their `metadata`. During the subsequent `merge_block_text` step, these
texts cannot be correctly associated with their original `<div>` block
due to the missing ID. As a result, all text from `<font>` tags gets
merged together, which then triggers a second issue during
concatenation.
6. **Solution:** Remove the forced reset of `block_id` to `None`. When
the current tag (e.g., `<font>`) is not a block-level element, it should
inherit the `block_id` passed down from its parent. This ensures
consistent ownership across the hierarchy: `div` → `font` → `text`.

###### Root Cause infiniflow#2: Data loss during text concatenation
1. The line `current_content += (" " if current_content else "" +
content)` has a misplaced parenthesis. When `current_content` is
non-empty (`True`):
    - The ternary expression evaluates to `" "` (a single space).
    - The code executes `current_content += " "`.
- **Result**: Only a space is appended—**the new `content` string is
completely discarded**.

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants