Skip to content

Regression: DOMElement::appendChild(DOMDocument) TypeError — Zend DOMException Hierarchy Request Error (ext/dom/node.c) #22698

Closed

Description

@PurHur

Category

stdlib · php-src-strict · DOM tree mutation (pillar 4)

Problem

DOMDocument is a DOMNode subclass. Appending the document itself as a child of an element must raise DOMException with Zend message Hierarchy Request Error and code 3 (DOM_HIERARCHY_REQUEST_ERR). The VM rejects the argument in the typed parameter check with TypeError (expects argument #1 to be of type DOMNode, DOMDocument given) before hierarchy validation runs — despite instanceof DOMNode being true on the same object.

Sibling paths differ: insertBefore($doc, null) reaches a DOMException but with wrong message casing / code 0 (see related #22658/#22694 code-plumbing). replaceChild($doc, $child) and appendChild($ancestor) already emit code 3 correctly.

Probed 2026-07-23 on master VM vs host Zend 8.2.32.

Repro Zend 8.2+ VM (2026-07-23)
$e->appendChild($d) where $e is element in $d DOMException msg Hierarchy Request Error, code 3 TypeError — not DOMException
$d instanceof DOMNode true true
DOM_HIERARCHY_REQUEST_ERR 3 3

php-src reference

PHP implementation target

  • ext/dom/VmDom.php (appendChild / arg type check) — accept DOMDocument as DOMNode for the parameter, then throw DOMException('Hierarchy Request Error', DomExceptionConstants::HIERARCHY_REQUEST_ERR) like replaceChild already does
  • PHP-in-PHP only; no new runtime/*.c logic

Repro

./script/docker-exec.sh -- bash -lc 'php bin/vm.php -r '"'"'
$d = new DOMDocument();
$e = $d->createElement("a");
$d->appendChild($e);
echo "instanceof DOMNode=", $d instanceof DOMNode ? "yes" : "no", "\n";
try {
  $e->appendChild($d);
  echo "OK\n";
} catch (Throwable $ex) {
  echo get_class($ex), ": ", $ex->getMessage(), "\n";
  if ($ex instanceof DOMException) echo "code=", $ex->getCode(), "\n";
}
echo "DOM_HIERARCHY_REQUEST_ERR=", DOM_HIERARCHY_REQUEST_ERR, "\n";
'"'"''

Done when

Activity

  1. added
    bugSomething isn't working
    phase-4:stdlibPhase 4 – stdlib for web apps
    area:vmVirtual machine
    implementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claim
    on Jul 23, 2026
  2. PurHur commented on Jul 24, 2026

    @PurHur
    OwnerAuthor

    claim: PHP Compiler Ext — DOM & XML — starting this run

    Trust preflight (this host, before claim):

    • docker info: ok
    • bootstrap-selfhost-link: running in parallel with claim
    • Target: $e->appendChild($d) → DOMException Hierarchy Request Error code 3 (not TypeError)
  3. PurHur commented on Jul 24, 2026

    @PurHur
    OwnerAuthor

    Merged via PR #22800.

    Fix: appendChild type gates accept DOMDocument as DOMNode; VmDom::appendChild rejects with DOMException Hierarchy Request Error / code 3 (DomExceptionConstants::raiseHierarchyRequest).

    Verification:

    php bin/vm.php test/repro/issue_22698_appendchild_document_hierarchy.php
    # instanceof DOMNode=yes / DOMException: Hierarchy Request Error / code=3
    
    php bin/jit.php test/repro/issue_22698_appendchild_document_hierarchy.php  # same
    
    ./script/phpunit.sh --filter dom_appendchild_document_hierarchy  # OK (2 tests)
    php script/bootstrap-inventory.php --check  # OK 6508/6508
    

    Trust preflight: inventory/spine OK; north-star5-fast still red on master (gen-0 mismatch #8713) — unrelated to this DOM-only change.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:vmVirtual machinebugSomething isn't workingimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web appsstdlib

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions