Skip to content

Place template elements in head before body - #614

Open
gyanu2507 wants to merge 2 commits into
html5lib:masterfrom
gyanu2507:fix/595-template-in-head
Open

gyanu2507 wants to merge 2 commits into
html5lib:masterfrom
gyanu2507:fix/595-template-in-head

Conversation

@gyanu2507

@gyanu2507 gyanu2507 commented Nov 2, 2026 •

Copy link
Copy Markdown

Summary

Before this change, a leading <template> was treated as an ordinary body element: InHead closed the head and reprocessed the token, so the element landed under <body>.

WHATWG says a template start tag in the "in body" insertion mode must use the "in head" rules, which place the element in <head>. This PR:

  • Handles template in InHead (and the AfterHead / InBody "process in head" lists)
  • Adds a simplified InTemplate phase so contents stay under the template element
  • Leaves template children as ordinary child nodes (html5lib still does not implement the template content document fragment)

Fixes #595

Test plan

  • pytest html5lib/tests/test_parser2.py (12 passed)
  • Manual check: <template></template> and <template>hi</template> put template under head

Handle template start tags with the in-head rules and a simplified
in-template phase so <template> before <body> lands in <head>, matching
WHATWG insertion-mode requirements. Contents remain ordinary children.

Fixes html5lib#595
The in-template phase saved the mode to return to in parser.originalPhase.
A nested <template> overwrote it, so closing the outer template left the
parser in the in-template phase, and EOF with more than one open template
reprocessed EOF in the same phase and hit the assertion in mainLoop. That
crash made 25 template.dat tree-walker tests fail on AppVeyor.

Keep a stack of saved phases, one per open template, close all open
templates at EOF in one step, and only match HTML-namespace template
elements.
@gyanu2507

Copy link
Copy Markdown
Author

The AppVeyor build (https://ci.appveyor.com/project/gsnedders/html5lib-python/builds/55011423) failed for two reasons, one of them mine.

The py37 jobs fail before running anything: the AppVeyor image no longer has Python 3.7 (py -VV gives "No suitable Python runtime found"). py27-optional can't build lxml ("Microsoft Visual C++ 9.0 is required"). Both happen on every recent PR build too, for example #612 (build 54986314) and #615 (build 55174918).

py27-base was this PR's fault. It passes on #612 and #615 but here had 75 failures, all template.dat tree-walker tests (25 cases on three tree builders). The parser crashed with the assert self.phase not in phases in mainLoop. The in-template phase stored the mode to return to in originalPhase, which a nested <template> overwrote, so EOF with nested open templates got reprocessed in the same phase. The parser tests for those cases are already listed as expected failures in .pytest.expect, which hid the crash. The tree-walker tests were skipped before because the parser output was wrong, so they only showed up once the output got closer.

Fixed in 7bb2df9: a stack of saved modes, one per open template, and EOF closes all open templates in one step. Two new tests in test_parser2.py cover nested templates and EOF with several templates open.

Locally on Python 3.12 the full suite passes and flake8 is clean. With lxml installed, the tree-construction and tree-walker tests match master plus the new tests. The py37 and py27-optional AppVeyor jobs will still fail for the image reasons above.

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.

<template> element placed in <body> instead of <head> (tree builder bug)

1 participant