Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Closed
wants to merge 2 commits into from
Closed

[DomCrawler] [2.7] Abstract URI logic and crawl images #13649

wants to merge 2 commits into from

Conversation

valeriangalliat
Copy link
Contributor

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.

@valeriangalliat
Copy link
Contributor Author

The tests pass on my side BTW, I don't know what happened with Travis.

@fabpot
Copy link
Member

fabpot commented Mar 27, 2015

👍 ping @jakzal

@jakzal
Copy link
Contributor

jakzal commented Mar 27, 2015

@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.
@valeriangalliat
Copy link
Contributor Author

Just rebased. Had a conflict with Link.php, I ported the changes in AbstractUriElement.php since it has been renamed in the PR.

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.' '));
Copy link
Member

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

Copy link
Contributor Author

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.
@valeriangalliat
Copy link
Contributor Author

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');
  1. Fix the change I made following this comment (I could not run tests the other day).
  2. Fix selectImage tests (I originally forgot to rename two selectLink)…
  3. Remove the image method test since it was removed after this comment.

DomCrawler tests passes on my side now.

*
* @author Fabien Potencier <[email protected]>
*
* @api
Copy link
Member

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.

@fabpot
Copy link
Member

fabpot commented Jan 25, 2016

👍 to be merged in master/3.1.

@fabpot
Copy link
Member

fabpot commented Jan 25, 2016

@valeriangalliat Can you submit a PR on master?

@valeriangalliat
Copy link
Contributor Author

@fabpot Thanks, PR on master is #17585

@fabpot fabpot closed this Jan 28, 2016
jakzal added a commit that referenced this pull request Feb 4, 2016
…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
xabbuh added a commit to symfony/symfony-docs that referenced this pull request Mar 10, 2016
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants