Skip to content

Commit

Permalink
Add a getOuterHtml() method to DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
stevegrunwell committed Dec 12, 2023
1 parent 26a085d commit fdf46f3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
15 changes: 15 additions & 0 deletions src/DOM.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ public function getInnerHtml(Selector $selector)
});
}

/**
* Retrieve the inner contents of elements matching the given selector.
*
* @param Selector $selector The query selector.
*
* @return array<string> The inner contents of the matched selector. Each match is a separate
* value in the array.
*/
public function getOuterHtml(Selector $selector)
{
return $this->query($selector)->each(function ($element) {
return $element->outerHtml();
});
}

/**
* @param Selector $selector The query selector.
*
Expand Down
57 changes: 44 additions & 13 deletions tests/Unit/DOMTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public function it_should_be_able_to_count_selectors($markup, $expected)
public function getInnerHtml_should_retrieve_the_inner_HTML_for_each_matching_element()
{
$markup = <<<'HTML'
<ul>
<li>The <strong>strong</strong> element</li>
<li>The <em>em</em> element</li>
<li>The <kbd>kbd</kbd> element</li>
</ul>
HTML;
<ul>
<li>The <strong>strong</strong> element</li>
<li>The <em>em</em> element</li>
<li>The <kbd>kbd</kbd> element</li>
</ul>
HTML;
$dom = new DOM($markup);

$this->assertSame(
Expand All @@ -48,7 +48,7 @@ public function getInnerHtml_should_retrieve_the_inner_HTML_for_each_matching_el
'The <em>em</em> element',
'The <kbd>kbd</kbd> element',
],
$dom->getInnerHtml(new Selector('li')),
$dom->getInnerHtml(new Selector('li'))
);
}

Expand All @@ -63,6 +63,42 @@ public function getInnerHtml_should_return_an_empty_array_if_there_are_no_matche
$this->assertEmpty($dom->getInnerHtml(new Selector('h2')));
}

/**
* @test
* @testdox getOuterHtml() should retrieve the outer HTML for each matching element.
*/
public function getOuterHtml_should_retrieve_the_outer_HTML_for_each_matching_element()
{
$markup = <<<'HTML'
<ul>
<li>The <strong>strong</strong> element</li>
<li>The <em>em</em> element</li>
<li>The <kbd>kbd</kbd> element</li>
</ul>
HTML;
$dom = new DOM($markup);

$this->assertSame(
[
'<li>The <strong>strong</strong> element</li>',
'<li>The <em>em</em> element</li>',
'<li>The <kbd>kbd</kbd> element</li>',
],
$dom->getOuterHtml(new Selector('li'))
);
}

/**
* @test
* @testdox getOuterHtml() should return an empty array if there are no matches
*/
public function getOuterHtml_should_return_an_empty_array_if_there_are_no_matches()
{
$dom = new DOM('<h1>A title</h1>');

$this->assertEmpty($dom->getOuterHtml(new Selector('h2')));
}

/**
* @test
* @testdox query() should throw a SelectorException if the selector is invalid
Expand Down Expand Up @@ -100,12 +136,7 @@ public function provideMarkupWithInnerClass()
];

yield 'Two matches' => [
<<<'HTML'
<div class="outer">
<div class="inner">One</div>
<div class="inner">Two</div>
</div>
HTML,
'<div class="outer"><div class="inner">One</div><div class="inner">Two</div></div>',
2
];
}
Expand Down

0 comments on commit fdf46f3

Please sign in to comment.