diff --git a/src/DiDom/Element.php b/src/DiDom/Element.php index 4052288..44991a7 100644 --- a/src/DiDom/Element.php +++ b/src/DiDom/Element.php @@ -195,7 +195,7 @@ public function attr($name, $value = null) } /** - * Dumps the internal document into a string using HTML formatting. + * Dumps the node into a string using HTML formatting. * * @param int $options Additional options * @@ -207,7 +207,29 @@ public function html($options = 0) } /** - * Dumps the internal document into a string using XML formatting. + * Dumps the node descendants into a string using HTML formatting. + * + * @param int $options Additional options + * + * @return string + */ + public function innerHtml($options = 0) + { + $childrenHtml = []; + $children = $this->node->childNodes; + + foreach ($children as $child) + { + $childrenHtml[] = trim($child->ownerDocument->saveXml($child, $options)); + } + + $html = implode(PHP_EOL, $childrenHtml); + + return str_replace(' ', '', $html); + } + + /** + * Dumps the node into a string using XML formatting. * * @param int $options Additional options * diff --git a/tests/DiDom/ElementTest.php b/tests/DiDom/ElementTest.php index 30cbb65..5e8fd4f 100644 --- a/tests/DiDom/ElementTest.php +++ b/tests/DiDom/ElementTest.php @@ -232,6 +232,14 @@ public function testHtml() $this->assertEquals('hello', $element->html()); } + public function testInnerHtml() + { + $html = $this->loadFixture('posts.html'); + $document = new Document($html, false); + + $this->assertTrue(is_string($document->find('body')[0]->innerHtml())); + } + public function testHtmlWithOptions() { $html = '';