HtmlRewriter duplicates head insert on pages embedding a nested HTML document

未關閉
#417 1 則留言 1 個 reaction 已指派 1 人 在 GitHub 檢視

維護者通常 1 天內回覆

@pawank925 已經在處理了。

開始於 2026年12月2日。

  • #418 來自 @pawank925 —— 未關閉

評估

這個 Issue 還沒有評估資料。

描述

HtmlRewriter inserts pre_head_insert / post_head_insert on every <head> tag it encounters, not just the document's own one. Any page that embeds a second, nested HTML document in its body therefore gets the warc2zim head insert twice, and the duplicated inline script dies with a SyntaxError.

Root cause

src/zimscraperlib/rewriting/html.py:

def handle_starttag(self, tag: str, attrs: AttrsList, *, auto_close: bool = False):
    ...
    if tag == "head" and self.pre_head_insert:
        self.send(self.pre_head_insert)

def handle_endtag(self, tag: str):
    self.html_rewrite_context = None
    if tag == "head" and self.post_head_insert:
        self.send(self.post_head_insert)
    self.send(f"</{tag}>")

There is no "already inserted" guard, so the insert is emitted once per <head> start/end tag in the stream.

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

rewriter = HtmlRewriter(
    url_rewriter=ArticleUrlRewriter(article_url=HttpUrl("https://example.com/page")),
    pre_head_insert="<!--PRE-->",
    post_head_insert="<!--POST-->",
    notify_js_module=None,
)
out = rewriter.rewrite(
    "<html><head><title>t</title></head><body>"
    "<div><html><head></head><body>inner</body></html></div>"
    "</body></html>"
)
print(out.content)
print("PRE:", out.content.count("<!--PRE-->"), "POST:", out.content.count("<!--POST-->"))
<html><head><!--PRE--><title>t</title><!--POST--></head><body><div><html><head><!--PRE--><!--POST--></head><body>inner</body></html></div></body></html>
PRE: 2 POST: 2
Real-world impact

Hit on medcoe.army.mil/borden (zimfarm recipe medcoe.army.mil_en_borden), where the CMS stores each page's body as a complete <!DOCTYPE html><html><head>… document inside the Gatsby page data. Every HTML page in the resulting ZIM contains the <!-- WB Insert --> block twice, so head_insert.html's inline script runs a second time and throws on every page load:

Uncaught SyntaxError: Identifier 'current_url' has already been declared

Checked against a page of the produced ZIM:

$ grep -c 'const current_url' page.html
2
$ grep -c '<head>' page.html
2

It is currently benign — the first copy has already called window._WBWombatInit(wbinfo), and the second block fails at parse time before it can do anything — but it means every page of such a ZIM throws a SyntaxError on load, which is noise that masks real errors when debugging a ZIM, and it is only benign by accident: the head insert is not idempotent, and any future statefulness in it would misbehave.

It also wastes a copy of the insert (two <script> tags for wombat.js and wombatSetup.js) in every page's payload.

Suggested fix

Guard both inserts so they only fire for the first <head> of the document, e.g. a self.head_insert_done flag set when pre_head_insert is emitted and checked by both handlers, so the nested document's <head> is passed through untouched.

Worth a regression test covering the nested-document case alongside the existing head-insert tests.

主要語言
Python
星號
33
分支
36
平均合併
1 天 10 小時
30 天內合併 PR
19

環境準備

這個專案沒有提供開發容器、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 摘要。