From fe5d2d1554097861cb5535ee70595bd1e2ba6530 Mon Sep 17 00:00:00 2001 From: Benjamin Grandfond Date: Sat, 21 Jun 2014 22:15:52 +0200 Subject: [PATCH] [DomCrawler] Remove the query string and the anchor of the uri of a link --- src/Symfony/Component/DomCrawler/Link.php | 67 ++++++++++++++----- .../Component/DomCrawler/Tests/LinkTest.php | 4 ++ 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/Symfony/Component/DomCrawler/Link.php b/src/Symfony/Component/DomCrawler/Link.php index 05d5e927a2..57cf2b4f21 100644 --- a/src/Symfony/Component/DomCrawler/Link.php +++ b/src/Symfony/Component/DomCrawler/Link.php @@ -98,34 +98,23 @@ class Link 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 + $baseUri = $this->cleanupUri($this->currentUri); + if ('?' === $uri[0]) { - $baseUri = $this->currentUri; - - // remove the query string from the current URI - if (false !== $pos = strpos($baseUri, '?')) { - $baseUri = substr($baseUri, 0, $pos); - } - 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]) { @@ -194,4 +183,48 @@ class Link $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; + } } diff --git a/src/Symfony/Component/DomCrawler/Tests/LinkTest.php b/src/Symfony/Component/DomCrawler/Tests/LinkTest.php index a17142830f..941e4b2f6d 100644 --- a/src/Symfony/Component/DomCrawler/Tests/LinkTest.php +++ b/src/Symfony/Component/DomCrawler/Tests/LinkTest.php @@ -101,7 +101,9 @@ class LinkTest extends \PHPUnit_Framework_TestCase 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'), @@ -135,6 +137,8 @@ class LinkTest extends \PHPUnit_Framework_TestCase 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'),