dodov
dodov

Reputation: 5854

Newline appears after <br> tag in DOMDocument saveHTML()

I'm using the PHP DOM API to create some markup. It works OK, but a newline \n appears after <br> tags in certain cases. I managed to create a reprex:

$document = new DOMDocument();
$document->loadHTML('<div>text<br></div>');
$element = $document->getElementsByTagName('div')->item(0);

echo $document->saveHTML($element); // newline appears after <br>

For some reason, saveHTML() doesn't return the same markup that was initially loaded. As I've demonstrated in the repl, adding text around the <br> tag or using saveXML() works correctly.

Is this some special behavior defined in the DOM spec? The problem only appears to happen when the <br> tag is the first or last child, I.E. there's no text in between.

Upvotes: 0

Views: 464

Answers (1)

dodov
dodov

Reputation: 5854

This appears to have been a bug in libxml. The problem occurred in version 2.9.7, but after updating to XAMPP 3.2.4 with PHP 7.3.8 and libxml 2.9.9, the problem ceased to exist.

Upvotes: 1

Related Questions