Skip to content

Commit

Permalink
Added ability to get inner HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
Imangazaliev committed Mar 15, 2016
1 parent c18683b commit 8f3dba5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/DiDom/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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
*
Expand Down
8 changes: 8 additions & 0 deletions tests/DiDom/ElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ public function testHtml()
$this->assertEquals('<span>hello</span>', $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 = '<html><body><span></span></body></html>';
Expand Down

0 comments on commit 8f3dba5

Please sign in to comment.