Skip to content

Commit c6f6712

Browse files
committed
Fix issue paquettg#166
1 parent 45cdfb4 commit c6f6712

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/PHPHtmlParser/Dom.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public function loadFromUrl(string $url, array $options = [], CurlInterface $cur
192192
* @return Dom
193193
* @chainable
194194
*/
195-
public function loadStr(string $str, array $option): Dom
195+
public function loadStr(string $str, array $option = []): Dom
196196
{
197197
$this->options = new Options;
198198
$this->options->setOptions($this->globalOptions)

src/PHPHtmlParser/Dom/InnerNode.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,15 @@ public function replaceChild(int $childId, AbstractNode $newChild): void
357357
*
358358
* @return AbstractNode
359359
* @uses $this->getChild()
360+
* @throws ChildNotFoundException
360361
*/
361362
public function firstChild(): AbstractNode
362363
{
364+
if (count($this->children) == 0) {
365+
// no children
366+
throw new ChildNotFoundException("No children found in node.");
367+
}
368+
363369
reset($this->children);
364370
$key = key($this->children);
365371

@@ -370,9 +376,16 @@ public function firstChild(): AbstractNode
370376
* Attempts to get the last child.
371377
*
372378
* @return AbstractNode
379+
* @uses $this->getChild()
380+
* @throws ChildNotFoundException
373381
*/
374382
public function lastChild(): AbstractNode
375383
{
384+
if (count($this->children) == 0) {
385+
// no children
386+
throw new ChildNotFoundException("No children found in node.");
387+
}
388+
376389
end($this->children);
377390
$key = key($this->children);
378391

tests/DomTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,14 @@ public function testGetComplexAttribute()
397397
$href = $dom->find('a', 0)->href;
398398
$this->assertEquals('?search=Fort+William&session_type=face&distance=100&uqs=119846&page=4', $href);
399399
}
400+
401+
public function testGetChildrenNoChildren()
402+
{
403+
$dom = new Dom();
404+
$dom->loadStr('<div>Test <img src="test.jpg"></div>');
405+
406+
$imgNode = $dom->root->find('img');
407+
$children = $imgNode->getChildren();
408+
$this->assertTrue(count($children) === 0);
409+
}
400410
}

0 commit comments

Comments
 (0)