Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d256b2f

Browse filesBrowse files
committed
Cache discovered namespaces in Crawler
1 parent 091265b commit d256b2f
Copy full SHA for d256b2f

File tree

1 file changed

+13
-2
lines changed
Filter options

1 file changed

+13
-2
lines changed

‎src/Symfony/Component/DomCrawler/Crawler.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DomCrawler/Crawler.php
+13-2Lines changed: 13 additions & 2 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 array A map of cached namespaces
38+
*/
39+
private $cachedNamespaces = [];
40+
3641
/**
3742
* @var string The base href value
3843
*/
@@ -99,6 +104,7 @@ public function clear()
99104
{
100105
$this->nodes = [];
101106
$this->document = null;
107+
$this->cachedNamespaces = $this->namespaces;
102108
}
103109

104110
/**
@@ -968,6 +974,7 @@ public function setDefaultNamespacePrefix($prefix)
968974
public function registerNamespace($prefix, $namespace)
969975
{
970976
$this->namespaces[$prefix] = $namespace;
977+
$this->cachedNamespaces[$prefix] = $namespace;
971978
}
972979

973980
/**
@@ -1034,6 +1041,8 @@ private function filterRelativeXPath(string $xpath)
10341041
$crawler->add($domxpath->query($xpath, $node));
10351042
}
10361043

1044+
$crawler->cachedNamespaces = $this->cachedNamespaces;
1045+
10371046
return $crawler;
10381047
}
10391048

@@ -1235,6 +1244,7 @@ private function createDOMXPath(\DOMDocument $document, array $prefixes = []): \
12351244
$namespace = $this->discoverNamespace($domxpath, $prefix);
12361245
if (null !== $namespace) {
12371246
$domxpath->registerNamespace($prefix, $namespace);
1247+
$this->cachedNamespaces[$prefix] = $namespace;
12381248
}
12391249
}
12401250

@@ -1246,8 +1256,8 @@ private function createDOMXPath(\DOMDocument $document, array $prefixes = []): \
12461256
*/
12471257
private function discoverNamespace(\DOMXPath $domxpath, string $prefix): ?string
12481258
{
1249-
if (isset($this->namespaces[$prefix])) {
1250-
return $this->namespaces[$prefix];
1259+
if (isset($this->cachedNamespaces[$prefix])) {
1260+
return $this->cachedNamespaces[$prefix];
12511261
}
12521262

12531263
// ask for one namespace, otherwise we'd get a collection with an item for each node
@@ -1278,6 +1288,7 @@ private function createSubCrawler($nodes)
12781288
$crawler->isHtml = $this->isHtml;
12791289
$crawler->document = $this->document;
12801290
$crawler->namespaces = $this->namespaces;
1291+
$crawler->cachedNamespaces = $this->cachedNamespaces;
12811292
$crawler->html5Parser = $this->html5Parser;
12821293

12831294
return $crawler;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.