Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow content to be empty when calling Crawler::html() method #616

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check warning on line 1 in src/DomCrawler/Crawler.php

View workflow job for this annotation

GitHub Actions / Coding Standards

Found violation(s) of type: nullable_type_declaration_for_default_null_value

/*
* This file is part of the Panther project.
Expand Down Expand Up @@ -229,13 +229,13 @@
return $this->webDriver->getPageSource();
}

return $this->attr('outerHTML');
return $this->attr('outerHTML', (string) $default);
} catch (\InvalidArgumentException $e) {
if (null === $default) {
throw $e;
}

return (string) $default;
return $default;
}
}

Expand Down
20 changes: 19 additions & 1 deletion tests/DomCrawler/CrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public function testChildren(callable $clientFactory): void
$names[$i] = $c->nodeName();
});

$this->assertSame(['h1', 'main', 'p', 'p', 'input', 'p'], $names);
$this->assertSame(['h1', 'main', 'p', 'p', 'input', 'p', 'div'], $names);
}

/**
Expand Down Expand Up @@ -385,6 +385,24 @@ public function testHtmlDefault(callable $clientFactory): void
$this->assertSame('default', $crawler->filter('header')->html('default'));
}

/**
* @dataProvider clientFactoryProvider
*/
public function testEmptyHtml(callable $clientFactory): void
{
$crawler = $this->request($clientFactory, '/basic.html');
dunglas marked this conversation as resolved.
Show resolved Hide resolved
$this->assertEmpty($crawler->filter('.empty')->html(''));
}

/**
* @dataProvider clientFactoryProvider
*/
public function testEmptyHtmlWithoutDefault(callable $clientFactory): void
{
$crawler = $this->request($clientFactory, '/basic.html');
$this->assertEmpty($crawler->filter('.empty')->html());
}

/**
* @dataProvider clientFactoryProvider
*/
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ <h1>Main</h1>
<p class="p-2">P2</p>
<input name="in" value="test">
<p class="price" data-old-price="42">36</p>
<div class="empty"></div>
</body>
</html>
Loading