Skip to content

Commit fdf46f3

Browse files
committed
Add a getOuterHtml() method to DOM
1 parent 26a085d commit fdf46f3

File tree

2 files changed

+59
-13
lines changed

2 files changed

+59
-13
lines changed

src/DOM.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ public function getInnerHtml(Selector $selector)
5252
});
5353
}
5454

55+
/**
56+
* Retrieve the inner contents of elements matching the given selector.
57+
*
58+
* @param Selector $selector The query selector.
59+
*
60+
* @return array<string> The inner contents of the matched selector. Each match is a separate
61+
* value in the array.
62+
*/
63+
public function getOuterHtml(Selector $selector)
64+
{
65+
return $this->query($selector)->each(function ($element) {
66+
return $element->outerHtml();
67+
});
68+
}
69+
5570
/**
5671
* @param Selector $selector The query selector.
5772
*

tests/Unit/DOMTest.php

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public function it_should_be_able_to_count_selectors($markup, $expected)
3434
public function getInnerHtml_should_retrieve_the_inner_HTML_for_each_matching_element()
3535
{
3636
$markup = <<<'HTML'
37-
<ul>
38-
<li>The <strong>strong</strong> element</li>
39-
<li>The <em>em</em> element</li>
40-
<li>The <kbd>kbd</kbd> element</li>
41-
</ul>
42-
HTML;
37+
<ul>
38+
<li>The <strong>strong</strong> element</li>
39+
<li>The <em>em</em> element</li>
40+
<li>The <kbd>kbd</kbd> element</li>
41+
</ul>
42+
HTML;
4343
$dom = new DOM($markup);
4444

4545
$this->assertSame(
@@ -48,7 +48,7 @@ public function getInnerHtml_should_retrieve_the_inner_HTML_for_each_matching_el
4848
'The <em>em</em> element',
4949
'The <kbd>kbd</kbd> element',
5050
],
51-
$dom->getInnerHtml(new Selector('li')),
51+
$dom->getInnerHtml(new Selector('li'))
5252
);
5353
}
5454

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

66+
/**
67+
* @test
68+
* @testdox getOuterHtml() should retrieve the outer HTML for each matching element.
69+
*/
70+
public function getOuterHtml_should_retrieve_the_outer_HTML_for_each_matching_element()
71+
{
72+
$markup = <<<'HTML'
73+
<ul>
74+
<li>The <strong>strong</strong> element</li>
75+
<li>The <em>em</em> element</li>
76+
<li>The <kbd>kbd</kbd> element</li>
77+
</ul>
78+
HTML;
79+
$dom = new DOM($markup);
80+
81+
$this->assertSame(
82+
[
83+
'<li>The <strong>strong</strong> element</li>',
84+
'<li>The <em>em</em> element</li>',
85+
'<li>The <kbd>kbd</kbd> element</li>',
86+
],
87+
$dom->getOuterHtml(new Selector('li'))
88+
);
89+
}
90+
91+
/**
92+
* @test
93+
* @testdox getOuterHtml() should return an empty array if there are no matches
94+
*/
95+
public function getOuterHtml_should_return_an_empty_array_if_there_are_no_matches()
96+
{
97+
$dom = new DOM('<h1>A title</h1>');
98+
99+
$this->assertEmpty($dom->getOuterHtml(new Selector('h2')));
100+
}
101+
66102
/**
67103
* @test
68104
* @testdox query() should throw a SelectorException if the selector is invalid
@@ -100,12 +136,7 @@ public function provideMarkupWithInnerClass()
100136
];
101137

102138
yield 'Two matches' => [
103-
<<<'HTML'
104-
<div class="outer">
105-
<div class="inner">One</div>
106-
<div class="inner">Two</div>
107-
</div>
108-
HTML,
139+
'<div class="outer"><div class="inner">One</div><div class="inner">Two</div></div>',
109140
2
110141
];
111142
}

0 commit comments

Comments
 (0)