scotteh/php-dom-wrapper

Why do getHtml() and getOuterHtml() only work on the first child?

Closed this issue · 1 comments

Today, I tried returning a NodeList of DomElements from a function, to allow callers to either include it in a DOM tree, or call ->getOuterHtml() on it to process the elements as an HTML string.

However, I found that this actually only returns the HTML of the first element of the nodelist:

public function getOuterHtml(): string {
return $this->document()->saveHTML(
$this->collection()->first()
);
}

The same thing happens for getHTML():

public function getHtml(): string {
return $this->collection()->first()->contents()->reduce(function($carry, $node) {
return $carry . $this->document()->saveHTML($node);
}, '');
}

Why is this? Wouldn't it make more sense to add an additional reduce() instead of the first() to iterate all elements of the collection?

Thanks!