8000 Final refactor · symfony/symfony@a58b301 · GitHub
[go: up one dir, main page]

Skip to content

Commit a58b301

Browse files
committed
Final refactor
1 parent 3f6d709 commit a58b301

File tree

3 files changed

+10
-26
lines changed

3 files changed

+10
-26
lines changed

src/Symfony/Component/DomCrawler/AbstractUriElement.php

Lines changed: 6 additions & 4 deletions
< 10000 th scope="col">Diff line change
Original file line numberDiff line number
@@ -45,6 +45,12 @@ public function __construct(\DOMElement $node, string $currentUri = null, ?strin
4545
$this->setNode($node);
4646
$this->method = $method ? strtoupper($method) : null;
4747
$this->currentUri = $currentUri;
48+
49+
$elementUriIsRelative = null === parse_url(trim($this->getRawUri()), PHP_URL_SCHEME);
50+
$baseUriIsAbsolute = in_array(strtolower(substr($this->currentUri, 0, 4)), array('http', 'file'));
51+
if ($elementUriIsRelative && !$baseUriIsAbsolute) {
52+
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));
53+
}
4854
}
4955

5056
/**
@@ -81,10 +87,6 @@ public function getUri()
8187
return $uri;
8288
}
8389

84-
if (!in_array(strtolower(substr($this->currentUri, 0, 4)), array('http', 'file'))) {
85-
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));
86-
}
87-
8890
// empty URI
8991
if (!$uri) {
9092
return $this->currentUri;

src/Symfony/Component/DomCrawler/Tests/ImageTest.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,7 @@ public function testConstructorWithANonImgTag()
2727
new Image($dom->getElementsByTagName('div')->item(0), 'http://www.example.com/');
2828
}
2929

30-
public function testBaseUriIsOptional()
31-
{
32-
$dom = new \DOMDocument();
33-
$dom->loadHTML('<html><img alt="foo" src="https://example.com/foo" /></html>');
34-
35-
$image = new Image($dom->getElementsByTagName('img')->item(0));
36-
$this->assertSame('foo', $image->getNode()->getAttribute('alt'));
37-
}
38-
39-
public function testBaseUriIsOptionaIfImageContainsAbsoluteUrl()
30+
public function testBaseUriIsOptionalWhenImageUrlIsAbsolute()
4031
{
4132
$dom = new \DOMDocument();
4233
$dom->loadHTML('<html><img alt="foo" src="https://example.com/foo" /></html>');
@@ -48,7 +39,7 @@ public function testBaseUriIsOptionaIfImageContainsAbsoluteUrl()
4839
/**
4940
* @expectedException \InvalidArgumentException
5041
*/
51-
public function testBaseUriMustBeAnAbsoluteUrlWhenImageUrlIsRelative()
42+
public function testAbsoluteBaseUriIsMandatoryWhenImageUrlIsRelative()
5243
{
5344
$dom = new \DOMDocument();
5445
$dom->loadHTML('<html><img alt="foo" src="/foo" /></html>');

src/Symfony/Component/DomCrawler/Tests/LinkTest.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,7 @@ public function testConstructorWithANonATag()
2727
new Link($dom->getElementsByTagName('div')->item(0), 'http://www.example.com/');
2828
}
2929

30-
public function testBaseUriIsOptional()
31-
{
32-
$dom = new \DOMDocument();
33-
$dom->loadHTML('<html><a href="/foo">foo</a></html>');
34-
35-
$link = new Link($dom->getElementsByTagName('a')->item(0));
36-
$this->assertSame('foo', $link->getNode()->nodeValue);
37-
}
38-
39-
public function testBaseUriIsOptionaIfLinkContainsAbsoluteUrl()
30+
public function testBaseUriIsOptionalWhenLinkUrlIsAbsolute()
4031
{
4132
$dom = new \DOMDocument();
4233
$dom->loadHTML('<html><a href="https://example.com/foo">foo</a></html>');
@@ -48,7 +39,7 @@ public function testBaseUriIsOptionaIfLinkContainsAbsoluteUrl()
4839
/**
4940
* @expectedException \InvalidArgumentException
5041
*/
51-
public function testBaseUriMustBeAnAbsoluteUrlWhenLinkUrlIsRelative()
42+
public function testAbsoluteBaseUriIsMandatoryWhenLinkUrlIsRelative()
5243
{
5344
$dom = new \DOMDocument();
5445
$dom->loadHTML('<html><a href="/foo">foo</a></html>');

0 commit comments

Comments
 (0)
0