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

Ouverte Adaptée aux débutants
#401 3 commentaires 0 réactions 1 personne assignée Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

@rajanpanth y travaille déjà.

Depuis le 12/11/2026.

  • #402 par @pawank925 — fermée sans fusion

Évaluation

Difficulté
2/5
Temps estimé
1-3 heures
Accessibilité débutants
77/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
python
Domaine
backend

Piste de recherche

L’issue identifie la gestion des titres dans HtmlRewriter et indique tests/rewriting/test_html_rewriting.py pour les tests. Lisez la gestion des titres dans handle_data, handle_entityref, handle_charref et handle_endtag("title"), puis exécutez les tests existants d’extraction des titres. Le résultat attendu est que les titres conservent le texte et les entités décodées après la première esperluette, que le premier title reste prioritaire et que le contenu réécrit reste inchangé.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

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.

Langage dominant
Python
Étoiles
33
Forks
34
Merge moyen
1 j 8 h
PR mergées (30 j)
24

Préparer son environnement

Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de openzim/python-scraperlib

Toutes les issues de openzim/python-scraperlib

Issues similaires

Plus d'issues Python

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.