From 4c74dead48752058ba4e133ac1775a52d98a63c4 Mon Sep 17 00:00:00 2001 From: Simon Berger Date: Mon, 16 Nov 2020 20:09:52 +0100 Subject: [PATCH 1/2] Cache discovered namespaces in DomCrawler --- src/Symfony/Component/DomCrawler/Crawler.php | 25 ++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/DomCrawler/Crawler.php b/src/Symfony/Component/DomCrawler/Crawler.php index 3627aad5a10c1..b6ccda13726cb 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 \ArrayIterator 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 \ArrayIterator(); $this->add($node); } @@ -99,6 +105,7 @@ public function clear() { $this->nodes = []; $this->document = null; + $this->cachedNamespaces = new \ArrayIterator(); } /** @@ -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,17 @@ private function discoverNamespace(\DOMXPath $domxpath, string $prefix): ?string return $this->namespaces[$prefix]; } + if ($this->cachedNamespaces->offsetExists($prefix)) { + return $this->cachedNamespaces->offsetGet($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; + $namespace = ($node = $namespaces->item(0)) ? $node->nodeValue : null; + $this->cachedNamespaces->offsetSet($prefix, $namespace); + + return $namespace; } private function findNamespacePrefixes(string $xpath): array @@ -1217,6 +1233,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; From a8e85ecbbd52bc2aaa50d58daf02afb8b4764e9d Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 27 Nov 2020 08:00:10 +0100 Subject: [PATCH 2/2] Make some CS changes --- src/Symfony/Component/DomCrawler/Crawler.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/DomCrawler/Crawler.php b/src/Symfony/Component/DomCrawler/Crawler.php index b6ccda13726cb..2b4e1dfaaaad9 100644 --- a/src/Symfony/Component/DomCrawler/Crawler.php +++ b/src/Symfony/Component/DomCrawler/Crawler.php @@ -34,7 +34,7 @@ class Crawler implements \Countable, \IteratorAggregate private $namespaces = []; /** - * @var \ArrayIterator A map of cached namespaces + * @var \ArrayObject A map of cached namespaces */ private $cachedNamespaces; @@ -73,7 +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 \ArrayIterator(); + $this->cachedNamespaces = new \ArrayObject(); $this->add($node); } @@ -105,7 +105,7 @@ public function clear() { $this->nodes = []; $this->document = null; - $this->cachedNamespaces = new \ArrayIterator(); + $this->cachedNamespaces = new \ArrayObject(); } /** @@ -1198,17 +1198,14 @@ private function discoverNamespace(\DOMXPath $domxpath, string $prefix): ?string return $this->namespaces[$prefix]; } - if ($this->cachedNamespaces->offsetExists($prefix)) { - return $this->cachedNamespaces->offsetGet($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)); - $namespace = ($node = $namespaces->item(0)) ? $node->nodeValue : null; - $this->cachedNamespaces->offsetSet($prefix, $namespace); - - return $namespace; + return $this->cachedNamespaces[$prefix] = ($node = $namespaces->item(0)) ? $node->nodeValue : null; } private function findNamespacePrefixes(string $xpath): array 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