Skip to content

[DomCrawler] Cache discovered namespaces #39097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -99,6 +105,7 @@ public function clear()
{
$this->nodes = [];
$this->document = null;
$this->cachedNamespaces = new \ArrayObject();
}

/**
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down
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