Skip to content

Preserve HTML template fragments in Dom\Document::adoptNode() - #25800

Open
hohphu8 wants to merge 1 commit into
php:PHP-8.4from
hohphu8:fix-dom-adoptnode-template-fragments
Open

hohphu8 wants to merge 1 commit into
php:PHP-8.4from
hohphu8:fix-dom-adoptnode-template-fragments

Conversation

@hohphu8

@hohphu8 hohphu8 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

When Dom\Document::adoptNode() moves an HTML <template> element (directly, wrapped in another element, or nested inside another <template>) to a different document, php_dom_adopt_node() updates the node tree's document reference but leaves the template's fragment in the source document's private_data->template_fragments. The template contents are then missing from saveHtml() and innerHTML, and get freed when the source document is destroyed.

This adds php_dom_detach_templated_content() in ext/dom/private_data.{c,h} and updates php_dom_adopt_node() (ext/dom/document.c) to recursively detach and transfer template fragments in the adopted subtree to the destination document's private_data (including xmlSetTreeDoc, namespace reconciliation, < 2.13 libxml2 dictionary fixup, document ref transfer, and dom_add_element_ns_hook).

Adds ext/dom/tests/modern/spec/Document_adoptNode_template.phpt covering direct, wrapped, and nested <template> elements both before and after unset($src). Verified FAIL -> PASS on PHP-8.4 (Debug + ZTS) with ext/dom/tests/modern/ passing.

Found via LLM-assisted source inspection and manually reproduced

@hohphu8
hohphu8 force-pushed the fix-dom-adoptnode-template-fragments branch from e0f913a to d9468c7 Compare November 26, 2026 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant