[DomCrawler] Invalid uri created from forms if base tag present

This commit is contained in:
Daniel Tschinder 2015-02-25 22:18:34 +01:00 committed by Fabien Potencier
parent fedbf711a1
commit dc57a7a5a7
3 changed files with 16 additions and 2 deletions

View File

@ -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);

View File

@ -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 <base> 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)

View File

@ -853,9 +853,12 @@ HTML;
public function getBaseTagWithFormData()
{
return array(
array('https://base.com/', 'link/', 'https://base.com/link/', 'https://base.com/link/', '<base> tag does work with a path and relative form action'),
array('/basepath', '/registration', 'http://domain.com/registration', 'http://domain.com/registration', '<base> tag does work with a path and form action'),
array('/basepath', '', 'http://domain.com/registration', 'http://domain.com/registration', '<base> tag does work with a path and empty form action'),
array('http://base.com/', '/registration', 'http://base.com/registration', 'http://domain.com/registration', '<base> tag does work with a URL and form action'),
array('http://base.com', '', 'http://domain.com/path/form', 'http://domain.com/path/form', '<base> 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', '<base> tag does work with a URL and form action'),
);
}