From dc57a7a5a7670b3103f1882845bfdb07138422db Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Wed, 25 Feb 2015 22:18:34 +0100 Subject: [PATCH] [DomCrawler] Invalid uri created from forms if base tag present --- src/Symfony/Component/DomCrawler/Crawler.php | 2 +- src/Symfony/Component/DomCrawler/Form.php | 13 ++++++++++++- .../Component/DomCrawler/Tests/CrawlerTest.php | 3 +++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/DomCrawler/Crawler.php b/src/Symfony/Component/DomCrawler/Crawler.php index 823f135239..84ce8f3f8d 100644 --- a/src/Symfony/Component/DomCrawler/Crawler.php +++ b/src/Symfony/Component/DomCrawler/Crawler.php @@ -755,7 +755,7 @@ class Crawler extends \SplObjectStorage throw new \InvalidArgumentException('The current node list is empty.'); } - $form = new Form($this->getNode(0), $this->uri, $method); + $form = new Form($this->getNode(0), $this->uri, $method, $this->baseHref); if (null !== $values) { $form->setValues($values); diff --git a/src/Symfony/Component/DomCrawler/Form.php b/src/Symfony/Component/DomCrawler/Form.php index b0462f098f..a447c830d5 100644 --- a/src/Symfony/Component/DomCrawler/Form.php +++ b/src/Symfony/Component/DomCrawler/Form.php @@ -33,20 +33,27 @@ class Form extends Link implements \ArrayAccess */ private $fields; + /** + * @var string + */ + private $baseHref; + /** * Constructor. * * @param \DOMNode $node A \DOMNode instance * @param string $currentUri The URI of the page where the form is embedded * @param string $method The method to use for the link (if null, it defaults to the method defined by the form) + * @param string $baseHref The URI of the used for relative links, but not for empty action * * @throws \LogicException if the node is not a button inside a form tag * * @api */ - public function __construct(\DOMNode $node, $currentUri, $method = null) + public function __construct(\DOMNode $node, $currentUri, $method = null, $baseHref = null) { parent::__construct($node, $currentUri, $method); + $this->baseHref = $baseHref; $this->initialize(); } @@ -442,6 +449,10 @@ class Form extends Link implements \ArrayAccess $this->addField($node); } } + + if ($this->baseHref && '' !== $this->node->getAttribute('action')) { + $this->currentUri = $this->baseHref; + } } private function addField(\DOMNode $node) diff --git a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php index 9906fedc7e..ca7802d45d 100644 --- a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php +++ b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php @@ -853,9 +853,12 @@ HTML; public function getBaseTagWithFormData() { return array( + array('https://base.com/', 'link/', 'https://base.com/link/', 'https://base.com/link/', ' tag does work with a path and relative form action'), array('/basepath', '/registration', 'http://domain.com/registration', 'http://domain.com/registration', ' tag does work with a path and form action'), array('/basepath', '', 'http://domain.com/registration', 'http://domain.com/registration', ' tag does work with a path and empty form action'), + array('http://base.com/', '/registration', 'http://base.com/registration', 'http://domain.com/registration', ' tag does work with a URL and form action'), array('http://base.com', '', 'http://domain.com/path/form', 'http://domain.com/path/form', ' tag does work with a URL and an empty form action'), + array('http://base.com/path', '/registration', 'http://base.com/registration', 'http://domain.com/path/form', ' tag does work with a URL and form action'), ); }