@@ -33,6 +33,11 @@ class Crawler implements \Countable, \IteratorAggregate
33
33
*/
34
34
private $ namespaces = [];
35
35
36
+ /**
37
+ * @var array A map of cached namespaces
38
+ */
39
+ private $ cachedNamespaces = [];
40
+
36
41
/**
37
42
* @var string The base href value
38
43
*/
@@ -99,6 +104,7 @@ public function clear()
99
104
{
100
105
$ this ->nodes = [];
101
106
$ this ->document = null ;
107
+ $ this ->cachedNamespaces = $ this ->namespaces ;
102
108
}
103
109
104
110
/**
@@ -968,6 +974,7 @@ public function setDefaultNamespacePrefix($prefix)
968
974
public function registerNamespace ($ prefix , $ namespace )
969
975
{
970
976
$ this ->namespaces [$ prefix ] = $ namespace ;
977
+ $ this ->cachedNamespaces [$ prefix ] = $ namespace ;
971
978
}
972
979
973
980
/**
@@ -1034,6 +1041,8 @@ private function filterRelativeXPath(string $xpath)
1034
1041
$ crawler ->add ($ domxpath ->query ($ xpath , $ node ));
1035
1042
}
1036
1043
1044
+ $ crawler ->cachedNamespaces = $ this ->cachedNamespaces ;
1045
+
1037
1046
return $ crawler ;
1038
1047
}
1039
1048
@@ -1235,6 +1244,7 @@ private function createDOMXPath(\DOMDocument $document, array $prefixes = []): \
1235
1244
$ namespace = $ this ->discoverNamespace ($ domxpath , $ prefix );
1236
1245
if (null !== $ namespace ) {
1237
1246
$ domxpath ->registerNamespace ($ prefix , $ namespace );
1247
+ $ this ->cachedNamespaces [$ prefix ] = $ namespace ;
1238
1248
}
1239
1249
}
1240
1250
@@ -1246,8 +1256,8 @@ private function createDOMXPath(\DOMDocument $document, array $prefixes = []): \
1246
1256
*/
1247
1257
private function discoverNamespace (\DOMXPath $ domxpath , string $ prefix ): ?string
1248
1258
{
1249
- if (isset ($ this ->namespaces [$ prefix ])) {
1250
- return $ this ->namespaces [$ prefix ];
1259
+ if (isset ($ this ->cachedNamespaces [$ prefix ])) {
1260
+ return $ this ->cachedNamespaces [$ prefix ];
1251
1261
}
1252
1262
1253
1263
// ask for one namespace, otherwise we'd get a collection with an item for each node
@@ -1278,6 +1288,7 @@ private function createSubCrawler($nodes)
1278
1288
$ crawler ->isHtml = $ this ->isHtml ;
1279
1289
$ crawler ->document = $ this ->document ;
1280
1290
$ crawler ->namespaces = $ this ->namespaces ;
1291
+ $ crawler ->cachedNamespaces = $ this ->cachedNamespaces ;
1281
1292
$ crawler ->html5Parser = $ this ->html5Parser ;
1282
1293
1283
1294
return $ crawler ;
0 commit comments