Skip to content

Commit

Permalink
Merge branch 'master' into 2.x-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Dec 6, 2024
2 parents 0f5a612 + 88ef637 commit 7c2beb3
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 7c2beb3

Please sign in to comment.