bug #11194 [DomCrawler] Remove the query string and the anchor of the uri of a link (benja-M-1)
This PR was squashed before being merged into the 2.3 branch (closes #11194).
Discussion
----------
[DomCrawler] Remove the query string and the anchor of the uri of a link
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | ~
| License | MIT
| Doc PR | ~
Commits
-------
fe5d2d1
[DomCrawler] Remove the query string and the anchor of the uri of a link
This commit is contained in:
commit
38be14dd14
@ -98,34 +98,23 @@ class Link
|
|||||||
return $this->currentUri;
|
return $this->currentUri;
|
||||||
}
|
}
|
||||||
|
|
||||||
// only an anchor
|
// an anchor
|
||||||
if ('#' === $uri[0]) {
|
if ('#' === $uri[0]) {
|
||||||
$baseUri = $this->currentUri;
|
return $this->cleanupAnchor($this->currentUri).$uri;
|
||||||
if (false !== $pos = strpos($baseUri, '#')) {
|
|
||||||
$baseUri = substr($baseUri, 0, $pos);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $baseUri.$uri;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// only a query string
|
$baseUri = $this->cleanupUri($this->currentUri);
|
||||||
|
|
||||||
if ('?' === $uri[0]) {
|
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;
|
return $baseUri.$uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
// absolute URL with relative schema
|
// absolute URL with relative schema
|
||||||
if (0 === strpos($uri, '//')) {
|
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
|
// absolute path
|
||||||
if ('/' === $uri[0]) {
|
if ('/' === $uri[0]) {
|
||||||
@ -194,4 +183,48 @@ class Link
|
|||||||
|
|
||||||
$this->node = $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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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('#', '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('#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('?a=b', 'http://localhost/bar/', 'http://localhost/bar/?a=b'),
|
||||||
|
|
||||||
array('http://login.foo.com/foo', 'http://localhost/bar/', 'http://login.foo.com/foo'),
|
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('../../', '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:///', 'file:///foo'),
|
||||||
array('/foo', 'file:///bar/baz', 'file:///foo'),
|
array('/foo', 'file:///bar/baz', 'file:///foo'),
|
||||||
array('foo', 'file:///', 'file:///foo'),
|
array('foo', 'file:///', 'file:///foo'),
|
||||||
|
Reference in New Issue
Block a user