Skip to content

Commit 711ac32

Browse files
robbertklstof
authored andcommitted
[DomCrawler] Fixed filterXPath() chaining
1 parent 8f706c9 commit 711ac32

File tree

4 files changed

+19
-32
lines changed

4 files changed

+19
-32
lines changed

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ public function parents()
445445
$nodes = array();
446446

447447
while ($node = $node->parentNode) {
448-
if (1 === $node->nodeType && '_root' !== $node->nodeName) {
448+
if (1 === $node->nodeType) {
449449
$nodes[] = $node;
450450
}
451451
}
@@ -588,15 +588,14 @@ public function extract($attributes)
588588
*/
589589
public function filterXPath($xpath)
590590
{
591-
$document = new \DOMDocument('1.0', 'UTF-8');
592-
$root = $document->appendChild($document->createElement('_root'));
591+
$crawler = new static(null, $this->uri);
592+
593593
foreach ($this as $node) {
594-
$root->appendChild($document->importNode($node, true));
594+
$domxpath = new \DOMXPath($node->ownerDocument);
595+
$crawler->add($domxpath->query($xpath, $node));
595596
}
596597

597-
$domxpath = new \DOMXPath($document);
598-
599-
return new static($domxpath->query($xpath), $this->uri);
598+
return $crawler;
600599
}
601600

602601
/**

src/Symfony/Component/DomCrawler/Field/FormField.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,7 @@ public function __construct(\DOMNode $node)
5252
{
5353
$this->node = $node;
5454
$this->name = $node->getAttribute('name');
55-
56-
$this->document = new \DOMDocument('1.0', 'UTF-8');
57-
$this->node = $this->document->importNode($this->node, true);
58-
59-
$root = $this->document->appendChild($this->document->createElement('_root'));
60-
$root->appendChild($this->node);
61-
$this->xpath = new \DOMXPath($this->document);
55+
$this->xpath = new \DOMXPath($node->ownerDocument);
6256

6357
$this->initialize();
6458
}

src/Symfony/Component/DomCrawler/Form.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,7 @@ private function initialize()
388388
{
389389
$this->fields = new FormFieldRegistry();
390390

391-
$document = new \DOMDocument('1.0', 'UTF-8');
392-
$xpath = new \DOMXPath($document);
393-
$root = $document->appendChild($document->createElement('_root'));
391+
$xpath = new \DOMXPath($this->node->ownerDocument);
394392

395393
// add submitted button if it has a valid name
396394
if ('form' !== $this->button->nodeName && $this->button->hasAttribute('name') && $this->button->getAttribute('name')) {
@@ -400,38 +398,32 @@ private function initialize()
400398

401399
// temporarily change the name of the input node for the x coordinate
402400
$this->button->setAttribute('name', $name.'.x');
403-
$this->set(new Field\InputFormField($document->importNode($this->button, true)));
401+
$this->set(new Field\InputFormField($this->button));
404402

405403
// temporarily change the name of the input node for the y coordinate
406404
$this->button->setAttribute('name', $name.'.y');
407-
$this->set(new Field\InputFormField($document->importNode($this->button, true)));
405+
$this->set(new Field\InputFormField($this->button));
408406

409407
// restore the original name of the input node
410408
$this->button->setAttribute('name', $name);
411409
} else {
412-
$this->set(new Field\InputFormField($document->importNode($this->button, true)));
410+
$this->set(new Field\InputFormField($this->button));
413411
}
414412
}
415413

416414
// find form elements corresponding to the current form
417415
if ($this->node->hasAttribute('id')) {
418-
// traverse through the whole document
419-
$node = $document->importNode($this->node->ownerDocument->documentElement, true);
420-
$root->appendChild($node);
421-
422416
// corresponding elements are either descendants or have a matching HTML5 form attribute
423417
$formId = Crawler::xpathLiteral($this->node->getAttribute('id'));
424-
$fieldNodes = $xpath->query(sprintf('descendant::input[@form=%s] | descendant::button[@form=%s] | descendant::textarea[@form=%s] | descendant::select[@form=%s] | //form[@id=%s]//input[not(@form)] | //form[@id=%s]//button[not(@form)] | //form[@id=%s]//textarea[not(@form)] | //form[@id=%s]//select[not(@form)]', $formId, $formId, $formId, $formId, $formId, $formId, $formId, $formId), $root);
418+
419+
$fieldNodes = $xpath->query(sprintf('descendant::input[@form=%s] | descendant::button[@form=%s] | descendant::textarea[@form=%s] | descendant::select[@form=%s] | //form[@id=%s]//input[not(@form)] | //form[@id=%s]//button[not(@form)] | //form[@id=%s]//textarea[not(@form)] | //form[@id=%s]//select[not(@form)]', $formId, $formId, $formId, $formId, $formId, $formId, $formId, $formId));
425420
foreach ($fieldNodes as $node) {
426421
$this->addField($node);
427422
}
428423
} else {
429-
// parent form has no id, add descendant elements only
430-
$node = $document->importNode($this->node, true);
431-
$root->appendChild($node);
432-
433-
// descendant elements with form attribute are not part of this form
434-
$fieldNodes = $xpath->query('descendant::input[not(@form)] | descendant::button[not(@form)] | descendant::textarea[not(@form)] | descendant::select[not(@form)]', $root);
424+
// do the xpath query with $this->node as the context node, to only find descendant elements
425+
// however, descendant elements with form attribute are not part of this form
426+
$fieldNodes = $xpath->query('descendant::input[not(@form)] | descendant::button[not(@form)] | descendant::textarea[not(@form)] | descendant::select[not(@form)]', $this->node);
435427
foreach ($fieldNodes as $node) {
436428
$this->addField($node);
437429
}

src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,10 @@ public function testFilterXPath()
399399
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->filterXPath() returns a new instance of a crawler');
400400

401401
$crawler = $this->createTestCrawler()->filterXPath('//ul');
402-
403402
$this->assertCount(6, $crawler->filterXPath('//li'), '->filterXPath() filters the node list with the XPath expression');
403+
404+
$crawler = $this->createTestCrawler();
405+
$this->assertCount(3, $crawler->filterXPath('//body')->filterXPath('//button')->parents(), '->filterXpath() preserves parents when chained');
404406
}
405407

406408
/**

0 commit comments

Comments
 (0)