Skip to content

Ignore the form element pointer throughout template contents - #12911

Merged
annevk merged 1 commit into
mainfrom
form-element-pointer-template-contents
Sep 7, 2026
Merged

annevk merged 1 commit into
mainfrom
form-element-pointer-template-contents

Conversation

@annevk

@annevk annevk commented Sep 6, 2026 •

Copy link
Copy Markdown
Member

The form element pointer is deliberately not used inside template
contents, but the conditions expressing that only tested the stack of
open elements. A fragment parse whose context element is a template
element produces template contents without ever pushing a template
element onto that stack, so the pointer was seeded from the context
element's ancestors and then used: a nested <form> was dropped, and
controls could be associated with a form outside the template.

Introduce "parsing template contents", which covers that case as well,
and use it for the <form> start and end tags and when associating a
control with the form element pointer.

While there, the "in table" <form> start tag dropped the element
entirely while parsing template contents. It now inserts the element and
skips only the pointer assignment, as the "in body" one already did.

Also require the parser's fragment context element to be null when
associating a control with the form element pointer. No implementation
does this during a fragment parse, where the pointer either comes from the
context element's ancestors or points into the fragment being built.
Association by ancestry is unaffected, so only markup that leaves a
control outside its form can tell the difference.

Tests: web-platform-tests/wpt#62488

Fixes #12257.

(See WHATWG Working Mode: Changes for more details.)

The form element pointer is deliberately not used inside template
contents, but the conditions expressing that only tested the stack of
open elements. A fragment parse whose context element is a `template`
element produces template contents without ever pushing a `template`
element onto that stack, so the pointer was seeded from the context
element's ancestors and then used: a nested `<form>` was dropped, and
controls could be associated with a form outside the template.

Introduce "parsing template contents", which covers that case as well,
and use it for the `<form>` start and end tags and when associating a
control with the form element pointer.

While there, the "in table" `<form>` start tag dropped the element
entirely while parsing template contents. It now inserts the element and
skips only the pointer assignment, as the "in body" one already did.

Also require the parser's fragment context element to be null when
associating a control with the form element pointer. No implementation
does this during a fragment parse, where the pointer either comes from the
context element's ancestors or points into the fragment being built.
Association by ancestry is unaffected, so only markup that leaves a
control outside its form can tell the difference.

Tests: web-platform-tests/wpt#62488

Fixes #12257.
@annevk annevk added the topic: parser The HTML parser and serializer, including fragment parsing and character references label Sep 6, 2026
@annevk
annevk requested review from dbaron, noamr and zcorpan September 6, 2026 10:34
Comment thread source Resolved
@zcorpan

zcorpan commented Sep 7, 2026

Copy link
Copy Markdown
Member

cc @hsivonen @keithamus

@annevk
annevk merged commit 9ed7f27 into main Sep 7, 2026
2 checks passed
@annevk
annevk deleted the form-element-pointer-template-contents branch September 7, 2026 13:23
webkit-commit-queue pushed a commit to annevk/WebKit that referenced this pull request Sep 8, 2026
…e> context element

https://bugs.webkit.org/show_bug.cgi?id=323586

Reviewed by Chris Dumez.

The form element pointer is meant to be ignored inside template contents, but
isParsingTemplateContents() only consulted the stack of open elements. A fragment
parse whose context element is a template element produces template contents
without ever pushing a template element onto that stack, so setting innerHTML on
a template element used the pointer anyway. Given

  <form><template id=t></template></form>

then t.innerHTML = "<div><form></div><input></form>" dropped the nested <form>,
because the pointer had been seeded from the template's form ancestor.

Two more cases went wrong without any form ancestor, because a <form> in the
markup set the pointer. t.innerHTML = "<form><form>" dropped the second <form>,
and t.innerHTML = "<br>BC<form>D<div>E</form>F</div>G" took the pointer-based
</form> branch, which removes the form element from the stack of open elements
and leaves the <div> open, rather than the branch used for template contents,
which pops through it. The same markup written inside <template> in a document
produced a different tree as a result.

Extend isParsingTemplateContents() to also cover a template context element, and
pass the result into insertHTMLFormElement() instead of having it re-derive the
narrower condition from the stack of open elements, so the "in body" and "in
table" <form> start tags agree with it.

Matches whatwg/html#12911.

Tests: imported/w3c/web-platform-tests/html/syntax/parsing-html-fragments/fragment-parse-form-in-template-001.html
       imported/w3c/web-platform-tests/html/syntax/parsing-html-fragments/fragment-parse-form-in-template-002.html
       imported/w3c/web-platform-tests/html/syntax/parsing-html-fragments/fragment-parse-form-owner-001.html
       imported/w3c/web-platform-tests/html/syntax/parsing/form-element-pointer-in-template.html

Tests upstream: web-platform-tests/wpt#62488

Canonical link: https://commits.webkit.org/320641@main
lexborisov added a commit to TimWolla/lexbor that referenced this pull request Sep 26, 2026
The form element pointer is not used inside template contents, but the
parser checked only for a template element on the stack of open elements.
A fragment parse with a template context element produces template
contents without that element on the stack, so a nested form was dropped
and a form end tag closed the element the pointer referred to instead of
the form in scope. The form start tag in the "in table" insertion mode
was also ignored in template contents instead of being inserted.

whatwg/html#12911
txgk added a commit to gumbo-parser/gumbo-parser that referenced this pull request Sep 27, 2026
fixes 6 new test cases from WPT scenarios added in the last commit

whatwg/html#12908
whatwg/html#12911
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: parser The HTML parser and serializer, including fragment parsing and character references

Development

Successfully merging this pull request may close these issues.

interaction of "form element pointer", fragment parsing, and <template> elements seems incorrect

3 participants