Skip to content

Fix duplicate head insert on pages embedding a nested HTML document - #418

Open
pawank925 wants to merge 1 commit into
openzim:mainfrom
pawank925:issue-417-duplicate-head-insert
Open

pawank925 wants to merge 1 commit into
openzim:mainfrom
pawank925:issue-417-duplicate-head-insert

Conversation

@pawank925

Copy link
Copy Markdown
Contributor

Fix duplicate head insert on pages embedding a nested HTML document

HtmlRewriter now emits pre_head_insert / post_head_insert only for the first

of the document. A head_insert_done guard prevents the inserts from being applied again to a nested document's , which previously caused the inline script to run twice and throw a SyntaxError.

Fixes #417

@pawank925
pawank925 force-pushed the issue-417-duplicate-head-insert branch from 3bb693a to 9dcea1e Compare December 1, 2026 15:34
@pawank925
pawank925 marked this pull request as draft December 1, 2026 15:35
@pawank925
pawank925 force-pushed the issue-417-duplicate-head-insert branch from 9dcea1e to ad5f612 Compare December 1, 2026 15:36
@pawank925
pawank925 marked this pull request as ready for review December 1, 2026 15:38
@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (0f8b295) to head (428ea70).

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #418   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           42        42           
  Lines         2990      2998    +8     
  Branches       457       459    +2     
=========================================
+ Hits          2990      2998    +8     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@benoit74 benoit74 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be a bit more defensive for bad HTML? Something like <html><head><head></head></head>... would still include the head insert twice AFAIK (at least it is not tested)

@pawank925
pawank925 force-pushed the issue-417-duplicate-head-insert branch 2 times, most recently from 94b3ebe to 437ea98 Compare December 3, 2026 14:17
@pawank925

Copy link
Copy Markdown
Contributor Author

@benoit74 Good catch — the guard was only set on </head>, so <html><head><head></head></head> emitted the pre-insert twice. It is now set on the first opening <head>, and the post-insert is emitted only once for that head (a stray </head> no longer emits one). Added regression tests for nested, repeated and stray head tags.

@pawank925
pawank925 requested a review from benoit74 December 3, 2026 14:18

@benoit74 benoit74 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, forgot to ask about a CHANGELOG entry. Thank you

HtmlRewriter now emits pre_head_insert / post_head_insert only for the first
<head> of the document. The guard is set when that opening tag is seen, so
malformed HTML with repeated or nested <head> tags cannot emit the pre-insert
twice, and a stray </head> cannot emit the post-insert on its own. Pages that
embed a second HTML document in their body no longer run the inline head
insert script twice.

Fixes openzim#417
@pawank925
pawank925 force-pushed the issue-417-duplicate-head-insert branch from 437ea98 to 428ea70 Compare December 3, 2026 14:46
@pawank925

Copy link
Copy Markdown
Contributor Author

Added the changelog entry too: HtmlRewriter now inserts the head block only once for the document's first <head> (#417).

@pawank925
pawank925 requested a review from benoit74 December 3, 2026 14:47

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HtmlRewriter duplicates head insert on pages embedding a nested HTML document

2 participants