Skip to content

Commit

Permalink
Merge pull request #2057 from hydephp/more-reliable-internal-testing-…
Browse files Browse the repository at this point in the history
…helper

Internal: More reliable internal testing helper implementation
  • Loading branch information
caendesilva authored Dec 6, 2024
2 parents 191ebc4 + 673573c commit 88ef637
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions packages/testing/src/Support/HtmlTesting/TestableHtmlDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use DOMElement;
use DOMDocument;
use DOMXPath;
use InvalidArgumentException;
use Illuminate\Support\Collection;
use Illuminate\Testing\Assert as PHPUnit;
Expand Down Expand Up @@ -80,21 +81,17 @@ public function getElementUsingQuery(string $selector): TestableHtmlElement
*/
public function getElementsByClass(string $class): Collection
{
$matchingNodes = collect();
$xpath = new DOMXPath($this->document);
$nodeList = $xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $class ')]");

$traverse = function (TestableHtmlElement $node) use (&$traverse, $class, &$matchingNodes): void {
if (in_array($class, $node->classes, true)) {
$matchingNodes->push($node);
$collection = collect();
foreach ($nodeList as $node) {
if ($node instanceof DOMElement) {
$collection->push($this->parseNodeRecursive($node));
}
}

foreach ($node->nodes as $childNode) {
$traverse($childNode);
}
};

$traverse($this->getRootElement());

return $matchingNodes;
return $collection;
}

/**
Expand Down

0 comments on commit 88ef637

Please sign in to comment.