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
This commit is contained in:
Matthijs van den Bos 2013-02-25 14:25:47 +01:00 committed by Fabien Potencier
parent ae5b94fd7c
commit b1ea8e5b68
2 changed files with 15 additions and 2 deletions

View File

@ -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;
}
}

View File

@ -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('<html><head><base target="_top"></head><a href="/contact"></a></html>', '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
*/