Skip to content

DOMDocument formatOutput fails when node contains both child text and element nodes #16552

@billynoah

Description

@billynoah

Description

This behaves as expected:

$dom = new DOMDocument('1.0', 'UTF-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$root = $dom->createElement('root');
$dom->appendChild($root);
$root->appendChild($dom->createElement('child', 'nodevalue B'));
echo $dom->saveXml();

Output:

<root>
  <child>nodevalue B</child>
</root>

But when a node contains both a child text node and a child element node the output is collapsed. The following code:

$dom = new DOMDocument('1.0', 'UTF-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$root = $dom->createElement('root', 'nodeValue A');
$dom->appendChild($root);
$root->appendChild($dom->createElement('child', 'nodevalue B'));
echo $dom->saveXml();

Resulted in this output:

<root>nodeValue A<child>nodevalue B</child></root

But I expected this output instead:

<root>nodeValue A
  <child>nodevalue B</child>
</root>

PHP Version

PHP 8.3.12

Operating System

Ubuntu 24.04.01

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions