Skip to content

Commit 4c74dea

Browse files
simonbergerfabpot
authored andcommitted
Cache discovered namespaces in DomCrawler
1 parent 3ac26fc commit 4c74dea

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ class Crawler implements \Countable, \IteratorAggregate
3333
*/
3434
private $namespaces = [];
3535

36+
/**
37+
* @var \ArrayIterator A map of cached namespaces
38+
*/
39+
private $cachedNamespaces;
40+
3641
/**
3742
* @var string The base href value
3843
*/
@@ -68,6 +73,7 @@ public function __construct($node = null, string $uri = null, string $baseHref =
6873
$this->uri = $uri;
6974
$this->baseHref = $baseHref ?: $uri;
7075
$this->html5Parser = class_exists(HTML5::class) ? new HTML5(['disable_html_ns' => true]) : null;
76+
$this->cachedNamespaces = new \ArrayIterator();
7177

7278
$this->add($node);
7379
}
@@ -99,6 +105,7 @@ public function clear()
99105
{
100106
$this->nodes = [];
101107
$this->document = null;
108+
$this->cachedNamespaces = new \ArrayIterator();
102109
}
103110

104111
/**
@@ -967,12 +974,14 @@ public static function xpathLiteral(string $s)
967974
*/
968975
private function filterRelativeXPath(string $xpath): object
969976
{
970-
$prefixes = $this->findNamespacePrefixes($xpath);
971-
972977
$crawler = $this->createSubCrawler(null);
978+
if (null === $this->document) {
979+
return $crawler;
980+
}
981+
982+
$domxpath = $this->createDOMXPath($this->document, $this->findNamespacePrefixes($xpath));
973983

974984
foreach ($this->nodes as $node) {
975-
$domxpath = $this->createDOMXPath($node->ownerDocument, $prefixes);
976985
$crawler->add($domxpath->query($xpath, $node));
977986
}
978987

@@ -1189,10 +1198,17 @@ private function discoverNamespace(\DOMXPath $domxpath, string $prefix): ?string
11891198
return $this->namespaces[$prefix];
11901199
}
11911200

1201+
if ($this->cachedNamespaces->offsetExists($prefix)) {
1202+
return $this->cachedNamespaces->offsetGet($prefix);
1203+
}
1204+
11921205
// ask for one namespace, otherwise we'd get a collection with an item for each node
11931206
$namespaces = $domxpath->query(sprintf('(//namespace::*[name()="%s"])[last()]', $this->defaultNamespacePrefix === $prefix ? '' : $prefix));
11941207

1195-
return ($node = $namespaces->item(0)) ? $node->nodeValue : null;
1208+
$namespace = ($node = $namespaces->item(0)) ? $node->nodeValue : null;
1209+
$this->cachedNamespaces->offsetSet($prefix, $namespace);
1210+
1211+
return $namespace;
11961212
}
11971213

11981214
private function findNamespacePrefixes(string $xpath): array
@@ -1217,6 +1233,7 @@ private function createSubCrawler($nodes): object
12171233
$crawler->isHtml = $this->isHtml;
12181234
$crawler->document = $this->document;
12191235
$crawler->namespaces = $this->namespaces;
1236+
$crawler->cachedNamespaces = $this->cachedNamespaces;
12201237
$crawler->html5Parser = $this->html5Parser;
12211238

12221239
return $crawler;

0 commit comments

Comments
 (0)
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