diff --git a/CHANGELOG.md b/CHANGELOG.md index 77dcde7..7e01fce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- `HtmlRewriter` now inserts the head insert block only once, for the document's first ``: pages embedding a nested HTML document or malformed markup with repeated `` tags no longer run the inline script twice and throw a `SyntaxError` (#417) + ## [5.6.0] - 2026-11-20 ### Added diff --git a/src/zimscraperlib/rewriting/html.py b/src/zimscraperlib/rewriting/html.py index 35247d4..f9d748c 100644 --- a/src/zimscraperlib/rewriting/html.py +++ b/src/zimscraperlib/rewriting/html.py @@ -201,12 +201,16 @@ def __init__( self.pre_head_insert = pre_head_insert self.post_head_insert = post_head_insert self.notify_js_module = notify_js_module + self.head_insert_started = False + self.head_insert_done = False def rewrite(self, content: str) -> RewritenHtml: """Rewrite HTML code passed""" if self.output is not None: raise Exception("ouput should not already be set") # pragma: no cover self.output = io.StringIO() + self.head_insert_started = False + self.head_insert_done = False self.base_href = extract_base_href(content) self.css_rewriter = CssRewriter(self.url_rewriter, self.base_href) @@ -270,14 +274,18 @@ def handle_starttag(self, tag: str, attrs: AttrsList, *, auto_close: bool = Fals self.send(" />") else: self.send(">") - if tag == "head" and self.pre_head_insert: - self.send(self.pre_head_insert) + if tag == "head" and not self.head_insert_started: + self.head_insert_started = True + if self.pre_head_insert: + self.send(self.pre_head_insert) def handle_endtag(self, tag: str): """Overwrite handle_endtag from HTMLParser""" self.html_rewrite_context = None - if tag == "head" and self.post_head_insert: - self.send(self.post_head_insert) + if tag == "head" and self.head_insert_started and not self.head_insert_done: + self.head_insert_done = True + if self.post_head_insert: + self.send(self.post_head_insert) self.send(f"") def handle_startendtag(self, tag: str, attrs: AttrsList): diff --git a/tests/rewriting/test_html_rewriting.py b/tests/rewriting/test_html_rewriting.py index 71df415..d60208f 100644 --- a/tests/rewriting/test_html_rewriting.py +++ b/tests/rewriting/test_html_rewriting.py @@ -511,6 +511,68 @@ def test_head_insert(): ).replace("", "POST_HEAD_INSERT") +def test_head_insert_nested_document(): + """Head inserts must only be applied to the first of the document. + + Pages that embed a second, nested HTML document in their body (e.g. a CMS + storing a full page as data) must not get a duplicate insert, otherwise the + inline script runs twice and throws a SyntaxError. + """ + content = ( + "t" + "
inner
" + "" + ) + + url_rewriter = ArticleUrlRewriter(article_url=HttpUrl("http://kiwix.org/")) + result = ( + HtmlRewriter(url_rewriter, "PRE_HEAD_INSERT", "POST_HEAD_INSERT", None) + .rewrite(content) + .content + ) + + assert result == ( + "PRE_HEAD_INSERTtPOST_HEAD_INSERT" + "
inner
" + "" + ) + assert result.count("PRE_HEAD_INSERT") == 1 + assert result.count("POST_HEAD_INSERT") == 1 + + +@pytest.mark.parametrize( + "content,expected", + [ + ( + "x", + ( + "PRE_HEAD_INSERTPOST_HEAD_INSERT" + "x" + ), + ), + ( + "x", + ( + "PRE_HEAD_INSERTPOST_HEAD_INSERT" + "x" + ), + ), + ("x", "x"), + ], +) +def test_head_insert_malformed_head_tags(content: str, expected: str): + url_rewriter = ArticleUrlRewriter(article_url=HttpUrl("http://kiwix.org/")) + result = ( + HtmlRewriter(url_rewriter, "PRE_HEAD_INSERT", "POST_HEAD_INSERT", None) + .rewrite(content) + .content + ) + + assert result == expected + assert result.count("PRE_HEAD_INSERT") <= 1 + assert result.count("POST_HEAD_INSERT") <= 1 + + @pytest.mark.parametrize( "js_src,expected_js_module_path", [