Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/Symfony/Component/DomCrawler/AbstractUriElement.php
Original file line number Diff line number Diff line change
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'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use strpos() here instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same code as in the older branches, so we know it works and I'd prefer to not change it 😅

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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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