Skip to content

Commit

Permalink
fix: Make sure to pass non-empty strings to SpiderBits\Dom
Browse files Browse the repository at this point in the history
\DOMDocument::loadHTML fails if an empty string is passed.
It's pretty strange as the documentation says it only generates a
warning.

Nevertheless, it's way better to make sure to always pass non-empty
strings to it.
  • Loading branch information
marienfressinaud committed Aug 3, 2023
1 parent 487adc4 commit 45d60fd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/SpiderBits/src/Dom.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Dom

/**
* Return a new Dom object from text.
*
* @param non-empty-string $html_as_string
*/
public static function fromText(string $html_as_string, int $libxml_options = 0): self
{
Expand Down
4 changes: 4 additions & 0 deletions lib/SpiderBits/src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public function encoding(): string
return $charset;
}

if (!$data) {
return 'utf-8';
}

$result = preg_match(
'/^\s*<\?xml\s+(?:(?:.*?)\s)?encoding=[\'"](?P<encoding>.+?)[\'"]/i',
$data,
Expand Down
9 changes: 9 additions & 0 deletions src/services/FeedFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ public function fetch(models\Collection $collection): void

$data = $response->utf8Data();

if (!$data) {
return;
}

$dom = \SpiderBits\Dom::fromText($data);
$url_illustration = \SpiderBits\DomExtractor::illustration($dom);
$url_illustration = \SpiderBits\Url::sanitize($url_illustration);
Expand Down Expand Up @@ -373,6 +377,11 @@ public function fetchUrl(string $url): array
return $info; // @codeCoverageIgnore
}

if (!$data) {
$info['error'] = 'Empty content';
return $info;
}

try {
$feed = \SpiderBits\feeds\Feed::fromText($data);
$info['feed'] = $feed;
Expand Down
4 changes: 4 additions & 0 deletions src/services/LinkFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ public function fetchUrl(string $url): array
return $info; // @codeCoverageIgnore
}

if (!$data) {
return $info;
}

$dom = \SpiderBits\Dom::fromText($data);

// Parse the title from the DOM
Expand Down

0 comments on commit 45d60fd

Please sign in to comment.