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

Open Beginner friendly
#401 3 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@rajanpanth is already working on this.

Since Nov 12, 2026.

  • #402 by @pawank925 — closed without merging

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
77/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend

Research direction

The issue identifies HtmlRewriter title handling and names tests/rewriting/test_html_rewriting.py for tests. Read the title handling in handle_data, handle_entityref, handle_charref, and handle_endtag("title"), then run the existing title extraction tests. Done means titles retain text and decoded entities after the first ampersand, the first title still wins, and rewritten content is unchanged.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Python
Stars
33
Forks
34
Avg merge
1d 8h
Merged PRs (30d)
24

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from openzim/python-scraperlib

All issues in openzim/python-scraperlib

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.