-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DomCrawler] [2.7] Abstract URI logic and crawl images #13649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DomCrawler] [2.7] Abstract URI logic and crawl images #13649
Conversation
The tests pass on my side BTW, I don't know what happened with Travis. |
👍 ping @jakzal |
@valeriangalliat would you mind rebasing? |
All the URI parsing logic is externalized in the AbstractUriElement class, implementing the UriElementInterface interface. The AbstractUriElement class have two abstract methods: * setNode: validate the DOMElement node according to the concrete class rules, and set $this->node. * getRawUri: get the raw URI from $this->node. The Link classs now extends AbstractUriElement. This refactor is desirable for #12429.
Just rebased. Had a conflict with I'm waiting for the Travis tests since it would take too much time where I am to fetch all the development dependencies to run the tests, and I have nothing up-to-date in cache. |
*/ | ||
public function selectImage($value) | ||
{ | ||
$xpath = sprintf('descendant-or-self::img[contains(concat(\' \', normalize-space(string(@alt)), \' \'), %s)]', static::xpathLiteral(' '.$value.' ')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't need this concatenation stuff at all for the alt attribute. Adding a space at the beginning and the end of the class attribute and around the value is the simple way to search elements with a given class in the space separated list. the alt attribute is not a space separated list in HTML
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh okay, thank you. I'm updating this.
A new Image class is added, extending AbstractUriElement, to leverage URI methods for the HTML img src attribute. Two methods are added to the Crawler class, image and images, that are the equivalent of link and links for images.
I just amended my commit with the following changes: diff --git a/src/Symfony/Component/DomCrawler/Crawler.php b/src/Symfony/Component/DomCrawler/Crawler.php
index 1352c74..493d205 100644
--- a/src/Symfony/Component/DomCrawler/Crawler.php
+++ b/src/Symfony/Component/DomCrawler/Crawler.php
@@ -702,7 +702,7 @@ class Crawler extends \SplObjectStorage
*/
public function selectImage($value)
{
- $xpath = sprintf('descendant-or-self::img[contains(normalize-space(string(@alt)), %s)]', static::xpathLiteral(' '.$value.' '));
+ $xpath = sprintf('descendant-or-self::img[contains(normalize-space(string(@alt)), %s)]', static::xpathLiteral($value));
return $this->filterRelativeXPath($xpath);
}
diff --git a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
index 97f256d..b658ff1 100755
--- a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
+++ b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
@@ -679,8 +679,8 @@ EOF
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->selectImage() returns a new instance of a crawler');
$this->assertCount(1, $crawler->selectImage('Fabien\'s Bar'), '->selectImage() selects images by alt attribute');
- $this->assertCount(2, $crawler->selectLink('Fabien"s Bar'), '->selectImage() selects images by alt attribute');
- $this->assertCount(1, $crawler->selectLink('\' Fabien"s Bar'), '->selectImage() selects images by alt attribute');
+ $this->assertCount(2, $crawler->selectImage('Fabien"s Bar'), '->selectImage() selects images by alt attribute');
+ $this->assertCount(1, $crawler->selectImage('\' Fabien"s Bar'), '->selectImage() selects images by alt attribute');
}
public function testSelectButton()
@@ -766,8 +766,6 @@ HTML;
$crawler = $this->createTestCrawler('http://example.com/bar/')->selectImage('Bar');
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Image', $crawler->image(), '->image() returns an Image instance');
- $this->assertEquals('POST', $crawler->image('post')->getMethod(), '->image() takes a method as its argument');
-
try {
$this->createTestCrawler()->filterXPath('//ol')->image();
$this->fail('->image() throws an \InvalidArgumentException if the node list is empty');
DomCrawler tests passes on my side now. |
* | ||
* @author Fabien Potencier <[email protected]> | ||
* | ||
* @api |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All @api
tags must be removed, we don't use them anymore.
👍 to be merged in master/3.1. |
@valeriangalliat Can you submit a PR on master? |
…riangalliat) This PR was squashed before being merged into the 3.1-dev branch (closes #17585). Discussion ---------- [DomCrawler] Abstract URI logic and crawl images | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #12429 | License | MIT | Doc PR | symfony/symfony-docs#4971 This is a backward-compatible version of #13620, and a rebase of #13649 on current `master`. Commits ------- 1553b07 [DomCrawler] Abstract URI logic and crawl images
This PR was merged into the master branch. Discussion ---------- [DomCrawler] Document images crawler | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes (symfony/symfony#13649, symfony/symfony#13650) | Applies to | 3.0.0 | Fixed tickets | symfony/symfony#12429 Commits ------- 355a83f [DomCrawler] Document images crawler
This is a backward-compatible version of #13620.