8000 feature #28035 [DomCrawler] Allow using non-absolute base URIs (javie… · symfony/symfony@924f7f9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 924f7f9

Browse files
committed
feature #28035 [DomCrawler] Allow using non-absolute base URIs (javiereguiluz)
This PR was squashed before being merged into the 4.2-dev branch (closes #28035). Discussion ---------- [DomCrawler] Allow using non-absolute base URIs | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #12318 | License | MIT | Doc PR | - @xabbuh @stof I implemented in this PR your comments from #12318 Commits ------- 130119f [DomCrawler] Allow using non-absolute base URIs
2 parents fbe4bc1 + 130119f commit 924f7f9

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

src/Symfony/Component/DomCrawler/AbstractUriElement.php

Lines changed: 7 additions & 5 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
/**

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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,27 @@ public function testConstructorWithANonImgTag()
2727
new Image($dom->getElementsByTagName('div')->item(0), 'http://www.example.com/');
2828
}
2929

30+
public function testBaseUriIsOptionalWhenImageUrlIsAbsolute()
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('https://example.com/foo', $image->getUri());
37+
}
38+
39+
/**
40+
* @expectedException \InvalidArgumentException
41+
*/
42+
public function testAbsoluteBaseUriIsMandatoryWhenImageUrlIsRelative()
43+
{
44+
$dom = new \DOMDocument();
45+
$dom->loadHTML('<html><img alt="foo" src="/foo" /></html>');
46+
47+
$image = new Image($dom->getElementsByTagName('img')->item(0), 'example.com');
48+
$image->getUri();
49+
}
50+
3051
/**
3152
* @dataProvider getGetUriTests
3253
*/

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

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

30+
public function testBaseUriIsOptionalWhenLinkUrlIsAbsolute()
31+
{
32+
$dom = new \DOMDocument();
33+
$dom->loadHTML('<html><a href="https://example.com/foo">foo</a></html>');
34+
35+
$link = new Link($dom->getElementsByTagName('a')->item(0));
36+
$this->assertSame('https://example.com/foo', $link->getUri());
37+
}
38+
3039
/**
3140
* @expectedException \InvalidArgumentException
3241
*/
33-
public function testConstructorWithAnInvalidCurrentUri()
42+
public function testAbsoluteBaseUriIsMandatoryWhenLinkUrlIsRelative()
3443
{
3544
$dom = new \DOMDocument();
3645
$dom->loadHTML('<html><a href="/foo">foo</a></html>');
3746

38-
new Link($dom->getElementsByTagName('a')->item(0), 'example.com');
47+
$link = new Link($dom->getElementsByTagName('a')->item(0), 'example.com');
48+
$link->getUri();
3949
}
4050

4151
public function testGetNode()

0 commit comments

Comments
 (0)
0