Skip to content

Commit

Permalink
Create base AST parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Mar 16, 2024
1 parent 42ee04e commit fac62ea
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/testing/src/Support/HtmlTesting/TestableHtmlDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Hyde\Testing\Support\HtmlTesting;

use DOMXPath;
use DOMDocument;
use Illuminate\Support\Collection;

/**
Expand All @@ -19,5 +21,17 @@ class TestableHtmlDocument
public function __construct(string $html)
{
$this->html = $html;

$this->nodes = $this->parseNodes($html);
}

protected function parseNodes(string $html): Collection
{
$dom = new DOMDocument();
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

$xpath = new DOMXPath($dom);

return collect($xpath->query('//*'))->map(fn ($node) => new TestableHtmlElement($node->ownerDocument->saveHTML($node)));
}
}

0 comments on commit fac62ea

Please sign in to comment.