From 8f3dba51dc1d07f83cfd7121e6c6a79593675caf Mon Sep 17 00:00:00 2001 From: Imangazaliev Date: Tue, 15 Mar 2016 19:49:09 +0300 Subject: [PATCH] Added ability to get inner HTML --- src/DiDom/Element.php | 26 ++++++++++++++++++++++++-- tests/DiDom/ElementTest.php | 8 ++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) 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 = '';