Browse Source

Improve handling of empty dom

pull/10/head
Richard Dern 3 months ago
parent
commit
3e76cc800c
  1. 28
      package/Analyzers/HtmlAnalyzer.php

28
package/Analyzers/HtmlAnalyzer.php

@ -110,6 +110,10 @@ class HtmlAnalyzer extends BaseAnalyzer implements Analyzer
{
$filePath = Arr::get($this->baseAttributes, 'sourcePath');
if (empty($this->fileSystem)) {
throw new Exception("No file system specified");
}
if (empty($filePath)) {
throw new Exception("There is no body to analyze");
}
@ -132,12 +136,16 @@ class HtmlAnalyzer extends BaseAnalyzer implements Analyzer
*/
protected function createDomDocument(): self
{
if (empty($this->body)) {
return $this;
}
$this->domDocument = new DomDocument('1.0', 'UTF-8');
$this->body = mb_convert_encoding($this->body, 'HTML-ENTITIES', 'UTF-8');
libxml_use_internal_errors(true);
$this->domDocument = new DomDocument('1.0', 'UTF-8');
$this->domDocument->loadHtml($this->body);
libxml_clear_errors();
@ -154,6 +162,10 @@ class HtmlAnalyzer extends BaseAnalyzer implements Analyzer
*/
protected function findNodes(string $xpathQuery): DOMNodeList|false
{
if (empty($this->domDocument)) {
return false;
}
$xpath = new DOMXPath($this->domDocument);
return $xpath->query($xpathQuery);
@ -170,7 +182,7 @@ class HtmlAnalyzer extends BaseAnalyzer implements Analyzer
{
$nodes = $this->findNodes($xpathQuery);
if ($nodes->length === 0) {
if (!$nodes || $nodes->length === 0) {
return null;
}
@ -188,11 +200,13 @@ class HtmlAnalyzer extends BaseAnalyzer implements Analyzer
{
$data = [];
foreach ($node->attributes as $attribute) {
$key = Str::slug($attribute->localName);
$value = (new StringHelper($attribute->nodeValue))->cleanup();
if ($node->attributes) {
foreach ($node->attributes as $attribute) {
$key = Str::slug($attribute->localName);
$value = (new StringHelper($attribute->nodeValue))->cleanup();
$data[$key] = $value;
$data[$key] = $value;
}
}
return $data;

Loading…
Cancel
Save