8000 Rebased the change into a feature of master · symfony/symfony@fdc7f29 · GitHub
[go: up one dir, main page]

Skip to content

Commit fdc7f29

Browse files
committed
Rebased the change into a feature of master
1 parent 15950ec commit fdc7f29

File tree

5 files changed

+56
-18
lines changed

5 files changed

+56
-18
lines changed

src/Symfony/Component/DomCrawler/AbstractUriElement.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,17 @@ abstract class AbstractUriElement
4040
*
4141
* @throws \InvalidArgumentException if the node is not a link
4242
*/
43-
public function __construct(\DOMElement $node, string $currentUri, ?string $method = 'GET')
43+
public function __construct(\DOMElement $node, string $currentUri = null, ?string $method = 'GET')
4444
{
45-
if (!\in_array(strtolower(substr($currentUri, 0, 4)), array('http', 'file'))) {
46-
throw new \InvalidArgumentException(sprintf('Current URI must be an absolute URL ("%s").', $currentUri));
47-
}
48-
4945
$this->setNode($node);
5046
$this->method = $method ? strtoupper($method) : null;
5147
$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+
}
5254
}
5355

5456
/**
@@ -114,7 +116,7 @@ public function getUri()
114116
}
115117

116118
// relative path
117-
$path = parse_url(substr($this->currentUri, \strlen($baseUri)), PHP_URL_PATH);
119+
$path = parse_url(substr($this->currentUri, strlen($baseUri)), PHP_URL_PATH);
118120
$path = $this->canonicalizePath(substr($path, 0, strrpos($path, '/')).'/'.$uri);
119121

120122
return $baseUri.('' === $path || '/' !== $path[0] ? '/' : '').$path;

src/Symfony/Component/DomCrawler/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
4.2.0
5+
-----
6+
7+
* The `$currentUri` constructor argument of the `AbstractUriElement`, `Link` and
8+
`Image` classes is now optional.
9+
410
3.1.0
511
-----
612

src/Symfony/Component/DomCrawler/Image.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
class Image extends AbstractUriElement
1818
{
19-
public function __construct(\DOMElement $node, string $currentUri)
19+
public function __construct(\DOMElement $node, string $currentUri = null)
2020
{
2121
parent::__construct($node, $currentUri, 'GET');
2222
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,36 @@ 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()
40+
{
41+
$dom = new \DOMDocument();
42+
$dom->loadHTML('<html><img alt="foo" src="https://example.com/foo" /></html>');
43+
44+
$image = new Image($dom->getElementsByTagName('img')->item(0));
45+
$this->assertSame('https://example.com/foo', $image->getUri());
46+
}
47+
48+
/**
49+
* @expectedException \InvalidArgumentException
50+
*/
51+
public function testBaseUriMustBeAnAbsoluteUrlWhenImageUrlIsRelative()
52+
{
53+
$dom = new \DOMDocument();
54+
$dom->loadHTML('<html><img alt="foo" src="/foo" /></html>');
55+
56+
$image = new Image($dom->getElementsByTagName('img')->item(0), 'example.com');
57+
$image->getUri();
58+
}
59+
3060
/**
3161
* @dataProvider getGetUriTests
3262
*/

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

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

30-
public function testBaseUriIsOptionaInConstructor()
30+
public function testBaseUriIsOptional()
3131
{
3232
$dom = new \DOMDocument();
3333
$dom->loadHTML('<html><a href="/foo">foo</a></html>');
@@ -36,10 +36,19 @@ public function testBaseUriIsOptionaInConstructor()
3636
$this->assertSame('foo', $link->getNode()->nodeValue);
3737
}
3838

39+
public function testBaseUriIsOptionaIfLinkContainsAbsoluteUrl()
40+
{
41+
$dom = new \DOMDocument();
42+
$dom->loadHTML('<html><a href="https://example.com/foo">foo</a></html>');
43+
44+
$link = new Link($dom->getElementsByTagName('a')->item(0));
45+
$this->assertSame('https://example.com/foo', $link->getUri());
46+
}
47+
3948
/**
4049
* @expectedException \InvalidArgumentException
4150
*/
42-
public function testGetUriWithRelativeLinkUriAndNonAbsoluteBaseUri()
51+
public function testBaseUriMustBeAnAbsoluteUrlWhenLinkUrlIsRelative()
4352
{
4453
$dom = new \DOMDocument();
4554
$dom->loadHTML('<html><a href="/foo">foo</a></html>');
@@ -48,15 +57,6 @@ public function testGetUriWithRelativeLinkUriAndNonAbsoluteBaseUri()
4857
$link->getUri();
4958
}
5059

51-
public function testGetUriWithAbsoluteLinkUri()
52-
{
53-
$dom = new \DOMDocument();
54-
$dom->loadHTML('<html><a href="https://example.com/foo">foo</a></html>');
55-
56-
$link = new Link($dom->getElementsByTagName('a')->item(0));
57-
$this->assertSame('https://example.com/foo', $link->getUri());
58-
}
59-
6060
public function testGetNode()
6161
{
6262
$dom = new \DOMDocument();

0 commit comments

Comments
 (0)
0