diff --git a/packages/testing/src/Support/HtmlTesting/TestableHtmlDocument.php b/packages/testing/src/Support/HtmlTesting/TestableHtmlDocument.php
index 3b90ebb9946..463357807ba 100644
--- a/packages/testing/src/Support/HtmlTesting/TestableHtmlDocument.php
+++ b/packages/testing/src/Support/HtmlTesting/TestableHtmlDocument.php
@@ -6,6 +6,7 @@
use DOMElement;
use DOMDocument;
+use DOMXPath;
use InvalidArgumentException;
use Illuminate\Support\Collection;
use Illuminate\Testing\Assert as PHPUnit;
@@ -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;
}
/**