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

Đang mở Phù hợp với người mới
#401 3 bình luận 0 reaction 1 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@rajanpanth đang làm issue này rồi.

Từ ngày 12/11/2026.

  • #402 của @pawank925 — đã đóng, không merge

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
77/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
backend

Hướng nghiên cứu

Issue xác định phần xử lý tiêu đề trong HtmlRewriter và chỉ định tests/rewriting/test_html_rewriting.py để viết kiểm thử. Đọc phần xử lý tiêu đề trong handle_data, handle_entityref, handle_charref và handle_endtag("title"), sau đó chạy các kiểm thử trích xuất tiêu đề hiện có. Kết quả cần đạt là tiêu đề giữ lại văn bản và các entity đã giải mã sau dấu ampersand đầu tiên, title đầu tiên vẫn được ưu tiên và nội dung sau khi viết lại không thay đổi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
33
Fork
34
Merge trung bình
1 ngày 8 giờ
Pull request đã merge (30 ngày)
24

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của openzim/python-scraperlib

Tất cả issue của openzim/python-scraperlib

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.