HtmlRewriter: title is cut at the first `&` (entity or bare ampersand)
维护者通常 1 天内回复
评估
调研方向
Issue指出了HtmlRewriter中的标题处理,并指定tests/rewriting/test_html_rewriting.py作为测试文件。阅读handle_data、handle_entityref、handle_charref和handle_endtag("title")中的标题处理,然后运行现有的标题提取测试。完成标准是:标题保留第一个与号之后的文本和已解码实体,第一个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 & Jerry</title></head></html>",
"<html><head><title>Rock & Roll – My Blog</title></head></html>",
"<html><head><title>L'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 & Jerry |
'Tom' |
'Tom & Jerry' |
Rock & Roll – My Blog |
'Rock' |
'Rock & Roll – My Blog' |
L'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 &, ', 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, appenddatato 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"), setself.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,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
openzim/python-scraperlib 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 85/100
openzim/python-scraperlib#414 ·
维护者通常 1 天内回复
-
[next major] Remove deprecated 'convert' parameter for optimize_image function inside optimization.py可能已有人在做 @Sriram-Nambiar 于 40 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 68/100
openzim/python-scraperlib#292 · 1 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
compute_tags keeps empty tags可能已有人在做 @ahmershahdev 于 1 天前认领。 未关闭
难度 1/5 1 小时以内 新手友好度 32/100
openzim/python-scraperlib#412 · 1 条评论 ·
维护者通常 1 天内回复
-
Release 5.7.0可能已有人在做 @benoit74 于 4 天前认领。 未关闭
openzim/python-scraperlib#411 · 已指派 1 人 ·
维护者通常 1 天内回复
-
colorthief is unmaintained and will break with Pillow 14可能已有人在做 @anshuman83-40 于 26 天前认领。 未关闭
难度 4/5 3-5 天 新手友好度 48/100
openzim/python-scraperlib#365 · 2 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
查看 openzim/python-scraperlib 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 74/100
vllm-project/vllm#69791 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
-
bug
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
BCStudentSoftwareDevTeam/cas#481 ·
维护者通常 7 天内回复
-
难度 1/5 1 小时以内 新手友好度 68/100
维护者通常 1 天内回复