Skip to content

Commit

Permalink
[DomCrawler] Allow using non-absolute base URIs
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz authored and fabpot committed Aug 2, 2018
1 parent f834c92 commit 130119f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
12 changes: 7 additions & 5 deletions src/Symfony/Component/DomCrawler/AbstractUriElement.php
Expand Up @@ -40,15 +40,17 @@ abstract class AbstractUriElement
*
* @throws \InvalidArgumentException if the node is not a link
*/
public function __construct(\DOMElement $node, string $currentUri, ?string $method = 'GET')
public function __construct(\DOMElement $node, string $currentUri = null, ?string $method = 'GET')
{
if (!\in_array(strtolower(substr($currentUri, 0, 4)), array('http', 'file'))) {
throw new \InvalidArgumentException(sprintf('Current URI must be an absolute URL ("%s").', $currentUri));
}

$this->setNode($node);
$this->method = $method ? strtoupper($method) : null;
$this->currentUri = $currentUri;

$elementUriIsRelative = null === parse_url(trim($this->getRawUri()), PHP_URL_SCHEME);
$baseUriIsAbsolute = \in_array(strtolower(substr($this->currentUri, 0, 4)), array('http', 'file'));
if ($elementUriIsRelative && !$baseUriIsAbsolute) {
throw new \InvalidArgumentException(sprintf('The URL of the element is relative, so you must define its base URI passing an absolute URL to the constructor of the %s class ("%s" was passed).', __CLASS__, $this->currentUri));
}
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/DomCrawler/CHANGELOG.md
@@ -1,6 +1,12 @@
CHANGELOG
=========

4.2.0
-----

* The `$currentUri` constructor argument of the `AbstractUriElement`, `Link` and
`Image` classes is now optional.

3.1.0
-----

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/DomCrawler/Image.php
Expand Up @@ -16,7 +16,7 @@
*/
class Image extends AbstractUriElement
{
public function __construct(\DOMElement $node, string $currentUri)
public function __construct(\DOMElement $node, string $currentUri = null)
{
parent::__construct($node, $currentUri, 'GET');
}
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/DomCrawler/Tests/ImageTest.php
Expand Up @@ -27,6 +27,27 @@ public function testConstructorWithANonImgTag()
new Image($dom->getElementsByTagName('div')->item(0), 'http://www.example.com/');
}

public function testBaseUriIsOptionalWhenImageUrlIsAbsolute()
{
$dom = new \DOMDocument();
$dom->loadHTML('<html><img alt="foo" src="https://example.com/foo" /></html>');

$image = new Image($dom->getElementsByTagName('img')->item(0));
$this->assertSame('https://example.com/foo', $image->getUri());
}

/**
* @expectedException \InvalidArgumentException
*/
public function testAbsoluteBaseUriIsMandatoryWhenImageUrlIsRelative()
{
$dom = new \DOMDocument();
$dom->loadHTML('<html><img alt="foo" src="/foo" /></html>');

$image = new Image($dom->getElementsByTagName('img')->item(0), 'example.com');
$image->getUri();
}

/**
* @dataProvider getGetUriTests
*/
Expand Down
14 changes: 12 additions & 2 deletions src/Symfony/Component/DomCrawler/Tests/LinkTest.php
Expand Up @@ -27,15 +27,25 @@ public function testConstructorWithANonATag()
new Link($dom->getElementsByTagName('div')->item(0), 'http://www.example.com/');
}

public function testBaseUriIsOptionalWhenLinkUrlIsAbsolute()
{
$dom = new \DOMDocument();
$dom->loadHTML('<html><a href="https://example.com/foo">foo</a></html>');

$link = new Link($dom->getElementsByTagName('a')->item(0));
$this->assertSame('https://example.com/foo', $link->getUri());
}

/**
* @expectedException \InvalidArgumentException
*/
public function testConstructorWithAnInvalidCurrentUri()
public function testAbsoluteBaseUriIsMandatoryWhenLinkUrlIsRelative()
{
$dom = new \DOMDocument();
$dom->loadHTML('<html><a href="/foo">foo</a></html>');

new Link($dom->getElementsByTagName('a')->item(0), 'example.com');
$link = new Link($dom->getElementsByTagName('a')->item(0), 'example.com');
$link->getUri();
}

public function testGetNode()
Expand Down

0 comments on commit 130119f

Please sign in to comment.