Skip to content

Commit 6d3f124

Browse files
committed
bug symfony#13794 [DomCrawler] Invalid uri created from forms if base tag present (danez)
This PR was squashed before being merged into the 2.3 branch (closes symfony#13794). Discussion ---------- [DomCrawler] Invalid uri created from forms if base tag present | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Since symfony#13145 was merged the provided testcase does not work anymore and creates an invalid link This affects the latest versions of 2.3, 2.5, 2.6 and 2.7 Right now this PR only includes the failing test, as I could no easy find a fix. If someone immediately knows whats the problem, give me a hint please. Commits ------- dc57a7a [DomCrawler] Invalid uri created from forms if base tag present
2 parents 3cf95ee + dc57a7a commit 6d3f124

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ public function form(array $values = null, $method = null)
755755
throw new \InvalidArgumentException('The current node list is empty.');
756756
}
757757

758-
$form = new Form($this->getNode(0), $this->uri, $method);
758+
$form = new Form($this->getNode(0), $this->uri, $method, $this->baseHref);
759759

760760
if (null !== $values) {
761761
$form->setValues($values);

src/Symfony/Component/DomCrawler/Form.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,27 @@ class Form extends Link implements \ArrayAccess
3333
*/
3434
private $fields;
3535

36+
/**
37+
* @var string
38+
*/
39+
private $baseHref;
40+
3641
/**
3742
* Constructor.
3843
*
3944
* @param \DOMNode $node A \DOMNode instance
4045
* @param string $currentUri The URI of the page where the form is embedded
4146
* @param string $method The method to use for the link (if null, it defaults to the method defined by the form)
47+
* @param string $baseHref The URI of the <base> used for relative links, but not for empty action
4248
*
4349
* @throws \LogicException if the node is not a button inside a form tag
4450
*
4551
* @api
4652
*/
47-
public function __construct(\DOMNode $node, $currentUri, $method = null)
53+
public function __construct(\DOMNode $node, $currentUri, $method = null, $baseHref = null)
4854
{
4955
parent::__construct($node, $currentUri, $method);
56+
$this->baseHref = $baseHref;
5057

5158
$this->initialize();
5259
}
@@ -442,6 +449,10 @@ private function initialize()
442449
$this->addField($node);
443450
}
444451
}
452+
453+
if ($this->baseHref && '' !== $this->node->getAttribute('action')) {
454+
$this->currentUri = $this->baseHref;
455+
}
445456
}
446457

447458
private function addField(\DOMNode $node)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,9 +853,12 @@ public function testBaseTagWithForm($baseValue, $actionValue, $expectedUri, $cur
853853
public function getBaseTagWithFormData()
854854
{
855855
return array(
856+
array('https://base.com/', 'link/', 'https://base.com/link/', 'https://base.com/link/', '<base> tag does work with a path and relative form action'),
856857
array('/basepath', '/registration', 'http://domain.com/registration', 'http://domain.com/registration', '<base> tag does work with a path and form action'),
857858
array('/basepath', '', 'http://domain.com/registration', 'http://domain.com/registration', '<base> tag does work with a path and empty form action'),
859+
array('http://base.com/', '/registration', 'http://base.com/registration', 'http://domain.com/registration', '<base> tag does work with a URL and form action'),
858860
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'),
861+
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'),
859862
);
860863
}
861864

0 commit comments

Comments
 (0)