HtmlRewriter: title is cut at the first `&` (entity or bare ampersand)

未關閉 適合新手
#401 3 則留言 0 個 reaction 已指派 1 人 在 GitHub 檢視

維護者通常 1 天內回覆

@rajanpanth 已經在處理了。

開始於 2026年11月12日。

  • #402 來自 @pawank925 —— 已關閉,未合併

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
77/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
python
領域
backend

研究方向

Issue指出了HtmlRewriter中的標題處理,並指定tests/rewriting/test_html_rewriting.py作為測試檔案。閱讀handle_data、handle_entityref、handle_charref和handle_endtag("title")中的標題處理,然後執行現有的標題擷取測試。完成條件是:標題保留第一個 ampersand 之後的文字和已解碼實體,第一個title仍然優先,且改寫後的內容保持不變。

由索引模型根據 Issue 內容生成。

描述

HtmlRewriter.rewrite(...).title only keeps the text before the first & in <title>.

from zimscraperlib.html import find_title_in
from zimscraperlib.rewriting.html import HtmlRewriter
from zimscraperlib.rewriting.url_rewriting import ArticleUrlRewriter, HttpUrl

def title(html: str) -> str:
    url_rewriter = ArticleUrlRewriter(article_url=HttpUrl("https://www.example.com/page.html"))
    return HtmlRewriter(url_rewriter, None, None, None).rewrite(html).title

for html in (
    "<html><head><title>Tom &amp; Jerry</title></head></html>",
    "<html><head><title>Rock &amp; Roll &#8211; My Blog</title></head></html>",
    "<html><head><title>L&#039;Illustre Maurin</title></head></html>",
):
    print(repr(title(html)), "| find_title_in:", repr(find_title_in(html, "text/html")))
<title> HtmlRewriter(...).rewrite().title find_title_in (same library)
Tom &amp; Jerry 'Tom' 'Tom & Jerry'
Rock &amp; Roll &#8211; My Blog 'Rock' 'Rock & Roll – My Blog'
L&#039;Illustre Maurin 'L' "L'Illustre Maurin"

Tested on main (d1deb81, 5.5.3-dev0), Python 3.14.

Why it matters

warc2zim uses this value as the entry title: Rewriter.rewrite_html() returns HtmlRewriter(...).rewrite(...) and items.py does self.title, self.content = rewriter.rewrite(...). So any page whose title contains &amp;, &#39;, &nbsp; and so on gets a truncated title in the ZIM, which is what title search and suggestions show. French and Italian titles with an escaped apostrophe are an easy way to hit it.

Cause

The parser runs with convert_charrefs=False, so HTMLParser delivers a title in pieces: handle_data("Tom "), handle_entityref("amp"), handle_data(" Jerry"). handle_data stores the first piece and then ignores the rest because self.title is no longer None:

if self.html_rewrite_context == "title" and self.title is None:
    self.title = data.strip()

Proposed fix

Collect the pieces while inside the first <title> and join them when it closes:

  • in handle_data, append data to a list instead of assigning the title;
  • in handle_entityref / handle_charref, append the decoded reference (html.unescape(f"&{name};") / html.unescape(f"&#{name};"));
  • in handle_endtag("title"), set self.title = "".join(parts).strip() if it isn't set yet.

The rewritten HTML is not affected, only the extracted title. "First <title> wins" stays as it is, so test_extract_title still holds. I tried this as a subclass outside the repo and it gives the same titles as find_title_in for the cases above, with identical content.

I can open a PR with this and tests in tests/rewriting/test_html_rewriting.py if the approach looks right to you.

Disclosure: I found this with the help of an AI assistant (Claude Code). The reproduction above was run locally against main.

主要語言
Python
星號
33
分支
34
平均合併
1 天 8 小時
30 天內合併 PR
24

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

openzim/python-scraperlib 的其他 Issue

查看 openzim/python-scraperlib 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。