Skip to content

Commit 8ccebc4

Browse files
committed
[DomCrawler] fixed Link::getUri() method for anchors
1 parent 88c8119 commit 8ccebc4

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/Symfony/Component/DomCrawler/Link.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,21 @@ public function getUri()
9191

9292
// only an anchor
9393
if ('#' === $uri[0]) {
94-
return $this->currentUri.$uri;
94+
$baseUri = $this->currentUri;
95+
if (false !== $pos = strpos($baseUri, '#')) {
96+
$baseUri = substr($baseUri, 0, $pos);
97+
}
98+
99+
return $baseUri.$uri;
95100
}
96101

97102
// only a query string
98-
if ('?' === $uri[0] ) {
103+
if ('?' === $uri[0]) {
99104
$baseUri = $this->currentUri;
100105

101106
// remove the query string from the current uri
102-
if (false !== ($pos = strpos($this->currentUri, '?'))) {
103-
$baseUri = substr($this->currentUri, 0, strpos($this->currentUri, '?'));
107+
if (false !== $pos = strpos($baseUri, '?')) {
108+
$baseUri = substr($baseUri, 0, $pos);
104109
}
105110

106111
return $baseUri.$uri;

tests/Symfony/Tests/Component/DomCrawler/LinkTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public function getGetUriTests()
8585

8686
array('', 'http://localhost/bar/', 'http://localhost/bar/'),
8787
array('#', 'http://localhost/bar/', 'http://localhost/bar/#'),
88+
array('#bar', 'http://localhost/bar/#foo', 'http://localhost/bar/#bar'),
8889
array('?a=b', 'http://localhost/bar/', 'http://localhost/bar/?a=b'),
8990

9091
array('http://login.foo.com/foo', 'http://localhost/bar/', 'http://login.foo.com/foo'),
@@ -93,6 +94,7 @@ public function getGetUriTests()
9394
array('?foo=2', 'http://localhost/?foo=1', 'http://localhost/?foo=2'),
9495
array('?foo=2', 'http://localhost/bar?foo=1', 'http://localhost/bar?foo=2'),
9596
array('?foo=2', 'http://localhost/bar/?foo=1', 'http://localhost/bar/?foo=2'),
97+
array('?bar=2', 'http://localhost?foo=1', 'http://localhost?bar=2'),
9698
);
9799
}
98100
}

0 commit comments

Comments
 (0)