@@ -327,11 +327,11 @@ public function eq($position)
327
327
{
328
328
foreach ($ this as $ i => $ node ) {
329
329
if ($ i == $ position ) {
330
- return new static ( $ node , $ this ->uri , $ this -> baseHref );
330
+ return $ this ->createSubCrawler ( $ node );
331
331
}
332
332
}
333
333
334
- return new static ( null , $ this ->uri , $ this -> baseHref );
334
+ return $ this ->createSubCrawler ( null );
335
335
}
336
336
337
337
/**
@@ -354,7 +354,7 @@ public function each(\Closure $closure)
354
354
{
355
355
$ data = array ();
356
356
foreach ($ this as $ i => $ node ) {
357
- $ data [] = $ closure (new static ( $ node , $ this ->uri , $ this -> baseHref ), $ i );
357
+ $ data [] = $ closure ($ this ->createSubCrawler ( $ node ), $ i );
358
358
}
359
359
360
360
return $ data ;
@@ -370,7 +370,7 @@ public function each(\Closure $closure)
370
370
*/
371
371
public function slice ($ offset = 0 , $ length = -1 )
372
372
{
373
- return new static (iterator_to_array (new \LimitIterator ($ this , $ offset , $ length )), $ this -> uri );
373
+ return $ this -> createSubCrawler (iterator_to_array (new \LimitIterator ($ this , $ offset , $ length )));
374
374
}
375
375
376
376
/**
@@ -386,12 +386,12 @@ public function reduce(\Closure $closure)
386
386
{
387
387
$ nodes = array ();
388
388
foreach ($ this as $ i => $ node ) {
389
- if (false !== $ closure (new static ( $ node , $ this ->uri , $ this -> baseHref ), $ i )) {
389
+ if (false !== $ closure ($ this ->createSubCrawler ( $ node ), $ i )) {
390
390
$ nodes [] = $ node ;
391
391
}
392
392
}
393
393
394
- return new static ( $ nodes , $ this ->uri , $ this -> baseHref );
394
+ return $ this ->createSubCrawler ( $ nodes );
395
395
}
396
396
397
397
/**
@@ -427,7 +427,7 @@ public function siblings()
427
427
throw new \InvalidArgumentException ('The current node list is empty. ' );
428
428
}
429
429
430
- return new static ($ this ->sibling ($ this ->getNode (0 )->parentNode ->firstChild ), $ this -> uri , $ this -> baseHref );
430
+ return $ this -> createSubCrawler ($ this ->sibling ($ this ->getNode (0 )->parentNode ->firstChild ));
431
431
}
432
432
433
433
/**
@@ -443,7 +443,7 @@ public function nextAll()
443
443
throw new \InvalidArgumentException ('The current node list is empty. ' );
444
444
}
445
445
446
- return new static ($ this ->sibling ($ this ->getNode (0 )), $ this -> uri , $ this -> baseHref );
446
+ return $ this -> createSubCrawler ($ this ->sibling ($ this ->getNode (0 )));
447
447
}
448
448
449
449
/**
@@ -459,7 +459,7 @@ public function previousAll()
459
459
throw new \InvalidArgumentException ('The current node list is empty. ' );
460
460
}
461
461
462
- return new static ($ this ->sibling ($ this ->getNode (0 ), 'previousSibling ' ), $ this -> uri , $ this -> baseHref );
462
+ return $ this -> createSubCrawler ($ this ->sibling ($ this ->getNode (0 ), 'previousSibling ' ));
463
463
}
464
464
465
465
/**
@@ -484,7 +484,7 @@ public function parents()
484
484
}
485
485
}
486
486
487
- return new static ( $ nodes , $ this ->uri , $ this -> baseHref );
487
+ return $ this ->createSubCrawler ( $ nodes );
488
488
}
489
489
490
490
/**
@@ -502,7 +502,7 @@ public function children()
502
502
503
503
$ node = $ this ->getNode (0 )->firstChild ;
504
504
505
- return new static ($ node ? $ this ->sibling ($ node ) : array (), $ this -> uri , $ this -> baseHref );
505
+ return $ this -> createSubCrawler ($ node ? $ this ->sibling ($ node ) : array ());
506
506
}
507
507
508
508
/**
@@ -631,7 +631,7 @@ public function filterXPath($xpath)
631
631
632
632
// If we dropped all expressions in the XPath while preparing it, there would be no match
633
633
if ('' === $ xpath ) {
634
- return new static ( null , $ this ->uri , $ this -> baseHref );
634
+ return $ this ->createSubCrawler ( null );
635
635
}
636
636
637
637
return $ this ->filterRelativeXPath ($ xpath );
@@ -829,7 +829,7 @@ private function filterRelativeXPath($xpath)
829
829
{
830
830
$ prefixes = $ this ->findNamespacePrefixes ($ xpath );
831
831
832
- $ crawler = new static ( null , $ this ->uri , $ this -> baseHref );
832
+ $ crawler = $ this ->createSubCrawler ( null );
833
833
834
834
foreach ($ this as $ node ) {
835
835
$ domxpath = $ this ->createDOMXPath ($ node ->ownerDocument , $ prefixes );
@@ -999,4 +999,18 @@ private function findNamespacePrefixes($xpath)
999
999
1000
1000
return array ();
1001
1001
}
1002
+
1003
+ /**
1004
+ * Creates a crawler for some subnodes.
1005
+ *
1006
+ * @param \DOMElement|\DOMElement[]|\DOMNodeList|null $nodes
1007
+ *
1008
+ * @return static
1009
+ */
1010
+ private function createSubCrawler ($ nodes )
1011
+ {
1012
+ $ crawler = new static ($ nodes , $ this ->uri , $ this ->baseHref );
1013
+
1014
+ return $ crawler ;
1015
+ }
1002
1016
}
0 commit comments