Skip to content

Commit

Permalink
[DomCrawler] Remove the query string and the anchor of the uri of a link
Browse files Browse the repository at this point in the history
  • Loading branch information
benja-M-1 authored and stof committed Jul 3, 2014
1 parent eeeae94 commit fe5d2d1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
67 changes: 50 additions & 17 deletions src/Symfony/Component/DomCrawler/Link.php
Expand Up @@ -98,34 +98,23 @@ public function getUri()
return $this->currentUri;
}

// only an anchor
// an anchor
if ('#' === $uri[0]) {
$baseUri = $this->currentUri;
if (false !== $pos = strpos($baseUri, '#')) {
$baseUri = substr($baseUri, 0, $pos);
}

return $baseUri.$uri;
return $this->cleanupAnchor($this->currentUri).$uri;
}

// only a query string
if ('?' === $uri[0]) {
$baseUri = $this->currentUri;

// remove the query string from the current URI
if (false !== $pos = strpos($baseUri, '?')) {
$baseUri = substr($baseUri, 0, $pos);
}
$baseUri = $this->cleanupUri($this->currentUri);

if ('?' === $uri[0]) {
return $baseUri.$uri;
}

// absolute URL with relative schema
if (0 === strpos($uri, '//')) {
return preg_replace('#^([^/]*)//.*$#', '$1', $this->currentUri).$uri;
return preg_replace('#^([^/]*)//.*$#', '$1', $baseUri).$uri;
}

$baseUri = preg_replace('#^(.*?//[^/]*)(?:\/.*)?$#', '$1', $this->currentUri);
$baseUri = preg_replace('#^(.*?//[^/]*)(?:\/.*)?$#', '$1', $baseUri);

// absolute path
if ('/' === $uri[0]) {
Expand Down Expand Up @@ -194,4 +183,48 @@ protected function setNode(\DOMNode $node)

$this->node = $node;
}

/**
* Removes the query string and the anchor from the given uri.
*
* @param string $uri The uri to clean
*
* @return string
*/
private function cleanupUri($uri)
{
return $this->cleanupQuery($this->cleanupAnchor($uri));
}

/**
* Remove the query string from the uri.
*
* @param $uri
*
* @return array
*/
private function cleanupQuery($uri)
{
if (false !== $pos = strpos($uri, '?')) {
return substr($uri, 0, $pos);
}

return $uri;
}

/**
* Remove the anchor from the uri.
*
* @param $uri
*
* @return string
*/
private function cleanupAnchor($uri)
{
if (false !== $pos = strpos($uri, '#')) {
return substr($uri, 0, $pos);
}

return $uri;
}
}
4 changes: 4 additions & 0 deletions src/Symfony/Component/DomCrawler/Tests/LinkTest.php
Expand Up @@ -101,7 +101,9 @@ public function getGetUriTests()

array('', 'http://localhost/bar/', 'http://localhost/bar/'),
array('#', 'http://localhost/bar/', 'http://localhost/bar/#'),
array('#bar', 'http://localhost/bar?a=b', 'http://localhost/bar?a=b#bar'),
array('#bar', 'http://localhost/bar/#foo', 'http://localhost/bar/#bar'),
array('?a=b', 'http://localhost/bar#foo', 'http://localhost/bar?a=b'),
array('?a=b', 'http://localhost/bar/', 'http://localhost/bar/?a=b'),

array('http://login.foo.com/foo', 'http://localhost/bar/', 'http://login.foo.com/foo'),
Expand Down Expand Up @@ -135,6 +137,8 @@ public function getGetUriTests()
array('../../', 'http://localhost/', 'http://localhost/'),
array('../../', 'http://localhost', 'http://localhost/'),

array('/foo', 'http://localhost?bar=1', 'http://localhost/foo'),
array('/foo', 'http://localhost#bar', 'http://localhost/foo'),
array('/foo', 'file:///', 'file:///foo'),
array('/foo', 'file:///bar/baz', 'file:///foo'),
array('foo', 'file:///', 'file:///foo'),
Expand Down

0 comments on commit fe5d2d1

Please sign in to comment.