HtmlRewriter duplicates head insert on pages embedding a nested HTML document
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
Issue này chưa được đánh giá.
Mô tả
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.
- Ngôn ngữ chính
- Python
- Star
- 33
- Fork
- 36
- Merge trung bình
- 1 ngày 10 giờ
- Pull request đã merge (30 ngày)
- 19
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
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của openzim/python-scraperlib
-
handle_user_provided_file leaves an empty temp fileCó thể đã có người làm @kevincommits đã nhận 3 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
openzim/python-scraperlib#414 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
HtmlRewriter: title is cut at the first `&` (entity or bare ampersand)Có thể đã có người làm @rajanpanth đã nhận 23 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
openzim/python-scraperlib#401 · 3 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[next major] Remove deprecated 'convert' parameter for optimize_image function inside optimization.pyCó thể đã có người làm @Sriram-Nambiar đã nhận 51 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
openzim/python-scraperlib#292 · 1 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Deduplicator: xxh32 digest can collide on large ZIMsCó thể đã có người làm @i-varunsharma đã nhận 1 ngày trước. Đang mở
openzim/python-scraperlib#426 · 3 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Metadata accepts empty BytesIO/file values although empty is not allowedCó thể đã có người làm @playlikeboy đã nhận 1 ngày trước. Đang mở
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 25/100
openzim/python-scraperlib#422 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của openzim/python-scraperlib
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
521xueweihan/HelloGitHub#4729 ·
-
Relatório de erro do LinuxToysĐang mởauto-reported bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày
-
cliproxy-quota: a tab around a credential email skips own-accounts and paced-accounts matchingĐang mởneeds-triage type:bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 90/100
pollen-robotics/reachy_mini_conversation_app#631 ·
Maintainer thường phản hồi trong vòng 7 ngày
-
stix-atlas.json: the same relationship id is used for several different mitigates relationshipsĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100