From b1ea8e5b681cd76aa2e623f2d7b3f3d64d121fbf Mon Sep 17 00:00:00 2001 From: Matthijs van den Bos Date: Mon, 25 Feb 2013 14:25:47 +0100 Subject: [PATCH] Fixed handling absent href attribute in base tag The HTML5 spec states that the href attribute is optional for the base tag. Fixed the DomCrawler::addHtmlContent() method to support this See here and here: http://www.w3.org/TR/html-markup/base.html#base.attrs.href http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#the-base-element --- src/Symfony/Component/DomCrawler/Crawler.php | 5 +++-- .../Component/DomCrawler/Tests/CrawlerTest.php | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/DomCrawler/Crawler.php b/src/Symfony/Component/DomCrawler/Crawler.php index 3fc881b841..03c6effebc 100644 --- a/src/Symfony/Component/DomCrawler/Crawler.php +++ b/src/Symfony/Component/DomCrawler/Crawler.php @@ -145,8 +145,9 @@ class Crawler extends \SplObjectStorage $base = $this->filterXPath('descendant-or-self::base')->extract(array('href')); - if (count($base)) { - $this->uri = current($base); + $baseHref = current($base); + if (count($base) && !empty($baseHref)) { + $this->uri = $baseHref; } } diff --git a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php index 30401b9d75..8aac5d082f 100644 --- a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php +++ b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php @@ -80,6 +80,18 @@ class CrawlerTest extends \PHPUnit_Framework_TestCase $this->assertEquals('Tiếng Việt', $crawler->filterXPath('//div')->text()); } + /** + * @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent + */ + public function testAddHtmlContentInvalidBaseTag() + { + $crawler = new Crawler(null, 'http://symfony.com'); + + $crawler->addHtmlContent('', 'UTF-8'); + + $this->assertEquals('http://symfony.com/contact', current($crawler->filterXPath('//a')->links())->getUri(), '->addHtmlContent() correctly handles a non-existent base tag href attribute'); + } + /** * @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent */