diff --git a/src/Symfony/Component/DomCrawler/Crawler.php b/src/Symfony/Component/DomCrawler/Crawler.php index 3627aad5a10c..2b4e1dfaaaad 100644 --- a/src/Symfony/Component/DomCrawler/Crawler.php +++ b/src/Symfony/Component/DomCrawler/Crawler.php @@ -33,6 +33,11 @@ class Crawler implements \Countable, \IteratorAggregate */ private $namespaces = []; + /** + * @var \ArrayObject A map of cached namespaces + */ + private $cachedNamespaces; + /** * @var string The base href value */ @@ -68,6 +73,7 @@ public function __construct($node = null, string $uri = null, string $baseHref = $this->uri = $uri; $this->baseHref = $baseHref ?: $uri; $this->html5Parser = class_exists(HTML5::class) ? new HTML5(['disable_html_ns' => true]) : null; + $this->cachedNamespaces = new \ArrayObject(); $this->add($node); } @@ -99,6 +105,7 @@ public function clear() { $this->nodes = []; $this->document = null; + $this->cachedNamespaces = new \ArrayObject(); } /** @@ -967,12 +974,14 @@ public static function xpathLiteral(string $s) */ private function filterRelativeXPath(string $xpath): object { - $prefixes = $this->findNamespacePrefixes($xpath); - $crawler = $this->createSubCrawler(null); + if (null === $this->document) { + return $crawler; + } + + $domxpath = $this->createDOMXPath($this->document, $this->findNamespacePrefixes($xpath)); foreach ($this->nodes as $node) { - $domxpath = $this->createDOMXPath($node->ownerDocument, $prefixes); $crawler->add($domxpath->query($xpath, $node)); } @@ -1189,10 +1198,14 @@ private function discoverNamespace(\DOMXPath $domxpath, string $prefix): ?string return $this->namespaces[$prefix]; } + if (isset($this->cachedNamespaces[$prefix])) { + return $this->cachedNamespaces[$prefix]; + } + // ask for one namespace, otherwise we'd get a collection with an item for each node $namespaces = $domxpath->query(sprintf('(//namespace::*[name()="%s"])[last()]', $this->defaultNamespacePrefix === $prefix ? '' : $prefix)); - return ($node = $namespaces->item(0)) ? $node->nodeValue : null; + return $this->cachedNamespaces[$prefix] = ($node = $namespaces->item(0)) ? $node->nodeValue : null; } private function findNamespacePrefixes(string $xpath): array @@ -1217,6 +1230,7 @@ private function createSubCrawler($nodes): object $crawler->isHtml = $this->isHtml; $crawler->document = $this->document; $crawler->namespaces = $this->namespaces; + $crawler->cachedNamespaces = $this->cachedNamespaces; $crawler->html5Parser = $this->html5Parser; return $crawler; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy